|
| 1 | +# Copyright 2024 The Sigstore Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Signing payloads for models as in-toto statements. |
| 16 | +
|
| 17 | +To generate the signing payload we convert model manifests to in-toto formats, |
| 18 | +as described by https://github.com/in-toto/attestation/tree/main/spec/v1. The |
| 19 | +envelope format is DSSE, see https://github.com/secure-systems-lab/dsse. |
| 20 | +""" |
| 21 | + |
| 22 | +from typing import Final, Self |
| 23 | +from typing_extensions import override |
| 24 | + |
| 25 | +from in_toto_attestation.v1 import statement |
| 26 | + |
| 27 | +from model_signing.manifest import manifest as manifest_module |
| 28 | +from model_signing.signing import signing |
| 29 | + |
| 30 | + |
| 31 | +class IntotoPayload(signing.SigningPayload): |
| 32 | + """A generic payload in in-toto format. |
| 33 | +
|
| 34 | + This class is abstract for now as we will support multiple payload formats |
| 35 | + below. |
| 36 | +
|
| 37 | + Each subclass defines a constant for the predicate type class attribute |
| 38 | + defined below. |
| 39 | + """ |
| 40 | + |
| 41 | + predicate_type: Final[str] |
| 42 | + |
| 43 | + |
| 44 | +class SingleDigestIntotoPayload(IntotoPayload): |
| 45 | + """In-toto payload where the model is serialized to just one digest. |
| 46 | +
|
| 47 | + In this case, we encode the model as the only subject of the statement. We |
| 48 | + don't set the name field, and use the digest as the one resulting from the |
| 49 | + model serialization. |
| 50 | +
|
| 51 | + However, since we use custom hashing algorithms, but these are not supported |
| 52 | + by existing tools, we claim that the digest algorithm is sha-256 and include |
| 53 | + the real digest in the predicate. |
| 54 | +
|
| 55 | + Example: |
| 56 | + ```json |
| 57 | + { |
| 58 | + "_type": "https://in-toto.io/Statement/v1", |
| 59 | + "subject": [ |
| 60 | + { |
| 61 | + "digest": { |
| 62 | + "sha256": "3aab065c...." |
| 63 | + } |
| 64 | + } |
| 65 | + ], |
| 66 | + "predicateType": "https://model_signing/Digest/v0.1", |
| 67 | + "predicate": { |
| 68 | + "actual_hash_algorithm": "file-sha256" |
| 69 | + } |
| 70 | + } |
| 71 | + ``` |
| 72 | +
|
| 73 | + If the predicate is missing (or does not set "actual_hash_algorithm"), it |
| 74 | + should be assumed that the digest is actually computed via the algorithm |
| 75 | + present in the resource descriptor (i.e., sha256). |
| 76 | +
|
| 77 | + See also https://github.com/sigstore/sigstore-python/issues/1018. |
| 78 | + """ |
| 79 | + |
| 80 | + predicate_type: Final[str] = "https://model_signing/Digest/v0.1" |
| 81 | + |
| 82 | + def __init__(self, *, digest_hex: str, digest_algorithm: str): |
| 83 | + """Builds an instance of this in-toto payload. |
| 84 | +
|
| 85 | + Don't call this directly in production. Use `from_manifest()` instead. |
| 86 | +
|
| 87 | + Args: |
| 88 | + digest_hex: the hexadecimal, human readable, digest of the subject. |
| 89 | + digest_algorithm: the algorithm used to compute the digest. |
| 90 | + """ |
| 91 | + digest = {"sha256": digest_hex} |
| 92 | + descriptor = statement.ResourceDescriptor(digest=digest).pb |
| 93 | + |
| 94 | + self.statement = statement.Statement( |
| 95 | + subjects=[descriptor], |
| 96 | + predicate_type=self.predicate_type, |
| 97 | + predicate={"actual_hash_algorithm": digest_algorithm}, |
| 98 | + ) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + @override |
| 102 | + def from_manifest(cls, manifest: manifest_module.Manifest) -> Self: |
| 103 | + """Converts a manifest to the signing payload used for signing. |
| 104 | +
|
| 105 | + The manifest must be a `DigestManifest` instance. |
| 106 | +
|
| 107 | + Args: |
| 108 | + manifest: the manifest to convert to signing payload. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + An instance of `SingleDigestIntotoPayload`. |
| 112 | +
|
| 113 | + Raises: |
| 114 | + TypeError: If the manifest is not `DigestManifest`. |
| 115 | + """ |
| 116 | + if not isinstance(manifest, manifest_module.DigestManifest): |
| 117 | + raise TypeError("Only DigestManifest is supported") |
| 118 | + |
| 119 | + # guaranteed to have exactly one item |
| 120 | + subject = list(manifest.resource_descriptors())[0] |
| 121 | + digest = subject.digest |
| 122 | + return cls( |
| 123 | + digest_hex=digest.digest_hex, |
| 124 | + digest_algorithm=digest.algorithm, |
| 125 | + ) |
0 commit comments