4
4
using System . Collections . Generic ;
5
5
using System . IO ;
6
6
using System . Runtime . InteropServices ;
7
+ using System . Security . Cryptography . X509Certificates . Tests . CertificateCreation ;
7
8
using System . Threading ;
8
9
using Microsoft . DotNet . XUnitExtensions ;
9
10
using Test . Cryptography ;
@@ -24,6 +25,108 @@ public CertTests(ITestOutputHelper output)
24
25
_log = output ;
25
26
}
26
27
28
+ [ Fact ]
29
+ public static void PrivateKey_FromCertificate_CanExportPrivate_ECDsa ( )
30
+ {
31
+ using ( ECDsa ca = ECDsa . Create ( ECCurve . NamedCurves . nistP256 ) )
32
+ {
33
+ CertificateRequest req = new ( "CN=potatos" , ca , HashAlgorithmName . SHA256 ) ;
34
+
35
+ using ( X509Certificate2 cert = req . CreateSelfSigned ( DateTimeOffset . Now , DateTimeOffset . Now . AddDays ( 3 ) ) )
36
+ using ( ECDsa certKey = cert . GetECDsaPrivateKey ( ) )
37
+ {
38
+ ECParameters certParameters = certKey . ExportParameters ( true ) ;
39
+ ECParameters originalParameters = ca . ExportParameters ( true ) ;
40
+ AssertExtensions . SequenceEqual ( originalParameters . D , certParameters . D ) ;
41
+ }
42
+ }
43
+ }
44
+
45
+ [ Fact ]
46
+ public static void PrivateKey_FromCertificate_CanExportPrivate_RSA ( )
47
+ {
48
+ using ( RSA ca = RSA . Create ( 2048 ) )
49
+ {
50
+ CertificateRequest req = new ( "CN=potatos" , ca , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
51
+
52
+ using ( X509Certificate2 cert = req . CreateSelfSigned ( DateTimeOffset . Now , DateTimeOffset . Now . AddDays ( 3 ) ) )
53
+ using ( RSA certKey = cert . GetRSAPrivateKey ( ) )
54
+ {
55
+ RSAParameters certParameters = certKey . ExportParameters ( true ) ;
56
+ RSAParameters originalParameters = ca . ExportParameters ( true ) ;
57
+ AssertExtensions . SequenceEqual ( originalParameters . P , certParameters . P ) ;
58
+ AssertExtensions . SequenceEqual ( originalParameters . Q , certParameters . Q ) ;
59
+ }
60
+ }
61
+ }
62
+
63
+ [ Fact ]
64
+ [ SkipOnPlatform ( PlatformSupport . MobileAppleCrypto , "DSA is not available" ) ]
65
+ public static void PrivateKey_FromCertificate_CanExportPrivate_DSA ( )
66
+ {
67
+ DSAParameters originalParameters = TestData . GetDSA1024Params ( ) ;
68
+
69
+ using ( DSA ca = DSA . Create ( ) )
70
+ {
71
+ ca . ImportParameters ( originalParameters ) ;
72
+ DSAX509SignatureGenerator gen = new DSAX509SignatureGenerator ( ca ) ;
73
+ X500DistinguishedName dn = new X500DistinguishedName ( "CN=potatos" ) ;
74
+
75
+ CertificateRequest req = new CertificateRequest (
76
+ dn ,
77
+ gen . PublicKey ,
78
+ HashAlgorithmName . SHA1 ) ;
79
+
80
+ using ( X509Certificate2 cert = req . Create ( dn , gen , DateTimeOffset . Now , DateTimeOffset . Now . AddDays ( 3 ) , new byte [ ] { 1 , 2 , 3 } ) )
81
+ using ( X509Certificate2 certWithKey = cert . CopyWithPrivateKey ( ca ) )
82
+ using ( DSA certKey = certWithKey . GetDSAPrivateKey ( ) )
83
+ {
84
+ DSAParameters certParameters = certKey . ExportParameters ( true ) ;
85
+ AssertExtensions . SequenceEqual ( originalParameters . X , certParameters . X ) ;
86
+ }
87
+ }
88
+ }
89
+
90
+ [ Fact ]
91
+ public static void PrivateKey_FromCertificate_CanExportPrivate_ECDiffieHellman ( )
92
+ {
93
+ using ( ECDsa ca = ECDsa . Create ( ECCurve . NamedCurves . nistP256 ) )
94
+ using ( ECDiffieHellman ecdh = ECDiffieHellman . Create ( ECCurve . NamedCurves . nistP256 ) )
95
+ {
96
+ CertificateRequest issuerRequest = new CertificateRequest (
97
+ new X500DistinguishedName ( "CN=root" ) ,
98
+ ca ,
99
+ HashAlgorithmName . SHA256 ) ;
100
+
101
+ issuerRequest . CertificateExtensions . Add (
102
+ new X509BasicConstraintsExtension ( true , false , 0 , true ) ) ;
103
+
104
+ CertificateRequest request = new CertificateRequest (
105
+ new X500DistinguishedName ( "CN=potato" ) ,
106
+ new PublicKey ( ecdh ) ,
107
+ HashAlgorithmName . SHA256 ) ;
108
+
109
+ request . CertificateExtensions . Add (
110
+ new X509BasicConstraintsExtension ( false , false , 0 , true ) ) ;
111
+ request . CertificateExtensions . Add (
112
+ new X509KeyUsageExtension ( X509KeyUsageFlags . KeyAgreement , true ) ) ;
113
+
114
+ DateTimeOffset notBefore = DateTimeOffset . UtcNow ;
115
+ DateTimeOffset notAfter = notBefore . AddDays ( 30 ) ;
116
+ byte [ ] serial = new byte [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
117
+
118
+ using ( X509Certificate2 issuer = issuerRequest . CreateSelfSigned ( notBefore , notAfter ) )
119
+ using ( X509Certificate2 cert = request . Create ( issuer , notBefore , notAfter , serial ) )
120
+ using ( X509Certificate2 certWithKey = cert . CopyWithPrivateKey ( ecdh ) )
121
+ using ( ECDiffieHellman certKey = certWithKey . GetECDiffieHellmanPrivateKey ( ) )
122
+ {
123
+ ECParameters certParameters = certKey . ExportParameters ( true ) ;
124
+ ECParameters originalParameters = ecdh . ExportParameters ( true ) ;
125
+ AssertExtensions . SequenceEqual ( originalParameters . D , certParameters . D ) ;
126
+ }
127
+ }
128
+ }
129
+
27
130
[ Fact ]
28
131
public static void RaceUseAndDisposeDoesNotCrash ( )
29
132
{
@@ -79,7 +182,7 @@ public static void X509CertTest()
79
182
// GetSerialNumber() returns in little-endian order.
80
183
Array . Reverse ( expectedSerial ) ;
81
184
AssertExtensions . SequenceEqual ( expectedSerial , serial1 ) ;
82
-
185
+
83
186
Assert . Equal ( "1.2.840.113549.1.1.1" , cert . GetKeyAlgorithm ( ) ) ;
84
187
85
188
int pklen = cert . GetPublicKey ( ) . Length ;
0 commit comments