@@ -18,7 +18,6 @@ package sign
18
18
import (
19
19
"bytes"
20
20
"context"
21
- "crypto"
22
21
"crypto/x509"
23
22
"encoding/base64"
24
23
"encoding/json"
@@ -32,6 +31,7 @@ import (
32
31
"github.com/google/go-containerregistry/pkg/name"
33
32
v1 "github.com/google/go-containerregistry/pkg/v1"
34
33
"github.com/google/go-containerregistry/pkg/v1/remote"
34
+ pb_go_v1 "github.com/sigstore/protobuf-specs/gen/pb-go/common/v1"
35
35
36
36
"github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio"
37
37
"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
138
138
ctx , cancel := context .WithTimeout (context .Background (), ro .Timeout )
139
139
defer cancel ()
140
140
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 )
147
142
if err != nil {
148
143
return fmt .Errorf ("getting signer: %w" , err )
149
144
}
@@ -531,8 +526,8 @@ func signerFromKeyRef(ctx context.Context, certPath, certChainPath, keyRef strin
531
526
return certSigner , nil
532
527
}
533
528
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 )
536
531
if err != nil {
537
532
return nil , fmt .Errorf ("generating cert: %w" , err )
538
533
}
@@ -569,9 +564,27 @@ func keylessSigner(ctx context.Context, ko options.KeyOpts, sv *SignerVerifier)
569
564
}, nil
570
565
}
571
566
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
+
573
587
var sv * SignerVerifier
574
- var err error
575
588
genKey := false
576
589
switch {
577
590
case ko .Sk :
@@ -581,7 +594,7 @@ func signerFromKeyOptsWithSVOpts(ctx context.Context, certPath string, certChain
581
594
default :
582
595
genKey = true
583
596
ui .Infof (ctx , "Generating ephemeral keys..." )
584
- sv , err = signerFromNewKey (svOpts ... )
597
+ sv , err = signerFromNewKey (algorithmDetails , svOpts ... )
585
598
}
586
599
if err != nil {
587
600
return nil , err
@@ -594,10 +607,6 @@ func signerFromKeyOptsWithSVOpts(ctx context.Context, certPath string, certChain
594
607
return sv , nil
595
608
}
596
609
597
- func SignerFromKeyOpts (ctx context.Context , certPath string , certChainPath string , ko options.KeyOpts ) (* SignerVerifier , error ) {
598
- return signerFromKeyOptsWithSVOpts (ctx , certPath , certChainPath , ko )
599
- }
600
-
601
610
type SignerVerifier struct {
602
611
Cert []byte
603
612
Chain []byte
0 commit comments