|
2 | 2 | Utilities for verifying Signed Entry Timestamps.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import base64 |
| 6 | +from importlib import resources |
| 7 | +from typing import cast |
| 8 | + |
| 9 | +import cryptography.hazmat.primitives.asymmetric.ec as ec |
| 10 | +from cryptography.exceptions import InvalidSignature |
| 11 | +from cryptography.hazmat.primitives import hashes |
| 12 | +from cryptography.hazmat.primitives.serialization import load_pem_public_key |
| 13 | +from securesystemslib.formats import encode_canonical # type: ignore |
| 14 | + |
5 | 15 | from sigstore._internal.rekor import RekorEntry
|
6 | 16 |
|
| 17 | +REKOR_ROOT_PUBKEY = resources.read_binary("sigstore._store", "rekor.pub") |
7 | 18 |
|
8 |
| -def verify_set(entry: RekorEntry) -> None: |
| 19 | + |
| 20 | +class InvalidSetError(Exception): |
9 | 21 | pass
|
| 22 | + |
| 23 | + |
| 24 | +def verify_set(entry: RekorEntry) -> None: |
| 25 | + """Verify the Signed Entry Timestamp for a given Rekor entry""" |
| 26 | + |
| 27 | + # Put together the payload |
| 28 | + # |
| 29 | + # This involves removing any non-required fields (verification and attestation) and then |
| 30 | + # canonicalizing the remaining JSON in accordance with IETF's RFC 8785. |
| 31 | + raw_data = entry.raw_data.copy() |
| 32 | + del raw_data["verification"] |
| 33 | + del raw_data["attestation"] |
| 34 | + canon_data: bytes = encode_canonical(raw_data).encode() |
| 35 | + |
| 36 | + # Decode the SET field |
| 37 | + signed_entry_ts: bytes = base64.b64decode( |
| 38 | + entry.verification["signedEntryTimestamp"].encode() |
| 39 | + ) |
| 40 | + |
| 41 | + # Load the Rekor public key |
| 42 | + rekor_key = load_pem_public_key(REKOR_ROOT_PUBKEY) |
| 43 | + rekor_key = cast(ec.EllipticCurvePublicKey, rekor_key) |
| 44 | + |
| 45 | + # Validate the SET |
| 46 | + try: |
| 47 | + rekor_key.verify( |
| 48 | + signature=signed_entry_ts, |
| 49 | + data=canon_data, |
| 50 | + signature_algorithm=ec.ECDSA(hashes.SHA256()), |
| 51 | + ) |
| 52 | + except InvalidSignature as inval_sig: |
| 53 | + raise InvalidSetError from inval_sig |
0 commit comments