ECDH accepts peer keys whose explicit curve parameters are malformed (order = 0, cofactor = n, no cofactor)

未關閉
#390 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
76/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
cryptography

研究方向

Start with ECDH.load_received_public_key_der() and the repro_pyecdsa_ecdh_params.py reproducer using the named Wycheproof Brainpool vectors. Trace where explicit curve parameters are matched to a known curve, then add validation for order and cofactor alongside regression tests for the listed invalid cases. The work is done when loading or exchanging rejects order 0, order 1, a missing cofactor, and a cofactor equal to the curve order.

由索引模型根據 Issue 內容生成。

描述

Versions: python-ecdsa 0.19.2 (latest release), CPython 3.13.15.

What happens

ECDH.load_received_public_key_der() accepts a SubjectPublicKeyInfo with explicit (unnamed) curve parameters that
are malformed, and generate_sharedsecret_bytes() then returns a secret. Wycheproof testvectors_v1
ecdh_brainpoolP256r1_test.json (sha256 8502c0259d85813d93cba5a664245b2d5ad1cdd3480a77337f3b377b8d63ccf4) marks these cases invalid:

tcId comment Wycheproof python-ecdsa 0.19.2
545 order = 0 invalid accepted
551 no cofactor invalid accepted
553 cofactor = n invalid accepted

Across the five ecdh_brainpool* files: 5 "order = 0", 3 "no cofactor" and 3 "cofactor = n" invalid cases accepted.
The secret returned equals the correct one for the named curve, so we found no wrong result; the problem is that
parameters which are not a valid group description are accepted. It looks as if the explicit parameters are matched
to a known curve without comparing the order and cofactor fields.

Reproducer

repro_pyecdsa_ecdh_params.py (below): for each test case with one of those comments, load the private key on
brainpoolP256r1, load the peer key from its DER, exchange. Run as python repro_pyecdsa_ecdh_params.py ecdh_brainpoolP256r1_test.json.

Suggested fix

When explicit parameters are parsed, require the order and (if present) the cofactor to equal the matched curve's,
and reject an order of 0 or 1 outright.

repro_pyecdsa_ecdh_params.py
"""Minimal reproducer: python-ecdsa's ECDH accepts a peer public key whose explicit curve parameters are malformed
(order = 0, no cofactor, cofactor = n), cases Wycheproof marks "invalid".

Input: Wycheproof ecdh_brainpoolP256r1_test.json (C2SP/wycheproof testvectors_v1). For each test case with one of those
comments: load our private key on the named curve, load the peer key from its DER, and run the exchange.
Expected: loading or exchanging raises an error.
"""
import json
import pathlib
import sys

import ecdsa
from ecdsa import ECDH, SigningKey
from ecdsa.curves import BRAINPOOLP256r1

d = json.loads(pathlib.Path(sys.argv[1]).read_text())
print("python-ecdsa", ecdsa.__version__)
hits = 0
for g in d["testGroups"]:
    for tc in g["tests"]:
        if tc.get("comment") not in ("order = 0", "no cofactor", "cofactor = n"):
            continue
        sk = SigningKey.from_secret_exponent(int(tc["private"], 16) % BRAINPOOLP256r1.order, curve=BRAINPOOLP256r1)
        ecdh = ECDH(curve=BRAINPOOLP256r1, private_key=sk)
        try:
            ecdh.load_received_public_key_der(bytes.fromhex(tc["public"]))
            secret = ecdh.generate_sharedsecret_bytes()
            hits += tc["result"] == "invalid"
            res = f"ACCEPTED, secret equals Wycheproof's value: {secret.hex() == tc['shared']}"
        except Exception as exc:  # noqa: BLE001
            res = f"rejected: {type(exc).__name__}: {exc}"
        print(f"  tcId {tc['tcId']:4d} '{tc['comment']}' (expected {tc['result']}) -> {res}")
print("REPRODUCED" if hits else "NOT REPRODUCED", f"({hits} invalid cases accepted)")
主要語言
Python
星號
975
分支
346
平均合併
26 分鐘
30 天內合併 PR
1

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

tlsfuzzer/python-ecdsa 的其他 Issue

查看 tlsfuzzer/python-ecdsa 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。