Skip to content

Commit fdb640b

Browse files
crypto/x509: disable signing with MD5WithRSA
MD5 is hopelessly broken, we already don't allow verification of MD5 signatures, we shouldn't support generating them. Fixes #42125 Change-Id: Ib25d750e6fc72a03198a505ac71e6d2c99eff2ed Reviewed-on: https://go-review.googlesource.com/c/go/+/285872 Run-TryBot: Roland Shoemaker <[email protected]> Reviewed-by: Katie Hockman <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 485a572 commit fdb640b

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/crypto/x509/x509.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,10 @@ func signingParamsForPublicKey(pub any, requestedSigAlgo SignatureAlgorithm) (ha
13971397
err = errors.New("x509: cannot sign with hash function requested")
13981398
return
13991399
}
1400+
if hashFunc == crypto.MD5 {
1401+
err = errors.New("x509: signing with MD5 is not supported")
1402+
return
1403+
}
14001404
if requestedSigAlgo.isRSAPSS() {
14011405
sigAlgo.Parameters = hashToPSSParameters[hashFunc]
14021406
}
@@ -1591,15 +1595,8 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv
15911595
}
15921596

15931597
// Check the signature to ensure the crypto.Signer behaved correctly.
1594-
sigAlg := getSignatureAlgorithmFromAI(signatureAlgorithm)
1595-
switch sigAlg {
1596-
case MD5WithRSA:
1597-
// We skip the check if the signature algorithm is only supported for
1598-
// signing, not verification.
1599-
default:
1600-
if err := checkSignature(sigAlg, c.Raw, signature, key.Public(), true); err != nil {
1601-
return nil, fmt.Errorf("x509: signature over certificate returned by signer is invalid: %w", err)
1602-
}
1598+
if err := checkSignature(getSignatureAlgorithmFromAI(signatureAlgorithm), c.Raw, signature, key.Public(), true); err != nil {
1599+
return nil, fmt.Errorf("x509: signature over certificate returned by signer is invalid: %w", err)
16031600
}
16041601

16051602
return signedCert, nil

src/crypto/x509/x509_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,8 +2929,8 @@ func TestCreateCertificateLegacy(t *testing.T) {
29292929
SignatureAlgorithm: sigAlg,
29302930
}
29312931
_, err := CreateCertificate(rand.Reader, template, template, testPrivateKey.Public(), &brokenSigner{testPrivateKey.Public()})
2932-
if err != nil {
2933-
t.Fatalf("CreateCertificate failed when SignatureAlgorithm = %v: %s", sigAlg, err)
2932+
if err == nil {
2933+
t.Fatal("CreateCertificate didn't fail when SignatureAlgorithm = MD5WithRSA")
29342934
}
29352935
}
29362936

0 commit comments

Comments
 (0)