| |
| |
| """ |
| This module contains functions that verify signatures and thereby authenticate |
| data. |
| |
| Function Manifest for this Module |
| verify_signature |
| verify_gpg_signature |
| verify_signable |
| verify_root |
| verify_delegation |
| """ |
| from binascii import unhexlify |
| from struct import pack |
|
|
| import cryptography.exceptions |
| from cryptography.hazmat.primitives.asymmetric import ed25519 |
|
|
| from .common import ( |
| MetadataVerificationError, |
| PublicKey, |
| SignatureError, |
| UnknownRoleError, |
| canonserialize, |
| checkformat_byteslike, |
| checkformat_delegating_metadata, |
| checkformat_gpg_signature, |
| checkformat_hex_key, |
| checkformat_signable, |
| is_gpg_signature, |
| is_hex_key, |
| is_hex_signature, |
| is_signable, |
| is_signature, |
| ) |
|
|
|
|
| |
| def verify_root(trusted_current_root_metadata, untrusted_new_root_metadata): |
| """ |
| Given currently trusted root metadata, verify that new root metadata is |
| trustworthy per the currently trusted root metadata. |
| |
| This requires a root chaining process as specified in The Update Framework |
| specification. (Version N must be used in order to verify version N+1. |
| Versions cannot be skipped.) |
| |
| # TODOβ
: Proper docstring. |
| """ |
| |
| |
|
|
| |
| checkformat_delegating_metadata(trusted_current_root_metadata) |
| checkformat_delegating_metadata(untrusted_new_root_metadata) |
|
|
| if ( |
| trusted_current_root_metadata["signed"]["type"] != "root" |
| or untrusted_new_root_metadata["signed"]["type"] != "root" |
| ): |
| raise ValueError( |
| "Expected two instances of root metadata. Listed metadata " |
| "type in one or both pieces of metadata provided is not " |
| '"root".' |
| ) |
|
|
| |
| root_expectations = trusted_current_root_metadata["signed"]["delegations"]["root"] |
| expected_threshold = root_expectations["threshold"] |
| authorized_pub_keys = root_expectations["pubkeys"] |
|
|
| |
| |
| |
| |
| |
| new_root_expectations = untrusted_new_root_metadata["signed"]["delegations"]["root"] |
| new_expected_threshold = new_root_expectations["threshold"] |
| new_authorized_pub_keys = new_root_expectations["pubkeys"] |
|
|
| trusted_root_version = trusted_current_root_metadata["signed"]["version"] |
| untrusted_root_version = untrusted_new_root_metadata["signed"]["version"] |
|
|
| if trusted_root_version + 1 != untrusted_root_version: |
| |
| raise MetadataVerificationError( |
| "Root chaining failure: we currently trust a version of root " |
| "that marks itself as version " |
| + str(trusted_root_version) |
| + ", and the provided new root metadata to verify marks itself " |
| "as version " + str(untrusted_root_version) + "; the new " |
| "version must be 1 more than the old version: root updates " |
| "MUST be processed one at a time for security reasons: no " |
| "root version may be skipped." |
| ) |
|
|
| |
| verify_signable( |
| untrusted_new_root_metadata, authorized_pub_keys, expected_threshold, gpg=True |
| ) |
|
|
| |
| |
| |
| verify_signable( |
| untrusted_new_root_metadata, |
| new_authorized_pub_keys, |
| new_expected_threshold, |
| gpg=True, |
| ) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| def verify_delegation( |
| delegation_name, |
| untrusted_delegated_metadata, |
| trusted_delegating_metadata, |
| gpg=False, |
| ): |
| """ |
| Verify that the given untrusted, delegated-to metadata is trustworthy, |
| based on the given trusted metadata's expectations (expected keys and |
| threshold). This function returns if verification succeeds. |
| |
| |
| In other words, check trusted_delegating_metadata's delegation |
| to delegation_name to find the expected signing keys and threshold for |
| delegation_name, and then check untrusted_delegated_metadata to see if it |
| is signed by enough of the right keys to be trustworthy. |
| |
| For example, using root metadata to verify key_mgr metadata looks like |
| this: |
| verify_delegation( |
| 'key_mgr', <full root metadata>, <full key_mgr metadata>) |
| |
| Arguments: |
| (string) delegation_name is the name of the role delegated. |
| (dict) trusted_delegating_metadata is a signable JSON-serializable |
| object representing the full metadata that delegates to role |
| delegation_name. |
| (dict) untrusted_delegated_metadata is a signable JSON-serializable |
| object |
| (bool) gpg should be true if the signatures to be verified in the |
| delegated metadata are expected to be OpenPGP signatures |
| rather than the usual raw ed25519 signatures. |
| |
| Exceptions: |
| - raises UnknownRoleError if there's no matching delegation |
| - raises SignatureError if a signature is bad or signatures |
| don't match expectations (threshold, wrong keys, etc.) |
| # TODO: Consider exception handling to raise MetadataVerificationError instead? |
| - raises MetadataVerificationError if the metadata type is unexpected |
| - raises TypeError or ValueError if the arguments are invalid |
| """ |
|
|
| |
|
|
| if not isinstance(delegation_name, str): |
| raise TypeError( |
| "delegation_name must be a string, not a " + str(type(delegation_name)) |
| ) |
|
|
| if gpg not in [True, False]: |
| raise TypeError( |
| 'Argument "gpg" must be a boolean.' |
| ) |
|
|
| checkformat_delegating_metadata(trusted_delegating_metadata) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| checkformat_signable(untrusted_delegated_metadata) |
| try: |
| checkformat_delegating_metadata(untrusted_delegated_metadata) |
| except: |
| |
| |
| |
| |
| pass |
| else: |
| |
| |
| if delegation_name != untrusted_delegated_metadata["signed"]["type"]: |
| raise MetadataVerificationError( |
| "Instructed to verify provided metadata as if it is of " |
| 'type "' + delegation_name + '", but it claims to be of ' |
| 'type "' + untrusted_delegated_metadata["signed"]["type"] + '"!' |
| ) |
|
|
| |
| delegations = trusted_delegating_metadata["signed"]["delegations"] |
|
|
| if delegation_name not in delegations: |
| raise UnknownRoleError( |
| "Role " + delegation_name + " not found in the given " |
| "delegating metadata." |
| ) |
|
|
| expected_keys = delegations[delegation_name]["pubkeys"] |
| threshold = delegations[delegation_name]["threshold"] |
|
|
| verify_signable( |
| untrusted_delegated_metadata, |
| expected_keys, |
| threshold, |
| gpg=gpg, |
| ) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| def verify_signature(signature, public_key, data): |
| """ |
| Raises βcryptography.exceptions.InvalidSignature if signature is not a |
| correct signature by the given key over the given data. |
| |
| Raises βTypeError if public_key, signature, or data are not correctly |
| formatted. |
| |
| Otherwise, returns (nothing), indicating the signature was verified. |
| |
| Note that this does not use the generalized signature format (which would |
| be compatible with OpenPGP/GPG signatures as well as pyca/cryptography's |
| simple ed25519 sigs). |
| |
| Args: |
| - public_key must be an ed25519.Ed25519PublicKeyObject |
| - signature must be a hex string, length 128, representing a 64-byte |
| raw ed25519 signature |
| - data must be bytes |
| """ |
| if not isinstance(public_key, ed25519.Ed25519PublicKey): |
| raise TypeError( |
| "verify_signature expects a " |
| "cryptography.hazmat.primitives.asymmetric.ed25519ed25519.Ed25519PublicKey" |
| 'object as the "public_key" argument. Instead, received ' |
| + str(type(public_key)) |
| ) |
|
|
| if not is_hex_signature(signature): |
| raise TypeError( |
| "verify_signature expects a hex string representing an " |
| 'ed25519 signature as the "signature" argument. Instead, ' |
| "received object of type " + str(type(signature)) |
| ) |
|
|
| if not isinstance(data, bytes): |
| raise TypeError( |
| 'verify_signature expects a bytes object as the "signature" ' |
| "argument. Instead, received " + str(type(data)) |
| ) |
|
|
| public_key.verify(unhexlify(signature), data) |
|
|
| |
| return |
|
|
|
|
| def verify_signable(signable, authorized_pub_keys, threshold, gpg=False): |
| """ |
| Raises a βSignatureError if signable does not include at least threshold |
| good signatures from (unique) keys with public keys listed in |
| authorized_pub_keys, over the data contained in signable['signed']. |
| |
| Raises βTypeError if the arguments are invalid. |
| |
| Else returns (nothing). |
| |
| Args: |
| - signable |
| common.is_a_signable(signable) must return true. |
| wrap_as_signable() produces output of this type. See those |
| functions. |
| |
| - authorized_pub_keys |
| a list of ed25519 public keys (32 bytes) expressed as 64-character |
| hex strings. This is the form in which they appear in authority |
| metadata (root.json, etc.) Only good signatures from keys listed |
| in authorized_pub_keys count against the threshold of signatures |
| required to verify the signable. |
| |
| - threshold |
| the number of good signatures from unique authorized keys required |
| in order to verify the signable. |
| |
| - gpg (boolean, default False) |
| If True, expects OpenPGP ed25519 signatures (see RFC 4880 bis-08) |
| instead of raw ed25519 signatures. |
| If False, expects raw ed25519 signatures. |
| """ |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| if not is_signable(signable): |
| raise TypeError( |
| "verify_signable expects a signable dictionary. " |
| "Given argument failed the test." |
| ) |
| if not ( |
| isinstance(authorized_pub_keys, list) |
| and all([is_hex_key(k) for k in authorized_pub_keys]) |
| ): |
| raise TypeError("authorized_pub_keys must be a list of hex strings ") |
| |
| |
| |
| |
| |
| if not isinstance(threshold, int) or threshold <= 0: |
| raise TypeError("threshold must be a positive integer.") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| signed_data = canonserialize(signable["signed"]) |
|
|
| |
| |
| |
| |
| good_sigs_from_trusted_keys = {} |
|
|
| for pubkey_hex, signature in signable["signatures"].items(): |
| |
| if not is_hex_key(pubkey_hex): |
| |
| print( |
| 'Ignoring signature from "key" with public key value that ' |
| "does not look like a key value: " + str(pubkey_hex) |
| ) |
| continue |
|
|
| if gpg and not is_gpg_signature(signature): |
| |
| print( |
| 'Ignoring "signature" that does not look like a gpg ' |
| "signature value: " + str(signature) |
| ) |
| continue |
|
|
| if pubkey_hex not in authorized_pub_keys: |
| |
| print( |
| 'Ignoring signature from a key ("' |
| + str(pubkey_hex) |
| + '") that is not authorized to sign this metadata.' |
| ) |
| continue |
|
|
| if not gpg: |
| if not is_signature(signature): |
| |
| print( |
| 'Ignoring "signature" that does not look like a hex ' |
| "signature value: " + str(signature) |
| ) |
| continue |
|
|
| public = PublicKey.from_hex(pubkey_hex) |
|
|
| try: |
| verify_signature(signature["signature"], public, signed_data) |
|
|
| except cryptography.exceptions.InvalidSignature: |
| |
| continue |
|
|
| else: |
| good_sigs_from_trusted_keys[pubkey_hex] = signature |
|
|
| else: |
| assert gpg |
|
|
| try: |
| verify_gpg_signature(signature, pubkey_hex, signed_data) |
|
|
| except cryptography.exceptions.InvalidSignature: |
| |
| continue |
|
|
| else: |
| good_sigs_from_trusted_keys[pubkey_hex] = signature |
|
|
| |
| if len(good_sigs_from_trusted_keys) < threshold: |
| raise SignatureError( |
| "Expected good signatures from at least " |
| + str(threshold) |
| + " unique keys from a set of " |
| + str(len(authorized_pub_keys)) |
| + " keys. Saw " |
| + str(len(signable["signatures"])) |
| + " signatures, only " |
| + str(len(good_sigs_from_trusted_keys)) |
| + " of which were good signatures over the given data from the " |
| "expected keys." |
| ) |
|
|
| |
| return |
|
|
|
|
| def verify_gpg_signature(signature, key_value, data): |
| """ |
| Verifies a raw ed25519 signature that happens to have been produced by an |
| OpenPGP signing process (RFC4880). |
| |
| NOTE that this code DISREGARDS most OpenPGP semantics: is interested solely |
| in the verification of a signature over the given data, with the raw |
| ed25519 public key given (in the form of a hex string). This code does not |
| care about the GPG public key infrastructure, including key |
| self-revocation, expiry, or the relationship of any key with any other key |
| through OpenPGP (subkeys, key-to-key signoff, etc.). |
| |
| This codebase uses OpenPGP signatures solely as a means of facilitating a |
| TUF-style public key infrastructure, where the public key values are |
| trusted with specific privileges directly. |
| |
| |
| β οΈπ£ ABSOLUTELY DO NOT use this for general purpose verification of GPG |
| signatures!! It is for our root signatures only, where OpenPGP |
| signing is just a proxy for a simple ed25519 signature through a |
| hardware signing mechanism. |
| |
| |
| # TODO: β
Proper docstring modeled on verify_signature. |
| """ |
|
|
| checkformat_gpg_signature(signature) |
| checkformat_hex_key(key_value) |
| checkformat_byteslike(data) |
| |
| |
|
|
| public_key = PublicKey.from_hex(key_value) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| additional_header_data = unhexlify(signature["other_headers"]) |
|
|
| |
| |
| hasher = cryptography.hazmat.primitives.hashes.Hash( |
| cryptography.hazmat.primitives.hashes.SHA256(), |
| cryptography.hazmat.backends.default_backend(), |
| ) |
| hasher.update(data) |
| hasher.update(additional_header_data) |
| hasher.update(b"\x04\xff") |
| hasher.update(pack(">I", len(additional_header_data))) |
|
|
| digest = hasher.finalize() |
|
|
| |
| |
| |
|
|
| |
| public_key.verify(unhexlify(signature["signature"]), digest) |
|
|
| |
| return |
|
|