Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Interop.cs
Go to the documentation of this file.
1using System;
9
10internal static class Interop
11{
12 public static class cryptoapi
13 {
14 [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "CryptAcquireContextW", SetLastError = true)]
15 [return: MarshalAs(UnmanagedType.Bool)]
16 public unsafe static extern bool CryptAcquireContext(out IntPtr psafeProvHandle, char* pszContainer, char* pszProvider, int dwProvType, CryptAcquireContextFlags dwFlags);
17 }
18
19 public static class crypt32
20 {
21 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
22 public unsafe static extern bool CryptQueryObject(CertQueryObjectType dwObjectType, void* pvObject, ExpectedContentTypeFlags dwExpectedContentTypeFlags, ExpectedFormatTypeFlags dwExpectedFormatTypeFlags, int dwFlags, out CertEncodingType pdwMsgAndCertEncodingType, out ContentType pdwContentType, out FormatType pdwFormatType, out SafeCertStoreHandle phCertStore, out SafeCryptMsgHandle phMsg, out SafeCertContextHandle ppvContext);
23
24 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
25 public unsafe static extern bool CryptQueryObject(CertQueryObjectType dwObjectType, void* pvObject, ExpectedContentTypeFlags dwExpectedContentTypeFlags, ExpectedFormatTypeFlags dwExpectedFormatTypeFlags, int dwFlags, IntPtr pdwMsgAndCertEncodingType, out ContentType pdwContentType, IntPtr pdwFormatType, IntPtr phCertStore, IntPtr phMsg, IntPtr ppvContext);
26
27 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
28 public unsafe static extern bool CryptQueryObject(CertQueryObjectType dwObjectType, void* pvObject, ExpectedContentTypeFlags dwExpectedContentTypeFlags, ExpectedFormatTypeFlags dwExpectedFormatTypeFlags, int dwFlags, IntPtr pdwMsgAndCertEncodingType, out ContentType pdwContentType, IntPtr pdwFormatType, out SafeCertStoreHandle phCertStore, IntPtr phMsg, IntPtr ppvContext);
29
30 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
31 public static extern bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, [Out] byte[] pvData, [In][Out] ref int pcbData);
32
33 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
34 public static extern bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, out CRYPTOAPI_BLOB pvData, [In][Out] ref int pcbData);
35
36 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
37 public static extern bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, out IntPtr pvData, [In][Out] ref int pcbData);
38
39 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CertGetCertificateContextProperty", SetLastError = true)]
40 public unsafe static extern bool CertGetCertificateContextPropertyString(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, byte* pvData, ref int pcbData);
41
42 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
43 public unsafe static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, CertSetPropertyFlags dwFlags, [In] CRYPTOAPI_BLOB* pvData);
44
45 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
46 public unsafe static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, CertSetPropertyFlags dwFlags, [In] CRYPT_KEY_PROV_INFO* pvData);
47
48 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
49 public static extern bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, CertSetPropertyFlags dwFlags, [In] SafeNCryptKeyHandle keyHandle);
50
51 public unsafe static string CertGetNameString(SafeCertContextHandle certContext, CertNameType certNameType, CertNameFlags certNameFlags, CertNameStringType strType)
52 {
53 int num = CertGetNameString(certContext, certNameType, certNameFlags, in strType, null, 0);
54 if (num == 0)
55 {
56 throw Marshal.GetLastWin32Error().ToCryptographicException();
57 }
58 Span<char> span = ((num > 256) ? ((Span<char>)new char[num]) : stackalloc char[num]);
59 Span<char> span2 = span;
60 fixed (char* pszNameString = &MemoryMarshal.GetReference(span2))
61 {
62 if (CertGetNameString(certContext, certNameType, certNameFlags, in strType, pszNameString, num) == 0)
63 {
64 throw Marshal.GetLastWin32Error().ToCryptographicException();
65 }
66 return new string(span2.Slice(0, num - 1));
67 }
68 }
69
70 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CertGetNameStringW", SetLastError = true)]
71 private unsafe static extern int CertGetNameString(SafeCertContextHandle pCertContext, CertNameType dwType, CertNameFlags dwFlags, in CertNameStringType pvTypePara, char* pszNameString, int cchNameString);
72
73 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
75
76 [DllImport("crypt32.dll", SetLastError = true)]
77 public static extern SafeX509ChainHandle CertDuplicateCertificateChain(IntPtr pChainContext);
78
79 [DllImport("crypt32.dll", SetLastError = true)]
80 internal static extern SafeCertStoreHandle CertDuplicateStore(IntPtr hCertStore);
81
82 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CertDuplicateCertificateContext", SetLastError = true)]
84
85 public static SafeCertStoreHandle CertOpenStore(CertStoreProvider lpszStoreProvider, CertEncodingType dwMsgAndCertEncodingType, IntPtr hCryptProv, CertStoreFlags dwFlags, string pvPara)
86 {
87 return CertOpenStore((IntPtr)(int)lpszStoreProvider, dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
88 }
89
90 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
91 private static extern SafeCertStoreHandle CertOpenStore(IntPtr lpszStoreProvider, CertEncodingType dwMsgAndCertEncodingType, IntPtr hCryptProv, CertStoreFlags dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pvPara);
92
93 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
94 public static extern bool CertAddCertificateContextToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, CertStoreAddDisposition dwAddDisposition, IntPtr ppStoreContext);
95
96 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
97 public static extern bool CertAddCertificateLinkToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, CertStoreAddDisposition dwAddDisposition, IntPtr ppStoreContext);
98
99 public unsafe static bool CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, [NotNull] ref SafeCertContextHandle pCertContext)
100 {
101 CERT_CONTEXT* pPrevCertContext;
102 if (pCertContext == null)
103 {
104 pCertContext = new SafeCertContextHandle();
105 pPrevCertContext = null;
106 }
107 else
108 {
109 pPrevCertContext = pCertContext.Disconnect();
110 }
111 pCertContext.SetHandle((IntPtr)CertEnumCertificatesInStore(hCertStore, pPrevCertContext));
112 if (!pCertContext.IsInvalid)
113 {
114 return true;
115 }
116 pCertContext.Dispose();
117 return false;
118 }
119
120 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
121 private unsafe static extern CERT_CONTEXT* CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, CERT_CONTEXT* pPrevCertContext);
122
123 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
124 public static extern SafeCertStoreHandle PFXImportCertStore([In] ref CRYPTOAPI_BLOB pPFX, SafePasswordHandle password, PfxCertStoreFlags dwFlags);
125
126 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
127 public unsafe static extern bool CryptMsgGetParam(SafeCryptMsgHandle hCryptMsg, CryptMessageParameterType dwParamType, int dwIndex, byte* pvData, [In][Out] ref int pcbData);
128
129 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
130 public static extern bool CryptMsgGetParam(SafeCryptMsgHandle hCryptMsg, CryptMessageParameterType dwParamType, int dwIndex, out int pvData, [In][Out] ref int pcbData);
131
132 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
133 public static extern bool CertSerializeCertificateStoreElement(SafeCertContextHandle pCertContext, int dwFlags, [Out] byte[] pbElement, [In][Out] ref int pcbElement);
134
135 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
136 public static extern bool PFXExportCertStore(SafeCertStoreHandle hStore, [In][Out] ref CRYPTOAPI_BLOB pPFX, SafePasswordHandle szPassword, PFXExportFlags dwFlags);
137
138 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CertStrToNameW", SetLastError = true)]
139 public static extern bool CertStrToName(CertEncodingType dwCertEncodingType, string pszX500, CertNameStrTypeAndFlags dwStrType, IntPtr pvReserved, [Out] byte[] pbEncoded, [In][Out] ref int pcbEncoded, IntPtr ppszError);
140
141 public static bool CryptDecodeObject(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, byte[] pvStructInfo, ref int pcbStructInfo)
142 {
143 return CryptDecodeObject(dwCertEncodingType, (IntPtr)(int)lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, ref pcbStructInfo);
144 }
145
146 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
147 private static extern bool CryptDecodeObject(CertEncodingType dwCertEncodingType, IntPtr lpszStructType, [In] byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, [Out] byte[] pvStructInfo, [In][Out] ref int pcbStructInfo);
148
149 public unsafe static bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, void* pvStructInfo, ref int pcbStructInfo)
150 {
151 return CryptDecodeObjectPointer(dwCertEncodingType, (IntPtr)(int)lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, ref pcbStructInfo);
152 }
153
154 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CryptDecodeObject", SetLastError = true)]
155 private unsafe static extern bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, IntPtr lpszStructType, [In] byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, [Out] void* pvStructInfo, [In][Out] ref int pcbStructInfo);
156
157 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CryptDecodeObject", SetLastError = true)]
158 public unsafe static extern bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, [MarshalAs(UnmanagedType.LPStr)] string lpszStructType, [In] byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, [Out] void* pvStructInfo, [In][Out] ref int pcbStructInfo);
159
160 public unsafe static bool CryptEncodeObject(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, void* pvStructInfo, byte[] pbEncoded, ref int pcbEncoded)
161 {
162 return CryptEncodeObject(dwCertEncodingType, (IntPtr)(int)lpszStructType, pvStructInfo, pbEncoded, ref pcbEncoded);
163 }
164
165 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
166 private unsafe static extern bool CryptEncodeObject(CertEncodingType dwCertEncodingType, IntPtr lpszStructType, void* pvStructInfo, [Out] byte[] pbEncoded, [In][Out] ref int pcbEncoded);
167
168 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
169 public unsafe static extern bool CryptEncodeObject(CertEncodingType dwCertEncodingType, [MarshalAs(UnmanagedType.LPStr)] string lpszStructType, void* pvStructInfo, [Out] byte[] pbEncoded, [In][Out] ref int pcbEncoded);
170
171 public unsafe static byte[] EncodeObject(CryptDecodeObjectStructType lpszStructType, void* decoded)
172 {
173 int pcbEncoded = 0;
174 if (!CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, null, ref pcbEncoded))
175 {
176 throw Marshal.GetLastWin32Error().ToCryptographicException();
177 }
178 byte[] array = new byte[pcbEncoded];
179 if (!CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, array, ref pcbEncoded))
180 {
181 throw Marshal.GetLastWin32Error().ToCryptographicException();
182 }
183 return array;
184 }
185
186 public unsafe static byte[] EncodeObject(string lpszStructType, void* decoded)
187 {
188 int pcbEncoded = 0;
189 if (!CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, null, ref pcbEncoded))
190 {
191 throw Marshal.GetLastWin32Error().ToCryptographicException();
192 }
193 byte[] array = new byte[pcbEncoded];
194 if (!CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, array, ref pcbEncoded))
195 {
196 throw Marshal.GetLastWin32Error().ToCryptographicException();
197 }
198 return array;
199 }
200
202 {
203 if (!CertCreateCertificateChainEngine(ref config, out var hChainEngineHandle))
204 {
205 int lastWin32Error = Marshal.GetLastWin32Error();
206 throw lastWin32Error.ToCryptographicException();
207 }
208 return hChainEngineHandle;
209 }
210
211 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
212 private static extern bool CertCreateCertificateChainEngine(ref CERT_CHAIN_ENGINE_CONFIG pConfig, out SafeChainEngineHandle hChainEngineHandle);
213
214 [DllImport("crypt32.dll")]
215 public static extern void CertFreeCertificateChainEngine(IntPtr hChainEngine);
216
217 [DllImport("crypt32.dll", SetLastError = true)]
218 public unsafe static extern bool CertGetCertificateChain(IntPtr hChainEngine, SafeCertContextHandle pCertContext, FILETIME* pTime, SafeCertStoreHandle hStore, [In] ref CERT_CHAIN_PARA pChainPara, CertChainFlags dwFlags, IntPtr pvReserved, out SafeX509ChainHandle ppChainContext);
219
220 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
221 public static extern bool CryptHashPublicKeyInfo(IntPtr hCryptProv, int algId, int dwFlags, CertEncodingType dwCertEncodingType, [In] ref CERT_PUBLIC_KEY_INFO pInfo, [Out] byte[] pbComputedHash, [In][Out] ref int pcbComputedHash);
222
223 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
224 public static extern bool CertSaveStore(SafeCertStoreHandle hCertStore, CertEncodingType dwMsgAndCertEncodingType, CertStoreSaveAs dwSaveAs, CertStoreSaveTo dwSaveTo, ref CRYPTOAPI_BLOB pvSaveToPara, int dwFlags);
225
226 public unsafe static bool CertFindCertificateInStore(SafeCertStoreHandle hCertStore, CertFindType dwFindType, void* pvFindPara, [NotNull] ref SafeCertContextHandle pCertContext)
227 {
228 CERT_CONTEXT* pPrevCertContext = ((pCertContext == null) ? null : pCertContext.Disconnect());
229 pCertContext = CertFindCertificateInStore(hCertStore, CertEncodingType.All, CertFindFlags.None, dwFindType, pvFindPara, pPrevCertContext);
230 return !pCertContext.IsInvalid;
231 }
232
233 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
234 private unsafe static extern SafeCertContextHandle CertFindCertificateInStore(SafeCertStoreHandle hCertStore, CertEncodingType dwCertEncodingType, CertFindFlags dwFindFlags, CertFindType dwFindType, void* pvFindPara, CERT_CONTEXT* pPrevCertContext);
235
236 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
237 public unsafe static extern int CertVerifyTimeValidity([In] ref FILETIME pTimeToVerify, [In] CERT_INFO* pCertInfo);
238
239 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
240 public unsafe static extern CERT_EXTENSION* CertFindExtension([MarshalAs(UnmanagedType.LPStr)] string pszObjId, int cExtensions, CERT_EXTENSION* rgExtensions);
241
242 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
243 public unsafe static extern bool CertGetIntendedKeyUsage(CertEncodingType dwCertEncodingType, CERT_INFO* pCertInfo, out X509KeyUsageFlags pbKeyUsage, int cbKeyUsage);
244
245 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
246 public unsafe static extern bool CertGetValidUsages(int cCerts, [In] ref SafeCertContextHandle rghCerts, out int cNumOIDs, [Out] void* rghOIDs, [In][Out] ref int pcbOIDs);
247
248 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
249 public static extern bool CertControlStore(SafeCertStoreHandle hCertStore, CertControlStoreFlags dwFlags, CertControlStoreType dwControlType, IntPtr pvCtrlPara);
250
251 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
252 public unsafe static extern bool CertDeleteCertificateFromStore(CERT_CONTEXT* pCertContext);
253
254 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
255 public static extern void CertFreeCertificateChain(IntPtr pChainContext);
256
257 public static bool CertVerifyCertificateChainPolicy(ChainPolicy pszPolicyOID, SafeX509ChainHandle pChainContext, ref CERT_CHAIN_POLICY_PARA pPolicyPara, ref CERT_CHAIN_POLICY_STATUS pPolicyStatus)
258 {
259 return CertVerifyCertificateChainPolicy((IntPtr)(int)pszPolicyOID, pChainContext, ref pPolicyPara, ref pPolicyStatus);
260 }
261
262 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
263 private static extern bool CertVerifyCertificateChainPolicy(IntPtr pszPolicyOID, SafeX509ChainHandle pChainContext, [In] ref CERT_CHAIN_POLICY_PARA pPolicyPara, [In][Out] ref CERT_CHAIN_POLICY_STATUS pPolicyStatus);
264
265 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
266 public unsafe static extern bool CryptImportPublicKeyInfoEx2(CertEncodingType dwCertEncodingType, CERT_PUBLIC_KEY_INFO* pInfo, CryptImportPublicKeyInfoFlags dwFlags, void* pvAuxInfo, out Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle phKey);
267
268 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
269 public static extern bool CryptAcquireCertificatePrivateKey(SafeCertContextHandle pCert, CryptAcquireFlags dwFlags, IntPtr pvParameters, out SafeNCryptKeyHandle phCryptProvOrNCryptKey, out int pdwKeySpec, out bool pfCallerFreeProvOrNCryptKey);
270 }
271
272 internal static class Crypt32
273 {
274 internal struct CRYPT_OID_INFO
275 {
276 public int cbSize;
277
278 public IntPtr pszOID;
279
280 public IntPtr pwszName;
281
282 public OidGroup dwGroupId;
283
284 public int AlgId;
285
286 public int cbData;
287
288 public IntPtr pbData;
289
290 public string OID => Marshal.PtrToStringAnsi(pszOID);
291 }
292
302
303 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
304 internal static extern bool CertCloseStore(IntPtr hCertStore, uint dwFlags);
305
306 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, EntryPoint = "CertNameToStrW", SetLastError = true)]
307 internal unsafe static extern int CertNameToStr(int dwCertEncodingType, void* pName, int dwStrType, char* psz, int csz);
308
309 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
310 internal static extern bool CryptMsgClose(IntPtr hCryptMsg);
311
312 [DllImport("crypt32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
313 internal static extern bool CertFreeCertificateContext(IntPtr pCertContext);
314
315 [DllImport("crypt32.dll", BestFitMapping = false, SetLastError = true)]
316 internal unsafe static extern bool CryptFormatObject([In] int dwCertEncodingType, [In] int dwFormatType, [In] int dwFormatStrType, [In] IntPtr pFormatStruct, [In] byte* lpszStructType, [In] byte[] pbEncoded, [In] int cbEncoded, [Out] void* pbFormat, [In][Out] ref int pcbFormat);
317
318 internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
319 {
320 IntPtr intPtr = IntPtr.Zero;
321 try
322 {
323 intPtr = keyType switch
324 {
325 CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY => Marshal.StringToCoTaskMemAnsi(key),
326 CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY => Marshal.StringToCoTaskMemUni(key),
327 _ => throw new NotSupportedException(),
328 };
330 {
331 OidGroup group2 = group | (OidGroup)(-2147483648);
332 IntPtr intPtr2 = CryptFindOIDInfo(keyType, intPtr, group2);
333 if (intPtr2 != IntPtr.Zero)
334 {
335 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr2);
336 }
337 }
338 IntPtr intPtr3 = CryptFindOIDInfo(keyType, intPtr, group);
339 if (intPtr3 != IntPtr.Zero)
340 {
341 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr3);
342 }
343 if (fallBackToAllGroups && group != 0)
344 {
345 IntPtr intPtr4 = CryptFindOIDInfo(keyType, intPtr, OidGroup.All);
346 if (intPtr4 != IntPtr.Zero)
347 {
348 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr4);
349 }
350 }
351 CRYPT_OID_INFO result = default(CRYPT_OID_INFO);
352 result.AlgId = -1;
353 return result;
354 }
355 finally
356 {
357 if (intPtr != IntPtr.Zero)
358 {
359 Marshal.FreeCoTaskMem(intPtr);
360 }
361 }
362 }
363
365 {
366 if (group != OidGroup.HashAlgorithm && group != OidGroup.EncryptionAlgorithm && group != OidGroup.PublicKeyAlgorithm && group != OidGroup.SignatureAlgorithm && group != OidGroup.Attribute && group != OidGroup.ExtensionOrAttribute)
367 {
368 return group == OidGroup.KeyDerivationFunction;
369 }
370 return true;
371 }
372
373 [DllImport("crypt32.dll", CharSet = CharSet.Unicode)]
374 private static extern IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group);
375 }
376
377 internal static class Kernel32
378 {
379 [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Unicode, EntryPoint = "FormatMessageW", ExactSpelling = true, SetLastError = true)]
380 private unsafe static extern int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void* lpBuffer, int nSize, IntPtr arguments);
381
382 internal static string GetMessage(int errorCode)
383 {
384 return GetMessage(errorCode, IntPtr.Zero);
385 }
386
387 internal unsafe static string GetMessage(int errorCode, IntPtr moduleHandle)
388 {
389 int num = 12800;
390 if (moduleHandle != IntPtr.Zero)
391 {
392 num |= 0x800;
393 }
394 Span<char> span = stackalloc char[256];
395 fixed (char* lpBuffer = span)
396 {
397 int num2 = FormatMessage(num, moduleHandle, (uint)errorCode, 0, lpBuffer, span.Length, IntPtr.Zero);
398 if (num2 > 0)
399 {
400 return GetAndTrimString(span.Slice(0, num2));
401 }
402 }
403 if (Marshal.GetLastWin32Error() == 122)
404 {
405 IntPtr intPtr = default(IntPtr);
406 try
407 {
408 int num3 = FormatMessage(num | 0x100, moduleHandle, (uint)errorCode, 0, &intPtr, 0, IntPtr.Zero);
409 if (num3 > 0)
410 {
411 return GetAndTrimString(new Span<char>((void*)intPtr, num3));
412 }
413 }
414 finally
415 {
416 Marshal.FreeHGlobal(intPtr);
417 }
418 }
419 return $"Unknown error (0x{errorCode:x})";
420 }
421
422 private static string GetAndTrimString(Span<char> buffer)
423 {
424 int num = buffer.Length;
425 while (num > 0 && buffer[num - 1] <= ' ')
426 {
427 num--;
428 }
429 return buffer.Slice(0, num).ToString();
430 }
431 }
432
433 internal static class BCrypt
434 {
462
463 internal struct BCRYPT_ECCKEY_BLOB
464 {
465 internal KeyBlobMagicNumber Magic;
466
467 internal int cbKey;
468 }
469
470 internal enum NTSTATUS : uint
471 {
472 STATUS_SUCCESS = 0u,
473 STATUS_NOT_FOUND = 3221226021u,
474 STATUS_INVALID_PARAMETER = 3221225485u,
475 STATUS_NO_MEMORY = 3221225495u,
476 STATUS_AUTH_TAG_MISMATCH = 3221266434u
477 }
478
479 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
480 internal static extern NTSTATUS BCryptDestroyKey(IntPtr hKey);
481
482 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
483 internal static extern NTSTATUS BCryptExportKey(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags);
484
485 [DllImport("BCrypt.dll", CharSet = CharSet.Unicode)]
486 internal unsafe static extern NTSTATUS BCryptGetProperty(Microsoft.Win32.SafeHandles.SafeBCryptHandle hObject, string pszProperty, void* pbOutput, int cbOutput, out int pcbResult, int dwFlags);
487
488 internal static byte[] Consume(byte[] blob, ref int offset, int count)
489 {
490 byte[] array = new byte[count];
491 Buffer.BlockCopy(blob, offset, array, 0, count);
492 offset += count;
493 return array;
494 }
495 }
496}
static unsafe NTSTATUS BCryptGetProperty(Microsoft.Win32.SafeHandles.SafeBCryptHandle hObject, string pszProperty, void *pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static NTSTATUS BCryptExportKey(Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle hKey, IntPtr hExportKey, string pszBlobType, [Out] byte[] pbOutput, int cbOutput, out int pcbResult, int dwFlags)
static NTSTATUS BCryptDestroyKey(IntPtr hKey)
static byte[] Consume(byte[] blob, ref int offset, int count)
Definition Interop.cs:488
static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
Definition Interop.cs:318
static unsafe int CertNameToStr(int dwCertEncodingType, void *pName, int dwStrType, char *psz, int csz)
static bool CryptMsgClose(IntPtr hCryptMsg)
static bool CertFreeCertificateContext(IntPtr pCertContext)
static bool CertCloseStore(IntPtr hCertStore, uint dwFlags)
static unsafe bool CryptFormatObject([In] int dwCertEncodingType, [In] int dwFormatType, [In] int dwFormatStrType, [In] IntPtr pFormatStruct, [In] byte *lpszStructType, [In] byte[] pbEncoded, [In] int cbEncoded, [Out] void *pbFormat, [In][Out] ref int pcbFormat)
static IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group)
static bool OidGroupWillNotUseActiveDirectory(OidGroup group)
Definition Interop.cs:900
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
Definition Interop.cs:387
static void SetLastError(int errorCode)
static string GetMessage(int errorCode)
Definition Interop.cs:382
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static string GetAndTrimString(Span< char > buffer)
Definition Interop.cs:153
static unsafe byte[] EncodeObject(CryptDecodeObjectStructType lpszStructType, void *decoded)
Definition Interop.cs:171
static bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, out CRYPTOAPI_BLOB pvData, [In][Out] ref int pcbData)
static unsafe bool CertGetValidUsages(int cCerts, [In] ref SafeCertContextHandle rghCerts, out int cNumOIDs, [Out] void *rghOIDs, [In][Out] ref int pcbOIDs)
static unsafe bool CryptQueryObject(CertQueryObjectType dwObjectType, void *pvObject, ExpectedContentTypeFlags dwExpectedContentTypeFlags, ExpectedFormatTypeFlags dwExpectedFormatTypeFlags, int dwFlags, IntPtr pdwMsgAndCertEncodingType, out ContentType pdwContentType, IntPtr pdwFormatType, out SafeCertStoreHandle phCertStore, IntPtr phMsg, IntPtr ppvContext)
static unsafe SafeCertContextHandle CertFindCertificateInStore(SafeCertStoreHandle hCertStore, CertEncodingType dwCertEncodingType, CertFindFlags dwFindFlags, CertFindType dwFindType, void *pvFindPara, CERT_CONTEXT *pPrevCertContext)
static bool CryptMsgGetParam(SafeCryptMsgHandle hCryptMsg, CryptMessageParameterType dwParamType, int dwIndex, out int pvData, [In][Out] ref int pcbData)
static unsafe bool CryptEncodeObject(CertEncodingType dwCertEncodingType, [MarshalAs(UnmanagedType.LPStr)] string lpszStructType, void *pvStructInfo, [Out] byte[] pbEncoded, [In][Out] ref int pcbEncoded)
static unsafe string CertGetNameString(SafeCertContextHandle certContext, CertNameType certNameType, CertNameFlags certNameFlags, CertNameStringType strType)
Definition Interop.cs:51
static bool CertVerifyCertificateChainPolicy(IntPtr pszPolicyOID, SafeX509ChainHandle pChainContext, [In] ref CERT_CHAIN_POLICY_PARA pPolicyPara, [In][Out] ref CERT_CHAIN_POLICY_STATUS pPolicyStatus)
static bool CertSaveStore(SafeCertStoreHandle hCertStore, CertEncodingType dwMsgAndCertEncodingType, CertStoreSaveAs dwSaveAs, CertStoreSaveTo dwSaveTo, ref CRYPTOAPI_BLOB pvSaveToPara, int dwFlags)
static unsafe bool CryptEncodeObject(CertEncodingType dwCertEncodingType, IntPtr lpszStructType, void *pvStructInfo, [Out] byte[] pbEncoded, [In][Out] ref int pcbEncoded)
static unsafe bool CryptEncodeObject(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, void *pvStructInfo, byte[] pbEncoded, ref int pcbEncoded)
Definition Interop.cs:160
static SafeCertStoreHandle CertOpenStore(CertStoreProvider lpszStoreProvider, CertEncodingType dwMsgAndCertEncodingType, IntPtr hCryptProv, CertStoreFlags dwFlags, string pvPara)
Definition Interop.cs:85
static SafeCertStoreHandle CertOpenStore(IntPtr lpszStoreProvider, CertEncodingType dwMsgAndCertEncodingType, IntPtr hCryptProv, CertStoreFlags dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pvPara)
static bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, [Out] byte[] pvData, [In][Out] ref int pcbData)
static unsafe int CertGetNameString(SafeCertContextHandle pCertContext, CertNameType dwType, CertNameFlags dwFlags, in CertNameStringType pvTypePara, char *pszNameString, int cchNameString)
static unsafe int CertVerifyTimeValidity([In] ref FILETIME pTimeToVerify, [In] CERT_INFO *pCertInfo)
static bool CertAddCertificateContextToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, CertStoreAddDisposition dwAddDisposition, IntPtr ppStoreContext)
static SafeX509ChainHandle CertDuplicateCertificateChain(IntPtr pChainContext)
static SafeCertStoreHandle PFXImportCertStore([In] ref CRYPTOAPI_BLOB pPFX, SafePasswordHandle password, PfxCertStoreFlags dwFlags)
static unsafe bool CryptImportPublicKeyInfoEx2(CertEncodingType dwCertEncodingType, CERT_PUBLIC_KEY_INFO *pInfo, CryptImportPublicKeyInfoFlags dwFlags, void *pvAuxInfo, out Microsoft.Win32.SafeHandles.SafeBCryptKeyHandle phKey)
static bool CryptHashPublicKeyInfo(IntPtr hCryptProv, int algId, int dwFlags, CertEncodingType dwCertEncodingType, [In] ref CERT_PUBLIC_KEY_INFO pInfo, [Out] byte[] pbComputedHash, [In][Out] ref int pcbComputedHash)
static bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, CertSetPropertyFlags dwFlags, [In] SafeNCryptKeyHandle keyHandle)
static bool CertAddCertificateLinkToStore(SafeCertStoreHandle hCertStore, SafeCertContextHandle pCertContext, CertStoreAddDisposition dwAddDisposition, IntPtr ppStoreContext)
static unsafe bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, void *pvStructInfo, ref int pcbStructInfo)
Definition Interop.cs:149
static SafeCertContextHandleWithKeyContainerDeletion CertDuplicateCertificateContextWithKeyContainerDeletion(IntPtr pCertContext)
static void CertFreeCertificateChainEngine(IntPtr hChainEngine)
static unsafe bool CryptQueryObject(CertQueryObjectType dwObjectType, void *pvObject, ExpectedContentTypeFlags dwExpectedContentTypeFlags, ExpectedFormatTypeFlags dwExpectedFormatTypeFlags, int dwFlags, IntPtr pdwMsgAndCertEncodingType, out ContentType pdwContentType, IntPtr pdwFormatType, IntPtr phCertStore, IntPtr phMsg, IntPtr ppvContext)
static unsafe bool CertGetIntendedKeyUsage(CertEncodingType dwCertEncodingType, CERT_INFO *pCertInfo, out X509KeyUsageFlags pbKeyUsage, int cbKeyUsage)
static bool PFXExportCertStore(SafeCertStoreHandle hStore, [In][Out] ref CRYPTOAPI_BLOB pPFX, SafePasswordHandle szPassword, PFXExportFlags dwFlags)
static bool CertControlStore(SafeCertStoreHandle hCertStore, CertControlStoreFlags dwFlags, CertControlStoreType dwControlType, IntPtr pvCtrlPara)
static bool CertSerializeCertificateStoreElement(SafeCertContextHandle pCertContext, int dwFlags, [Out] byte[] pbElement, [In][Out] ref int pcbElement)
static SafeCertContextHandle CertDuplicateCertificateContext(IntPtr pCertContext)
static SafeCertStoreHandle CertDuplicateStore(IntPtr hCertStore)
static unsafe bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, [MarshalAs(UnmanagedType.LPStr)] string lpszStructType, [In] byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, [Out] void *pvStructInfo, [In][Out] ref int pcbStructInfo)
static void CertFreeCertificateChain(IntPtr pChainContext)
static unsafe bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, CertSetPropertyFlags dwFlags, [In] CRYPT_KEY_PROV_INFO *pvData)
static unsafe bool CertDeleteCertificateFromStore(CERT_CONTEXT *pCertContext)
static unsafe bool CertGetCertificateContextPropertyString(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, byte *pvData, ref int pcbData)
static bool CertStrToName(CertEncodingType dwCertEncodingType, string pszX500, CertNameStrTypeAndFlags dwStrType, IntPtr pvReserved, [Out] byte[] pbEncoded, [In][Out] ref int pcbEncoded, IntPtr ppszError)
static bool CryptDecodeObject(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, byte[] pvStructInfo, ref int pcbStructInfo)
Definition Interop.cs:141
static unsafe bool CertSetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, CertSetPropertyFlags dwFlags, [In] CRYPTOAPI_BLOB *pvData)
static bool CryptAcquireCertificatePrivateKey(SafeCertContextHandle pCert, CryptAcquireFlags dwFlags, IntPtr pvParameters, out SafeNCryptKeyHandle phCryptProvOrNCryptKey, out int pdwKeySpec, out bool pfCallerFreeProvOrNCryptKey)
static unsafe bool CryptMsgGetParam(SafeCryptMsgHandle hCryptMsg, CryptMessageParameterType dwParamType, int dwIndex, byte *pvData, [In][Out] ref int pcbData)
static unsafe bool CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, [NotNull] ref SafeCertContextHandle pCertContext)
Definition Interop.cs:99
static unsafe CERT_EXTENSION * CertFindExtension([MarshalAs(UnmanagedType.LPStr)] string pszObjId, int cExtensions, CERT_EXTENSION *rgExtensions)
static unsafe CERT_CONTEXT * CertEnumCertificatesInStore(SafeCertStoreHandle hCertStore, CERT_CONTEXT *pPrevCertContext)
static bool CertVerifyCertificateChainPolicy(ChainPolicy pszPolicyOID, SafeX509ChainHandle pChainContext, ref CERT_CHAIN_POLICY_PARA pPolicyPara, ref CERT_CHAIN_POLICY_STATUS pPolicyStatus)
Definition Interop.cs:257
static bool CertCreateCertificateChainEngine(ref CERT_CHAIN_ENGINE_CONFIG pConfig, out SafeChainEngineHandle hChainEngineHandle)
static unsafe bool CryptQueryObject(CertQueryObjectType dwObjectType, void *pvObject, ExpectedContentTypeFlags dwExpectedContentTypeFlags, ExpectedFormatTypeFlags dwExpectedFormatTypeFlags, int dwFlags, out CertEncodingType pdwMsgAndCertEncodingType, out ContentType pdwContentType, out FormatType pdwFormatType, out SafeCertStoreHandle phCertStore, out SafeCryptMsgHandle phMsg, out SafeCertContextHandle ppvContext)
static unsafe byte[] EncodeObject(string lpszStructType, void *decoded)
Definition Interop.cs:186
static unsafe bool CertGetCertificateChain(IntPtr hChainEngine, SafeCertContextHandle pCertContext, FILETIME *pTime, SafeCertStoreHandle hStore, [In] ref CERT_CHAIN_PARA pChainPara, CertChainFlags dwFlags, IntPtr pvReserved, out SafeX509ChainHandle ppChainContext)
static bool CertGetCertificateContextProperty(SafeCertContextHandle pCertContext, CertContextPropId dwPropId, out IntPtr pvData, [In][Out] ref int pcbData)
static SafeChainEngineHandle CertCreateCertificateChainEngine(ref CERT_CHAIN_ENGINE_CONFIG config)
Definition Interop.cs:201
static unsafe bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, IntPtr lpszStructType, [In] byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, [Out] void *pvStructInfo, [In][Out] ref int pcbStructInfo)
static bool CryptDecodeObject(CertEncodingType dwCertEncodingType, IntPtr lpszStructType, [In] byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, [Out] byte[] pvStructInfo, [In][Out] ref int pcbStructInfo)
static unsafe bool CertFindCertificateInStore(SafeCertStoreHandle hCertStore, CertFindType dwFindType, void *pvFindPara, [NotNull] ref SafeCertContextHandle pCertContext)
Definition Interop.cs:226
static unsafe bool CryptAcquireContext(out IntPtr psafeProvHandle, char *pszContainer, char *pszProvider, int dwProvType, CryptAcquireContextFlags dwFlags)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static unsafe IntPtr StringToCoTaskMemUni(string? s)
Definition Marshal.cs:1327
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static void FreeCoTaskMem(IntPtr ptr)
Definition Marshal.cs:1712
static unsafe? string PtrToStringAnsi(IntPtr ptr)
Definition Marshal.cs:630
static ? object PtrToStructure(IntPtr ptr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type structureType)
Definition Marshal.cs:1164
static unsafe IntPtr StringToCoTaskMemAnsi(string? s)
Definition Marshal.cs:1362
static readonly IntPtr Zero
Definition IntPtr.cs:18
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70