| 74 | |
| 75 | |
| 76 | def verify(raw, key_bytes): |
| 77 | envelope = admitted_json(raw) |
| 78 | if envelope["payloadType"] != PAYLOAD_TYPE: |
| 79 | raise ValueError("wrong payload type") |
| 80 | payload = decode(envelope["payload"]) |
| 81 | kind = PAYLOAD_TYPE.encode("utf-8") |
| 82 | pae = b"DSSEv1 " + str(len(kind)).encode() + b" " + kind |
| 83 | pae += b" " + str(len(payload)).encode() + b" " + payload |
| 84 | key = Ed25519PublicKey.from_public_bytes(key_bytes) |
| 85 | signatures = envelope["signatures"] |
| 86 | if not isinstance(signatures, list) or not 1 <= len(signatures) <= 1024: |
| 87 | raise ValueError("invalid signature count") |
| 88 | accepted = False |
| 89 | for entry in signatures: |
| 90 | try: |
| 91 | key.verify(decode(entry["sig"]), pae) |
| 92 | accepted = True |
| 93 | break |
| 94 | except (ValueError, TypeError, KeyError, InvalidSignature): |
| 95 | continue |
| 96 | if not accepted: |
| 97 | raise ValueError("no signature verifies under pinned key") |
| 98 | # Interpret only the same payload bytes used to construct the PAE above. |
| 99 | value = admitted_json(payload) |
| 100 | if set(value) != {"schema", "coverage", "projections"}: |
| 101 | raise ValueError("wrong export fields") |
| 102 | if value["schema"] != "atomic-provenance-export/v1": |
| 103 | raise ValueError("wrong export schema") |
| 104 | if value["coverage"] != "exporter-signed-projections-only": |
| 105 | raise ValueError("unsupported coverage claim") |
| 106 | projections = value["projections"] |
| 107 | if not isinstance(projections, list) or not projections: |
| 108 | raise ValueError("no projections") |
| 109 | for projection in projections: |
| 110 | if not isinstance(projection.get("@id"), str) or not projection["@id"].startswith("urn:atomic:provgraph:"): |
| 111 | raise ValueError("not an Atomic provenance projection") |
| 112 | if not isinstance(projection.get("@graph"), list): |
| 113 | raise ValueError("missing graph") |
| 114 | return payload |
| 115 | |
| 116 | |
| 117 | def main(): |