Skip to content

Commit 1046eea

Browse files
committed
Use a diffrent Signer when interacting with Fulcio
ED25519-ph is not widely supported and it is not an accepted option in x509 Certificates/CSR, so Fulcio does not accept them. Instead, clients are supposed to use PureED25519 when interacting with Fulcio. This commit provides to the Fulcio code a separate SignerVerifier created from the one loaded from the private key. This SignerVerifier is usually of the same type, except when dealing with ED25519ph. Signed-off-by: Riccardo Schirone <riccardo.schirone@trailofbits.com>
1 parent 260978c commit 1046eea

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

cmd/cosign/cli/fulcio/fulcio.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type Signer struct {
109109
signature.SignerVerifier
110110
}
111111

112-
func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*Signer, error) {
112+
func NewSignerWithAdapter(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier, fulcioSigner signature.SignerVerifier) (*Signer, error) {
113113
fClient, err := NewClient(ko.FulcioURL)
114114
if err != nil {
115115
return nil, fmt.Errorf("creating Fulcio client: %w", err)
@@ -164,7 +164,7 @@ func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerV
164164
}
165165
flow = flowNormal
166166
}
167-
Resp, err := GetCert(ctx, signer, idToken, flow, ko.OIDCIssuer, ko.OIDCClientID, ko.OIDCClientSecret, ko.OIDCRedirectURL, fClient) // TODO, use the chain.
167+
Resp, err := GetCert(ctx, fulcioSigner, idToken, flow, ko.OIDCIssuer, ko.OIDCClientID, ko.OIDCClientSecret, ko.OIDCRedirectURL, fClient) // TODO, use the chain.
168168
if err != nil {
169169
return nil, fmt.Errorf("retrieving cert: %w", err)
170170
}
@@ -179,6 +179,10 @@ func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerV
179179
return f, nil
180180
}
181181

182+
func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*Signer, error) {
183+
return NewSignerWithAdapter(ctx, ko, signer, signer)
184+
}
185+
182186
func (f *Signer) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey, error) { //nolint: revive
183187
return f.SignerVerifier.PublicKey()
184188
}

cmd/cosign/cli/fulcio/fulcioverifier/fulcioverifier.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
"github.com/sigstore/sigstore/pkg/signature"
2727
)
2828

29-
func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*fulcio.Signer, error) {
30-
fs, err := fulcio.NewSigner(ctx, ko, signer)
29+
func NewSignerWithAdapter(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier, fulcioSigner signature.SignerVerifier) (*fulcio.Signer, error) {
30+
fs, err := fulcio.NewSignerWithAdapter(ctx, ko, signer, fulcioSigner)
3131
if err != nil {
3232
return nil, err
3333
}
@@ -46,3 +46,7 @@ func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerV
4646

4747
return fs, nil
4848
}
49+
50+
func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*fulcio.Signer, error) {
51+
return NewSignerWithAdapter(ctx, ko, signer, signer)
52+
}

cmd/cosign/cli/sign/sign.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,17 @@ func keylessSigner(ctx context.Context, ko options.KeyOpts, sv *SignerVerifier)
552552
err error
553553
)
554554

555+
fulcioSV, err := adaptSignerVerifierToFulcio(sv)
556+
if err != nil {
557+
return nil, fmt.Errorf("adapting signer verifier to Fulcio: %w", err)
558+
}
559+
555560
if ko.InsecureSkipFulcioVerify {
556-
if k, err = fulcio.NewSigner(ctx, ko, sv); err != nil {
561+
if k, err = fulcio.NewSignerWithAdapter(ctx, ko, sv, fulcioSV); err != nil {
557562
return nil, fmt.Errorf("getting key from Fulcio: %w", err)
558563
}
559564
} else {
560-
if k, err = fulcioverifier.NewSigner(ctx, ko, sv); err != nil {
565+
if k, err = fulcioverifier.NewSignerWithAdapter(ctx, ko, sv, fulcioSV); err != nil {
561566
return nil, fmt.Errorf("getting key from Fulcio: %w", err)
562567
}
563568
}
@@ -624,6 +629,27 @@ func (c *SignerVerifier) Bytes(ctx context.Context) ([]byte, error) {
624629
return pemBytes, nil
625630
}
626631

632+
// adaptSignerVerifierToFulcio adapts, if necessary, the SignerVerifier to be used to interact with Fulcio.
633+
//
634+
// This is needed in particular for ED25519 keys with the pre-hashed version of
635+
// the algorithm, which is not supported by Fulcio. This function creates a
636+
// ED25519 SignerVerifier based on that instead.
637+
func adaptSignerVerifierToFulcio(sv *SignerVerifier) (*SignerVerifier, error) {
638+
if ed25519phSV, ok := sv.SignerVerifier.(*signature.ED25519phSignerVerifier); ok {
639+
signerVerifier, err := ed25519phSV.ToED25519SignerVerifier()
640+
if err != nil {
641+
return nil, err
642+
}
643+
644+
return &SignerVerifier{
645+
SignerVerifier: signerVerifier,
646+
Cert: sv.Cert,
647+
Chain: sv.Chain,
648+
}, nil
649+
}
650+
return sv, nil
651+
}
652+
627653
func fetchLocalSignedPayload(sig oci.Signature) (*cosign.LocalSignedPayload, error) {
628654
signedPayload := &cosign.LocalSignedPayload{}
629655
var err error

0 commit comments

Comments
 (0)