aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2017-12-05 21:26:07 -0500
committerBrad Fitzpatrick <bradfitz@golang.org>2017-12-06 03:59:54 +0000
commitffd79b310736d768593b207946744e94d918749a (patch)
tree83786cc3cfb9b204a241dd3ac0c8dc0d6b1bfd5c
parentc3fa046f543526cf1fd15d9d2b995f57511837c1 (diff)
downloadgo-ffd79b310736d768593b207946744e94d918749a.tar.xz
crypto/x509: add test for asn1.Marshal of rsa.PublicKey
Go 1.10 is adding new API MarshalPKCS1PublicKey and ParsePKCS1PublicKey for converting rsa.PublicKeys. Even though we'd prefer that users did not, check that if users call asn1.Marshal and asn1.Unmarshal directly instead, they get the same results. We know that code exists in the wild that depends on this. Change-Id: Ia385d6954fda2eba7da228dc42f229b6839ef11e Reviewed-on: https://go-review.googlesource.com/82080 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/crypto/x509/x509_test.go22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/crypto/x509/x509_test.go b/src/crypto/x509/x509_test.go
index 4f271e310f..502f5d21d3 100644
--- a/src/crypto/x509/x509_test.go
+++ b/src/crypto/x509/x509_test.go
@@ -187,10 +187,28 @@ func TestMarshalRSAPublicKey(t *testing.T) {
derBytes := MarshalPKCS1PublicKey(pub)
pub2, err := ParsePKCS1PublicKey(derBytes)
if err != nil {
- t.Errorf("error parsing serialized key: %s", err)
+ t.Errorf("ParsePKCS1PublicKey: %s", err)
}
if pub.N.Cmp(pub2.N) != 0 || pub.E != pub2.E {
- t.Errorf("got:%+v want:%+v", pub, pub2)
+ t.Errorf("ParsePKCS1PublicKey = %+v, want %+v", pub, pub2)
+ }
+
+ // It's never been documented that asn1.Marshal/Unmarshal on rsa.PublicKey works,
+ // but it does, and we know of code that depends on it.
+ // Lock that in, even though we'd prefer that people use MarshalPKCS1PublicKey and ParsePKCS1PublicKey.
+ derBytes2, err := asn1.Marshal(*pub)
+ if err != nil {
+ t.Errorf("Marshal(rsa.PublicKey): %v", err)
+ } else if !bytes.Equal(derBytes, derBytes2) {
+ t.Errorf("Marshal(rsa.PublicKey) = %x, want %x", derBytes2, derBytes)
+ }
+ pub3 := new(rsa.PublicKey)
+ rest, err := asn1.Unmarshal(derBytes, pub3)
+ if err != nil {
+ t.Errorf("Unmarshal(rsa.PublicKey): %v", err)
+ }
+ if len(rest) != 0 || pub.N.Cmp(pub3.N) != 0 || pub.E != pub3.E {
+ t.Errorf("Unmarshal(rsa.PublicKey) = %+v, %q want %+v, %q", pub, rest, pub2, []byte(nil))
}
publicKeys := []struct {