]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/x509: fix certificate request creation with RSA-PSS
authorYoann Congal <yoann.congal@smile.fr>
Thu, 16 May 2024 09:19:20 +0000 (09:19 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 16 May 2024 17:32:30 +0000 (17:32 +0000)
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
Fixes #65074

Change-Id: I8475afa79d8add107f092cc2871d38300e7b3903
GitHub-Last-Rev: 63fb0214c3b03a18e184562a9510145ea817bc20
GitHub-Pull-Request: golang/go#55153
Reviewed-on: https://go-review.googlesource.com/c/go/+/431916
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
TryBot-Bypass: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Alex Scheel <alex.scheel@hashicorp.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/crypto/x509/x509.go
src/crypto/x509/x509_test.go

index 3e26941573170d11d885751baeae3453e2814a8d..47bb4281102d42d0ed6739a2b2aac3ed70995c2f 100644 (file)
@@ -2111,8 +2111,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
        }
index a9dc14526519ee376dd2d9cacfb953cf68ffb274..026367b167624d7aaed0fc50733d5f07e96323e5 100644 (file)
@@ -1418,6 +1418,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},