Skip to content

Commit

Permalink
Use a diffrent Signer when interacting with Fulcio
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ret2libc committed Feb 29, 2024
1 parent 260978c commit 1046eea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
8 changes: 6 additions & 2 deletions cmd/cosign/cli/fulcio/fulcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Signer struct {
signature.SignerVerifier
}

func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*Signer, error) {
func NewSignerWithAdapter(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier, fulcioSigner signature.SignerVerifier) (*Signer, error) {
fClient, err := NewClient(ko.FulcioURL)
if err != nil {
return nil, fmt.Errorf("creating Fulcio client: %w", err)
Expand Down Expand Up @@ -164,7 +164,7 @@ func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerV
}
flow = flowNormal
}
Resp, err := GetCert(ctx, signer, idToken, flow, ko.OIDCIssuer, ko.OIDCClientID, ko.OIDCClientSecret, ko.OIDCRedirectURL, fClient) // TODO, use the chain.
Resp, err := GetCert(ctx, fulcioSigner, idToken, flow, ko.OIDCIssuer, ko.OIDCClientID, ko.OIDCClientSecret, ko.OIDCRedirectURL, fClient) // TODO, use the chain.
if err != nil {
return nil, fmt.Errorf("retrieving cert: %w", err)
}
Expand All @@ -179,6 +179,10 @@ func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerV
return f, nil
}

func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*Signer, error) {
return NewSignerWithAdapter(ctx, ko, signer, signer)
}

func (f *Signer) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey, error) { //nolint: revive
return f.SignerVerifier.PublicKey()
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/cosign/cli/fulcio/fulcioverifier/fulcioverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/sigstore/sigstore/pkg/signature"
)

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

return fs, nil
}

func NewSigner(ctx context.Context, ko options.KeyOpts, signer signature.SignerVerifier) (*fulcio.Signer, error) {
return NewSignerWithAdapter(ctx, ko, signer, signer)
}
30 changes: 28 additions & 2 deletions cmd/cosign/cli/sign/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,17 @@ func keylessSigner(ctx context.Context, ko options.KeyOpts, sv *SignerVerifier)
err error
)

fulcioSV, err := adaptSignerVerifierToFulcio(sv)
if err != nil {
return nil, fmt.Errorf("adapting signer verifier to Fulcio: %w", err)
}

if ko.InsecureSkipFulcioVerify {
if k, err = fulcio.NewSigner(ctx, ko, sv); err != nil {
if k, err = fulcio.NewSignerWithAdapter(ctx, ko, sv, fulcioSV); err != nil {
return nil, fmt.Errorf("getting key from Fulcio: %w", err)
}
} else {
if k, err = fulcioverifier.NewSigner(ctx, ko, sv); err != nil {
if k, err = fulcioverifier.NewSignerWithAdapter(ctx, ko, sv, fulcioSV); err != nil {
return nil, fmt.Errorf("getting key from Fulcio: %w", err)
}
}
Expand Down Expand Up @@ -624,6 +629,27 @@ func (c *SignerVerifier) Bytes(ctx context.Context) ([]byte, error) {
return pemBytes, nil
}

// adaptSignerVerifierToFulcio adapts, if necessary, the SignerVerifier to be used to interact with Fulcio.
//
// This is needed in particular for ED25519 keys with the pre-hashed version of
// the algorithm, which is not supported by Fulcio. This function creates a
// ED25519 SignerVerifier based on that instead.
func adaptSignerVerifierToFulcio(sv *SignerVerifier) (*SignerVerifier, error) {
if ed25519phSV, ok := sv.SignerVerifier.(*signature.ED25519phSignerVerifier); ok {
signerVerifier, err := ed25519phSV.ToED25519SignerVerifier()
if err != nil {
return nil, err
}

return &SignerVerifier{
SignerVerifier: signerVerifier,
Cert: sv.Cert,
Chain: sv.Chain,
}, nil
}
return sv, nil
}

func fetchLocalSignedPayload(sig oci.Signature) (*cosign.LocalSignedPayload, error) {
signedPayload := &cosign.LocalSignedPayload{}
var err error
Expand Down

0 comments on commit 1046eea

Please sign in to comment.