@@ -2924,30 +2924,15 @@ func TestCreateCertificateBrokenSigner(t *testing.T) {
2924
2924
}
2925
2925
2926
2926
func TestCreateCertificateLegacy (t * testing.T ) {
2927
- ecdsaPriv , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
2928
- if err != nil {
2929
- t .Fatalf ("Failed to generate ECDSA key: %s" , err )
2927
+ sigAlg := MD5WithRSA
2928
+ template := & Certificate {
2929
+ SerialNumber : big .NewInt (10 ),
2930
+ DNSNames : []string {"example.com" },
2931
+ SignatureAlgorithm : sigAlg ,
2930
2932
}
2931
-
2932
- for _ , sigAlg := range []SignatureAlgorithm {
2933
- MD5WithRSA , SHA1WithRSA , ECDSAWithSHA1 ,
2934
- } {
2935
- template := & Certificate {
2936
- SerialNumber : big .NewInt (10 ),
2937
- DNSNames : []string {"example.com" },
2938
- SignatureAlgorithm : sigAlg ,
2939
- }
2940
- var k crypto.Signer
2941
- switch sigAlg {
2942
- case MD5WithRSA , SHA1WithRSA :
2943
- k = testPrivateKey
2944
- case ECDSAWithSHA1 :
2945
- k = ecdsaPriv
2946
- }
2947
- _ , err := CreateCertificate (rand .Reader , template , template , k .Public (), & brokenSigner {k .Public ()})
2948
- if err != nil {
2949
- t .Fatalf ("CreateCertificate failed when SignatureAlgorithm = %v: %s" , sigAlg , err )
2950
- }
2933
+ _ , err := CreateCertificate (rand .Reader , template , template , testPrivateKey .Public (), & brokenSigner {testPrivateKey .Public ()})
2934
+ if err != nil {
2935
+ t .Fatalf ("CreateCertificate failed when SignatureAlgorithm = %v: %s" , sigAlg , err )
2951
2936
}
2952
2937
}
2953
2938
@@ -3396,3 +3381,66 @@ func TestParseUniqueID(t *testing.T) {
3396
3381
t .Fatalf ("unexpected number of extensions (probably because the extension section was not parsed): got %d, want 7" , len (cert .Extensions ))
3397
3382
}
3398
3383
}
3384
+
3385
+ func TestDisableSHA1ForCertOnly (t * testing.T ) {
3386
+ defer func (old bool ) { debugAllowSHA1 = old }(debugAllowSHA1 )
3387
+ debugAllowSHA1 = false
3388
+
3389
+ tmpl := & Certificate {
3390
+ SerialNumber : big .NewInt (1 ),
3391
+ NotBefore : time .Now ().Add (- time .Hour ),
3392
+ NotAfter : time .Now ().Add (time .Hour ),
3393
+ SignatureAlgorithm : SHA1WithRSA ,
3394
+ BasicConstraintsValid : true ,
3395
+ IsCA : true ,
3396
+ KeyUsage : KeyUsageCertSign | KeyUsageCRLSign ,
3397
+ }
3398
+ certDER , err := CreateCertificate (rand .Reader , tmpl , tmpl , rsaPrivateKey .Public (), rsaPrivateKey )
3399
+ if err != nil {
3400
+ t .Fatalf ("failed to generate test cert: %s" , err )
3401
+ }
3402
+ cert , err := ParseCertificate (certDER )
3403
+ if err != nil {
3404
+ t .Fatalf ("failed to parse test cert: %s" , err )
3405
+ }
3406
+
3407
+ err = cert .CheckSignatureFrom (cert )
3408
+ if err == nil {
3409
+ t .Error ("expected CheckSignatureFrom to fail" )
3410
+ } else if _ , ok := err .(InsecureAlgorithmError ); ! ok {
3411
+ t .Errorf ("expected InsecureAlgorithmError error, got %T" , err )
3412
+ }
3413
+
3414
+ crlDER , err := CreateRevocationList (rand .Reader , & RevocationList {
3415
+ SignatureAlgorithm : SHA1WithRSA ,
3416
+ Number : big .NewInt (1 ),
3417
+ ThisUpdate : time .Now ().Add (- time .Hour ),
3418
+ NextUpdate : time .Now ().Add (time .Hour ),
3419
+ }, cert , rsaPrivateKey )
3420
+ if err != nil {
3421
+ t .Fatalf ("failed to generate test CRL: %s" , err )
3422
+ }
3423
+ // TODO(rolandshoemaker): this should be ParseRevocationList once it lands
3424
+ crl , err := ParseCRL (crlDER )
3425
+ if err != nil {
3426
+ t .Fatalf ("failed to parse test CRL: %s" , err )
3427
+ }
3428
+
3429
+ if err = cert .CheckCRLSignature (crl ); err != nil {
3430
+ t .Errorf ("unexpected error: %s" , err )
3431
+ }
3432
+
3433
+ // This is an unrelated OCSP response, which will fail signature verification
3434
+ // but shouldn't return a InsecureAlgorithmError, since SHA1 should be allowed
3435
+ // for OCSP.
3436
+ ocspTBSHex := "30819fa2160414884451ff502a695e2d88f421bad90cf2cecbea7c180f32303133303631383037323434335a30743072304a300906052b0e03021a0500041448b60d38238df8456e4ee5843ea394111802979f0414884451ff502a695e2d88f421bad90cf2cecbea7c021100f78b13b946fc9635d8ab49de9d2148218000180f32303133303631383037323434335aa011180f32303133303632323037323434335a"
3437
+ ocspTBS , err := hex .DecodeString (ocspTBSHex )
3438
+ if err != nil {
3439
+ t .Fatalf ("failed to decode OCSP response TBS hex: %s" , err )
3440
+ }
3441
+
3442
+ err = cert .CheckSignature (SHA1WithRSA , ocspTBS , nil )
3443
+ if err != rsa .ErrVerification {
3444
+ t .Errorf ("unexpected error: %s" , err )
3445
+ }
3446
+ }
0 commit comments