Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PublicKey.cs
Go to the documentation of this file.
7
9
10public sealed class PublicKey
11{
12 private readonly Oid _oid;
13
15
16 public AsnEncodedData EncodedKeyValue { get; private set; }
17
18 public AsnEncodedData EncodedParameters { get; private set; }
19
20 [Obsolete("PublicKey.Key is obsolete. Use the appropriate method to get the public key, such as GetRSAPublicKey.", DiagnosticId = "SYSLIB0027", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
22 {
23 get
24 {
25 if (_key == null)
26 {
27 string value = _oid.Value;
28 if (!(value == "1.2.840.113549.1.1.1") && !(value == "1.2.840.10040.4.1"))
29 {
31 }
33 }
34 return _key;
35 }
36 }
37
38 public Oid Oid => _oid;
39
40 public PublicKey(Oid oid, AsnEncodedData parameters, AsnEncodedData keyValue)
41 {
42 _oid = oid;
43 EncodedParameters = new AsnEncodedData(parameters);
44 EncodedKeyValue = new AsnEncodedData(keyValue);
45 }
46
48 {
49 byte[] array = key.ExportSubjectPublicKeyInfo();
50 DecodeSubjectPublicKeyInfo(array, out var oid, out var parameters, out var keyValue);
51 _oid = oid;
52 EncodedParameters = parameters;
53 EncodedKeyValue = keyValue;
54 }
55
56 public bool TryExportSubjectPublicKeyInfo(Span<byte> destination, out int bytesWritten)
57 {
58 return EncodeSubjectPublicKeyInfo().TryEncode(destination, out bytesWritten);
59 }
60
62 {
64 }
65
67 {
68 Oid oid;
69 AsnEncodedData parameters;
70 AsnEncodedData keyValue;
71 int num = DecodeSubjectPublicKeyInfo(source, out oid, out parameters, out keyValue);
72 bytesRead = num;
73 return new PublicKey(oid, parameters, keyValue);
74 }
75
77 {
78 if (_oid.Value != "1.2.840.113549.1.1.1")
79 {
80 return null;
81 }
82 RSA rSA = RSA.Create();
83 try
84 {
86 return rSA;
87 }
88 catch
89 {
90 rSA.Dispose();
91 throw;
92 }
93 }
94
95 [UnsupportedOSPlatform("ios")]
96 [UnsupportedOSPlatform("tvos")]
98 {
99 if (_oid.Value != "1.2.840.10040.4.1")
100 {
101 return null;
102 }
103 DSA dSA = DSA.Create();
104 try
105 {
107 return dSA;
108 }
109 catch
110 {
111 dSA.Dispose();
112 throw;
113 }
114 }
115
117 {
118 if (_oid.Value != "1.2.840.10045.2.1")
119 {
120 return null;
121 }
122 ECDsa eCDsa = ECDsa.Create();
123 try
124 {
126 return eCDsa;
127 }
128 catch
129 {
130 eCDsa.Dispose();
131 throw;
132 }
133 }
134
136 {
137 if (_oid.Value != "1.2.840.10045.2.1")
138 {
139 return null;
140 }
141 ECDiffieHellman eCDiffieHellman = ECDiffieHellman.Create();
142 try
143 {
144 eCDiffieHellman.ImportSubjectPublicKeyInfo(ExportSubjectPublicKeyInfo(), out var _);
145 return eCDiffieHellman;
146 }
147 catch
148 {
149 eCDiffieHellman.Dispose();
150 throw;
151 }
152 }
153
155 {
157 subjectPublicKeyInfoAsn.Algorithm = new System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn
158 {
159 Algorithm = (_oid.Value ?? string.Empty),
160 Parameters = EncodedParameters.RawData
161 };
162 subjectPublicKeyInfoAsn.SubjectPublicKey = EncodedKeyValue.RawData;
163 System.Security.Cryptography.Asn1.SubjectPublicKeyInfoAsn subjectPublicKeyInfoAsn2 = subjectPublicKeyInfoAsn;
164 AsnWriter asnWriter = new AsnWriter(AsnEncodingRules.DER);
165 subjectPublicKeyInfoAsn2.Encode(asnWriter);
166 return asnWriter;
167 }
168
169 private unsafe static int DecodeSubjectPublicKeyInfo(ReadOnlySpan<byte> source, out Oid oid, out AsnEncodedData parameters, out AsnEncodedData keyValue)
170 {
171 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
172 {
173 using MemoryManager<byte> memoryManager = new System.Buffers.PointerMemoryManager<byte>(pointer, source.Length);
175 int length;
177 try
178 {
179 length = reader.PeekEncodedValue().Length;
180 System.Security.Cryptography.Asn1.SubjectPublicKeyInfoAsn.Decode(ref reader, memoryManager.Memory, out decoded);
181 }
182 catch (AsnContentException inner)
183 {
185 }
186 oid = new Oid(decoded.Algorithm.Algorithm, null);
187 parameters = new AsnEncodedData(decoded.Algorithm.Parameters?.ToArray() ?? Array.Empty<byte>());
188 keyValue = new AsnEncodedData(decoded.SubjectPublicKey.ToArray());
189 return length;
190 }
191 }
192}
bool TryEncode(Span< byte > destination, out int bytesWritten)
Definition AsnWriter.cs:173
int Encode(Span< byte > destination)
Definition AsnWriter.cs:195
static string Cryptography_Der_Invalid_Encoding
Definition SR.cs:50
static string NotSupported_KeyAlgorithm
Definition SR.cs:114
Definition SR.cs:7
override void ImportSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
Definition DSA.cs:638
static new? DSA Create(string algName)
Definition DSA.cs:19
override void ImportSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
static new? ECDiffieHellman Create(string algorithm)
static new? ECDsa Create(string algorithm)
Definition ECDsa.cs:20
override void ImportSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
Definition ECDsa.cs:689
static new? RSA Create(string algName)
Definition RSA.cs:21
unsafe override void ImportSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
Definition RSA.cs:481
bool TryExportSubjectPublicKeyInfo(Span< byte > destination, out int bytesWritten)
Definition PublicKey.cs:56
static PublicKey CreateFromSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
Definition PublicKey.cs:66
static unsafe int DecodeSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out Oid oid, out AsnEncodedData parameters, out AsnEncodedData keyValue)
Definition PublicKey.cs:169
PublicKey(Oid oid, AsnEncodedData parameters, AsnEncodedData keyValue)
Definition PublicKey.cs:40
static void Decode(ref AsnValueReader reader, ReadOnlyMemory< byte > rebind, out SubjectPublicKeyInfoAsn decoded)