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
32 #include "wine/unicode.h"
41 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
47 HWND crypt_hWindow
= 0;
49 #define CRYPT_ReturnLastError(err) {SetLastError(err); return FALSE;}
51 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
52 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
54 static inline PSTR
CRYPT_GetProvKeyName(PCSTR pProvName
)
56 PCSTR KEYSTR
= "Software\\Microsoft\\Cryptography\\Defaults\\Provider\\";
59 keyname
= CRYPT_Alloc(strlen(KEYSTR
) + strlen(pProvName
) +1);
62 strcpy(keyname
, KEYSTR
);
63 strcpy(keyname
+ strlen(KEYSTR
), pProvName
);
65 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
69 static inline PSTR
CRYPT_GetTypeKeyName(DWORD dwType
, BOOL user
)
71 PCSTR MACHINESTR
= "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type XXX";
72 PCSTR USERSTR
= "Software\\Microsoft\\Cryptography\\Provider Type XXX";
76 keyname
= CRYPT_Alloc( (user
? strlen(USERSTR
) : strlen(MACHINESTR
)) +1);
79 user
? strcpy(keyname
, USERSTR
) : strcpy(keyname
, MACHINESTR
);
80 ptr
= keyname
+ strlen(keyname
);
81 *(--ptr
) = (dwType
% 10) + '0';
82 *(--ptr
) = (dwType
/ 10) + '0';
83 *(--ptr
) = (dwType
/ 100) + '0';
85 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
89 /* CRYPT_UnicodeTOANSI
90 * wstr - unicode string
91 * str - pointer to ANSI string
92 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
94 * returns TRUE if unsuccessfull, FALSE otherwise.
95 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
97 static inline BOOL
CRYPT_UnicodeToANSI(LPCWSTR wstr
, LPSTR
* str
, int strsize
)
106 count
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
107 count
= count
< strsize
? count
: strsize
;
109 *str
= CRYPT_Alloc(count
* sizeof(CHAR
));
112 WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, *str
, count
, NULL
, NULL
);
115 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
119 /* CRYPT_ANSITOUnicode
121 * wstr - pointer to unicode string
122 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
124 static inline BOOL
CRYPT_ANSIToUnicode(LPCSTR str
, LPWSTR
* wstr
, int wstrsize
)
133 wcount
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
134 wcount
= wcount
< wstrsize
/sizeof(WCHAR
) ? wcount
: wstrsize
/sizeof(WCHAR
);
136 *wstr
= CRYPT_Alloc(wcount
* sizeof(WCHAR
));
139 MultiByteToWideChar(CP_ACP
, 0, str
, -1, *wstr
, wcount
);
142 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
146 /* These next 2 functions are used by the VTableProvStruc structure */
147 static BOOL CALLBACK
CRYPT_VerifyImage(LPCSTR lpszImage
, BYTE
* pData
)
149 if (!lpszImage
|| !pData
)
151 SetLastError(ERROR_INVALID_PARAMETER
);
155 FIXME("(%s, %p): not verifying image\n", lpszImage
, pData
);
160 static BOOL CALLBACK
CRYPT_ReturnhWnd(HWND
*phWnd
)
164 *phWnd
= crypt_hWindow
;
168 #define CRYPT_GetProvFunc(name) \
169 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
170 #define CRYPT_GetProvFuncOpt(name) \
171 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
172 PCRYPTPROV
CRYPT_LoadProvider(PSTR pImage
)
175 DWORD errorcode
= ERROR_NOT_ENOUGH_MEMORY
;
177 if ( !(provider
= CRYPT_Alloc(sizeof(CRYPTPROV
))) ) goto error
;
178 if ( !(provider
->pFuncs
= CRYPT_Alloc(sizeof(PROVFUNCS
))) ) goto error
;
179 if ( !(provider
->pVTable
= CRYPT_Alloc(sizeof(VTableProvStruc
))) ) goto error
;
180 if ( !(provider
->hModule
= LoadLibraryA(pImage
)) )
182 errorcode
= (GetLastError() == ERROR_FILE_NOT_FOUND
) ? NTE_PROV_DLL_NOT_FOUND
: NTE_PROVIDER_DLL_FAIL
;
183 FIXME("Failed to load dll %s\n", debugstr_a(pImage
));
186 provider
->refcount
= 1;
188 errorcode
= NTE_PROVIDER_DLL_FAIL
;
189 CRYPT_GetProvFunc(CPAcquireContext
);
190 CRYPT_GetProvFunc(CPCreateHash
);
191 CRYPT_GetProvFunc(CPDecrypt
);
192 CRYPT_GetProvFunc(CPDeriveKey
);
193 CRYPT_GetProvFunc(CPDestroyHash
);
194 CRYPT_GetProvFunc(CPDestroyKey
);
195 CRYPT_GetProvFuncOpt(CPDuplicateHash
);
196 CRYPT_GetProvFuncOpt(CPDuplicateKey
);
197 CRYPT_GetProvFunc(CPEncrypt
);
198 CRYPT_GetProvFunc(CPExportKey
);
199 CRYPT_GetProvFunc(CPGenKey
);
200 CRYPT_GetProvFunc(CPGenRandom
);
201 CRYPT_GetProvFunc(CPGetHashParam
);
202 CRYPT_GetProvFunc(CPGetKeyParam
);
203 CRYPT_GetProvFunc(CPGetProvParam
);
204 CRYPT_GetProvFunc(CPGetUserKey
);
205 CRYPT_GetProvFunc(CPHashData
);
206 CRYPT_GetProvFunc(CPHashSessionKey
);
207 CRYPT_GetProvFunc(CPImportKey
);
208 CRYPT_GetProvFunc(CPReleaseContext
);
209 CRYPT_GetProvFunc(CPSetHashParam
);
210 CRYPT_GetProvFunc(CPSetKeyParam
);
211 CRYPT_GetProvFunc(CPSetProvParam
);
212 CRYPT_GetProvFunc(CPSignHash
);
213 CRYPT_GetProvFunc(CPVerifySignature
);
215 /* FIXME: Not sure what the pbContextInfo field is for.
216 * Does it need memory allocation?
218 provider
->pVTable
->Version
= 3;
219 provider
->pVTable
->pFuncVerifyImage
= (FARPROC
)CRYPT_VerifyImage
;
220 provider
->pVTable
->pFuncReturnhWnd
= (FARPROC
)CRYPT_ReturnhWnd
;
221 provider
->pVTable
->dwProvType
= 0;
222 provider
->pVTable
->pbContextInfo
= NULL
;
223 provider
->pVTable
->cbContextInfo
= 0;
224 provider
->pVTable
->pszProvName
= NULL
;
228 SetLastError(errorcode
);
231 if (provider
->hModule
)
232 FreeLibrary(provider
->hModule
);
233 CRYPT_Free(provider
->pVTable
);
234 CRYPT_Free(provider
->pFuncs
);
235 CRYPT_Free(provider
);
239 #undef CRYPT_GetProvFunc
240 #undef CRYPT_GetProvFuncOpt
243 /******************************************************************************
244 * CryptAcquireContextA (ADVAPI32.@)
245 * Acquire a crypto provider context handle.
248 * phProv: Pointer to HCRYPTPROV for the output.
249 * pszContainer: Key Container Name
250 * pszProvider: Cryptographic Service Provider Name
251 * dwProvType: Crypto provider type to get a handle.
252 * dwFlags: flags for the operation
254 * RETURNS TRUE on success, FALSE on failure.
256 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
257 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
259 PCRYPTPROV pProv
= NULL
;
261 PSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
263 DWORD keytype
, type
, len
;
266 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv
, pszContainer
,
267 pszProvider
, dwProvType
, dwFlags
);
269 if (!phProv
|| !dwProvType
)
271 SetLastError(ERROR_INVALID_PARAMETER
);
274 if (dwProvType
> MAXPROVTYPES
)
276 SetLastError(NTE_BAD_PROV_TYPE
);
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
)) ) {
286 FIXME("No provider registered for crypto provider type %ld.\n", dwProvType
);
287 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
290 if (RegOpenKeyA(HKEY_CURRENT_USER
, keyname
, &key
))
293 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
294 FIXME("No type registered for crypto provider type %ld.\n", dwProvType
);
296 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
299 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
300 FIXME("Did not find registry entry of crypto provider for %s.\n", debugstr_a(keyname
));
303 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
308 r
= RegQueryValueExA(key
, "Name", NULL
, &keytype
, NULL
, &len
);
309 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
311 TRACE("error %ld reading size of 'Name' from registry\n", r
);
313 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
316 if(!(provname
= CRYPT_Alloc(len
)))
319 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
322 r
= RegQueryValueExA(key
, "Name", NULL
, NULL
, provname
, &len
);
323 if( r
!= ERROR_SUCCESS
)
325 TRACE("error %ld reading 'Name' from registry\n", r
);
327 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
332 if ( !(provname
= CRYPT_Alloc(strlen(pszProvider
) +1)) )
334 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
337 strcpy(provname
, pszProvider
);
340 keyname
= CRYPT_GetProvKeyName(provname
);
341 r
= RegOpenKeyA(HKEY_LOCAL_MACHINE
, keyname
, &key
);
345 r
= RegQueryValueExA(key
, "Type", NULL
, NULL
, (BYTE
*)&type
, &len
);
346 if (r
!= ERROR_SUCCESS
|| type
!= dwProvType
)
348 FIXME("Crypto provider has wrong type (%ld vs expected %ld).\n", type
, dwProvType
);
349 SetLastError(NTE_BAD_PROV_TYPE
);
353 r
= RegQueryValueExA(key
, "Image Path", NULL
, &keytype
, NULL
, &len
);
354 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
356 TRACE("error %ld reading size of 'Image Path' from registry\n", r
);
358 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
361 if (!(temp
= CRYPT_Alloc(len
)))
364 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
367 r
= RegQueryValueExA(key
, "Image Path", NULL
, NULL
, temp
, &len
);
368 if( r
!= ERROR_SUCCESS
)
370 TRACE("error %ld reading 'Image Path' from registry\n", r
);
372 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
376 r
= RegQueryValueExA(key
, "Signature", NULL
, &keytype
, NULL
, &len
);
377 if ( r
== ERROR_SUCCESS
&& keytype
== REG_BINARY
)
379 if (!(signature
= CRYPT_Alloc(len
)))
382 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
385 r
= RegQueryValueExA(key
, "Signature", NULL
, NULL
, signature
, &len
);
386 if ( r
!= ERROR_SUCCESS
)
388 TRACE("error %ld reading 'Signature'\n", r
);
390 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
396 r
= RegQueryValueExA(key
, "SigInFile", NULL
, &keytype
, NULL
, &len
);
397 if (r
!= ERROR_SUCCESS
)
399 TRACE("error %ld reading size of 'SigInFile'\n", r
);
401 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
406 /* FIXME: The presence of the SigInFile value indicates the
407 * provider's signature is in its resources, so need to read it.
408 * But since CRYPT_VerifyImage is stubbed, provide any old thing
411 if (!(signature
= CRYPT_Alloc(1)))
414 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
420 len
= ExpandEnvironmentStringsA(temp
, NULL
, 0);
421 if ( !(imagepath
= CRYPT_Alloc(len
)) )
423 CRYPT_Free(signature
);
424 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
427 if (!ExpandEnvironmentStringsA(temp
, imagepath
, len
))
429 CRYPT_Free(signature
);
430 /* ExpandEnvironmentStrings will call SetLastError */
434 if (!CRYPT_VerifyImage(imagepath
, signature
))
436 CRYPT_Free(signature
);
437 SetLastError(NTE_SIGNATURE_FILE_BAD
);
440 pProv
= CRYPT_LoadProvider(imagepath
);
442 CRYPT_Free(signature
);
444 /* CRYPT_LoadProvider calls SetLastError */
447 pProv
->pVTable
->dwProvType
= dwProvType
;
448 pProv
->pVTable
->pszProvName
= provname
;
449 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, (CHAR
*)pszContainer
, dwFlags
, pProv
->pVTable
))
451 /* MSDN: When this flag is set, the value returned in phProv is undefined,
452 * and thus, the CryptReleaseContext function need not be called afterwards.
453 * Therefore, we must clean up everything now.
455 if (dwFlags
& CRYPT_DELETEKEYSET
)
457 FreeLibrary(pProv
->hModule
);
458 CRYPT_Free(provname
);
459 CRYPT_Free(pProv
->pFuncs
);
462 *phProv
= (HCRYPTPROV
)pProv
;
466 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
470 FreeLibrary(pProv
->hModule
);
471 CRYPT_Free(pProv
->pVTable
);
472 CRYPT_Free(pProv
->pFuncs
);
475 CRYPT_Free(provname
);
477 CRYPT_Free(imagepath
);
481 /******************************************************************************
482 * CryptAcquireContextW (ADVAPI32.@)
484 * see CryptAcquireContextA
486 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
487 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
489 PSTR pProvider
= NULL
, pContainer
= NULL
;
492 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv
, debugstr_w(pszContainer
),
493 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
495 if ( !CRYPT_UnicodeToANSI(pszContainer
, &pContainer
, -1) )
496 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
497 if ( !CRYPT_UnicodeToANSI(pszProvider
, &pProvider
, -1) )
499 CRYPT_Free(pContainer
);
500 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
503 ret
= CryptAcquireContextA(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
506 CRYPT_Free(pContainer
);
508 CRYPT_Free(pProvider
);
513 /******************************************************************************
514 * CryptContextAddRef (ADVAPI32.@)
516 * Increases reference count of a cryptographic service provider handle
520 * hProv [I] Handle to the CSP whose reference is being incremented.
521 * pdwReserved [IN] Reserved for future use and must be NULL.
522 * dwFlags [I] Reserved for future use and must be NULL.
528 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
530 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
532 TRACE("(0x%lx, %p, %08lx)\n", hProv
, pdwReserved
, dwFlags
);
536 SetLastError(NTE_BAD_UID
);
544 /******************************************************************************
545 * CryptReleaseContext (ADVAPI32.@)
547 * Releases the handle of a CSP. Reference count is decreased.
550 * hProv [I] Handle of a CSP.
551 * dwFlags [I] Reserved for future use and must be NULL.
557 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, DWORD dwFlags
)
559 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
562 TRACE("(0x%lx, %08ld)\n", hProv
, dwFlags
);
566 SetLastError(NTE_BAD_UID
);
571 if (pProv
->refcount
<= 0)
573 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
574 FreeLibrary(pProv
->hModule
);
576 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
578 CRYPT_Free(pProv
->pVTable
->pszProvName
);
579 CRYPT_Free(pProv
->pVTable
);
580 CRYPT_Free(pProv
->pFuncs
);
586 /******************************************************************************
587 * CryptGenRandom (ADVAPI32.@)
589 * Fills a buffer with cryptographically random bytes.
592 * hProv [I] Handle of a CSP.
593 * dwLen [I] Number of bytes to generate.
594 * pbBuffer [I/O] Buffer to contain random bytes.
601 * pdBuffer must be at least dwLen bytes long.
603 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
605 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
607 TRACE("(0x%lx, %ld, %p)\n", hProv
, dwLen
, pbBuffer
);
610 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
612 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
615 /******************************************************************************
616 * CryptCreateHash (ADVAPI32.@)
618 * Initiates the hashing of a stream of data.
621 * hProv [I] Handle of a CSP.
622 * Algid [I] Identifies the hash algorithm to use.
623 * hKey [I] Key for the hash (if required).
624 * dwFlags [I] Reserved for future use and must be NULL.
625 * phHash [O] Address of the future handle to the new hash object.
632 * If the algorithm is a keyed hash, hKey is the key.
634 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
635 DWORD dwFlags
, HCRYPTHASH
*phHash
)
637 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
638 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
641 TRACE("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
644 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
646 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
648 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
649 if ( !(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
650 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
652 hash
->pProvider
= prov
;
654 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
655 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
657 *phHash
= (HCRYPTHASH
)hash
;
666 /******************************************************************************
667 * CryptDecrypt (ADVAPI32.@)
669 * Decrypts data encrypted by CryptEncrypt.
672 * hKey [I] Handle to the decryption key.
673 * hHash [I] Handle to a hash object.
674 * Final [I] TRUE if this is the last section to be decrypted.
675 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
676 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
678 * pdwDataLen [I/O] Length of pbData before and after the call.
684 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
685 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
688 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
689 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
691 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
693 if (!key
|| !pbData
|| !pdwDataLen
)
694 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
696 prov
= key
->pProvider
;
697 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
698 Final
, dwFlags
, pbData
, pdwDataLen
);
701 /******************************************************************************
702 * CryptDeriveKey (ADVAPI32.@)
704 * Generates session keys derived from a base data value.
707 * hProv [I] Handle to a CSP.
708 * Algid [I] Identifies the symmetric encryption algorithm to use.
709 * hBaseData [I] Handle to a hash object.
710 * dwFlags [I] Type of key to generate.
711 * phKey [I/O] Address of the newly generated key.
717 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
718 DWORD dwFlags
, HCRYPTKEY
*phKey
)
720 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
721 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
724 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
727 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
729 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
730 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
731 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
733 key
->pProvider
= prov
;
734 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
736 *phKey
= (HCRYPTKEY
)key
;
745 /******************************************************************************
746 * CryptDestroyHash (ADVAPI32.@)
748 * Destroys the hash object referenced by hHash.
751 * hHash [I] Handle of the hash object to be destroyed.
757 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
759 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
763 TRACE("(0x%lx)\n", hHash
);
766 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
768 prov
= hash
->pProvider
;
769 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
774 /******************************************************************************
775 * CryptDestroyKey (ADVAPI32.@)
777 * Releases the handle referenced by hKey.
780 * hKey [I] Handle of the key to be destroyed.
786 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
788 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
792 TRACE("(0x%lx)\n", hKey
);
795 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
797 prov
= key
->pProvider
;
798 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
803 /******************************************************************************
804 * CryptDuplicateHash (ADVAPI32.@)
809 * hHash [I] Handle to the hash to be copied.
810 * pdwReserved [I] Reserved for future use and must be zero.
811 * dwFlags [I] Reserved for future use and must be zero.
812 * phHash [O] Address of the handle to receive the copy.
818 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
819 DWORD dwFlags
, HCRYPTHASH
*phHash
)
822 PCRYPTHASH orghash
, newhash
;
824 TRACE("(0x%lx, %p, %08ld, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
826 orghash
= (PCRYPTHASH
)hHash
;
827 if (!orghash
|| pdwReserved
|| !phHash
)
828 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
830 prov
= orghash
->pProvider
;
831 if (!prov
->pFuncs
->pCPDuplicateHash
)
832 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED
);
834 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
835 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
837 newhash
->pProvider
= prov
;
838 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
840 *phHash
= (HCRYPTHASH
)newhash
;
847 /******************************************************************************
848 * CryptDuplicateKey (ADVAPI32.@)
850 * Duplicate a key and the key's state.
853 * hKey [I] Handle of the key to copy.
854 * pdwReserved [I] Reserved for future use and must be NULL.
855 * dwFlags [I] Reserved for future use and must be zero.
856 * phKey [I] Address of the handle to the duplicated key.
862 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
865 PCRYPTKEY orgkey
, newkey
;
867 TRACE("(0x%lx, %p, %08ld, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
869 orgkey
= (PCRYPTKEY
)hKey
;
870 if (!orgkey
|| pdwReserved
|| !phKey
)
871 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
873 prov
= orgkey
->pProvider
;
874 if (!prov
->pFuncs
->pCPDuplicateKey
)
875 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED
);
877 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
878 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
880 newkey
->pProvider
= prov
;
881 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
883 *phKey
= (HCRYPTKEY
)newkey
;
890 /******************************************************************************
891 * CryptEncrypt (ADVAPI32.@)
896 * hKey [I] Handle to the enryption key.
897 * hHash [I] Handle to a hash object.
898 * Final [I] TRUE if this is the last section to encrypt.
899 * dwFlags [I] Can be CRYPT_OAEP.
900 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
901 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
902 * encrypted data after call.
903 * dwBufLen [I] Length of the input pbData buffer.
910 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
911 * required for the returned data in pdwDataLen.
913 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
914 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
917 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
918 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
920 TRACE("(0x%lx, 0x%lx, %d, %08ld, %p, %p, %ld)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
922 if (!key
|| !pdwDataLen
)
923 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
925 prov
= key
->pProvider
;
926 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
927 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
930 /******************************************************************************
931 * CryptEnumProvidersA (ADVAPI32.@)
933 * Returns the next availabe CPS.
936 * dwIndex [I] Index of the next provider to be enumerated.
937 * pdwReserved [I] Reserved for future use and must be NULL.
938 * dwFlags [I] Reserved for future use and must be zero.
939 * pdwProvType [O] DWORD designating the type of the provider.
940 * pszProvName [O] Buffer that receives data from the provider.
941 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
942 * of bytes stored in the buffer no return.
949 * If pszProvName is NULL, CryptEnumProvidersA sets the size of the name
950 * for memory allocation purposes.
952 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
953 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
957 TRACE("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
958 pdwProvType
, pszProvName
, pcbProvName
);
960 if (pdwReserved
|| !pcbProvName
) CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
961 if (dwFlags
) CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
963 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider", &hKey
))
964 CRYPT_ReturnLastError(NTE_FAIL
);
969 RegQueryInfoKeyA(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
971 if (dwIndex
>= numkeys
)
972 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS
);
974 DWORD size
= sizeof(DWORD
);
976 if (RegEnumKeyA(hKey
, dwIndex
, pszProvName
, *pcbProvName
))
978 if (RegOpenKeyA(hKey
, pszProvName
, &subkey
))
980 if (RegQueryValueExA(subkey
, "Type", NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
988 /******************************************************************************
989 * CryptEnumProvidersW (ADVAPI32.@)
991 * see CryptEnumProvidersA
993 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
994 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
998 BOOL ret
; /* = FALSE; */
1000 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1001 pdwProvType
, pszProvName
, pcbProvName
);
1003 strlen
= *pcbProvName
/ sizeof(WCHAR
);
1004 if ( pszProvName
&& (str
= CRYPT_Alloc(strlen
)) )
1005 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1006 ret
= CryptEnumProvidersA(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1009 CRYPT_ANSIToUnicode(str
, &pszProvName
, *pcbProvName
);
1012 *pcbProvName
= strlen
* sizeof(WCHAR
);
1016 /******************************************************************************
1017 * CryptEnumProviderTypesA (ADVAPI32i.@)
1019 * Retrieves the next type of CSP supported.
1022 * dwIndex [I] Index of the next provider to be enumerated.
1023 * pdwReserved [I] Reserved for future use and must be NULL.
1024 * dwFlags [I] Reserved for future use and must be zero.
1025 * pdwProvType [O] DWORD designating the type of the provider.
1026 * pszTypeName [O] Buffer that receives data from the provider type.
1027 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1028 * of bytes stored in the buffer no return.
1035 * If pszTypeName is NULL, CryptEnumProviderTypesA sets the size of the name
1036 * for memory allocation purposes.
1038 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1039 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1042 DWORD keylen
, numkeys
, dwType
;
1045 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1046 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1048 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1049 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1050 if (dwFlags
) CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1052 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Cryptography\\Defaults\\Provider Types", &hKey
))
1055 RegQueryInfoKeyA(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1056 if (dwIndex
>= numkeys
)
1057 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS
);
1059 if ( !(keyname
= CRYPT_Alloc(keylen
)) )
1060 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1061 if ( RegEnumKeyA(hKey
, dwIndex
, keyname
, keylen
) ) {
1062 CRYPT_Free(keyname
);
1065 RegOpenKeyA(hKey
, keyname
, &hSubkey
);
1066 ch
= keyname
+ strlen(keyname
);
1067 /* Convert "Type 000" to 0, etc/ */
1068 *pdwProvType
= *(--ch
) - '0';
1069 *pdwProvType
+= (*(--ch
) - '0') * 10;
1070 *pdwProvType
+= (*(--ch
) - '0') * 100;
1071 CRYPT_Free(keyname
);
1072 RegQueryValueExA(hSubkey
, "TypeName", NULL
, &dwType
, pszTypeName
, pcbTypeName
);
1073 RegCloseKey(hSubkey
);
1078 /******************************************************************************
1079 * CryptEnumProviderTypesW (ADVAPI32.@)
1081 * see CryptEnumProviderTypesA
1083 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1084 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1090 TRACE("(%ld, %p, %08ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1091 pdwProvType
, pszTypeName
, pcbTypeName
);
1092 strlen
= *pcbTypeName
/ sizeof(WCHAR
);
1093 if ( pszTypeName
&& (str
= CRYPT_Alloc(strlen
)) )
1094 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1095 ret
= CryptEnumProviderTypesA(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1098 CRYPT_ANSIToUnicode(str
, &pszTypeName
, *pcbTypeName
);
1101 *pcbTypeName
= strlen
* sizeof(WCHAR
);
1105 /******************************************************************************
1106 * CryptExportKey (ADVAPI32.@)
1108 * Exports a cryptographic key from a CSP.
1111 * hKey [I] Handle to the key to export.
1112 * hExpKey [I] Handle to a cryptographic key of the end user.
1113 * dwBlobType [I] Type of BLOB to be exported.
1114 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1115 * pbData [O] Buffer to receive BLOB data.
1116 * pdwDataLen [I/O] Specifies the size of pbData.
1123 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1124 * buffer needed to hold the BLOB.
1126 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1127 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1130 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1132 TRACE("(0x%lx, 0x%lx, %ld, %08ld, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1134 if (!key
|| pdwDataLen
)
1135 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1137 prov
= key
->pProvider
;
1138 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1139 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1142 /******************************************************************************
1143 * CryptGenKey (ADVAPI32.@)
1145 * Generates a random cryptographic session key or a pub/priv key pair.
1148 * hProv [I] Handle to a CSP.
1149 * Algid [I] Algorithm to use to make key.
1150 * dwFlags [I] Specifies type of key to make.
1151 * phKey [I] Address of the handle to which the new key is copied.
1157 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1159 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1162 TRACE("(0x%lx, %d, %08ld, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1165 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1167 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1168 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1169 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1171 key
->pProvider
= prov
;
1173 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1175 *phKey
= (HCRYPTKEY
)key
;
1184 /******************************************************************************
1185 * CryptGetDefaultProviderA (ADVAPI32.@)
1187 * Finds the default CSP of a certain provider type.
1190 * dwProvType [I] Provider type to look for.
1191 * pdwReserved [I] Reserved for future use and must be NULL.
1192 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1193 * pszProvName [O] Name of the default CSP.
1194 * pcbProvName [I/O] Size of pszProvName
1201 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1202 * memory allocation purposes on return.
1204 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1205 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1210 if (pdwReserved
|| !pcbProvName
)
1211 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1212 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1213 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1214 if (dwProvType
> 999)
1215 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE
);
1216 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1217 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1218 if (RegOpenKeyA((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1220 CRYPT_Free(keyname
);
1221 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF
);
1223 CRYPT_Free(keyname
);
1224 if (RegQueryValueExA(hKey
, "Name", NULL
, NULL
, pszProvName
, pcbProvName
))
1226 if (GetLastError() != ERROR_MORE_DATA
)
1227 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1234 /******************************************************************************
1235 * CryptGetDefaultProviderW (ADVAPI32.@)
1237 * see CryptGetDefaultProviderA
1239 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1240 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1246 TRACE("(%ld, %p, %08ld, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1248 strlen
= *pcbProvName
/ sizeof(WCHAR
);
1249 if ( pszProvName
&& !(str
= CRYPT_Alloc(strlen
)) )
1250 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1251 ret
= CryptGetDefaultProviderA(dwProvType
, pdwReserved
, dwFlags
, str
, &strlen
);
1254 CRYPT_ANSIToUnicode(str
, &pszProvName
, *pcbProvName
);
1257 *pcbProvName
= strlen
* sizeof(WCHAR
);
1261 /******************************************************************************
1262 * CryptGetHashParam (ADVAPI32.@)
1264 * Retrieves data that controls the operations of a hash object.
1267 * hHash [I] Handle of the hash object to question.
1268 * dwParam [I] Query type.
1269 * pbData [O] Buffer that receives the value data.
1270 * pdwDataLen [I/O] Size of the pbData buffer.
1271 * dwFlags [I] Reserved for future use and must be zero.
1278 * If pbData is NULL, pdwDataLen will contain the length required.
1280 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1281 DWORD
*pdwDataLen
, DWORD dwFlags
)
1284 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1286 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1288 if (!hash
|| !pdwDataLen
)
1289 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1291 prov
= hash
->pProvider
;
1292 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1293 pbData
, pdwDataLen
, dwFlags
);
1296 /******************************************************************************
1297 * CryptGetKeyParam (ADVAPI32.@)
1299 * Retrieves data that controls the operations of a key.
1302 * hKey [I] Handle to they key in question.
1303 * dwParam [I] Specifies query type.
1304 * pbData [O] Sequence of bytes to receive data.
1305 * pdwDataLen [I/O] Size of pbData.
1306 * dwFlags [I] Reserved for future use and must be zero.
1313 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1315 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1316 DWORD
*pdwDataLen
, DWORD dwFlags
)
1319 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1321 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1323 if (!key
|| !pdwDataLen
)
1324 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1326 prov
= key
->pProvider
;
1327 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1328 pbData
, pdwDataLen
, dwFlags
);
1331 /******************************************************************************
1332 * CryptGetProvParam (ADVAPI32.@)
1334 * Retrieves parameters that control the operations of a CSP.
1337 * hProv [I] Handle of the CSP in question.
1338 * dwParam [I] Specifies query type.
1339 * pbData [O] Buffer to receive the data.
1340 * pdwDataLen [I/O] Size of pbData.
1341 * dwFlags [I] see MSDN Docs.
1348 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1350 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1351 DWORD
*pdwDataLen
, DWORD dwFlags
)
1353 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1355 TRACE("(0x%lx, %ld, %p, %p, %08ld)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1357 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1360 /******************************************************************************
1361 * CryptGetUserKey (ADVAPI32.@)
1363 * Gets a handle of one of a user's two public/private key pairs.
1366 * hProv [I] Handle of a CSP.
1367 * dwKeySpec [I] Private key to use.
1368 * phUserKey [O] Pointer to the handle of the retrieved keys.
1374 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1376 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1379 TRACE("(0x%lx, %ld, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1382 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1384 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1385 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1386 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1388 key
->pProvider
= prov
;
1390 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1392 *phUserKey
= (HCRYPTKEY
)key
;
1401 /******************************************************************************
1402 * CryptHashData (ADVAPI32.@)
1404 * Adds data to a hash object.
1407 * hHash [I] Handle of the hash object.
1408 * pbData [I] Buffer of data to be hashed.
1409 * dwDataLen [I] Number of bytes to add.
1410 * dwFlags [I] Can be CRYPT_USERDATA
1416 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1418 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1421 TRACE("(0x%lx, %p, %ld, %08ld)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1424 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1425 if (!pbData
|| !dwDataLen
)
1426 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1428 prov
= hash
->pProvider
;
1429 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1432 /******************************************************************************
1433 * CryptHashSessionKey (ADVAPI32.@)
1436 * hHash [I] Handle to the hash object.
1437 * hKey [I] Handle to the key to be hashed.
1438 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1444 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1446 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1447 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1450 TRACE("(0x%lx, 0x%lx, %08ld)\n", hHash
, hKey
, dwFlags
);
1453 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1455 prov
= hash
->pProvider
;
1456 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1459 /******************************************************************************
1460 * CryptImportKey (ADVAPI32.@)
1463 * hProv [I] Handle of a CSP.
1464 * pbData [I] Contains the key to be imported.
1465 * dwDataLen [I] Length of the key.
1466 * hPubKey [I] Cryptographic key that decrypts pdData
1467 * dwFlags [I] Used only with a public/private key pair.
1468 * phKey [O] Imported key.
1474 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, BYTE
*pbData
, DWORD dwDataLen
,
1475 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1477 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1478 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1480 TRACE("(0x%lx, %p, %ld, 0x%lx, %08ld, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1482 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
)
1483 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1485 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1486 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1488 importkey
->pProvider
= prov
;
1489 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1490 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1492 *phKey
= (HCRYPTKEY
)importkey
;
1496 CRYPT_Free(importkey
);
1500 /******************************************************************************
1503 * Note: Since the sDesciption (string) is supposed to be NULL and
1504 * is only retained for compatibility no string conversions are required
1505 * and only one implementation is required for both ANSI and Unicode.
1506 * We still need to export both:
1508 * CryptSignHashA (ADVAPI32.@)
1509 * CryptSignHashW (ADVAPI32.@)
1514 * hHash [I] Handle of the hash object to be signed.
1515 * dwKeySpec [I] Private key to use.
1516 * sDescription [I] Must be NULL.
1517 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1518 * pbSignature [O] Buffer of the signature data.
1519 * pdwSigLen [I/O] Size of the pbSignature buffer.
1525 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1526 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1528 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1531 TRACE("(0x%lx, %ld, %08ld, %p, %p)\n", hHash
, dwKeySpec
, dwFlags
, pbSignature
, pdwSigLen
);
1533 WARN("The sDescription parameter is not supported (and no longer used). Ignoring.\n");
1536 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1538 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1540 prov
= hash
->pProvider
;
1541 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, NULL
,
1542 dwFlags
, pbSignature
, pdwSigLen
);
1545 /******************************************************************************
1546 * CryptSetHashParam (ADVAPI32.@)
1548 * Customizes the operations of a hash object.
1551 * hHash [I] Handle of the hash object to set parameters.
1552 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1553 * pbData [I] Value data buffer.
1554 * dwFlags [I] Reserved for future use and must be zero.
1560 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1563 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1565 TRACE("(0x%lx, %ld, %p, %08ld)\n", hHash
, dwParam
, pbData
, dwFlags
);
1567 if (!hash
|| !pbData
)
1568 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1570 prov
= hash
->pProvider
;
1571 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1572 dwParam
, pbData
, dwFlags
);
1575 /******************************************************************************
1576 * CryptSetKeyParam (ADVAPI32.@)
1578 * Customizes a session key's operations.
1581 * hKey [I] Handle to the key to set values.
1582 * dwParam [I] See MSDN Doc.
1583 * pbData [I] Buffer of values to set.
1584 * dwFlags [I] Only used when dwParam == KP_ALGID.
1590 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1593 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1595 TRACE("(0x%lx, %ld, %p, %08ld)\n", hKey
, dwParam
, pbData
, dwFlags
);
1597 if (!key
|| !pbData
)
1598 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1600 prov
= key
->pProvider
;
1601 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
1602 dwParam
, pbData
, dwFlags
);
1605 /******************************************************************************
1606 * CryptSetProviderA (ADVAPI32.@)
1608 * Specifies the current user's default CSP.
1611 * pszProvName [I] Name of the new default CSP.
1612 * dwProvType [I] Provider type of the CSP.
1618 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
1620 TRACE("(%s, %ld)\n", pszProvName
, dwProvType
);
1621 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1624 /******************************************************************************
1625 * CryptSetProviderW (ADVAPI32.@)
1627 * See CryptSetProviderA
1629 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
1631 TRACE("(%s, %ld)\n", debugstr_w(pszProvName
), dwProvType
);
1632 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1635 /******************************************************************************
1636 * CryptSetProviderExA (ADVAPI32.@)
1638 * Specifies the default CSP.
1641 * pszProvName [I] Name of the new default CSP.
1642 * dwProvType [I] Provider type of the CSP.
1643 * pdwReserved [I] Reserved for future use and must be NULL.
1644 * dwFlags [I] See MSDN Doc.
1650 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
1655 TRACE("(%s, %ld, %p, %08ld)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
1657 if (!pszProvName
|| pdwReserved
)
1658 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1659 if (dwProvType
> MAXPROVTYPES
)
1660 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE
);
1661 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
1662 || dwFlags
== CRYPT_DELETE_DEFAULT
)
1663 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1665 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
1667 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1668 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1669 RegDeleteKeyA( (dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
, keyname
);
1670 CRYPT_Free(keyname
);
1674 if ( !(keyname
= CRYPT_GetProvKeyName(pszProvName
)) )
1675 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1676 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, keyname
, &hKey
))
1678 CRYPT_Free(keyname
);
1679 CRYPT_ReturnLastError(NTE_BAD_PROVIDER
);
1681 CRYPT_Free(keyname
);
1683 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1684 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1685 RegCreateKeyA( (dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
, keyname
, &hKey
);
1686 CRYPT_Free(keyname
);
1687 if (RegSetValueExA(hKey
, "Name", 0, REG_SZ
, pszProvName
, strlen(pszProvName
) +1))
1692 /******************************************************************************
1693 * CryptSetProviderExW (ADVAPI32.@)
1695 * See CryptSetProviderExA
1697 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
1702 TRACE("(%s, %ld, %p, %08ld)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
1704 if (CRYPT_UnicodeToANSI(pszProvName
, &str
, -1))
1706 ret
= CryptSetProviderExA(str
, dwProvType
, pdwReserved
, dwFlags
);
1712 /******************************************************************************
1713 * CryptSetProvParam (ADVAPI32.@)
1715 * Customizes the operations of a CSP.
1718 * hProv [I] Handle of a CSP.
1719 * dwParam [I] See MSDN Doc.
1720 * pbData [I] Buffer that contains a value to set as a parameter.
1721 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1727 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1729 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1731 TRACE("(0x%lx, %ld, %p, %08ld)\n", hProv
, dwParam
, pbData
, dwFlags
);
1734 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1735 if (dwFlags
& PP_USE_HARDWARE_RNG
)
1737 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1738 FIXME("\tLetting the CSP decide.\n");
1740 if (dwFlags
& PP_CLIENT_HWND
)
1742 /* FIXME: Should verify the parameter */
1743 if (pbData
/* && IsWindow((HWND)pbData) */)
1745 crypt_hWindow
= (HWND
)(pbData
);
1748 SetLastError(ERROR_INVALID_PARAMETER
);
1752 /* All other flags go to the CSP */
1753 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
1756 /******************************************************************************
1757 * CryptVerifySignatureA
1759 * Note: Since the sDesciption (string) is supposed to be NULL and
1760 * is only retained for compatibility no string conversions are required
1761 * and only one implementation is required for both ANSI and Unicode.
1762 * We still need to export both:
1764 * CryptVerifySignatureA (ADVAPI32.@)
1765 * CryptVerifySignatureW (ADVAPI32.@)
1767 * Verifies the signature of a hash object.
1770 * hHash [I] Handle of the hash object to verify.
1771 * pbSignature [I] Signature data to verify.
1772 * dwSigLen [I] Size of pbSignature.
1773 * hPubKey [I] Handle to the public key to authenticate signature.
1774 * sDescription [I] Must be set to NULL.
1775 * dwFlags [I] See MSDN doc.
1781 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, BYTE
*pbSignature
, DWORD dwSigLen
,
1782 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
1784 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1785 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
1788 TRACE("(0x%lx, %p, %ld, 0x%lx, %08ld)\n", hHash
, pbSignature
,
1789 dwSigLen
, hPubKey
, dwFlags
);
1791 WARN("The sDescription parameter is not supported (and no longer used). Ignoring.\n");
1794 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1795 if (!pbSignature
|| !dwSigLen
)
1796 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1798 prov
= hash
->pProvider
;
1799 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
1800 key
->hPrivate
, NULL
, dwFlags
);
1805 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
1809 /******************************************************************************
1810 * SystemFunction040 (ADVAPI32.@)
1813 * memory : pointer to memory to encrypt
1814 * length : length of region to encrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE
1815 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON
1816 * control whether other processes are able to decrypt the memory. The same value must be given
1817 * when decrypting the memory.
1819 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
) /* RtlEncryptMemory */
1821 FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
1822 return STATUS_SUCCESS
;
1825 /******************************************************************************
1826 * SystemFunction041 (ADVAPI32.@)
1829 * memory : pointer to memory to decrypt
1830 * length : length of region to decrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE
1831 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON
1832 * control whether other processes are able to decrypt the memory. The same value must be given
1833 * when encrypting the memory.
1835 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
) /* RtlDecryptMemory */
1837 FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
1838 return STATUS_SUCCESS
;