重新按英文命名,修改成相对路径

This commit is contained in:
lianjianwei2024
2026-10-11 10:52:01 +08:00
parent eea5d511dc
commit 9bb23ac13b
64 changed files with 913 additions and 1427 deletions

View File

@@ -0,0 +1,189 @@
# -*- coding: utf-8 -*-
"""验签单元测试:包内向量 + 全套负向(篡改/未知密钥/撤销/实例不匹配/v1/额外字段/
非法类型/CR-LF-NUL/Base64URL/canonical_sha256 不符/非 P-256 公钥)。"""
import hashlib
from datetime import timedelta
import pytest
from cryptography.hazmat.primitives.asymmetric import ec
from lic_helpers import (IID, IID2, NOW, canonical, cert_payload, envelope,
lease_payload, load_vectors, sign_b64)
from license_client.verify import (LICENSE_V2, LEASE_V2, VerifyError,
build_keyring, canonical_bytes,
verify_certificate, verify_lease)
# ------------------------------------------------------------ 包内随机测试向量
def test_package_vectors_canonical_matches():
"""先验规范化串本身:我们拼的字节必须和接入包给的 canonical 逐字节一致。"""
v = load_vectors()
for kind, version in (("license", LICENSE_V2), ("lease", LEASE_V2)):
assert canonical_bytes(v[kind]["payload"], version) == v[kind]["canonical"].encode("utf-8")
assert hashlib.sha256(v[kind]["canonical"].encode("utf-8")).hexdigest() \
== v[kind]["canonical_sha256"]
def test_package_vectors_verify():
"""用包内随机测试公钥过 license-v2 / lease-v2 验签(接入包要求的先行动作)。"""
v = load_vectors()
ring = build_keyring(v["pinned_keys"])
lic = {"version": LICENSE_V2,
"payload": v["license"]["payload"],
"signature": v["license"]["signature"],
"canonical_sha256": v["license"]["canonical_sha256"]}
assert verify_certificate(lic, IID, ring)["version"] == LICENSE_V2
lease = {"payload": v["lease"]["payload"],
"signature": v["lease"]["signature"],
"canonical_sha256": v["lease"]["canonical_sha256"],
"algorithm": "ECDSA-P256-SHA256", "next_heartbeat_seconds": 60}
assert verify_lease(lease, IID, ring)["state"] == "active"
# ------------------------------------------------------------ 正常路径
def test_roundtrip_cert_and_lease(priv_key, keyring):
p = cert_payload()
assert verify_certificate(envelope(p, priv_key, "cert"), IID, keyring) == p
p2 = lease_payload()
assert verify_lease(envelope(p2, priv_key, "lease"), IID, keyring) == p2
# ------------------------------------------------------------ 篡改与伪造
def test_tampered_payload_field(priv_key, keyring):
env = envelope(lease_payload(), priv_key, "lease")
env["payload"]["state"] = "frozen" # 换状态不换签名
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
def test_tampered_signature(priv_key, keyring):
env = envelope(lease_payload(), priv_key, "lease")
sig = env["signature"]
env["signature"] = ("A" if sig[0] != "A" else "B") + sig[1:]
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
def test_signed_by_foreign_key(keyring):
evil = ec.generate_private_key(ec.SECP256R1())
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(), evil, "lease"), IID, keyring)
def test_unknown_key_id(priv_key):
ring = build_keyring([]) # 空信任根
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(), priv_key, "lease"), IID, ring)
def test_revoked_key(priv_key):
from lic_helpers import make_keyring
ring = make_keyring(priv_key, revoked=True)
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(), priv_key, "lease"), IID, ring)
def test_instance_mismatch(priv_key, keyring):
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(iid=IID2), priv_key, "lease"), IID, keyring)
# ------------------------------------------------------------ 结构拒绝
def test_v1_rejected(priv_key, keyring):
p = lease_payload(version="lease-v1")
with pytest.raises(VerifyError):
verify_lease(envelope(p, priv_key, "lease"), IID, keyring)
def test_missing_field(priv_key, keyring):
env = envelope(lease_payload(), priv_key, "lease")
env["payload"].pop("key_id") # 先签名再抠字段:canonical 与字段集必须双双拒
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
def test_extra_field(priv_key, keyring):
env = envelope(lease_payload(), priv_key, "lease")
env["payload"]["extra"] = "x"
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
def test_wrong_types(priv_key, keyring):
def bad_cert(mut):
env = envelope(cert_payload(), priv_key, "cert")
mut(env["payload"])
return env
with pytest.raises(VerifyError): # ID 变字符串
verify_certificate(bad_cert(lambda p: p.update(license_id="1")), IID, keyring)
with pytest.raises(VerifyError): # ID 非正数
verify_certificate(bad_cert(lambda p: p.update(license_id=0)), IID, keyring)
with pytest.raises(VerifyError): # bool 冒充整数
verify_certificate(bad_cert(lambda p: p.update(license_id=True)), IID, keyring)
with pytest.raises(VerifyError): # 非法状态枚举
verify_lease(bad_cert(lambda p: p.update(state="睡了的")), IID, keyring)
with pytest.raises(VerifyError): # 时间格式坏
verify_lease(bad_cert(lambda p: p.update(server_time="2026/10/09 03:00:00")),
IID, keyring)
with pytest.raises(VerifyError): # instance_id 非 UUID
verify_certificate(bad_cert(lambda p: p.update(instance_id="not-a-uuid")),
IID, keyring)
def test_cr_lf_nul_in_fields(priv_key, keyring):
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(key_id="k\nid"), priv_key, "lease"), IID, keyring)
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(key_id="k\r"), priv_key, "lease"), IID, keyring)
with pytest.raises(VerifyError):
verify_lease(envelope(lease_payload(key_id="k\x00"), priv_key, "lease"), IID, keyring)
def test_base64url_signature_rejected(priv_key, keyring):
"""Base64URL 字母表(-/_)不是标准 Base64:出现即拒,不猜填充。"""
env = envelope(lease_payload(), priv_key, "lease")
env["signature"] = "-" + env["signature"][1:]
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
env2 = envelope(lease_payload(), priv_key, "lease")
env2["signature"] = "_" + env2["signature"][1:]
with pytest.raises(VerifyError):
verify_lease(env2, IID, keyring)
def test_canonical_sha_mismatch(priv_key, keyring):
env = envelope(lease_payload(), priv_key, "lease", sha="0" * 64)
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
def test_non_p256_key_rejected_at_ring_build():
p384 = ec.generate_private_key(ec.SECP384R1())
from cryptography.hazmat.primitives import serialization
pem = p384.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo).decode("ascii")
with pytest.raises(VerifyError):
build_keyring([{"key_id": "k", "public_key_pem": pem}])
def test_lease_envelope_requires_algorithm_and_next(priv_key, keyring):
env = envelope(lease_payload(), priv_key, "lease")
env.pop("algorithm")
with pytest.raises(VerifyError):
verify_lease(env, IID, keyring)
env2 = envelope(lease_payload(), priv_key, "lease", next_hb=30) # < 60 拒
with pytest.raises(VerifyError):
verify_lease(env2, IID, keyring)
def test_cert_envelope_missing_version(priv_key, keyring):
env = envelope(cert_payload(), priv_key, "cert")
env.pop("version")
with pytest.raises(VerifyError):
verify_certificate(env, IID, keyring)