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 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
*);
300 static const WCHAR uuidFmt
[] = {
301 '%','0','8','x','-','%','0','4','x','-',
302 '%','0','4','x','-','%','0','2','x',
303 '%','0','2','x','-','%','0','2','x',
304 '%','0','2','x','%','0','2','x',
305 '%','0','2','x','%','0','2','x',
308 pUuidCreate
= (void *)GetProcAddress(lib
, "UuidCreate");
309 rs
= pUuidCreate(&uuid
);
312 sprintfW(buf
, uuidFmt
,
313 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
314 uuid
.Data4
[0], uuid
.Data4
[1],
315 uuid
.Data4
[2], uuid
.Data4
[3],
316 uuid
.Data4
[4], uuid
.Data4
[5],
317 uuid
.Data4
[6], uuid
.Data4
[7] );
318 RegSetValueExW(key
, machineGuidW
, 0, REG_SZ
,
320 (lstrlenW(buf
)+1)*sizeof(WCHAR
));
329 /******************************************************************************
330 * CryptAcquireContextW (ADVAPI32.@)
332 * Acquire a crypto provider context handle.
335 * phProv [O] Pointer to HCRYPTPROV for the output.
336 * pszContainer [I] Key Container Name
337 * pszProvider [I] Cryptographic Service Provider Name
338 * dwProvType [I] Crypto provider type to get a handle.
339 * dwFlags [I] flags for the operation
342 * TRUE on success, FALSE on failure.
344 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
345 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
347 PCRYPTPROV pProv
= NULL
;
349 PWSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
350 PSTR provnameA
= NULL
, pszContainerA
= NULL
;
351 DWORD keytype
, type
, len
;
353 static const WCHAR nameW
[] = {'N','a','m','e',0};
354 static const WCHAR typeW
[] = {'T','y','p','e',0};
355 static const WCHAR imagepathW
[] = {'I','m','a','g','e',' ','P','a','t','h',0};
357 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_w(pszContainer
),
358 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
360 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
362 SetLastError(NTE_BAD_PROV_TYPE
);
368 SetLastError(ERROR_INVALID_PARAMETER
);
372 /* Make sure the MachineGuid value exists */
373 CRYPT_CreateMachineGuid();
375 if (!pszProvider
|| !*pszProvider
)
377 /* No CSP name specified so try the user default CSP first
378 * then try the machine default CSP
380 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)) ) {
381 TRACE("No provider registered for crypto provider type %d.\n", dwProvType
);
382 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
385 if (RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &key
))
388 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
389 TRACE("No type registered for crypto provider type %d.\n", dwProvType
);
391 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
394 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
395 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname
));
398 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
403 r
= RegQueryValueExW(key
, nameW
, NULL
, &keytype
, NULL
, &len
);
404 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
406 TRACE("error %d reading size of 'Name' from registry\n", r
);
408 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
411 if(!(provname
= CRYPT_Alloc(len
)))
414 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
417 r
= RegQueryValueExW(key
, nameW
, NULL
, NULL
, (LPBYTE
)provname
, &len
);
418 if( r
!= ERROR_SUCCESS
)
420 TRACE("error %d reading 'Name' from registry\n", r
);
422 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
427 if ( !(provname
= CRYPT_Alloc((strlenW(pszProvider
) +1)*sizeof(WCHAR
))) )
429 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
432 strcpyW(provname
, pszProvider
);
435 keyname
= CRYPT_GetProvKeyName(provname
);
436 r
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
);
438 if (r
!= ERROR_SUCCESS
)
440 SetLastError(NTE_KEYSET_NOT_DEF
);
444 r
= RegQueryValueExW(key
, typeW
, NULL
, NULL
, (BYTE
*)&type
, &len
);
445 if (r
!= ERROR_SUCCESS
)
447 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
450 if (type
!= dwProvType
)
452 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type
, dwProvType
);
453 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
457 r
= RegQueryValueExW(key
, imagepathW
, NULL
, &keytype
, NULL
, &len
);
458 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
460 TRACE("error %d reading size of 'Image Path' from registry\n", r
);
462 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
465 if (!(temp
= CRYPT_Alloc(len
)))
468 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
471 r
= RegQueryValueExW(key
, imagepathW
, NULL
, NULL
, (LPBYTE
)temp
, &len
);
472 if( r
!= ERROR_SUCCESS
)
474 TRACE("error %d reading 'Image Path' from registry\n", r
);
476 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
480 len
= ExpandEnvironmentStringsW(temp
, NULL
, 0);
481 if ( !(imagepath
= CRYPT_Alloc(len
*sizeof(WCHAR
))) )
483 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
486 if (!ExpandEnvironmentStringsW(temp
, imagepath
, len
))
488 /* ExpandEnvironmentStrings will call SetLastError */
491 pProv
= CRYPT_LoadProvider(imagepath
);
493 /* CRYPT_LoadProvider calls SetLastError */
496 pProv
->pVTable
->dwProvType
= dwProvType
;
497 if(!CRYPT_UnicodeToANSI(provname
, &provnameA
, -1))
499 /* CRYPT_UnicodeToANSI calls SetLastError */
502 pProv
->pVTable
->pszProvName
= provnameA
;
503 if(!CRYPT_UnicodeToANSI(pszContainer
, &pszContainerA
, -1))
505 /* CRYPT_UnicodeToANSI calls SetLastError */
508 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, pszContainerA
, dwFlags
, pProv
->pVTable
))
510 /* MSDN: When this flag is set, the value returned in phProv is undefined,
511 * and thus, the CryptReleaseContext function need not be called afterwards.
512 * Therefore, we must clean up everything now.
514 if (dwFlags
& CRYPT_DELETEKEYSET
)
517 FreeLibrary(pProv
->hModule
);
518 CRYPT_Free(provnameA
);
519 CRYPT_Free(pProv
->pVTable
);
520 CRYPT_Free(pProv
->pFuncs
);
523 *phProv
= (HCRYPTPROV
)pProv
;
525 CRYPT_Free(pszContainerA
);
526 CRYPT_Free(provname
);
528 CRYPT_Free(imagepath
);
531 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
537 FreeLibrary(pProv
->hModule
);
538 CRYPT_Free(pProv
->pVTable
);
539 CRYPT_Free(pProv
->pFuncs
);
542 CRYPT_Free(pszContainerA
);
543 CRYPT_Free(provnameA
);
544 CRYPT_Free(provname
);
546 CRYPT_Free(imagepath
);
550 /******************************************************************************
551 * CryptAcquireContextA (ADVAPI32.@)
553 * See CryptAcquireContextW.
555 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
556 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
558 PWSTR pProvider
= NULL
, pContainer
= NULL
;
561 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, pszContainer
,
562 pszProvider
, dwProvType
, dwFlags
);
564 if ( !CRYPT_ANSIToUnicode(pszContainer
, &pContainer
, -1) )
566 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
569 if ( !CRYPT_ANSIToUnicode(pszProvider
, &pProvider
, -1) )
571 CRYPT_Free(pContainer
);
572 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
576 ret
= CryptAcquireContextW(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
578 CRYPT_Free(pContainer
);
579 CRYPT_Free(pProvider
);
584 /******************************************************************************
585 * CryptContextAddRef (ADVAPI32.@)
587 * Increases reference count of a cryptographic service provider handle
591 * hProv [I] Handle to the CSP whose reference is being incremented.
592 * pdwReserved [IN] Reserved for future use and must be NULL.
593 * dwFlags [I] Reserved for future use and must be NULL.
599 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
601 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
603 TRACE("(0x%lx, %p, %08x)\n", hProv
, pdwReserved
, dwFlags
);
607 SetLastError(NTE_BAD_UID
);
611 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
613 SetLastError(ERROR_INVALID_PARAMETER
);
621 /******************************************************************************
622 * CryptReleaseContext (ADVAPI32.@)
624 * Releases the handle of a CSP. Reference count is decreased.
627 * hProv [I] Handle of a CSP.
628 * dwFlags [I] Reserved for future use and must be NULL.
634 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, ULONG_PTR dwFlags
)
636 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
639 TRACE("(0x%lx, %08lx)\n", hProv
, dwFlags
);
643 SetLastError(NTE_BAD_UID
);
647 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
649 SetLastError(ERROR_INVALID_PARAMETER
);
654 if (pProv
->refcount
<= 0)
656 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
658 FreeLibrary(pProv
->hModule
);
660 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
662 CRYPT_Free(pProv
->pVTable
->pszProvName
);
663 CRYPT_Free(pProv
->pVTable
);
664 CRYPT_Free(pProv
->pFuncs
);
670 /******************************************************************************
671 * CryptGenRandom (ADVAPI32.@)
673 * Fills a buffer with cryptographically random bytes.
676 * hProv [I] Handle of a CSP.
677 * dwLen [I] Number of bytes to generate.
678 * pbBuffer [I/O] Buffer to contain random bytes.
685 * pdBuffer must be at least dwLen bytes long.
687 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
689 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
691 TRACE("(0x%lx, %d, %p)\n", hProv
, dwLen
, pbBuffer
);
695 SetLastError(ERROR_INVALID_HANDLE
);
699 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
701 SetLastError(ERROR_INVALID_PARAMETER
);
705 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
708 /******************************************************************************
709 * CryptCreateHash (ADVAPI32.@)
711 * Initiates the hashing of a stream of data.
714 * hProv [I] Handle of a CSP.
715 * Algid [I] Identifies the hash algorithm to use.
716 * hKey [I] Key for the hash (if required).
717 * dwFlags [I] Reserved for future use and must be NULL.
718 * phHash [O] Address of the future handle to the new hash object.
725 * If the algorithm is a keyed hash, hKey is the key.
727 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
728 DWORD dwFlags
, HCRYPTHASH
*phHash
)
730 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
731 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
734 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
738 SetLastError(ERROR_INVALID_HANDLE
);
741 if (!phHash
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
743 SetLastError(ERROR_INVALID_PARAMETER
);
748 SetLastError(NTE_BAD_FLAGS
);
751 if ( !(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
753 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
757 hash
->pProvider
= prov
;
759 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
760 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
762 *phHash
= (HCRYPTHASH
)hash
;
772 /******************************************************************************
773 * CryptDecrypt (ADVAPI32.@)
775 * Decrypts data encrypted by CryptEncrypt.
778 * hKey [I] Handle to the decryption key.
779 * hHash [I] Handle to a hash object.
780 * Final [I] TRUE if this is the last section to be decrypted.
781 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
782 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
784 * pdwDataLen [I/O] Length of pbData before and after the call.
790 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
791 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
794 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
795 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
797 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
799 if (!key
|| !pbData
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
801 SetLastError(ERROR_INVALID_PARAMETER
);
805 prov
= key
->pProvider
;
806 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
807 Final
, dwFlags
, pbData
, pdwDataLen
);
810 /******************************************************************************
811 * CryptDeriveKey (ADVAPI32.@)
813 * Generates session keys derived from a base data value.
816 * hProv [I] Handle to a CSP.
817 * Algid [I] Identifies the symmetric encryption algorithm to use.
818 * hBaseData [I] Handle to a hash object.
819 * dwFlags [I] Type of key to generate.
820 * phKey [I/O] Address of the newly generated key.
826 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
827 DWORD dwFlags
, HCRYPTKEY
*phKey
)
829 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
830 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
833 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
837 SetLastError(ERROR_INVALID_HANDLE
);
840 if (!phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
842 SetLastError(ERROR_INVALID_PARAMETER
);
845 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
847 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
851 key
->pProvider
= prov
;
852 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
854 *phKey
= (HCRYPTKEY
)key
;
864 /******************************************************************************
865 * CryptDestroyHash (ADVAPI32.@)
867 * Destroys the hash object referenced by hHash.
870 * hHash [I] Handle of the hash object to be destroyed.
876 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
878 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
882 TRACE("(0x%lx)\n", hHash
);
886 SetLastError(ERROR_INVALID_HANDLE
);
890 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
892 SetLastError(ERROR_INVALID_PARAMETER
);
896 prov
= hash
->pProvider
;
897 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
902 /******************************************************************************
903 * CryptDestroyKey (ADVAPI32.@)
905 * Releases the handle referenced by hKey.
908 * hKey [I] Handle of the key to be destroyed.
914 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
916 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
920 TRACE("(0x%lx)\n", hKey
);
924 SetLastError(ERROR_INVALID_HANDLE
);
928 if (!key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
930 SetLastError(ERROR_INVALID_PARAMETER
);
934 prov
= key
->pProvider
;
935 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
940 /******************************************************************************
941 * CryptDuplicateHash (ADVAPI32.@)
946 * hHash [I] Handle to the hash to be copied.
947 * pdwReserved [I] Reserved for future use and must be zero.
948 * dwFlags [I] Reserved for future use and must be zero.
949 * phHash [O] Address of the handle to receive the copy.
955 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
956 DWORD dwFlags
, HCRYPTHASH
*phHash
)
959 PCRYPTHASH orghash
, newhash
;
961 TRACE("(0x%lx, %p, %08x, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
963 orghash
= (PCRYPTHASH
)hHash
;
964 if (!orghash
|| pdwReserved
|| !phHash
|| !orghash
->pProvider
||
965 orghash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
967 SetLastError(ERROR_INVALID_PARAMETER
);
971 prov
= orghash
->pProvider
;
972 if (!prov
->pFuncs
->pCPDuplicateHash
)
974 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
978 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
980 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
984 newhash
->pProvider
= prov
;
985 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
987 *phHash
= (HCRYPTHASH
)newhash
;
994 /******************************************************************************
995 * CryptDuplicateKey (ADVAPI32.@)
997 * Duplicate a key and the key's state.
1000 * hKey [I] Handle of the key to copy.
1001 * pdwReserved [I] Reserved for future use and must be NULL.
1002 * dwFlags [I] Reserved for future use and must be zero.
1003 * phKey [I] Address of the handle to the duplicated key.
1009 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1012 PCRYPTKEY orgkey
, newkey
;
1014 TRACE("(0x%lx, %p, %08x, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
1016 orgkey
= (PCRYPTKEY
)hKey
;
1017 if (!orgkey
|| pdwReserved
|| !phKey
|| !orgkey
->pProvider
||
1018 orgkey
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1020 SetLastError(ERROR_INVALID_PARAMETER
);
1024 prov
= orgkey
->pProvider
;
1025 if (!prov
->pFuncs
->pCPDuplicateKey
)
1027 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1031 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1033 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1037 newkey
->pProvider
= prov
;
1038 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
1040 *phKey
= (HCRYPTKEY
)newkey
;
1047 /******************************************************************************
1048 * CryptEncrypt (ADVAPI32.@)
1053 * hKey [I] Handle to the encryption key.
1054 * hHash [I] Handle to a hash object.
1055 * Final [I] TRUE if this is the last section to encrypt.
1056 * dwFlags [I] Can be CRYPT_OAEP.
1057 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1058 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1059 * encrypted data after call.
1060 * dwBufLen [I] Length of the input pbData buffer.
1067 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1068 * required for the returned data in pdwDataLen.
1070 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1071 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1074 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1075 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1077 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1079 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1081 SetLastError(ERROR_INVALID_PARAMETER
);
1085 prov
= key
->pProvider
;
1086 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
1087 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1090 /******************************************************************************
1091 * CryptEnumProvidersW (ADVAPI32.@)
1093 * Returns the next available CSP.
1096 * dwIndex [I] Index of the next provider to be enumerated.
1097 * pdwReserved [I] Reserved for future use and must be NULL.
1098 * dwFlags [I] Reserved for future use and must be zero.
1099 * pdwProvType [O] DWORD designating the type of the provider.
1100 * pszProvName [O] Buffer that receives data from the provider.
1101 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1102 * of bytes stored in the buffer on return.
1109 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1110 * for memory allocation purposes.
1112 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
1113 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1116 static const WCHAR providerW
[] = {
1117 'S','o','f','t','w','a','r','e','\\',
1118 'M','i','c','r','o','s','o','f','t','\\',
1119 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1120 'D','e','f','a','u','l','t','s','\\',
1121 'P','r','o','v','i','d','e','r',0
1123 static const WCHAR typeW
[] = {'T','y','p','e',0};
1125 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1126 pdwProvType
, pszProvName
, pcbProvName
);
1128 if (pdwReserved
|| !pcbProvName
)
1130 SetLastError(ERROR_INVALID_PARAMETER
);
1135 SetLastError(NTE_BAD_FLAGS
);
1139 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, providerW
, &hKey
))
1141 SetLastError(NTE_FAIL
);
1150 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1151 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1153 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1155 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1159 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1160 CRYPT_Free(provNameW
);
1162 *pcbProvName
*= sizeof(WCHAR
);
1164 if (dwIndex
>= numkeys
)
1166 SetLastError(ERROR_NO_MORE_ITEMS
);
1170 DWORD size
= sizeof(DWORD
);
1174 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1177 SetLastError(result
);
1180 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1182 if (RegQueryValueExW(subkey
, typeW
, NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1184 RegCloseKey(subkey
);
1190 /******************************************************************************
1191 * CryptEnumProvidersA (ADVAPI32.@)
1193 * See CryptEnumProvidersW.
1195 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1196 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1200 BOOL ret
; /* = FALSE; */
1202 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1203 pdwProvType
, pszProvName
, pcbProvName
);
1205 if(!CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1207 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1209 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1212 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1214 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1215 *pcbProvName
= bufsize
/ sizeof(WCHAR
); /* FIXME: not correct */
1219 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1221 SetLastError(ERROR_MORE_DATA
);
1228 /******************************************************************************
1229 * CryptEnumProviderTypesW (ADVAPI32.@)
1231 * Retrieves the next type of CSP supported.
1234 * dwIndex [I] Index of the next provider to be enumerated.
1235 * pdwReserved [I] Reserved for future use and must be NULL.
1236 * dwFlags [I] Reserved for future use and must be zero.
1237 * pdwProvType [O] DWORD designating the type of the provider.
1238 * pszTypeName [O] Buffer that receives data from the provider type.
1239 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1240 * of bytes stored in the buffer on return.
1247 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1248 * for memory allocation purposes.
1250 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1251 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1254 DWORD keylen
, numkeys
, dwType
;
1257 static const WCHAR KEYSTR
[] = {
1258 'S','o','f','t','w','a','r','e','\\',
1259 'M','i','c','r','o','s','o','f','t','\\',
1260 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1261 'D','e','f','a','u','l','t','s','\\',
1262 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1264 static const WCHAR typenameW
[] = {'T','y','p','e','N','a','m','e',0};
1266 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1267 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1269 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1271 SetLastError(ERROR_INVALID_PARAMETER
);
1276 SetLastError(NTE_BAD_FLAGS
);
1280 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, KEYSTR
, &hKey
))
1283 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1284 if (dwIndex
>= numkeys
)
1286 SetLastError(ERROR_NO_MORE_ITEMS
);
1290 if ( !(keyname
= CRYPT_Alloc(keylen
*sizeof(WCHAR
))) )
1292 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1295 if ( RegEnumKeyW(hKey
, dwIndex
, keyname
, keylen
) ) {
1296 CRYPT_Free(keyname
);
1299 RegOpenKeyW(hKey
, keyname
, &hSubkey
);
1300 ch
= keyname
+ strlenW(keyname
);
1301 /* Convert "Type 000" to 0, etc/ */
1302 *pdwProvType
= *(--ch
) - '0';
1303 *pdwProvType
+= (*(--ch
) - '0') * 10;
1304 *pdwProvType
+= (*(--ch
) - '0') * 100;
1305 CRYPT_Free(keyname
);
1307 result
= RegQueryValueExW(hSubkey
, typenameW
, NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1310 SetLastError(result
);
1314 RegCloseKey(hSubkey
);
1319 /******************************************************************************
1320 * CryptEnumProviderTypesA (ADVAPI32.@)
1322 * See CryptEnumProviderTypesW.
1324 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1325 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1331 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1332 pdwProvType
, pszTypeName
, pcbTypeName
);
1334 if(!CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1336 if ( pszTypeName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1338 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1341 ret
= CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1343 CRYPT_UnicodeToANSI(str
, &pszTypeName
, *pcbTypeName
);
1344 *pcbTypeName
= bufsize
/ sizeof(WCHAR
);
1348 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1350 SetLastError(ERROR_MORE_DATA
);
1357 /******************************************************************************
1358 * CryptExportKey (ADVAPI32.@)
1360 * Exports a cryptographic key from a CSP.
1363 * hKey [I] Handle to the key to export.
1364 * hExpKey [I] Handle to a cryptographic key of the end user.
1365 * dwBlobType [I] Type of BLOB to be exported.
1366 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1367 * pbData [O] Buffer to receive BLOB data.
1368 * pdwDataLen [I/O] Specifies the size of pbData.
1375 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1376 * buffer needed to hold the BLOB.
1378 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1379 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1382 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1384 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1386 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1388 SetLastError(ERROR_INVALID_PARAMETER
);
1392 prov
= key
->pProvider
;
1393 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1394 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1397 /******************************************************************************
1398 * CryptGenKey (ADVAPI32.@)
1400 * Generates a random cryptographic session key or a pub/priv key pair.
1403 * hProv [I] Handle to a CSP.
1404 * Algid [I] Algorithm to use to make key.
1405 * dwFlags [I] Specifies type of key to make.
1406 * phKey [I] Address of the handle to which the new key is copied.
1412 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1414 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1417 TRACE("(0x%lx, %d, %08x, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1421 SetLastError(ERROR_INVALID_HANDLE
);
1424 if (!phKey
|| !prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1426 SetLastError(ERROR_INVALID_PARAMETER
);
1429 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1431 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1435 key
->pProvider
= prov
;
1437 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1439 *phKey
= (HCRYPTKEY
)key
;
1448 /******************************************************************************
1449 * CryptGetDefaultProviderW (ADVAPI32.@)
1451 * Finds the default CSP of a certain provider type.
1454 * dwProvType [I] Provider type to look for.
1455 * pdwReserved [I] Reserved for future use and must be NULL.
1456 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1457 * pszProvName [O] Name of the default CSP.
1458 * pcbProvName [I/O] Size of pszProvName
1465 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1466 * memory allocation purposes on return.
1468 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1469 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1474 static const WCHAR nameW
[] = {'N','a','m','e',0};
1476 if (pdwReserved
|| !pcbProvName
)
1478 SetLastError(ERROR_INVALID_PARAMETER
);
1481 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1483 SetLastError(NTE_BAD_FLAGS
);
1486 if (dwProvType
> 999)
1488 SetLastError(NTE_BAD_PROV_TYPE
);
1491 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1493 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1496 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1498 CRYPT_Free(keyname
);
1499 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
1502 CRYPT_Free(keyname
);
1504 result
= RegQueryValueExW(hKey
, nameW
, NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1507 if (result
!= ERROR_MORE_DATA
)
1508 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1510 SetLastError(result
);
1519 /******************************************************************************
1520 * CryptGetDefaultProviderA (ADVAPI32.@)
1522 * See CryptGetDefaultProviderW.
1524 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1525 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1531 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1533 CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, NULL
, &bufsize
);
1534 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1536 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1539 ret
= CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, str
, &bufsize
);
1541 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1542 *pcbProvName
= bufsize
/ sizeof(WCHAR
);
1546 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1548 SetLastError(ERROR_MORE_DATA
);
1555 /******************************************************************************
1556 * CryptGetHashParam (ADVAPI32.@)
1558 * Retrieves data that controls the operations of a hash object.
1561 * hHash [I] Handle of the hash object to question.
1562 * dwParam [I] Query type.
1563 * pbData [O] Buffer that receives the value data.
1564 * pdwDataLen [I/O] Size of the pbData buffer.
1565 * dwFlags [I] Reserved for future use and must be zero.
1572 * If pbData is NULL, pdwDataLen will contain the length required.
1574 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1575 DWORD
*pdwDataLen
, DWORD dwFlags
)
1578 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1580 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1582 if (!hash
|| !pdwDataLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1584 SetLastError(ERROR_INVALID_PARAMETER
);
1588 prov
= hash
->pProvider
;
1589 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1590 pbData
, pdwDataLen
, dwFlags
);
1593 /******************************************************************************
1594 * CryptGetKeyParam (ADVAPI32.@)
1596 * Retrieves data that controls the operations of a key.
1599 * hKey [I] Handle to they key in question.
1600 * dwParam [I] Specifies query type.
1601 * pbData [O] Sequence of bytes to receive data.
1602 * pdwDataLen [I/O] Size of pbData.
1603 * dwFlags [I] Reserved for future use and must be zero.
1610 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1612 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1613 DWORD
*pdwDataLen
, DWORD dwFlags
)
1616 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1618 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1620 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1622 SetLastError(ERROR_INVALID_PARAMETER
);
1626 prov
= key
->pProvider
;
1627 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1628 pbData
, pdwDataLen
, dwFlags
);
1631 /******************************************************************************
1632 * CryptGetProvParam (ADVAPI32.@)
1634 * Retrieves parameters that control the operations of a CSP.
1637 * hProv [I] Handle of the CSP in question.
1638 * dwParam [I] Specifies query type.
1639 * pbData [O] Buffer to receive the data.
1640 * pdwDataLen [I/O] Size of pbData.
1641 * dwFlags [I] see MSDN Docs.
1648 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1650 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1651 DWORD
*pdwDataLen
, DWORD dwFlags
)
1653 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1655 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1657 if (!prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1659 SetLastError(ERROR_INVALID_PARAMETER
);
1663 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1666 /******************************************************************************
1667 * CryptGetUserKey (ADVAPI32.@)
1669 * Gets a handle of one of a user's two public/private key pairs.
1672 * hProv [I] Handle of a CSP.
1673 * dwKeySpec [I] Private key to use.
1674 * phUserKey [O] Pointer to the handle of the retrieved keys.
1680 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1682 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1685 TRACE("(0x%lx, %d, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1689 SetLastError(ERROR_INVALID_HANDLE
);
1692 if (!phUserKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1694 SetLastError(ERROR_INVALID_PARAMETER
);
1697 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1699 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1703 key
->pProvider
= prov
;
1705 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1707 *phUserKey
= (HCRYPTKEY
)key
;
1717 /******************************************************************************
1718 * CryptHashData (ADVAPI32.@)
1720 * Adds data to a hash object.
1723 * hHash [I] Handle of the hash object.
1724 * pbData [I] Buffer of data to be hashed.
1725 * dwDataLen [I] Number of bytes to add.
1726 * dwFlags [I] Can be CRYPT_USERDATA
1732 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, const BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1734 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1737 TRACE("(0x%lx, %p, %d, %08x)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1741 SetLastError(ERROR_INVALID_HANDLE
);
1744 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1746 SetLastError(ERROR_INVALID_PARAMETER
);
1750 prov
= hash
->pProvider
;
1751 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1754 /******************************************************************************
1755 * CryptHashSessionKey (ADVAPI32.@)
1757 * Compute the cryptographic hash of a session key object.
1760 * hHash [I] Handle to the hash object.
1761 * hKey [I] Handle to the key to be hashed.
1762 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1768 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1770 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1771 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1774 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash
, hKey
, dwFlags
);
1778 SetLastError(ERROR_INVALID_HANDLE
);
1782 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1784 SetLastError(ERROR_INVALID_PARAMETER
);
1788 prov
= hash
->pProvider
;
1789 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1792 /******************************************************************************
1793 * CryptImportKey (ADVAPI32.@)
1795 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1798 * hProv [I] Handle of a CSP.
1799 * pbData [I] Contains the key to be imported.
1800 * dwDataLen [I] Length of the key.
1801 * hPubKey [I] Cryptographic key that decrypts pdData
1802 * dwFlags [I] Used only with a public/private key pair.
1803 * phKey [O] Imported key.
1809 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
1810 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1812 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1813 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1815 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1817 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1819 SetLastError(ERROR_INVALID_PARAMETER
);
1823 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1825 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1829 importkey
->pProvider
= prov
;
1830 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1831 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1833 *phKey
= (HCRYPTKEY
)importkey
;
1837 CRYPT_Free(importkey
);
1841 /******************************************************************************
1842 * CryptSignHashW (ADVAPI32.@)
1847 * hHash [I] Handle of the hash object to be signed.
1848 * dwKeySpec [I] Private key to use.
1849 * sDescription [I] Should be NULL.
1850 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1851 * pbSignature [O] Buffer of the signature data.
1852 * pdwSigLen [I/O] Size of the pbSignature buffer.
1859 * Because of security flaws sDescription should not be used and should thus be
1860 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1863 BOOL WINAPI
CryptSignHashW (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCWSTR sDescription
,
1864 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1866 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1869 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1870 hHash
, dwKeySpec
, debugstr_w(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1874 SetLastError(ERROR_INVALID_HANDLE
);
1877 if (!pdwSigLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1879 SetLastError(ERROR_INVALID_PARAMETER
);
1883 prov
= hash
->pProvider
;
1884 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, sDescription
,
1885 dwFlags
, pbSignature
, pdwSigLen
);
1888 /******************************************************************************
1889 * CryptSignHashA (ADVAPI32.@)
1891 * See CryptSignHashW.
1893 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1894 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1896 LPWSTR wsDescription
;
1899 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1900 hHash
, dwKeySpec
, debugstr_a(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1902 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1903 result
= CryptSignHashW(hHash
, dwKeySpec
, wsDescription
, dwFlags
, pbSignature
, pdwSigLen
);
1904 CRYPT_Free(wsDescription
);
1909 /******************************************************************************
1910 * CryptSetHashParam (ADVAPI32.@)
1912 * Customizes the operations of a hash object.
1915 * hHash [I] Handle of the hash object to set parameters.
1916 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1917 * pbData [I] Value data buffer.
1918 * dwFlags [I] Reserved for future use and must be zero.
1924 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
1927 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1929 TRACE("(0x%lx, %d, %p, %08x)\n", hHash
, dwParam
, pbData
, dwFlags
);
1931 if (!hash
|| !pbData
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1933 SetLastError(ERROR_INVALID_PARAMETER
);
1937 prov
= hash
->pProvider
;
1938 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1939 dwParam
, pbData
, dwFlags
);
1942 /******************************************************************************
1943 * CryptSetKeyParam (ADVAPI32.@)
1945 * Customizes a session key's operations.
1948 * hKey [I] Handle to the key to set values.
1949 * dwParam [I] See MSDN Doc.
1950 * pbData [I] Buffer of values to set.
1951 * dwFlags [I] Only used when dwParam == KP_ALGID.
1957 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
1960 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1962 TRACE("(0x%lx, %d, %p, %08x)\n", hKey
, dwParam
, pbData
, dwFlags
);
1964 if (!key
|| !pbData
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1966 SetLastError(ERROR_INVALID_PARAMETER
);
1970 prov
= key
->pProvider
;
1971 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
1972 dwParam
, pbData
, dwFlags
);
1975 /******************************************************************************
1976 * CryptSetProviderA (ADVAPI32.@)
1978 * Specifies the current user's default CSP.
1981 * pszProvName [I] Name of the new default CSP.
1982 * dwProvType [I] Provider type of the CSP.
1988 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
1990 TRACE("(%s, %d)\n", pszProvName
, dwProvType
);
1991 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1994 /******************************************************************************
1995 * CryptSetProviderW (ADVAPI32.@)
1997 * See CryptSetProviderA.
1999 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
2001 TRACE("(%s, %d)\n", debugstr_w(pszProvName
), dwProvType
);
2002 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
2005 /******************************************************************************
2006 * CryptSetProviderExW (ADVAPI32.@)
2008 * Specifies the default CSP.
2011 * pszProvName [I] Name of the new default CSP.
2012 * dwProvType [I] Provider type of the CSP.
2013 * pdwReserved [I] Reserved for future use and must be NULL.
2014 * dwFlags [I] See MSDN Doc.
2020 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2022 HKEY hProvKey
, hTypeKey
;
2024 static const WCHAR nameW
[] = {'N','a','m','e',0};
2026 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
2028 if (!pszProvName
|| pdwReserved
)
2030 SetLastError(ERROR_INVALID_PARAMETER
);
2033 if (dwProvType
> MAXPROVTYPES
)
2035 SetLastError(NTE_BAD_PROV_TYPE
);
2038 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
2039 || dwFlags
== CRYPT_DELETE_DEFAULT
)
2041 SetLastError(NTE_BAD_FLAGS
);
2045 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
2047 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2050 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2051 keyname
, &hTypeKey
))
2053 CRYPT_Free(keyname
);
2054 SetLastError(NTE_BAD_PROVIDER
);
2057 CRYPT_Free(keyname
);
2059 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
2061 RegDeleteValueW(hTypeKey
, nameW
);
2065 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
2067 RegCloseKey(hTypeKey
);
2068 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2071 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2072 keyname
, &hProvKey
))
2074 CRYPT_Free(keyname
);
2075 RegCloseKey(hTypeKey
);
2076 SetLastError(NTE_BAD_PROVIDER
);
2079 CRYPT_Free(keyname
);
2081 if (RegSetValueExW(hTypeKey
, nameW
, 0, REG_SZ
, (const BYTE
*)pszProvName
,
2082 (strlenW(pszProvName
) + 1)*sizeof(WCHAR
)))
2084 RegCloseKey(hTypeKey
);
2085 RegCloseKey(hProvKey
);
2089 RegCloseKey(hProvKey
);
2091 RegCloseKey(hTypeKey
);
2096 /******************************************************************************
2097 * CryptSetProviderExA (ADVAPI32.@)
2099 * See CryptSetProviderExW.
2101 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2106 TRACE("(%s, %d, %p, %08x)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
2108 if (CRYPT_ANSIToUnicode(pszProvName
, &str
, -1))
2110 ret
= CryptSetProviderExW(str
, dwProvType
, pdwReserved
, dwFlags
);
2116 /******************************************************************************
2117 * CryptSetProvParam (ADVAPI32.@)
2119 * Customizes the operations of a CSP.
2122 * hProv [I] Handle of a CSP.
2123 * dwParam [I] See MSDN Doc.
2124 * pbData [I] Buffer that contains a value to set as a parameter.
2125 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2131 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, CONST BYTE
*pbData
, DWORD dwFlags
)
2133 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
2135 TRACE("(0x%lx, %d, %p, %08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
2139 SetLastError(ERROR_INVALID_HANDLE
);
2142 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
2144 SetLastError(ERROR_INVALID_PARAMETER
);
2147 if (dwFlags
& PP_USE_HARDWARE_RNG
)
2149 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2150 FIXME("\tLetting the CSP decide.\n");
2152 if (dwFlags
& PP_CLIENT_HWND
)
2154 /* FIXME: Should verify the parameter */
2155 if (pbData
/* && IsWindow((HWND)pbData) */)
2157 crypt_hWindow
= (HWND
)(pbData
);
2160 SetLastError(ERROR_INVALID_PARAMETER
);
2164 /* All other flags go to the CSP */
2165 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
2168 /******************************************************************************
2169 * CryptVerifySignatureW (ADVAPI32.@)
2171 * Verifies the signature of a hash object.
2174 * hHash [I] Handle of the hash object to verify.
2175 * pbSignature [I] Signature data to verify.
2176 * dwSigLen [I] Size of pbSignature.
2177 * hPubKey [I] Handle to the public key to authenticate signature.
2178 * sDescription [I] Should be NULL.
2179 * dwFlags [I] See MSDN doc.
2186 * Because of security flaws sDescription should not be used and should thus be
2187 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2190 BOOL WINAPI
CryptVerifySignatureW (HCRYPTHASH hHash
, CONST BYTE
*pbSignature
, DWORD dwSigLen
,
2191 HCRYPTKEY hPubKey
, LPCWSTR sDescription
, DWORD dwFlags
)
2193 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
2194 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
2197 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2198 dwSigLen
, hPubKey
, debugstr_w(sDescription
), dwFlags
);
2200 if (!hash
|| !key
||
2201 !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
||
2202 !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
2204 SetLastError(ERROR_INVALID_PARAMETER
);
2208 prov
= hash
->pProvider
;
2209 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
2210 key
->hPrivate
, sDescription
, dwFlags
);
2213 /******************************************************************************
2214 * CryptVerifySignatureA (ADVAPI32.@)
2216 * See CryptVerifySignatureW.
2218 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, CONST BYTE
*pbSignature
, DWORD dwSigLen
,
2219 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
2221 LPWSTR wsDescription
;
2224 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2225 dwSigLen
, hPubKey
, debugstr_a(sDescription
), dwFlags
);
2227 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
2228 result
= CryptVerifySignatureW(hHash
, pbSignature
, dwSigLen
, hPubKey
, wsDescription
, dwFlags
);
2229 CRYPT_Free(wsDescription
);
2234 /******************************************************************************
2235 * SystemFunction030 (ADVAPI32.@)
2237 * Tests if two blocks of 16 bytes are equal
2240 * b1,b2 [I] block of 16 bytes
2243 * TRUE if blocks are the same
2244 * FALSE if blocks are different
2246 BOOL WINAPI
SystemFunction030(PVOID b1
, PVOID b2
)
2248 return !memcmp(b1
, b2
, 0x10);
2251 /******************************************************************************
2252 * SystemFunction035 (ADVAPI32.@)
2255 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2258 * Stub, always return TRUE.
2260 BOOL WINAPI
SystemFunction035(LPCSTR lpszDllFilePath
)
2262 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath
));
2266 /******************************************************************************
2267 * SystemFunction036 (ADVAPI32.@)
2269 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2272 * pbBufer [O] Pointer to memory to receive random bytes.
2273 * dwLen [I] Number of random bytes to fetch.
2280 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
2284 dev_random
= open("/dev/urandom", O_RDONLY
);
2285 if (dev_random
!= -1)
2287 if (read(dev_random
, pbBuffer
, dwLen
) == (ssize_t
)dwLen
)
2295 FIXME("couldn't open /dev/urandom\n");
2296 SetLastError(NTE_FAIL
);
2301 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2305 /******************************************************************************
2306 * SystemFunction040 (ADVAPI32.@)
2308 * MSDN documents this function as RtlEncryptMemory.
2311 * memory [I/O] Pointer to memory to encrypt.
2312 * length [I] Length of region to encrypt in bytes.
2313 * flags [I] Control whether other processes are able to decrypt the memory.
2314 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2315 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2316 * RTL_ENCRYPT_OPTION_SAME_LOGON
2319 * Success: STATUS_SUCCESS
2320 * Failure: NTSTATUS error code
2323 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2324 * If flags are specified when encrypting, the same flag value must be given
2325 * when decrypting the memory.
2327 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
)
2329 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
2330 return STATUS_SUCCESS
;
2333 /******************************************************************************
2334 * SystemFunction041 (ADVAPI32.@)
2336 * MSDN documents this function as RtlDecryptMemory.
2339 * memory [I/O] Pointer to memory to decrypt.
2340 * length [I] Length of region to decrypt in bytes.
2341 * flags [I] Control whether other processes are able to decrypt the memory.
2342 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2343 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2344 * RTL_ENCRYPT_OPTION_SAME_LOGON
2347 * Success: STATUS_SUCCESS
2348 * Failure: NTSTATUS error code
2351 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2352 * If flags are specified when encrypting, the same flag value must be given
2353 * when decrypting the memory.
2355 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
)
2357 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
2358 return STATUS_SUCCESS
;