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"
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
44 #define WIN32_NO_STATUS
49 #include "wine/debug.h"
50 #include "wine/unicode.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
55 static HWND crypt_hWindow
;
57 #define CRYPT_Alloc(size) (LocalAlloc(LMEM_ZEROINIT, size))
58 #define CRYPT_Free(buffer) (LocalFree(buffer))
60 static inline PWSTR
CRYPT_GetProvKeyName(PCWSTR pProvName
)
62 static const WCHAR KEYSTR
[] = {
63 'S','o','f','t','w','a','r','e','\\',
64 'M','i','c','r','o','s','o','f','t','\\',
65 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
66 'D','e','f','a','u','l','t','s','\\',
67 'P','r','o','v','i','d','e','r','\\',0
71 keyname
= CRYPT_Alloc((strlenW(KEYSTR
) + strlenW(pProvName
) +1)*sizeof(WCHAR
));
74 strcpyW(keyname
, KEYSTR
);
75 strcpyW(keyname
+ strlenW(KEYSTR
), pProvName
);
77 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
81 static inline PWSTR
CRYPT_GetTypeKeyName(DWORD dwType
, BOOL user
)
83 static const WCHAR MACHINESTR
[] = {
84 'S','o','f','t','w','a','r','e','\\',
85 'M','i','c','r','o','s','o','f','t','\\',
86 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
87 'D','e','f','a','u','l','t','s','\\',
88 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
89 'T','y','p','e',' ','X','X','X',0
91 static const WCHAR USERSTR
[] = {
92 'S','o','f','t','w','a','r','e','\\',
93 'M','i','c','r','o','s','o','f','t','\\',
94 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
95 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
100 keyname
= CRYPT_Alloc( ((user
? strlenW(USERSTR
) : strlenW(MACHINESTR
)) +1)*sizeof(WCHAR
));
103 user
? strcpyW(keyname
, USERSTR
) : strcpyW(keyname
, MACHINESTR
);
104 ptr
= keyname
+ strlenW(keyname
);
105 *(--ptr
) = (dwType
% 10) + '0';
106 *(--ptr
) = ((dwType
/ 10) % 10) + '0';
107 *(--ptr
) = (dwType
/ 100) + '0';
109 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
113 /* CRYPT_UnicodeToANSI
114 * wstr - unicode string
115 * str - pointer to ANSI string or pointer to null pointer if we have to do the allocation
116 * strsize - size of buffer pointed to by str
118 * returns TRUE if unsuccessful, FALSE otherwise.
119 * if wstr is NULL, returns TRUE and sets str to NULL! Value of str should be checked after call
121 static inline BOOL
CRYPT_UnicodeToANSI(LPCWSTR wstr
, LPSTR
* str
, int strsize
)
131 strsize
= WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, NULL
, 0, NULL
, NULL
);
132 *str
= CRYPT_Alloc(strsize
* sizeof(CHAR
));
134 else if (strsize
< 0)
136 strsize
= INT_MAX
; /* windows will pretend that the buffer is infinitely long */
141 WideCharToMultiByte(CP_ACP
, 0, wstr
, -1, *str
, strsize
, NULL
, NULL
);
145 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
149 /* CRYPT_ANSITOUnicode
151 * wstr - pointer to unicode string
152 * wstrsize - size of buffer pointed to by wstr or -1 if we have to do the allocation
154 static inline BOOL
CRYPT_ANSIToUnicode(LPCSTR str
, LPWSTR
* wstr
, int wstrsize
)
163 wcount
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
165 *wstr
= CRYPT_Alloc(wcount
* sizeof(WCHAR
));
167 wcount
= min( wcount
, wstrsize
/sizeof(WCHAR
) );
170 MultiByteToWideChar(CP_ACP
, 0, str
, -1, *wstr
, wcount
);
173 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
177 /* These next 2 functions are used by the VTableProvStruc structure */
178 static BOOL CALLBACK
CRYPT_VerifyImage(LPCSTR lpszImage
, BYTE
* pData
)
180 if (!lpszImage
|| !pData
)
182 SetLastError(ERROR_INVALID_PARAMETER
);
186 FIXME("(%s, %p): not verifying image\n", lpszImage
, pData
);
191 static void CALLBACK
CRYPT_ReturnhWnd(HWND
*phWnd
)
193 if (phWnd
) *phWnd
= crypt_hWindow
;
196 #define CRYPT_GetProvFunc(name) \
197 if ( !(provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)) ) goto error
198 #define CRYPT_GetProvFuncOpt(name) \
199 provider->pFuncs->p##name = (void*)GetProcAddress(provider->hModule, #name)
200 static PCRYPTPROV
CRYPT_LoadProvider(PCWSTR pImage
)
203 DWORD errorcode
= ERROR_NOT_ENOUGH_MEMORY
;
205 if ( !(provider
= CRYPT_Alloc(sizeof(CRYPTPROV
))) ) goto error
;
206 if ( !(provider
->pFuncs
= CRYPT_Alloc(sizeof(PROVFUNCS
))) ) goto error
;
207 if ( !(provider
->pVTable
= CRYPT_Alloc(sizeof(VTableProvStruc
))) ) goto error
;
208 if ( !(provider
->hModule
= LoadLibraryW(pImage
)) )
210 errorcode
= (GetLastError() == ERROR_FILE_NOT_FOUND
) ? NTE_PROV_DLL_NOT_FOUND
: NTE_PROVIDER_DLL_FAIL
;
211 FIXME("Failed to load dll %s\n", debugstr_w(pImage
));
214 provider
->dwMagic
= MAGIC_CRYPTPROV
;
215 provider
->refcount
= 1;
217 errorcode
= NTE_PROVIDER_DLL_FAIL
;
218 CRYPT_GetProvFunc(CPAcquireContext
);
219 CRYPT_GetProvFunc(CPCreateHash
);
220 CRYPT_GetProvFunc(CPDecrypt
);
221 CRYPT_GetProvFunc(CPDeriveKey
);
222 CRYPT_GetProvFunc(CPDestroyHash
);
223 CRYPT_GetProvFunc(CPDestroyKey
);
224 CRYPT_GetProvFuncOpt(CPDuplicateHash
);
225 CRYPT_GetProvFuncOpt(CPDuplicateKey
);
226 CRYPT_GetProvFunc(CPEncrypt
);
227 CRYPT_GetProvFunc(CPExportKey
);
228 CRYPT_GetProvFunc(CPGenKey
);
229 CRYPT_GetProvFunc(CPGenRandom
);
230 CRYPT_GetProvFunc(CPGetHashParam
);
231 CRYPT_GetProvFunc(CPGetKeyParam
);
232 CRYPT_GetProvFunc(CPGetProvParam
);
233 CRYPT_GetProvFunc(CPGetUserKey
);
234 CRYPT_GetProvFunc(CPHashData
);
235 CRYPT_GetProvFunc(CPHashSessionKey
);
236 CRYPT_GetProvFunc(CPImportKey
);
237 CRYPT_GetProvFunc(CPReleaseContext
);
238 CRYPT_GetProvFunc(CPSetHashParam
);
239 CRYPT_GetProvFunc(CPSetKeyParam
);
240 CRYPT_GetProvFunc(CPSetProvParam
);
241 CRYPT_GetProvFunc(CPSignHash
);
242 CRYPT_GetProvFunc(CPVerifySignature
);
244 /* FIXME: Not sure what the pbContextInfo field is for.
245 * Does it need memory allocation?
247 provider
->pVTable
->Version
= 3;
248 provider
->pVTable
->FuncVerifyImage
= CRYPT_VerifyImage
;
249 provider
->pVTable
->FuncReturnhWnd
= CRYPT_ReturnhWnd
;
250 provider
->pVTable
->dwProvType
= 0;
251 provider
->pVTable
->pbContextInfo
= NULL
;
252 provider
->pVTable
->cbContextInfo
= 0;
253 provider
->pVTable
->pszProvName
= NULL
;
257 SetLastError(errorcode
);
260 provider
->dwMagic
= 0;
261 if (provider
->hModule
)
262 FreeLibrary(provider
->hModule
);
263 CRYPT_Free(provider
->pVTable
);
264 CRYPT_Free(provider
->pFuncs
);
265 CRYPT_Free(provider
);
269 #undef CRYPT_GetProvFunc
270 #undef CRYPT_GetProvFuncOpt
273 static void CRYPT_CreateMachineGuid(void)
275 static const WCHAR cryptographyW
[] = {
276 'S','o','f','t','w','a','r','e','\\',
277 'M','i','c','r','o','s','o','f','t','\\',
278 'C','r','y','p','t','o','g','r','a','p','h','y',0 };
279 static const WCHAR machineGuidW
[] = {
280 'M','a','c','h','i','n','e','G','u','i','d',0 };
284 r
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, cryptographyW
, 0, KEY_ALL_ACCESS
,
290 r
= RegQueryValueExW(key
, machineGuidW
, NULL
, NULL
, NULL
, &size
);
291 if (r
== ERROR_FILE_NOT_FOUND
)
296 static const WCHAR uuidFmt
[] = {
297 '%','0','8','x','-','%','0','4','x','-',
298 '%','0','4','x','-','%','0','2','x',
299 '%','0','2','x','-','%','0','2','x',
300 '%','0','2','x','%','0','2','x',
301 '%','0','2','x','%','0','2','x',
304 rs
= UuidCreate(&uuid
);
307 sprintfW(buf
, uuidFmt
,
308 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
309 uuid
.Data4
[0], uuid
.Data4
[1],
310 uuid
.Data4
[2], uuid
.Data4
[3],
311 uuid
.Data4
[4], uuid
.Data4
[5],
312 uuid
.Data4
[6], uuid
.Data4
[7] );
313 RegSetValueExW(key
, machineGuidW
, 0, REG_SZ
,
315 (lstrlenW(buf
)+1)*sizeof(WCHAR
));
322 /******************************************************************************
323 * CryptAcquireContextW (ADVAPI32.@)
325 * Acquire a crypto provider context handle.
328 * phProv [O] Pointer to HCRYPTPROV for the output.
329 * pszContainer [I] Key Container Name
330 * pszProvider [I] Cryptographic Service Provider Name
331 * dwProvType [I] Crypto provider type to get a handle.
332 * dwFlags [I] flags for the operation
335 * TRUE on success, FALSE on failure.
337 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
338 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
340 PCRYPTPROV pProv
= NULL
;
342 PWSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
343 PSTR provnameA
= NULL
, pszContainerA
= NULL
;
344 DWORD keytype
, type
, len
;
346 static const WCHAR nameW
[] = {'N','a','m','e',0};
347 static const WCHAR typeW
[] = {'T','y','p','e',0};
348 static const WCHAR imagepathW
[] = {'I','m','a','g','e',' ','P','a','t','h',0};
350 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_w(pszContainer
),
351 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
353 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
355 SetLastError(NTE_BAD_PROV_TYPE
);
361 SetLastError(ERROR_INVALID_PARAMETER
);
365 /* Make sure the MachineGuid value exists */
366 CRYPT_CreateMachineGuid();
368 if (!pszProvider
|| !*pszProvider
)
370 /* No CSP name specified so try the user default CSP first
371 * then try the machine default CSP
373 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)) ) {
374 TRACE("No provider registered for crypto provider type %d.\n", dwProvType
);
375 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
378 if (RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &key
))
381 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
382 TRACE("No type registered for crypto provider type %d.\n", dwProvType
);
384 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
387 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
388 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname
));
391 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
396 r
= RegQueryValueExW(key
, nameW
, NULL
, &keytype
, NULL
, &len
);
397 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
399 TRACE("error %d reading size of 'Name' from registry\n", r
);
401 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
404 if(!(provname
= CRYPT_Alloc(len
)))
407 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
410 r
= RegQueryValueExW(key
, nameW
, NULL
, NULL
, (LPBYTE
)provname
, &len
);
411 if( r
!= ERROR_SUCCESS
)
413 TRACE("error %d reading 'Name' from registry\n", r
);
415 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
420 if ( !(provname
= CRYPT_Alloc((strlenW(pszProvider
) +1)*sizeof(WCHAR
))) )
422 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
425 strcpyW(provname
, pszProvider
);
428 keyname
= CRYPT_GetProvKeyName(provname
);
429 r
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
);
431 if (r
!= ERROR_SUCCESS
)
433 SetLastError(NTE_KEYSET_NOT_DEF
);
437 r
= RegQueryValueExW(key
, typeW
, NULL
, NULL
, (BYTE
*)&type
, &len
);
438 if (r
!= ERROR_SUCCESS
)
440 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
443 if (type
!= dwProvType
)
445 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type
, dwProvType
);
446 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
450 r
= RegQueryValueExW(key
, imagepathW
, NULL
, &keytype
, NULL
, &len
);
451 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
453 TRACE("error %d reading size of 'Image Path' from registry\n", r
);
455 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
458 if (!(temp
= CRYPT_Alloc(len
)))
461 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
464 r
= RegQueryValueExW(key
, imagepathW
, NULL
, NULL
, (LPBYTE
)temp
, &len
);
465 if( r
!= ERROR_SUCCESS
)
467 TRACE("error %d reading 'Image Path' from registry\n", r
);
469 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
473 len
= ExpandEnvironmentStringsW(temp
, NULL
, 0);
474 if ( !(imagepath
= CRYPT_Alloc(len
*sizeof(WCHAR
))) )
476 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
479 if (!ExpandEnvironmentStringsW(temp
, imagepath
, len
))
481 /* ExpandEnvironmentStrings will call SetLastError */
484 pProv
= CRYPT_LoadProvider(imagepath
);
486 /* CRYPT_LoadProvider calls SetLastError */
489 pProv
->pVTable
->dwProvType
= dwProvType
;
490 if(!CRYPT_UnicodeToANSI(provname
, &provnameA
, 0))
492 /* CRYPT_UnicodeToANSI calls SetLastError */
495 pProv
->pVTable
->pszProvName
= provnameA
;
496 if(!CRYPT_UnicodeToANSI(pszContainer
, &pszContainerA
, 0))
498 /* CRYPT_UnicodeToANSI calls SetLastError */
501 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, pszContainerA
, dwFlags
, pProv
->pVTable
))
503 /* MSDN: When this flag is set, the value returned in phProv is undefined,
504 * and thus, the CryptReleaseContext function need not be called afterwards.
505 * Therefore, we must clean up everything now.
507 if (dwFlags
& CRYPT_DELETEKEYSET
)
510 FreeLibrary(pProv
->hModule
);
511 CRYPT_Free(provnameA
);
512 CRYPT_Free(pProv
->pVTable
);
513 CRYPT_Free(pProv
->pFuncs
);
516 *phProv
= (HCRYPTPROV
)pProv
;
518 CRYPT_Free(pszContainerA
);
519 CRYPT_Free(provname
);
521 CRYPT_Free(imagepath
);
524 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
530 FreeLibrary(pProv
->hModule
);
531 CRYPT_Free(pProv
->pVTable
);
532 CRYPT_Free(pProv
->pFuncs
);
535 CRYPT_Free(pszContainerA
);
536 CRYPT_Free(provnameA
);
537 CRYPT_Free(provname
);
539 CRYPT_Free(imagepath
);
543 /******************************************************************************
544 * CryptAcquireContextA (ADVAPI32.@)
546 * See CryptAcquireContextW.
548 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
549 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
551 PWSTR pProvider
= NULL
, pContainer
= NULL
;
554 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_a(pszContainer
),
555 debugstr_a(pszProvider
), dwProvType
, dwFlags
);
557 if ( !CRYPT_ANSIToUnicode(pszContainer
, &pContainer
, -1) )
559 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
562 if ( !CRYPT_ANSIToUnicode(pszProvider
, &pProvider
, -1) )
564 CRYPT_Free(pContainer
);
565 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
569 ret
= CryptAcquireContextW(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
571 CRYPT_Free(pContainer
);
572 CRYPT_Free(pProvider
);
577 /******************************************************************************
578 * CryptContextAddRef (ADVAPI32.@)
580 * Increases reference count of a cryptographic service provider handle
584 * hProv [I] Handle to the CSP whose reference is being incremented.
585 * pdwReserved [IN] Reserved for future use and must be NULL.
586 * dwFlags [I] Reserved for future use and must be NULL.
592 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
594 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
596 TRACE("(0x%lx, %p, %08x)\n", hProv
, pdwReserved
, dwFlags
);
600 SetLastError(NTE_BAD_UID
);
604 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
606 SetLastError(ERROR_INVALID_PARAMETER
);
614 /******************************************************************************
615 * CryptReleaseContext (ADVAPI32.@)
617 * Releases the handle of a CSP. Reference count is decreased.
620 * hProv [I] Handle of a CSP.
621 * dwFlags [I] Reserved for future use and must be NULL.
627 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, ULONG_PTR dwFlags
)
629 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
632 TRACE("(0x%lx, %08lx)\n", hProv
, dwFlags
);
636 SetLastError(NTE_BAD_UID
);
640 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
642 SetLastError(ERROR_INVALID_PARAMETER
);
647 if (pProv
->refcount
<= 0)
649 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
651 FreeLibrary(pProv
->hModule
);
653 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
655 CRYPT_Free(pProv
->pVTable
->pszProvName
);
656 CRYPT_Free(pProv
->pVTable
);
657 CRYPT_Free(pProv
->pFuncs
);
663 /******************************************************************************
664 * CryptGenRandom (ADVAPI32.@)
666 * Fills a buffer with cryptographically random bytes.
669 * hProv [I] Handle of a CSP.
670 * dwLen [I] Number of bytes to generate.
671 * pbBuffer [I/O] Buffer to contain random bytes.
678 * pdBuffer must be at least dwLen bytes long.
680 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
682 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
684 TRACE("(0x%lx, %d, %p)\n", hProv
, dwLen
, pbBuffer
);
688 SetLastError(ERROR_INVALID_HANDLE
);
692 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
694 SetLastError(ERROR_INVALID_PARAMETER
);
698 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
701 /******************************************************************************
702 * CryptCreateHash (ADVAPI32.@)
704 * Initiates the hashing of a stream of data.
707 * hProv [I] Handle of a CSP.
708 * Algid [I] Identifies the hash algorithm to use.
709 * hKey [I] Key for the hash (if required).
710 * dwFlags [I] Reserved for future use and must be NULL.
711 * phHash [O] Address of the future handle to the new hash object.
718 * If the algorithm is a keyed hash, hKey is the key.
720 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
721 DWORD dwFlags
, HCRYPTHASH
*phHash
)
723 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
724 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
727 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
729 if (!prov
|| !phHash
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
||
730 (key
&& key
->dwMagic
!= MAGIC_CRYPTKEY
))
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
;
747 hash
->dwMagic
= MAGIC_CRYPTHASH
;
748 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
749 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
751 *phHash
= (HCRYPTHASH
)hash
;
762 /******************************************************************************
763 * CryptDecrypt (ADVAPI32.@)
765 * Decrypts data encrypted by CryptEncrypt.
768 * hKey [I] Handle to the decryption key.
769 * hHash [I] Handle to a hash object.
770 * Final [I] TRUE if this is the last section to be decrypted.
771 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
772 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
774 * pdwDataLen [I/O] Length of pbData before and after the call.
780 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
781 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
784 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
785 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
787 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
789 if (!key
|| !pbData
|| !pdwDataLen
||
790 !key
->pProvider
|| key
->dwMagic
!= MAGIC_CRYPTKEY
||
791 key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
793 SetLastError(ERROR_INVALID_PARAMETER
);
797 prov
= key
->pProvider
;
798 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
799 Final
, dwFlags
, pbData
, pdwDataLen
);
802 /******************************************************************************
803 * CryptDeriveKey (ADVAPI32.@)
805 * Generates session keys derived from a base data value.
808 * hProv [I] Handle to a CSP.
809 * Algid [I] Identifies the symmetric encryption algorithm to use.
810 * hBaseData [I] Handle to a hash object.
811 * dwFlags [I] Type of key to generate.
812 * phKey [I/O] Address of the newly generated key.
818 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
819 DWORD dwFlags
, HCRYPTKEY
*phKey
)
821 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
822 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
825 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
829 SetLastError(ERROR_INVALID_HANDLE
);
832 if (!phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
)
834 SetLastError(ERROR_INVALID_PARAMETER
);
837 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
839 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
843 key
->pProvider
= prov
;
844 key
->dwMagic
= MAGIC_CRYPTKEY
;
845 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
847 *phKey
= (HCRYPTKEY
)key
;
858 /******************************************************************************
859 * CryptDestroyHash (ADVAPI32.@)
861 * Destroys the hash object referenced by hHash.
864 * hHash [I] Handle of the hash object to be destroyed.
870 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
872 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
876 TRACE("(0x%lx)\n", hHash
);
880 SetLastError(ERROR_INVALID_HANDLE
);
884 if (!hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
885 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
887 SetLastError(ERROR_INVALID_PARAMETER
);
891 prov
= hash
->pProvider
;
892 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
898 /******************************************************************************
899 * CryptDestroyKey (ADVAPI32.@)
901 * Releases the handle referenced by hKey.
904 * hKey [I] Handle of the key to be destroyed.
910 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
912 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
916 TRACE("(0x%lx)\n", hKey
);
920 SetLastError(ERROR_INVALID_HANDLE
);
924 if (!key
->pProvider
|| key
->dwMagic
!= MAGIC_CRYPTKEY
||
925 key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
927 SetLastError(ERROR_INVALID_PARAMETER
);
931 prov
= key
->pProvider
;
932 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
938 /******************************************************************************
939 * CryptDuplicateHash (ADVAPI32.@)
944 * hHash [I] Handle to the hash to be copied.
945 * pdwReserved [I] Reserved for future use and must be zero.
946 * dwFlags [I] Reserved for future use and must be zero.
947 * phHash [O] Address of the handle to receive the copy.
953 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
954 DWORD dwFlags
, HCRYPTHASH
*phHash
)
957 PCRYPTHASH orghash
, newhash
;
959 TRACE("(0x%lx, %p, %08x, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
961 orghash
= (PCRYPTHASH
)hHash
;
962 if (!orghash
|| pdwReserved
|| !phHash
|| !orghash
->pProvider
||
963 orghash
->dwMagic
!= MAGIC_CRYPTHASH
|| orghash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
965 SetLastError(ERROR_INVALID_PARAMETER
);
969 prov
= orghash
->pProvider
;
970 if (!prov
->pFuncs
->pCPDuplicateHash
)
972 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
976 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
978 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
982 newhash
->pProvider
= prov
;
983 newhash
->dwMagic
= MAGIC_CRYPTHASH
;
984 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
986 *phHash
= (HCRYPTHASH
)newhash
;
989 newhash
->dwMagic
= 0;
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
->dwMagic
!= MAGIC_CRYPTKEY
||
1019 orgkey
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1021 SetLastError(ERROR_INVALID_PARAMETER
);
1025 prov
= orgkey
->pProvider
;
1026 if (!prov
->pFuncs
->pCPDuplicateKey
)
1028 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1032 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1034 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1038 newkey
->pProvider
= prov
;
1039 newkey
->dwMagic
= MAGIC_CRYPTKEY
;
1040 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
1042 *phKey
= (HCRYPTKEY
)newkey
;
1045 newkey
->dwMagic
= 0;
1050 /******************************************************************************
1051 * CryptEncrypt (ADVAPI32.@)
1056 * hKey [I] Handle to the encryption key.
1057 * hHash [I] Handle to a hash object.
1058 * Final [I] TRUE if this is the last section to encrypt.
1059 * dwFlags [I] Can be CRYPT_OAEP.
1060 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1061 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1062 * encrypted data after call.
1063 * dwBufLen [I] Length of the input pbData buffer.
1070 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1071 * required for the returned data in pdwDataLen.
1073 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1074 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1077 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1078 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1080 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1082 if (!key
|| !pdwDataLen
|| !key
->pProvider
||
1083 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1085 SetLastError(ERROR_INVALID_PARAMETER
);
1089 prov
= key
->pProvider
;
1090 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
1091 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1094 /******************************************************************************
1095 * CryptEnumProvidersW (ADVAPI32.@)
1097 * Returns the next available CSP.
1100 * dwIndex [I] Index of the next provider to be enumerated.
1101 * pdwReserved [I] Reserved for future use and must be NULL.
1102 * dwFlags [I] Reserved for future use and must be zero.
1103 * pdwProvType [O] DWORD designating the type of the provider.
1104 * pszProvName [O] Buffer that receives data from the provider.
1105 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1106 * of bytes stored in the buffer on return.
1113 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1114 * for memory allocation purposes.
1116 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
1117 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1120 static const WCHAR providerW
[] = {
1121 'S','o','f','t','w','a','r','e','\\',
1122 'M','i','c','r','o','s','o','f','t','\\',
1123 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1124 'D','e','f','a','u','l','t','s','\\',
1125 'P','r','o','v','i','d','e','r',0
1127 static const WCHAR typeW
[] = {'T','y','p','e',0};
1130 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1131 pdwProvType
, pszProvName
, pcbProvName
);
1133 if (pdwReserved
|| !pcbProvName
)
1135 SetLastError(ERROR_INVALID_PARAMETER
);
1140 SetLastError(NTE_BAD_FLAGS
);
1144 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, providerW
, &hKey
))
1146 SetLastError(NTE_FAIL
);
1156 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1157 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1159 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1161 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1166 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1167 CRYPT_Free(provNameW
);
1169 *pcbProvName
*= sizeof(WCHAR
);
1171 if (dwIndex
>= numkeys
)
1173 SetLastError(ERROR_NO_MORE_ITEMS
);
1177 DWORD size
= sizeof(DWORD
);
1181 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1184 SetLastError(result
);
1188 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1194 if (RegQueryValueExW(subkey
, typeW
, NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1197 RegCloseKey(subkey
);
1203 /******************************************************************************
1204 * CryptEnumProvidersA (ADVAPI32.@)
1206 * See CryptEnumProvidersW.
1208 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1209 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1215 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1216 pdwProvType
, pszProvName
, pcbProvName
);
1218 if(!CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1220 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1222 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1225 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1227 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1228 *pcbProvName
= bufsize
/ sizeof(WCHAR
); /* FIXME: not correct */
1232 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1234 SetLastError(ERROR_MORE_DATA
);
1241 /******************************************************************************
1242 * CryptEnumProviderTypesW (ADVAPI32.@)
1244 * Retrieves the next type of CSP supported.
1247 * dwIndex [I] Index of the next provider to be enumerated.
1248 * pdwReserved [I] Reserved for future use and must be NULL.
1249 * dwFlags [I] Reserved for future use and must be zero.
1250 * pdwProvType [O] DWORD designating the type of the provider.
1251 * pszTypeName [O] Buffer that receives data from the provider type.
1252 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1253 * of bytes stored in the buffer on return.
1260 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1261 * for memory allocation purposes.
1263 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1264 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1267 DWORD keylen
, numkeys
, dwType
;
1270 static const WCHAR KEYSTR
[] = {
1271 'S','o','f','t','w','a','r','e','\\',
1272 'M','i','c','r','o','s','o','f','t','\\',
1273 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1274 'D','e','f','a','u','l','t','s','\\',
1275 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1277 static const WCHAR typenameW
[] = {'T','y','p','e','N','a','m','e',0};
1279 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1280 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1282 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1284 SetLastError(ERROR_INVALID_PARAMETER
);
1289 SetLastError(NTE_BAD_FLAGS
);
1293 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, KEYSTR
, &hKey
))
1296 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1297 if (dwIndex
>= numkeys
)
1299 SetLastError(ERROR_NO_MORE_ITEMS
);
1304 if ( !(keyname
= CRYPT_Alloc(keylen
*sizeof(WCHAR
))) )
1306 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1310 if ( RegEnumKeyW(hKey
, dwIndex
, keyname
, keylen
) ) {
1311 CRYPT_Free(keyname
);
1315 RegOpenKeyW(hKey
, keyname
, &hSubkey
);
1318 ch
= keyname
+ strlenW(keyname
);
1319 /* Convert "Type 000" to 0, etc/ */
1320 *pdwProvType
= *(--ch
) - '0';
1321 *pdwProvType
+= (*(--ch
) - '0') * 10;
1322 *pdwProvType
+= (*(--ch
) - '0') * 100;
1323 CRYPT_Free(keyname
);
1325 result
= RegQueryValueExW(hSubkey
, typenameW
, NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1328 SetLastError(result
);
1329 RegCloseKey(hSubkey
);
1333 RegCloseKey(hSubkey
);
1337 /******************************************************************************
1338 * CryptEnumProviderTypesA (ADVAPI32.@)
1340 * See CryptEnumProviderTypesW.
1342 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1343 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1349 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1350 pdwProvType
, pszTypeName
, pcbTypeName
);
1352 if(!CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1354 if ( pszTypeName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1356 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1359 ret
= CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1361 CRYPT_UnicodeToANSI(str
, &pszTypeName
, *pcbTypeName
);
1362 *pcbTypeName
= bufsize
/ sizeof(WCHAR
);
1366 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1368 SetLastError(ERROR_MORE_DATA
);
1375 /******************************************************************************
1376 * CryptExportKey (ADVAPI32.@)
1378 * Exports a cryptographic key from a CSP.
1381 * hKey [I] Handle to the key to export.
1382 * hExpKey [I] Handle to a cryptographic key of the end user.
1383 * dwBlobType [I] Type of BLOB to be exported.
1384 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1385 * pbData [O] Buffer to receive BLOB data.
1386 * pdwDataLen [I/O] Specifies the size of pbData.
1393 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1394 * buffer needed to hold the BLOB.
1396 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1397 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1400 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1402 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1404 if (!key
|| !pdwDataLen
|| !key
->pProvider
||
1405 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1407 SetLastError(ERROR_INVALID_PARAMETER
);
1411 prov
= key
->pProvider
;
1412 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1413 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1416 /******************************************************************************
1417 * CryptGenKey (ADVAPI32.@)
1419 * Generates a random cryptographic session key or a pub/priv key pair.
1422 * hProv [I] Handle to a CSP.
1423 * Algid [I] Algorithm to use to make key.
1424 * dwFlags [I] Specifies type of key to make.
1425 * phKey [I] Address of the handle to which the new key is copied.
1431 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1433 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1436 TRACE("(0x%lx, %d, %08x, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1438 if (!phKey
|| !prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1440 SetLastError(ERROR_INVALID_PARAMETER
);
1443 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1445 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1449 key
->pProvider
= prov
;
1450 key
->dwMagic
= MAGIC_CRYPTKEY
;
1451 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1453 *phKey
= (HCRYPTKEY
)key
;
1463 /******************************************************************************
1464 * CryptGetDefaultProviderW (ADVAPI32.@)
1466 * Finds the default CSP of a certain provider type.
1469 * dwProvType [I] Provider type to look for.
1470 * pdwReserved [I] Reserved for future use and must be NULL.
1471 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1472 * pszProvName [O] Name of the default CSP.
1473 * pcbProvName [I/O] Size of pszProvName
1480 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1481 * memory allocation purposes on return.
1483 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1484 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1489 static const WCHAR nameW
[] = {'N','a','m','e',0};
1491 if (pdwReserved
|| !pcbProvName
)
1493 SetLastError(ERROR_INVALID_PARAMETER
);
1496 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1498 SetLastError(NTE_BAD_FLAGS
);
1501 if (dwProvType
> 999)
1503 SetLastError(NTE_BAD_PROV_TYPE
);
1506 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1508 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1511 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1513 CRYPT_Free(keyname
);
1514 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
1517 CRYPT_Free(keyname
);
1519 result
= RegQueryValueExW(hKey
, nameW
, NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1524 if (result
!= ERROR_MORE_DATA
)
1525 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1527 SetLastError(result
);
1535 /******************************************************************************
1536 * CryptGetDefaultProviderA (ADVAPI32.@)
1538 * See CryptGetDefaultProviderW.
1540 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1541 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1547 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1549 CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, NULL
, &bufsize
);
1550 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1552 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1555 ret
= CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, str
, &bufsize
);
1557 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1558 *pcbProvName
= bufsize
/ sizeof(WCHAR
);
1562 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1564 SetLastError(ERROR_MORE_DATA
);
1571 /******************************************************************************
1572 * CryptGetHashParam (ADVAPI32.@)
1574 * Retrieves data that controls the operations of a hash object.
1577 * hHash [I] Handle of the hash object to question.
1578 * dwParam [I] Query type.
1579 * pbData [O] Buffer that receives the value data.
1580 * pdwDataLen [I/O] Size of the pbData buffer.
1581 * dwFlags [I] Reserved for future use and must be zero.
1588 * If pbData is NULL, pdwDataLen will contain the length required.
1590 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1591 DWORD
*pdwDataLen
, DWORD dwFlags
)
1594 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1596 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1598 if (!hash
|| !pdwDataLen
|| !hash
->pProvider
||
1599 hash
->dwMagic
!= MAGIC_CRYPTHASH
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1601 SetLastError(ERROR_INVALID_PARAMETER
);
1605 prov
= hash
->pProvider
;
1606 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1607 pbData
, pdwDataLen
, dwFlags
);
1610 /******************************************************************************
1611 * CryptGetKeyParam (ADVAPI32.@)
1613 * Retrieves data that controls the operations of a key.
1616 * hKey [I] Handle to they key in question.
1617 * dwParam [I] Specifies query type.
1618 * pbData [O] Sequence of bytes to receive data.
1619 * pdwDataLen [I/O] Size of pbData.
1620 * dwFlags [I] Reserved for future use and must be zero.
1627 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1629 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1630 DWORD
*pdwDataLen
, DWORD dwFlags
)
1633 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1635 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1637 if (!key
|| !pdwDataLen
|| !key
->pProvider
||
1638 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1640 SetLastError(ERROR_INVALID_PARAMETER
);
1644 prov
= key
->pProvider
;
1645 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1646 pbData
, pdwDataLen
, dwFlags
);
1649 /******************************************************************************
1650 * CryptGetProvParam (ADVAPI32.@)
1652 * Retrieves parameters that control the operations of a CSP.
1655 * hProv [I] Handle of the CSP in question.
1656 * dwParam [I] Specifies query type.
1657 * pbData [O] Buffer to receive the data.
1658 * pdwDataLen [I/O] Size of pbData.
1659 * dwFlags [I] see MSDN Docs.
1666 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1668 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1669 DWORD
*pdwDataLen
, DWORD dwFlags
)
1671 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1673 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1675 if (!prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1677 SetLastError(ERROR_INVALID_PARAMETER
);
1681 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1684 /******************************************************************************
1685 * CryptGetUserKey (ADVAPI32.@)
1687 * Gets a handle of one of a user's two public/private key pairs.
1690 * hProv [I] Handle of a CSP.
1691 * dwKeySpec [I] Private key to use.
1692 * phUserKey [O] Pointer to the handle of the retrieved keys.
1698 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1700 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1703 TRACE("(0x%lx, %d, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1707 SetLastError(ERROR_INVALID_HANDLE
);
1710 if (!phUserKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1712 SetLastError(ERROR_INVALID_PARAMETER
);
1715 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1717 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1721 key
->pProvider
= prov
;
1722 key
->dwMagic
= MAGIC_CRYPTKEY
;
1723 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1725 *phUserKey
= (HCRYPTKEY
)key
;
1736 /******************************************************************************
1737 * CryptHashData (ADVAPI32.@)
1739 * Adds data to a hash object.
1742 * hHash [I] Handle of the hash object.
1743 * pbData [I] Buffer of data to be hashed.
1744 * dwDataLen [I] Number of bytes to add.
1745 * dwFlags [I] Can be CRYPT_USERDATA
1751 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, const BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1753 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1756 TRACE("(0x%lx, %p, %d, %08x)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1760 SetLastError(ERROR_INVALID_HANDLE
);
1763 if (!hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
1764 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1766 SetLastError(ERROR_INVALID_PARAMETER
);
1770 prov
= hash
->pProvider
;
1771 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1774 /******************************************************************************
1775 * CryptHashSessionKey (ADVAPI32.@)
1777 * Compute the cryptographic hash of a session key object.
1780 * hHash [I] Handle to the hash object.
1781 * hKey [I] Handle to the key to be hashed.
1782 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1788 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1790 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1791 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1794 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash
, hKey
, dwFlags
);
1798 SetLastError(ERROR_INVALID_HANDLE
);
1802 if (!hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
1803 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
|| key
->dwMagic
!= MAGIC_CRYPTKEY
)
1805 SetLastError(ERROR_INVALID_PARAMETER
);
1809 prov
= hash
->pProvider
;
1810 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1813 /******************************************************************************
1814 * CryptImportKey (ADVAPI32.@)
1816 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1819 * hProv [I] Handle of a CSP.
1820 * pbData [I] Contains the key to be imported.
1821 * dwDataLen [I] Length of the key.
1822 * hPubKey [I] Cryptographic key that decrypts pdData
1823 * dwFlags [I] Used only with a public/private key pair.
1824 * phKey [O] Imported key.
1830 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
1831 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1833 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1834 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1836 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1838 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
||
1839 prov
->dwMagic
!= MAGIC_CRYPTPROV
||
1840 (pubkey
&& pubkey
->dwMagic
!= MAGIC_CRYPTKEY
))
1842 SetLastError(ERROR_INVALID_PARAMETER
);
1846 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1848 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1852 importkey
->pProvider
= prov
;
1853 importkey
->dwMagic
= MAGIC_CRYPTKEY
;
1854 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1855 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1857 *phKey
= (HCRYPTKEY
)importkey
;
1861 importkey
->dwMagic
= 0;
1862 CRYPT_Free(importkey
);
1866 /******************************************************************************
1867 * CryptSignHashW (ADVAPI32.@)
1872 * hHash [I] Handle of the hash object to be signed.
1873 * dwKeySpec [I] Private key to use.
1874 * sDescription [I] Should be NULL.
1875 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1876 * pbSignature [O] Buffer of the signature data.
1877 * pdwSigLen [I/O] Size of the pbSignature buffer.
1884 * Because of security flaws sDescription should not be used and should thus be
1885 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1888 BOOL WINAPI
CryptSignHashW (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCWSTR sDescription
,
1889 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1891 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1894 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1895 hHash
, dwKeySpec
, debugstr_w(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1899 SetLastError(ERROR_INVALID_HANDLE
);
1902 if (!pdwSigLen
|| !hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
1903 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1905 SetLastError(ERROR_INVALID_PARAMETER
);
1909 prov
= hash
->pProvider
;
1910 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, sDescription
,
1911 dwFlags
, pbSignature
, pdwSigLen
);
1914 /******************************************************************************
1915 * CryptSignHashA (ADVAPI32.@)
1917 * See CryptSignHashW.
1919 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1920 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1922 LPWSTR wsDescription
;
1925 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1926 hHash
, dwKeySpec
, debugstr_a(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1928 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1929 result
= CryptSignHashW(hHash
, dwKeySpec
, wsDescription
, dwFlags
, pbSignature
, pdwSigLen
);
1930 CRYPT_Free(wsDescription
);
1935 /******************************************************************************
1936 * CryptSetHashParam (ADVAPI32.@)
1938 * Customizes the operations of a hash object.
1941 * hHash [I] Handle of the hash object to set parameters.
1942 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1943 * pbData [I] Value data buffer.
1944 * dwFlags [I] Reserved for future use and must be zero.
1950 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, const BYTE
*pbData
, DWORD dwFlags
)
1953 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1955 TRACE("(0x%lx, %d, %p, %08x)\n", hHash
, dwParam
, pbData
, dwFlags
);
1957 if (!hash
|| !pbData
|| !hash
->pProvider
||
1958 hash
->dwMagic
!= MAGIC_CRYPTHASH
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1960 SetLastError(ERROR_INVALID_PARAMETER
);
1964 prov
= hash
->pProvider
;
1965 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1966 dwParam
, pbData
, dwFlags
);
1969 /******************************************************************************
1970 * CryptSetKeyParam (ADVAPI32.@)
1972 * Customizes a session key's operations.
1975 * hKey [I] Handle to the key to set values.
1976 * dwParam [I] See MSDN Doc.
1977 * pbData [I] Buffer of values to set.
1978 * dwFlags [I] Only used when dwParam == KP_ALGID.
1984 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, const BYTE
*pbData
, DWORD dwFlags
)
1987 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1989 TRACE("(0x%lx, %d, %p, %08x)\n", hKey
, dwParam
, pbData
, dwFlags
);
1991 if (!key
|| !pbData
|| !key
->pProvider
||
1992 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1994 SetLastError(ERROR_INVALID_PARAMETER
);
1998 prov
= key
->pProvider
;
1999 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
2000 dwParam
, pbData
, dwFlags
);
2003 /******************************************************************************
2004 * CryptSetProviderA (ADVAPI32.@)
2006 * Specifies the current user's default CSP.
2009 * pszProvName [I] Name of the new default CSP.
2010 * dwProvType [I] Provider type of the CSP.
2016 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
2018 TRACE("(%s, %d)\n", pszProvName
, dwProvType
);
2019 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
2022 /******************************************************************************
2023 * CryptSetProviderW (ADVAPI32.@)
2025 * See CryptSetProviderA.
2027 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
2029 TRACE("(%s, %d)\n", debugstr_w(pszProvName
), dwProvType
);
2030 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
2033 /******************************************************************************
2034 * CryptSetProviderExW (ADVAPI32.@)
2036 * Specifies the default CSP.
2039 * pszProvName [I] Name of the new default CSP.
2040 * dwProvType [I] Provider type of the CSP.
2041 * pdwReserved [I] Reserved for future use and must be NULL.
2042 * dwFlags [I] See MSDN Doc.
2048 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2050 HKEY hProvKey
, hTypeKey
;
2052 static const WCHAR nameW
[] = {'N','a','m','e',0};
2054 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
2056 if (!pszProvName
|| pdwReserved
)
2058 SetLastError(ERROR_INVALID_PARAMETER
);
2061 if (dwProvType
> MAXPROVTYPES
)
2063 SetLastError(NTE_BAD_PROV_TYPE
);
2066 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
2067 || dwFlags
== CRYPT_DELETE_DEFAULT
)
2069 SetLastError(NTE_BAD_FLAGS
);
2073 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
2075 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2078 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2079 keyname
, &hTypeKey
))
2081 CRYPT_Free(keyname
);
2082 SetLastError(NTE_BAD_PROVIDER
);
2085 CRYPT_Free(keyname
);
2087 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
2089 RegDeleteValueW(hTypeKey
, nameW
);
2093 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
2095 RegCloseKey(hTypeKey
);
2096 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2099 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2100 keyname
, &hProvKey
))
2102 CRYPT_Free(keyname
);
2103 RegCloseKey(hTypeKey
);
2104 SetLastError(NTE_BAD_PROVIDER
);
2107 CRYPT_Free(keyname
);
2109 if (RegSetValueExW(hTypeKey
, nameW
, 0, REG_SZ
, (const BYTE
*)pszProvName
,
2110 (strlenW(pszProvName
) + 1)*sizeof(WCHAR
)))
2112 RegCloseKey(hTypeKey
);
2113 RegCloseKey(hProvKey
);
2117 RegCloseKey(hProvKey
);
2119 RegCloseKey(hTypeKey
);
2124 /******************************************************************************
2125 * CryptSetProviderExA (ADVAPI32.@)
2127 * See CryptSetProviderExW.
2129 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2134 TRACE("(%s, %d, %p, %08x)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
2136 if (CRYPT_ANSIToUnicode(pszProvName
, &str
, -1))
2138 ret
= CryptSetProviderExW(str
, dwProvType
, pdwReserved
, dwFlags
);
2144 /******************************************************************************
2145 * CryptSetProvParam (ADVAPI32.@)
2147 * Customizes the operations of a CSP.
2150 * hProv [I] Handle of a CSP.
2151 * dwParam [I] See MSDN Doc.
2152 * pbData [I] Buffer that contains a value to set as a parameter.
2153 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2159 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, const BYTE
*pbData
, DWORD dwFlags
)
2161 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
2163 TRACE("(0x%lx, %d, %p, %08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
2167 SetLastError(ERROR_INVALID_HANDLE
);
2170 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
2172 SetLastError(ERROR_INVALID_PARAMETER
);
2175 if (dwFlags
& PP_USE_HARDWARE_RNG
)
2177 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2178 FIXME("\tLetting the CSP decide.\n");
2180 if (dwFlags
& PP_CLIENT_HWND
)
2182 /* FIXME: Should verify the parameter */
2183 if (pbData
/* && IsWindow((HWND)pbData) */)
2185 crypt_hWindow
= (HWND
)(pbData
);
2188 SetLastError(ERROR_INVALID_PARAMETER
);
2192 /* All other flags go to the CSP */
2193 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
2196 /******************************************************************************
2197 * CryptVerifySignatureW (ADVAPI32.@)
2199 * Verifies the signature of a hash object.
2202 * hHash [I] Handle of the hash object to verify.
2203 * pbSignature [I] Signature data to verify.
2204 * dwSigLen [I] Size of pbSignature.
2205 * hPubKey [I] Handle to the public key to authenticate signature.
2206 * sDescription [I] Should be NULL.
2207 * dwFlags [I] See MSDN doc.
2214 * Because of security flaws sDescription should not be used and should thus be
2215 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2218 BOOL WINAPI
CryptVerifySignatureW (HCRYPTHASH hHash
, const BYTE
*pbSignature
, DWORD dwSigLen
,
2219 HCRYPTKEY hPubKey
, LPCWSTR sDescription
, DWORD dwFlags
)
2221 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
2222 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
2225 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2226 dwSigLen
, hPubKey
, debugstr_w(sDescription
), dwFlags
);
2228 if (!hash
|| !key
|| key
->dwMagic
!= MAGIC_CRYPTKEY
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
2229 !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
||
2230 !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
2232 SetLastError(ERROR_INVALID_PARAMETER
);
2236 prov
= hash
->pProvider
;
2237 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
2238 key
->hPrivate
, sDescription
, dwFlags
);
2241 /******************************************************************************
2242 * CryptVerifySignatureA (ADVAPI32.@)
2244 * See CryptVerifySignatureW.
2246 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, const BYTE
*pbSignature
, DWORD dwSigLen
,
2247 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
2249 LPWSTR wsDescription
;
2252 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2253 dwSigLen
, hPubKey
, debugstr_a(sDescription
), dwFlags
);
2255 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
2256 result
= CryptVerifySignatureW(hHash
, pbSignature
, dwSigLen
, hPubKey
, wsDescription
, dwFlags
);
2257 CRYPT_Free(wsDescription
);
2262 /******************************************************************************
2263 * SystemFunction030 (ADVAPI32.@)
2265 * Tests if two blocks of 16 bytes are equal
2268 * b1,b2 [I] block of 16 bytes
2271 * TRUE if blocks are the same
2272 * FALSE if blocks are different
2274 BOOL WINAPI
SystemFunction030(LPCVOID b1
, LPCVOID b2
)
2276 return !memcmp(b1
, b2
, 0x10);
2279 /******************************************************************************
2280 * SystemFunction035 (ADVAPI32.@)
2283 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2286 * Stub, always return TRUE.
2288 BOOL WINAPI
SystemFunction035(LPCSTR lpszDllFilePath
)
2290 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath
));
2294 /******************************************************************************
2295 * SystemFunction036 (ADVAPI32.@)
2297 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2300 * pbBufer [O] Pointer to memory to receive random bytes.
2301 * dwLen [I] Number of random bytes to fetch.
2308 BOOLEAN WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
2312 dev_random
= open("/dev/urandom", O_RDONLY
);
2313 if (dev_random
!= -1)
2315 if (read(dev_random
, pbBuffer
, dwLen
) == (ssize_t
)dwLen
)
2323 FIXME("couldn't open /dev/urandom\n");
2324 SetLastError(NTE_FAIL
);
2329 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2333 /******************************************************************************
2334 * SystemFunction040 (ADVAPI32.@)
2336 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2339 * memory [I/O] Pointer to memory to encrypt.
2340 * length [I] Length of region to encrypt 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
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
)
2357 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
2358 return STATUS_SUCCESS
;
2361 /******************************************************************************
2362 * SystemFunction041 (ADVAPI32.@)
2364 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2367 * memory [I/O] Pointer to memory to decrypt.
2368 * length [I] Length of region to decrypt in bytes.
2369 * flags [I] Control whether other processes are able to decrypt the memory.
2370 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2371 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2372 * RTL_ENCRYPT_OPTION_SAME_LOGON
2375 * Success: STATUS_SUCCESS
2376 * Failure: NTSTATUS error code
2379 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2380 * If flags are specified when encrypting, the same flag value must be given
2381 * when decrypting the memory.
2383 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
)
2385 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
2386 return STATUS_SUCCESS
;