Skip to content

Commit db5cb5f

Browse files
committed
[release-branch.go1.18] 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 #41682. Fixes #56436. Fixes #56437. 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]> Reviewed-on: https://go-review.googlesource.com/c/go/+/445656
1 parent 156bf3d commit db5cb5f

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
@@ -540,8 +540,8 @@ func testVerify(t *testing.T, test verifyTest, useSystemRoots bool) {
540540
func TestGoVerify(t *testing.T) {
541541
// Temporarily enable SHA-1 verification since a number of test chains
542542
// require it. TODO(filippo): regenerate test chains.
543-
defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
544-
debugAllowSHA1 = true
543+
t.Setenv("GODEBUG", "x509sha1=1")
544+
545545
for _, test := range verifyTests {
546546
t.Run(test.name, func(t *testing.T) {
547547
testVerify(t, test, false)

src/crypto/x509/x509.go

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

733-
// debugAllowSHA1 allows SHA-1 signatures. See issue 41682.
734-
var debugAllowSHA1 = godebug.Get("x509sha1") == "1"
735-
736733
// An InsecureAlgorithmError indicates that the SignatureAlgorithm used to
737734
// generate the signature is not secure, and the signature has been rejected.
738735
//
@@ -792,7 +789,7 @@ func (c *Certificate) CheckSignatureFrom(parent *Certificate) error {
792789

793790
// TODO(agl): don't ignore the path length constraint.
794791

795-
return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature, parent.PublicKey, debugAllowSHA1)
792+
return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature, parent.PublicKey, false)
796793
}
797794

798795
// CheckSignature verifies that signature is a valid signature over signed from
@@ -839,7 +836,8 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey
839836
case crypto.MD5:
840837
return InsecureAlgorithmError(algo)
841838
case crypto.SHA1:
842-
if !allowSHA1 {
839+
// SHA-1 signatures are mostly disabled. See go.dev/issue/41682.
840+
if !allowSHA1 && godebug.Get("x509sha1") != "1" {
843841
return InsecureAlgorithmError(algo)
844842
}
845843
fallthrough

src/crypto/x509/x509_test.go

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

1867-
defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
1868-
debugAllowSHA1 = true
1869-
1867+
t.Setenv("GODEBUG", "x509sha1=1")
18701868
if err = cert.CheckSignatureFrom(cert); err != nil {
18711869
t.Fatalf("SHA-1 certificate did not verify with GODEBUG=x509sha1=1: %v", err)
18721870
}
@@ -3335,8 +3333,7 @@ func TestLargeOID(t *testing.T) {
33353333
}
33363334

33373335
func TestDisableSHA1ForCertOnly(t *testing.T) {
3338-
defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
3339-
debugAllowSHA1 = false
3336+
t.Setenv("GODEBUG", "")
33403337

33413338
tmpl := &Certificate{
33423339
SerialNumber: big.NewInt(1),

0 commit comments

Comments
 (0)