8 internal static class Advapi32
69 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, EntryPoint =
"CryptAcquireContextW", SetLastError =
true)]
72 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
75 [DllImport(
"advapi32.dll", SetLastError =
true)]
78 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
81 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
84 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
87 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
90 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
93 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
96 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, EntryPoint =
"CryptGetDefaultProviderW", SetLastError =
true)]
99 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
102 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
105 [DllImport(
"advapi32.dll", SetLastError =
true)]
108 [DllImport(
"advapi32.dll", SetLastError =
true)]
111 [DllImport(
"advapi32.dll", SetLastError =
true)]
114 [DllImport(
"advapi32.dll", SetLastError =
true)]
123 if (dwDataLen > pbData.
Length)
133 [DllImport(
"advapi32.dll", SetLastError =
true)]
136 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
139 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, SetLastError =
true)]
142 [DllImport(
"advapi32.dll", SetLastError =
true)]
145 [DllImport(
"advapi32.dll", SetLastError =
true)]
148 [DllImport(
"advapi32.dll", SetLastError =
true)]
151 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, EntryPoint =
"CryptSignHashW", SetLastError =
true)]
154 [DllImport(
"advapi32.dll",
CharSet =
CharSet.Unicode, EntryPoint =
"CryptVerifySignatureW", SetLastError =
true)]
160 internal struct CRYPT_OID_INFO
192 intPtr = keyType
switch
212 if (fallBackToAllGroups && group != 0)
237 return group ==
OidGroup.KeyDerivationFunction;
248 [DllImport(
"kernel32.dll", BestFitMapping =
true,
CharSet =
CharSet.Unicode, EntryPoint =
"FormatMessageW", ExactSpelling =
true,
SetLastError =
true)]
249 private unsafe
static extern int FormatMessage(
int dwFlags,
IntPtr lpSource, uint dwMessageId,
int dwLanguageId,
void* lpBuffer,
int nSize,
IntPtr arguments);
264 fixed (
char* lpBuffer = span)
288 return $
"Unknown error (0x{errorCode:x})";
294 while (num > 0 &&
buffer[num - 1] <=
' ')
298 return buffer.Slice(0, num).ToString();
static bool CryptReleaseContext(IntPtr hProv, int dwFlags)
static bool CryptSetProvParam(SafeHandle safeProvHandle, CryptProvParam dwParam, IntPtr pbData, int dwFlags)
static bool CryptGetKeyParam(SafeKeyHandle hKey, CryptGetKeyParamFlags dwParam, byte[] pbData, ref int pdwDataLen, int dwFlags)
CryptSignAndVerifyHashFlags
static unsafe bool CryptGetProvParam(SafeHandle safeProvHandle, CryptProvParam dwParam, Span< byte > pbData, ref int dwDataLen)
static bool CryptGenKey(SafeProvHandle hProv, int Algid, int dwFlags, out SafeKeyHandle phKey)
static bool CryptVerifySignature(SafeHashHandle hHash, byte[] pbSignature, int dwSigLen, SafeKeyHandle hPubKey, string szDescription, CryptSignAndVerifyHashFlags dwFlags)
static bool CryptCreateHash(SafeProvHandle hProv, int Algid, SafeKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash)
static bool CryptSetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, byte[] buffer, int dwFlags)
static bool CryptEncrypt(SafeKeyHandle hKey, SafeHashHandle hHash, bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen, int dwBufLen)
static bool CryptHashData(SafeHashHandle hHash, byte[] pbData, int dwDataLen, int dwFlags)
static bool CryptAcquireContext(out SafeProvHandle phProv, string szContainer, string szProvider, int dwProvType, uint dwFlags)
static bool CryptGetProvParam(SafeHandle safeProvHandle, CryptProvParam dwParam, IntPtr pbData, ref int dwDataLen, int dwFlags)
static bool CryptExportKey(SafeKeyHandle hKey, SafeKeyHandle hExpKey, int dwBlobType, int dwFlags, [In][Out] byte[] pbData, ref int dwDataLen)
static bool CryptDeriveKey(SafeProvHandle hProv, int Algid, SafeHashHandle hBaseData, int dwFlags, out SafeKeyHandle phKey)
static bool CryptDestroyKey(IntPtr hKey)
static bool CryptGetHashParam(SafeHashHandle hHash, CryptHashProperty dwParam, out int pbData, [In][Out] ref int pdwDataLen, int dwFlags)
static bool CryptSetKeyParam(SafeKeyHandle safeKeyHandle, int dwParam, ref int pdw, int dwFlags)
static bool CryptSetProvParam(SafeProvHandle hProv, CryptProvParam dwParam, ref IntPtr pbData, int dwFlags)
static bool CryptSetKeyParam(SafeKeyHandle hKey, int dwParam, byte[] pbData, int dwFlags)
static bool CryptDestroyHash(IntPtr hHash)
static bool CryptGetDefaultProvider(int dwProvType, IntPtr pdwReserved, GetDefaultProviderFlags dwFlags, [Out] StringBuilder pszProvName, ref int pcbProvName)
static bool CryptGetUserKey(SafeProvHandle hProv, int dwKeySpec, out SafeKeyHandle phUserKey)
static bool CryptDecrypt(SafeKeyHandle hKey, SafeHashHandle hHash, bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen)
static bool CryptSignHash(SafeHashHandle hHash, KeySpec dwKeySpec, string szDescription, CryptSignAndVerifyHashFlags dwFlags, [Out] byte[] pbSignature, [In][Out] ref int pdwSigLen)
static unsafe bool CryptImportKey(SafeProvHandle hProv, byte *pbData, int dwDataLen, SafeKeyHandle hPubKey, int dwFlags, out SafeKeyHandle phKey)
static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
@ CRYPT_OID_INFO_NAME_KEY
@ CRYPT_OID_INFO_CNG_SIGN_KEY
@ CRYPT_OID_INFO_SIGN_KEY
@ CRYPT_OID_INFO_CNG_ALGID_KEY
@ CRYPT_OID_INFO_ALGID_KEY
static IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group)
static bool OidGroupWillNotUseActiveDirectory(OidGroup group)
static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
static void SetLastError(int errorCode)
static string GetMessage(int errorCode)
static unsafe int FormatMessage(int dwFlags, IntPtr lpSource, uint dwMessageId, int dwLanguageId, void *lpBuffer, int nSize, IntPtr arguments)
static string GetAndTrimString(Span< char > buffer)
static unsafe IntPtr StringToCoTaskMemUni(string? s)
static void FreeHGlobal(IntPtr hglobal)
static void FreeCoTaskMem(IntPtr ptr)
static int GetLastWin32Error()
static ? object PtrToStructure(IntPtr ptr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type structureType)
static unsafe IntPtr StringToCoTaskMemAnsi(string? s)
static readonly IntPtr Zero
Span< T > Slice(int start)