Debian Codegraph @

signature/simplesequoia/signer.go

raw
1 //go:build containers_image_sequoia
2
3 package simplesequoia
4
5 import (
6 "context"
7 "errors"
8 "fmt"
9
10 "go.podman.io/image/v5/docker/reference"
11 internalSig "go.podman.io/image/v5/internal/signature"
12 internalSigner "go.podman.io/image/v5/internal/signer"
13 "go.podman.io/image/v5/signature"
14 "go.podman.io/image/v5/signature/internal/sequoia"
15 "go.podman.io/image/v5/signature/signer"
16 )
17
18 // simpleSequoiaSigner is a signer.SignerImplementation implementation for simple signing signatures using Sequoia.
19 type simpleSequoiaSigner struct {
20 mech *sequoia.SigningMechanism
21 sequoiaHome string // "" if using the system’s default
22 keyFingerprint string
23 passphrase string // "" if not provided.
24 }
25
26 // NewSigner returns a signature.Signer which creates “simple signing” signatures using the user’s default
27 // Sequoia PGP configuration.
28 //
29 // The set of options must identify a key to sign with, probably using a WithKeyFingerprint.
30 //
31 // The caller must call Close() on the returned Signer.
32 func NewSigner(opts ...Option) (*signer.Signer, error) {
33 s := simpleSequoiaSigner{}
34 for _, o := range opts {
35 if err := o(&s); err != nil {
36 return nil, err
37 }
38 }
39 if s.keyFingerprint == "" {
40 return nil, errors.New("no key identity provided for simple signing")
41 }
42
43 if err := sequoia.Init(); err != nil {
44 return nil, err // Coverage: This is impractical to test in-process, with the static go_sequoia_dlhandle.
45 }
46 mech, err := sequoia.NewMechanismFromDirectory(s.sequoiaHome)
47 if err != nil {
48 return nil, fmt.Errorf("initializing Sequoia: %w", err)
49 }
50 s.mech = mech
51 succeeded := false
52 defer func() {
53 if !succeeded {
54 s.mech.Close() // Coverage: This is currently unreachable.
55 }
56 }()
57
58 // Ideally, we should look up (and unlock?) the key at this point already. FIXME: is that possible? Anyway, low-priority.
59
60 succeeded = true
61 return internalSigner.NewSigner(&s), nil
62 }
63
64 // ProgressMessage returns a human-readable sentence that makes sense to write before starting to create a single signature.
65 func (s *simpleSequoiaSigner) ProgressMessage() string {
66 return "Signing image using Sequoia-PGP simple signing"
67 }
68
69 // SignImageManifest creates a new signature for manifest m as dockerReference.
70 func (s *simpleSequoiaSigner) SignImageManifest(ctx context.Context, m []byte, dockerReference reference.Named) (internalSig.Signature, error) {
71 if reference.IsNameOnly(dockerReference) {
72 return nil, fmt.Errorf("reference %s can’t be signed, it has neither a tag nor a digest", dockerReference.String())
73 }
74 wrapped := sequoiaSigningOnlyMechanism{
75 inner: s.mech,
76 }
77 simpleSig, err := signature.SignDockerManifestWithOptions(m, dockerReference.String(), &wrapped, s.keyFingerprint, &signature.SignOptions{
78 Passphrase: s.passphrase,
79 })
80 if err != nil {
81 return nil, err
82 }
83 return internalSig.SimpleSigningFromBlob(simpleSig), nil
84 }
85
86 func (s *simpleSequoiaSigner) Close() error {
87 return s.mech.Close()
88 }