Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
Interop.cs
Go to the documentation of this file.
1using System;
4
5internal static class Interop
6{
7 internal static class Crypt32
8 {
9 internal struct CRYPT_OID_INFO
10 {
11 public int cbSize;
12
13 public IntPtr pszOID;
14
15 public IntPtr pwszName;
16
17 public OidGroup dwGroupId;
18
19 public int AlgId;
20
21 public int cbData;
22
23 public IntPtr pbData;
24
25 public string OID => Marshal.PtrToStringAnsi(pszOID);
26
27 public string Name => Marshal.PtrToStringUni(pwszName);
28 }
29
39
40 [DllImport("crypt32.dll", BestFitMapping = false, SetLastError = true)]
41 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);
42
43 internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
44 {
45 IntPtr intPtr = IntPtr.Zero;
46 try
47 {
48 intPtr = keyType switch
49 {
50 CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY => Marshal.StringToCoTaskMemAnsi(key),
51 CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY => Marshal.StringToCoTaskMemUni(key),
52 _ => throw new NotSupportedException(),
53 };
55 {
56 OidGroup group2 = group | (OidGroup)(-2147483648);
57 IntPtr intPtr2 = CryptFindOIDInfo(keyType, intPtr, group2);
58 if (intPtr2 != IntPtr.Zero)
59 {
60 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr2);
61 }
62 }
63 IntPtr intPtr3 = CryptFindOIDInfo(keyType, intPtr, group);
64 if (intPtr3 != IntPtr.Zero)
65 {
66 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr3);
67 }
68 if (fallBackToAllGroups && group != 0)
69 {
70 IntPtr intPtr4 = CryptFindOIDInfo(keyType, intPtr, OidGroup.All);
71 if (intPtr4 != IntPtr.Zero)
72 {
73 return Marshal.PtrToStructure<CRYPT_OID_INFO>(intPtr4);
74 }
75 }
76 CRYPT_OID_INFO result = default(CRYPT_OID_INFO);
77 result.AlgId = -1;
78 return result;
79 }
80 finally
81 {
82 if (intPtr != IntPtr.Zero)
83 {
84 Marshal.FreeCoTaskMem(intPtr);
85 }
86 }
87 }
88
90 {
91 if (group != OidGroup.HashAlgorithm && group != OidGroup.EncryptionAlgorithm && group != OidGroup.PublicKeyAlgorithm && group != OidGroup.SignatureAlgorithm && group != OidGroup.Attribute && group != OidGroup.ExtensionOrAttribute)
92 {
93 return group == OidGroup.KeyDerivationFunction;
94 }
95 return true;
96 }
97
98 [DllImport("crypt32.dll", CharSet = CharSet.Unicode)]
99 private static extern IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group);
100 }
101}
static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
Definition Interop.cs:43
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 IntPtr StringToCoTaskMemUni(string? s)
Definition Marshal.cs:1327
static void FreeCoTaskMem(IntPtr ptr)
Definition Marshal.cs:1712
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
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