7 internal static class X509Utils
9 private static bool OidGroupWillNotUseActiveDirectory(OidGroup group)
13 return group ==
OidGroup.KeyDerivationFunction;
19 private static CRYPT_OID_INFO FindOidInfo(OidKeyType keyType,
string key, OidGroup group)
21 IntPtr intPtr = IntPtr.Zero;
26 if (!OidGroupWillNotUseActiveDirectory(group))
29 IntPtr intPtr2 = CryptFindOIDInfo(keyType, intPtr, dwGroupId);
30 if (intPtr2 != IntPtr.Zero)
35 IntPtr intPtr3 = CryptFindOIDInfo(keyType, intPtr, group);
36 if (intPtr3 != IntPtr.Zero)
42 IntPtr intPtr4 = CryptFindOIDInfo(keyType, intPtr,
OidGroup.AllGroups);
43 if (intPtr4 != IntPtr.Zero)
48 return default(CRYPT_OID_INFO);
52 if (intPtr != IntPtr.Zero)
59 [SecuritySafeCritical]
60 internal static int GetAlgIdFromOid(
string oid, OidGroup oidGroup)
74 return FindOidInfo(OidKeyType.Oid, oid, oidGroup).AlgId;
77 [SecuritySafeCritical]
78 internal static string GetFriendlyNameFromOid(
string oid, OidGroup oidGroup)
80 CRYPT_OID_INFO cRYPT_OID_INFO = FindOidInfo(OidKeyType.Oid, oid, oidGroup);
81 return cRYPT_OID_INFO.pwszName;
84 [SecuritySafeCritical]
85 internal static string GetOidFromFriendlyName(
string friendlyName, OidGroup oidGroup)
87 CRYPT_OID_INFO cRYPT_OID_INFO = FindOidInfo(OidKeyType.Name, friendlyName, oidGroup);
88 return cRYPT_OID_INFO.pszOID;
91 internal static int NameOrOidToAlgId(
string oid, OidGroup oidGroup)
97 string text = CryptoConfig.MapNameToOID(oid, oidGroup);
102 int algIdFromOid = GetAlgIdFromOid(text, oidGroup);
103 if (algIdFromOid == 0 || algIdFromOid == -1)
105 throw new CryptographicException(Environment.GetResourceString(
"Cryptography_InvalidOID"));
136 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidFlag"),
"keyStorageFlags");
159 internal static SafeCertStoreHandle ExportCertToMemoryStore(X509Certificate certificate)
161 SafeCertStoreHandle safeCertStoreHandle = SafeCertStoreHandle.InvalidHandle;
162 _OpenX509Store(2u, 8704u,
null, ref safeCertStoreHandle);
163 _AddCertificateToStore(safeCertStoreHandle, certificate.CertContext);
164 return safeCertStoreHandle;
168 internal static IntPtr PasswordToHGlobalUni(
object password)
170 if (password !=
null)
172 string text = password as string;
177 SecureString secureString = password as SecureString;
178 if (secureString !=
null)
186 [DllImport(
"crypt32")]
188 [SuppressUnmanagedCodeSecurity]
189 private static extern IntPtr CryptFindOIDInfo(OidKeyType dwKeyType, IntPtr pvKey, OidGroup dwGroupId);
193 internal static extern void _AddCertificateToStore(SafeCertStoreHandle safeCertStoreHandle, SafeCertContextHandle safeCertContext);
197 internal static extern void _DuplicateCertContext(IntPtr handle, ref SafeCertContextHandle safeCertContext);
201 internal static extern byte[] _ExportCertificatesToBlob(SafeCertStoreHandle safeCertStoreHandle,
X509ContentType contentType, IntPtr password);
205 internal static extern byte[] _GetCertRawData(SafeCertContextHandle safeCertContext);
209 internal static extern void _GetDateNotAfter(SafeCertContextHandle safeCertContext, ref Win32Native.FILE_TIME fileTime);
213 internal static extern void _GetDateNotBefore(SafeCertContextHandle safeCertContext, ref Win32Native.FILE_TIME fileTime);
217 internal static extern string _GetIssuerName(SafeCertContextHandle safeCertContext,
bool legacyV1Mode);
221 internal static extern string _GetPublicKeyOid(SafeCertContextHandle safeCertContext);
225 internal static extern byte[] _GetPublicKeyParameters(SafeCertContextHandle safeCertContext);
229 internal static extern byte[] _GetPublicKeyValue(SafeCertContextHandle safeCertContext);
233 internal static extern string _GetSubjectInfo(SafeCertContextHandle safeCertContext, uint displayType,
bool legacyV1Mode);
237 internal static extern byte[] _GetSerialNumber(SafeCertContextHandle safeCertContext);
241 internal static extern byte[] _GetThumbprint(SafeCertContextHandle safeCertContext);
245 internal static extern void _LoadCertFromBlob(
byte[] rawData, IntPtr password, uint dwFlags,
bool persistKeySet, ref SafeCertContextHandle pCertCtx);
249 internal static extern void _LoadCertFromFile(
string fileName, IntPtr password, uint dwFlags,
bool persistKeySet, ref SafeCertContextHandle pCertCtx);
253 internal static extern void _OpenX509Store(uint storeType, uint flags,
string storeName, ref SafeCertStoreHandle safeCertStoreHandle);
257 internal static extern uint _QueryCertBlobType(
byte[] rawData);
261 internal static extern uint _QueryCertFileType(
string fileName);
static unsafe IntPtr StringToHGlobalUni(string s)
Copies the contents of a managed T:System.String into unmanaged memory.
static void FreeCoTaskMem(IntPtr ptr)
Frees a block of memory allocated by the unmanaged COM task memory allocator.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
static IntPtr SecureStringToGlobalAllocUnicode(SecureString s)
Copies the contents of a managed T:System.Security.SecureString object into unmanaged memory.
X509ContentType
Specifies the format of an X.509 certificate.
OidGroup
Identifies Windows cryptographic object identifier (OID) groups.
static unsafe IntPtr StringToCoTaskMemAnsi(string s)
Copies the contents of a managed T:System.String to a block of memory allocated from the unmanaged CO...
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
MethodImplOptions
Defines the details of how a method is implemented.
static unsafe IntPtr StringToCoTaskMemUni(string s)
Copies the contents of a managed T:System.String to a block of memory allocated from the unmanaged CO...
static void PtrToStructure(IntPtr ptr, object structure)
Marshals data from an unmanaged block of memory to a managed object.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
X509KeyStorageFlags
Defines where and how to import the private key of an X.509 certificate.
Provides a set of static methods and properties that provide support for compilers....