Skip to content

Commit 9ef6b20

Browse files
Support PKCS1 encoded and non-ECDSA CT log public keys (#1806)
* Support PKCS1 encoded CT log public keys This came up while testing out staging, which uses a PKCS1 encoded public key. We should be flexible on the supported key format. Signed-off-by: Hayden Blauzvern <[email protected]> * Update comment Signed-off-by: Hayden Blauzvern <[email protected]> * Remove requirement that key is ECDSA Signed-off-by: Hayden Blauzvern <[email protected]>
1 parent 27caa98 commit 9ef6b20

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package ctl
1717
import (
1818
"context"
1919
"crypto"
20-
"crypto/ecdsa"
2120
"crypto/sha256"
2221
"crypto/x509"
2322
"encoding/json"
@@ -89,27 +88,23 @@ func VerifySCT(ctx context.Context, certPEM, chainPEM, rawSCT []byte) error {
8988
return err
9089
}
9190
for _, t := range targets {
92-
pub, err := cryptoutils.UnmarshalPEMToPublicKey(t.Target)
91+
pub, err := getPublicKey(t.Target)
9392
if err != nil {
9493
return err
9594
}
96-
ctPub, ok := pub.(*ecdsa.PublicKey)
97-
if !ok {
98-
return fmt.Errorf("invalid public key: was %T, require *ecdsa.PublicKey", pub)
99-
}
100-
keyID, err := ctutil.GetCTLogID(ctPub)
95+
keyID, err := ctutil.GetCTLogID(pub)
10196
if err != nil {
10297
return errors.Wrap(err, "error getting CTFE public key hash")
10398
}
104-
pubKeys[keyID] = logIDMetadata{ctPub, t.Status}
99+
pubKeys[keyID] = logIDMetadata{pub, t.Status}
105100
}
106101
} else {
107102
fmt.Fprintf(os.Stderr, "**Warning** Using a non-standard public key for verifying SCT: %s\n", rootEnv)
108103
raw, err := os.ReadFile(rootEnv)
109104
if err != nil {
110105
return errors.Wrap(err, "error reading alternate public key file")
111106
}
112-
pubKey, err := getAlternatePublicKey(raw)
107+
pubKey, err := getPublicKey(raw)
113108
if err != nil {
114109
return errors.Wrap(err, "error parsing alternate public key from the file")
115110
}
@@ -204,9 +199,9 @@ func VerifyEmbeddedSCT(ctx context.Context, chain []*x509.Certificate) error {
204199
}
205200

206201
// Given a byte array, try to construct a public key from it.
207-
// Will try first to see if it's PEM formatted, if not, then it will
208-
// try to parse it as der publics, and failing that
209-
func getAlternatePublicKey(in []byte) (crypto.PublicKey, error) {
202+
// Supports PEM encoded public keys, falling back to DER. Supports
203+
// PKIX and PKCS1 encoded keys.
204+
func getPublicKey(in []byte) (crypto.PublicKey, error) {
210205
var pubKey crypto.PublicKey
211206
var err error
212207
var derBytes []byte
@@ -222,7 +217,7 @@ func getAlternatePublicKey(in []byte) (crypto.PublicKey, error) {
222217
// Try using the PKCS1 before giving up.
223218
pubKey, err = x509.ParsePKCS1PublicKey(derBytes)
224219
if err != nil {
225-
return nil, errors.Wrap(err, "failed to parse alternate public key")
220+
return nil, errors.Wrap(err, "failed to parse CT log public key")
226221
}
227222
}
228223
return pubKey, nil

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"github.com/sigstore/sigstore/pkg/cryptoutils"
3434
)
3535

36-
func TestGetAlternatePublicKey(t *testing.T) {
36+
func TestGetPublicKey(t *testing.T) {
3737
wd, err := os.Getwd()
3838
if err != nil {
3939
t.Fatalf("Failed to get cwd: %v", err)
@@ -58,7 +58,7 @@ func TestGetAlternatePublicKey(t *testing.T) {
5858
if err != nil {
5959
t.Fatalf("Failed to read testfile %s : %v", tc.file, err)
6060
}
61-
got, err := getAlternatePublicKey(bytes)
61+
got, err := getPublicKey(bytes)
6262
switch {
6363
case err == nil && tc.wantErrSub != "":
6464
t.Errorf("Wanted Error for %s but got none", tc.file)

0 commit comments

Comments
 (0)