Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CngPkcs8.cs
Go to the documentation of this file.
5
7
8internal static class CngPkcs8
9{
10 internal struct Pkcs8Response
11 {
12 internal CngKey Key;
13
14 internal string GetAlgorithmGroup()
15 {
17 }
18
19 internal void FreeKey()
20 {
21 Key.Dispose();
22 }
23 }
24
25 private static readonly PbeParameters s_platformParameters = new PbeParameters(PbeEncryptionAlgorithm.TripleDes3KeyPkcs12, HashAlgorithmName.SHA1, 1);
26
28 {
30 cngKey.ExportPolicy = CngExportPolicies.AllowExport | CngExportPolicies.AllowPlaintextExport;
31 Pkcs8Response result = default(Pkcs8Response);
32 result.Key = cngKey;
33 return result;
34 }
35
37 {
38 CngKey cngKey = CngKey.ImportEncryptedPkcs8(keyBlob, password);
39 cngKey.ExportPolicy = CngExportPolicies.AllowExport | CngExportPolicies.AllowPlaintextExport;
40 Pkcs8Response result = default(Pkcs8Response);
41 result.Key = cngKey;
42 return result;
43 }
44
45 internal static bool IsPlatformScheme(PbeParameters pbeParameters)
46 {
47 if (pbeParameters.EncryptionAlgorithm == s_platformParameters.EncryptionAlgorithm)
48 {
49 return pbeParameters.HashAlgorithm == s_platformParameters.HashAlgorithm;
50 }
51 return false;
52 }
53
54 internal static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan<byte> passwordBytes, PbeParameters pbeParameters)
55 {
56 if (pbeParameters == null)
57 {
58 throw new ArgumentNullException("pbeParameters");
59 }
61 if (passwordBytes.Length == 0)
62 {
63 return key.ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>.Empty, pbeParameters);
64 }
65 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, passwordBytes, pbeParameters);
66 return asnWriter.Encode();
67 }
68
69 internal static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan<byte> passwordBytes, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
70 {
71 if (passwordBytes.Length == 0)
72 {
73 return key.TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>.Empty, pbeParameters, destination, out bytesWritten);
74 }
75 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, passwordBytes, pbeParameters);
76 return asnWriter.TryEncode(destination, out bytesWritten);
77 }
78
80 {
81 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, password, pbeParameters);
82 return asnWriter.Encode();
83 }
84
85 internal static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan<char> password, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
86 {
87 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, password, pbeParameters);
88 return asnWriter.TryEncode(destination, out bytesWritten);
89 }
90
92 {
93 int bytesConsumed;
94 try
95 {
96 AsnDecoder.ReadEncodedValue(source, AsnEncodingRules.BER, out var _, out var _, out bytesConsumed);
97 }
98 catch (AsnContentException inner)
99 {
101 }
102 bytesRead = bytesConsumed;
103 ReadOnlySpan<byte> readOnlySpan = source.Slice(0, bytesConsumed);
104 try
105 {
106 return ImportPkcs8(readOnlySpan);
107 }
109 {
111 if (asnWriter == null)
112 {
113 throw;
114 }
115 return ImportPkcs8(asnWriter);
116 }
117 catch (AsnContentException inner2)
118 {
120 }
121 }
122
123 private static Pkcs8Response ImportPkcs8(AsnWriter pkcs8Writer)
124 {
126 if (!pkcs8Writer.TryEncode(array, out var bytesWritten))
127 {
128 throw new CryptographicException();
129 }
130 Pkcs8Response result = ImportPkcs8(array.AsSpan(0, bytesWritten));
132 return result;
133 }
134
135 internal unsafe static Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, ReadOnlySpan<byte> source, out int bytesRead)
136 {
137 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
138 {
139 using MemoryManager<byte> memoryManager = new System.Buffers.PointerMemoryManager<byte>(pointer, source.Length);
140 try
141 {
142 ArraySegment<byte> arraySegment = System.Security.Cryptography.KeyFormatHelper.DecryptPkcs8(passwordBytes, memoryManager.Memory, out bytesRead);
143 Span<byte> span = arraySegment;
144 try
145 {
146 return ImportPkcs8(span);
147 }
148 catch (CryptographicException inner)
149 {
151 if (asnWriter == null)
152 {
154 }
155 try
156 {
157 return ImportPkcs8(asnWriter);
158 }
160 {
162 }
163 }
164 finally
165 {
167 }
168 }
169 catch (AsnContentException inner2)
170 {
172 }
173 }
174 }
175
177 {
178 try
179 {
180 AsnDecoder.ReadEncodedValue(source, AsnEncodingRules.BER, out var _, out var _, out var bytesConsumed);
181 source = source.Slice(0, bytesConsumed);
182 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
183 {
184 using MemoryManager<byte> memoryManager = new System.Buffers.PointerMemoryManager<byte>(pointer, source.Length);
185 try
186 {
187 bytesRead = bytesConsumed;
188 return ImportPkcs8(source, password);
189 }
191 {
192 }
193 int bytesRead2;
194 ArraySegment<byte> arraySegment = System.Security.Cryptography.KeyFormatHelper.DecryptPkcs8(password, memoryManager.Memory.Slice(0, bytesConsumed), out bytesRead2);
195 Span<byte> span = arraySegment;
196 try
197 {
198 if (bytesRead2 != bytesConsumed)
199 {
201 }
202 bytesRead = bytesConsumed;
203 return ImportPkcs8(span);
204 }
205 catch (CryptographicException inner)
206 {
208 if (asnWriter == null)
209 {
211 }
212 try
213 {
214 bytesRead = bytesConsumed;
215 return ImportPkcs8(asnWriter);
216 }
218 {
220 }
221 }
222 finally
223 {
225 }
226 }
227 }
228 catch (AsnContentException inner2)
229 {
231 }
232 }
233
235 {
237 int bytesWritten = 0;
238 Span<char> span = stackalloc char[22];
239 try
240 {
242 while (!key.TryExportEncryptedPkcs8PrivateKey(span, s_platformParameters, array, out bytesWritten))
243 {
244 int num = array.Length;
245 byte[] array2 = array;
246 array = System.Security.Cryptography.CryptoPool.Rent(checked(num * 2));
247 System.Security.Cryptography.CryptoPool.Return(array2, bytesWritten);
248 }
249 return System.Security.Cryptography.KeyFormatHelper.ReencryptPkcs8(span, array.AsMemory(0, bytesWritten), passwordBytes, pbeParameters);
250 }
251 finally
252 {
253 span.Clear();
255 }
256 }
257
259 {
261 int bytesWritten = 0;
262 try
263 {
264 while (!key.TryExportEncryptedPkcs8PrivateKey(password, s_platformParameters, array, out bytesWritten))
265 {
266 int num = array.Length;
267 byte[] array2 = array;
268 array = System.Security.Cryptography.CryptoPool.Rent(checked(num * 2));
269 System.Security.Cryptography.CryptoPool.Return(array2, bytesWritten);
270 }
271 return System.Security.Cryptography.KeyFormatHelper.ReencryptPkcs8(password, array.AsMemory(0, bytesWritten), password, pbeParameters);
272 }
273 finally
274 {
276 }
277 }
278
280 {
281 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
282 {
283 using MemoryManager<byte> memoryManager = new System.Buffers.PointerMemoryManager<byte>(pointer, source.Length);
285 System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn algId = privateKeyInfoAsn.PrivateKeyAlgorithm;
286 if (algId.Algorithm != "1.2.840.10045.2.1")
287 {
288 return null;
289 }
292 fixed (byte* ptr = ret.D)
293 {
294 try
295 {
296 if (!ret.Curve.IsExplicit || ret.Q.X != null || ret.Q.Y != null)
297 {
298 return null;
299 }
300 byte[] array = new byte[ret.D.Length];
301 ret.Q.Y = array;
302 ret.Q.X = array;
303 return System.Security.Cryptography.EccKeyFormatHelper.WritePkcs8PrivateKey(ret, privateKeyInfoAsn.Attributes);
304 }
305 finally
306 {
307 Array.Clear(ret.D);
308 }
309 }
310 }
311 }
312
314 {
315 Span<byte> data = stackalloc byte[destination.Length];
317 for (int i = 0; i < data.Length; i++)
318 {
319 destination[i] = (char)(33 + (data[i] & 0x3F));
320 }
321 }
322}
static unsafe void Clear(Array array)
Definition Array.cs:755
static Asn1Tag ReadEncodedValue(ReadOnlySpan< byte > source, AsnEncodingRules ruleSet, out int contentOffset, out int contentLength, out int bytesConsumed)
Definition AsnDecoder.cs:57
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 Cryptography_Pkcs8_EncryptedReadFailed
Definition SR.cs:126
Definition SR.cs:7
static CngKey ImportEncryptedPkcs8(ReadOnlySpan< byte > keyBlob, ReadOnlySpan< char > password)
Definition CngKey.cs:545
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
Definition CngKey.cs:525
CngAlgorithmGroup? AlgorithmGroup
Definition CngKey.cs:27
static Pkcs8Response ImportPkcs8(AsnWriter pkcs8Writer)
Definition CngPkcs8.cs:123
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition CngPkcs8.cs:85
static bool IsPlatformScheme(PbeParameters pbeParameters)
Definition CngPkcs8.cs:45
static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< char > password, PbeParameters pbeParameters)
Definition CngPkcs8.cs:79
static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:135
static Pkcs8Response ImportPkcs8(ReadOnlySpan< byte > keyBlob)
Definition CngPkcs8.cs:27
static AsnWriter RewriteEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< char > password, PbeParameters pbeParameters)
Definition CngPkcs8.cs:258
static Pkcs8Response ImportPkcs8(ReadOnlySpan< byte > keyBlob, ReadOnlySpan< char > password)
Definition CngPkcs8.cs:36
static readonly PbeParameters s_platformParameters
Definition CngPkcs8.cs:26
static unsafe AsnWriter RewritePkcs8ECPrivateKeyWithZeroPublicKey(ReadOnlySpan< byte > source)
Definition CngPkcs8.cs:278
static AsnWriter RewriteEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
Definition CngPkcs8.cs:233
static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
Definition CngPkcs8.cs:54
static Pkcs8Response ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:91
static void FillRandomAsciiString(Span< char > destination)
Definition CngPkcs8.cs:312
static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:176
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition CngPkcs8.cs:69
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
static unsafe ECParameters FromECPrivateKey(ReadOnlySpan< byte > key, out int bytesRead)
static AsnWriter WritePkcs8PrivateKey(ECParameters ecParameters, AttributeAsn[] attributes=null)
static ArraySegment< byte > DecryptPkcs8(ReadOnlySpan< char > inputPassword, ReadOnlyMemory< byte > source, out int bytesRead)
static AsnWriter ReencryptPkcs8(ReadOnlySpan< char > inputPassword, ReadOnlyMemory< byte > current, ReadOnlySpan< char > newPassword, PbeParameters pbeParameters)
static void ValidatePbeParameters(PbeParameters pbeParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes)
ReadOnlySpan< T > Slice(int start)
static ECPrivateKey Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
static PrivateKeyInfoAsn Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
unsafe void Clear()
Definition Span.cs:198
int Length
Definition Span.cs:70