Skip to content

Commit 5fc9a4c

Browse files
rscromaindoumenc
authored andcommitted
crypto/x509: respect GODEBUG changes for allowing SHA1 certificates
This allows programs that want SHA1 support to call os.Setenv at startup instead of insisting that users set the environment variable themselves. For golang#41682. Fixes golang#56436. Change-Id: Idcb96212a1d8c560e1dd8eaf7c80b6266f16431e Reviewed-on: https://go-review.googlesource.com/c/go/+/445496 Reviewed-by: David Chase <[email protected]> Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Russ Cox <[email protected]>
1 parent 1a51c19 commit 5fc9a4c

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

src/crypto/x509/verify_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ func testVerify(t *testing.T, test verifyTest, useSystemRoots bool) {
543543
func TestGoVerify(t *testing.T) {
544544
// Temporarily enable SHA-1 verification since a number of test chains
545545
// require it. TODO(filippo): regenerate test chains.
546-
defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
547-
debugAllowSHA1 = true
546+
t.Setenv("GODEBUG", "x509sha1=1")
547+
548548
for _, test := range verifyTests {
549549
t.Run(test.name, func(t *testing.T) {
550550
testVerify(t, test, false)

src/crypto/x509/x509.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,6 @@ type Certificate struct {
728728
// involves algorithms that are not currently implemented.
729729
var ErrUnsupportedAlgorithm = errors.New("x509: cannot verify signature: algorithm unimplemented")
730730

731-
// debugAllowSHA1 allows SHA-1 signatures. See issue 41682.
732-
var debugAllowSHA1 = godebug.Get("x509sha1") == "1"
733-
734731
// An InsecureAlgorithmError indicates that the SignatureAlgorithm used to
735732
// generate the signature is not secure, and the signature has been rejected.
736733
//
@@ -790,7 +787,7 @@ func (c *Certificate) CheckSignatureFrom(parent *Certificate) error {
790787

791788
// TODO(agl): don't ignore the path length constraint.
792789

793-
return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature, parent.PublicKey, debugAllowSHA1)
790+
return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature, parent.PublicKey, false)
794791
}
795792

796793
// CheckSignature verifies that signature is a valid signature over signed from
@@ -837,7 +834,8 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey
837834
case crypto.MD5:
838835
return InsecureAlgorithmError(algo)
839836
case crypto.SHA1:
840-
if !allowSHA1 {
837+
// SHA-1 signatures are mostly disabled. See go.dev/issue/41682.
838+
if !allowSHA1 && godebug.Get("x509sha1") != "1" {
841839
return InsecureAlgorithmError(algo)
842840
}
843841
fallthrough

src/crypto/x509/x509_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,9 +1876,7 @@ func TestSHA1(t *testing.T) {
18761876
t.Fatalf("certificate verification returned %v (%T), wanted InsecureAlgorithmError", err, err)
18771877
}
18781878

1879-
defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
1880-
debugAllowSHA1 = true
1881-
1879+
t.Setenv("GODEBUG", "x509sha1=1")
18821880
if err = cert.CheckSignatureFrom(cert); err != nil {
18831881
t.Fatalf("SHA-1 certificate did not verify with GODEBUG=x509sha1=1: %v", err)
18841882
}
@@ -3470,8 +3468,7 @@ func TestParseUniqueID(t *testing.T) {
34703468
}
34713469

34723470
func TestDisableSHA1ForCertOnly(t *testing.T) {
3473-
defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
3474-
debugAllowSHA1 = false
3471+
t.Setenv("GODEBUG", "")
34753472

34763473
tmpl := &Certificate{
34773474
SerialNumber: big.NewInt(1),

0 commit comments

Comments
 (0)