海光 CSV 报告的 user_data / mnonce / measure / pek_cert / sn 被 anonce 按 u32 数组逐个异或混淆,此前框架按明文比对,永远对不上。现按厂商 SDK (csv_sdk/csv_status.c)规则还原(deobfuscate_csv_field),SNP 走明文分支。 另修两个真实 CLI bug: - 全局 --report text|json 与 `tee verify --report <文件>` 撞名,导致 tee verify 永远拿不到文件路径;改为上下文感知。 - tee verify 的 --tcb 默认 0,而 tee plan 用节点描述里的 tcb 算绑定值, 二者不一致时报「user_data 与本次会话绑定不一致(疑似重放报告)」, 把实现问题误导成安全事件。现给 tee verify 加 --node,site / measurement / tcb / platform 默认与 tee plan 同源,平台冲突直接拒绝。 新增真机可复现资产(deploy/tee/real-machine/): - csv-attest-request.c:支持自定义挑战值取报告,自带 SM3 实现 (厂商 get-attestation 把挑战值写死成 "user data",无法与会话绑定) - csv-report-restore.py:还原 anonce 混淆字段 - csv-verify-attestation.sh:官方验签器 wrapper,堵住退出码恒 0、 报告路径写死、离线证书 fallback 同目录、截断报告 fail-open 四个坑 - capture-csv-report.sh / verify-csv-report-e2e.sh / attest-init.sh / build-attest-initrd.sh 真机(192.168.1.102)实测:捕获 2548 字节真实报告,官方验签器完成 芯片签名与 hrk→hsk→cek→pek 全链验证,框架校验通过(structure_ok / bound_ok / chip=已验证)。篡改 user_data、篡改 sig1、截断报告三种负向 场景全部 fail-closed。
107 lines
3.6 KiB
Python
Executable File
107 lines
3.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""还原海光 CSV 证明报告中被 `anonce` 混淆的字段。
|
||
|
||
CSV 的固件报告把 `user_data` / `mnonce` / `measure` / `pek_cert` / `sn` 与一个
|
||
32 位随机数 `anonce` 异或后落盘(厂商 SDK `csv_sdk/csv_status.c`)。厂商验签器
|
||
在验签前会还原;我们要独立核对框架侧的解析,就用同样的规则还原一遍。
|
||
|
||
规则与 SDK 一致:**按 u32 数组逐个异或**
|
||
((uint32_t *)dst)[i] = ((uint32_t *)src)[i] ^ report.anonce;
|
||
|
||
用法:
|
||
csv-report-restore.py --report report.cert --field user_data
|
||
csv-report-restore.py --report report.cert --field all --json
|
||
|
||
兼容 Python 3.6(海光 Anolis 8.10)。
|
||
"""
|
||
import argparse
|
||
import json
|
||
import struct
|
||
import sys
|
||
|
||
REPORT_LEN = 2548 # sizeof(struct csv_attestation_report),厂商 SDK 实测
|
||
|
||
# 字段名 -> (偏移, 长度)
|
||
FIELDS = {
|
||
"user_data": (0x0040, 64),
|
||
"mnonce": (0x0080, 16),
|
||
"measure": (0x0090, 32),
|
||
"pek_cert": (0x0150, 2084),
|
||
"sn": (0x0974, 64),
|
||
}
|
||
ANONCE_OFF = 0xBC
|
||
|
||
|
||
def xor32(buf, anonce):
|
||
"""按 u32 数组逐个异或,与厂商 SDK 语义一致。"""
|
||
if len(buf) % 4 != 0:
|
||
raise ValueError("字段长度 %d 不是 4 的倍数" % len(buf))
|
||
out = bytearray(len(buf))
|
||
for i in range(len(buf) // 4):
|
||
v = struct.unpack_from("<I", buf, i * 4)[0] ^ anonce
|
||
struct.pack_into("<I", out, i * 4, v)
|
||
return bytes(out)
|
||
|
||
|
||
def restore(report, field):
|
||
anonce = struct.unpack_from("<I", report, ANONCE_OFF)[0]
|
||
off, ln = FIELDS[field]
|
||
return anonce, xor32(report[off:off + ln], anonce)
|
||
|
||
|
||
def main():
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--report", help="报告原始字节文件")
|
||
ap.add_argument("--field", default="all",
|
||
help="字段名(user_data/mnonce/measure/pek_cert/sn)或 all")
|
||
ap.add_argument("--json", action="store_true", help="输出 JSON")
|
||
ap.add_argument("--selftest", action="store_true", help="自检(不需要报告文件)")
|
||
args = ap.parse_args()
|
||
|
||
if args.selftest:
|
||
# 用真机报告的实测值核对:anonce=c96e190d,user_data 混淆值 -> "user data"。
|
||
anonce = 0xC96E190D
|
||
# 真机报告里 user_data 的前 9 字节混淆值("user data" 逐字节异或 anonce)。
|
||
raw = bytes.fromhex("786a0bbb2d7d0fbd6c") + b"\x00" * 3
|
||
got = xor32(raw, anonce)[:9]
|
||
if got != b"user data":
|
||
sys.stderr.write("自检失败: 还原结果 %r\n" % got)
|
||
return 1
|
||
print("selftest ok: anonce=%08x, user_data 还原为 'user data'" % anonce)
|
||
return 0
|
||
|
||
if not args.report:
|
||
ap.error("缺少 --report(或用 --selftest)")
|
||
|
||
with open(args.report, "rb") as f:
|
||
report = f.read()
|
||
if len(report) != REPORT_LEN:
|
||
sys.stderr.write("报告长度 %d != %d(不是完整报告)\n"
|
||
% (len(report), REPORT_LEN))
|
||
return 2
|
||
|
||
names = list(FIELDS.keys()) if args.field == "all" else [args.field]
|
||
for n in names:
|
||
if n not in FIELDS:
|
||
sys.stderr.write("未知字段: %s\n" % n)
|
||
return 2
|
||
|
||
result = {}
|
||
for n in names:
|
||
anonce, val = restore(report, n)
|
||
result[n] = {"anonce": "%08x" % anonce, "hex": val.hex()}
|
||
|
||
if args.json:
|
||
print(json.dumps(result, indent=2))
|
||
else:
|
||
anonce = result[names[0]]["anonce"]
|
||
print("anonce = %s" % anonce)
|
||
for n in names:
|
||
val = result[n]["hex"]
|
||
print("%-9s %s" % (n, val[:64] + ("..." if len(val) > 64 else "")))
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|