2 * Copyright 1999 Ian Schmidt
3 * Copyright 2001 Travis Michielsen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /***********************************************************************
23 * - Reference counting
25 * - Signature checking
35 HWND crypt_hWindow
= 0;
37 #define CRYPT_ReturnLastError(err) {SetLastError(err); return FALSE;}
39 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
40 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
42 static inline PSTR
CRYPT_GetProvKeyName(PCSTR pProvName
)
44 PCSTR KEYSTR
= "Software\\Microsoft\\Cryptography\\Defaults\\Provider\\";
47 keyname
= CRYPT_Alloc(strlen(KEYSTR
) + strlen(pProvName
) +1);
50 strcpy(keyname
, KEYSTR
);
51 strcpy(keyname
+ strlen(KEYSTR
), pProvName
);
55 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
61 static inline PSTR
CRYPT_GetTypeKeyName(DWORD dwType
, BOOL user
)
63 PCSTR MACHINESTR
= "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
64 PCSTR USERSTR
= "Software\\Microsoft\\Cryptography\\Provider Type XXX";
68 keyname
= CRYPT_Alloc( (user
? strlen(USERSTR
) : strlen(MACHINESTR
)) +1);
71 user
? strcpy(keyname
, USERSTR
) : strcpy(keyname
, MACHINESTR
);
72 ptr
= keyname
+ strlen(keyname
);
73 *(--ptr
) = (dwType
% 10) + '0';
74 *(--ptr
) = ((dwType
/ 10) % 10) + '0';
75 *(--ptr
) = (dwType
/ 100) + '0';
79 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
84 /* CRYPT_UnicodeTOANSI
85 * wstr - unicode string
86 * str - pointer to ANSI string
87 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
89 * returns TRUE if unsuccessfull, FALSE otherwise.
90 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
92 static inline BOOL
CRYPT_UnicodeToANSI(LPCWSTR wstr
, LPSTR
* str
, int strsize
)
101 count
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
102 count
= count
< strsize
? count
: strsize
;
104 *str
= CRYPT_Alloc(count
* sizeof(CHAR
));
107 WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, *str
, count
, NULL
, NULL
);
110 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
114 /* CRYPT_ANSITOUnicode
116 * wstr - pointer to unicode string
117 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
119 static inline BOOL
CRYPT_ANSIToUnicode(LPCSTR str
, LPWSTR
* wstr
, int wstrsize
)
128 wcount
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
129 wcount
= wcount
< wstrsize
/sizeof(WCHAR
) ? wcount
: wstrsize
/sizeof(WCHAR
);
131 *wstr
= CRYPT_Alloc(wcount
* sizeof(WCHAR
));
134 MultiByteToWideChar(CP_ACP
, 0, str
, -1, *wstr
, wcount
);
137 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
141 /* These next 2 functions are used by the VTableProvStruc structure */
142 static BOOL CALLBACK
CRYPT_VerifyImage(LPCSTR lpszImage
, BYTE
* pData
)
144 if (!lpszImage
|| !pData
)
146 SetLastError(ERROR_INVALID_PARAMETER
);
150 DPRINT1("FIXME (%s, %p): not verifying image\n", lpszImage
, pData
);
155 static BOOL CALLBACK
CRYPT_ReturnhWnd(HWND
*phWnd
)
159 *phWnd
= crypt_hWindow
;
163 #define CRYPT_GetProvFunc(name) \
164 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
166 #define CRYPT_GetProvFuncOpt(name) \
167 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
168 PCRYPTPROV
CRYPT_LoadProvider(PSTR pImage
)
171 DWORD errorcode
= ERROR_NOT_ENOUGH_MEMORY
;
173 if (!(provider
= CRYPT_Alloc(sizeof(CRYPTPROV
))))
175 if (!(provider
->pFuncs
= CRYPT_Alloc(sizeof(PROVFUNCS
))))
177 if (!(provider
->pVTable
= CRYPT_Alloc(sizeof(VTableProvStruc
))))
179 if (!(provider
->hModule
= LoadLibraryA(pImage
)))
181 errorcode
= (GetLastError() == ERROR_FILE_NOT_FOUND
) ? NTE_PROV_DLL_NOT_FOUND
: NTE_PROVIDER_DLL_FAIL
;
182 DPRINT1("Failed to load dll %s\n", pImage
);
185 provider
->refcount
= 1;
187 errorcode
= NTE_PROVIDER_DLL_FAIL
;
188 CRYPT_GetProvFunc(CPAcquireContext
);
189 CRYPT_GetProvFunc(CPCreateHash
);
190 CRYPT_GetProvFunc(CPDecrypt
);
191 CRYPT_GetProvFunc(CPDeriveKey
);
192 CRYPT_GetProvFunc(CPDestroyHash
);
193 CRYPT_GetProvFunc(CPDestroyKey
);
194 CRYPT_GetProvFuncOpt(CPDuplicateHash
);
195 CRYPT_GetProvFuncOpt(CPDuplicateKey
);
196 CRYPT_GetProvFunc(CPEncrypt
);
197 CRYPT_GetProvFunc(CPExportKey
);
198 CRYPT_GetProvFunc(CPGenKey
);
199 CRYPT_GetProvFunc(CPGenRandom
);
200 CRYPT_GetProvFunc(CPGetHashParam
);
201 CRYPT_GetProvFunc(CPGetKeyParam
);
202 CRYPT_GetProvFunc(CPGetProvParam
);
203 CRYPT_GetProvFunc(CPGetUserKey
);
204 CRYPT_GetProvFunc(CPHashData
);
205 CRYPT_GetProvFunc(CPHashSessionKey
);
206 CRYPT_GetProvFunc(CPImportKey
);
207 CRYPT_GetProvFunc(CPReleaseContext
);
208 CRYPT_GetProvFunc(CPSetHashParam
);
209 CRYPT_GetProvFunc(CPSetKeyParam
);
210 CRYPT_GetProvFunc(CPSetProvParam
);
211 CRYPT_GetProvFunc(CPSignHash
);
212 CRYPT_GetProvFunc(CPVerifySignature
);
214 /* FIXME: Not sure what the pbContextInfo field is for.
215 * Does it need memory allocation?
217 provider
->pVTable
->Version
= 3;
218 provider
->pVTable
->pFuncVerifyImage
= (FARPROC
)CRYPT_VerifyImage
;
219 provider
->pVTable
->pFuncReturnhWnd
= (FARPROC
)CRYPT_ReturnhWnd
;
220 provider
->pVTable
->dwProvType
= 0;
221 provider
->pVTable
->pbContextInfo
= NULL
;
222 provider
->pVTable
->cbContextInfo
= 0;
223 provider
->pVTable
->pszProvName
= NULL
;
227 SetLastError(errorcode
);
230 if (provider
->hModule
)
231 FreeLibrary(provider
->hModule
);
232 CRYPT_Free(provider
->pVTable
);
233 CRYPT_Free(provider
->pFuncs
);
234 CRYPT_Free(provider
);
238 #undef CRYPT_GetProvFunc
239 #undef CRYPT_GetProvFuncOpt
242 /******************************************************************************
243 * CryptAcquireContextA (ADVAPI32.@)
244 * Acquire a crypto provider context handle.
247 * phProv: Pointer to HCRYPTPROV for the output.
248 * pszContainer: Key Container Name
249 * pszProvider: Cryptographic Service Provider Name
250 * dwProvType: Crypto provider type to get a handle.
251 * dwFlags: flags for the operation
253 * RETURNS TRUE on success, FALSE on failure.
255 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
256 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
258 PCRYPTPROV pProv
= NULL
;
260 PSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
262 DWORD keytype
, type
, len
;
265 DPRINT("(%p, %s, %s, %ld, %08lx)\n", phProv
, pszContainer
,
266 pszProvider
, dwProvType
, dwFlags
);
268 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
270 SetLastError(NTE_BAD_PROV_TYPE
);
276 SetLastError(ERROR_INVALID_PARAMETER
);
280 if (!pszProvider
|| !*pszProvider
)
282 /* No CSP name specified so try the user default CSP first
283 * then try the machine default CSP
285 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)))
287 DPRINT("No provider registered for crypto provider type %ld.\n", dwProvType
);
288 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
292 if (RegOpenKeyA(HKEY_CURRENT_USER
, keyname
, &key
))
295 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)))
297 DPRINT("No type registered for crypto provider type %ld.\n", dwProvType
);
299 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
303 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, keyname
, &key
))
305 DPRINT("Did not find registry entry of crypto provider for %S.\n", keyname
);
308 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
314 r
= RegQueryValueExA(key
, "Name", NULL
, &keytype
, NULL
, &len
);
315 if (r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
317 DPRINT("error %ld reading size of 'Name' from registry\n", r
);
319 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
322 if (!(provname
= CRYPT_Alloc(len
)))
325 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
328 r
= RegQueryValueExA(key
, "Name", NULL
, NULL
, (LPBYTE
)provname
, &len
);
329 if (r
!= ERROR_SUCCESS
)
331 DPRINT("error %ld reading 'Name' from registry\n", r
);
333 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
340 if (!(provname
= CRYPT_Alloc(strlen(pszProvider
) +1)))
342 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
345 strcpy(provname
, pszProvider
);
348 keyname
= CRYPT_GetProvKeyName(provname
);
349 r
= RegOpenKeyA(HKEY_LOCAL_MACHINE
, keyname
, &key
);
351 if (r
!= ERROR_SUCCESS
)
353 SetLastError(NTE_KEYSET_NOT_DEF
);
357 r
= RegQueryValueExA(key
, "Type", NULL
, NULL
, (BYTE
*)&type
, &len
);
358 if (r
!= ERROR_SUCCESS
)
360 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
363 if (type
!= dwProvType
)
365 DPRINT("Crypto provider has wrong type (%ld vs expected %ld).\n", type
, dwProvType
);
366 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
370 r
= RegQueryValueExA(key
, "Image Path", NULL
, &keytype
, NULL
, &len
);
371 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
373 DPRINT("error %ld reading size of 'Image Path' from registry\n", r
);
375 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
378 if (!(temp
= CRYPT_Alloc(len
)))
381 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
384 r
= RegQueryValueExA(key
, "Image Path", NULL
, NULL
, (LPBYTE
)temp
, &len
);
385 if (r
!= ERROR_SUCCESS
)
387 DPRINT("error %ld reading 'Image Path' from registry\n", r
);
389 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
393 r
= RegQueryValueExA(key
, "Signature", NULL
, &keytype
, NULL
, &len
);
394 if (r
== ERROR_SUCCESS
&& keytype
== REG_BINARY
)
396 if (!(signature
= CRYPT_Alloc(len
)))
399 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
402 r
= RegQueryValueExA(key
, "Signature", NULL
, NULL
, signature
, &len
);
403 if (r
!= ERROR_SUCCESS
)
405 DPRINT("error %ld reading 'Signature'\n", r
);
406 CRYPT_Free(signature
);
408 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
414 r
= RegQueryValueExA(key
, "SigInFile", NULL
, &keytype
, NULL
, &len
);
415 if (r
!= ERROR_SUCCESS
)
417 DPRINT("error %ld reading size of 'SigInFile'\n", r
);
419 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
424 /* FIXME: The presence of the SigInFile value indicates the
425 * provider's signature is in its resources, so need to read it.
426 * But since CRYPT_VerifyImage is stubbed, provide any old thing
429 if (!(signature
= CRYPT_Alloc(1)))
432 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
438 len
= ExpandEnvironmentStringsA(temp
, NULL
, 0);
439 if (!(imagepath
= CRYPT_Alloc(len
)))
441 CRYPT_Free(signature
);
442 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
445 if (!ExpandEnvironmentStringsA(temp
, imagepath
, len
))
447 CRYPT_Free(signature
);
448 /* ExpandEnvironmentStrings will call SetLastError */
452 if (!CRYPT_VerifyImage(imagepath
, signature
))
454 CRYPT_Free(signature
);
455 SetLastError(NTE_SIGNATURE_FILE_BAD
);
458 pProv
= CRYPT_LoadProvider(imagepath
);
459 CRYPT_Free(signature
);
462 /* CRYPT_LoadProvider calls SetLastError */
465 pProv
->pVTable
->dwProvType
= dwProvType
;
466 pProv
->pVTable
->pszProvName
= provname
;
467 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, (CHAR
*)pszContainer
, dwFlags
, pProv
->pVTable
))
469 /* MSDN: When this flag is set, the value returned in phProv is undefined,
470 * and thus, the CryptReleaseContext function need not be called afterwards.
471 * Therefore, we must clean up everything now.
473 if (dwFlags
& CRYPT_DELETEKEYSET
)
475 FreeLibrary(pProv
->hModule
);
476 CRYPT_Free(provname
);
477 CRYPT_Free(pProv
->pFuncs
);
482 *phProv
= (HCRYPTPROV
)pProv
;
485 CRYPT_Free(imagepath
);
488 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
493 FreeLibrary(pProv
->hModule
);
495 CRYPT_Free(pProv
->pVTable
);
497 CRYPT_Free(pProv
->pFuncs
);
501 CRYPT_Free(provname
);
505 CRYPT_Free(imagepath
);
509 /******************************************************************************
510 * CryptAcquireContextW (ADVAPI32.@)
512 * see CryptAcquireContextA
514 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
515 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
517 PSTR pProvider
= NULL
, pContainer
= NULL
;
520 DPRINT("(%p, %S, %S, %ld, %08lx)\n", phProv
, pszContainer
,
521 pszProvider
, dwProvType
, dwFlags
);
523 if (!CRYPT_UnicodeToANSI(pszContainer
, &pContainer
, -1))
524 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
525 if (!CRYPT_UnicodeToANSI(pszProvider
, &pProvider
, -1))
527 CRYPT_Free(pContainer
);
528 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
531 ret
= CryptAcquireContextA(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
534 CRYPT_Free(pContainer
);
536 CRYPT_Free(pProvider
);
541 /******************************************************************************
542 * CryptContextAddRef (ADVAPI32.@)
544 * Increases reference count of a cryptographic service provider handle
548 * hProv [I] Handle to the CSP whose reference is being incremented.
549 * pdwReserved [IN] Reserved for future use and must be NULL.
550 * dwFlags [I] Reserved for future use and must be NULL.
556 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
558 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
560 DPRINT("(0x%lx, %p, %08lx)\n", hProv
, pdwReserved
, dwFlags
);
564 SetLastError(NTE_BAD_UID
);
572 /******************************************************************************
573 * CryptReleaseContext (ADVAPI32.@)
575 * Releases the handle of a CSP. Reference count is decreased.
578 * hProv [I] Handle of a CSP.
579 * dwFlags [I] Reserved for future use and must be NULL.
585 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, DWORD dwFlags
)
587 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
590 DPRINT("(0x%lx, %08ld)\n", hProv
, dwFlags
);
594 SetLastError(NTE_BAD_UID
);
599 if (pProv
->refcount
<= 0)
601 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
602 FreeLibrary(pProv
->hModule
);
604 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
606 CRYPT_Free(pProv
->pVTable
->pszProvName
);
607 CRYPT_Free(pProv
->pVTable
);
608 CRYPT_Free(pProv
->pFuncs
);
614 /******************************************************************************
615 * CryptGenRandom (ADVAPI32.@)
617 * Fills a buffer with cryptographically random bytes.
620 * hProv [I] Handle of a CSP.
621 * dwLen [I] Number of bytes to generate.
622 * pbBuffer [I/O] Buffer to contain random bytes.
629 * pdBuffer must be at least dwLen bytes long.
631 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
633 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
635 DPRINT("(0x%lx, %ld, %p)\n", hProv
, dwLen
, pbBuffer
);
638 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
640 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
643 /******************************************************************************
644 * CryptCreateHash (ADVAPI32.@)
646 * Initiates the hashing of a stream of data.
649 * hProv [I] Handle of a CSP.
650 * Algid [I] Identifies the hash algorithm to use.
651 * hKey [I] Key for the hash (if required).
652 * dwFlags [I] Reserved for future use and must be NULL.
653 * phHash [O] Address of the future handle to the new hash object.
660 * If the algorithm is a keyed hash, hKey is the key.
662 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
663 DWORD dwFlags
, HCRYPTHASH
*phHash
)
665 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
666 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
669 DPRINT("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
672 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
674 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
676 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
677 if (!(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))))
678 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
680 hash
->pProvider
= prov
;
682 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
683 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
685 *phHash
= (HCRYPTHASH
)hash
;
695 /******************************************************************************
696 * CryptDecrypt (ADVAPI32.@)
698 * Decrypts data encrypted by CryptEncrypt.
701 * hKey [I] Handle to the decryption key.
702 * hHash [I] Handle to a hash object.
703 * Final [I] TRUE if this is the last section to be decrypted.
704 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
705 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
707 * pdwDataLen [I/O] Length of pbData before and after the call.
713 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
714 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
717 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
718 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
720 DPRINT("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
722 if (!key
|| !pbData
|| !pdwDataLen
)
723 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
725 prov
= key
->pProvider
;
726 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
727 Final
, dwFlags
, pbData
, pdwDataLen
);
730 /******************************************************************************
731 * CryptDeriveKey (ADVAPI32.@)
733 * Generates session keys derived from a base data value.
736 * hProv [I] Handle to a CSP.
737 * Algid [I] Identifies the symmetric encryption algorithm to use.
738 * hBaseData [I] Handle to a hash object.
739 * dwFlags [I] Type of key to generate.
740 * phKey [I/O] Address of the newly generated key.
746 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
747 DWORD dwFlags
, HCRYPTKEY
*phKey
)
749 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
750 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
753 DPRINT("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
756 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
758 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
759 if (!(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))))
760 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
762 key
->pProvider
= prov
;
763 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
765 *phKey
= (HCRYPTKEY
)key
;
775 /******************************************************************************
776 * CryptDestroyHash (ADVAPI32.@)
778 * Destroys the hash object referenced by hHash.
781 * hHash [I] Handle of the hash object to be destroyed.
787 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
789 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
793 DPRINT("(0x%lx)\n", hHash
);
796 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
798 prov
= hash
->pProvider
;
799 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
804 /******************************************************************************
805 * CryptDestroyKey (ADVAPI32.@)
807 * Releases the handle referenced by hKey.
810 * hKey [I] Handle of the key to be destroyed.
816 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
818 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
822 DPRINT("(0x%lx)\n", hKey
);
825 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
827 prov
= key
->pProvider
;
828 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
833 /******************************************************************************
834 * CryptDuplicateHash (ADVAPI32.@)
839 * hHash [I] Handle to the hash to be copied.
840 * pdwReserved [I] Reserved for future use and must be zero.
841 * dwFlags [I] Reserved for future use and must be zero.
842 * phHash [O] Address of the handle to receive the copy.
848 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
849 DWORD dwFlags
, HCRYPTHASH
*phHash
)
852 PCRYPTHASH orghash
, newhash
;
854 DPRINT("(0x%lx, %p, %08ld, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
856 orghash
= (PCRYPTHASH
)hHash
;
857 if (!orghash
|| pdwReserved
|| !phHash
)
858 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
860 prov
= orghash
->pProvider
;
861 if (!prov
->pFuncs
->pCPDuplicateHash
)
862 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED
);
864 if (!(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))))
865 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
867 newhash
->pProvider
= prov
;
868 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
870 *phHash
= (HCRYPTHASH
)newhash
;
877 /******************************************************************************
878 * CryptDuplicateKey (ADVAPI32.@)
880 * Duplicate a key and the key's state.
883 * hKey [I] Handle of the key to copy.
884 * pdwReserved [I] Reserved for future use and must be NULL.
885 * dwFlags [I] Reserved for future use and must be zero.
886 * phKey [I] Address of the handle to the duplicated key.
892 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
895 PCRYPTKEY orgkey
, newkey
;
897 DPRINT("(0x%lx, %p, %08ld, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
899 orgkey
= (PCRYPTKEY
)hKey
;
900 if (!orgkey
|| pdwReserved
|| !phKey
)
901 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
903 prov
= orgkey
->pProvider
;
904 if (!prov
->pFuncs
->pCPDuplicateKey
)
905 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED
);
907 if (!(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))))
908 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
910 newkey
->pProvider
= prov
;
911 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
913 *phKey
= (HCRYPTKEY
)newkey
;
920 /******************************************************************************
921 * CryptEncrypt (ADVAPI32.@)
926 * hKey [I] Handle to the enryption key.
927 * hHash [I] Handle to a hash object.
928 * Final [I] TRUE if this is the last section to encrypt.
929 * dwFlags [I] Can be CRYPT_OAEP.
930 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
931 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
932 * encrypted data after call.
933 * dwBufLen [I] Length of the input pbData buffer.
940 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
941 * required for the returned data in pdwDataLen.
943 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
944 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
947 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
948 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
950 DPRINT("(0x%lx, 0x%lx, %d, %08ld, %p, %p, %ld)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
952 if (!key
|| !pdwDataLen
)
953 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
955 prov
= key
->pProvider
;
956 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
957 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
960 /******************************************************************************
961 * CryptEnumProvidersW (ADVAPI32.@)
963 * Returns the next availabe CSP.
966 * dwIndex [I] Index of the next provider to be enumerated.
967 * pdwReserved [I] Reserved for future use and must be NULL.
968 * dwFlags [I] Reserved for future use and must be zero.
969 * pdwProvType [O] DWORD designating the type of the provider.
970 * pszProvName [O] Buffer that receives data from the provider.
971 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
972 * of bytes stored in the buffer on return.
979 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
980 * for memory allocation purposes.
982 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
983 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
987 DPRINT("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
988 pdwProvType
, pszProvName
, pcbProvName
);
990 if (pdwReserved
|| !pcbProvName
)
991 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
993 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
995 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
,
996 L
"Software\\Microsoft\\Cryptography\\Defaults\\Provider",
998 CRYPT_ReturnLastError(NTE_FAIL
);
1005 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1006 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1008 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1009 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1011 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1013 *pcbProvName
*= sizeof(WCHAR
);
1015 CRYPT_Free(provNameW
);
1017 if (dwIndex
>= numkeys
)
1018 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS
);
1022 DWORD size
= sizeof(DWORD
);
1026 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1028 CRYPT_ReturnLastError(result
);
1029 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1031 if (RegQueryValueExW(subkey
, L
"Type", NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1033 RegCloseKey(subkey
);
1039 /******************************************************************************
1040 * CryptEnumProvidersA (ADVAPI32.@)
1042 * see CryptEnumProvidersW
1044 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1045 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1049 BOOL ret
; /* = FALSE; */
1051 DPRINT("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1052 pdwProvType
, pszProvName
, pcbProvName
);
1054 strlen
= *pcbProvName
* sizeof(WCHAR
);
1055 if (pszProvName
&& !(str
= CRYPT_Alloc(strlen
)))
1056 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1057 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1060 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1063 *pcbProvName
= strlen
/ sizeof(WCHAR
); /* FIXME: not correct */
1067 /******************************************************************************
1068 * CryptEnumProviderTypesA (ADVAPI32i.@)
1070 * Retrieves the next type of CSP supported.
1073 * dwIndex [I] Index of the next provider to be enumerated.
1074 * pdwReserved [I] Reserved for future use and must be NULL.
1075 * dwFlags [I] Reserved for future use and must be zero.
1076 * pdwProvType [O] DWORD designating the type of the provider.
1077 * pszTypeName [O] Buffer that receives data from the provider type.
1078 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1079 * of bytes stored in the buffer on return.
1086 * If pszTypeName is NULL, CryptEnumProviderTypesA sets the size of the name
1087 * for memory allocation purposes.
1089 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1090 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1093 DWORD keylen
, numkeys
, dwType
;
1097 DPRINT("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1098 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1100 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1101 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1103 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1105 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey
))
1108 RegQueryInfoKeyA(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1109 if (dwIndex
>= numkeys
)
1110 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS
);
1112 if ( !(keyname
= CRYPT_Alloc(keylen
)) )
1113 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1114 if ( RegEnumKeyA(hKey
, dwIndex
, keyname
, keylen
) )
1116 CRYPT_Free(keyname
);
1119 RegOpenKeyA(hKey
, keyname
, &hSubkey
);
1120 ch
= keyname
+ strlen(keyname
);
1121 /* Convert "Type 000" to 0, etc/ */
1122 *pdwProvType
= *(--ch
) - '0';
1123 *pdwProvType
+= (*(--ch
) - '0') * 10;
1124 *pdwProvType
+= (*(--ch
) - '0') * 100;
1125 CRYPT_Free(keyname
);
1127 result
= RegQueryValueExA(hSubkey
, "TypeName", NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1129 CRYPT_ReturnLastError(result
);
1131 RegCloseKey(hSubkey
);
1136 /******************************************************************************
1137 * CryptEnumProviderTypesW (ADVAPI32.@)
1139 * see CryptEnumProviderTypesA
1141 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1142 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1148 DPRINT("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1149 pdwProvType
, pszTypeName
, pcbTypeName
);
1150 strlen
= *pcbTypeName
/ sizeof(WCHAR
);
1151 if (pszTypeName
&& !(str
= CRYPT_Alloc(strlen
)))
1152 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1153 ret
= CryptEnumProviderTypesA(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1156 CRYPT_ANSIToUnicode(str
, &pszTypeName
, *pcbTypeName
);
1159 *pcbTypeName
= strlen
* sizeof(WCHAR
);
1163 /******************************************************************************
1164 * CryptExportKey (ADVAPI32.@)
1166 * Exports a cryptographic key from a CSP.
1169 * hKey [I] Handle to the key to export.
1170 * hExpKey [I] Handle to a cryptographic key of the end user.
1171 * dwBlobType [I] Type of BLOB to be exported.
1172 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1173 * pbData [O] Buffer to receive BLOB data.
1174 * pdwDataLen [I/O] Specifies the size of pbData.
1181 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1182 * buffer needed to hold the BLOB.
1184 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1185 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1188 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1190 DPRINT("(0x%lx, 0x%lx, %ld, %08ld, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1192 if (!key
|| !pdwDataLen
)
1193 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1195 prov
= key
->pProvider
;
1196 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1197 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1200 /******************************************************************************
1201 * CryptGenKey (ADVAPI32.@)
1203 * Generates a random cryptographic session key or a pub/priv key pair.
1206 * hProv [I] Handle to a CSP.
1207 * Algid [I] Algorithm to use to make key.
1208 * dwFlags [I] Specifies type of key to make.
1209 * phKey [I] Address of the handle to which the new key is copied.
1215 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1217 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1220 DPRINT("(0x%lx, %d, %08ld, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1223 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1225 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1226 if (!(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))))
1227 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1229 key
->pProvider
= prov
;
1231 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1233 *phKey
= (HCRYPTKEY
)key
;
1242 /******************************************************************************
1243 * CryptGetDefaultProviderA (ADVAPI32.@)
1245 * Finds the default CSP of a certain provider type.
1248 * dwProvType [I] Provider type to look for.
1249 * pdwReserved [I] Reserved for future use and must be NULL.
1250 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1251 * pszProvName [O] Name of the default CSP.
1252 * pcbProvName [I/O] Size of pszProvName
1259 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1260 * memory allocation purposes on return.
1262 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1263 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1269 if (pdwReserved
|| !pcbProvName
)
1270 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1271 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1272 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1273 if (dwProvType
> 999)
1274 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE
);
1275 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
1276 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1277 if (RegOpenKeyA((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1279 CRYPT_Free(keyname
);
1280 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF
);
1282 CRYPT_Free(keyname
);
1284 result
= RegQueryValueExA(hKey
, "Name", NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1287 if (result
!= ERROR_MORE_DATA
)
1288 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1290 SetLastError(result
);
1299 /******************************************************************************
1300 * CryptGetDefaultProviderW (ADVAPI32.@)
1302 * see CryptGetDefaultProviderA
1304 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1305 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1311 DPRINT("(%ld, %p, %08ld, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1313 strlen
= *pcbProvName
/ sizeof(WCHAR
);
1314 if (pszProvName
&& !(str
= CRYPT_Alloc(strlen
)))
1315 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1316 ret
= CryptGetDefaultProviderA(dwProvType
, pdwReserved
, dwFlags
, str
, &strlen
);
1319 CRYPT_ANSIToUnicode(str
, &pszProvName
, *pcbProvName
);
1322 *pcbProvName
= strlen
* sizeof(WCHAR
);
1326 /******************************************************************************
1327 * CryptGetHashParam (ADVAPI32.@)
1329 * Retrieves data that controls the operations of a hash object.
1332 * hHash [I] Handle of the hash object to question.
1333 * dwParam [I] Query type.
1334 * pbData [O] Buffer that receives the value data.
1335 * pdwDataLen [I/O] Size of the pbData buffer.
1336 * dwFlags [I] Reserved for future use and must be zero.
1343 * If pbData is NULL, pdwDataLen will contain the length required.
1345 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1346 DWORD
*pdwDataLen
, DWORD dwFlags
)
1349 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1351 DPRINT("(0x%lx, %ld, %p, %p, %08ld)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1353 if (!hash
|| !pdwDataLen
)
1354 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1356 prov
= hash
->pProvider
;
1357 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1358 pbData
, pdwDataLen
, dwFlags
);
1361 /******************************************************************************
1362 * CryptGetKeyParam (ADVAPI32.@)
1364 * Retrieves data that controls the operations of a key.
1367 * hKey [I] Handle to they key in question.
1368 * dwParam [I] Specifies query type.
1369 * pbData [O] Sequence of bytes to receive data.
1370 * pdwDataLen [I/O] Size of pbData.
1371 * dwFlags [I] Reserved for future use and must be zero.
1378 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1380 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1381 DWORD
*pdwDataLen
, DWORD dwFlags
)
1384 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1386 DPRINT("(0x%lx, %ld, %p, %p, %08ld)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1388 if (!key
|| !pdwDataLen
)
1389 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1391 prov
= key
->pProvider
;
1392 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1393 pbData
, pdwDataLen
, dwFlags
);
1396 /******************************************************************************
1397 * CryptGetProvParam (ADVAPI32.@)
1399 * Retrieves parameters that control the operations of a CSP.
1402 * hProv [I] Handle of the CSP in question.
1403 * dwParam [I] Specifies query type.
1404 * pbData [O] Buffer to receive the data.
1405 * pdwDataLen [I/O] Size of pbData.
1406 * dwFlags [I] see MSDN Docs.
1413 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1415 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1416 DWORD
*pdwDataLen
, DWORD dwFlags
)
1418 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1420 DPRINT("(0x%lx, %ld, %p, %p, %08ld)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1422 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1425 /******************************************************************************
1426 * CryptGetUserKey (ADVAPI32.@)
1428 * Gets a handle of one of a user's two public/private key pairs.
1431 * hProv [I] Handle of a CSP.
1432 * dwKeySpec [I] Private key to use.
1433 * phUserKey [O] Pointer to the handle of the retrieved keys.
1439 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1441 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1444 DPRINT("(0x%lx, %ld, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1447 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1449 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1450 if (!(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))))
1451 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1453 key
->pProvider
= prov
;
1455 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1457 *phUserKey
= (HCRYPTKEY
)key
;
1467 /******************************************************************************
1468 * CryptHashData (ADVAPI32.@)
1470 * Adds data to a hash object.
1473 * hHash [I] Handle of the hash object.
1474 * pbData [I] Buffer of data to be hashed.
1475 * dwDataLen [I] Number of bytes to add.
1476 * dwFlags [I] Can be CRYPT_USERDATA
1482 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1484 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1487 DPRINT("(0x%lx, %p, %ld, %08ld)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1490 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1491 if (!pbData
|| !dwDataLen
)
1492 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1494 prov
= hash
->pProvider
;
1495 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1498 /******************************************************************************
1499 * CryptHashSessionKey (ADVAPI32.@)
1502 * hHash [I] Handle to the hash object.
1503 * hKey [I] Handle to the key to be hashed.
1504 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1510 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1512 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1513 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1516 DPRINT("(0x%lx, 0x%lx, %08ld)\n", hHash
, hKey
, dwFlags
);
1519 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1521 prov
= hash
->pProvider
;
1522 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1525 /******************************************************************************
1526 * CryptImportKey (ADVAPI32.@)
1529 * hProv [I] Handle of a CSP.
1530 * pbData [I] Contains the key to be imported.
1531 * dwDataLen [I] Length of the key.
1532 * hPubKey [I] Cryptographic key that decrypts pdData
1533 * dwFlags [I] Used only with a public/private key pair.
1534 * phKey [O] Imported key.
1540 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, BYTE
*pbData
, DWORD dwDataLen
,
1541 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1543 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1544 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1546 DPRINT("(0x%lx, %p, %ld, 0x%lx, %08ld, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1548 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
)
1549 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1551 if (!(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))))
1552 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1554 importkey
->pProvider
= prov
;
1555 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1556 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1558 *phKey
= (HCRYPTKEY
)importkey
;
1562 CRYPT_Free(importkey
);
1566 /******************************************************************************
1569 * Note: Since the sDesciption (string) is supposed to be NULL and
1570 * is only retained for compatibility no string conversions are required
1571 * and only one implementation is required for both ANSI and Unicode.
1572 * We still need to export both:
1574 * CryptSignHashA (ADVAPI32.@)
1575 * CryptSignHashW (ADVAPI32.@)
1580 * hHash [I] Handle of the hash object to be signed.
1581 * dwKeySpec [I] Private key to use.
1582 * sDescription [I] Must be NULL.
1583 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1584 * pbSignature [O] Buffer of the signature data.
1585 * pdwSigLen [I/O] Size of the pbSignature buffer.
1591 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1592 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1594 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1597 DPRINT("(0x%lx, %ld, %08ld, %p, %p)\n", hHash
, dwKeySpec
, dwFlags
, pbSignature
, pdwSigLen
);
1600 DPRINT("The sDescription parameter is not supported (and no longer used). Ignoring.\n");
1604 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1606 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1608 prov
= hash
->pProvider
;
1609 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, NULL
,
1610 dwFlags
, pbSignature
, pdwSigLen
);
1613 /******************************************************************************
1614 * CryptSetHashParam (ADVAPI32.@)
1616 * Customizes the operations of a hash object.
1619 * hHash [I] Handle of the hash object to set parameters.
1620 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1621 * pbData [I] Value data buffer.
1622 * dwFlags [I] Reserved for future use and must be zero.
1628 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1631 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1633 DPRINT("(0x%lx, %ld, %p, %08ld)\n", hHash
, dwParam
, pbData
, dwFlags
);
1635 if (!hash
|| !pbData
)
1636 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1638 prov
= hash
->pProvider
;
1639 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1640 dwParam
, pbData
, dwFlags
);
1643 /******************************************************************************
1644 * CryptSetKeyParam (ADVAPI32.@)
1646 * Customizes a session key's operations.
1649 * hKey [I] Handle to the key to set values.
1650 * dwParam [I] See MSDN Doc.
1651 * pbData [I] Buffer of values to set.
1652 * dwFlags [I] Only used when dwParam == KP_ALGID.
1658 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1661 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1663 DPRINT("(0x%lx, %ld, %p, %08ld)\n", hKey
, dwParam
, pbData
, dwFlags
);
1665 if (!key
|| !pbData
)
1666 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1668 prov
= key
->pProvider
;
1669 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
1670 dwParam
, pbData
, dwFlags
);
1673 /******************************************************************************
1674 * CryptSetProviderA (ADVAPI32.@)
1676 * Specifies the current user's default CSP.
1679 * pszProvName [I] Name of the new default CSP.
1680 * dwProvType [I] Provider type of the CSP.
1686 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
1688 DPRINT("(%s, %ld)\n", pszProvName
, dwProvType
);
1689 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1692 /******************************************************************************
1693 * CryptSetProviderW (ADVAPI32.@)
1695 * See CryptSetProviderA
1697 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
1699 DPRINT("(%S, %ld)\n", pszProvName
, dwProvType
);
1700 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1703 /******************************************************************************
1704 * CryptSetProviderExA (ADVAPI32.@)
1706 * Specifies the default CSP.
1709 * pszProvName [I] Name of the new default CSP.
1710 * dwProvType [I] Provider type of the CSP.
1711 * pdwReserved [I] Reserved for future use and must be NULL.
1712 * dwFlags [I] See MSDN Doc.
1718 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
1720 HKEY hProvKey
, hTypeKey
;
1723 DPRINT("(%s, %ld, %p, %08ld)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
1725 if (!pszProvName
|| pdwReserved
)
1726 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1727 if (dwProvType
> MAXPROVTYPES
)
1728 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE
);
1729 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
1730 || dwFlags
== CRYPT_DELETE_DEFAULT
)
1731 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1733 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
1734 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1735 if (RegOpenKeyA((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
1736 keyname
, &hTypeKey
))
1738 CRYPT_Free(keyname
);
1739 CRYPT_ReturnLastError(NTE_BAD_PROVIDER
);
1741 CRYPT_Free(keyname
);
1743 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
1745 RegDeleteValueA(hTypeKey
, "Name");
1749 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
1751 RegCloseKey(hTypeKey
);
1752 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1754 if (RegOpenKeyA((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
1755 keyname
, &hProvKey
))
1757 CRYPT_Free(keyname
);
1758 RegCloseKey(hTypeKey
);
1759 CRYPT_ReturnLastError(NTE_BAD_PROVIDER
);
1761 CRYPT_Free(keyname
);
1763 if (RegSetValueExA(hTypeKey
, "Name", 0, REG_SZ
, (LPBYTE
)pszProvName
, strlen(pszProvName
) + 1))
1765 RegCloseKey(hTypeKey
);
1766 RegCloseKey(hProvKey
);
1770 RegCloseKey(hProvKey
);
1772 RegCloseKey(hTypeKey
);
1777 /******************************************************************************
1778 * CryptSetProviderExW (ADVAPI32.@)
1780 * See CryptSetProviderExA
1782 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
1787 DPRINT("(%S, %ld, %p, %08ld)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
1789 if (CRYPT_UnicodeToANSI(pszProvName
, &str
, -1))
1791 ret
= CryptSetProviderExA(str
, dwProvType
, pdwReserved
, dwFlags
);
1797 /******************************************************************************
1798 * CryptSetProvParam (ADVAPI32.@)
1800 * Customizes the operations of a CSP.
1803 * hProv [I] Handle of a CSP.
1804 * dwParam [I] See MSDN Doc.
1805 * pbData [I] Buffer that contains a value to set as a parameter.
1806 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1812 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1814 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1816 DPRINT("(0x%lx, %ld, %p, %08ld)\n", hProv
, dwParam
, pbData
, dwFlags
);
1819 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1820 if (dwFlags
& PP_USE_HARDWARE_RNG
)
1822 DPRINT1("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1823 DPRINT1("\tLetting the CSP decide.\n");
1825 if (dwFlags
& PP_CLIENT_HWND
)
1827 /* FIXME: Should verify the parameter */
1828 if (pbData
/* && IsWindow((HWND)pbData) */)
1830 crypt_hWindow
= (HWND
)(pbData
);
1833 SetLastError(ERROR_INVALID_PARAMETER
);
1837 /* All other flags go to the CSP */
1838 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
1841 /******************************************************************************
1842 * CryptVerifySignatureA
1844 * Note: Since the sDesciption (string) is supposed to be NULL and
1845 * is only retained for compatibility no string conversions are required
1846 * and only one implementation is required for both ANSI and Unicode.
1847 * We still need to export both:
1849 * CryptVerifySignatureA (ADVAPI32.@)
1850 * CryptVerifySignatureW (ADVAPI32.@)
1852 * Verifies the signature of a hash object.
1855 * hHash [I] Handle of the hash object to verify.
1856 * pbSignature [I] Signature data to verify.
1857 * dwSigLen [I] Size of pbSignature.
1858 * hPubKey [I] Handle to the public key to authenticate signature.
1859 * sDescription [I] Must be set to NULL.
1860 * dwFlags [I] See MSDN doc.
1866 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, BYTE
*pbSignature
, DWORD dwSigLen
,
1867 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
1869 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1870 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
1873 DPRINT("(0x%lx, %p, %ld, 0x%lx, %08ld)\n", hHash
, pbSignature
,
1874 dwSigLen
, hPubKey
, dwFlags
);
1877 DPRINT("The sDescription parameter is not supported (and no longer used). Ignoring.\n");
1881 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1882 if (!pbSignature
|| !dwSigLen
)
1883 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1885 prov
= hash
->pProvider
;
1886 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
1887 key
->hPrivate
, NULL
, dwFlags
);
1891 CryptVerifySignatureW (HCRYPTHASH hHash
,
1895 LPCWSTR sDescription
,
1898 OutputDebugStringA ("ADVAPI32!CryptVerifySignatureW not implemented!");
1903 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
1907 /******************************************************************************
1908 * SystemFunction040 (ADVAPI32.@)
1911 * memory : pointer to memory to encrypt
1912 * length : length of region to encrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE
1913 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON
1914 * control whether other processes are able to decrypt the memory. The same value must be given
1915 * when decrypting the memory.
1917 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
) /* RtlEncryptMemory */
1919 DPRINT1("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
1920 return STATUS_SUCCESS
;
1923 /******************************************************************************
1924 * SystemFunction041 (ADVAPI32.@)
1927 * memory : pointer to memory to decrypt
1928 * length : length of region to decrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE
1929 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON
1930 * control whether other processes are able to decrypt the memory. The same value must be given
1931 * when encrypting the memory.
1933 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
) /* RtlDecryptMemory */
1935 DPRINT1("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
1936 return STATUS_SUCCESS
;