#!/usr/bin/env python3 """Independent offline verifier. Reads ONLY the files on disk: the COSE bytes, the subject document and the issuer's PEM public key. Shares no state with the signer. This is what would run in the room with the network off.""" import cbor2, hashlib, sys, os from cryptography.hazmat.primitives.asymmetric.utils import encode_dss_signature from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives import hashes, serialization H = os.path.dirname(os.path.abspath(__file__)) pub = serialization.load_pem_public_key(open(os.path.join(H, "issuer-public-key.pem"), "rb").read()) subject = open(os.path.join(H, "subject-cbom.json"), "rb").read() def check(path, external=None): tagged = cbor2.loads(open(os.path.join(H, path), "rb").read()) assert tagged.tag == 18, "not a COSE_Sign1" protected, unprotected, body, sig = tagged.value hdr = cbor2.loads(protected) payload = body if body is not None else external tbs = cbor2.dumps(["Signature1", protected, b"", payload]) pub.verify(encode_dss_signature(int.from_bytes(sig[:32], "big"), int.from_bytes(sig[32:], "big")), tbs, ec.ECDSA(hashes.SHA256())) return hdr, unprotected, payload hdr, unp, pl = check("signed-statement.cose") alg = {-7: "ES256"}.get(hdr.get(1), hdr.get(1)) print(f"protected header : alg={alg} content-type={hdr.get(3)}") print(f"kid : {unp.get(4).decode()}") print(f"payload sha256 : {hashlib.sha256(pl).hexdigest()}") print(f"matches subject : {hashlib.sha256(pl).hexdigest() == hashlib.sha256(subject).hexdigest()}") check("signed-statement-detached.cose", external=subject) print("detached form verifies against the subject file on disk") import json cb = json.loads(subject) ca = [c for c in cb.get("components", []) if c.get("type") == "cryptographic-asset"] print(f"subject is CycloneDX {cb.get('specVersion')} with {len(ca)} cryptographic-asset component(s):") for c in ca: p = c.get("cryptoProperties", {}) print(f" {c.get('name')} assetType={p.get('assetType')} oid={p.get('oid')}") print("VERIFIED OFFLINE")