File: signer.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (60 lines) | stat: -rw-r--r-- 1,696 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package sigstore

import (
	"errors"
	"fmt"
	"os"

	internalSigner "github.com/containers/image/v5/internal/signer"
	"github.com/containers/image/v5/signature/signer"
	"github.com/containers/image/v5/signature/sigstore/internal"
	"github.com/sigstore/sigstore/pkg/cryptoutils"
)

type Option = internal.Option

func WithPrivateKeyFile(file string, passphrase []byte) Option {
	return func(s *internal.SigstoreSigner) error {
		if s.PrivateKey != nil {
			return fmt.Errorf("multiple private key sources specified when preparing to create sigstore signatures")
		}

		if passphrase == nil {
			return errors.New("private key passphrase not provided")
		}

		privateKeyPEM, err := os.ReadFile(file)
		if err != nil {
			return fmt.Errorf("reading private key from %s: %w", file, err)
		}
		signerVerifier, err := loadPrivateKey(privateKeyPEM, passphrase)
		if err != nil {
			return fmt.Errorf("initializing private key: %w", err)
		}
		publicKey, err := signerVerifier.PublicKey()
		if err != nil {
			return fmt.Errorf("getting public key from private key: %w", err)
		}
		publicKeyPEM, err := cryptoutils.MarshalPublicKeyToPEM(publicKey)
		if err != nil {
			return fmt.Errorf("converting public key to PEM: %w", err)
		}
		s.PrivateKey = signerVerifier
		s.SigningKeyOrCert = publicKeyPEM
		return nil
	}
}

func NewSigner(opts ...Option) (*signer.Signer, error) {
	s := internal.SigstoreSigner{}
	for _, o := range opts {
		if err := o(&s); err != nil {
			return nil, err
		}
	}
	if s.PrivateKey == nil {
		return nil, errors.New("no private key source provided (neither a private key nor Fulcio) when preparing to create sigstore signatures")
	}

	return internalSigner.NewSigner(&s), nil
}