5 internal static class CapiNative
7 internal enum AlgorithmClass
15 internal enum AlgorithmType
21 internal enum AlgorithmSubId
31 internal enum AlgorithmID
35 RsaKeyExchange = 41984,
43 internal enum CryptAcquireContextFlags
50 VerifyContext = -268435456
53 internal enum ErrorCode
57 BadHash = -2146893822,
58 BadData = -2146893819,
59 BadSignature = -2146893818,
63 internal enum HashProperty
71 internal enum KeyGenerationFlags
79 internal enum KeyProperty
92 internal static class ProviderNames
94 internal const string MicrosoftEnhanced =
"Microsoft Enhanced Cryptographic Provider v1.0";
97 internal enum ProviderType
103 internal static class UnsafeNativeMethods
105 [DllImport(
"advapi32",
CharSet =
CharSet.Unicode, SetLastError =
true)]
107 internal static extern bool CryptAcquireContext(out SafeCspHandle phProv,
string pszContainer,
string pszProvider, ProviderType dwProvType, CryptAcquireContextFlags dwFlags);
109 [DllImport(
"advapi32", SetLastError =
true)]
111 internal static extern bool CryptCreateHash(SafeCspHandle hProv, AlgorithmID Algid, IntPtr hKey,
int dwFlags, out SafeCspHashHandle phHash);
113 [DllImport(
"advapi32", SetLastError =
true)]
115 internal static extern bool CryptGenKey(SafeCspHandle hProv,
int Algid, uint dwFlags, out SafeCspKeyHandle phKey);
117 [DllImport(
"advapi32", SetLastError =
true)]
119 internal static extern bool CryptGenRandom(SafeCspHandle hProv,
int dwLen, [In] [Out] [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbBuffer);
121 [DllImport(
"advapi32", SetLastError =
true)]
123 internal unsafe
static extern bool CryptGenRandom(SafeCspHandle hProv,
int dwLen,
byte* pbBuffer);
125 [DllImport(
"advapi32", SetLastError =
true)]
127 internal static extern bool CryptGetHashParam(SafeCspHashHandle hHash, HashProperty dwParam, [In] [Out] [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbData, [In] [Out] ref
int pdwDataLen,
int dwFlags);
129 [DllImport(
"advapi32", SetLastError =
true)]
131 internal static extern bool CryptGetKeyParam(SafeCspKeyHandle hKey, KeyProperty dwParam, [In] [Out] [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbData, [In] [Out] ref
int pdwDataLen,
int dwFlags);
133 [DllImport(
"advapi32", SetLastError =
true)]
135 internal static extern bool CryptImportKey(SafeCspHandle hProv, [In] [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbData,
int pdwDataLen, IntPtr hPubKey, KeyGenerationFlags dwFlags, out SafeCspKeyHandle phKey);
137 [DllImport(
"advapi32", SetLastError =
true)]
139 internal static extern bool CryptSetHashParam(SafeCspHashHandle hHash, HashProperty dwParam, [In] [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbData,
int dwFlags);
141 [DllImport(
"advapi32",
CharSet =
CharSet.Unicode, SetLastError =
true)]
143 internal static extern bool CryptVerifySignature(SafeCspHashHandle hHash, [In] [MarshalAs(
UnmanagedType.LPArray)]
byte[] pbSignature,
int dwSigLen, SafeCspKeyHandle hPubKey,
string sDescription,
int dwFlags);
147 internal static SafeCspHandle AcquireCsp(
string keyContainer,
string providerName, ProviderType providerType, CryptAcquireContextFlags flags)
149 if ((flags & CryptAcquireContextFlags.VerifyContext) == CryptAcquireContextFlags.VerifyContext && (flags & CryptAcquireContextFlags.MachineKeyset) == CryptAcquireContextFlags.MachineKeyset)
151 flags &= ~CryptAcquireContextFlags.MachineKeyset;
153 SafeCspHandle phProv =
null;
154 if (!UnsafeNativeMethods.CryptAcquireContext(out phProv, keyContainer, providerName, providerType, flags))
162 internal static SafeCspHashHandle CreateHashAlgorithm(SafeCspHandle cspHandle, AlgorithmID algorithm)
164 SafeCspHashHandle phHash =
null;
165 if (!UnsafeNativeMethods.CryptCreateHash(cspHandle, algorithm, IntPtr.Zero, 0, out phHash))
173 internal static void GenerateRandomBytes(SafeCspHandle cspHandle,
byte[] buffer)
175 if (!UnsafeNativeMethods.CryptGenRandom(cspHandle, buffer.Length, buffer))
182 internal unsafe
static void GenerateRandomBytes(SafeCspHandle cspHandle,
byte[] buffer,
int offset,
int count)
184 fixed (
byte* pbBuffer = &buffer[offset])
186 if (!UnsafeNativeMethods.CryptGenRandom(cspHandle, count, pbBuffer))
194 internal static int GetHashPropertyInt32(SafeCspHashHandle hashHandle, HashProperty property)
196 byte[] hashProperty = GetHashProperty(hashHandle, property);
197 if (hashProperty.Length != 4)
201 return BitConverter.ToInt32(hashProperty, 0);
205 internal static byte[] GetHashProperty(SafeCspHashHandle hashHandle, HashProperty property)
208 byte[] pbData =
null;
209 if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, pbData, ref pdwDataLen, 0))
212 if (lastWin32Error != 234)
214 throw new CryptographicException(lastWin32Error);
217 pbData =
new byte[pdwDataLen];
218 if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, property, pbData, ref pdwDataLen, 0))
226 internal static int GetKeyPropertyInt32(SafeCspKeyHandle keyHandle, KeyProperty property)
228 byte[] keyProperty = GetKeyProperty(keyHandle, property);
229 if (keyProperty.Length != 4)
233 return BitConverter.ToInt32(keyProperty, 0);
237 internal static byte[] GetKeyProperty(SafeCspKeyHandle keyHandle, KeyProperty property)
240 byte[] pbData =
null;
241 if (!UnsafeNativeMethods.CryptGetKeyParam(keyHandle, property, pbData, ref pdwDataLen, 0))
244 if (lastWin32Error != 234)
246 throw new CryptographicException(lastWin32Error);
249 pbData =
new byte[pdwDataLen];
250 if (!UnsafeNativeMethods.CryptGetKeyParam(keyHandle, property, pbData, ref pdwDataLen, 0))
258 internal static void SetHashProperty(SafeCspHashHandle hashHandle, HashProperty property,
byte[] value)
260 if (!UnsafeNativeMethods.CryptSetHashParam(hashHandle, property, value, 0))
267 internal static bool VerifySignature(SafeCspHandle cspHandle, SafeCspKeyHandle keyHandle, AlgorithmID signatureAlgorithm, AlgorithmID hashAlgorithm,
byte[] hashValue,
byte[] signature)
269 byte[] array =
new byte[signature.Length];
270 Array.Copy(signature, array, array.Length);
271 Array.Reverse(array);
272 using (SafeCspHashHandle safeCspHashHandle = CreateHashAlgorithm(cspHandle, hashAlgorithm))
274 if (hashValue.Length != GetHashPropertyInt32(safeCspHashHandle, HashProperty.HashSize))
276 throw new CryptographicException(-2146893822);
278 SetHashProperty(safeCspHashHandle, HashProperty.HashValue, hashValue);
279 if (UnsafeNativeMethods.CryptVerifySignature(safeCspHashHandle, array, array.Length, keyHandle,
null, 0))
284 if (lastWin32Error != -2146893818)
286 throw new CryptographicException(lastWin32Error);
A hash algorithm is used to generate key material. The P:System.Security.Cryptography....
The Secure Hashing Algorithm 2 (SHA-2), using a 256-bit digest.
The Secure Hashing Algorithm 2 (SHA-2), using a 512-bit digest.
No initialization action.
A signature key pair used for authenticating digitally signed messages or files.
Imported keys are marked as exportable.
The Secure Hashing Algorithm 2 (SHA-2), using a 384-bit digest.
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
UI prompting is suppressed.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
The RSA public-key signature algorithm.
CharSet
Dictates which character set marshaled strings should use.
The email was successfully sent to the SMTP service.
The Secure Hashing Algorithm (SHA1).
Notify the user through a dialog box or other method that the key is accessed. The Cryptographic Serv...
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...