Blame SOURCES/relax_sha1_restriction.patch

2dcf06
diff --git a/src/crypto/x509/verify.go b/src/crypto/x509/verify.go
2dcf06
index 98778fe..71ab62a 100644
2dcf06
--- a/src/crypto/x509/verify.go
2dcf06
+++ b/src/crypto/x509/verify.go
2dcf06
@@ -736,6 +736,9 @@ func (c *Certificate) isValid(certType int, currentChain []*Certificate, opts *V
2dcf06
 // list. (While this is not specified, it is common practice in order to limit
2dcf06
 // the types of certificates a CA can issue.)
2dcf06
 //
2dcf06
+// Certificates that use SHA1WithRSA and ECDSAWithSHA1 signatures are not supported,
2dcf06
+// and will not be used to build chains.
2dcf06
+//
2dcf06
 // WARNING: this function doesn't do any revocation checking.
2dcf06
 func (c *Certificate) Verify(opts VerifyOptions) (chains [][]*Certificate, err error) {
2dcf06
 	// Platform-specific verification needs the ASN.1 contents so
2dcf06
diff --git a/src/crypto/x509/x509.go b/src/crypto/x509/x509.go
2dcf06
index 47be77d..85720b3 100644
2dcf06
--- a/src/crypto/x509/x509.go
2dcf06
+++ b/src/crypto/x509/x509.go
2dcf06
@@ -184,13 +184,13 @@ const (
2dcf06
 
2dcf06
 	MD2WithRSA  // Unsupported.
2dcf06
 	MD5WithRSA  // Only supported for signing, not verification.
2dcf06
-	SHA1WithRSA // Only supported for signing, not verification.
2dcf06
+	SHA1WithRSA // Only supported for signing, and verification of CRLs, CSRs, and OCSP responses.
2dcf06
 	SHA256WithRSA
2dcf06
 	SHA384WithRSA
2dcf06
 	SHA512WithRSA
2dcf06
 	DSAWithSHA1   // Unsupported.
2dcf06
 	DSAWithSHA256 // Unsupported.
2dcf06
-	ECDSAWithSHA1 // Only supported for signing, not verification.
2dcf06
+	ECDSAWithSHA1 // Only supported for signing, and verification of CRLs, CSRs, and OCSP responses.
2dcf06
 	ECDSAWithSHA256
2dcf06
 	ECDSAWithSHA384
2dcf06
 	ECDSAWithSHA512
2dcf06
@@ -770,7 +770,7 @@ func (c *Certificate) hasSANExtension() bool {
2dcf06
 }
2dcf06
 
2dcf06
 // CheckSignatureFrom verifies that the signature on c is a valid signature
2dcf06
-// from parent.
2dcf06
+// from parent. SHA1WithRSA and ECDSAWithSHA1 signatures are not supported.
2dcf06
 func (c *Certificate) CheckSignatureFrom(parent *Certificate) error {
2dcf06
 	// RFC 5280, 4.2.1.9:
2dcf06
 	// "If the basic constraints extension is not present in a version 3
2dcf06
@@ -792,13 +792,13 @@ func (c *Certificate) CheckSignatureFrom(parent *Certificate) error {
2dcf06
 
2dcf06
 	// TODO(agl): don't ignore the path length constraint.
2dcf06
 
2dcf06
-	return parent.CheckSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature)
2dcf06
+	return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature, parent.PublicKey, debugAllowSHA1)
2dcf06
 }
2dcf06
 
2dcf06
 // CheckSignature verifies that signature is a valid signature over signed from
2dcf06
 // c's public key.
2dcf06
 func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) error {
2dcf06
-	return checkSignature(algo, signed, signature, c.PublicKey)
2dcf06
+	return checkSignature(algo, signed, signature, c.PublicKey, true)
2dcf06
 }
2dcf06
 
2dcf06
 func (c *Certificate) hasNameConstraints() bool {
2dcf06
@@ -818,9 +818,9 @@ func signaturePublicKeyAlgoMismatchError(expectedPubKeyAlgo PublicKeyAlgorithm,
2dcf06
 	return fmt.Errorf("x509: signature algorithm specifies an %s public key, but have public key of type %T", expectedPubKeyAlgo.String(), pubKey)
2dcf06
 }
2dcf06
 
2dcf06
-// CheckSignature verifies that signature is a valid signature over signed from
2dcf06
+// checkSignature verifies that signature is a valid signature over signed from
2dcf06
 // a crypto.PublicKey.
2dcf06
-func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey crypto.PublicKey) (err error) {
2dcf06
+func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey crypto.PublicKey, allowSHA1 bool) (err error) {
2dcf06
 	var hashType crypto.Hash
2dcf06
 	var pubKeyAlgo PublicKeyAlgorithm
2dcf06
 
2dcf06
@@ -839,7 +839,7 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey
2dcf06
 	case crypto.MD5:
2dcf06
 		return InsecureAlgorithmError(algo)
2dcf06
 	case crypto.SHA1:
2dcf06
-		if !debugAllowSHA1 {
2dcf06
+		if !allowSHA1 {
2dcf06
 			return InsecureAlgorithmError(algo)
2dcf06
 		}
2dcf06
 		fallthrough
2dcf06
@@ -1599,11 +1599,11 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv
2dcf06
 	// Check the signature to ensure the crypto.Signer behaved correctly.
2dcf06
 	sigAlg := getSignatureAlgorithmFromAI(signatureAlgorithm)
2dcf06
 	switch sigAlg {
2dcf06
-	case MD5WithRSA, SHA1WithRSA, ECDSAWithSHA1:
2dcf06
+	case MD5WithRSA:
2dcf06
 		// We skip the check if the signature algorithm is only supported for
2dcf06
 		// signing, not verification.
2dcf06
 	default:
2dcf06
-		if err := checkSignature(sigAlg, c.Raw, signature, key.Public()); err != nil {
2dcf06
+		if err := checkSignature(sigAlg, c.Raw, signature, key.Public(), true); err != nil {
2dcf06
 			return nil, fmt.Errorf("x509: signature over certificate returned by signer is invalid: %w", err)
2dcf06
 		}
2dcf06
 	}
2dcf06
@@ -2082,7 +2082,7 @@ func parseCertificateRequest(in *certificateRequest) (*CertificateRequest, error
2dcf06
 
2dcf06
 // CheckSignature reports whether the signature on c is valid.
2dcf06
 func (c *CertificateRequest) CheckSignature() error {
2dcf06
-	return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificateRequest, c.Signature, c.PublicKey)
2dcf06
+	return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificateRequest, c.Signature, c.PublicKey, true)
2dcf06
 }
2dcf06
 
2dcf06
 // RevocationList contains the fields used to create an X.509 v2 Certificate
2dcf06
diff --git a/src/crypto/x509/x509_test.go b/src/crypto/x509/x509_test.go
2dcf06
index f3e2a77..d31f70d 100644
2dcf06
--- a/src/crypto/x509/x509_test.go
2dcf06
+++ b/src/crypto/x509/x509_test.go
2dcf06
@@ -13,6 +13,7 @@ import (
2dcf06
 	"crypto/elliptic"
2dcf06
 	"crypto/rand"
2dcf06
 	"crypto/rsa"
2dcf06
+	"crypto/boring"
2dcf06
 	_ "crypto/sha256"
2dcf06
 	_ "crypto/sha512"
2dcf06
 	"crypto/x509/pkix"
2dcf06
@@ -2940,30 +2941,15 @@ func TestCreateCertificateBrokenSigner(t *testing.T) {
2dcf06
 }
2dcf06
 
2dcf06
 func TestCreateCertificateLegacy(t *testing.T) {
2dcf06
-	ecdsaPriv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
2dcf06
-	if err != nil {
2dcf06
-		t.Fatalf("Failed to generate ECDSA key: %s", err)
2dcf06
+	sigAlg := MD5WithRSA
2dcf06
+	template := &Certificate{
2dcf06
+		SerialNumber:       big.NewInt(10),
2dcf06
+		DNSNames:           []string{"example.com"},
2dcf06
+		SignatureAlgorithm: sigAlg,
2dcf06
 	}
2dcf06
-
2dcf06
-	for _, sigAlg := range []SignatureAlgorithm{
2dcf06
-		MD5WithRSA, SHA1WithRSA, ECDSAWithSHA1,
2dcf06
-	} {
2dcf06
-		template := &Certificate{
2dcf06
-			SerialNumber:       big.NewInt(10),
2dcf06
-			DNSNames:           []string{"example.com"},
2dcf06
-			SignatureAlgorithm: sigAlg,
2dcf06
-		}
2dcf06
-		var k crypto.Signer
2dcf06
-		switch sigAlg {
2dcf06
-		case MD5WithRSA, SHA1WithRSA:
2dcf06
-			k = testPrivateKey
2dcf06
-		case ECDSAWithSHA1:
2dcf06
-			k = ecdsaPriv
2dcf06
-		}
2dcf06
-		_, err := CreateCertificate(rand.Reader, template, template, k.Public(), &brokenSigner{k.Public()})
2dcf06
-		if err != nil {
2dcf06
-			t.Fatalf("CreateCertificate failed when SignatureAlgorithm = %v: %s", sigAlg, err)
2dcf06
-		}
2dcf06
+	_, err := CreateCertificate(rand.Reader, template, template, testPrivateKey.Public(), &brokenSigner{testPrivateKey.Public()})
2dcf06
+	if err != nil {
2dcf06
+		t.Fatalf("CreateCertificate failed when SignatureAlgorithm = %v: %s", sigAlg, err)
2dcf06
 	}
2dcf06
 }
2dcf06
 
2dcf06
@@ -3364,3 +3350,69 @@ func TestLargeOID(t *testing.T) {
2dcf06
 		t.Fatalf("ParseCertificate to failed to parse certificate with large OID: %s", err)
2dcf06
 	}
2dcf06
 }
2dcf06
+
2dcf06
+func TestDisableSHA1ForCertOnly(t *testing.T) {
2dcf06
+        if boring.Enabled() {
2dcf06
+		t.Skip("not supported in boring mode")
2dcf06
+	} 
2dcf06
+	defer func(old bool) { debugAllowSHA1 = old }(debugAllowSHA1)
2dcf06
+	debugAllowSHA1 = false
2dcf06
+
2dcf06
+	tmpl := &Certificate{
2dcf06
+		SerialNumber:          big.NewInt(1),
2dcf06
+		NotBefore:             time.Now().Add(-time.Hour),
2dcf06
+		NotAfter:              time.Now().Add(time.Hour),
2dcf06
+		SignatureAlgorithm:    SHA1WithRSA,
2dcf06
+		BasicConstraintsValid: true,
2dcf06
+		IsCA:                  true,
2dcf06
+		KeyUsage:              KeyUsageCertSign | KeyUsageCRLSign,
2dcf06
+	}
2dcf06
+	certDER, err := CreateCertificate(rand.Reader, tmpl, tmpl, rsaPrivateKey.Public(), rsaPrivateKey)
2dcf06
+	if err != nil {
2dcf06
+		t.Fatalf("failed to generate test cert: %s", err)
2dcf06
+	}
2dcf06
+	cert, err := ParseCertificate(certDER)
2dcf06
+	if err != nil {
2dcf06
+		t.Fatalf("failed to parse test cert: %s", err)
2dcf06
+	}
2dcf06
+
2dcf06
+	err = cert.CheckSignatureFrom(cert)
2dcf06
+	if err == nil {
2dcf06
+		t.Error("expected CheckSignatureFrom to fail")
2dcf06
+	} else if _, ok := err.(InsecureAlgorithmError); !ok {
2dcf06
+		t.Errorf("expected InsecureAlgorithmError error, got %T", err)
2dcf06
+	}
2dcf06
+
2dcf06
+	crlDER, err := CreateRevocationList(rand.Reader, &RevocationList{
2dcf06
+		SignatureAlgorithm: SHA1WithRSA,
2dcf06
+		Number:             big.NewInt(1),
2dcf06
+		ThisUpdate:         time.Now().Add(-time.Hour),
2dcf06
+		NextUpdate:         time.Now().Add(time.Hour),
2dcf06
+	}, cert, rsaPrivateKey)
2dcf06
+	if err != nil {
2dcf06
+		t.Fatalf("failed to generate test CRL: %s", err)
2dcf06
+	}
2dcf06
+	// TODO(rolandshoemaker): this should be ParseRevocationList once it lands
2dcf06
+	crl, err := ParseCRL(crlDER)
2dcf06
+	if err != nil {
2dcf06
+		t.Fatalf("failed to parse test CRL: %s", err)
2dcf06
+	}
2dcf06
+
2dcf06
+	if err = cert.CheckCRLSignature(crl); err != nil {
2dcf06
+		t.Errorf("unexpected error: %s", err)
2dcf06
+	}
2dcf06
+
2dcf06
+	// This is an unrelated OCSP response, which will fail signature verification
2dcf06
+	// but shouldn't return a InsecureAlgorithmError, since SHA1 should be allowed
2dcf06
+	// for OCSP.
2dcf06
+	ocspTBSHex := "30819fa2160414884451ff502a695e2d88f421bad90cf2cecbea7c180f32303133303631383037323434335a30743072304a300906052b0e03021a0500041448b60d38238df8456e4ee5843ea394111802979f0414884451ff502a695e2d88f421bad90cf2cecbea7c021100f78b13b946fc9635d8ab49de9d2148218000180f32303133303631383037323434335aa011180f32303133303632323037323434335a"
2dcf06
+	ocspTBS, err := hex.DecodeString(ocspTBSHex)
2dcf06
+	if err != nil {
2dcf06
+		t.Fatalf("failed to decode OCSP response TBS hex: %s", err)
2dcf06
+	}
2dcf06
+
2dcf06
+	err = cert.CheckSignature(SHA1WithRSA, ocspTBS, nil)
2dcf06
+	if err != rsa.ErrVerification {
2dcf06
+		t.Errorf("unexpected error: %s", err)
2dcf06
+	}
2dcf06
+}