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((HLOCAL)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 BOOL CALLBACK
CRYPT_ReturnhWnd(HWND
*phWnd
)
189 *phWnd
= crypt_hWindow
;
193 #define CRYPT_GetProvFunc(name) \
194 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
195 #define CRYPT_GetProvFuncOpt(name) \
196 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
197 static PCRYPTPROV
CRYPT_LoadProvider(PCWSTR pImage
)
200 DWORD errorcode
= ERROR_NOT_ENOUGH_MEMORY
;
202 if ( !(provider
= CRYPT_Alloc(sizeof(CRYPTPROV
))) ) goto error
;
203 if ( !(provider
->pFuncs
= CRYPT_Alloc(sizeof(PROVFUNCS
))) ) goto error
;
204 if ( !(provider
->pVTable
= CRYPT_Alloc(sizeof(VTableProvStruc
))) ) goto error
;
205 if ( !(provider
->hModule
= LoadLibraryW(pImage
)) )
207 errorcode
= (GetLastError() == ERROR_FILE_NOT_FOUND
) ? NTE_PROV_DLL_NOT_FOUND
: NTE_PROVIDER_DLL_FAIL
;
208 FIXME("Failed to load dll %s\n", debugstr_w(pImage
));
211 provider
->dwMagic
= MAGIC_CRYPTPROV
;
212 provider
->refcount
= 1;
214 errorcode
= NTE_PROVIDER_DLL_FAIL
;
215 CRYPT_GetProvFunc(CPAcquireContext
);
216 CRYPT_GetProvFunc(CPCreateHash
);
217 CRYPT_GetProvFunc(CPDecrypt
);
218 CRYPT_GetProvFunc(CPDeriveKey
);
219 CRYPT_GetProvFunc(CPDestroyHash
);
220 CRYPT_GetProvFunc(CPDestroyKey
);
221 CRYPT_GetProvFuncOpt(CPDuplicateHash
);
222 CRYPT_GetProvFuncOpt(CPDuplicateKey
);
223 CRYPT_GetProvFunc(CPEncrypt
);
224 CRYPT_GetProvFunc(CPExportKey
);
225 CRYPT_GetProvFunc(CPGenKey
);
226 CRYPT_GetProvFunc(CPGenRandom
);
227 CRYPT_GetProvFunc(CPGetHashParam
);
228 CRYPT_GetProvFunc(CPGetKeyParam
);
229 CRYPT_GetProvFunc(CPGetProvParam
);
230 CRYPT_GetProvFunc(CPGetUserKey
);
231 CRYPT_GetProvFunc(CPHashData
);
232 CRYPT_GetProvFunc(CPHashSessionKey
);
233 CRYPT_GetProvFunc(CPImportKey
);
234 CRYPT_GetProvFunc(CPReleaseContext
);
235 CRYPT_GetProvFunc(CPSetHashParam
);
236 CRYPT_GetProvFunc(CPSetKeyParam
);
237 CRYPT_GetProvFunc(CPSetProvParam
);
238 CRYPT_GetProvFunc(CPSignHash
);
239 CRYPT_GetProvFunc(CPVerifySignature
);
241 /* FIXME: Not sure what the pbContextInfo field is for.
242 * Does it need memory allocation?
244 provider
->pVTable
->Version
= 3;
245 provider
->pVTable
->pFuncVerifyImage
= (FARPROC
)CRYPT_VerifyImage
;
246 provider
->pVTable
->pFuncReturnhWnd
= (FARPROC
)CRYPT_ReturnhWnd
;
247 provider
->pVTable
->dwProvType
= 0;
248 provider
->pVTable
->pbContextInfo
= NULL
;
249 provider
->pVTable
->cbContextInfo
= 0;
250 provider
->pVTable
->pszProvName
= NULL
;
254 SetLastError(errorcode
);
257 provider
->dwMagic
= 0;
258 if (provider
->hModule
)
259 FreeLibrary(provider
->hModule
);
260 CRYPT_Free(provider
->pVTable
);
261 CRYPT_Free(provider
->pFuncs
);
262 CRYPT_Free(provider
);
266 #undef CRYPT_GetProvFunc
267 #undef CRYPT_GetProvFuncOpt
270 static void CRYPT_CreateMachineGuid(void)
272 static const WCHAR cryptographyW
[] = {
273 'S','o','f','t','w','a','r','e','\\',
274 'M','i','c','r','o','s','o','f','t','\\',
275 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
276 static const WCHAR machineGuidW
[] = {
277 'M','a','c','h','i','n','e','G','u','i','d',0 };
281 r
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, cryptographyW
, 0, KEY_ALL_ACCESS
,
287 r
= RegQueryValueExW(key
, machineGuidW
, NULL
, NULL
, NULL
, &size
);
288 if (r
== ERROR_FILE_NOT_FOUND
)
290 static const WCHAR rpcrt4
[] = {
291 'r','p','c','r','t','4',0 };
292 HMODULE lib
= LoadLibraryW(rpcrt4
);
296 RPC_STATUS (RPC_ENTRY
*pUuidCreate
)(UUID
*);
297 RPC_STATUS (RPC_ENTRY
*pUuidToString
)(UUID
*, WCHAR
**);
298 RPC_STATUS (RPC_ENTRY
*pRpcStringFree
)(WCHAR
**);
302 pUuidCreate
= GetProcAddress(lib
, "UuidCreate");
303 pUuidToString
= GetProcAddress(lib
, "UuidToStringW");
304 pRpcStringFree
= GetProcAddress(lib
, "RpcStringFreeW");
306 pUuidToString(&uuid
, &uuidStr
);
307 RegSetValueExW(key
, machineGuidW
, 0, REG_SZ
,
308 (const BYTE
*)uuidStr
,
309 (lstrlenW(uuidStr
)+1)*sizeof(WCHAR
));
310 pRpcStringFree(&uuidStr
);
318 /******************************************************************************
319 * CryptAcquireContextW (ADVAPI32.@)
321 * Acquire a crypto provider context handle.
324 * phProv [O] Pointer to HCRYPTPROV for the output.
325 * pszContainer [I] Key Container Name
326 * pszProvider [I] Cryptographic Service Provider Name
327 * dwProvType [I] Crypto provider type to get a handle.
328 * dwFlags [I] flags for the operation
331 * TRUE on success, FALSE on failure.
333 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
334 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
336 PCRYPTPROV pProv
= NULL
;
338 PWSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
339 PSTR provnameA
= NULL
, pszContainerA
= NULL
;
340 DWORD keytype
, type
, len
;
342 static const WCHAR nameW
[] = {'N','a','m','e',0};
343 static const WCHAR typeW
[] = {'T','y','p','e',0};
344 static const WCHAR imagepathW
[] = {'I','m','a','g','e',' ','P','a','t','h',0};
346 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_w(pszContainer
),
347 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
349 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
351 SetLastError(NTE_BAD_PROV_TYPE
);
357 SetLastError(ERROR_INVALID_PARAMETER
);
361 /* Make sure the MachineGuid value exists */
362 CRYPT_CreateMachineGuid();
364 if (!pszProvider
|| !*pszProvider
)
366 /* No CSP name specified so try the user default CSP first
367 * then try the machine default CSP
369 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)) ) {
370 TRACE("No provider registered for crypto provider type %d.\n", dwProvType
);
371 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
374 if (RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &key
))
377 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
378 TRACE("No type registered for crypto provider type %d.\n", dwProvType
);
380 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
383 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
384 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname
));
387 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
392 r
= RegQueryValueExW(key
, nameW
, NULL
, &keytype
, NULL
, &len
);
393 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
395 TRACE("error %d reading size of 'Name' from registry\n", r
);
397 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
400 if(!(provname
= CRYPT_Alloc(len
)))
403 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
406 r
= RegQueryValueExW(key
, nameW
, NULL
, NULL
, (LPBYTE
)provname
, &len
);
407 if( r
!= ERROR_SUCCESS
)
409 TRACE("error %d reading 'Name' from registry\n", r
);
411 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
416 if ( !(provname
= CRYPT_Alloc((strlenW(pszProvider
) +1)*sizeof(WCHAR
))) )
418 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
421 strcpyW(provname
, pszProvider
);
424 keyname
= CRYPT_GetProvKeyName(provname
);
425 r
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
);
427 if (r
!= ERROR_SUCCESS
)
429 SetLastError(NTE_KEYSET_NOT_DEF
);
433 r
= RegQueryValueExW(key
, typeW
, NULL
, NULL
, (BYTE
*)&type
, &len
);
434 if (r
!= ERROR_SUCCESS
)
436 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
439 if (type
!= dwProvType
)
441 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type
, dwProvType
);
442 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
446 r
= RegQueryValueExW(key
, imagepathW
, NULL
, &keytype
, NULL
, &len
);
447 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
449 TRACE("error %d reading size of 'Image Path' from registry\n", r
);
451 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
454 if (!(temp
= CRYPT_Alloc(len
)))
457 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
460 r
= RegQueryValueExW(key
, imagepathW
, NULL
, NULL
, (LPBYTE
)temp
, &len
);
461 if( r
!= ERROR_SUCCESS
)
463 TRACE("error %d reading 'Image Path' from registry\n", r
);
465 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
469 len
= ExpandEnvironmentStringsW(temp
, NULL
, 0);
470 if ( !(imagepath
= CRYPT_Alloc(len
*sizeof(WCHAR
))) )
472 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
475 if (!ExpandEnvironmentStringsW(temp
, imagepath
, len
))
477 /* ExpandEnvironmentStrings will call SetLastError */
480 pProv
= CRYPT_LoadProvider(imagepath
);
482 /* CRYPT_LoadProvider calls SetLastError */
485 pProv
->pVTable
->dwProvType
= dwProvType
;
486 if(!CRYPT_UnicodeToANSI(provname
, &provnameA
, -1))
488 /* CRYPT_UnicodeToANSI calls SetLastError */
491 pProv
->pVTable
->pszProvName
= provnameA
;
492 if(!CRYPT_UnicodeToANSI(pszContainer
, &pszContainerA
, -1))
494 /* CRYPT_UnicodeToANSI calls SetLastError */
497 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, pszContainerA
, dwFlags
, pProv
->pVTable
))
499 /* MSDN: When this flag is set, the value returned in phProv is undefined,
500 * and thus, the CryptReleaseContext function need not be called afterwards.
501 * Therefore, we must clean up everything now.
503 if (dwFlags
& CRYPT_DELETEKEYSET
)
506 FreeLibrary(pProv
->hModule
);
507 CRYPT_Free(provnameA
);
508 CRYPT_Free(pProv
->pVTable
);
509 CRYPT_Free(pProv
->pFuncs
);
512 *phProv
= (HCRYPTPROV
)pProv
;
514 CRYPT_Free(pszContainerA
);
515 CRYPT_Free(provname
);
517 CRYPT_Free(imagepath
);
520 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
526 FreeLibrary(pProv
->hModule
);
527 CRYPT_Free(pProv
->pVTable
);
528 CRYPT_Free(pProv
->pFuncs
);
531 CRYPT_Free(pszContainerA
);
532 CRYPT_Free(provnameA
);
533 CRYPT_Free(provname
);
535 CRYPT_Free(imagepath
);
539 /******************************************************************************
540 * CryptAcquireContextA (ADVAPI32.@)
542 * See CryptAcquireContextW.
544 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
545 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
547 PWSTR pProvider
= NULL
, pContainer
= NULL
;
550 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, pszContainer
,
551 pszProvider
, dwProvType
, dwFlags
);
553 if ( !CRYPT_ANSIToUnicode(pszContainer
, &pContainer
, -1) )
555 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
558 if ( !CRYPT_ANSIToUnicode(pszProvider
, &pProvider
, -1) )
560 CRYPT_Free(pContainer
);
561 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
565 ret
= CryptAcquireContextW(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
567 CRYPT_Free(pContainer
);
568 CRYPT_Free(pProvider
);
573 /******************************************************************************
574 * CryptContextAddRef (ADVAPI32.@)
576 * Increases reference count of a cryptographic service provider handle
580 * hProv [I] Handle to the CSP whose reference is being incremented.
581 * pdwReserved [IN] Reserved for future use and must be NULL.
582 * dwFlags [I] Reserved for future use and must be NULL.
588 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
590 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
592 TRACE("(0x%lx, %p, %08x)\n", hProv
, pdwReserved
, dwFlags
);
596 SetLastError(NTE_BAD_UID
);
600 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
602 SetLastError(ERROR_INVALID_PARAMETER
);
610 /******************************************************************************
611 * CryptReleaseContext (ADVAPI32.@)
613 * Releases the handle of a CSP. Reference count is decreased.
616 * hProv [I] Handle of a CSP.
617 * dwFlags [I] Reserved for future use and must be NULL.
623 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, ULONG_PTR dwFlags
)
625 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
628 TRACE("(0x%lx, %08lx)\n", hProv
, dwFlags
);
632 SetLastError(NTE_BAD_UID
);
636 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
638 SetLastError(ERROR_INVALID_PARAMETER
);
643 if (pProv
->refcount
<= 0)
645 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
647 FreeLibrary(pProv
->hModule
);
649 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
651 CRYPT_Free(pProv
->pVTable
->pszProvName
);
652 CRYPT_Free(pProv
->pVTable
);
653 CRYPT_Free(pProv
->pFuncs
);
659 /******************************************************************************
660 * CryptGenRandom (ADVAPI32.@)
662 * Fills a buffer with cryptographically random bytes.
665 * hProv [I] Handle of a CSP.
666 * dwLen [I] Number of bytes to generate.
667 * pbBuffer [I/O] Buffer to contain random bytes.
674 * pdBuffer must be at least dwLen bytes long.
676 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
678 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
680 TRACE("(0x%lx, %d, %p)\n", hProv
, dwLen
, pbBuffer
);
684 SetLastError(ERROR_INVALID_HANDLE
);
688 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
690 SetLastError(ERROR_INVALID_PARAMETER
);
694 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
697 /******************************************************************************
698 * CryptCreateHash (ADVAPI32.@)
700 * Initiates the hashing of a stream of data.
703 * hProv [I] Handle of a CSP.
704 * Algid [I] Identifies the hash algorithm to use.
705 * hKey [I] Key for the hash (if required).
706 * dwFlags [I] Reserved for future use and must be NULL.
707 * phHash [O] Address of the future handle to the new hash object.
714 * If the algorithm is a keyed hash, hKey is the key.
716 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
717 DWORD dwFlags
, HCRYPTHASH
*phHash
)
719 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
720 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
723 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
727 SetLastError(ERROR_INVALID_HANDLE
);
730 if (!phHash
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
732 SetLastError(ERROR_INVALID_PARAMETER
);
737 SetLastError(NTE_BAD_FLAGS
);
740 if ( !(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
742 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
746 hash
->pProvider
= prov
;
748 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
749 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
751 *phHash
= (HCRYPTHASH
)hash
;
761 /******************************************************************************
762 * CryptDecrypt (ADVAPI32.@)
764 * Decrypts data encrypted by CryptEncrypt.
767 * hKey [I] Handle to the decryption key.
768 * hHash [I] Handle to a hash object.
769 * Final [I] TRUE if this is the last section to be decrypted.
770 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
771 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
773 * pdwDataLen [I/O] Length of pbData before and after the call.
779 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
780 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
783 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
784 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
786 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
788 if (!key
|| !pbData
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
790 SetLastError(ERROR_INVALID_PARAMETER
);
794 prov
= key
->pProvider
;
795 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
796 Final
, dwFlags
, pbData
, pdwDataLen
);
799 /******************************************************************************
800 * CryptDeriveKey (ADVAPI32.@)
802 * Generates session keys derived from a base data value.
805 * hProv [I] Handle to a CSP.
806 * Algid [I] Identifies the symmetric encryption algorithm to use.
807 * hBaseData [I] Handle to a hash object.
808 * dwFlags [I] Type of key to generate.
809 * phKey [I/O] Address of the newly generated key.
815 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
816 DWORD dwFlags
, HCRYPTKEY
*phKey
)
818 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
819 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
822 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
826 SetLastError(ERROR_INVALID_HANDLE
);
829 if (!phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
831 SetLastError(ERROR_INVALID_PARAMETER
);
834 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
836 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
840 key
->pProvider
= prov
;
841 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
843 *phKey
= (HCRYPTKEY
)key
;
853 /******************************************************************************
854 * CryptDestroyHash (ADVAPI32.@)
856 * Destroys the hash object referenced by hHash.
859 * hHash [I] Handle of the hash object to be destroyed.
865 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
867 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
871 TRACE("(0x%lx)\n", hHash
);
875 SetLastError(ERROR_INVALID_HANDLE
);
879 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
881 SetLastError(ERROR_INVALID_PARAMETER
);
885 prov
= hash
->pProvider
;
886 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
891 /******************************************************************************
892 * CryptDestroyKey (ADVAPI32.@)
894 * Releases the handle referenced by hKey.
897 * hKey [I] Handle of the key to be destroyed.
903 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
905 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
909 TRACE("(0x%lx)\n", hKey
);
913 SetLastError(ERROR_INVALID_HANDLE
);
917 if (!key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
919 SetLastError(ERROR_INVALID_PARAMETER
);
923 prov
= key
->pProvider
;
924 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
929 /******************************************************************************
930 * CryptDuplicateHash (ADVAPI32.@)
935 * hHash [I] Handle to the hash to be copied.
936 * pdwReserved [I] Reserved for future use and must be zero.
937 * dwFlags [I] Reserved for future use and must be zero.
938 * phHash [O] Address of the handle to receive the copy.
944 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
945 DWORD dwFlags
, HCRYPTHASH
*phHash
)
948 PCRYPTHASH orghash
, newhash
;
950 TRACE("(0x%lx, %p, %08x, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
952 orghash
= (PCRYPTHASH
)hHash
;
953 if (!orghash
|| pdwReserved
|| !phHash
|| !orghash
->pProvider
||
954 orghash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
956 SetLastError(ERROR_INVALID_PARAMETER
);
960 prov
= orghash
->pProvider
;
961 if (!prov
->pFuncs
->pCPDuplicateHash
)
963 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
967 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
969 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
973 newhash
->pProvider
= prov
;
974 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
976 *phHash
= (HCRYPTHASH
)newhash
;
983 /******************************************************************************
984 * CryptDuplicateKey (ADVAPI32.@)
986 * Duplicate a key and the key's state.
989 * hKey [I] Handle of the key to copy.
990 * pdwReserved [I] Reserved for future use and must be NULL.
991 * dwFlags [I] Reserved for future use and must be zero.
992 * phKey [I] Address of the handle to the duplicated key.
998 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1001 PCRYPTKEY orgkey
, newkey
;
1003 TRACE("(0x%lx, %p, %08x, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
1005 orgkey
= (PCRYPTKEY
)hKey
;
1006 if (!orgkey
|| pdwReserved
|| !phKey
|| !orgkey
->pProvider
||
1007 orgkey
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1009 SetLastError(ERROR_INVALID_PARAMETER
);
1013 prov
= orgkey
->pProvider
;
1014 if (!prov
->pFuncs
->pCPDuplicateKey
)
1016 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1020 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1022 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1026 newkey
->pProvider
= prov
;
1027 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
1029 *phKey
= (HCRYPTKEY
)newkey
;
1036 /******************************************************************************
1037 * CryptEncrypt (ADVAPI32.@)
1042 * hKey [I] Handle to the encryption key.
1043 * hHash [I] Handle to a hash object.
1044 * Final [I] TRUE if this is the last section to encrypt.
1045 * dwFlags [I] Can be CRYPT_OAEP.
1046 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1047 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1048 * encrypted data after call.
1049 * dwBufLen [I] Length of the input pbData buffer.
1056 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1057 * required for the returned data in pdwDataLen.
1059 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1060 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1063 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1064 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1066 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1068 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1070 SetLastError(ERROR_INVALID_PARAMETER
);
1074 prov
= key
->pProvider
;
1075 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
1076 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1079 /******************************************************************************
1080 * CryptEnumProvidersW (ADVAPI32.@)
1082 * Returns the next available CSP.
1085 * dwIndex [I] Index of the next provider to be enumerated.
1086 * pdwReserved [I] Reserved for future use and must be NULL.
1087 * dwFlags [I] Reserved for future use and must be zero.
1088 * pdwProvType [O] DWORD designating the type of the provider.
1089 * pszProvName [O] Buffer that receives data from the provider.
1090 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1091 * of bytes stored in the buffer on return.
1098 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1099 * for memory allocation purposes.
1101 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
1102 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1105 static const WCHAR providerW
[] = {
1106 'S','o','f','t','w','a','r','e','\\',
1107 'M','i','c','r','o','s','o','f','t','\\',
1108 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1109 'D','e','f','a','u','l','t','s','\\',
1110 'P','r','o','v','i','d','e','r',0
1112 static const WCHAR typeW
[] = {'T','y','p','e',0};
1114 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1115 pdwProvType
, pszProvName
, pcbProvName
);
1117 if (pdwReserved
|| !pcbProvName
)
1119 SetLastError(ERROR_INVALID_PARAMETER
);
1124 SetLastError(NTE_BAD_FLAGS
);
1128 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, providerW
, &hKey
))
1130 SetLastError(NTE_FAIL
);
1139 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1140 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1142 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1144 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1148 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1149 CRYPT_Free(provNameW
);
1151 *pcbProvName
*= sizeof(WCHAR
);
1153 if (dwIndex
>= numkeys
)
1155 SetLastError(ERROR_NO_MORE_ITEMS
);
1159 DWORD size
= sizeof(DWORD
);
1163 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1166 SetLastError(result
);
1169 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1171 if (RegQueryValueExW(subkey
, typeW
, NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1173 RegCloseKey(subkey
);
1179 /******************************************************************************
1180 * CryptEnumProvidersA (ADVAPI32.@)
1182 * See CryptEnumProvidersW.
1184 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1185 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1189 BOOL ret
; /* = FALSE; */
1191 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1192 pdwProvType
, pszProvName
, pcbProvName
);
1194 if(!CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &strlen
))
1196 if ( pszProvName
&& !(str
= CRYPT_Alloc(strlen
)) )
1198 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1201 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1203 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1204 *pcbProvName
= strlen
/ sizeof(WCHAR
); /* FIXME: not correct */
1208 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1210 SetLastError(ERROR_MORE_DATA
);
1217 /******************************************************************************
1218 * CryptEnumProviderTypesW (ADVAPI32.@)
1220 * Retrieves the next type of CSP supported.
1223 * dwIndex [I] Index of the next provider to be enumerated.
1224 * pdwReserved [I] Reserved for future use and must be NULL.
1225 * dwFlags [I] Reserved for future use and must be zero.
1226 * pdwProvType [O] DWORD designating the type of the provider.
1227 * pszTypeName [O] Buffer that receives data from the provider type.
1228 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1229 * of bytes stored in the buffer on return.
1236 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1237 * for memory allocation purposes.
1239 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1240 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1243 DWORD keylen
, numkeys
, dwType
;
1246 static const WCHAR KEYSTR
[] = {
1247 'S','o','f','t','w','a','r','e','\\',
1248 'M','i','c','r','o','s','o','f','t','\\',
1249 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1250 'D','e','f','a','u','l','t','s','\\',
1251 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1253 static const WCHAR typenameW
[] = {'T','y','p','e','N','a','m','e',0};
1255 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1256 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1258 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1260 SetLastError(ERROR_INVALID_PARAMETER
);
1265 SetLastError(NTE_BAD_FLAGS
);
1269 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, KEYSTR
, &hKey
))
1272 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1273 if (dwIndex
>= numkeys
)
1275 SetLastError(ERROR_NO_MORE_ITEMS
);
1279 if ( !(keyname
= CRYPT_Alloc(keylen
*sizeof(WCHAR
))) )
1281 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1284 if ( RegEnumKeyW(hKey
, dwIndex
, keyname
, keylen
) ) {
1285 CRYPT_Free(keyname
);
1288 RegOpenKeyW(hKey
, keyname
, &hSubkey
);
1289 ch
= keyname
+ strlenW(keyname
);
1290 /* Convert "Type 000" to 0, etc/ */
1291 *pdwProvType
= *(--ch
) - '0';
1292 *pdwProvType
+= (*(--ch
) - '0') * 10;
1293 *pdwProvType
+= (*(--ch
) - '0') * 100;
1294 CRYPT_Free(keyname
);
1296 result
= RegQueryValueExW(hSubkey
, typenameW
, NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1299 SetLastError(result
);
1303 RegCloseKey(hSubkey
);
1308 /******************************************************************************
1309 * CryptEnumProviderTypesA (ADVAPI32.@)
1311 * See CryptEnumProviderTypesW.
1313 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1314 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1320 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1321 pdwProvType
, pszTypeName
, pcbTypeName
);
1323 if(!CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &strlen
))
1325 if ( pszTypeName
&& !(str
= CRYPT_Alloc(strlen
)) )
1327 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1330 ret
= CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1332 CRYPT_UnicodeToANSI(str
, &pszTypeName
, *pcbTypeName
);
1333 *pcbTypeName
= strlen
/ sizeof(WCHAR
);
1337 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1339 SetLastError(ERROR_MORE_DATA
);
1346 /******************************************************************************
1347 * CryptExportKey (ADVAPI32.@)
1349 * Exports a cryptographic key from a CSP.
1352 * hKey [I] Handle to the key to export.
1353 * hExpKey [I] Handle to a cryptographic key of the end user.
1354 * dwBlobType [I] Type of BLOB to be exported.
1355 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1356 * pbData [O] Buffer to receive BLOB data.
1357 * pdwDataLen [I/O] Specifies the size of pbData.
1364 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1365 * buffer needed to hold the BLOB.
1367 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1368 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1371 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1373 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1375 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1377 SetLastError(ERROR_INVALID_PARAMETER
);
1381 prov
= key
->pProvider
;
1382 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1383 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1386 /******************************************************************************
1387 * CryptGenKey (ADVAPI32.@)
1389 * Generates a random cryptographic session key or a pub/priv key pair.
1392 * hProv [I] Handle to a CSP.
1393 * Algid [I] Algorithm to use to make key.
1394 * dwFlags [I] Specifies type of key to make.
1395 * phKey [I] Address of the handle to which the new key is copied.
1401 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1403 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1406 TRACE("(0x%lx, %d, %08x, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1410 SetLastError(ERROR_INVALID_HANDLE
);
1413 if (!phKey
|| !prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1415 SetLastError(ERROR_INVALID_PARAMETER
);
1418 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1420 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1424 key
->pProvider
= prov
;
1426 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1428 *phKey
= (HCRYPTKEY
)key
;
1437 /******************************************************************************
1438 * CryptGetDefaultProviderW (ADVAPI32.@)
1440 * Finds the default CSP of a certain provider type.
1443 * dwProvType [I] Provider type to look for.
1444 * pdwReserved [I] Reserved for future use and must be NULL.
1445 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1446 * pszProvName [O] Name of the default CSP.
1447 * pcbProvName [I/O] Size of pszProvName
1454 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1455 * memory allocation purposes on return.
1457 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1458 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1463 static const WCHAR nameW
[] = {'N','a','m','e',0};
1465 if (pdwReserved
|| !pcbProvName
)
1467 SetLastError(ERROR_INVALID_PARAMETER
);
1470 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1472 SetLastError(NTE_BAD_FLAGS
);
1475 if (dwProvType
> 999)
1477 SetLastError(NTE_BAD_PROV_TYPE
);
1480 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1482 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1485 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1487 CRYPT_Free(keyname
);
1488 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
1491 CRYPT_Free(keyname
);
1493 result
= RegQueryValueExW(hKey
, nameW
, NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1496 if (result
!= ERROR_MORE_DATA
)
1497 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1499 SetLastError(result
);
1508 /******************************************************************************
1509 * CryptGetDefaultProviderA (ADVAPI32.@)
1511 * See CryptGetDefaultProviderW.
1513 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1514 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1520 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1522 CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, NULL
, &strlen
);
1523 if ( pszProvName
&& !(str
= CRYPT_Alloc(strlen
)) )
1525 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1528 ret
= CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, str
, &strlen
);
1530 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1531 *pcbProvName
= strlen
/ sizeof(WCHAR
);
1535 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1537 SetLastError(ERROR_MORE_DATA
);
1544 /******************************************************************************
1545 * CryptGetHashParam (ADVAPI32.@)
1547 * Retrieves data that controls the operations of a hash object.
1550 * hHash [I] Handle of the hash object to question.
1551 * dwParam [I] Query type.
1552 * pbData [O] Buffer that receives the value data.
1553 * pdwDataLen [I/O] Size of the pbData buffer.
1554 * dwFlags [I] Reserved for future use and must be zero.
1561 * If pbData is NULL, pdwDataLen will contain the length required.
1563 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1564 DWORD
*pdwDataLen
, DWORD dwFlags
)
1567 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1569 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1571 if (!hash
|| !pdwDataLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1573 SetLastError(ERROR_INVALID_PARAMETER
);
1577 prov
= hash
->pProvider
;
1578 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1579 pbData
, pdwDataLen
, dwFlags
);
1582 /******************************************************************************
1583 * CryptGetKeyParam (ADVAPI32.@)
1585 * Retrieves data that controls the operations of a key.
1588 * hKey [I] Handle to they key in question.
1589 * dwParam [I] Specifies query type.
1590 * pbData [O] Sequence of bytes to receive data.
1591 * pdwDataLen [I/O] Size of pbData.
1592 * dwFlags [I] Reserved for future use and must be zero.
1599 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1601 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1602 DWORD
*pdwDataLen
, DWORD dwFlags
)
1605 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1607 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1609 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1611 SetLastError(ERROR_INVALID_PARAMETER
);
1615 prov
= key
->pProvider
;
1616 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1617 pbData
, pdwDataLen
, dwFlags
);
1620 /******************************************************************************
1621 * CryptGetProvParam (ADVAPI32.@)
1623 * Retrieves parameters that control the operations of a CSP.
1626 * hProv [I] Handle of the CSP in question.
1627 * dwParam [I] Specifies query type.
1628 * pbData [O] Buffer to receive the data.
1629 * pdwDataLen [I/O] Size of pbData.
1630 * dwFlags [I] see MSDN Docs.
1637 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1639 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1640 DWORD
*pdwDataLen
, DWORD dwFlags
)
1642 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1644 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1646 if (!prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1648 SetLastError(ERROR_INVALID_PARAMETER
);
1652 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1655 /******************************************************************************
1656 * CryptGetUserKey (ADVAPI32.@)
1658 * Gets a handle of one of a user's two public/private key pairs.
1661 * hProv [I] Handle of a CSP.
1662 * dwKeySpec [I] Private key to use.
1663 * phUserKey [O] Pointer to the handle of the retrieved keys.
1669 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1671 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1674 TRACE("(0x%lx, %d, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1678 SetLastError(ERROR_INVALID_HANDLE
);
1681 if (!phUserKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1683 SetLastError(ERROR_INVALID_PARAMETER
);
1686 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1688 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1692 key
->pProvider
= prov
;
1694 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1696 *phUserKey
= (HCRYPTKEY
)key
;
1706 /******************************************************************************
1707 * CryptHashData (ADVAPI32.@)
1709 * Adds data to a hash object.
1712 * hHash [I] Handle of the hash object.
1713 * pbData [I] Buffer of data to be hashed.
1714 * dwDataLen [I] Number of bytes to add.
1715 * dwFlags [I] Can be CRYPT_USERDATA
1721 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, const BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1723 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1726 TRACE("(0x%lx, %p, %d, %08x)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1730 SetLastError(ERROR_INVALID_HANDLE
);
1733 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1735 SetLastError(ERROR_INVALID_PARAMETER
);
1739 prov
= hash
->pProvider
;
1740 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1743 /******************************************************************************
1744 * CryptHashSessionKey (ADVAPI32.@)
1746 * Compute the cryptographic hash of a session key object.
1749 * hHash [I] Handle to the hash object.
1750 * hKey [I] Handle to the key to be hashed.
1751 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1757 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1759 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1760 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1763 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash
, hKey
, dwFlags
);
1767 SetLastError(ERROR_INVALID_HANDLE
);
1771 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1773 SetLastError(ERROR_INVALID_PARAMETER
);
1777 prov
= hash
->pProvider
;
1778 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1781 /******************************************************************************
1782 * CryptImportKey (ADVAPI32.@)
1784 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1787 * hProv [I] Handle of a CSP.
1788 * pbData [I] Contains the key to be imported.
1789 * dwDataLen [I] Length of the key.
1790 * hPubKey [I] Cryptographic key that decrypts pdData
1791 * dwFlags [I] Used only with a public/private key pair.
1792 * phKey [O] Imported key.
1798 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
1799 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1801 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1802 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1804 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1806 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1808 SetLastError(ERROR_INVALID_PARAMETER
);
1812 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1814 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1818 importkey
->pProvider
= prov
;
1819 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1820 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1822 *phKey
= (HCRYPTKEY
)importkey
;
1826 CRYPT_Free(importkey
);
1830 /******************************************************************************
1831 * CryptSignHashW (ADVAPI32.@)
1836 * hHash [I] Handle of the hash object to be signed.
1837 * dwKeySpec [I] Private key to use.
1838 * sDescription [I] Should be NULL.
1839 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1840 * pbSignature [O] Buffer of the signature data.
1841 * pdwSigLen [I/O] Size of the pbSignature buffer.
1848 * Because of security flaws sDescription should not be used and should thus be
1849 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1852 BOOL WINAPI
CryptSignHashW (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCWSTR sDescription
,
1853 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1855 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1858 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1859 hHash
, dwKeySpec
, debugstr_w(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1863 SetLastError(ERROR_INVALID_HANDLE
);
1866 if (!pdwSigLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1868 SetLastError(ERROR_INVALID_PARAMETER
);
1872 prov
= hash
->pProvider
;
1873 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, sDescription
,
1874 dwFlags
, pbSignature
, pdwSigLen
);
1877 /******************************************************************************
1878 * CryptSignHashA (ADVAPI32.@)
1880 * See CryptSignHashW.
1882 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1883 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1885 LPWSTR wsDescription
;
1888 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1889 hHash
, dwKeySpec
, debugstr_a(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1891 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1892 result
= CryptSignHashW(hHash
, dwKeySpec
, wsDescription
, dwFlags
, pbSignature
, pdwSigLen
);
1893 CRYPT_Free(wsDescription
);
1898 /******************************************************************************
1899 * CryptSetHashParam (ADVAPI32.@)
1901 * Customizes the operations of a hash object.
1904 * hHash [I] Handle of the hash object to set parameters.
1905 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1906 * pbData [I] Value data buffer.
1907 * dwFlags [I] Reserved for future use and must be zero.
1913 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
1916 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1918 TRACE("(0x%lx, %d, %p, %08x)\n", hHash
, dwParam
, pbData
, dwFlags
);
1920 if (!hash
|| !pbData
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1922 SetLastError(ERROR_INVALID_PARAMETER
);
1926 prov
= hash
->pProvider
;
1927 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1928 dwParam
, pbData
, dwFlags
);
1931 /******************************************************************************
1932 * CryptSetKeyParam (ADVAPI32.@)
1934 * Customizes a session key's operations.
1937 * hKey [I] Handle to the key to set values.
1938 * dwParam [I] See MSDN Doc.
1939 * pbData [I] Buffer of values to set.
1940 * dwFlags [I] Only used when dwParam == KP_ALGID.
1946 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
1949 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1951 TRACE("(0x%lx, %d, %p, %08x)\n", hKey
, dwParam
, pbData
, dwFlags
);
1953 if (!key
|| !pbData
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1955 SetLastError(ERROR_INVALID_PARAMETER
);
1959 prov
= key
->pProvider
;
1960 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
1961 dwParam
, pbData
, dwFlags
);
1964 /******************************************************************************
1965 * CryptSetProviderA (ADVAPI32.@)
1967 * Specifies the current user's default CSP.
1970 * pszProvName [I] Name of the new default CSP.
1971 * dwProvType [I] Provider type of the CSP.
1977 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
1979 TRACE("(%s, %d)\n", pszProvName
, dwProvType
);
1980 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1983 /******************************************************************************
1984 * CryptSetProviderW (ADVAPI32.@)
1986 * See CryptSetProviderA.
1988 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
1990 TRACE("(%s, %d)\n", debugstr_w(pszProvName
), dwProvType
);
1991 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1994 /******************************************************************************
1995 * CryptSetProviderExW (ADVAPI32.@)
1997 * Specifies the default CSP.
2000 * pszProvName [I] Name of the new default CSP.
2001 * dwProvType [I] Provider type of the CSP.
2002 * pdwReserved [I] Reserved for future use and must be NULL.
2003 * dwFlags [I] See MSDN Doc.
2009 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2011 HKEY hProvKey
, hTypeKey
;
2013 static const WCHAR nameW
[] = {'N','a','m','e',0};
2015 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
2017 if (!pszProvName
|| pdwReserved
)
2019 SetLastError(ERROR_INVALID_PARAMETER
);
2022 if (dwProvType
> MAXPROVTYPES
)
2024 SetLastError(NTE_BAD_PROV_TYPE
);
2027 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
2028 || dwFlags
== CRYPT_DELETE_DEFAULT
)
2030 SetLastError(NTE_BAD_FLAGS
);
2034 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
2036 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2039 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2040 keyname
, &hTypeKey
))
2042 CRYPT_Free(keyname
);
2043 SetLastError(NTE_BAD_PROVIDER
);
2046 CRYPT_Free(keyname
);
2048 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
2050 RegDeleteValueW(hTypeKey
, nameW
);
2054 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
2056 RegCloseKey(hTypeKey
);
2057 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2060 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2061 keyname
, &hProvKey
))
2063 CRYPT_Free(keyname
);
2064 RegCloseKey(hTypeKey
);
2065 SetLastError(NTE_BAD_PROVIDER
);
2068 CRYPT_Free(keyname
);
2070 if (RegSetValueExW(hTypeKey
, nameW
, 0, REG_SZ
, (const BYTE
*)pszProvName
,
2071 (strlenW(pszProvName
) + 1)*sizeof(WCHAR
)))
2073 RegCloseKey(hTypeKey
);
2074 RegCloseKey(hProvKey
);
2078 RegCloseKey(hProvKey
);
2080 RegCloseKey(hTypeKey
);
2085 /******************************************************************************
2086 * CryptSetProviderExA (ADVAPI32.@)
2088 * See CryptSetProviderExW.
2090 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2095 TRACE("(%s, %d, %p, %08x)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
2097 if (CRYPT_ANSIToUnicode(pszProvName
, &str
, -1))
2099 ret
= CryptSetProviderExW(str
, dwProvType
, pdwReserved
, dwFlags
);
2105 /******************************************************************************
2106 * CryptSetProvParam (ADVAPI32.@)
2108 * Customizes the operations of a CSP.
2111 * hProv [I] Handle of a CSP.
2112 * dwParam [I] See MSDN Doc.
2113 * pbData [I] Buffer that contains a value to set as a parameter.
2114 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2120 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
2122 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
2124 TRACE("(0x%lx, %d, %p, %08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
2128 SetLastError(ERROR_INVALID_HANDLE
);
2131 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
2133 SetLastError(ERROR_INVALID_PARAMETER
);
2136 if (dwFlags
& PP_USE_HARDWARE_RNG
)
2138 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2139 FIXME("\tLetting the CSP decide.\n");
2141 if (dwFlags
& PP_CLIENT_HWND
)
2143 /* FIXME: Should verify the parameter */
2144 if (pbData
/* && IsWindow((HWND)pbData) */)
2146 crypt_hWindow
= (HWND
)(pbData
);
2149 SetLastError(ERROR_INVALID_PARAMETER
);
2153 /* All other flags go to the CSP */
2154 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
2157 /******************************************************************************
2158 * CryptVerifySignatureW (ADVAPI32.@)
2160 * Verifies the signature of a hash object.
2163 * hHash [I] Handle of the hash object to verify.
2164 * pbSignature [I] Signature data to verify.
2165 * dwSigLen [I] Size of pbSignature.
2166 * hPubKey [I] Handle to the public key to authenticate signature.
2167 * sDescription [I] Should be NULL.
2168 * dwFlags [I] See MSDN doc.
2175 * Because of security flaws sDescription should not be used and should thus be
2176 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2179 BOOL WINAPI
CryptVerifySignatureW (HCRYPTHASH hHash
, CONST BYTE
*pbSignature
, DWORD dwSigLen
,
2180 HCRYPTKEY hPubKey
, LPCWSTR sDescription
, DWORD dwFlags
)
2182 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
2183 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
2186 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2187 dwSigLen
, hPubKey
, debugstr_w(sDescription
), dwFlags
);
2189 if (!hash
|| !key
||
2190 !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
||
2191 !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
2193 SetLastError(ERROR_INVALID_PARAMETER
);
2197 prov
= hash
->pProvider
;
2198 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
2199 key
->hPrivate
, sDescription
, dwFlags
);
2202 /******************************************************************************
2203 * CryptVerifySignatureA (ADVAPI32.@)
2205 * See CryptVerifySignatureW.
2207 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, CONST BYTE
*pbSignature
, DWORD dwSigLen
,
2208 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
2210 LPWSTR wsDescription
;
2213 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2214 dwSigLen
, hPubKey
, debugstr_a(sDescription
), dwFlags
);
2216 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
2217 result
= CryptVerifySignatureW(hHash
, pbSignature
, dwSigLen
, hPubKey
, wsDescription
, dwFlags
);
2218 CRYPT_Free(wsDescription
);
2223 /******************************************************************************
2224 * SystemFunction030 (ADVAPI32.@)
2226 * Tests if two blocks of 16 bytes are equal
2229 * b1,b2 [I] block of 16 bytes
2232 * TRUE if blocks are the same
2233 * FALSE if blocks are different
2235 BOOL WINAPI
SystemFunction030(PVOID b1
, PVOID b2
)
2237 return !memcmp(b1
, b2
, 0x10);
2240 /******************************************************************************
2241 * SystemFunction035 (ADVAPI32.@)
2244 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2247 * Stub, always return TRUE.
2249 BOOL WINAPI
SystemFunction035(LPCSTR lpszDllFilePath
)
2251 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath
));
2255 /******************************************************************************
2256 * SystemFunction036 (ADVAPI32.@)
2258 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2261 * pbBufer [O] Pointer to memory to receive random bytes.
2262 * dwLen [I] Number of random bytes to fetch.
2269 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
2273 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
2274 * quality for cryptographic applications. /dev/random is much better,
2275 * but it blocks if the kernel has not yet collected enough entropy for
2276 * the request, which will suspend the calling thread for an indefinite
2277 * amount of time. */
2278 dev_random
= open("/dev/urandom", O_RDONLY
);
2279 if (dev_random
!= -1)
2281 if (read(dev_random
, pbBuffer
, dwLen
) == (ssize_t
)dwLen
)
2288 SetLastError(NTE_FAIL
);
2293 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2297 /******************************************************************************
2298 * SystemFunction040 (ADVAPI32.@)
2300 * MSDN documents this function as RtlEncryptMemory.
2303 * memory [I/O] Pointer to memory to encrypt.
2304 * length [I] Length of region to encrypt in bytes.
2305 * flags [I] Control whether other processes are able to decrypt the memory.
2306 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2307 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2308 * RTL_ENCRYPT_OPTION_SAME_LOGON
2311 * Success: STATUS_SUCCESS
2312 * Failure: NTSTATUS error code
2315 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2316 * If flags are specified when encrypting, the same flag value must be given
2317 * when decrypting the memory.
2319 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
)
2321 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
2322 return STATUS_SUCCESS
;
2325 /******************************************************************************
2326 * SystemFunction041 (ADVAPI32.@)
2328 * MSDN documents this function as RtlDecryptMemory.
2331 * memory [I/O] Pointer to memory to decrypt.
2332 * length [I] Length of region to decrypt in bytes.
2333 * flags [I] Control whether other processes are able to decrypt the memory.
2334 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2335 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2336 * RTL_ENCRYPT_OPTION_SAME_LOGON
2339 * Success: STATUS_SUCCESS
2340 * Failure: NTSTATUS error code
2343 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2344 * If flags are specified when encrypting, the same flag value must be given
2345 * when decrypting the memory.
2347 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
)
2349 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
2350 return STATUS_SUCCESS
;