1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
@@ -39,6 +39,7 @@ private readonly struct ECCPublicKeyBlob
39
39
// => ECDiffieHellmanPublicKey.ToByteArray() is not supported in Unix
40
40
internal static byte [ ] GetECDiffieHellmanPublicKeyBlob ( ECDiffieHellman ecDiffieHellman )
41
41
{
42
+ #if NET6_0_OR_GREATER
42
43
byte [ ] keyBlob = new byte [ ECCPublicKeyBlob . Size ] ;
43
44
44
45
// Set magic number
@@ -53,6 +54,16 @@ internal static byte[] GetECDiffieHellmanPublicKeyBlob(ECDiffieHellman ecDiffieH
53
54
Buffer . BlockCopy ( ecPoint . X , 0 , keyBlob , ECCPublicKeyBlob . HeaderSize , ECCPublicKeyBlob . KeySize ) ;
54
55
Buffer . BlockCopy ( ecPoint . Y , 0 , keyBlob , ECCPublicKeyBlob . HeaderSize + ECCPublicKeyBlob . KeySize , ECCPublicKeyBlob . KeySize ) ;
55
56
return keyBlob ;
57
+ #else
58
+ if ( ecDiffieHellman is ECDiffieHellmanCng cng )
59
+ {
60
+ return cng . Key . Export ( CngKeyBlobFormat . EccPublicBlob ) ;
61
+ }
62
+ else
63
+ {
64
+ throw new InvalidOperationException ( ) ;
65
+ }
66
+ #endif
56
67
}
57
68
58
69
// The RSA public key blob is structured as follows:
@@ -75,6 +86,7 @@ private readonly struct RSAPublicKeyBlob
75
86
76
87
internal static RSA CreateRSAFromPublicKeyBlob ( byte [ ] keyBlob )
77
88
{
89
+ #if NET6_0_OR_GREATER
78
90
Debug . Assert ( keyBlob . Length == RSAPublicKeyBlob . Size , $ "RSA public key blob was not the expected length. Actual: { keyBlob . Length } . Expected: { RSAPublicKeyBlob . Size } ") ;
79
91
80
92
byte [ ] exponent = new byte [ RSAPublicKeyBlob . ExponentSize ] ;
@@ -87,10 +99,15 @@ internal static RSA CreateRSAFromPublicKeyBlob(byte[] keyBlob)
87
99
Modulus = modulus
88
100
} ;
89
101
return RSA . Create ( rsaParameters ) ;
102
+ #else
103
+ CngKey key = CngKey . Import ( keyBlob , CngKeyBlobFormat . GenericPublicBlob ) ;
104
+ return new RSACng ( key ) ;
105
+ #endif
90
106
}
91
107
92
108
internal static ECDiffieHellman CreateECDiffieHellmanFromPublicKeyBlob ( byte [ ] keyBlob )
93
109
{
110
+ #if NET6_0_OR_GREATER
94
111
Debug . Assert ( keyBlob . Length == ECCPublicKeyBlob . Size , $ "ECC public key blob was not the expected length. Actual: { keyBlob . Length } . Expected: { ECCPublicKeyBlob . Size } ") ;
95
112
96
113
byte [ ] x = new byte [ ECCPublicKeyBlob . KeySize ] ;
@@ -109,27 +126,61 @@ internal static ECDiffieHellman CreateECDiffieHellmanFromPublicKeyBlob(byte[] ke
109
126
} ;
110
127
111
128
return ECDiffieHellman . Create ( parameters ) ;
129
+ #else
130
+ CngKey key = CngKey . Import ( keyBlob , CngKeyBlobFormat . GenericPublicBlob ) ;
131
+ return new ECDiffieHellmanCng ( key ) ;
132
+ #endif
112
133
}
113
134
114
135
internal static ECDiffieHellman CreateECDiffieHellman ( int keySize )
115
136
{
137
+ #if NET6_0_OR_GREATER
116
138
// platform agnostic creates a key of the correct size but does not
117
139
// set the key derivation type or algorithm, these must be set by calling
118
140
// DeriveKeyFromHash later in DeriveKey
119
141
ECDiffieHellman clientDHKey = ECDiffieHellman . Create ( ) ;
120
142
clientDHKey . KeySize = keySize ;
143
+ #else
144
+ // Cng sets the key size and hash algorithm at creation time and these
145
+ // parameters are then used later when DeriveKeyMaterial is called
146
+ ECDiffieHellmanCng clientDHKey = new ECDiffieHellmanCng ( keySize ) ;
147
+ clientDHKey . KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction . Hash ;
148
+ clientDHKey . HashAlgorithm = CngAlgorithm . Sha256 ;
149
+ #endif
121
150
return clientDHKey ;
122
151
}
123
152
124
- internal static byte [ ] DeriveKey ( ECDiffieHellman ecd , ECDiffieHellmanPublicKey publicKey )
153
+ internal static byte [ ] DeriveKey ( ECDiffieHellman ecDiffieHellman , ECDiffieHellmanPublicKey publicKey )
125
154
{
155
+ #if NET6_0_OR_GREATER
126
156
// see notes in CreateECDDiffieHellman
127
- return ecd . DeriveKeyFromHash ( publicKey , HashAlgorithmName . SHA256 ) ;
157
+ return ecDiffieHellman . DeriveKeyFromHash ( publicKey , HashAlgorithmName . SHA256 ) ;
158
+ #else
159
+ if ( ecDiffieHellman is ECDiffieHellmanCng cng )
160
+ {
161
+ return cng . DeriveKeyMaterial ( publicKey ) ;
162
+ }
163
+ else
164
+ {
165
+ throw new InvalidOperationException ( ) ;
166
+ }
167
+ #endif
128
168
}
129
169
130
170
internal static RSA GetRSAFromCertificate ( X509Certificate2 certificate )
131
171
{
172
+ #if NET6_0_OR_GREATER
132
173
return certificate . GetRSAPublicKey ( ) ;
174
+ #else
175
+ RSAParameters parameters ;
176
+ using ( RSA rsaCsp = certificate . GetRSAPublicKey ( ) )
177
+ {
178
+ parameters = rsaCsp . ExportParameters ( includePrivateParameters : false ) ;
179
+ }
180
+ RSACng rsaCng = new RSACng ( ) ;
181
+ rsaCng . ImportParameters ( parameters ) ;
182
+ return rsaCng ;
183
+ #endif
133
184
}
134
185
}
135
186
}
0 commit comments