From 1d33b5364e20d549422ee04fc5245b34fd181431 Mon Sep 17 00:00:00 2001 From: Yoann Congal Date: Thu, 6 May 2021 11:39:29 +0200 Subject: [PATCH 1/2] crypto/x509: add test for RSA-PSS-SHA256 CertificateRequest Updates #45990 --- src/crypto/x509/x509_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crypto/x509/x509_test.go b/src/crypto/x509/x509_test.go index ead0453f66d0ab..4eef253e1b7613 100644 --- a/src/crypto/x509/x509_test.go +++ b/src/crypto/x509/x509_test.go @@ -1417,6 +1417,7 @@ func TestCreateCertificateRequest(t *testing.T) { sigAlgo SignatureAlgorithm }{ {"RSA", testPrivateKey, SHA256WithRSA}, + {"RSA-PSS-SHA256", testPrivateKey, SHA256WithRSAPSS}, {"ECDSA-256", ecdsa256Priv, ECDSAWithSHA256}, {"ECDSA-384", ecdsa384Priv, ECDSAWithSHA256}, {"ECDSA-521", ecdsa521Priv, ECDSAWithSHA256}, From 63fb0214c3b03a18e184562a9510145ea817bc20 Mon Sep 17 00:00:00 2001 From: Yoann Congal Date: Fri, 7 May 2021 00:01:27 +0200 Subject: [PATCH 2/2] crypto/x509: fix certificate request creation with RSA-PSS In case of a RSA-PSS algorithm, the hashFunc of CreateCertificateRequest is embedded in a rsa.PSSOptions struct. Given to key.Sign(), this will generate a proper RSA-PSS signature. Pasted from the RSA-PSS handling code in CreateCertificate() Fixes #45990 --- src/crypto/x509/x509.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/crypto/x509/x509.go b/src/crypto/x509/x509.go index f33283b559f090..08608cdcf86c76 100644 --- a/src/crypto/x509/x509.go +++ b/src/crypto/x509/x509.go @@ -2110,8 +2110,16 @@ func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv signed = h.Sum(nil) } + var signerOpts crypto.SignerOpts = hashFunc + if template.SignatureAlgorithm != 0 && template.SignatureAlgorithm.isRSAPSS() { + signerOpts = &rsa.PSSOptions{ + SaltLength: rsa.PSSSaltLengthEqualsHash, + Hash: hashFunc, + } + } + var signature []byte - signature, err = key.Sign(rand, signed, hashFunc) + signature, err = key.Sign(rand, signed, signerOpts) if err != nil { return }