@@ -18,7 +18,6 @@ package sign
1818import (
1919 "bytes"
2020 "context"
21- "crypto"
2221 "crypto/x509"
2322 "encoding/base64"
2423 "encoding/json"
@@ -32,6 +31,7 @@ import (
3231 "github.com/google/go-containerregistry/pkg/name"
3332 v1 "github.com/google/go-containerregistry/pkg/v1"
3433 "github.com/google/go-containerregistry/pkg/v1/remote"
34+ pb_go_v1 "github.com/sigstore/protobuf-specs/gen/pb-go/common/v1"
3535
3636 "github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio"
3737 "github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio/fulcioverifier"
@@ -138,12 +138,7 @@ func SignCmd(ro *options.RootOptions, ko options.KeyOpts, signOpts options.SignO
138138 ctx , cancel := context .WithTimeout (context .Background (), ro .Timeout )
139139 defer cancel ()
140140
141- svOptions := []signature.LoadOption {
142- signatureoptions .WithHash (crypto .SHA256 ),
143- signatureoptions .WithED25519ph (),
144- }
145-
146- sv , err := signerFromKeyOptsWithSVOpts (ctx , signOpts .Cert , signOpts .CertChain , ko , svOptions ... )
141+ sv , err := SignerFromKeyOpts (ctx , signOpts .Cert , signOpts .CertChain , ko )
147142 if err != nil {
148143 return fmt .Errorf ("getting signer: %w" , err )
149144 }
@@ -531,8 +526,8 @@ func signerFromKeyRef(ctx context.Context, certPath, certChainPath, keyRef strin
531526 return certSigner , nil
532527}
533528
534- func signerFromNewKey (svOpts ... signature.LoadOption ) (* SignerVerifier , error ) {
535- privKey , err := cosign .GeneratePrivateKey ( )
529+ func signerFromNewKey (algorithmDetails signature. AlgorithmDetails , svOpts ... signature.LoadOption ) (* SignerVerifier , error ) {
530+ privKey , err := cosign .GeneratePrivateKeyWithAlgo ( algorithmDetails )
536531 if err != nil {
537532 return nil , fmt .Errorf ("generating cert: %w" , err )
538533 }
@@ -569,9 +564,27 @@ func keylessSigner(ctx context.Context, ko options.KeyOpts, sv *SignerVerifier)
569564 }, nil
570565}
571566
572- func signerFromKeyOptsWithSVOpts (ctx context.Context , certPath string , certChainPath string , ko options.KeyOpts , svOpts ... signature.LoadOption ) (* SignerVerifier , error ) {
567+ func SignerFromKeyOpts (ctx context.Context , certPath string , certChainPath string , ko options.KeyOpts ) (* SignerVerifier , error ) {
568+ var svOpts []signature.LoadOption
569+ signingAlgorithm , err := signature .ParseSignatureAlgorithmFlag (ko .SigningAlgorithm )
570+ if err != nil {
571+ // Default to ECDSA_SHA2_256_NISTP256 if no algorithm is specified
572+ signingAlgorithm = pb_go_v1 .KnownSignatureAlgorithm_ECDSA_SHA2_256_NISTP256
573+ }
574+
575+ algorithmDetails , err := signature .GetAlgorithmDetails (signingAlgorithm )
576+ if err != nil {
577+ return nil , err
578+ }
579+ hashAlgorithm := algorithmDetails .GetHashType ()
580+ svOpts = []signature.LoadOption {
581+ signatureoptions .WithHash (hashAlgorithm ),
582+ }
583+ if algorithmDetails .GetSignatureAlgorithm () == pb_go_v1 .KnownSignatureAlgorithm_ED25519_PH {
584+ svOpts = append (svOpts , signatureoptions .WithED25519ph ())
585+ }
586+
573587 var sv * SignerVerifier
574- var err error
575588 genKey := false
576589 switch {
577590 case ko .Sk :
@@ -581,7 +594,7 @@ func signerFromKeyOptsWithSVOpts(ctx context.Context, certPath string, certChain
581594 default :
582595 genKey = true
583596 ui .Infof (ctx , "Generating ephemeral keys..." )
584- sv , err = signerFromNewKey (svOpts ... )
597+ sv , err = signerFromNewKey (algorithmDetails , svOpts ... )
585598 }
586599 if err != nil {
587600 return nil , err
@@ -594,10 +607,6 @@ func signerFromKeyOptsWithSVOpts(ctx context.Context, certPath string, certChain
594607 return sv , nil
595608}
596609
597- func SignerFromKeyOpts (ctx context.Context , certPath string , certChainPath string , ko options.KeyOpts ) (* SignerVerifier , error ) {
598- return signerFromKeyOptsWithSVOpts (ctx , certPath , certChainPath , ko )
599- }
600-
601610type SignerVerifier struct {
602611 Cert []byte
603612 Chain []byte
0 commit comments