Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CertificateExtensionsCommon.cs
Go to the documentation of this file.
1using System;
4
6
7internal static class CertificateExtensionsCommon
8{
9 public static T GetPublicKey<T>(this X509Certificate2 certificate, Predicate<X509Certificate2> matchesConstraints = null) where T : AsymmetricAlgorithm
10 {
11 if (certificate == null)
12 {
13 throw new ArgumentNullException("certificate");
14 }
15 string expectedOidValue = GetExpectedOidValue<T>();
16 PublicKey publicKey = certificate.PublicKey;
17 Oid oid = publicKey.Oid;
18 if (expectedOidValue != oid.Value)
19 {
20 return null;
21 }
22 if (matchesConstraints != null && !matchesConstraints(certificate))
23 {
24 return null;
25 }
26 if (typeof(T) == typeof(RSA) || typeof(T) == typeof(DSA))
27 {
28 byte[] rawData = publicKey.EncodedKeyValue.RawData;
29 byte[] rawData2 = publicKey.EncodedParameters.RawData;
30 return (T)X509Pal.Instance.DecodePublicKey(oid, rawData, rawData2, certificate.Pal);
31 }
32 if (typeof(T) == typeof(ECDsa))
33 {
34 return (T)(AsymmetricAlgorithm)X509Pal.Instance.DecodeECDsaPublicKey(certificate.Pal);
35 }
36 if (typeof(T) == typeof(ECDiffieHellman))
37 {
38 return (T)(AsymmetricAlgorithm)X509Pal.Instance.DecodeECDiffieHellmanPublicKey(certificate.Pal);
39 }
41 }
42
43 public static T GetPrivateKey<T>(this X509Certificate2 certificate, Predicate<X509Certificate2> matchesConstraints = null) where T : AsymmetricAlgorithm
44 {
45 if (certificate == null)
46 {
47 throw new ArgumentNullException("certificate");
48 }
49 string expectedOidValue = GetExpectedOidValue<T>();
50 if (!certificate.HasPrivateKey || expectedOidValue != certificate.PublicKey.Oid.Value)
51 {
52 return null;
53 }
54 if (matchesConstraints != null && !matchesConstraints(certificate))
55 {
56 return null;
57 }
58 if (typeof(T) == typeof(RSA))
59 {
60 return (T)(AsymmetricAlgorithm)certificate.Pal.GetRSAPrivateKey();
61 }
62 if (typeof(T) == typeof(ECDsa))
63 {
64 return (T)(AsymmetricAlgorithm)certificate.Pal.GetECDsaPrivateKey();
65 }
66 if (typeof(T) == typeof(DSA))
67 {
68 return (T)(AsymmetricAlgorithm)certificate.Pal.GetDSAPrivateKey();
69 }
70 if (typeof(T) == typeof(ECDiffieHellman))
71 {
72 return (T)(AsymmetricAlgorithm)certificate.Pal.GetECDiffieHellmanPrivateKey();
73 }
75 }
76
77 private static string GetExpectedOidValue<T>() where T : AsymmetricAlgorithm
78 {
79 if (typeof(T) == typeof(RSA))
80 {
81 return "1.2.840.113549.1.1.1";
82 }
83 if (typeof(T) == typeof(ECDsa) || typeof(T) == typeof(ECDiffieHellman))
84 {
85 return "1.2.840.10045.2.1";
86 }
87 if (typeof(T) == typeof(DSA))
88 {
89 return "1.2.840.10040.4.1";
90 }
92 }
93}
static T GetPublicKey< T >(this X509Certificate2 certificate, Predicate< X509Certificate2 > matchesConstraints=null)
static T GetPrivateKey< T >(this X509Certificate2 certificate, Predicate< X509Certificate2 > matchesConstraints=null)
static string NotSupported_KeyAlgorithm
Definition SR.cs:114
Definition SR.cs:7