Skip to content

Commit e185b70

Browse files
committed
attempting RSA to RSACSP
Change-Id: I0b8785af9cb77ba3dc7e2d1b8c7f9ea999868f26
1 parent 3cda78c commit e185b70

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

DTLS.Net/Client.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,8 @@ public class Client : IDisposable
8787
public List<TCipherSuite> SupportedCipherSuites { get; }
8888
public byte[] ServerCertificate { get; set; }
8989

90-
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
91-
private CngKey _PrivateKeyRsa;
92-
public CngKey PublicKey { get; set; }
93-
#else
94-
private RSACryptoServiceProvider _PrivateKeyRsa;
95-
public RSACryptoServiceProvider PublicKey { get; set; }
96-
#endif
90+
private RSA _PrivateKeyRsa;
91+
public RSA PublicKey { get; set; }
9792

9893
public Client(EndPoint localEndPoint)
9994
: this(localEndPoint, [])
@@ -966,7 +961,6 @@ public async Task ConnectToServerAsync(EndPoint serverEndPoint, TimeSpan receive
966961
}
967962
}
968963

969-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Other methods are available but RSA is just for windows")]
970964
public void LoadX509Certificate(X509Chain chain)
971965
{
972966
if (chain == null)
@@ -976,17 +970,9 @@ public void LoadX509Certificate(X509Chain chain)
976970

977971
var mainCert = chain.ChainElements[0].Certificate;
978972

979-
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
980-
#pragma warning disable SYSLIB0028 // Type or member is obsolete
981-
_PrivateKeyRsa = ((RSACng)mainCert.PrivateKey).Key;
982-
#pragma warning restore SYSLIB0028 // Type or member is obsolete
983-
#pragma warning disable SYSLIB0027 // Type or member is obsolete
984-
PublicKey = ((RSACng)mainCert.PublicKey.Key).Key;
985-
#pragma warning restore SYSLIB0027 // Type or member is obsolete
986-
#else
987-
_PrivateKeyRsa = (RSACryptoServiceProvider)mainCert.PrivateKey;
988-
PublicKey = (RSACryptoServiceProvider)mainCert.PublicKey.Key;
989-
#endif
973+
974+
_PrivateKeyRsa = mainCert.GetRSAPrivateKey();
975+
PublicKey = mainCert.GetRSAPublicKey();
990976

991977
var certChain = new List<byte[]>();
992978
foreach (var element in chain.ChainElements)

DTLS.Net/Util/TLSUtils.cs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,20 @@ public static byte[] CalculateKeyBlock(TlsContext context, int size)
224224
: TlsUtilities.PRF(context, master_secret, ExporterLabel.key_expansion, seed, size);
225225
}
226226

227-
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
228-
public static byte[] Sign(AsymmetricKeyParameter privateKey, CngKey rsaKey, bool client, Version version, HandshakeInfo handshakeInfo,
227+
public static byte[] Sign(AsymmetricKeyParameter privateKey, RSA rsa, bool client, Version version, HandshakeInfo handshakeInfo,
229228
SignatureHashAlgorithm signatureHashAlgorithm, byte[] hash)
230-
#else
231-
public static byte[] Sign(AsymmetricKeyParameter privateKey, RSACryptoServiceProvider rsaKey, bool client, Version version, HandshakeInfo handshakeInfo,
232-
SignatureHashAlgorithm signatureHashAlgorithm, byte[] hash)
233-
#endif
234229
{
235-
if (privateKey == null && rsaKey == null)
230+
if (privateKey == null && rsa == null)
236231
{
237-
throw new ArgumentException("No key or Rsa CSP provided");
232+
throw new ArgumentException("No key or RSA provided");
238233
}
239234

240235
if (privateKey == null)
241236
{
242237

243238
if (signatureHashAlgorithm.Signature == TSignatureAlgorithm.RSA)
244239
{
245-
return SignRsa(rsaKey, hash);
240+
return SignRsa(rsa, hash);
246241
}
247242

248243
throw new ArgumentException("Need private key for non-RSA Algorithms");
@@ -304,34 +299,21 @@ public static byte[] Sign(AsymmetricKeyParameter privateKey, RSACryptoServicePro
304299
}
305300

306301
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Other methods are available but RSA is just for windows")]
307-
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
308-
public static byte[] SignRsa(CngKey cngKey, byte[] hash)
302+
public static byte[] SignRsa(RSA rsa, byte[] hash)
309303
{
310-
if(cngKey == null)
304+
if (rsa == null)
311305
{
312-
throw new ArgumentNullException(nameof(cngKey));
313-
}
314-
315-
if(hash == null)
316-
{
317-
throw new ArgumentNullException(nameof(hash));
318-
}
319-
320-
var result = NCryptInterop.SignHashRaw(cngKey, hash, cngKey.KeySize);
321-
return result;
322-
}
323-
#else
324-
public static byte[] SignRsa(RSACryptoServiceProvider rsaCsp, byte[] hash)
325-
{
326-
if (rsaCsp == null)
327-
{
328-
throw new ArgumentNullException(nameof(rsaCsp));
306+
throw new ArgumentNullException(nameof(rsa));
329307
}
330308

331309
if (hash == null)
332310
{
333311
throw new ArgumentNullException(nameof(hash));
334312
}
313+
string xmlPrivateKey = rsa.ToXmlString(true);
314+
315+
var rsaCsp = new RSACryptoServiceProvider();
316+
rsaCsp.FromXmlString(xmlPrivateKey);
335317

336318
var cspInfo = rsaCsp.CspKeyContainerInfo;
337319
var provider = new CngProvider(cspInfo.ProviderName);
@@ -348,7 +330,52 @@ public static byte[] SignRsa(RSACryptoServiceProvider rsaCsp, byte[] hash)
348330
return result;
349331
}
350332
}
351-
#endif
333+
334+
//#if NETSTANDARD2_1 || NET6_0_OR_GREATER
335+
// public static byte[] SignRsa(CngKey cngKey, byte[] hash)
336+
// {
337+
// if(cngKey == null)
338+
// {
339+
// throw new ArgumentNullException(nameof(cngKey));
340+
// }
341+
342+
// if(hash == null)
343+
// {
344+
// throw new ArgumentNullException(nameof(hash));
345+
// }
346+
347+
// var result = NCryptInterop.SignHashRaw(cngKey, hash, cngKey.KeySize);
348+
// return result;
349+
// }
350+
//#else
351+
// public static byte[] SignRsa(RSACryptoServiceProvider rsaCsp, byte[] hash)
352+
// {
353+
// if (rsaCsp == null)
354+
// {
355+
// throw new ArgumentNullException(nameof(rsaCsp));
356+
// }
357+
358+
// if (hash == null)
359+
// {
360+
// throw new ArgumentNullException(nameof(hash));
361+
// }
362+
363+
// var cspInfo = rsaCsp.CspKeyContainerInfo;
364+
// var provider = new CngProvider(cspInfo.ProviderName);
365+
// var options = CngKeyOpenOptions.None;
366+
367+
// if (cspInfo.MachineKeyStore)
368+
// {
369+
// options = CngKeyOpenOptions.MachineKey;
370+
// }
371+
372+
// using (var cngKey = CngKey.Open(cspInfo.KeyContainerName, provider, options))
373+
// {
374+
// var result = NCryptInterop.SignHashRaw(cngKey, hash, rsaCsp.KeySize);
375+
// return result;
376+
// }
377+
// }
378+
//#endif
352379

353380
public static byte[] GetVerifyData(Version version, HandshakeInfo handshakeInfo, bool client, bool isClientFinished,
354381
byte[] handshakeHash)

0 commit comments

Comments
 (0)