Skip to content

Commit 92d75a9

Browse files
authored
Improve core.PublicKeysEqual (#6996)
Rather than marshalling and comparing the bytes of each key, simply use the .Equal() method provided by all go stdlib types that implement the crypto.PublicKey interface.
1 parent d916809 commit 92d75a9

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

ca/crl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestGenerateCRL(t *testing.T) {
127127
close(ins)
128128
err = <-errs
129129
test.AssertError(t, err, "can't generate CRL with bad serials")
130-
test.AssertContains(t, err.Error(), "Invalid serial number")
130+
test.AssertContains(t, err.Error(), "invalid serial number")
131131

132132
// Test that we get an error when an entry has a bad revocation time.
133133
ins = make(chan *capb.GenerateCRLRequest)

core/util.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package core
22

33
import (
4-
"bytes"
54
"crypto"
5+
"crypto/ecdsa"
66
"crypto/rand"
7+
"crypto/rsa"
78
"crypto/sha256"
89
"crypto/x509"
910
"encoding/base64"
@@ -97,7 +98,7 @@ func KeyDigest(key crypto.PublicKey) (Sha256Digest, error) {
9798
switch t := key.(type) {
9899
case *jose.JSONWebKey:
99100
if t == nil {
100-
return Sha256Digest{}, fmt.Errorf("Cannot compute digest of nil key")
101+
return Sha256Digest{}, errors.New("cannot compute digest of nil key")
101102
}
102103
return KeyDigest(t.Key)
103104
case jose.JSONWebKey:
@@ -133,21 +134,16 @@ func KeyDigestEquals(j, k crypto.PublicKey) bool {
133134
return digestJ == digestK
134135
}
135136

136-
// PublicKeysEqual determines whether two public keys have the same marshalled
137-
// bytes as one another
138-
func PublicKeysEqual(a, b interface{}) (bool, error) {
139-
if a == nil || b == nil {
140-
return false, errors.New("One or more nil arguments to PublicKeysEqual")
141-
}
142-
aBytes, err := x509.MarshalPKIXPublicKey(a)
143-
if err != nil {
144-
return false, err
145-
}
146-
bBytes, err := x509.MarshalPKIXPublicKey(b)
147-
if err != nil {
148-
return false, err
137+
// PublicKeysEqual determines whether two public keys are identical.
138+
func PublicKeysEqual(a, b crypto.PublicKey) (bool, error) {
139+
switch ak := a.(type) {
140+
case *rsa.PublicKey:
141+
return ak.Equal(b), nil
142+
case *ecdsa.PublicKey:
143+
return ak.Equal(b), nil
144+
default:
145+
return false, fmt.Errorf("unsupported public key type %T", ak)
149146
}
150-
return bytes.Equal(aBytes, bBytes), nil
151147
}
152148

153149
// SerialToString converts a certificate serial number (big.Int) to a String
@@ -161,7 +157,7 @@ func SerialToString(serial *big.Int) string {
161157
func StringToSerial(serial string) (*big.Int, error) {
162158
var serialNum big.Int
163159
if !ValidSerial(serial) {
164-
return &serialNum, errors.New("Invalid serial number")
160+
return &serialNum, fmt.Errorf("invalid serial number %q", serial)
165161
}
166162
_, err := fmt.Sscanf(serial, "%036x", &serialNum)
167163
return &serialNum, err
@@ -254,7 +250,7 @@ func LoadCert(filename string) (*x509.Certificate, error) {
254250
}
255251
block, _ := pem.Decode(certPEM)
256252
if block == nil {
257-
return nil, fmt.Errorf("No data in cert PEM file %s", filename)
253+
return nil, fmt.Errorf("no data in cert PEM file %q", filename)
258254
}
259255
cert, err := x509.ParseCertificate(block.Bytes)
260256
if err != nil {

core/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestSerialUtils(t *testing.T) {
5252
}
5353

5454
badSerial, err := StringToSerial("doop!!!!000")
55-
test.AssertEquals(t, fmt.Sprintf("%v", err), "Invalid serial number")
55+
test.AssertContains(t, err.Error(), "invalid serial number")
5656
fmt.Println(badSerial)
5757
}
5858

@@ -166,7 +166,7 @@ func TestLoadCert(t *testing.T) {
166166

167167
_, err = LoadCert("../test/test-ca.der")
168168
test.AssertError(t, err, "Loading non-PEM file did not error")
169-
test.AssertEquals(t, err.Error(), "No data in cert PEM file ../test/test-ca.der")
169+
test.AssertEquals(t, err.Error(), "no data in cert PEM file \"../test/test-ca.der\"")
170170

171171
_, err = LoadCert("../test/test-ca.key")
172172
test.AssertError(t, err, "Loading non-cert file did not error")

0 commit comments

Comments
 (0)