| 1 | // Package cosign contains functionalities to interact with signatures generated by cosign. |
| 2 | // https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md. |
| 3 | package cosign |
| 4 | |
| 5 | import ( |
| 6 | "encoding/base64" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | |
| 10 | "github.com/google/go-tpm-tools/verifier/oci" |
| 11 | "github.com/opencontainers/go-digest" |
| 12 | v1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 13 | ) |
| 14 | |
| 15 | // Sig implements oci.Signature interface for cosign-generated signatures. |
| 16 | type Sig struct { |
| 17 | // Layer represents a layer descriptor for OCI image manifest. |
| 18 | // This contains the simple signing payload digest and Cosign signature, |
| 19 | // collected from the OCI image manifest object found using https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md#tag-based-discovery. |
| 20 | Layer v1.Descriptor |
| 21 | // Blob represents the opaque data uploaded to OCI registry associated with the layer. |
| 22 | // This contains the Simple Signing Payload as described in https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md#tag-based-discovery. |
| 23 | Blob []byte |
| 24 | // SourceRepo represents the location that stores this signature. |
| 25 | SourceRepo string |
| 26 | } |
| 27 | |
| 28 | // CosignSigKey is the key of the cosign-generated signature embedded in OCI image manifest. |
| 29 | const CosignSigKey = "dev.cosignproject.cosign/signature" |
| 30 | |
| 31 | var ( |
| 32 | // Verify that our Sig struct implements the expected public interface. |
| 33 | _ oci.Signature = Sig{} |
| 34 | encoding = base64.StdEncoding |
| 35 | ) |
| 36 | |
| 37 | // Payload implements oci.Signature interface. |
| 38 | func (s Sig) Payload() ([]byte, error) { |
| 39 | // The payload bytes are uploaded to an OCI registry as blob, and are referenced by digest. |
| 40 | // This digiest is embedded into the OCI image manifest as a layer via a descriptor (see https://github.com/opencontainers/image-spec/blob/main/descriptor.md). |
| 41 | // Here we compare the digest of the blob data with the layer digest to verify if this blob is associated with the layer. |
| 42 | if digest.FromBytes(s.Blob) != s.Layer.Digest { |
| 43 | return nil, errors.New("an unmatched payload digest is paired with a layer descriptor digest") |
| 44 | } |
| 45 | return s.Blob, nil |
| 46 | } |
| 47 | |
| 48 | // Base64Encoded implements oci.Signature interface. |
| 49 | func (s Sig) Base64Encoded() (string, error) { |
| 50 | sig, ok := s.Layer.Annotations[CosignSigKey] |
| 51 | if !ok { |
| 52 | return "", errors.New("cosign signature not found in the layer annotations") |
| 53 | } |
| 54 | if _, err := encoding.DecodeString(sig); err != nil { |
| 55 | return "", fmt.Errorf("invalid base64 encoded signature: %w", err) |
| 56 | } |
| 57 | return sig, nil |
| 58 | } |
| 59 | |
| 60 | // PublicKey implements oci.Signature interface. |
| 61 | // Since public key is attached to the `optional` field of payload, we don't actually implement this method. |
| 62 | // Instead we send payload directly to the Attestation service and let the service parse the payload. |
| 63 | func (s Sig) PublicKey() ([]byte, error) { |
| 64 | return nil, fmt.Errorf("not implemented") |
| 65 | } |
| 66 | |
| 67 | // SigningAlgorithm implements oci.Signature interface. |
| 68 | // Since signing algorithm is attached to the `optional` field of payload, we don't actually implement this method. |
| 69 | // Instead we send payload directly to the Attestation service and let the service parse the payload. |
| 70 | func (s Sig) SigningAlgorithm() (oci.SigningAlgorithm, error) { |
| 71 | return "", fmt.Errorf("not implemented") |
| 72 | } |
| 73 | |
| 74 | // String returns signature details |
| 75 | func (s Sig) String() string { |
| 76 | sig, err := s.Base64Encoded() |
| 77 | if err != nil { |
| 78 | return fmt.Sprintf("[signature error: %s]", err.Error()) |
| 79 | } |
| 80 | return fmt.Sprintf("[signature: %q, sourceRepo: %q]", sig, s.SourceRepo) |
| 81 | } |