| 1 |
package internal |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"encoding/base64" |
| 7 |
"encoding/json" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
|
| 11 |
sigstoreSignature "github.com/sigstore/sigstore/pkg/signature" |
| 12 |
"go.podman.io/image/v5/docker/reference" |
| 13 |
"go.podman.io/image/v5/internal/signature" |
| 14 |
"go.podman.io/image/v5/manifest" |
| 15 |
"go.podman.io/image/v5/signature/internal" |
| 16 |
) |
| 17 |
|
| 18 |
type Option func(*SigstoreSigner) error |
| 19 |
|
| 20 |
// SigstoreSigner is a signer.SignerImplementation implementation for sigstore signatures. |
| 21 |
// It is initialized using various closures that implement Option, sadly over several subpackages, to decrease the |
| 22 |
// dependency impact. |
| 23 |
type SigstoreSigner struct { |
| 24 |
PrivateKey sigstoreSignature.Signer // May be nil during initialization |
| 25 |
SigningKeyOrCert []byte // For possible Rekor upload; always initialized together with PrivateKey |
| 26 |
|
| 27 |
// Fulcio results to include |
| 28 |
FulcioGeneratedCertificate []byte // Or nil |
| 29 |
FulcioGeneratedCertificateChain []byte // Or nil |
| 30 |
|
| 31 |
// Rekor state |
| 32 |
RekorUploader func(ctx context.Context, keyOrCertBytes []byte, signatureBytes []byte, payloadBytes []byte) ([]byte, error) // Or nil |
| 33 |
} |
| 34 |
|
| 35 |
// ProgressMessage returns a human-readable sentence that makes sense to write before starting to create a single signature. |
| 36 |
func (s *SigstoreSigner) ProgressMessage() string { |
| 37 |
return "Signing image using a sigstore signature" |
| 38 |
} |
| 39 |
|
| 40 |
// SignImageManifest creates a new signature for manifest m as dockerReference. |
| 41 |
func (s *SigstoreSigner) SignImageManifest(ctx context.Context, m []byte, dockerReference reference.Named) (signature.Signature, error) { |
| 42 |
if s.PrivateKey == nil { |
| 43 |
return nil, errors.New("internal error: nothing to sign with, should have been detected in NewSigner") |
| 44 |
} |
| 45 |
|
| 46 |
if reference.IsNameOnly(dockerReference) { |
| 47 |
return nil, fmt.Errorf("reference %s can’t be signed, it has neither a tag nor a digest", dockerReference.String()) |
| 48 |
} |
| 49 |
manifestDigest, err := manifest.Digest(m) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
// sigstore/cosign completely ignores dockerReference for actual policy decisions. |
| 54 |
// They record the repo (but NOT THE TAG) in the value; without the tag we can’t detect version rollbacks. |
| 55 |
// So, just do what simple signing does, and cosign won’t mind. |
| 56 |
payloadData := internal.NewUntrustedSigstorePayload(manifestDigest, dockerReference.String()) |
| 57 |
payloadBytes, err := json.Marshal(payloadData) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
|
| 62 |
// github.com/sigstore/cosign/internal/pkg/cosign.payloadSigner uses signatureoptions.WithContext(), |
| 63 |
// which seems to be not used by anything. So we don’t bother. |
| 64 |
signatureBytes, err := s.PrivateKey.SignMessage(bytes.NewReader(payloadBytes)) |
| 65 |
if err != nil { |
| 66 |
return nil, fmt.Errorf("creating signature: %w", err) |
| 67 |
} |
| 68 |
base64Signature := base64.StdEncoding.EncodeToString(signatureBytes) |
| 69 |
var rekorSETBytes []byte // = nil |
| 70 |
if s.RekorUploader != nil { |
| 71 |
set, err := s.RekorUploader(ctx, s.SigningKeyOrCert, signatureBytes, payloadBytes) |
| 72 |
if err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
rekorSETBytes = set |
| 76 |
} |
| 77 |
|
| 78 |
annotations := map[string]string{ |
| 79 |
signature.SigstoreSignatureAnnotationKey: base64Signature, |
| 80 |
} |
| 81 |
if s.FulcioGeneratedCertificate != nil { |
| 82 |
annotations[signature.SigstoreCertificateAnnotationKey] = string(s.FulcioGeneratedCertificate) |
| 83 |
} |
| 84 |
if s.FulcioGeneratedCertificateChain != nil { |
| 85 |
annotations[signature.SigstoreIntermediateCertificateChainAnnotationKey] = string(s.FulcioGeneratedCertificateChain) |
| 86 |
} |
| 87 |
if rekorSETBytes != nil { |
| 88 |
annotations[signature.SigstoreSETAnnotationKey] = string(rekorSETBytes) |
| 89 |
} |
| 90 |
return signature.SigstoreFromComponents(signature.SigstoreSignatureMIMEType, payloadBytes, annotations), nil |
| 91 |
} |
| 92 |
|
| 93 |
func (s *SigstoreSigner) Close() error { |
| 94 |
return nil |
| 95 |
} |