Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CngPkcs8.cs
Go to the documentation of this file.
6
8
9internal static class CngPkcs8
10{
11 internal struct Pkcs8Response
12 {
14
15 internal string GetAlgorithmGroup()
16 {
17 return CngKeyLite.GetPropertyAsString(KeyHandle, "Algorithm Group", CngPropertyOptions.None);
18 }
19
20 internal void FreeKey()
21 {
23 }
24 }
25
26 private static readonly PbeParameters s_platformParameters = new PbeParameters(PbeEncryptionAlgorithm.TripleDes3KeyPkcs12, HashAlgorithmName.SHA1, 1);
27
29 {
30 SafeNCryptKeyHandle keyHandle = CngKeyLite.ImportKeyBlob("PKCS8_PRIVATEKEY", keyBlob);
31 Pkcs8Response result = default(Pkcs8Response);
32 result.KeyHandle = keyHandle;
33 return result;
34 }
35
37 {
38 SafeNCryptKeyHandle keyHandle = CngKeyLite.ImportKeyBlob("PKCS8_PRIVATEKEY", keyBlob, encrypted: true, password);
39 Pkcs8Response result = default(Pkcs8Response);
40 result.KeyHandle = keyHandle;
41 return result;
42 }
43
44 internal static bool IsPlatformScheme(PbeParameters pbeParameters)
45 {
46 if (pbeParameters.EncryptionAlgorithm == s_platformParameters.EncryptionAlgorithm)
47 {
48 return pbeParameters.HashAlgorithm == s_platformParameters.HashAlgorithm;
49 }
50 return false;
51 }
52
53 internal static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan<byte> passwordBytes, PbeParameters pbeParameters)
54 {
55 if (pbeParameters == null)
56 {
57 throw new ArgumentNullException("pbeParameters");
58 }
60 if (passwordBytes.Length == 0)
61 {
62 return key.ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>.Empty, pbeParameters);
63 }
64 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, passwordBytes, pbeParameters);
65 return asnWriter.Encode();
66 }
67
68 internal static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan<byte> passwordBytes, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
69 {
70 if (passwordBytes.Length == 0)
71 {
72 return key.TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char>.Empty, pbeParameters, destination, out bytesWritten);
73 }
74 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, passwordBytes, pbeParameters);
75 return asnWriter.TryEncode(destination, out bytesWritten);
76 }
77
79 {
80 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, password, pbeParameters);
81 return asnWriter.Encode();
82 }
83
84 internal static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan<char> password, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
85 {
86 AsnWriter asnWriter = RewriteEncryptedPkcs8PrivateKey(key, password, pbeParameters);
87 return asnWriter.TryEncode(destination, out bytesWritten);
88 }
89
91 {
92 int bytesConsumed;
93 try
94 {
95 AsnDecoder.ReadEncodedValue(source, AsnEncodingRules.BER, out var _, out var _, out bytesConsumed);
96 }
97 catch (AsnContentException inner)
98 {
100 }
101 bytesRead = bytesConsumed;
102 ReadOnlySpan<byte> readOnlySpan = source.Slice(0, bytesConsumed);
103 try
104 {
105 return ImportPkcs8(readOnlySpan);
106 }
108 {
110 if (asnWriter == null)
111 {
112 throw;
113 }
114 return ImportPkcs8(asnWriter);
115 }
116 catch (AsnContentException inner2)
117 {
119 }
120 }
121
122 private static Pkcs8Response ImportPkcs8(AsnWriter pkcs8Writer)
123 {
125 if (!pkcs8Writer.TryEncode(array, out var bytesWritten))
126 {
127 throw new CryptographicException();
128 }
129 Pkcs8Response result = ImportPkcs8(array.AsSpan(0, bytesWritten));
131 return result;
132 }
133
134 internal unsafe static Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, ReadOnlySpan<byte> source, out int bytesRead)
135 {
136 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
137 {
138 using MemoryManager<byte> memoryManager = new PointerMemoryManager<byte>(pointer, source.Length);
139 try
140 {
141 ArraySegment<byte> arraySegment = KeyFormatHelper.DecryptPkcs8(passwordBytes, memoryManager.Memory, out bytesRead);
142 Span<byte> span = arraySegment;
143 try
144 {
145 return ImportPkcs8(span);
146 }
147 catch (CryptographicException inner)
148 {
150 if (asnWriter == null)
151 {
153 }
154 try
155 {
156 return ImportPkcs8(asnWriter);
157 }
159 {
161 }
162 }
163 finally
164 {
166 }
167 }
168 catch (AsnContentException inner2)
169 {
171 }
172 }
173 }
174
176 {
177 try
178 {
179 AsnDecoder.ReadEncodedValue(source, AsnEncodingRules.BER, out var _, out var _, out var bytesConsumed);
180 source = source.Slice(0, bytesConsumed);
181 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
182 {
183 using MemoryManager<byte> memoryManager = new PointerMemoryManager<byte>(pointer, source.Length);
184 try
185 {
186 bytesRead = bytesConsumed;
187 return ImportPkcs8(source, password);
188 }
190 {
191 }
192 int bytesRead2;
193 ArraySegment<byte> arraySegment = KeyFormatHelper.DecryptPkcs8(password, memoryManager.Memory.Slice(0, bytesConsumed), out bytesRead2);
194 Span<byte> span = arraySegment;
195 try
196 {
197 if (bytesRead2 != bytesConsumed)
198 {
200 }
201 bytesRead = bytesConsumed;
202 return ImportPkcs8(span);
203 }
204 catch (CryptographicException inner)
205 {
207 if (asnWriter == null)
208 {
210 }
211 try
212 {
213 bytesRead = bytesConsumed;
214 return ImportPkcs8(asnWriter);
215 }
217 {
219 }
220 }
221 finally
222 {
224 }
225 }
226 }
227 catch (AsnContentException inner2)
228 {
230 }
231 }
232
234 {
236 int bytesWritten = 0;
237 Span<char> span = stackalloc char[22];
238 try
239 {
241 while (!key.TryExportEncryptedPkcs8PrivateKey(span, s_platformParameters, array, out bytesWritten))
242 {
243 int num = array.Length;
244 byte[] array2 = array;
245 array = System.Security.Cryptography.CryptoPool.Rent(checked(num * 2));
246 System.Security.Cryptography.CryptoPool.Return(array2, bytesWritten);
247 }
248 return KeyFormatHelper.ReencryptPkcs8(span, array.AsMemory(0, bytesWritten), passwordBytes, pbeParameters);
249 }
250 finally
251 {
252 span.Clear();
254 }
255 }
256
258 {
260 int bytesWritten = 0;
261 try
262 {
263 while (!key.TryExportEncryptedPkcs8PrivateKey(password, s_platformParameters, array, out bytesWritten))
264 {
265 int num = array.Length;
266 byte[] array2 = array;
267 array = System.Security.Cryptography.CryptoPool.Rent(checked(num * 2));
268 System.Security.Cryptography.CryptoPool.Return(array2, bytesWritten);
269 }
270 return KeyFormatHelper.ReencryptPkcs8(password, array.AsMemory(0, bytesWritten), password, pbeParameters);
271 }
272 finally
273 {
275 }
276 }
277
279 {
280 fixed (byte* pointer = &MemoryMarshal.GetReference(source))
281 {
282 using MemoryManager<byte> memoryManager = new PointerMemoryManager<byte>(pointer, source.Length);
283 PrivateKeyInfoAsn privateKeyInfoAsn = PrivateKeyInfoAsn.Decode(memoryManager.Memory, AsnEncodingRules.BER);
284 AlgorithmIdentifierAsn algId = privateKeyInfoAsn.PrivateKeyAlgorithm;
285 if (algId.Algorithm != "1.2.840.10045.2.1")
286 {
287 return null;
288 }
290 EccKeyFormatHelper.FromECPrivateKey(key, in algId, out var ret);
291 fixed (byte* ptr = ret.D)
292 {
293 try
294 {
295 if (!ret.Curve.IsExplicit || ret.Q.X != null || ret.Q.Y != null)
296 {
297 return null;
298 }
299 byte[] array = new byte[ret.D.Length];
300 ret.Q.Y = array;
301 ret.Q.X = array;
302 return EccKeyFormatHelper.WritePkcs8PrivateKey(ret, privateKeyInfoAsn.Attributes);
303 }
304 finally
305 {
306 Array.Clear(ret.D);
307 }
308 }
309 }
310 }
311
313 {
314 Span<byte> data = stackalloc byte[destination.Length];
316 for (int i = 0; i < data.Length; i++)
317 {
318 destination[i] = (char)(33 + (data[i] & 0x3F));
319 }
320 }
321}
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 unsafe SafeNCryptKeyHandle ImportKeyBlob(string blobType, ReadOnlySpan< byte > keyBlob, bool encrypted=false, ReadOnlySpan< char > password=default(ReadOnlySpan< char >))
Definition CngKeyLite.cs:14
static unsafe string GetPropertyAsString(SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options)
static Pkcs8Response ImportPkcs8(AsnWriter pkcs8Writer)
Definition CngPkcs8.cs:122
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition CngPkcs8.cs:84
static bool IsPlatformScheme(PbeParameters pbeParameters)
Definition CngPkcs8.cs:44
static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< char > password, PbeParameters pbeParameters)
Definition CngPkcs8.cs:78
static unsafe Pkcs8Response ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:134
static Pkcs8Response ImportPkcs8(ReadOnlySpan< byte > keyBlob)
Definition CngPkcs8.cs:28
static AsnWriter RewriteEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< char > password, PbeParameters pbeParameters)
Definition CngPkcs8.cs:257
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:53
static Pkcs8Response ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
Definition CngPkcs8.cs:90
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:175
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
Definition CngPkcs8.cs:68
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