2 * Copyright 1999 Ian Schmidt
3 * Copyright 2001 Travis Michielsen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /***********************************************************************
23 * - Reference counting
28 #include "wine/port.h"
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
45 #include "wine/debug.h"
46 #include "wine/unicode.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
52 HWND crypt_hWindow
= 0;
54 #define CRYPT_ReturnLastError(err) do {SetLastError(err); return FALSE;} while(0)
56 #define CRYPT_Alloc(size) ((LPVOID)LocalAlloc(LMEM_ZEROINIT, size))
57 #define CRYPT_Free(buffer) (LocalFree((HLOCAL)buffer))
59 static inline PWSTR
CRYPT_GetProvKeyName(PCWSTR pProvName
)
61 static const WCHAR KEYSTR
[] = {
62 'S','o','f','t','w','a','r','e','\\',
63 'M','i','c','r','o','s','o','f','t','\\',
64 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
65 'D','e','f','a','u','l','t','s','\\',
66 'P','r','o','v','i','d','e','r','\\',0
70 keyname
= CRYPT_Alloc((strlenW(KEYSTR
) + strlenW(pProvName
) +1)*sizeof(WCHAR
));
73 strcpyW(keyname
, KEYSTR
);
74 strcpyW(keyname
+ strlenW(KEYSTR
), pProvName
);
76 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
80 static inline PWSTR
CRYPT_GetTypeKeyName(DWORD dwType
, BOOL user
)
82 static const WCHAR MACHINESTR
[] = {
83 'S','o','f','t','w','a','r','e','\\',
84 'M','i','c','r','o','s','o','f','t','\\',
85 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
86 'D','e','f','a','u','l','t','s','\\',
87 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s','\\',
88 'T','y','p','e',' ','X','X','X',0
90 static const WCHAR USERSTR
[] = {
91 'S','o','f','t','w','a','r','e','\\',
92 'M','i','c','r','o','s','o','f','t','\\',
93 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
94 'P','r','o','v','i','d','e','r',' ','T','y','p','e',' ','X','X','X',0
99 keyname
= CRYPT_Alloc( ((user
? strlenW(USERSTR
) : strlenW(MACHINESTR
)) +1)*sizeof(WCHAR
));
102 user
? strcpyW(keyname
, USERSTR
) : strcpyW(keyname
, MACHINESTR
);
103 ptr
= keyname
+ strlenW(keyname
);
104 *(--ptr
) = (dwType
% 10) + '0';
105 *(--ptr
) = ((dwType
/ 10) % 10) + '0';
106 *(--ptr
) = (dwType
/ 100) + '0';
108 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
112 /* CRYPT_UnicodeTOANSI
113 * wstr - unicode string
114 * str - pointer to ANSI string
115 * strsize - size of buffer pointed to by str or -1 if we have to do the allocation
117 * returns TRUE if unsuccessfull, 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(PWSTR 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 /******************************************************************************
271 * CryptAcquireContextW (ADVAPI32.@)
272 * Acquire a crypto provider context handle.
275 * phProv: Pointer to HCRYPTPROV for the output.
276 * pszContainer: Key Container Name
277 * pszProvider: Cryptographic Service Provider Name
278 * dwProvType: Crypto provider type to get a handle.
279 * dwFlags: flags for the operation
281 * RETURNS TRUE on success, FALSE on failure.
283 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
284 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
286 PCRYPTPROV pProv
= NULL
;
288 PWSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
289 PSTR provnameA
= NULL
, pszContainerA
= NULL
;
290 DWORD keytype
, type
, len
;
292 static const WCHAR nameW
[] = {'N','a','m','e',0};
293 static const WCHAR typeW
[] = {'T','y','p','e',0};
294 static const WCHAR imagepathW
[] = {'I','m','a','g','e',' ','P','a','t','h',0};
296 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv
, debugstr_w(pszContainer
),
297 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
299 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
301 SetLastError(NTE_BAD_PROV_TYPE
);
307 SetLastError(ERROR_INVALID_PARAMETER
);
311 if (!pszProvider
|| !*pszProvider
)
313 /* No CSP name specified so try the user default CSP first
314 * then try the machine default CSP
316 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)) ) {
317 TRACE("No provider registered for crypto provider type %ld.\n", dwProvType
);
318 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
321 if (RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &key
))
324 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
325 TRACE("No type registered for crypto provider type %ld.\n", dwProvType
);
327 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
330 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
331 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname
));
334 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
339 r
= RegQueryValueExW(key
, nameW
, NULL
, &keytype
, NULL
, &len
);
340 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
342 TRACE("error %ld reading size of 'Name' from registry\n", r
);
344 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
347 if(!(provname
= CRYPT_Alloc(len
)))
350 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
353 r
= RegQueryValueExW(key
, nameW
, NULL
, NULL
, (LPBYTE
)provname
, &len
);
354 if( r
!= ERROR_SUCCESS
)
356 TRACE("error %ld reading 'Name' from registry\n", r
);
358 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
363 if ( !(provname
= CRYPT_Alloc((strlenW(pszProvider
) +1)*sizeof(WCHAR
))) )
365 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
368 strcpyW(provname
, pszProvider
);
371 keyname
= CRYPT_GetProvKeyName(provname
);
372 r
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
);
374 if (r
!= ERROR_SUCCESS
)
376 SetLastError(NTE_KEYSET_NOT_DEF
);
380 r
= RegQueryValueExW(key
, typeW
, NULL
, NULL
, (BYTE
*)&type
, &len
);
381 if (r
!= ERROR_SUCCESS
)
383 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
386 if (type
!= dwProvType
)
388 TRACE("Crypto provider has wrong type (%ld vs expected %ld).\n", type
, dwProvType
);
389 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
393 r
= RegQueryValueExW(key
, imagepathW
, NULL
, &keytype
, NULL
, &len
);
394 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
396 TRACE("error %ld reading size of 'Image Path' from registry\n", r
);
398 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
401 if (!(temp
= CRYPT_Alloc(len
)))
404 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
407 r
= RegQueryValueExW(key
, imagepathW
, NULL
, NULL
, (LPBYTE
)temp
, &len
);
408 if( r
!= ERROR_SUCCESS
)
410 TRACE("error %ld reading 'Image Path' from registry\n", r
);
412 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
416 len
= ExpandEnvironmentStringsW(temp
, NULL
, 0);
417 if ( !(imagepath
= CRYPT_Alloc(len
*sizeof(WCHAR
))) )
419 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
422 if (!ExpandEnvironmentStringsW(temp
, imagepath
, len
))
424 /* ExpandEnvironmentStrings will call SetLastError */
427 pProv
= CRYPT_LoadProvider(imagepath
);
429 /* CRYPT_LoadProvider calls SetLastError */
432 pProv
->pVTable
->dwProvType
= dwProvType
;
433 if(!CRYPT_UnicodeToANSI(provname
, &provnameA
, -1))
435 /* CRYPT_UnicodeToANSI calls SetLastError */
438 pProv
->pVTable
->pszProvName
= provnameA
;
439 if(!CRYPT_UnicodeToANSI(pszContainer
, &pszContainerA
, -1))
441 /* CRYPT_UnicodeToANSI calls SetLastError */
444 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, pszContainerA
, dwFlags
, pProv
->pVTable
))
446 /* MSDN: When this flag is set, the value returned in phProv is undefined,
447 * and thus, the CryptReleaseContext function need not be called afterwards.
448 * Therefore, we must clean up everything now.
450 if (dwFlags
& CRYPT_DELETEKEYSET
)
453 FreeLibrary(pProv
->hModule
);
454 CRYPT_Free(provnameA
);
455 CRYPT_Free(pProv
->pVTable
);
456 CRYPT_Free(pProv
->pFuncs
);
459 *phProv
= (HCRYPTPROV
)pProv
;
461 CRYPT_Free(pszContainerA
);
462 CRYPT_Free(provname
);
464 CRYPT_Free(imagepath
);
467 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
473 FreeLibrary(pProv
->hModule
);
475 CRYPT_Free(pProv
->pVTable
);
477 CRYPT_Free(pProv
->pFuncs
);
481 CRYPT_Free(pszContainerA
);
483 CRYPT_Free(provnameA
);
485 CRYPT_Free(provname
);
489 CRYPT_Free(imagepath
);
493 /******************************************************************************
494 * CryptAcquireContextA (ADVAPI32.@)
496 * see CryptAcquireContextW
498 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
499 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
501 PWSTR pProvider
= NULL
, pContainer
= NULL
;
504 TRACE("(%p, %s, %s, %ld, %08lx)\n", phProv
, pszContainer
,
505 pszProvider
, dwProvType
, dwFlags
);
507 if ( !CRYPT_ANSIToUnicode(pszContainer
, &pContainer
, -1) )
508 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
509 if ( !CRYPT_ANSIToUnicode(pszProvider
, &pProvider
, -1) )
511 CRYPT_Free(pContainer
);
512 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
515 ret
= CryptAcquireContextW(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
518 CRYPT_Free(pContainer
);
520 CRYPT_Free(pProvider
);
525 /******************************************************************************
526 * CryptContextAddRef (ADVAPI32.@)
528 * Increases reference count of a cryptographic service provider handle
532 * hProv [I] Handle to the CSP whose reference is being incremented.
533 * pdwReserved [IN] Reserved for future use and must be NULL.
534 * dwFlags [I] Reserved for future use and must be NULL.
540 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
542 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
544 TRACE("(0x%lx, %p, %08lx)\n", hProv
, pdwReserved
, dwFlags
);
548 SetLastError(NTE_BAD_UID
);
552 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
554 SetLastError(ERROR_INVALID_PARAMETER
);
562 /******************************************************************************
563 * CryptReleaseContext (ADVAPI32.@)
565 * Releases the handle of a CSP. Reference count is decreased.
568 * hProv [I] Handle of a CSP.
569 * dwFlags [I] Reserved for future use and must be NULL.
575 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, DWORD dwFlags
)
577 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
580 TRACE("(0x%lx, %08lx)\n", hProv
, dwFlags
);
584 SetLastError(NTE_BAD_UID
);
588 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
590 SetLastError(ERROR_INVALID_PARAMETER
);
595 if (pProv
->refcount
<= 0)
597 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
599 FreeLibrary(pProv
->hModule
);
601 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
603 CRYPT_Free(pProv
->pVTable
->pszProvName
);
604 CRYPT_Free(pProv
->pVTable
);
605 CRYPT_Free(pProv
->pFuncs
);
611 /******************************************************************************
612 * CryptGenRandom (ADVAPI32.@)
614 * Fills a buffer with cryptographically random bytes.
617 * hProv [I] Handle of a CSP.
618 * dwLen [I] Number of bytes to generate.
619 * pbBuffer [I/O] Buffer to contain random bytes.
626 * pdBuffer must be at least dwLen bytes long.
628 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
630 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
632 TRACE("(0x%lx, %ld, %p)\n", hProv
, dwLen
, pbBuffer
);
635 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
637 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
638 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
640 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
643 /******************************************************************************
644 * CryptCreateHash (ADVAPI32.@)
646 * Initiates the hashing of a stream of data.
649 * hProv [I] Handle of a CSP.
650 * Algid [I] Identifies the hash algorithm to use.
651 * hKey [I] Key for the hash (if required).
652 * dwFlags [I] Reserved for future use and must be NULL.
653 * phHash [O] Address of the future handle to the new hash object.
660 * If the algorithm is a keyed hash, hKey is the key.
662 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
663 DWORD dwFlags
, HCRYPTHASH
*phHash
)
665 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
666 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
669 TRACE("(0x%lx, 0x%x, 0x%lx, %08lx, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
672 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
673 if (!phHash
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
674 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
676 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
677 if ( !(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
678 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
680 hash
->pProvider
= prov
;
682 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
683 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
685 *phHash
= (HCRYPTHASH
)hash
;
695 /******************************************************************************
696 * CryptDecrypt (ADVAPI32.@)
698 * Decrypts data encrypted by CryptEncrypt.
701 * hKey [I] Handle to the decryption key.
702 * hHash [I] Handle to a hash object.
703 * Final [I] TRUE if this is the last section to be decrypted.
704 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
705 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
707 * pdwDataLen [I/O] Length of pbData before and after the call.
713 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
714 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
717 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
718 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
720 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
722 if (!key
|| !pbData
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
723 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
725 prov
= key
->pProvider
;
726 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
727 Final
, dwFlags
, pbData
, pdwDataLen
);
730 /******************************************************************************
731 * CryptDeriveKey (ADVAPI32.@)
733 * Generates session keys derived from a base data value.
736 * hProv [I] Handle to a CSP.
737 * Algid [I] Identifies the symmetric encryption algorithm to use.
738 * hBaseData [I] Handle to a hash object.
739 * dwFlags [I] Type of key to generate.
740 * phKey [I/O] Address of the newly generated key.
746 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
747 DWORD dwFlags
, HCRYPTKEY
*phKey
)
749 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
750 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
753 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08lx, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
756 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
757 if (!phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
758 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
759 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
760 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
762 key
->pProvider
= prov
;
763 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
765 *phKey
= (HCRYPTKEY
)key
;
775 /******************************************************************************
776 * CryptDestroyHash (ADVAPI32.@)
778 * Destroys the hash object referenced by hHash.
781 * hHash [I] Handle of the hash object to be destroyed.
787 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
789 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
793 TRACE("(0x%lx)\n", hHash
);
796 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
798 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
799 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
801 prov
= hash
->pProvider
;
802 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
807 /******************************************************************************
808 * CryptDestroyKey (ADVAPI32.@)
810 * Releases the handle referenced by hKey.
813 * hKey [I] Handle of the key to be destroyed.
819 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
821 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
825 TRACE("(0x%lx)\n", hKey
);
828 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
830 if (!key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
831 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
833 prov
= key
->pProvider
;
834 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
839 /******************************************************************************
840 * CryptDuplicateHash (ADVAPI32.@)
845 * hHash [I] Handle to the hash to be copied.
846 * pdwReserved [I] Reserved for future use and must be zero.
847 * dwFlags [I] Reserved for future use and must be zero.
848 * phHash [O] Address of the handle to receive the copy.
854 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
855 DWORD dwFlags
, HCRYPTHASH
*phHash
)
858 PCRYPTHASH orghash
, newhash
;
860 TRACE("(0x%lx, %p, %08lx, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
862 orghash
= (PCRYPTHASH
)hHash
;
863 if (!orghash
|| pdwReserved
|| !phHash
|| !orghash
->pProvider
||
864 orghash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
866 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
869 prov
= orghash
->pProvider
;
870 if (!prov
->pFuncs
->pCPDuplicateHash
)
871 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED
);
873 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
874 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
876 newhash
->pProvider
= prov
;
877 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
879 *phHash
= (HCRYPTHASH
)newhash
;
886 /******************************************************************************
887 * CryptDuplicateKey (ADVAPI32.@)
889 * Duplicate a key and the key's state.
892 * hKey [I] Handle of the key to copy.
893 * pdwReserved [I] Reserved for future use and must be NULL.
894 * dwFlags [I] Reserved for future use and must be zero.
895 * phKey [I] Address of the handle to the duplicated key.
901 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
904 PCRYPTKEY orgkey
, newkey
;
906 TRACE("(0x%lx, %p, %08lx, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
908 orgkey
= (PCRYPTKEY
)hKey
;
909 if (!orgkey
|| pdwReserved
|| !phKey
|| !orgkey
->pProvider
||
910 orgkey
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
912 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
915 prov
= orgkey
->pProvider
;
916 if (!prov
->pFuncs
->pCPDuplicateKey
)
917 CRYPT_ReturnLastError(ERROR_CALL_NOT_IMPLEMENTED
);
919 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
920 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
922 newkey
->pProvider
= prov
;
923 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
925 *phKey
= (HCRYPTKEY
)newkey
;
932 /******************************************************************************
933 * CryptEncrypt (ADVAPI32.@)
938 * hKey [I] Handle to the enryption key.
939 * hHash [I] Handle to a hash object.
940 * Final [I] TRUE if this is the last section to encrypt.
941 * dwFlags [I] Can be CRYPT_OAEP.
942 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
943 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
944 * encrypted data after call.
945 * dwBufLen [I] Length of the input pbData buffer.
952 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
953 * required for the returned data in pdwDataLen.
955 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
956 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
959 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
960 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
962 TRACE("(0x%lx, 0x%lx, %d, %08lx, %p, %p, %ld)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
964 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
965 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
967 prov
= key
->pProvider
;
968 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
969 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
972 /******************************************************************************
973 * CryptEnumProvidersW (ADVAPI32.@)
975 * Returns the next availabe CSP.
978 * dwIndex [I] Index of the next provider to be enumerated.
979 * pdwReserved [I] Reserved for future use and must be NULL.
980 * dwFlags [I] Reserved for future use and must be zero.
981 * pdwProvType [O] DWORD designating the type of the provider.
982 * pszProvName [O] Buffer that receives data from the provider.
983 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
984 * of bytes stored in the buffer on return.
991 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
992 * for memory allocation purposes.
994 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
995 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
998 static const WCHAR providerW
[] = {
999 'S','o','f','t','w','a','r','e','\\',
1000 'M','i','c','r','o','s','o','f','t','\\',
1001 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1002 'D','e','f','a','u','l','t','s','\\',
1003 'P','r','o','v','i','d','e','r',0
1005 static const WCHAR typeW
[] = {'T','y','p','e',0};
1007 TRACE("(%ld, %p, %ld, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1008 pdwProvType
, pszProvName
, pcbProvName
);
1010 if (pdwReserved
|| !pcbProvName
) CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1011 if (dwFlags
) CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1013 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, providerW
, &hKey
))
1014 CRYPT_ReturnLastError(NTE_FAIL
);
1021 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1022 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1024 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1025 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1027 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1028 CRYPT_Free(provNameW
);
1030 *pcbProvName
*= sizeof(WCHAR
);
1032 if (dwIndex
>= numkeys
)
1033 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS
);
1035 DWORD size
= sizeof(DWORD
);
1039 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1041 CRYPT_ReturnLastError(result
);
1042 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1044 if (RegQueryValueExW(subkey
, typeW
, NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1046 RegCloseKey(subkey
);
1052 /******************************************************************************
1053 * CryptEnumProvidersA (ADVAPI32.@)
1055 * see CryptEnumProvidersW
1057 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1058 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1062 BOOL ret
; /* = FALSE; */
1064 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1065 pdwProvType
, pszProvName
, pcbProvName
);
1067 CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &strlen
);
1068 if ( pszProvName
&& !(str
= CRYPT_Alloc(strlen
)) )
1069 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1070 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1072 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1073 *pcbProvName
= strlen
/ sizeof(WCHAR
); /* FIXME: not correct */
1077 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1078 CRYPT_ReturnLastError(ERROR_MORE_DATA
);
1083 /******************************************************************************
1084 * CryptEnumProviderTypesW (ADVAPI32i.@)
1086 * Retrieves the next type of CSP supported.
1089 * dwIndex [I] Index of the next provider to be enumerated.
1090 * pdwReserved [I] Reserved for future use and must be NULL.
1091 * dwFlags [I] Reserved for future use and must be zero.
1092 * pdwProvType [O] DWORD designating the type of the provider.
1093 * pszTypeName [O] Buffer that receives data from the provider type.
1094 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1095 * of bytes stored in the buffer on return.
1102 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1103 * for memory allocation purposes.
1105 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1106 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1109 DWORD keylen
, numkeys
, dwType
;
1112 static const WCHAR KEYSTR
[] = {
1113 'S','o','f','t','w','a','r','e','\\',
1114 'M','i','c','r','o','s','o','f','t','\\',
1115 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1116 'D','e','f','a','u','l','t','s','\\',
1117 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1119 static const WCHAR typenameW
[] = {'T','y','p','e','N','a','m','e',0};
1121 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1122 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1124 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1125 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1126 if (dwFlags
) CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1128 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, KEYSTR
, &hKey
))
1131 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1132 if (dwIndex
>= numkeys
)
1133 CRYPT_ReturnLastError(ERROR_NO_MORE_ITEMS
);
1135 if ( !(keyname
= CRYPT_Alloc(keylen
*sizeof(WCHAR
))) )
1136 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1137 if ( RegEnumKeyW(hKey
, dwIndex
, keyname
, keylen
) ) {
1138 CRYPT_Free(keyname
);
1141 RegOpenKeyW(hKey
, keyname
, &hSubkey
);
1142 ch
= keyname
+ strlenW(keyname
);
1143 /* Convert "Type 000" to 0, etc/ */
1144 *pdwProvType
= *(--ch
) - '0';
1145 *pdwProvType
+= (*(--ch
) - '0') * 10;
1146 *pdwProvType
+= (*(--ch
) - '0') * 100;
1147 CRYPT_Free(keyname
);
1149 result
= RegQueryValueExW(hSubkey
, typenameW
, NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1151 CRYPT_ReturnLastError(result
);
1153 RegCloseKey(hSubkey
);
1158 /******************************************************************************
1159 * CryptEnumProviderTypesA (ADVAPI32.@)
1161 * see CryptEnumProviderTypesW
1163 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1164 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1170 TRACE("(%ld, %p, %08lx, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1171 pdwProvType
, pszTypeName
, pcbTypeName
);
1173 CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &strlen
);
1174 if ( pszTypeName
&& !(str
= CRYPT_Alloc(strlen
)) )
1175 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1176 ret
= CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &strlen
);
1178 CRYPT_UnicodeToANSI(str
, &pszTypeName
, *pcbTypeName
);
1179 *pcbTypeName
= strlen
/ sizeof(WCHAR
);
1183 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1184 CRYPT_ReturnLastError(ERROR_MORE_DATA
);
1189 /******************************************************************************
1190 * CryptExportKey (ADVAPI32.@)
1192 * Exports a cryptographic key from a CSP.
1195 * hKey [I] Handle to the key to export.
1196 * hExpKey [I] Handle to a cryptographic key of the end user.
1197 * dwBlobType [I] Type of BLOB to be exported.
1198 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1199 * pbData [O] Buffer to receive BLOB data.
1200 * pdwDataLen [I/O] Specifies the size of pbData.
1207 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1208 * buffer needed to hold the BLOB.
1210 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1211 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1214 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1216 TRACE("(0x%lx, 0x%lx, %ld, %08lx, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1218 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1219 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1221 prov
= key
->pProvider
;
1222 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1223 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1226 /******************************************************************************
1227 * CryptGenKey (ADVAPI32.@)
1229 * Generates a random cryptographic session key or a pub/priv key pair.
1232 * hProv [I] Handle to a CSP.
1233 * Algid [I] Algorithm to use to make key.
1234 * dwFlags [I] Specifies type of key to make.
1235 * phKey [I] Address of the handle to which the new key is copied.
1241 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1243 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1246 TRACE("(0x%lx, %d, %08lx, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1249 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1250 if (!phKey
|| !prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1251 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1252 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1253 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1255 key
->pProvider
= prov
;
1257 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1259 *phKey
= (HCRYPTKEY
)key
;
1268 /******************************************************************************
1269 * CryptGetDefaultProviderW (ADVAPI32.@)
1271 * Finds the default CSP of a certain provider type.
1274 * dwProvType [I] Provider type to look for.
1275 * pdwReserved [I] Reserved for future use and must be NULL.
1276 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1277 * pszProvName [O] Name of the default CSP.
1278 * pcbProvName [I/O] Size of pszProvName
1285 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1286 * memory allocation purposes on return.
1288 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1289 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1294 static const WCHAR nameW
[] = {'N','a','m','e',0};
1296 if (pdwReserved
|| !pcbProvName
)
1297 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1298 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1299 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1300 if (dwProvType
> 999)
1301 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE
);
1302 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1303 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1304 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1306 CRYPT_Free(keyname
);
1307 CRYPT_ReturnLastError(NTE_PROV_TYPE_NOT_DEF
);
1309 CRYPT_Free(keyname
);
1311 result
= RegQueryValueExW(hKey
, nameW
, NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1314 if (result
!= ERROR_MORE_DATA
)
1315 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1317 SetLastError(result
);
1326 /******************************************************************************
1327 * CryptGetDefaultProviderA (ADVAPI32.@)
1329 * see CryptGetDefaultProviderW
1331 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1332 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1338 TRACE("(%ld, %p, %08lx, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1340 CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, NULL
, &strlen
);
1341 if ( pszProvName
&& !(str
= CRYPT_Alloc(strlen
)) )
1342 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1343 ret
= CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, str
, &strlen
);
1345 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1346 *pcbProvName
= strlen
/ sizeof(WCHAR
);
1350 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1351 CRYPT_ReturnLastError(ERROR_MORE_DATA
);
1356 /******************************************************************************
1357 * CryptGetHashParam (ADVAPI32.@)
1359 * Retrieves data that controls the operations of a hash object.
1362 * hHash [I] Handle of the hash object to question.
1363 * dwParam [I] Query type.
1364 * pbData [O] Buffer that receives the value data.
1365 * pdwDataLen [I/O] Size of the pbData buffer.
1366 * dwFlags [I] Reserved for future use and must be zero.
1373 * If pbData is NULL, pdwDataLen will contain the length required.
1375 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1376 DWORD
*pdwDataLen
, DWORD dwFlags
)
1379 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1381 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1383 if (!hash
|| !pdwDataLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1384 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1386 prov
= hash
->pProvider
;
1387 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1388 pbData
, pdwDataLen
, dwFlags
);
1391 /******************************************************************************
1392 * CryptGetKeyParam (ADVAPI32.@)
1394 * Retrieves data that controls the operations of a key.
1397 * hKey [I] Handle to they key in question.
1398 * dwParam [I] Specifies query type.
1399 * pbData [O] Sequence of bytes to receive data.
1400 * pdwDataLen [I/O] Size of pbData.
1401 * dwFlags [I] Reserved for future use and must be zero.
1408 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1410 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1411 DWORD
*pdwDataLen
, DWORD dwFlags
)
1414 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1416 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1418 if (!key
|| !pdwDataLen
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1419 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1421 prov
= key
->pProvider
;
1422 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1423 pbData
, pdwDataLen
, dwFlags
);
1426 /******************************************************************************
1427 * CryptGetProvParam (ADVAPI32.@)
1429 * Retrieves parameters that control the operations of a CSP.
1432 * hProv [I] Handle of the CSP in question.
1433 * dwParam [I] Specifies query type.
1434 * pbData [O] Buffer to receive the data.
1435 * pdwDataLen [I/O] Size of pbData.
1436 * dwFlags [I] see MSDN Docs.
1443 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1445 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1446 DWORD
*pdwDataLen
, DWORD dwFlags
)
1448 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1450 TRACE("(0x%lx, %ld, %p, %p, %08lx)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1452 if (!prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1453 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1455 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1458 /******************************************************************************
1459 * CryptGetUserKey (ADVAPI32.@)
1461 * Gets a handle of one of a user's two public/private key pairs.
1464 * hProv [I] Handle of a CSP.
1465 * dwKeySpec [I] Private key to use.
1466 * phUserKey [O] Pointer to the handle of the retrieved keys.
1472 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1474 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1477 TRACE("(0x%lx, %ld, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1480 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1481 if (!phUserKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1482 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1483 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1484 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1486 key
->pProvider
= prov
;
1488 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1490 *phUserKey
= (HCRYPTKEY
)key
;
1500 /******************************************************************************
1501 * CryptHashData (ADVAPI32.@)
1503 * Adds data to a hash object.
1506 * hHash [I] Handle of the hash object.
1507 * pbData [I] Buffer of data to be hashed.
1508 * dwDataLen [I] Number of bytes to add.
1509 * dwFlags [I] Can be CRYPT_USERDATA
1515 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, const BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1517 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1520 TRACE("(0x%lx, %p, %ld, %08lx)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1523 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1524 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1525 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1527 prov
= hash
->pProvider
;
1528 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1531 /******************************************************************************
1532 * CryptHashSessionKey (ADVAPI32.@)
1535 * hHash [I] Handle to the hash object.
1536 * hKey [I] Handle to the key to be hashed.
1537 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1543 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1545 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1546 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1549 TRACE("(0x%lx, 0x%lx, %08lx)\n", hHash
, hKey
, dwFlags
);
1552 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1554 if (!hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1555 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1557 prov
= hash
->pProvider
;
1558 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1561 /******************************************************************************
1562 * CryptImportKey (ADVAPI32.@)
1565 * hProv [I] Handle of a CSP.
1566 * pbData [I] Contains the key to be imported.
1567 * dwDataLen [I] Length of the key.
1568 * hPubKey [I] Cryptographic key that decrypts pdData
1569 * dwFlags [I] Used only with a public/private key pair.
1570 * phKey [O] Imported key.
1576 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, BYTE
*pbData
, DWORD dwDataLen
,
1577 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1579 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1580 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1582 TRACE("(0x%lx, %p, %ld, 0x%lx, %08lx, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1584 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1585 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1587 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1588 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1590 importkey
->pProvider
= prov
;
1591 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1592 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1594 *phKey
= (HCRYPTKEY
)importkey
;
1598 CRYPT_Free(importkey
);
1602 /******************************************************************************
1603 * CryptSignHashW (ADVAPI32.@)
1608 * hHash [I] Handle of the hash object to be signed.
1609 * dwKeySpec [I] Private key to use.
1610 * sDescription [I] Should be NULL.
1611 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1612 * pbSignature [O] Buffer of the signature data.
1613 * pdwSigLen [I/O] Size of the pbSignature buffer.
1620 * Because of security flaws sDescription should not be used and should thus be
1621 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1624 BOOL WINAPI
CryptSignHashW (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCWSTR sDescription
,
1625 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1627 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1630 TRACE("(0x%lx, %ld, %s, %08lx, %p, %p)\n",
1631 hHash
, dwKeySpec
, debugstr_w(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1634 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1635 if (!pdwSigLen
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1636 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1638 prov
= hash
->pProvider
;
1639 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, sDescription
,
1640 dwFlags
, pbSignature
, pdwSigLen
);
1643 /******************************************************************************
1644 * CryptSignHashA (ADVAPI32.@)
1646 * ASCII version of CryptSignHashW
1648 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1649 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1651 LPWSTR wsDescription
;
1654 TRACE("(0x%lx, %ld, %s, %08lx, %p, %p)\n",
1655 hHash
, dwKeySpec
, debugstr_a(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1657 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1658 result
= CryptSignHashW(hHash
, dwKeySpec
, wsDescription
, dwFlags
, pbSignature
, pdwSigLen
);
1659 if (wsDescription
) CRYPT_Free(wsDescription
);
1664 /******************************************************************************
1665 * CryptSetHashParam (ADVAPI32.@)
1667 * Customizes the operations of a hash object.
1670 * hHash [I] Handle of the hash object to set parameters.
1671 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1672 * pbData [I] Value data buffer.
1673 * dwFlags [I] Reserved for future use and must be zero.
1679 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1682 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1684 TRACE("(0x%lx, %ld, %p, %08lx)\n", hHash
, dwParam
, pbData
, dwFlags
);
1686 if (!hash
|| !pbData
|| !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1687 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1689 prov
= hash
->pProvider
;
1690 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1691 dwParam
, pbData
, dwFlags
);
1694 /******************************************************************************
1695 * CryptSetKeyParam (ADVAPI32.@)
1697 * Customizes a session key's operations.
1700 * hKey [I] Handle to the key to set values.
1701 * dwParam [I] See MSDN Doc.
1702 * pbData [I] Buffer of values to set.
1703 * dwFlags [I] Only used when dwParam == KP_ALGID.
1709 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1712 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1714 TRACE("(0x%lx, %ld, %p, %08lx)\n", hKey
, dwParam
, pbData
, dwFlags
);
1716 if (!key
|| !pbData
|| !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1717 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1719 prov
= key
->pProvider
;
1720 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
1721 dwParam
, pbData
, dwFlags
);
1724 /******************************************************************************
1725 * CryptSetProviderA (ADVAPI32.@)
1727 * Specifies the current user's default CSP.
1730 * pszProvName [I] Name of the new default CSP.
1731 * dwProvType [I] Provider type of the CSP.
1737 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
1739 TRACE("(%s, %ld)\n", pszProvName
, dwProvType
);
1740 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1743 /******************************************************************************
1744 * CryptSetProviderW (ADVAPI32.@)
1746 * See CryptSetProviderA
1748 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
1750 TRACE("(%s, %ld)\n", debugstr_w(pszProvName
), dwProvType
);
1751 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
1754 /******************************************************************************
1755 * CryptSetProviderExW (ADVAPI32.@)
1757 * Specifies the default CSP.
1760 * pszProvName [I] Name of the new default CSP.
1761 * dwProvType [I] Provider type of the CSP.
1762 * pdwReserved [I] Reserved for future use and must be NULL.
1763 * dwFlags [I] See MSDN Doc.
1769 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
1771 HKEY hProvKey
, hTypeKey
;
1773 static const WCHAR nameW
[] = {'N','a','m','e',0};
1775 TRACE("(%s, %ld, %p, %08lx)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
1777 if (!pszProvName
|| pdwReserved
)
1778 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1779 if (dwProvType
> MAXPROVTYPES
)
1780 CRYPT_ReturnLastError(NTE_BAD_PROV_TYPE
);
1781 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
1782 || dwFlags
== CRYPT_DELETE_DEFAULT
)
1783 CRYPT_ReturnLastError(NTE_BAD_FLAGS
);
1785 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
1786 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1787 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
1788 keyname
, &hTypeKey
))
1790 CRYPT_Free(keyname
);
1791 CRYPT_ReturnLastError(NTE_BAD_PROVIDER
);
1793 CRYPT_Free(keyname
);
1795 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
1797 RegDeleteValueW(hTypeKey
, nameW
);
1801 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
1803 RegCloseKey(hTypeKey
);
1804 CRYPT_ReturnLastError(ERROR_NOT_ENOUGH_MEMORY
);
1806 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
1807 keyname
, &hProvKey
))
1809 CRYPT_Free(keyname
);
1810 RegCloseKey(hTypeKey
);
1811 CRYPT_ReturnLastError(NTE_BAD_PROVIDER
);
1813 CRYPT_Free(keyname
);
1815 if (RegSetValueExW(hTypeKey
, nameW
, 0, REG_SZ
, (LPBYTE
)pszProvName
, (strlenW(pszProvName
) + 1)*sizeof(WCHAR
)))
1817 RegCloseKey(hTypeKey
);
1818 RegCloseKey(hProvKey
);
1822 RegCloseKey(hProvKey
);
1824 RegCloseKey(hTypeKey
);
1829 /******************************************************************************
1830 * CryptSetProviderExA (ADVAPI32.@)
1832 * See CryptSetProviderExW
1834 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
1839 TRACE("(%s, %ld, %p, %08lx)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
1841 if (CRYPT_ANSIToUnicode(pszProvName
, &str
, -1))
1843 ret
= CryptSetProviderExW(str
, dwProvType
, pdwReserved
, dwFlags
);
1849 /******************************************************************************
1850 * CryptSetProvParam (ADVAPI32.@)
1852 * Customizes the operations of a CSP.
1855 * hProv [I] Handle of a CSP.
1856 * dwParam [I] See MSDN Doc.
1857 * pbData [I] Buffer that contains a value to set as a parameter.
1858 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
1864 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
1866 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1868 TRACE("(0x%lx, %ld, %p, %08lx)\n", hProv
, dwParam
, pbData
, dwFlags
);
1871 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1872 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1873 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1874 if (dwFlags
& PP_USE_HARDWARE_RNG
)
1876 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
1877 FIXME("\tLetting the CSP decide.\n");
1879 if (dwFlags
& PP_CLIENT_HWND
)
1881 /* FIXME: Should verify the parameter */
1882 if (pbData
/* && IsWindow((HWND)pbData) */)
1884 crypt_hWindow
= (HWND
)(pbData
);
1887 SetLastError(ERROR_INVALID_PARAMETER
);
1891 /* All other flags go to the CSP */
1892 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
1895 /******************************************************************************
1896 * CryptVerifySignatureW (ADVAPI32.@)
1898 * Verifies the signature of a hash object.
1901 * hHash [I] Handle of the hash object to verify.
1902 * pbSignature [I] Signature data to verify.
1903 * dwSigLen [I] Size of pbSignature.
1904 * hPubKey [I] Handle to the public key to authenticate signature.
1905 * sDescription [I] Should be NULL.
1906 * dwFlags [I] See MSDN doc.
1913 * Because of security flaws sDescription should not be used and should thus be
1914 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1917 BOOL WINAPI
CryptVerifySignatureW (HCRYPTHASH hHash
, BYTE
*pbSignature
, DWORD dwSigLen
,
1918 HCRYPTKEY hPubKey
, LPCWSTR sDescription
, DWORD dwFlags
)
1920 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1921 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
1924 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08lx)\n", hHash
, pbSignature
,
1925 dwSigLen
, hPubKey
, debugstr_w(sDescription
), dwFlags
);
1928 CRYPT_ReturnLastError(ERROR_INVALID_HANDLE
);
1929 if (!pbSignature
|| !dwSigLen
||
1930 !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
||
1931 !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1933 CRYPT_ReturnLastError(ERROR_INVALID_PARAMETER
);
1936 prov
= hash
->pProvider
;
1937 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
1938 key
->hPrivate
, sDescription
, dwFlags
);
1941 /******************************************************************************
1942 * CryptVerifySignatureA (ADVAPI32.@)
1944 * ASCII version of CryptVerifySignatureW
1946 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, BYTE
*pbSignature
, DWORD dwSigLen
,
1947 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
1949 LPWSTR wsDescription
;
1952 TRACE("(0x%lx, %p, %ld, 0x%lx, %s, %08lx)\n", hHash
, pbSignature
,
1953 dwSigLen
, hPubKey
, debugstr_a(sDescription
), dwFlags
);
1955 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1956 result
= CryptVerifySignatureW(hHash
, pbSignature
, dwSigLen
, hPubKey
, wsDescription
, dwFlags
);
1957 if (wsDescription
) CRYPT_Free(wsDescription
);
1962 /******************************************************************************
1963 * SystemFunction036 (ADVAPI32.@)
1965 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
1968 * pbBufer [O] Pointer to memory to receive random bytes.
1969 * dwLen [I] Number of random bytes to fetch.
1972 BOOL WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
1976 /* FIXME: /dev/urandom does not provide random numbers of a sufficient
1977 * quality for cryptographic applications. /dev/random is much better,
1978 * but it blocks if the kernel has not yet collected enough entropy for
1979 * the request, which will suspend the calling thread for an indefinite
1980 * amount of time. */
1981 dev_random
= open("/dev/urandom", O_RDONLY
);
1982 if (dev_random
!= -1)
1984 if (read(dev_random
, pbBuffer
, dwLen
) == (ssize_t
)dwLen
)
1991 SetLastError(NTE_FAIL
);
1996 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2000 /******************************************************************************
2001 * SystemFunction040 (ADVAPI32.@)
2003 * MSDN documents this function as RtlEncryptMemory.
2006 * memory [I/O] Pointer to memory to encrypt.
2007 * length [I] Length of region to encrypt in bytes.
2008 * flags [I] Control whether other processes are able to decrypt the memory.
2009 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2010 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2011 * RTL_ENCRYPT_OPTION_SAME_LOGON
2014 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2015 * If flags are specified when encrypting, the same flag value must be given
2016 * when decrypting the memory.
2018 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
)
2020 FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
2021 return STATUS_SUCCESS
;
2024 /******************************************************************************
2025 * SystemFunction041 (ADVAPI32.@)
2027 * MSDN documents this function as RtlDecryptMemory.
2030 * memory [I/O] Pointer to memory to decrypt.
2031 * length [I] Length of region to decrypt in bytes.
2032 * flags [I] Control whether other processes are able to decrypt the memory.
2033 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2034 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2035 * RTL_ENCRYPT_OPTION_SAME_LOGON
2038 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2039 * If flags are specified when encrypting, the same flag value must be given
2040 * when decrypting the memory.
2042 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
)
2044 FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
2045 return STATUS_SUCCESS
;