Skip to content

Commit 8a15192

Browse files
aglFiloSottile
authored andcommitted
crypto/x509: support the PSS certificates that OpenSSL 1.1.0 generates.
It serialises optional parameters as empty rather than NULL. It's probably technically correct, although ASN.1 has a long history of doing this different ways. But OpenSSL is likely common enough that we want to support this encoding. Fixes #23847 Change-Id: I81c60f0996edfecf59467dfdf75b0cf8ba7b1efb Reviewed-on: https://go-review.googlesource.com/96417 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 983dcf7 commit 8a15192

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/crypto/x509/x509.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,10 @@ func getSignatureAlgorithmFromAI(ai pkix.AlgorithmIdentifier) SignatureAlgorithm
420420
// https://tools.ietf.org/html/rfc3447#section-8.1), that the
421421
// salt length matches the hash length, and that the trailer
422422
// field has the default value.
423-
if !bytes.Equal(params.Hash.Parameters.FullBytes, asn1.NullBytes) ||
423+
if (len(params.Hash.Parameters.FullBytes) != 0 && !bytes.Equal(params.Hash.Parameters.FullBytes, asn1.NullBytes)) ||
424424
!params.MGF.Algorithm.Equal(oidMGF1) ||
425425
!mgf1HashFunc.Algorithm.Equal(params.Hash.Algorithm) ||
426-
!bytes.Equal(mgf1HashFunc.Parameters.FullBytes, asn1.NullBytes) ||
426+
(len(mgf1HashFunc.Parameters.FullBytes) != 0 && !bytes.Equal(mgf1HashFunc.Parameters.FullBytes, asn1.NullBytes)) ||
427427
params.TrailerField != 1 {
428428
return UnknownSignatureAlgorithm
429429
}

src/crypto/x509/x509_test.go

+43-10
Original file line numberDiff line numberDiff line change
@@ -946,19 +946,52 @@ qsGZWxzFvvkXUkQSl0dQQ5jO/FtUJcAVXVVp20LxPemfatAHpW31WdJYeWSQWky2
946946
+f9b5TXKXVyjlUL7uHxowWrT2AtTchDH22wTEtqLEF9Z3Q==
947947
-----END CERTIFICATE-----`
948948

949+
// openssl req -newkey rsa:2048 -keyout test.key -sha256 -sigopt \
950+
// rsa_padding_mode:pss -sigopt rsa_pss_saltlen:32 -sigopt rsa_mgf1_md:sha256 \
951+
// -x509 -days 3650 -nodes -subj '/C=US/ST=CA/L=SF/O=Test/CN=Test' -out \
952+
// test.pem
953+
var rsaPSSSelfSignedOpenSSL110PEM = `-----BEGIN CERTIFICATE-----
954+
MIIDwDCCAnigAwIBAgIJAM9LAMHTE5xpMD0GCSqGSIb3DQEBCjAwoA0wCwYJYIZI
955+
AWUDBAIBoRowGAYJKoZIhvcNAQEIMAsGCWCGSAFlAwQCAaIDAgEgMEUxCzAJBgNV
956+
BAYTAlVTMQswCQYDVQQIDAJDQTELMAkGA1UEBwwCU0YxDTALBgNVBAoMBFRlc3Qx
957+
DTALBgNVBAMMBFRlc3QwHhcNMTgwMjIyMjIxMzE4WhcNMjgwMjIwMjIxMzE4WjBF
958+
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExCzAJBgNVBAcMAlNGMQ0wCwYDVQQK
959+
DARUZXN0MQ0wCwYDVQQDDARUZXN0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
960+
CgKCAQEA4Zrsydod+GoTAJLLutWNF87qhhVPBsK1zB1Gj+NAAe4+VbrZ1E41H1wp
961+
qITx7DA8DRtJEf+NqrTAnAdZWBG/tAOA5LfXVax0ZSQtLnYLSeylLoMtDyY3eFAj
962+
TmuTOoyVy6raktowCnHCh01NsstqqTfrx6SbmzOmDmKTkq/I+7K0MCVsn41xRDVM
963+
+ShD0WGFGioEGoiWnFSWupxJDA3Q6jIDEygVwNKHwnhv/2NgG2kqZzrZSQA67en0
964+
iKAXtoDNPpmyD5oS9YbEJ+2Nbm7oLeON30i6kZvXKIzJXx+UWViazHZqnsi5rQ8G
965+
RHF+iVFXsqd0MzDKmkKOT5FDhrsbKQIDAQABo1MwUTAdBgNVHQ4EFgQU9uFY/nlg
966+
gLH00NBnr/o7QvpN9ugwHwYDVR0jBBgwFoAU9uFY/nlggLH00NBnr/o7QvpN9ugw
967+
DwYDVR0TAQH/BAUwAwEB/zA9BgkqhkiG9w0BAQowMKANMAsGCWCGSAFlAwQCAaEa
968+
MBgGCSqGSIb3DQEBCDALBglghkgBZQMEAgGiAwIBIAOCAQEAhJzpwxBNGKvzKWDe
969+
WLqv6RMrl/q4GcH3b7M9wjxe0yOm4F+Tb2zJ7re4h+D39YkJf8cX1NV9UQVu6z4s
970+
Fvo2kmlR0qZOXAg5augmCQ1xS0WHFoF6B52anNzHkZQbAIYJ3kGoFsUHzs7Sz7F/
971+
656FsRpHA9UzJQ3avPPMrA4Y4aoJ7ANJ6XIwTrdWrhULOVuvYRLCl4CdTVztVFX6
972+
wxX8nS1ISYd8jXPUMgsBKVbWufvLoIymMJW8CZbpprVZel5zFn0bmPrON8IHS30w
973+
Gs+ITJjKEnZgXmAQ25SLKVzkZkBcGANs2GsdHNJ370Puisy0FIPD2NXR5uASAf7J
974+
+w9fjQ==
975+
-----END CERTIFICATE-----`
976+
949977
func TestRSAPSSSelfSigned(t *testing.T) {
950-
der, _ := pem.Decode([]byte(rsaPSSSelfSignedPEM))
951-
if der == nil {
952-
t.Fatal("Failed to find PEM block")
953-
}
978+
for i, pemBlock := range []string{rsaPSSSelfSignedPEM, rsaPSSSelfSignedOpenSSL110PEM} {
979+
der, _ := pem.Decode([]byte(pemBlock))
980+
if der == nil {
981+
t.Errorf("#%d: failed to find PEM block", i)
982+
continue
983+
}
954984

955-
cert, err := ParseCertificate(der.Bytes)
956-
if err != nil {
957-
t.Fatal(err)
958-
}
985+
cert, err := ParseCertificate(der.Bytes)
986+
if err != nil {
987+
t.Errorf("#%d: failed to parse: %s", i, err)
988+
continue
989+
}
959990

960-
if err = cert.CheckSignatureFrom(cert); err != nil {
961-
t.Fatal(err)
991+
if err = cert.CheckSignatureFrom(cert); err != nil {
992+
t.Errorf("#%d: signature check failed: %s", i, err)
993+
continue
994+
}
962995
}
963996
}
964997

0 commit comments

Comments
 (0)