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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /***********************************************************************
23 * - Reference counting
28 #include "wine/port.h"
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
43 #define WIN32_NO_STATUS
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
54 static HWND crypt_hWindow
;
56 #define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size))
57 #define CRYPT_Free(buffer) (LocalFree(buffer))
59 static inline PWSTR
CRYPT_GetProvKeyName(PCWSTR pProvName
)
61 static const WCHAR KEYSTR
[] = {
62 'S','o','f','t','w','a','r','e','\\',
63 'M','i','c','r','o','s','o','f','t','\\',
64 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
65 'D','e','f','a','u','l','t','s','\\',
66 'P','r','o','v','i','d','e','r','\\',0
70 keyname
= CRYPT_Alloc((strlenW(KEYSTR
) + strlenW(pProvName
) +1)*sizeof(WCHAR
));
73 strcpyW(keyname
, KEYSTR
);
74 strcpyW(keyname
+ strlenW(KEYSTR
), pProvName
);
76 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
80 static inline PWSTR
CRYPT_GetTypeKeyName(DWORD dwType
, BOOL user
)
82 static const WCHAR MACHINESTR
[] = {
83 'S','o','f','t','w','a','r','e','\\',
84 'M','i','c','r','o','s','o','f','t','\\',
85 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
86 'D','e','f','a','u','l','t','s','\\',
87 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
88 'T','y','p','e',' ','X','X','X',0
90 static const WCHAR USERSTR
[] = {
91 'S','o','f','t','w','a','r','e','\\',
92 'M','i','c','r','o','s','o','f','t','\\',
93 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
94 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
99 keyname
= CRYPT_Alloc( ((user
? strlenW(USERSTR
) : strlenW(MACHINESTR
)) +1)*sizeof(WCHAR
));
102 user
? strcpyW(keyname
, USERSTR
) : strcpyW(keyname
, MACHINESTR
);
103 ptr
= keyname
+ strlenW(keyname
);
104 *(--ptr
) = (dwType
% 10) + '0';
105 *(--ptr
) = ((dwType
/ 10) % 10) + '0';
106 *(--ptr
) = (dwType
/ 100) + '0';
108 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
112 /* CRYPT_UnicodeTOANSI
113 * wstr - unicode string
114 * str - pointer to ANSI string
115 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
117 * returns TRUE if unsuccessful, FALSE otherwise.
118 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
120 static inline BOOL
CRYPT_UnicodeToANSI(LPCWSTR wstr
, LPSTR
* str
, int strsize
)
129 count
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
131 *str
= CRYPT_Alloc(count
* sizeof(CHAR
));
133 count
= min( count
, strsize
);
136 WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, *str
, count
, NULL
, NULL
);
139 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
143 /* CRYPT_ANSITOUnicode
145 * wstr - pointer to unicode string
146 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
148 static inline BOOL
CRYPT_ANSIToUnicode(LPCSTR str
, LPWSTR
* wstr
, int wstrsize
)
157 wcount
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
159 *wstr
= CRYPT_Alloc(wcount
* sizeof(WCHAR
));
161 wcount
= min( wcount
, wstrsize
/sizeof(WCHAR
) );
164 MultiByteToWideChar(CP_ACP
, 0, str
, -1, *wstr
, wcount
);
167 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
171 /* These next 2 functions are used by the VTableProvStruc structure */
172 static BOOL CALLBACK
CRYPT_VerifyImage(LPCSTR lpszImage
, BYTE
* pData
)
174 if (!lpszImage
|| !pData
)
176 SetLastError(ERROR_INVALID_PARAMETER
);
180 FIXME("(%s, %p): not verifying image\n", lpszImage
, pData
);
185 static void CALLBACK
CRYPT_ReturnhWnd(HWND
*phWnd
)
187 if (phWnd
) *phWnd
= crypt_hWindow
;
190 #define CRYPT_GetProvFunc(name) \
191 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
192 #define CRYPT_GetProvFuncOpt(name) \
193 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
194 static PCRYPTPROV
CRYPT_LoadProvider(PCWSTR pImage
)
197 DWORD errorcode
= ERROR_NOT_ENOUGH_MEMORY
;
199 if ( !(provider
= CRYPT_Alloc(sizeof(CRYPTPROV
))) ) goto error
;
200 if ( !(provider
->pFuncs
= CRYPT_Alloc(sizeof(PROVFUNCS
))) ) goto error
;
201 if ( !(provider
->pVTable
= CRYPT_Alloc(sizeof(VTableProvStruc
))) ) goto error
;
202 if ( !(provider
->hModule
= LoadLibraryW(pImage
)) )
204 errorcode
= (GetLastError() == ERROR_FILE_NOT_FOUND
) ? NTE_PROV_DLL_NOT_FOUND
: NTE_PROVIDER_DLL_FAIL
;
205 FIXME("Failed to load dll %s\n", debugstr_w(pImage
));
208 provider
->dwMagic
= MAGIC_CRYPTPROV
;
209 provider
->refcount
= 1;
211 errorcode
= NTE_PROVIDER_DLL_FAIL
;
212 CRYPT_GetProvFunc(CPAcquireContext
);
213 CRYPT_GetProvFunc(CPCreateHash
);
214 CRYPT_GetProvFunc(CPDecrypt
);
215 CRYPT_GetProvFunc(CPDeriveKey
);
216 CRYPT_GetProvFunc(CPDestroyHash
);
217 CRYPT_GetProvFunc(CPDestroyKey
);
218 CRYPT_GetProvFuncOpt(CPDuplicateHash
);
219 CRYPT_GetProvFuncOpt(CPDuplicateKey
);
220 CRYPT_GetProvFunc(CPEncrypt
);
221 CRYPT_GetProvFunc(CPExportKey
);
222 CRYPT_GetProvFunc(CPGenKey
);
223 CRYPT_GetProvFunc(CPGenRandom
);
224 CRYPT_GetProvFunc(CPGetHashParam
);
225 CRYPT_GetProvFunc(CPGetKeyParam
);
226 CRYPT_GetProvFunc(CPGetProvParam
);
227 CRYPT_GetProvFunc(CPGetUserKey
);
228 CRYPT_GetProvFunc(CPHashData
);
229 CRYPT_GetProvFunc(CPHashSessionKey
);
230 CRYPT_GetProvFunc(CPImportKey
);
231 CRYPT_GetProvFunc(CPReleaseContext
);
232 CRYPT_GetProvFunc(CPSetHashParam
);
233 CRYPT_GetProvFunc(CPSetKeyParam
);
234 CRYPT_GetProvFunc(CPSetProvParam
);
235 CRYPT_GetProvFunc(CPSignHash
);
236 CRYPT_GetProvFunc(CPVerifySignature
);
238 /* FIXME: Not sure what the pbContextInfo field is for.
239 * Does it need memory allocation?
241 provider
->pVTable
->Version
= 3;
242 provider
->pVTable
->FuncVerifyImage
= CRYPT_VerifyImage
;
243 provider
->pVTable
->FuncReturnhWnd
= CRYPT_ReturnhWnd
;
244 provider
->pVTable
->dwProvType
= 0;
245 provider
->pVTable
->pbContextInfo
= NULL
;
246 provider
->pVTable
->cbContextInfo
= 0;
247 provider
->pVTable
->pszProvName
= NULL
;
251 SetLastError(errorcode
);
254 provider
->dwMagic
= 0;
255 if (provider
->hModule
)
256 FreeLibrary(provider
->hModule
);
257 CRYPT_Free(provider
->pVTable
);
258 CRYPT_Free(provider
->pFuncs
);
259 CRYPT_Free(provider
);
263 #undef CRYPT_GetProvFunc
264 #undef CRYPT_GetProvFuncOpt
267 static void CRYPT_CreateMachineGuid(void)
269 static const WCHAR cryptographyW
[] = {
270 'S','o','f','t','w','a','r','e','\\',
271 'M','i','c','r','o','s','o','f','t','\\',
272 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
273 static const WCHAR machineGuidW
[] = {
274 'M','a','c','h','i','n','e','G','u','i','d',0 };
278 r
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, cryptographyW
, 0, KEY_ALL_ACCESS
,
284 r
= RegQueryValueExW(key
, machineGuidW
, NULL
, NULL
, NULL
, &size
);
285 if (r
== ERROR_FILE_NOT_FOUND
)
287 static const WCHAR rpcrt4
[] = {
288 'r','p','c','r','t','4',0 };
289 HMODULE lib
= LoadLibraryW(rpcrt4
);
293 RPC_STATUS (RPC_ENTRY
*pUuidCreate
)(UUID
*);
297 static const WCHAR uuidFmt
[] = {
298 '%','0','8','x','-','%','0','4','x','-',
299 '%','0','4','x','-','%','0','2','x',
300 '%','0','2','x','-','%','0','2','x',
301 '%','0','2','x','%','0','2','x',
302 '%','0','2','x','%','0','2','x',
305 pUuidCreate
= (void *)GetProcAddress(lib
, "UuidCreate");
306 rs
= pUuidCreate(&uuid
);
309 sprintfW(buf
, uuidFmt
,
310 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
311 uuid
.Data4
[0], uuid
.Data4
[1],
312 uuid
.Data4
[2], uuid
.Data4
[3],
313 uuid
.Data4
[4], uuid
.Data4
[5],
314 uuid
.Data4
[6], uuid
.Data4
[7] );
315 RegSetValueExW(key
, machineGuidW
, 0, REG_SZ
,
317 (lstrlenW(buf
)+1)*sizeof(WCHAR
));
326 /******************************************************************************
327 * CryptAcquireContextW (ADVAPI32.@)
329 * Acquire a crypto provider context handle.
332 * phProv [O] Pointer to HCRYPTPROV for the output.
333 * pszContainer [I] Key Container Name
334 * pszProvider [I] Cryptographic Service Provider Name
335 * dwProvType [I] Crypto provider type to get a handle.
336 * dwFlags [I] flags for the operation
339 * TRUE on success, FALSE on failure.
341 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
342 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
344 PCRYPTPROV pProv
= NULL
;
346 PWSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
347 PSTR provnameA
= NULL
, pszContainerA
= NULL
;
348 DWORD keytype
, type
, len
;
350 static const WCHAR nameW
[] = {'N','a','m','e',0};
351 static const WCHAR typeW
[] = {'T','y','p','e',0};
352 static const WCHAR imagepathW
[] = {'I','m','a','g','e',' ','P','a','t','h',0};
354 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_w(pszContainer
),
355 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
357 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
359 SetLastError(NTE_BAD_PROV_TYPE
);
365 SetLastError(ERROR_INVALID_PARAMETER
);
369 /* Make sure the MachineGuid value exists */
370 CRYPT_CreateMachineGuid();
372 if (!pszProvider
|| !*pszProvider
)
374 /* No CSP name specified so try the user default CSP first
375 * then try the machine default CSP
377 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)) ) {
378 TRACE("No provider registered for crypto provider type %d.\n", dwProvType
);
379 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
382 if (RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &key
))
385 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
386 TRACE("No type registered for crypto provider type %d.\n", dwProvType
);
388 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
391 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
392 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname
));
395 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
400 r
= RegQueryValueExW(key
, nameW
, NULL
, &keytype
, NULL
, &len
);
401 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
403 TRACE("error %d reading size of 'Name' from registry\n", r
);
405 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
408 if(!(provname
= CRYPT_Alloc(len
)))
411 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
414 r
= RegQueryValueExW(key
, nameW
, NULL
, NULL
, (LPBYTE
)provname
, &len
);
415 if( r
!= ERROR_SUCCESS
)
417 TRACE("error %d reading 'Name' from registry\n", r
);
419 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
424 if ( !(provname
= CRYPT_Alloc((strlenW(pszProvider
) +1)*sizeof(WCHAR
))) )
426 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
429 strcpyW(provname
, pszProvider
);
432 keyname
= CRYPT_GetProvKeyName(provname
);
433 r
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
);
435 if (r
!= ERROR_SUCCESS
)
437 SetLastError(NTE_KEYSET_NOT_DEF
);
441 r
= RegQueryValueExW(key
, typeW
, NULL
, NULL
, (BYTE
*)&type
, &len
);
442 if (r
!= ERROR_SUCCESS
)
444 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
447 if (type
!= dwProvType
)
449 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type
, dwProvType
);
450 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
454 r
= RegQueryValueExW(key
, imagepathW
, NULL
, &keytype
, NULL
, &len
);
455 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
457 TRACE("error %d reading size of 'Image Path' from registry\n", r
);
459 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
462 if (!(temp
= CRYPT_Alloc(len
)))
465 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
468 r
= RegQueryValueExW(key
, imagepathW
, NULL
, NULL
, (LPBYTE
)temp
, &len
);
469 if( r
!= ERROR_SUCCESS
)
471 TRACE("error %d reading 'Image Path' from registry\n", r
);
473 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
477 len
= ExpandEnvironmentStringsW(temp
, NULL
, 0);
478 if ( !(imagepath
= CRYPT_Alloc(len
*sizeof(WCHAR
))) )
480 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
483 if (!ExpandEnvironmentStringsW(temp
, imagepath
, len
))
485 /* ExpandEnvironmentStrings will call SetLastError */
488 pProv
= CRYPT_LoadProvider(imagepath
);
490 /* CRYPT_LoadProvider calls SetLastError */
493 pProv
->pVTable
->dwProvType
= dwProvType
;
494 if(!CRYPT_UnicodeToANSI(provname
, &provnameA
, -1))
496 /* CRYPT_UnicodeToANSI calls SetLastError */
499 pProv
->pVTable
->pszProvName
= provnameA
;
500 if(!CRYPT_UnicodeToANSI(pszContainer
, &pszContainerA
, -1))
502 /* CRYPT_UnicodeToANSI calls SetLastError */
505 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, pszContainerA
, dwFlags
, pProv
->pVTable
))
507 /* MSDN: When this flag is set, the value returned in phProv is undefined,
508 * and thus, the CryptReleaseContext function need not be called afterwards.
509 * Therefore, we must clean up everything now.
511 if (dwFlags
& CRYPT_DELETEKEYSET
)
514 FreeLibrary(pProv
->hModule
);
515 CRYPT_Free(provnameA
);
516 CRYPT_Free(pProv
->pVTable
);
517 CRYPT_Free(pProv
->pFuncs
);
520 *phProv
= (HCRYPTPROV
)pProv
;
522 CRYPT_Free(pszContainerA
);
523 CRYPT_Free(provname
);
525 CRYPT_Free(imagepath
);
528 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
534 FreeLibrary(pProv
->hModule
);
535 CRYPT_Free(pProv
->pVTable
);
536 CRYPT_Free(pProv
->pFuncs
);
539 CRYPT_Free(pszContainerA
);
540 CRYPT_Free(provnameA
);
541 CRYPT_Free(provname
);
543 CRYPT_Free(imagepath
);
547 /******************************************************************************
548 * CryptAcquireContextA (ADVAPI32.@)
550 * See CryptAcquireContextW.
552 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
553 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
555 PWSTR pProvider
= NULL
, pContainer
= NULL
;
558 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, pszContainer
,
559 pszProvider
, dwProvType
, dwFlags
);
561 if ( !CRYPT_ANSIToUnicode(pszContainer
, &pContainer
, -1) )
563 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
566 if ( !CRYPT_ANSIToUnicode(pszProvider
, &pProvider
, -1) )
568 CRYPT_Free(pContainer
);
569 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
573 ret
= CryptAcquireContextW(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
575 CRYPT_Free(pContainer
);
576 CRYPT_Free(pProvider
);
581 /******************************************************************************
582 * CryptContextAddRef (ADVAPI32.@)
584 * Increases reference count of a cryptographic service provider handle
588 * hProv [I] Handle to the CSP whose reference is being incremented.
589 * pdwReserved [IN] Reserved for future use and must be NULL.
590 * dwFlags [I] Reserved for future use and must be NULL.
596 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
598 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
600 TRACE("(0x%lx, %p, %08x)\n", hProv
, pdwReserved
, dwFlags
);
604 SetLastError(NTE_BAD_UID
);
608 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
610 SetLastError(ERROR_INVALID_PARAMETER
);
618 /******************************************************************************
619 * CryptReleaseContext (ADVAPI32.@)
621 * Releases the handle of a CSP. Reference count is decreased.
624 * hProv [I] Handle of a CSP.
625 * dwFlags [I] Reserved for future use and must be NULL.
631 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, ULONG_PTR dwFlags
)
633 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
636 TRACE("(0x%lx, %08lx)\n", hProv
, dwFlags
);
640 SetLastError(NTE_BAD_UID
);
644 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
646 SetLastError(ERROR_INVALID_PARAMETER
);
651 if (pProv
->refcount
<= 0)
653 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
655 FreeLibrary(pProv
->hModule
);
657 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
659 CRYPT_Free(pProv
->pVTable
->pszProvName
);
660 CRYPT_Free(pProv
->pVTable
);
661 CRYPT_Free(pProv
->pFuncs
);
667 /******************************************************************************
668 * CryptGenRandom (ADVAPI32.@)
670 * Fills a buffer with cryptographically random bytes.
673 * hProv [I] Handle of a CSP.
674 * dwLen [I] Number of bytes to generate.
675 * pbBuffer [I/O] Buffer to contain random bytes.
682 * pdBuffer must be at least dwLen bytes long.
684 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
686 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
688 TRACE("(0x%lx, %d, %p)\n", hProv
, dwLen
, pbBuffer
);
692 SetLastError(ERROR_INVALID_HANDLE
);
696 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
698 SetLastError(ERROR_INVALID_PARAMETER
);
702 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
705 /******************************************************************************
706 * CryptCreateHash (ADVAPI32.@)
708 * Initiates the hashing of a stream of data.
711 * hProv [I] Handle of a CSP.
712 * Algid [I] Identifies the hash algorithm to use.
713 * hKey [I] Key for the hash (if required).
714 * dwFlags [I] Reserved for future use and must be NULL.
715 * phHash [O] Address of the future handle to the new hash object.
722 * If the algorithm is a keyed hash, hKey is the key.
724 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
725 DWORD dwFlags
, HCRYPTHASH
*phHash
)
727 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
728 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
731 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
735 SetLastError(ERROR_INVALID_HANDLE
);
738 if (!phHash
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
740 SetLastError(ERROR_INVALID_PARAMETER
);
745 SetLastError(NTE_BAD_FLAGS
);
748 if ( !(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
750 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
754 hash
->pProvider
= prov
;
756 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
757 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
759 *phHash
= (HCRYPTHASH
)hash
;
769 /******************************************************************************
770 * CryptDecrypt (ADVAPI32.@)
772 * Decrypts data encrypted by CryptEncrypt.
775 * hKey [I] Handle to the decryption key.
776 * hHash [I] Handle to a hash object.
777 * Final [I] TRUE if this is the last section to be decrypted.
778 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
779 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
781 * pdwDataLen [I/O] Length of pbData before and after the call.
787 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
788 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
791 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
792 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
794 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
796 if (!key
|| !pbData
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
798 SetLastError(ERROR_INVALID_PARAMETER
);
802 prov
= key
->pProvider
;
803 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
804 Final
, dwFlags
, pbData
, pdwDataLen
);
807 /******************************************************************************
808 * CryptDeriveKey (ADVAPI32.@)
810 * Generates session keys derived from a base data value.
813 * hProv [I] Handle to a CSP.
814 * Algid [I] Identifies the symmetric encryption algorithm to use.
815 * hBaseData [I] Handle to a hash object.
816 * dwFlags [I] Type of key to generate.
817 * phKey [I/O] Address of the newly generated key.
823 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
824 DWORD dwFlags
, HCRYPTKEY
*phKey
)
826 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
827 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
830 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
834 SetLastError(ERROR_INVALID_HANDLE
);
837 if (!phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
839 SetLastError(ERROR_INVALID_PARAMETER
);
842 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
844 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
848 key
->pProvider
= prov
;
849 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
851 *phKey
= (HCRYPTKEY
)key
;
861 /******************************************************************************
862 * CryptDestroyHash (ADVAPI32.@)
864 * Destroys the hash object referenced by hHash.
867 * hHash [I] Handle of the hash object to be destroyed.
873 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
875 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
879 TRACE("(0x%lx)\n", hHash
);
883 SetLastError(ERROR_INVALID_HANDLE
);
887 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
889 SetLastError(ERROR_INVALID_PARAMETER
);
893 prov
= hash
->pProvider
;
894 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
899 /******************************************************************************
900 * CryptDestroyKey (ADVAPI32.@)
902 * Releases the handle referenced by hKey.
905 * hKey [I] Handle of the key to be destroyed.
911 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
913 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
917 TRACE("(0x%lx)\n", hKey
);
921 SetLastError(ERROR_INVALID_HANDLE
);
925 if (!key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
927 SetLastError(ERROR_INVALID_PARAMETER
);
931 prov
= key
->pProvider
;
932 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
937 /******************************************************************************
938 * CryptDuplicateHash (ADVAPI32.@)
943 * hHash [I] Handle to the hash to be copied.
944 * pdwReserved [I] Reserved for future use and must be zero.
945 * dwFlags [I] Reserved for future use and must be zero.
946 * phHash [O] Address of the handle to receive the copy.
952 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
953 DWORD dwFlags
, HCRYPTHASH
*phHash
)
956 PCRYPTHASH orghash
, newhash
;
958 TRACE("(0x%lx, %p, %08x, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
960 orghash
= (PCRYPTHASH
)hHash
;
961 if (!orghash
|| pdwReserved
|| !phHash
|| !orghash
->pProvider
||
962 orghash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
964 SetLastError(ERROR_INVALID_PARAMETER
);
968 prov
= orghash
->pProvider
;
969 if (!prov
->pFuncs
->pCPDuplicateHash
)
971 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
975 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
977 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
981 newhash
->pProvider
= prov
;
982 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
984 *phHash
= (HCRYPTHASH
)newhash
;
991 /******************************************************************************
992 * CryptDuplicateKey (ADVAPI32.@)
994 * Duplicate a key and the key's state.
997 * hKey [I] Handle of the key to copy.
998 * pdwReserved [I] Reserved for future use and must be NULL.
999 * dwFlags [I] Reserved for future use and must be zero.
1000 * phKey [I] Address of the handle to the duplicated key.
1006 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1009 PCRYPTKEY orgkey
, newkey
;
1011 TRACE("(0x%lx, %p, %08x, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
1013 orgkey
= (PCRYPTKEY
)hKey
;
1014 if (!orgkey
|| pdwReserved
|| !phKey
|| !orgkey
->pProvider
||
1015 orgkey
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1017 SetLastError(ERROR_INVALID_PARAMETER
);
1021 prov
= orgkey
->pProvider
;
1022 if (!prov
->pFuncs
->pCPDuplicateKey
)
1024 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1028 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1030 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1034 newkey
->pProvider
= prov
;
1035 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
1037 *phKey
= (HCRYPTKEY
)newkey
;
1044 /******************************************************************************
1045 * CryptEncrypt (ADVAPI32.@)
1050 * hKey [I] Handle to the encryption key.
1051 * hHash [I] Handle to a hash object.
1052 * Final [I] TRUE if this is the last section to encrypt.
1053 * dwFlags [I] Can be CRYPT_OAEP.
1054 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1055 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1056 * encrypted data after call.
1057 * dwBufLen [I] Length of the input pbData buffer.
1064 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1065 * required for the returned data in pdwDataLen.
1067 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1068 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1071 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1072 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1074 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1076 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1078 SetLastError(ERROR_INVALID_PARAMETER
);
1082 prov
= key
->pProvider
;
1083 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
1084 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1087 /******************************************************************************
1088 * CryptEnumProvidersW (ADVAPI32.@)
1090 * Returns the next available CSP.
1093 * dwIndex [I] Index of the next provider to be enumerated.
1094 * pdwReserved [I] Reserved for future use and must be NULL.
1095 * dwFlags [I] Reserved for future use and must be zero.
1096 * pdwProvType [O] DWORD designating the type of the provider.
1097 * pszProvName [O] Buffer that receives data from the provider.
1098 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1099 * of bytes stored in the buffer on return.
1106 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1107 * for memory allocation purposes.
1109 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
1110 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1113 static const WCHAR providerW
[] = {
1114 'S','o','f','t','w','a','r','e','\\',
1115 'M','i','c','r','o','s','o','f','t','\\',
1116 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1117 'D','e','f','a','u','l','t','s','\\',
1118 'P','r','o','v','i','d','e','r',0
1120 static const WCHAR typeW
[] = {'T','y','p','e',0};
1122 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1123 pdwProvType
, pszProvName
, pcbProvName
);
1125 if (pdwReserved
|| !pcbProvName
)
1127 SetLastError(ERROR_INVALID_PARAMETER
);
1132 SetLastError(NTE_BAD_FLAGS
);
1136 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, providerW
, &hKey
))
1138 SetLastError(NTE_FAIL
);
1147 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1148 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1150 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1152 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1156 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1157 CRYPT_Free(provNameW
);
1159 *pcbProvName
*= sizeof(WCHAR
);
1161 if (dwIndex
>= numkeys
)
1163 SetLastError(ERROR_NO_MORE_ITEMS
);
1167 DWORD size
= sizeof(DWORD
);
1171 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1174 SetLastError(result
);
1177 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1179 if (RegQueryValueExW(subkey
, typeW
, NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1181 RegCloseKey(subkey
);
1187 /******************************************************************************
1188 * CryptEnumProvidersA (ADVAPI32.@)
1190 * See CryptEnumProvidersW.
1192 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1193 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1197 BOOL ret
; /* = FALSE; */
1199 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1200 pdwProvType
, pszProvName
, pcbProvName
);
1202 if(!CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1204 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1206 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1209 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1211 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1212 *pcbProvName
= bufsize
/ sizeof(WCHAR
); /* FIXME: not correct */
1216 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1218 SetLastError(ERROR_MORE_DATA
);
1225 /******************************************************************************
1226 * CryptEnumProviderTypesW (ADVAPI32.@)
1228 * Retrieves the next type of CSP supported.
1231 * dwIndex [I] Index of the next provider to be enumerated.
1232 * pdwReserved [I] Reserved for future use and must be NULL.
1233 * dwFlags [I] Reserved for future use and must be zero.
1234 * pdwProvType [O] DWORD designating the type of the provider.
1235 * pszTypeName [O] Buffer that receives data from the provider type.
1236 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1237 * of bytes stored in the buffer on return.
1244 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1245 * for memory allocation purposes.
1247 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1248 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1251 DWORD keylen
, numkeys
, dwType
;
1254 static const WCHAR KEYSTR
[] = {
1255 'S','o','f','t','w','a','r','e','\\',
1256 'M','i','c','r','o','s','o','f','t','\\',
1257 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1258 'D','e','f','a','u','l','t','s','\\',
1259 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1261 static const WCHAR typenameW
[] = {'T','y','p','e','N','a','m','e',0};
1263 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1264 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1266 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1268 SetLastError(ERROR_INVALID_PARAMETER
);
1273 SetLastError(NTE_BAD_FLAGS
);
1277 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, KEYSTR
, &hKey
))
1280 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1281 if (dwIndex
>= numkeys
)
1283 SetLastError(ERROR_NO_MORE_ITEMS
);
1287 if ( !(keyname
= CRYPT_Alloc(keylen
*sizeof(WCHAR
))) )
1289 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1292 if ( RegEnumKeyW(hKey
, dwIndex
, keyname
, keylen
) ) {
1293 CRYPT_Free(keyname
);
1296 RegOpenKeyW(hKey
, keyname
, &hSubkey
);
1297 ch
= keyname
+ strlenW(keyname
);
1298 /* Convert "Type 000" to 0, etc/ */
1299 *pdwProvType
= *(--ch
) - '0';
1300 *pdwProvType
+= (*(--ch
) - '0') * 10;
1301 *pdwProvType
+= (*(--ch
) - '0') * 100;
1302 CRYPT_Free(keyname
);
1304 result
= RegQueryValueExW(hSubkey
, typenameW
, NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1307 SetLastError(result
);
1311 RegCloseKey(hSubkey
);
1316 /******************************************************************************
1317 * CryptEnumProviderTypesA (ADVAPI32.@)
1319 * See CryptEnumProviderTypesW.
1321 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1322 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1328 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1329 pdwProvType
, pszTypeName
, pcbTypeName
);
1331 if(!CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1333 if ( pszTypeName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1335 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1338 ret
= CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1340 CRYPT_UnicodeToANSI(str
, &pszTypeName
, *pcbTypeName
);
1341 *pcbTypeName
= bufsize
/ sizeof(WCHAR
);
1345 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1347 SetLastError(ERROR_MORE_DATA
);
1354 /******************************************************************************
1355 * CryptExportKey (ADVAPI32.@)
1357 * Exports a cryptographic key from a CSP.
1360 * hKey [I] Handle to the key to export.
1361 * hExpKey [I] Handle to a cryptographic key of the end user.
1362 * dwBlobType [I] Type of BLOB to be exported.
1363 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1364 * pbData [O] Buffer to receive BLOB data.
1365 * pdwDataLen [I/O] Specifies the size of pbData.
1372 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1373 * buffer needed to hold the BLOB.
1375 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1376 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1379 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1381 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1383 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1385 SetLastError(ERROR_INVALID_PARAMETER
);
1389 prov
= key
->pProvider
;
1390 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1391 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1394 /******************************************************************************
1395 * CryptGenKey (ADVAPI32.@)
1397 * Generates a random cryptographic session key or a pub/priv key pair.
1400 * hProv [I] Handle to a CSP.
1401 * Algid [I] Algorithm to use to make key.
1402 * dwFlags [I] Specifies type of key to make.
1403 * phKey [I] Address of the handle to which the new key is copied.
1409 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1411 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1414 TRACE("(0x%lx, %d, %08x, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1418 SetLastError(ERROR_INVALID_HANDLE
);
1421 if (!phKey
|| !prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1423 SetLastError(ERROR_INVALID_PARAMETER
);
1426 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1428 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1432 key
->pProvider
= prov
;
1434 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1436 *phKey
= (HCRYPTKEY
)key
;
1445 /******************************************************************************
1446 * CryptGetDefaultProviderW (ADVAPI32.@)
1448 * Finds the default CSP of a certain provider type.
1451 * dwProvType [I] Provider type to look for.
1452 * pdwReserved [I] Reserved for future use and must be NULL.
1453 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1454 * pszProvName [O] Name of the default CSP.
1455 * pcbProvName [I/O] Size of pszProvName
1462 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1463 * memory allocation purposes on return.
1465 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1466 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1471 static const WCHAR nameW
[] = {'N','a','m','e',0};
1473 if (pdwReserved
|| !pcbProvName
)
1475 SetLastError(ERROR_INVALID_PARAMETER
);
1478 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1480 SetLastError(NTE_BAD_FLAGS
);
1483 if (dwProvType
> 999)
1485 SetLastError(NTE_BAD_PROV_TYPE
);
1488 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1490 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1493 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1495 CRYPT_Free(keyname
);
1496 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
1499 CRYPT_Free(keyname
);
1501 result
= RegQueryValueExW(hKey
, nameW
, NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1504 if (result
!= ERROR_MORE_DATA
)
1505 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1507 SetLastError(result
);
1516 /******************************************************************************
1517 * CryptGetDefaultProviderA (ADVAPI32.@)
1519 * See CryptGetDefaultProviderW.
1521 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1522 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1528 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1530 CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, NULL
, &bufsize
);
1531 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1533 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1536 ret
= CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, str
, &bufsize
);
1538 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1539 *pcbProvName
= bufsize
/ sizeof(WCHAR
);
1543 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1545 SetLastError(ERROR_MORE_DATA
);
1552 /******************************************************************************
1553 * CryptGetHashParam (ADVAPI32.@)
1555 * Retrieves data that controls the operations of a hash object.
1558 * hHash [I] Handle of the hash object to question.
1559 * dwParam [I] Query type.
1560 * pbData [O] Buffer that receives the value data.
1561 * pdwDataLen [I/O] Size of the pbData buffer.
1562 * dwFlags [I] Reserved for future use and must be zero.
1569 * If pbData is NULL, pdwDataLen will contain the length required.
1571 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1572 DWORD
*pdwDataLen
, DWORD dwFlags
)
1575 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1577 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1579 if (!hash
|| !pdwDataLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1581 SetLastError(ERROR_INVALID_PARAMETER
);
1585 prov
= hash
->pProvider
;
1586 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1587 pbData
, pdwDataLen
, dwFlags
);
1590 /******************************************************************************
1591 * CryptGetKeyParam (ADVAPI32.@)
1593 * Retrieves data that controls the operations of a key.
1596 * hKey [I] Handle to they key in question.
1597 * dwParam [I] Specifies query type.
1598 * pbData [O] Sequence of bytes to receive data.
1599 * pdwDataLen [I/O] Size of pbData.
1600 * dwFlags [I] Reserved for future use and must be zero.
1607 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1609 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1610 DWORD
*pdwDataLen
, DWORD dwFlags
)
1613 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1615 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1617 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1619 SetLastError(ERROR_INVALID_PARAMETER
);
1623 prov
= key
->pProvider
;
1624 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1625 pbData
, pdwDataLen
, dwFlags
);
1628 /******************************************************************************
1629 * CryptGetProvParam (ADVAPI32.@)
1631 * Retrieves parameters that control the operations of a CSP.
1634 * hProv [I] Handle of the CSP in question.
1635 * dwParam [I] Specifies query type.
1636 * pbData [O] Buffer to receive the data.
1637 * pdwDataLen [I/O] Size of pbData.
1638 * dwFlags [I] see MSDN Docs.
1645 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1647 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1648 DWORD
*pdwDataLen
, DWORD dwFlags
)
1650 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1652 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1654 if (!prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1656 SetLastError(ERROR_INVALID_PARAMETER
);
1660 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1663 /******************************************************************************
1664 * CryptGetUserKey (ADVAPI32.@)
1666 * Gets a handle of one of a user's two public/private key pairs.
1669 * hProv [I] Handle of a CSP.
1670 * dwKeySpec [I] Private key to use.
1671 * phUserKey [O] Pointer to the handle of the retrieved keys.
1677 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1679 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1682 TRACE("(0x%lx, %d, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1686 SetLastError(ERROR_INVALID_HANDLE
);
1689 if (!phUserKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1691 SetLastError(ERROR_INVALID_PARAMETER
);
1694 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1696 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1700 key
->pProvider
= prov
;
1702 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1704 *phUserKey
= (HCRYPTKEY
)key
;
1714 /******************************************************************************
1715 * CryptHashData (ADVAPI32.@)
1717 * Adds data to a hash object.
1720 * hHash [I] Handle of the hash object.
1721 * pbData [I] Buffer of data to be hashed.
1722 * dwDataLen [I] Number of bytes to add.
1723 * dwFlags [I] Can be CRYPT_USERDATA
1729 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, const BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1731 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1734 TRACE("(0x%lx, %p, %d, %08x)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1738 SetLastError(ERROR_INVALID_HANDLE
);
1741 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1743 SetLastError(ERROR_INVALID_PARAMETER
);
1747 prov
= hash
->pProvider
;
1748 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1751 /******************************************************************************
1752 * CryptHashSessionKey (ADVAPI32.@)
1754 * Compute the cryptographic hash of a session key object.
1757 * hHash [I] Handle to the hash object.
1758 * hKey [I] Handle to the key to be hashed.
1759 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1765 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1767 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1768 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1771 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash
, hKey
, dwFlags
);
1775 SetLastError(ERROR_INVALID_HANDLE
);
1779 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1781 SetLastError(ERROR_INVALID_PARAMETER
);
1785 prov
= hash
->pProvider
;
1786 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1789 /******************************************************************************
1790 * CryptImportKey (ADVAPI32.@)
1792 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1795 * hProv [I] Handle of a CSP.
1796 * pbData [I] Contains the key to be imported.
1797 * dwDataLen [I] Length of the key.
1798 * hPubKey [I] Cryptographic key that decrypts pdData
1799 * dwFlags [I] Used only with a public/private key pair.
1800 * phKey [O] Imported key.
1806 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
1807 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1809 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1810 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1812 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1814 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1816 SetLastError(ERROR_INVALID_PARAMETER
);
1820 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1822 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1826 importkey
->pProvider
= prov
;
1827 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1828 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1830 *phKey
= (HCRYPTKEY
)importkey
;
1834 CRYPT_Free(importkey
);
1838 /******************************************************************************
1839 * CryptSignHashW (ADVAPI32.@)
1844 * hHash [I] Handle of the hash object to be signed.
1845 * dwKeySpec [I] Private key to use.
1846 * sDescription [I] Should be NULL.
1847 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1848 * pbSignature [O] Buffer of the signature data.
1849 * pdwSigLen [I/O] Size of the pbSignature buffer.
1856 * Because of security flaws sDescription should not be used and should thus be
1857 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1860 BOOL WINAPI
CryptSignHashW (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCWSTR sDescription
,
1861 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1863 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1866 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1867 hHash
, dwKeySpec
, debugstr_w(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1871 SetLastError(ERROR_INVALID_HANDLE
);
1874 if (!pdwSigLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1876 SetLastError(ERROR_INVALID_PARAMETER
);
1880 prov
= hash
->pProvider
;
1881 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, sDescription
,
1882 dwFlags
, pbSignature
, pdwSigLen
);
1885 /******************************************************************************
1886 * CryptSignHashA (ADVAPI32.@)
1888 * See CryptSignHashW.
1890 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1891 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1893 LPWSTR wsDescription
;
1896 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1897 hHash
, dwKeySpec
, debugstr_a(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1899 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1900 result
= CryptSignHashW(hHash
, dwKeySpec
, wsDescription
, dwFlags
, pbSignature
, pdwSigLen
);
1901 CRYPT_Free(wsDescription
);
1906 /******************************************************************************
1907 * CryptSetHashParam (ADVAPI32.@)
1909 * Customizes the operations of a hash object.
1912 * hHash [I] Handle of the hash object to set parameters.
1913 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1914 * pbData [I] Value data buffer.
1915 * dwFlags [I] Reserved for future use and must be zero.
1921 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
1924 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1926 TRACE("(0x%lx, %d, %p, %08x)\n", hHash
, dwParam
, pbData
, dwFlags
);
1928 if (!hash
|| !pbData
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1930 SetLastError(ERROR_INVALID_PARAMETER
);
1934 prov
= hash
->pProvider
;
1935 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1936 dwParam
, pbData
, dwFlags
);
1939 /******************************************************************************
1940 * CryptSetKeyParam (ADVAPI32.@)
1942 * Customizes a session key's operations.
1945 * hKey [I] Handle to the key to set values.
1946 * dwParam [I] See MSDN Doc.
1947 * pbData [I] Buffer of values to set.
1948 * dwFlags [I] Only used when dwParam == KP_ALGID.
1954 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
1957 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1959 TRACE("(0x%lx, %d, %p, %08x)\n", hKey
, dwParam
, pbData
, dwFlags
);
1961 if (!key
|| !pbData
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1963 SetLastError(ERROR_INVALID_PARAMETER
);
1967 prov
= key
->pProvider
;
1968 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
1969 dwParam
, pbData
, dwFlags
);
1972 /******************************************************************************
1973 * CryptSetProviderA (ADVAPI32.@)
1975 * Specifies the current user's default CSP.
1978 * pszProvName [I] Name of the new default CSP.
1979 * dwProvType [I] Provider type of the CSP.
1985 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
1987 TRACE("(%s, %d)\n", pszProvName
, dwProvType
);
1988 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1991 /******************************************************************************
1992 * CryptSetProviderW (ADVAPI32.@)
1994 * See CryptSetProviderA.
1996 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
1998 TRACE("(%s, %d)\n", debugstr_w(pszProvName
), dwProvType
);
1999 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
2002 /******************************************************************************
2003 * CryptSetProviderExW (ADVAPI32.@)
2005 * Specifies the default CSP.
2008 * pszProvName [I] Name of the new default CSP.
2009 * dwProvType [I] Provider type of the CSP.
2010 * pdwReserved [I] Reserved for future use and must be NULL.
2011 * dwFlags [I] See MSDN Doc.
2017 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2019 HKEY hProvKey
, hTypeKey
;
2021 static const WCHAR nameW
[] = {'N','a','m','e',0};
2023 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
2025 if (!pszProvName
|| pdwReserved
)
2027 SetLastError(ERROR_INVALID_PARAMETER
);
2030 if (dwProvType
> MAXPROVTYPES
)
2032 SetLastError(NTE_BAD_PROV_TYPE
);
2035 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
2036 || dwFlags
== CRYPT_DELETE_DEFAULT
)
2038 SetLastError(NTE_BAD_FLAGS
);
2042 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
2044 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2047 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2048 keyname
, &hTypeKey
))
2050 CRYPT_Free(keyname
);
2051 SetLastError(NTE_BAD_PROVIDER
);
2054 CRYPT_Free(keyname
);
2056 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
2058 RegDeleteValueW(hTypeKey
, nameW
);
2062 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
2064 RegCloseKey(hTypeKey
);
2065 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2068 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2069 keyname
, &hProvKey
))
2071 CRYPT_Free(keyname
);
2072 RegCloseKey(hTypeKey
);
2073 SetLastError(NTE_BAD_PROVIDER
);
2076 CRYPT_Free(keyname
);
2078 if (RegSetValueExW(hTypeKey
, nameW
, 0, REG_SZ
, (const BYTE
*)pszProvName
,
2079 (strlenW(pszProvName
) + 1)*sizeof(WCHAR
)))
2081 RegCloseKey(hTypeKey
);
2082 RegCloseKey(hProvKey
);
2086 RegCloseKey(hProvKey
);
2088 RegCloseKey(hTypeKey
);
2093 /******************************************************************************
2094 * CryptSetProviderExA (ADVAPI32.@)
2096 * See CryptSetProviderExW.
2098 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2103 TRACE("(%s, %d, %p, %08x)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
2105 if (CRYPT_ANSIToUnicode(pszProvName
, &str
, -1))
2107 ret
= CryptSetProviderExW(str
, dwProvType
, pdwReserved
, dwFlags
);
2113 /******************************************************************************
2114 * CryptSetProvParam (ADVAPI32.@)
2116 * Customizes the operations of a CSP.
2119 * hProv [I] Handle of a CSP.
2120 * dwParam [I] See MSDN Doc.
2121 * pbData [I] Buffer that contains a value to set as a parameter.
2122 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2128 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
2130 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
2132 TRACE("(0x%lx, %d, %p, %08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
2136 SetLastError(ERROR_INVALID_HANDLE
);
2139 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
2141 SetLastError(ERROR_INVALID_PARAMETER
);
2144 if (dwFlags
& PP_USE_HARDWARE_RNG
)
2146 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2147 FIXME("\tLetting the CSP decide.\n");
2149 if (dwFlags
& PP_CLIENT_HWND
)
2151 /* FIXME: Should verify the parameter */
2152 if (pbData
/* && IsWindow((HWND)pbData) */)
2154 crypt_hWindow
= (HWND
)(pbData
);
2157 SetLastError(ERROR_INVALID_PARAMETER
);
2161 /* All other flags go to the CSP */
2162 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
2165 /******************************************************************************
2166 * CryptVerifySignatureW (ADVAPI32.@)
2168 * Verifies the signature of a hash object.
2171 * hHash [I] Handle of the hash object to verify.
2172 * pbSignature [I] Signature data to verify.
2173 * dwSigLen [I] Size of pbSignature.
2174 * hPubKey [I] Handle to the public key to authenticate signature.
2175 * sDescription [I] Should be NULL.
2176 * dwFlags [I] See MSDN doc.
2183 * Because of security flaws sDescription should not be used and should thus be
2184 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2187 BOOL WINAPI
CryptVerifySignatureW (HCRYPTHASH hHash
, CONST BYTE
*pbSignature
, DWORD dwSigLen
,
2188 HCRYPTKEY hPubKey
, LPCWSTR sDescription
, DWORD dwFlags
)
2190 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
2191 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
2194 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2195 dwSigLen
, hPubKey
, debugstr_w(sDescription
), dwFlags
);
2197 if (!hash
|| !key
||
2198 !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
||
2199 !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
2201 SetLastError(ERROR_INVALID_PARAMETER
);
2205 prov
= hash
->pProvider
;
2206 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
2207 key
->hPrivate
, sDescription
, dwFlags
);
2210 /******************************************************************************
2211 * CryptVerifySignatureA (ADVAPI32.@)
2213 * See CryptVerifySignatureW.
2215 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, CONST BYTE
*pbSignature
, DWORD dwSigLen
,
2216 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
2218 LPWSTR wsDescription
;
2221 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2222 dwSigLen
, hPubKey
, debugstr_a(sDescription
), dwFlags
);
2224 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
2225 result
= CryptVerifySignatureW(hHash
, pbSignature
, dwSigLen
, hPubKey
, wsDescription
, dwFlags
);
2226 CRYPT_Free(wsDescription
);
2231 /******************************************************************************
2232 * SystemFunction030 (ADVAPI32.@)
2234 * Tests if two blocks of 16 bytes are equal
2237 * b1,b2 [I] block of 16 bytes
2240 * TRUE if blocks are the same
2241 * FALSE if blocks are different
2243 BOOL WINAPI
SystemFunction030(LPCVOID b1
, LPCVOID b2
)
2245 return !memcmp(b1
, b2
, 0x10);
2248 /******************************************************************************
2249 * SystemFunction035 (ADVAPI32.@)
2252 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2255 * Stub, always return TRUE.
2257 BOOL WINAPI
SystemFunction035(LPCSTR lpszDllFilePath
)
2259 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath
));
2263 /******************************************************************************
2264 * SystemFunction036 (ADVAPI32.@)
2266 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2269 * pbBufer [O] Pointer to memory to receive random bytes.
2270 * dwLen [I] Number of random bytes to fetch.
2277 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
2281 dev_random
= open("/dev/urandom", O_RDONLY
);
2282 if (dev_random
!= -1)
2284 if (read(dev_random
, pbBuffer
, dwLen
) == (ssize_t
)dwLen
)
2292 FIXME("couldn't open /dev/urandom\n");
2293 SetLastError(NTE_FAIL
);
2298 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2302 /******************************************************************************
2303 * SystemFunction040 (ADVAPI32.@)
2305 * MSDN documents this function as RtlEncryptMemory.
2308 * memory [I/O] Pointer to memory to encrypt.
2309 * length [I] Length of region to encrypt in bytes.
2310 * flags [I] Control whether other processes are able to decrypt the memory.
2311 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2312 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2313 * RTL_ENCRYPT_OPTION_SAME_LOGON
2316 * Success: STATUS_SUCCESS
2317 * Failure: NTSTATUS error code
2320 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2321 * If flags are specified when encrypting, the same flag value must be given
2322 * when decrypting the memory.
2324 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
)
2326 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
2327 return STATUS_SUCCESS
;
2330 /******************************************************************************
2331 * SystemFunction041 (ADVAPI32.@)
2333 * MSDN documents this function as RtlDecryptMemory.
2336 * memory [I/O] Pointer to memory to decrypt.
2337 * length [I] Length of region to decrypt in bytes.
2338 * flags [I] Control whether other processes are able to decrypt the memory.
2339 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2340 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2341 * RTL_ENCRYPT_OPTION_SAME_LOGON
2344 * Success: STATUS_SUCCESS
2345 * Failure: NTSTATUS error code
2348 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2349 * If flags are specified when encrypting, the same flag value must be given
2350 * when decrypting the memory.
2352 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
)
2354 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
2355 return STATUS_SUCCESS
;