| 1 |
package sigstore |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/sigstore/sigstore/pkg/cryptoutils" |
| 9 |
internalSigner "go.podman.io/image/v5/internal/signer" |
| 10 |
"go.podman.io/image/v5/signature/signer" |
| 11 |
"go.podman.io/image/v5/signature/sigstore/internal" |
| 12 |
) |
| 13 |
|
| 14 |
type Option = internal.Option |
| 15 |
|
| 16 |
func WithPrivateKeyFile(file string, passphrase []byte) Option { |
| 17 |
return func(s *internal.SigstoreSigner) error { |
| 18 |
if s.PrivateKey != nil { |
| 19 |
return fmt.Errorf("multiple private key sources specified when preparing to create sigstore signatures") |
| 20 |
} |
| 21 |
|
| 22 |
if passphrase == nil { |
| 23 |
return errors.New("private key passphrase not provided") |
| 24 |
} |
| 25 |
|
| 26 |
privateKeyPEM, err := os.ReadFile(file) |
| 27 |
if err != nil { |
| 28 |
return fmt.Errorf("reading private key from %s: %w", file, err) |
| 29 |
} |
| 30 |
signerVerifier, err := loadPrivateKey(privateKeyPEM, passphrase) |
| 31 |
if err != nil { |
| 32 |
return fmt.Errorf("initializing private key: %w", err) |
| 33 |
} |
| 34 |
publicKey, err := signerVerifier.PublicKey() |
| 35 |
if err != nil { |
| 36 |
return fmt.Errorf("getting public key from private key: %w", err) |
| 37 |
} |
| 38 |
publicKeyPEM, err := cryptoutils.MarshalPublicKeyToPEM(publicKey) |
| 39 |
if err != nil { |
| 40 |
return fmt.Errorf("converting public key to PEM: %w", err) |
| 41 |
} |
| 42 |
s.PrivateKey = signerVerifier |
| 43 |
s.SigningKeyOrCert = publicKeyPEM |
| 44 |
return nil |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func NewSigner(opts ...Option) (*signer.Signer, error) { |
| 49 |
s := internal.SigstoreSigner{} |
| 50 |
for _, o := range opts { |
| 51 |
if err := o(&s); err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
} |
| 55 |
if s.PrivateKey == nil { |
| 56 |
return nil, errors.New("no private key source provided (neither a private key nor Fulcio) when preparing to create sigstore signatures") |
| 57 |
} |
| 58 |
|
| 59 |
return internalSigner.NewSigner(&s), nil |
| 60 |
} |