33
44using System . Collections . Generic ;
55using System . IO ;
6+ using Microsoft . DotNet . XUnitExtensions ;
67using Test . Cryptography ;
78using Test . IO . Streams ;
89using Xunit ;
@@ -26,14 +27,14 @@ public void NullArray_Throws()
2627 {
2728 using ( RSA rsa = RSAFactory . Create ( ) )
2829 {
29- AssertExtensions . Throws < ArgumentNullException > ( "data" , ( ) => SignData ( rsa , null , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
30- AssertExtensions . Throws < ArgumentNullException > ( "hash" , ( ) => SignHash ( rsa , null , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
30+ AssertExtensions . Throws < ArgumentNullException > ( "data" , ( ) => SignData ( rsa , null , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
31+ AssertExtensions . Throws < ArgumentNullException > ( "hash" , ( ) => SignHash ( rsa , null , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
3132
32- AssertExtensions . Throws < ArgumentNullException > ( "data" , ( ) => VerifyData ( rsa , null , new byte [ 1 ] , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
33- AssertExtensions . Throws < ArgumentNullException > ( "hash" , ( ) => VerifyHash ( rsa , null , new byte [ 1 ] , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
33+ AssertExtensions . Throws < ArgumentNullException > ( "data" , ( ) => VerifyData ( rsa , null , new byte [ 1 ] , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
34+ AssertExtensions . Throws < ArgumentNullException > ( "hash" , ( ) => VerifyHash ( rsa , null , new byte [ 1 ] , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
3435
35- AssertExtensions . Throws < ArgumentNullException > ( "signature" , ( ) => VerifyData ( rsa , new byte [ 1 ] , null , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
36- AssertExtensions . Throws < ArgumentNullException > ( "signature" , ( ) => VerifyHash ( rsa , new byte [ 1 ] , null , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
36+ AssertExtensions . Throws < ArgumentNullException > ( "signature" , ( ) => VerifyData ( rsa , new byte [ 1 ] , null , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
37+ AssertExtensions . Throws < ArgumentNullException > ( "signature" , ( ) => VerifyHash ( rsa , new byte [ 1 ] , null , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
3738 }
3839 }
3940 }
@@ -72,10 +73,10 @@ public void NullPadding_Throws()
7273 {
7374 using ( RSA rsa = RSAFactory . Create ( ) )
7475 {
75- AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => SignData ( rsa , new byte [ 1 ] , HashAlgorithmName . SHA1 , null ) ) ;
76- AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => SignHash ( rsa , new byte [ 1 ] , HashAlgorithmName . SHA1 , null ) ) ;
77- AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => VerifyData ( rsa , new byte [ 1 ] , new byte [ 1 ] , HashAlgorithmName . SHA1 , null ) ) ;
78- AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => VerifyHash ( rsa , new byte [ 1 ] , new byte [ 1 ] , HashAlgorithmName . SHA1 , null ) ) ;
76+ AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => SignData ( rsa , new byte [ 1 ] , HashAlgorithmName . SHA256 , null ) ) ;
77+ AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => SignHash ( rsa , new byte [ 1 ] , HashAlgorithmName . SHA256 , null ) ) ;
78+ AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => VerifyData ( rsa , new byte [ 1 ] , new byte [ 1 ] , HashAlgorithmName . SHA256 , null ) ) ;
79+ AssertExtensions . Throws < ArgumentNullException > ( "padding" , ( ) => VerifyHash ( rsa , new byte [ 1 ] , new byte [ 1 ] , HashAlgorithmName . SHA256 , null ) ) ;
7980 }
8081 }
8182
@@ -87,7 +88,7 @@ public void UseAfterDispose(bool importKey)
8788 RSA rsa = importKey ? RSAFactory . Create ( TestData . RSA2048Params ) : RSAFactory . Create ( 1024 ) ;
8889 byte [ ] data = TestData . HelloBytes ;
8990 byte [ ] sig ;
90- HashAlgorithmName alg = HashAlgorithmName . SHA1 ;
91+ HashAlgorithmName alg = HashAlgorithmName . SHA256 ;
9192 RSASignaturePadding padding = RSASignaturePadding . Pkcs1 ;
9293
9394 using ( rsa )
@@ -115,12 +116,12 @@ public void InvalidKeySize_DoesNotInvalidateKey()
115116 {
116117 using ( RSA rsa = RSAFactory . Create ( ) )
117118 {
118- byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
119+ byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
119120
120121 // A 2049-bit key is hard to describe, none of the providers support it.
121122 Assert . ThrowsAny < CryptographicException > ( ( ) => rsa . KeySize = 2049 ) ;
122123
123- Assert . True ( VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
124+ Assert . True ( VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
124125 }
125126 }
126127
@@ -143,11 +144,11 @@ public void SignEmptyHash()
143144 using ( RSA rsa = RSAFactory . Create ( ) )
144145 {
145146 Assert . ThrowsAny < CryptographicException > (
146- ( ) => SignHash ( rsa , Array . Empty < byte > ( ) , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ) ;
147+ ( ) => SignHash ( rsa , Array . Empty < byte > ( ) , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ) ;
147148 }
148149 }
149150
150- [ Fact ]
151+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
151152 public void ExpectedSignature_SHA1_384 ( )
152153 {
153154 byte [ ] expectedSignature =
@@ -178,7 +179,7 @@ public void ExpectedSignature_SHA1_384()
178179 }
179180 }
180181
181- [ Fact ]
182+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
182183 public void ExpectedSignature_SHA1_1032 ( )
183184 {
184185 byte [ ] expectedSignature =
@@ -205,7 +206,7 @@ public void ExpectedSignature_SHA1_1032()
205206 ExpectSignature ( expectedSignature , TestData . HelloBytes , "SHA1" , TestData . RSA1032Parameters ) ;
206207 }
207208
208- [ Fact ]
209+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
209210 public void ExpectedSignature_SHA1_2048 ( )
210211 {
211212 byte [ ] expectedSignature = new byte [ ]
@@ -350,7 +351,7 @@ public void ExpectSignature_SHA256_1024_Stream()
350351 Assert . Equal ( expectedSignature , signature ) ;
351352 }
352353
353- [ Fact ]
354+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
354355 public void VerifySignature_SHA1_384 ( )
355356 {
356357 byte [ ] signature =
@@ -366,7 +367,7 @@ public void VerifySignature_SHA1_384()
366367 VerifySignature ( signature , TestData . HelloBytes , "SHA1" , TestData . RSA384Parameters ) ;
367368 }
368369
369- [ Fact ]
370+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
370371 public void VerifySignature_SHA1_1032 ( )
371372 {
372373 byte [ ] signature =
@@ -393,7 +394,7 @@ public void VerifySignature_SHA1_1032()
393394 VerifySignature ( signature , TestData . HelloBytes , "SHA1" , TestData . RSA1032Parameters ) ;
394395 }
395396
396- [ Fact ]
397+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
397398 public void VerifySignature_SHA1_2048 ( )
398399 {
399400 byte [ ] signature = new byte [ ]
@@ -516,8 +517,12 @@ public static IEnumerable<object[]> RoundTripTheories
516517 {
517518 foreach ( RSAParameters rsaParameters in new [ ] { TestData . RSA1024Params , TestData . RSA2048Params } )
518519 {
520+ if ( RSAFactory . SupportsSha1Signatures )
521+ {
522+ yield return new object [ ] { nameof ( HashAlgorithmName . SHA1 ) , rsaParameters } ;
523+ }
524+
519525 yield return new object [ ] { nameof ( HashAlgorithmName . MD5 ) , rsaParameters } ;
520- yield return new object [ ] { nameof ( HashAlgorithmName . SHA1 ) , rsaParameters } ;
521526 yield return new object [ ] { nameof ( HashAlgorithmName . SHA256 ) , rsaParameters } ;
522527 }
523528
@@ -532,8 +537,8 @@ public void NegativeVerify_WrongAlgorithm()
532537 using ( RSA rsa = RSAFactory . Create ( ) )
533538 {
534539 rsa . ImportParameters ( TestData . RSA2048Params ) ;
535- byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
536- bool signatureMatched = VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
540+ byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
541+ bool signatureMatched = VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA384 , RSASignaturePadding . Pkcs1 ) ;
537542
538543 Assert . False ( signatureMatched ) ;
539544 }
@@ -545,12 +550,12 @@ public void NegativeVerify_WrongSignature()
545550 using ( RSA rsa = RSAFactory . Create ( ) )
546551 {
547552 rsa . ImportParameters ( TestData . RSA2048Params ) ;
548- byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
553+ byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
549554
550555 // Invalidate the signature.
551556 signature [ 0 ] = unchecked ( ( byte ) ~ signature [ 0 ] ) ;
552557
553- bool signatureMatched = VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
558+ bool signatureMatched = VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
554559 Assert . False ( signatureMatched ) ;
555560 }
556561 }
@@ -561,8 +566,8 @@ public void NegativeVerify_TamperedData()
561566 using ( RSA rsa = RSAFactory . Create ( ) )
562567 {
563568 rsa . ImportParameters ( TestData . RSA2048Params ) ;
564- byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
565- bool signatureMatched = VerifyData ( rsa , Array . Empty < byte > ( ) , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
569+ byte [ ] signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
570+ bool signatureMatched = VerifyData ( rsa , Array . Empty < byte > ( ) , signature , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
566571 Assert . False ( signatureMatched ) ;
567572 }
568573 }
@@ -575,13 +580,13 @@ public void NegativeVerify_BadKeysize()
575580 using ( RSA rsa = RSAFactory . Create ( ) )
576581 {
577582 rsa . ImportParameters ( TestData . RSA2048Params ) ;
578- signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
583+ signature = SignData ( rsa , TestData . HelloBytes , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
579584 }
580585
581586 using ( RSA rsa = RSAFactory . Create ( ) )
582587 {
583588 rsa . ImportParameters ( TestData . RSA1024Params ) ;
584- bool signatureMatched = VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
589+ bool signatureMatched = VerifyData ( rsa , TestData . HelloBytes , signature , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
585590
586591 Assert . False ( signatureMatched ) ;
587592 }
@@ -610,7 +615,7 @@ public void PkcsSignHash_MismatchedHashSize()
610615 }
611616 }
612617
613- [ Fact ]
618+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
614619 public void ExpectedHashSignature_SHA1_2048 ( )
615620 {
616621 byte [ ] expectedHashSignature = new byte [ ]
@@ -741,7 +746,7 @@ public void ExpectedHashSignature_SHA256_2048()
741746 ExpectHashSignature ( expectedHashSignature , dataHash , "SHA256" , TestData . RSA2048Params ) ;
742747 }
743748
744- [ Fact ]
749+ [ ConditionalFact ( typeof ( RSAFactory ) , nameof ( RSAFactory . SupportsSha1Signatures ) ) ]
745750 public void VerifyHashSignature_SHA1_2048 ( )
746751 {
747752 byte [ ] hashSignature = new byte [ ]
@@ -872,14 +877,19 @@ public void VerifyHashSignature_SHA256_2048()
872877 VerifyHashSignature ( hashSignature , dataHash , "SHA256" , TestData . RSA2048Params ) ;
873878 }
874879
875- [ Theory ]
880+ [ ConditionalTheory ]
876881 [ InlineData ( "SHA256" ) ]
877882 [ InlineData ( "SHA384" ) ]
878883 [ InlineData ( "SHA512" ) ]
879884 [ InlineData ( "MD5" ) ]
880885 [ InlineData ( "SHA1" ) ]
881886 public void PssRoundtrip ( string hashAlgorithmName )
882887 {
888+ if ( ! RSAFactory . SupportsSha1Signatures && hashAlgorithmName == "SHA1" )
889+ {
890+ throw new SkipTestException ( "Platform does not support RSA with SHA1 signatures." ) ;
891+ }
892+
883893 RSAParameters privateParameters = TestData . RSA2048Params ;
884894 RSAParameters publicParameters = new RSAParameters
885895 {
0 commit comments