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
));
323 /******************************************************************************
324 * CloseEncryptedFileRaw (ADVAPI32.@)
326 * Close encrypted files
329 * context [I] pointer to the context
331 * Success: ERROR_SUCCESS
332 * Failure: NTSTATUS error code
334 void WINAPI
CloseEncryptedFileRaw(PVOID context
)
336 FIXME("(%p): stub\n", context
);
339 /******************************************************************************
340 * CryptAcquireContextW (ADVAPI32.@)
342 * Acquire a crypto provider context handle.
345 * phProv [O] Pointer to HCRYPTPROV for the output.
346 * pszContainer [I] Key Container Name
347 * pszProvider [I] Cryptographic Service Provider Name
348 * dwProvType [I] Crypto provider type to get a handle.
349 * dwFlags [I] flags for the operation
352 * TRUE on success, FALSE on failure.
354 BOOL WINAPI
CryptAcquireContextW (HCRYPTPROV
*phProv
, LPCWSTR pszContainer
,
355 LPCWSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
357 PCRYPTPROV pProv
= NULL
;
359 PWSTR imagepath
= NULL
, keyname
= NULL
, provname
= NULL
, temp
= NULL
;
360 PSTR provnameA
= NULL
, pszContainerA
= NULL
;
361 DWORD keytype
, type
, len
;
363 static const WCHAR nameW
[] = {'N','a','m','e',0};
364 static const WCHAR typeW
[] = {'T','y','p','e',0};
365 static const WCHAR imagepathW
[] = {'I','m','a','g','e',' ','P','a','t','h',0};
367 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_w(pszContainer
),
368 debugstr_w(pszProvider
), dwProvType
, dwFlags
);
370 if (dwProvType
< 1 || dwProvType
> MAXPROVTYPES
)
372 SetLastError(NTE_BAD_PROV_TYPE
);
378 SetLastError(ERROR_INVALID_PARAMETER
);
382 /* Make sure the MachineGuid value exists */
383 CRYPT_CreateMachineGuid();
385 if (!pszProvider
|| !*pszProvider
)
387 /* No CSP name specified so try the user default CSP first
388 * then try the machine default CSP
390 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, TRUE
)) ) {
391 TRACE("No provider registered for crypto provider type %d.\n", dwProvType
);
392 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
395 if (RegOpenKeyW(HKEY_CURRENT_USER
, keyname
, &key
))
398 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, FALSE
)) ) {
399 TRACE("No type registered for crypto provider type %d.\n", dwProvType
);
401 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
404 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
)) {
405 TRACE("Did not find registry entry of crypto provider for %s.\n", debugstr_w(keyname
));
408 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
413 r
= RegQueryValueExW(key
, nameW
, NULL
, &keytype
, NULL
, &len
);
414 if( r
!= ERROR_SUCCESS
|| !len
|| keytype
!= REG_SZ
)
416 TRACE("error %d reading size of 'Name' from registry\n", r
);
418 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
421 if(!(provname
= CRYPT_Alloc(len
)))
424 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
427 r
= RegQueryValueExW(key
, nameW
, NULL
, NULL
, (LPBYTE
)provname
, &len
);
428 if( r
!= ERROR_SUCCESS
)
430 TRACE("error %d reading 'Name' from registry\n", r
);
432 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
437 if ( !(provname
= CRYPT_Alloc((strlenW(pszProvider
) +1)*sizeof(WCHAR
))) )
439 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
442 strcpyW(provname
, pszProvider
);
445 keyname
= CRYPT_GetProvKeyName(provname
);
446 r
= RegOpenKeyW(HKEY_LOCAL_MACHINE
, keyname
, &key
);
448 if (r
!= ERROR_SUCCESS
)
450 SetLastError(NTE_KEYSET_NOT_DEF
);
454 r
= RegQueryValueExW(key
, typeW
, NULL
, NULL
, (BYTE
*)&type
, &len
);
455 if (r
!= ERROR_SUCCESS
)
457 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
460 if (type
!= dwProvType
)
462 TRACE("Crypto provider has wrong type (%d vs expected %d).\n", type
, dwProvType
);
463 SetLastError(NTE_PROV_TYPE_NO_MATCH
);
467 r
= RegQueryValueExW(key
, imagepathW
, NULL
, &keytype
, NULL
, &len
);
468 if ( r
!= ERROR_SUCCESS
|| keytype
!= REG_SZ
)
470 TRACE("error %d reading size of 'Image Path' from registry\n", r
);
472 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
475 if (!(temp
= CRYPT_Alloc(len
)))
478 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
481 r
= RegQueryValueExW(key
, imagepathW
, NULL
, NULL
, (LPBYTE
)temp
, &len
);
482 if( r
!= ERROR_SUCCESS
)
484 TRACE("error %d reading 'Image Path' from registry\n", r
);
486 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
490 len
= ExpandEnvironmentStringsW(temp
, NULL
, 0);
491 if ( !(imagepath
= CRYPT_Alloc(len
*sizeof(WCHAR
))) )
493 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
496 if (!ExpandEnvironmentStringsW(temp
, imagepath
, len
))
498 /* ExpandEnvironmentStrings will call SetLastError */
501 pProv
= CRYPT_LoadProvider(imagepath
);
503 /* CRYPT_LoadProvider calls SetLastError */
506 pProv
->pVTable
->dwProvType
= dwProvType
;
507 if(!CRYPT_UnicodeToANSI(provname
, &provnameA
, 0))
509 /* CRYPT_UnicodeToANSI calls SetLastError */
512 pProv
->pVTable
->pszProvName
= provnameA
;
513 if(!CRYPT_UnicodeToANSI(pszContainer
, &pszContainerA
, 0))
515 /* CRYPT_UnicodeToANSI calls SetLastError */
518 if (pProv
->pFuncs
->pCPAcquireContext(&pProv
->hPrivate
, pszContainerA
, dwFlags
, pProv
->pVTable
))
520 /* MSDN: When this flag is set, the value returned in phProv is undefined,
521 * and thus, the CryptReleaseContext function need not be called afterwards.
522 * Therefore, we must clean up everything now.
524 if (dwFlags
& CRYPT_DELETEKEYSET
)
527 FreeLibrary(pProv
->hModule
);
528 CRYPT_Free(provnameA
);
529 CRYPT_Free(pProv
->pVTable
);
530 CRYPT_Free(pProv
->pFuncs
);
533 *phProv
= (HCRYPTPROV
)pProv
;
535 CRYPT_Free(pszContainerA
);
536 CRYPT_Free(provname
);
538 CRYPT_Free(imagepath
);
541 /* FALLTHROUGH TO ERROR IF FALSE - CSP internal error! */
547 FreeLibrary(pProv
->hModule
);
548 CRYPT_Free(pProv
->pVTable
);
549 CRYPT_Free(pProv
->pFuncs
);
552 CRYPT_Free(pszContainerA
);
553 CRYPT_Free(provnameA
);
554 CRYPT_Free(provname
);
556 CRYPT_Free(imagepath
);
560 /******************************************************************************
561 * CryptAcquireContextA (ADVAPI32.@)
563 * See CryptAcquireContextW.
565 BOOL WINAPI
CryptAcquireContextA (HCRYPTPROV
*phProv
, LPCSTR pszContainer
,
566 LPCSTR pszProvider
, DWORD dwProvType
, DWORD dwFlags
)
568 PWSTR pProvider
= NULL
, pContainer
= NULL
;
571 TRACE("(%p, %s, %s, %d, %08x)\n", phProv
, debugstr_a(pszContainer
),
572 debugstr_a(pszProvider
), dwProvType
, dwFlags
);
574 if ( !CRYPT_ANSIToUnicode(pszContainer
, &pContainer
, -1) )
576 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
579 if ( !CRYPT_ANSIToUnicode(pszProvider
, &pProvider
, -1) )
581 CRYPT_Free(pContainer
);
582 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
586 ret
= CryptAcquireContextW(phProv
, pContainer
, pProvider
, dwProvType
, dwFlags
);
588 CRYPT_Free(pContainer
);
589 CRYPT_Free(pProvider
);
594 /******************************************************************************
595 * CryptContextAddRef (ADVAPI32.@)
597 * Increases reference count of a cryptographic service provider handle
601 * hProv [I] Handle to the CSP whose reference is being incremented.
602 * pdwReserved [IN] Reserved for future use and must be NULL.
603 * dwFlags [I] Reserved for future use and must be NULL.
609 BOOL WINAPI
CryptContextAddRef (HCRYPTPROV hProv
, DWORD
*pdwReserved
, DWORD dwFlags
)
611 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
613 TRACE("(0x%lx, %p, %08x)\n", hProv
, pdwReserved
, dwFlags
);
617 SetLastError(NTE_BAD_UID
);
621 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
623 SetLastError(ERROR_INVALID_PARAMETER
);
631 /******************************************************************************
632 * CryptReleaseContext (ADVAPI32.@)
634 * Releases the handle of a CSP. Reference count is decreased.
637 * hProv [I] Handle of a CSP.
638 * dwFlags [I] Reserved for future use and must be NULL.
644 BOOL WINAPI
CryptReleaseContext (HCRYPTPROV hProv
, ULONG_PTR dwFlags
)
646 PCRYPTPROV pProv
= (PCRYPTPROV
)hProv
;
649 TRACE("(0x%lx, %08lx)\n", hProv
, dwFlags
);
653 SetLastError(NTE_BAD_UID
);
657 if (pProv
->dwMagic
!= MAGIC_CRYPTPROV
)
659 SetLastError(ERROR_INVALID_PARAMETER
);
664 if (pProv
->refcount
<= 0)
666 ret
= pProv
->pFuncs
->pCPReleaseContext(pProv
->hPrivate
, dwFlags
);
668 FreeLibrary(pProv
->hModule
);
670 CRYPT_Free(pProv
->pVTable
->pContextInfo
);
672 CRYPT_Free(pProv
->pVTable
->pszProvName
);
673 CRYPT_Free(pProv
->pVTable
);
674 CRYPT_Free(pProv
->pFuncs
);
680 /******************************************************************************
681 * CryptGenRandom (ADVAPI32.@)
683 * Fills a buffer with cryptographically random bytes.
686 * hProv [I] Handle of a CSP.
687 * dwLen [I] Number of bytes to generate.
688 * pbBuffer [I/O] Buffer to contain random bytes.
695 * pdBuffer must be at least dwLen bytes long.
697 BOOL WINAPI
CryptGenRandom (HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
699 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
701 TRACE("(0x%lx, %d, %p)\n", hProv
, dwLen
, pbBuffer
);
705 SetLastError(ERROR_INVALID_HANDLE
);
709 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
711 SetLastError(ERROR_INVALID_PARAMETER
);
715 return prov
->pFuncs
->pCPGenRandom(prov
->hPrivate
, dwLen
, pbBuffer
);
718 /******************************************************************************
719 * CryptCreateHash (ADVAPI32.@)
721 * Initiates the hashing of a stream of data.
724 * hProv [I] Handle of a CSP.
725 * Algid [I] Identifies the hash algorithm to use.
726 * hKey [I] Key for the hash (if required).
727 * dwFlags [I] Reserved for future use and must be NULL.
728 * phHash [O] Address of the future handle to the new hash object.
735 * If the algorithm is a keyed hash, hKey is the key.
737 BOOL WINAPI
CryptCreateHash (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
,
738 DWORD dwFlags
, HCRYPTHASH
*phHash
)
740 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
741 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
744 TRACE("(0x%lx, 0x%x, 0x%lx, %08x, %p)\n", hProv
, Algid
, hKey
, dwFlags
, phHash
);
746 if (!prov
|| !phHash
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
||
747 (key
&& key
->dwMagic
!= MAGIC_CRYPTKEY
))
749 SetLastError(ERROR_INVALID_PARAMETER
);
754 SetLastError(NTE_BAD_FLAGS
);
757 if ( !(hash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
759 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
763 hash
->pProvider
= prov
;
764 hash
->dwMagic
= MAGIC_CRYPTHASH
;
765 if (prov
->pFuncs
->pCPCreateHash(prov
->hPrivate
, Algid
,
766 key
? key
->hPrivate
: 0, 0, &hash
->hPrivate
))
768 *phHash
= (HCRYPTHASH
)hash
;
779 /******************************************************************************
780 * CryptDecrypt (ADVAPI32.@)
782 * Decrypts data encrypted by CryptEncrypt.
785 * hKey [I] Handle to the decryption key.
786 * hHash [I] Handle to a hash object.
787 * Final [I] TRUE if this is the last section to be decrypted.
788 * dwFlags [I] Reserved for future use. Can be CRYPT_OAEP.
789 * pbData [I/O] Buffer that holds the encrypted data. Holds decrypted
791 * pdwDataLen [I/O] Length of pbData before and after the call.
797 BOOL WINAPI
CryptDecrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
798 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
801 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
802 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
804 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
806 if (!key
|| !pbData
|| !pdwDataLen
||
807 !key
->pProvider
|| key
->dwMagic
!= MAGIC_CRYPTKEY
||
808 key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
810 SetLastError(ERROR_INVALID_PARAMETER
);
814 prov
= key
->pProvider
;
815 return prov
->pFuncs
->pCPDecrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
816 Final
, dwFlags
, pbData
, pdwDataLen
);
819 /******************************************************************************
820 * CryptDeriveKey (ADVAPI32.@)
822 * Generates session keys derived from a base data value.
825 * hProv [I] Handle to a CSP.
826 * Algid [I] Identifies the symmetric encryption algorithm to use.
827 * hBaseData [I] Handle to a hash object.
828 * dwFlags [I] Type of key to generate.
829 * phKey [I/O] Address of the newly generated key.
835 BOOL WINAPI
CryptDeriveKey (HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
836 DWORD dwFlags
, HCRYPTKEY
*phKey
)
838 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
839 PCRYPTHASH hash
= (PCRYPTHASH
)hBaseData
;
842 TRACE("(0x%lx, 0x%08x, 0x%lx, 0x%08x, %p)\n", hProv
, Algid
, hBaseData
, dwFlags
, phKey
);
846 SetLastError(ERROR_INVALID_HANDLE
);
849 if (!phKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
)
851 SetLastError(ERROR_INVALID_PARAMETER
);
854 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
856 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
860 key
->pProvider
= prov
;
861 key
->dwMagic
= MAGIC_CRYPTKEY
;
862 if (prov
->pFuncs
->pCPDeriveKey(prov
->hPrivate
, Algid
, hash
->hPrivate
, dwFlags
, &key
->hPrivate
))
864 *phKey
= (HCRYPTKEY
)key
;
875 /******************************************************************************
876 * CryptDestroyHash (ADVAPI32.@)
878 * Destroys the hash object referenced by hHash.
881 * hHash [I] Handle of the hash object to be destroyed.
887 BOOL WINAPI
CryptDestroyHash (HCRYPTHASH hHash
)
889 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
893 TRACE("(0x%lx)\n", hHash
);
897 SetLastError(ERROR_INVALID_HANDLE
);
901 if (!hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
902 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
904 SetLastError(ERROR_INVALID_PARAMETER
);
908 prov
= hash
->pProvider
;
909 ret
= prov
->pFuncs
->pCPDestroyHash(prov
->hPrivate
, hash
->hPrivate
);
915 /******************************************************************************
916 * CryptDestroyKey (ADVAPI32.@)
918 * Releases the handle referenced by hKey.
921 * hKey [I] Handle of the key to be destroyed.
927 BOOL WINAPI
CryptDestroyKey (HCRYPTKEY hKey
)
929 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
933 TRACE("(0x%lx)\n", hKey
);
937 SetLastError(ERROR_INVALID_HANDLE
);
941 if (!key
->pProvider
|| key
->dwMagic
!= MAGIC_CRYPTKEY
||
942 key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
944 SetLastError(ERROR_INVALID_PARAMETER
);
948 prov
= key
->pProvider
;
949 ret
= prov
->pFuncs
->pCPDestroyKey(prov
->hPrivate
, key
->hPrivate
);
955 /******************************************************************************
956 * CryptDuplicateHash (ADVAPI32.@)
961 * hHash [I] Handle to the hash to be copied.
962 * pdwReserved [I] Reserved for future use and must be zero.
963 * dwFlags [I] Reserved for future use and must be zero.
964 * phHash [O] Address of the handle to receive the copy.
970 BOOL WINAPI
CryptDuplicateHash (HCRYPTHASH hHash
, DWORD
*pdwReserved
,
971 DWORD dwFlags
, HCRYPTHASH
*phHash
)
974 PCRYPTHASH orghash
, newhash
;
976 TRACE("(0x%lx, %p, %08x, %p)\n", hHash
, pdwReserved
, dwFlags
, phHash
);
978 orghash
= (PCRYPTHASH
)hHash
;
979 if (!orghash
|| pdwReserved
|| !phHash
|| !orghash
->pProvider
||
980 orghash
->dwMagic
!= MAGIC_CRYPTHASH
|| orghash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
982 SetLastError(ERROR_INVALID_PARAMETER
);
986 prov
= orghash
->pProvider
;
987 if (!prov
->pFuncs
->pCPDuplicateHash
)
989 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
993 if ( !(newhash
= CRYPT_Alloc(sizeof(CRYPTHASH
))) )
995 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
999 newhash
->pProvider
= prov
;
1000 newhash
->dwMagic
= MAGIC_CRYPTHASH
;
1001 if (prov
->pFuncs
->pCPDuplicateHash(prov
->hPrivate
, orghash
->hPrivate
, pdwReserved
, dwFlags
, &newhash
->hPrivate
))
1003 *phHash
= (HCRYPTHASH
)newhash
;
1006 newhash
->dwMagic
= 0;
1007 CRYPT_Free(newhash
);
1011 /******************************************************************************
1012 * CryptDuplicateKey (ADVAPI32.@)
1014 * Duplicate a key and the key's state.
1017 * hKey [I] Handle of the key to copy.
1018 * pdwReserved [I] Reserved for future use and must be NULL.
1019 * dwFlags [I] Reserved for future use and must be zero.
1020 * phKey [I] Address of the handle to the duplicated key.
1026 BOOL WINAPI
CryptDuplicateKey (HCRYPTKEY hKey
, DWORD
*pdwReserved
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1029 PCRYPTKEY orgkey
, newkey
;
1031 TRACE("(0x%lx, %p, %08x, %p)\n", hKey
, pdwReserved
, dwFlags
, phKey
);
1033 orgkey
= (PCRYPTKEY
)hKey
;
1034 if (!orgkey
|| pdwReserved
|| !phKey
|| !orgkey
->pProvider
||
1035 orgkey
->dwMagic
!= MAGIC_CRYPTKEY
||
1036 orgkey
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1038 SetLastError(ERROR_INVALID_PARAMETER
);
1042 prov
= orgkey
->pProvider
;
1043 if (!prov
->pFuncs
->pCPDuplicateKey
)
1045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1049 if ( !(newkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1051 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1055 newkey
->pProvider
= prov
;
1056 newkey
->dwMagic
= MAGIC_CRYPTKEY
;
1057 if (prov
->pFuncs
->pCPDuplicateKey(prov
->hPrivate
, orgkey
->hPrivate
, pdwReserved
, dwFlags
, &newkey
->hPrivate
))
1059 *phKey
= (HCRYPTKEY
)newkey
;
1062 newkey
->dwMagic
= 0;
1067 /******************************************************************************
1068 * CryptEncrypt (ADVAPI32.@)
1073 * hKey [I] Handle to the encryption key.
1074 * hHash [I] Handle to a hash object.
1075 * Final [I] TRUE if this is the last section to encrypt.
1076 * dwFlags [I] Can be CRYPT_OAEP.
1077 * pbData [I/O] Data to be encrypted. Contains encrypted data after call.
1078 * pdwDataLen [I/O] Length of the data to encrypt. Contains the length of the
1079 * encrypted data after call.
1080 * dwBufLen [I] Length of the input pbData buffer.
1087 * If pbData is NULL, CryptEncrypt determines stores the number of bytes
1088 * required for the returned data in pdwDataLen.
1090 BOOL WINAPI
CryptEncrypt (HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1091 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1094 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1095 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1097 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1099 if (!key
|| !pdwDataLen
|| !key
->pProvider
||
1100 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1102 SetLastError(ERROR_INVALID_PARAMETER
);
1106 prov
= key
->pProvider
;
1107 return prov
->pFuncs
->pCPEncrypt(prov
->hPrivate
, key
->hPrivate
, hash
? hash
->hPrivate
: 0,
1108 Final
, dwFlags
, pbData
, pdwDataLen
, dwBufLen
);
1111 /******************************************************************************
1112 * CryptEnumProvidersW (ADVAPI32.@)
1114 * Returns the next available CSP.
1117 * dwIndex [I] Index of the next provider to be enumerated.
1118 * pdwReserved [I] Reserved for future use and must be NULL.
1119 * dwFlags [I] Reserved for future use and must be zero.
1120 * pdwProvType [O] DWORD designating the type of the provider.
1121 * pszProvName [O] Buffer that receives data from the provider.
1122 * pcbProvName [I/O] Specifies the size of pszProvName. Contains the number
1123 * of bytes stored in the buffer on return.
1130 * If pszProvName is NULL, CryptEnumProvidersW sets the size of the name
1131 * for memory allocation purposes.
1133 BOOL WINAPI
CryptEnumProvidersW (DWORD dwIndex
, DWORD
*pdwReserved
,
1134 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1137 static const WCHAR providerW
[] = {
1138 'S','o','f','t','w','a','r','e','\\',
1139 'M','i','c','r','o','s','o','f','t','\\',
1140 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1141 'D','e','f','a','u','l','t','s','\\',
1142 'P','r','o','v','i','d','e','r',0
1144 static const WCHAR typeW
[] = {'T','y','p','e',0};
1147 TRACE("(%d, %p, %d, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1148 pdwProvType
, pszProvName
, pcbProvName
);
1150 if (pdwReserved
|| !pcbProvName
)
1152 SetLastError(ERROR_INVALID_PARAMETER
);
1157 SetLastError(NTE_BAD_FLAGS
);
1161 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, providerW
, &hKey
))
1163 SetLastError(NTE_FAIL
);
1173 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, pcbProvName
,
1174 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1176 if (!(provNameW
= CRYPT_Alloc(*pcbProvName
* sizeof(WCHAR
))))
1178 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1183 RegEnumKeyExW(hKey
, dwIndex
, provNameW
, pcbProvName
, NULL
, NULL
, NULL
, NULL
);
1184 CRYPT_Free(provNameW
);
1186 *pcbProvName
*= sizeof(WCHAR
);
1188 if (dwIndex
>= numkeys
)
1190 SetLastError(ERROR_NO_MORE_ITEMS
);
1194 DWORD size
= sizeof(DWORD
);
1198 result
= RegEnumKeyW(hKey
, dwIndex
, pszProvName
, *pcbProvName
/ sizeof(WCHAR
));
1201 SetLastError(result
);
1205 if (RegOpenKeyW(hKey
, pszProvName
, &subkey
))
1211 if (RegQueryValueExW(subkey
, typeW
, NULL
, NULL
, (BYTE
*)pdwProvType
, &size
))
1214 RegCloseKey(subkey
);
1220 /******************************************************************************
1221 * CryptEnumProvidersA (ADVAPI32.@)
1223 * See CryptEnumProvidersW.
1225 BOOL WINAPI
CryptEnumProvidersA (DWORD dwIndex
, DWORD
*pdwReserved
,
1226 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1232 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1233 pdwProvType
, pszProvName
, pcbProvName
);
1235 if(!CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1237 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1239 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1242 ret
= CryptEnumProvidersW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1244 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1245 *pcbProvName
= bufsize
/ sizeof(WCHAR
); /* FIXME: not correct */
1249 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1251 SetLastError(ERROR_MORE_DATA
);
1258 /******************************************************************************
1259 * CryptEnumProviderTypesW (ADVAPI32.@)
1261 * Retrieves the next type of CSP supported.
1264 * dwIndex [I] Index of the next provider to be enumerated.
1265 * pdwReserved [I] Reserved for future use and must be NULL.
1266 * dwFlags [I] Reserved for future use and must be zero.
1267 * pdwProvType [O] DWORD designating the type of the provider.
1268 * pszTypeName [O] Buffer that receives data from the provider type.
1269 * pcbTypeName [I/O] Specifies the size of pszTypeName. Contains the number
1270 * of bytes stored in the buffer on return.
1277 * If pszTypeName is NULL, CryptEnumProviderTypesW sets the size of the name
1278 * for memory allocation purposes.
1280 BOOL WINAPI
CryptEnumProviderTypesW (DWORD dwIndex
, DWORD
*pdwReserved
,
1281 DWORD dwFlags
, DWORD
*pdwProvType
, LPWSTR pszTypeName
, DWORD
*pcbTypeName
)
1284 DWORD keylen
, numkeys
, dwType
;
1287 static const WCHAR KEYSTR
[] = {
1288 'S','o','f','t','w','a','r','e','\\',
1289 'M','i','c','r','o','s','o','f','t','\\',
1290 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
1291 'D','e','f','a','u','l','t','s','\\',
1292 'P','r','o','v','i','d','e','r',' ','T','y','p','e','s',0
1294 static const WCHAR typenameW
[] = {'T','y','p','e','N','a','m','e',0};
1296 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
,
1297 dwFlags
, pdwProvType
, pszTypeName
, pcbTypeName
);
1299 if (pdwReserved
|| !pdwProvType
|| !pcbTypeName
)
1301 SetLastError(ERROR_INVALID_PARAMETER
);
1306 SetLastError(NTE_BAD_FLAGS
);
1310 if (RegOpenKeyW(HKEY_LOCAL_MACHINE
, KEYSTR
, &hKey
))
1313 RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, &numkeys
, &keylen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1314 if (dwIndex
>= numkeys
)
1316 SetLastError(ERROR_NO_MORE_ITEMS
);
1321 if ( !(keyname
= CRYPT_Alloc(keylen
*sizeof(WCHAR
))) )
1323 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1327 if ( RegEnumKeyW(hKey
, dwIndex
, keyname
, keylen
) ) {
1328 CRYPT_Free(keyname
);
1332 RegOpenKeyW(hKey
, keyname
, &hSubkey
);
1335 ch
= keyname
+ strlenW(keyname
);
1336 /* Convert "Type 000" to 0, etc/ */
1337 *pdwProvType
= *(--ch
) - '0';
1338 *pdwProvType
+= (*(--ch
) - '0') * 10;
1339 *pdwProvType
+= (*(--ch
) - '0') * 100;
1340 CRYPT_Free(keyname
);
1342 result
= RegQueryValueExW(hSubkey
, typenameW
, NULL
, &dwType
, (LPBYTE
)pszTypeName
, pcbTypeName
);
1345 SetLastError(result
);
1346 RegCloseKey(hSubkey
);
1350 RegCloseKey(hSubkey
);
1354 /******************************************************************************
1355 * CryptEnumProviderTypesA (ADVAPI32.@)
1357 * See CryptEnumProviderTypesW.
1359 BOOL WINAPI
CryptEnumProviderTypesA (DWORD dwIndex
, DWORD
*pdwReserved
,
1360 DWORD dwFlags
, DWORD
*pdwProvType
, LPSTR pszTypeName
, DWORD
*pcbTypeName
)
1366 TRACE("(%d, %p, %08x, %p, %p, %p)\n", dwIndex
, pdwReserved
, dwFlags
,
1367 pdwProvType
, pszTypeName
, pcbTypeName
);
1369 if(!CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, NULL
, &bufsize
))
1371 if ( pszTypeName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1373 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1376 ret
= CryptEnumProviderTypesW(dwIndex
, pdwReserved
, dwFlags
, pdwProvType
, str
, &bufsize
);
1378 CRYPT_UnicodeToANSI(str
, &pszTypeName
, *pcbTypeName
);
1379 *pcbTypeName
= bufsize
/ sizeof(WCHAR
);
1383 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1385 SetLastError(ERROR_MORE_DATA
);
1392 /******************************************************************************
1393 * CryptExportKey (ADVAPI32.@)
1395 * Exports a cryptographic key from a CSP.
1398 * hKey [I] Handle to the key to export.
1399 * hExpKey [I] Handle to a cryptographic key of the end user.
1400 * dwBlobType [I] Type of BLOB to be exported.
1401 * dwFlags [I] CRYPT_DESTROYKEY/SSL2_FALLBACK/OAEP.
1402 * pbData [O] Buffer to receive BLOB data.
1403 * pdwDataLen [I/O] Specifies the size of pbData.
1410 * if pbData is NULL, CryptExportKey sets pdwDataLen as the size of the
1411 * buffer needed to hold the BLOB.
1413 BOOL WINAPI
CryptExportKey (HCRYPTKEY hKey
, HCRYPTKEY hExpKey
, DWORD dwBlobType
,
1414 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1417 PCRYPTKEY key
= (PCRYPTKEY
)hKey
, expkey
= (PCRYPTKEY
)hExpKey
;
1419 TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey
, hExpKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1421 if (!key
|| !pdwDataLen
|| !key
->pProvider
||
1422 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1424 SetLastError(ERROR_INVALID_PARAMETER
);
1428 prov
= key
->pProvider
;
1429 return prov
->pFuncs
->pCPExportKey(prov
->hPrivate
, key
->hPrivate
, expkey
? expkey
->hPrivate
: 0,
1430 dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1433 /******************************************************************************
1434 * CryptGenKey (ADVAPI32.@)
1436 * Generates a random cryptographic session key or a pub/priv key pair.
1439 * hProv [I] Handle to a CSP.
1440 * Algid [I] Algorithm to use to make key.
1441 * dwFlags [I] Specifies type of key to make.
1442 * phKey [I] Address of the handle to which the new key is copied.
1448 BOOL WINAPI
CryptGenKey (HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1450 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1453 TRACE("(0x%lx, %d, %08x, %p)\n", hProv
, Algid
, dwFlags
, phKey
);
1455 if (!phKey
|| !prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1457 SetLastError(ERROR_INVALID_PARAMETER
);
1460 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1462 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1466 key
->pProvider
= prov
;
1467 key
->dwMagic
= MAGIC_CRYPTKEY
;
1468 if (prov
->pFuncs
->pCPGenKey(prov
->hPrivate
, Algid
, dwFlags
, &key
->hPrivate
))
1470 *phKey
= (HCRYPTKEY
)key
;
1480 /******************************************************************************
1481 * CryptGetDefaultProviderW (ADVAPI32.@)
1483 * Finds the default CSP of a certain provider type.
1486 * dwProvType [I] Provider type to look for.
1487 * pdwReserved [I] Reserved for future use and must be NULL.
1488 * dwFlags [I] CRYPT_MACHINE_DEFAULT/USER_DEFAULT
1489 * pszProvName [O] Name of the default CSP.
1490 * pcbProvName [I/O] Size of pszProvName
1497 * If pszProvName is NULL, pcbProvName will hold the size of the buffer for
1498 * memory allocation purposes on return.
1500 BOOL WINAPI
CryptGetDefaultProviderW (DWORD dwProvType
, DWORD
*pdwReserved
,
1501 DWORD dwFlags
, LPWSTR pszProvName
, DWORD
*pcbProvName
)
1506 static const WCHAR nameW
[] = {'N','a','m','e',0};
1508 if (pdwReserved
|| !pcbProvName
)
1510 SetLastError(ERROR_INVALID_PARAMETER
);
1513 if (dwFlags
& ~(CRYPT_USER_DEFAULT
| CRYPT_MACHINE_DEFAULT
))
1515 SetLastError(NTE_BAD_FLAGS
);
1518 if (dwProvType
> 999)
1520 SetLastError(NTE_BAD_PROV_TYPE
);
1523 if ( !(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)) )
1525 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1528 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,keyname
, &hKey
))
1530 CRYPT_Free(keyname
);
1531 SetLastError(NTE_PROV_TYPE_NOT_DEF
);
1534 CRYPT_Free(keyname
);
1536 result
= RegQueryValueExW(hKey
, nameW
, NULL
, NULL
, (LPBYTE
)pszProvName
, pcbProvName
);
1541 if (result
!= ERROR_MORE_DATA
)
1542 SetLastError(NTE_PROV_TYPE_ENTRY_BAD
);
1544 SetLastError(result
);
1552 /******************************************************************************
1553 * CryptGetDefaultProviderA (ADVAPI32.@)
1555 * See CryptGetDefaultProviderW.
1557 BOOL WINAPI
CryptGetDefaultProviderA (DWORD dwProvType
, DWORD
*pdwReserved
,
1558 DWORD dwFlags
, LPSTR pszProvName
, DWORD
*pcbProvName
)
1564 TRACE("(%d, %p, %08x, %p, %p)\n", dwProvType
, pdwReserved
, dwFlags
, pszProvName
, pcbProvName
);
1566 CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, NULL
, &bufsize
);
1567 if ( pszProvName
&& !(str
= CRYPT_Alloc(bufsize
)) )
1569 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1572 ret
= CryptGetDefaultProviderW(dwProvType
, pdwReserved
, dwFlags
, str
, &bufsize
);
1574 CRYPT_UnicodeToANSI(str
, &pszProvName
, *pcbProvName
);
1575 *pcbProvName
= bufsize
/ sizeof(WCHAR
);
1579 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1581 SetLastError(ERROR_MORE_DATA
);
1588 /******************************************************************************
1589 * CryptGetHashParam (ADVAPI32.@)
1591 * Retrieves data that controls the operations of a hash object.
1594 * hHash [I] Handle of the hash object to question.
1595 * dwParam [I] Query type.
1596 * pbData [O] Buffer that receives the value data.
1597 * pdwDataLen [I/O] Size of the pbData buffer.
1598 * dwFlags [I] Reserved for future use and must be zero.
1605 * If pbData is NULL, pdwDataLen will contain the length required.
1607 BOOL WINAPI
CryptGetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1608 DWORD
*pdwDataLen
, DWORD dwFlags
)
1611 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1613 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1615 if (!hash
|| !pdwDataLen
|| !hash
->pProvider
||
1616 hash
->dwMagic
!= MAGIC_CRYPTHASH
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1618 SetLastError(ERROR_INVALID_PARAMETER
);
1622 prov
= hash
->pProvider
;
1623 return prov
->pFuncs
->pCPGetHashParam(prov
->hPrivate
, hash
->hPrivate
, dwParam
,
1624 pbData
, pdwDataLen
, dwFlags
);
1627 /******************************************************************************
1628 * CryptGetKeyParam (ADVAPI32.@)
1630 * Retrieves data that controls the operations of a key.
1633 * hKey [I] Handle to they key in question.
1634 * dwParam [I] Specifies query type.
1635 * pbData [O] Sequence of bytes to receive data.
1636 * pdwDataLen [I/O] Size of pbData.
1637 * dwFlags [I] Reserved for future use and must be zero.
1644 * If pbData is NULL, pdwDataLen is set to the needed length of the buffer.
1646 BOOL WINAPI
CryptGetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
1647 DWORD
*pdwDataLen
, DWORD dwFlags
)
1650 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1652 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1654 if (!key
|| !pdwDataLen
|| !key
->pProvider
||
1655 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1657 SetLastError(ERROR_INVALID_PARAMETER
);
1661 prov
= key
->pProvider
;
1662 return prov
->pFuncs
->pCPGetKeyParam(prov
->hPrivate
, key
->hPrivate
, dwParam
,
1663 pbData
, pdwDataLen
, dwFlags
);
1666 /******************************************************************************
1667 * CryptGetProvParam (ADVAPI32.@)
1669 * Retrieves parameters that control the operations of a CSP.
1672 * hProv [I] Handle of the CSP in question.
1673 * dwParam [I] Specifies query type.
1674 * pbData [O] Buffer to receive the data.
1675 * pdwDataLen [I/O] Size of pbData.
1676 * dwFlags [I] see MSDN Docs.
1683 * If pbData is NULL, pdwDataLen is set to the needed buffer length.
1685 BOOL WINAPI
CryptGetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
1686 DWORD
*pdwDataLen
, DWORD dwFlags
)
1688 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1690 TRACE("(0x%lx, %d, %p, %p, %08x)\n", hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1692 if (!prov
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1694 SetLastError(ERROR_INVALID_PARAMETER
);
1698 return prov
->pFuncs
->pCPGetProvParam(prov
->hPrivate
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1701 /******************************************************************************
1702 * CryptGetUserKey (ADVAPI32.@)
1704 * Gets a handle of one of a user's two public/private key pairs.
1707 * hProv [I] Handle of a CSP.
1708 * dwKeySpec [I] Private key to use.
1709 * phUserKey [O] Pointer to the handle of the retrieved keys.
1715 BOOL WINAPI
CryptGetUserKey (HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
1717 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1720 TRACE("(0x%lx, %d, %p)\n", hProv
, dwKeySpec
, phUserKey
);
1724 SetLastError(ERROR_INVALID_HANDLE
);
1727 if (!phUserKey
|| prov
->dwMagic
!= MAGIC_CRYPTPROV
)
1729 SetLastError(ERROR_INVALID_PARAMETER
);
1732 if ( !(key
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1734 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1738 key
->pProvider
= prov
;
1739 key
->dwMagic
= MAGIC_CRYPTKEY
;
1740 if (prov
->pFuncs
->pCPGetUserKey(prov
->hPrivate
, dwKeySpec
, &key
->hPrivate
))
1742 *phUserKey
= (HCRYPTKEY
)key
;
1753 /******************************************************************************
1754 * CryptHashData (ADVAPI32.@)
1756 * Adds data to a hash object.
1759 * hHash [I] Handle of the hash object.
1760 * pbData [I] Buffer of data to be hashed.
1761 * dwDataLen [I] Number of bytes to add.
1762 * dwFlags [I] Can be CRYPT_USERDATA
1768 BOOL WINAPI
CryptHashData (HCRYPTHASH hHash
, const BYTE
*pbData
, DWORD dwDataLen
, DWORD dwFlags
)
1770 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1773 TRACE("(0x%lx, %p, %d, %08x)\n", hHash
, pbData
, dwDataLen
, dwFlags
);
1777 SetLastError(ERROR_INVALID_HANDLE
);
1780 if (!hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
1781 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1783 SetLastError(ERROR_INVALID_PARAMETER
);
1787 prov
= hash
->pProvider
;
1788 return prov
->pFuncs
->pCPHashData(prov
->hPrivate
, hash
->hPrivate
, pbData
, dwDataLen
, dwFlags
);
1791 /******************************************************************************
1792 * CryptHashSessionKey (ADVAPI32.@)
1794 * Compute the cryptographic hash of a session key object.
1797 * hHash [I] Handle to the hash object.
1798 * hKey [I] Handle to the key to be hashed.
1799 * dwFlags [I] Can be CRYPT_LITTLE_ENDIAN.
1805 BOOL WINAPI
CryptHashSessionKey (HCRYPTHASH hHash
, HCRYPTKEY hKey
, DWORD dwFlags
)
1807 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1808 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
1811 TRACE("(0x%lx, 0x%lx, %08x)\n", hHash
, hKey
, dwFlags
);
1815 SetLastError(ERROR_INVALID_HANDLE
);
1819 if (!hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
1820 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
|| key
->dwMagic
!= MAGIC_CRYPTKEY
)
1822 SetLastError(ERROR_INVALID_PARAMETER
);
1826 prov
= hash
->pProvider
;
1827 return prov
->pFuncs
->pCPHashSessionKey(prov
->hPrivate
, hash
->hPrivate
, key
->hPrivate
, dwFlags
);
1830 /******************************************************************************
1831 * CryptImportKey (ADVAPI32.@)
1833 * Transfer a cryptographic key from a key BLOB into a cryptographic service provider (CSP).
1836 * hProv [I] Handle of a CSP.
1837 * pbData [I] Contains the key to be imported.
1838 * dwDataLen [I] Length of the key.
1839 * hPubKey [I] Cryptographic key that decrypts pdData
1840 * dwFlags [I] Used only with a public/private key pair.
1841 * phKey [O] Imported key.
1847 BOOL WINAPI
CryptImportKey (HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
1848 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1850 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
1851 PCRYPTKEY pubkey
= (PCRYPTKEY
)hPubKey
, importkey
;
1853 TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1855 if (!prov
|| !pbData
|| !dwDataLen
|| !phKey
||
1856 prov
->dwMagic
!= MAGIC_CRYPTPROV
||
1857 (pubkey
&& pubkey
->dwMagic
!= MAGIC_CRYPTKEY
))
1859 SetLastError(ERROR_INVALID_PARAMETER
);
1863 if ( !(importkey
= CRYPT_Alloc(sizeof(CRYPTKEY
))) )
1865 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1869 importkey
->pProvider
= prov
;
1870 importkey
->dwMagic
= MAGIC_CRYPTKEY
;
1871 if (prov
->pFuncs
->pCPImportKey(prov
->hPrivate
, pbData
, dwDataLen
,
1872 pubkey
? pubkey
->hPrivate
: 0, dwFlags
, &importkey
->hPrivate
))
1874 *phKey
= (HCRYPTKEY
)importkey
;
1878 importkey
->dwMagic
= 0;
1879 CRYPT_Free(importkey
);
1883 /******************************************************************************
1884 * CryptSignHashW (ADVAPI32.@)
1889 * hHash [I] Handle of the hash object to be signed.
1890 * dwKeySpec [I] Private key to use.
1891 * sDescription [I] Should be NULL.
1892 * dwFlags [I] CRYPT_NOHASHOID/X931_FORMAT.
1893 * pbSignature [O] Buffer of the signature data.
1894 * pdwSigLen [I/O] Size of the pbSignature buffer.
1901 * Because of security flaws sDescription should not be used and should thus be
1902 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
1905 BOOL WINAPI
CryptSignHashW (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCWSTR sDescription
,
1906 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1908 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1911 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1912 hHash
, dwKeySpec
, debugstr_w(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1916 SetLastError(ERROR_INVALID_HANDLE
);
1919 if (!pdwSigLen
|| !hash
->pProvider
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
1920 hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1922 SetLastError(ERROR_INVALID_PARAMETER
);
1926 prov
= hash
->pProvider
;
1927 return prov
->pFuncs
->pCPSignHash(prov
->hPrivate
, hash
->hPrivate
, dwKeySpec
, sDescription
,
1928 dwFlags
, pbSignature
, pdwSigLen
);
1931 /******************************************************************************
1932 * CryptSignHashA (ADVAPI32.@)
1934 * See CryptSignHashW.
1936 BOOL WINAPI
CryptSignHashA (HCRYPTHASH hHash
, DWORD dwKeySpec
, LPCSTR sDescription
,
1937 DWORD dwFlags
, BYTE
*pbSignature
, DWORD
*pdwSigLen
)
1939 LPWSTR wsDescription
;
1942 TRACE("(0x%lx, %d, %s, %08x, %p, %p)\n",
1943 hHash
, dwKeySpec
, debugstr_a(sDescription
), dwFlags
, pbSignature
, pdwSigLen
);
1945 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
1946 result
= CryptSignHashW(hHash
, dwKeySpec
, wsDescription
, dwFlags
, pbSignature
, pdwSigLen
);
1947 CRYPT_Free(wsDescription
);
1952 /******************************************************************************
1953 * CryptSetHashParam (ADVAPI32.@)
1955 * Customizes the operations of a hash object.
1958 * hHash [I] Handle of the hash object to set parameters.
1959 * dwParam [I] HP_HMAC_INFO/HASHVAL.
1960 * pbData [I] Value data buffer.
1961 * dwFlags [I] Reserved for future use and must be zero.
1967 BOOL WINAPI
CryptSetHashParam (HCRYPTHASH hHash
, DWORD dwParam
, const BYTE
*pbData
, DWORD dwFlags
)
1970 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
1972 TRACE("(0x%lx, %d, %p, %08x)\n", hHash
, dwParam
, pbData
, dwFlags
);
1974 if (!hash
|| !pbData
|| !hash
->pProvider
||
1975 hash
->dwMagic
!= MAGIC_CRYPTHASH
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
1977 SetLastError(ERROR_INVALID_PARAMETER
);
1981 prov
= hash
->pProvider
;
1982 return prov
->pFuncs
->pCPSetHashParam(prov
->hPrivate
, hash
->hPrivate
,
1983 dwParam
, pbData
, dwFlags
);
1986 /******************************************************************************
1987 * CryptSetKeyParam (ADVAPI32.@)
1989 * Customizes a session key's operations.
1992 * hKey [I] Handle to the key to set values.
1993 * dwParam [I] See MSDN Doc.
1994 * pbData [I] Buffer of values to set.
1995 * dwFlags [I] Only used when dwParam == KP_ALGID.
2001 BOOL WINAPI
CryptSetKeyParam (HCRYPTKEY hKey
, DWORD dwParam
, const BYTE
*pbData
, DWORD dwFlags
)
2004 PCRYPTKEY key
= (PCRYPTKEY
)hKey
;
2006 TRACE("(0x%lx, %d, %p, %08x)\n", hKey
, dwParam
, pbData
, dwFlags
);
2008 if (!key
|| !pbData
|| !key
->pProvider
||
2009 key
->dwMagic
!= MAGIC_CRYPTKEY
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
2011 SetLastError(ERROR_INVALID_PARAMETER
);
2015 prov
= key
->pProvider
;
2016 return prov
->pFuncs
->pCPSetKeyParam(prov
->hPrivate
, key
->hPrivate
,
2017 dwParam
, pbData
, dwFlags
);
2020 /******************************************************************************
2021 * CryptSetProviderA (ADVAPI32.@)
2023 * Specifies the current user's default CSP.
2026 * pszProvName [I] Name of the new default CSP.
2027 * dwProvType [I] Provider type of the CSP.
2033 BOOL WINAPI
CryptSetProviderA (LPCSTR pszProvName
, DWORD dwProvType
)
2035 TRACE("(%s, %d)\n", pszProvName
, dwProvType
);
2036 return CryptSetProviderExA(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
2039 /******************************************************************************
2040 * CryptSetProviderW (ADVAPI32.@)
2042 * See CryptSetProviderA.
2044 BOOL WINAPI
CryptSetProviderW (LPCWSTR pszProvName
, DWORD dwProvType
)
2046 TRACE("(%s, %d)\n", debugstr_w(pszProvName
), dwProvType
);
2047 return CryptSetProviderExW(pszProvName
, dwProvType
, NULL
, CRYPT_USER_DEFAULT
);
2050 /******************************************************************************
2051 * CryptSetProviderExW (ADVAPI32.@)
2053 * Specifies the default CSP.
2056 * pszProvName [I] Name of the new default CSP.
2057 * dwProvType [I] Provider type of the CSP.
2058 * pdwReserved [I] Reserved for future use and must be NULL.
2059 * dwFlags [I] See MSDN Doc.
2065 BOOL WINAPI
CryptSetProviderExW (LPCWSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2067 HKEY hProvKey
, hTypeKey
;
2069 static const WCHAR nameW
[] = {'N','a','m','e',0};
2071 TRACE("(%s, %d, %p, %08x)\n", debugstr_w(pszProvName
), dwProvType
, pdwReserved
, dwFlags
);
2073 if (!pszProvName
|| pdwReserved
)
2075 SetLastError(ERROR_INVALID_PARAMETER
);
2078 if (dwProvType
> MAXPROVTYPES
)
2080 SetLastError(NTE_BAD_PROV_TYPE
);
2083 if (dwFlags
& ~(CRYPT_MACHINE_DEFAULT
| CRYPT_USER_DEFAULT
| CRYPT_DELETE_DEFAULT
)
2084 || dwFlags
== CRYPT_DELETE_DEFAULT
)
2086 SetLastError(NTE_BAD_FLAGS
);
2090 if (!(keyname
= CRYPT_GetTypeKeyName(dwProvType
, dwFlags
& CRYPT_USER_DEFAULT
)))
2092 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2095 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2096 keyname
, &hTypeKey
))
2098 CRYPT_Free(keyname
);
2099 SetLastError(NTE_BAD_PROVIDER
);
2102 CRYPT_Free(keyname
);
2104 if (dwFlags
& CRYPT_DELETE_DEFAULT
)
2106 RegDeleteValueW(hTypeKey
, nameW
);
2110 if (!(keyname
= CRYPT_GetProvKeyName(pszProvName
)))
2112 RegCloseKey(hTypeKey
);
2113 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2116 if (RegOpenKeyW((dwFlags
& CRYPT_USER_DEFAULT
) ? HKEY_CURRENT_USER
: HKEY_LOCAL_MACHINE
,
2117 keyname
, &hProvKey
))
2119 CRYPT_Free(keyname
);
2120 RegCloseKey(hTypeKey
);
2121 SetLastError(NTE_BAD_PROVIDER
);
2124 CRYPT_Free(keyname
);
2126 if (RegSetValueExW(hTypeKey
, nameW
, 0, REG_SZ
, (const BYTE
*)pszProvName
,
2127 (strlenW(pszProvName
) + 1)*sizeof(WCHAR
)))
2129 RegCloseKey(hTypeKey
);
2130 RegCloseKey(hProvKey
);
2134 RegCloseKey(hProvKey
);
2136 RegCloseKey(hTypeKey
);
2141 /******************************************************************************
2142 * CryptSetProviderExA (ADVAPI32.@)
2144 * See CryptSetProviderExW.
2146 BOOL WINAPI
CryptSetProviderExA (LPCSTR pszProvName
, DWORD dwProvType
, DWORD
*pdwReserved
, DWORD dwFlags
)
2151 TRACE("(%s, %d, %p, %08x)\n", pszProvName
, dwProvType
, pdwReserved
, dwFlags
);
2153 if (CRYPT_ANSIToUnicode(pszProvName
, &str
, -1))
2155 ret
= CryptSetProviderExW(str
, dwProvType
, pdwReserved
, dwFlags
);
2161 /******************************************************************************
2162 * CryptSetProvParam (ADVAPI32.@)
2164 * Customizes the operations of a CSP.
2167 * hProv [I] Handle of a CSP.
2168 * dwParam [I] See MSDN Doc.
2169 * pbData [I] Buffer that contains a value to set as a parameter.
2170 * dwFlags [I] if dwParam is PP_USE_HARDWARE_RNG, dwFlags must be zero.
2176 BOOL WINAPI
CryptSetProvParam (HCRYPTPROV hProv
, DWORD dwParam
, const BYTE
*pbData
, DWORD dwFlags
)
2178 PCRYPTPROV prov
= (PCRYPTPROV
)hProv
;
2180 TRACE("(0x%lx, %d, %p, %08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
2184 SetLastError(ERROR_INVALID_HANDLE
);
2187 if (prov
->dwMagic
!= MAGIC_CRYPTPROV
)
2189 SetLastError(ERROR_INVALID_PARAMETER
);
2192 if (dwParam
== PP_USE_HARDWARE_RNG
)
2194 FIXME("PP_USE_HARDWARE_RNG: What do I do with this?\n");
2195 FIXME("\tLetting the CSP decide.\n");
2197 if (dwFlags
& PP_CLIENT_HWND
)
2199 /* FIXME: Should verify the parameter */
2200 if (pbData
/* && IsWindow((HWND)pbData) */)
2202 crypt_hWindow
= (HWND
)(pbData
);
2205 SetLastError(ERROR_INVALID_PARAMETER
);
2209 /* All other flags go to the CSP */
2210 return prov
->pFuncs
->pCPSetProvParam(prov
->hPrivate
, dwParam
, pbData
, dwFlags
);
2213 /******************************************************************************
2214 * CryptVerifySignatureW (ADVAPI32.@)
2216 * Verifies the signature of a hash object.
2219 * hHash [I] Handle of the hash object to verify.
2220 * pbSignature [I] Signature data to verify.
2221 * dwSigLen [I] Size of pbSignature.
2222 * hPubKey [I] Handle to the public key to authenticate signature.
2223 * sDescription [I] Should be NULL.
2224 * dwFlags [I] See MSDN doc.
2231 * Because of security flaws sDescription should not be used and should thus be
2232 * NULL. It is supported only for compatibility with Microsoft's Cryptographic
2235 BOOL WINAPI
CryptVerifySignatureW (HCRYPTHASH hHash
, const BYTE
*pbSignature
, DWORD dwSigLen
,
2236 HCRYPTKEY hPubKey
, LPCWSTR sDescription
, DWORD dwFlags
)
2238 PCRYPTHASH hash
= (PCRYPTHASH
)hHash
;
2239 PCRYPTKEY key
= (PCRYPTKEY
)hPubKey
;
2242 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2243 dwSigLen
, hPubKey
, debugstr_w(sDescription
), dwFlags
);
2245 if (!hash
|| !key
|| key
->dwMagic
!= MAGIC_CRYPTKEY
|| hash
->dwMagic
!= MAGIC_CRYPTHASH
||
2246 !hash
->pProvider
|| hash
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
||
2247 !key
->pProvider
|| key
->pProvider
->dwMagic
!= MAGIC_CRYPTPROV
)
2249 SetLastError(ERROR_INVALID_PARAMETER
);
2253 prov
= hash
->pProvider
;
2254 return prov
->pFuncs
->pCPVerifySignature(prov
->hPrivate
, hash
->hPrivate
, pbSignature
, dwSigLen
,
2255 key
->hPrivate
, sDescription
, dwFlags
);
2258 /******************************************************************************
2259 * CryptVerifySignatureA (ADVAPI32.@)
2261 * See CryptVerifySignatureW.
2263 BOOL WINAPI
CryptVerifySignatureA (HCRYPTHASH hHash
, const BYTE
*pbSignature
, DWORD dwSigLen
,
2264 HCRYPTKEY hPubKey
, LPCSTR sDescription
, DWORD dwFlags
)
2266 LPWSTR wsDescription
;
2269 TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash
, pbSignature
,
2270 dwSigLen
, hPubKey
, debugstr_a(sDescription
), dwFlags
);
2272 CRYPT_ANSIToUnicode(sDescription
, &wsDescription
, -1);
2273 result
= CryptVerifySignatureW(hHash
, pbSignature
, dwSigLen
, hPubKey
, wsDescription
, dwFlags
);
2274 CRYPT_Free(wsDescription
);
2279 /******************************************************************************
2280 * OpenEncryptedFileRawA (ADVAPI32.@)
2282 * See OpenEncryptedFileRawW
2284 DWORD WINAPI
OpenEncryptedFileRawA(LPCSTR filename
, ULONG flags
, PVOID
*context
)
2286 FIXME("(%s, %x, %p): stub\n", debugstr_a(filename
), flags
, context
);
2287 return ERROR_CALL_NOT_IMPLEMENTED
;
2290 /******************************************************************************
2291 * OpenEncryptedFileRawW (ADVAPI32.@)
2293 * Opens an EFS encrypted file for backup/restore
2296 * filename [I] Filename to operate on
2297 * flags [I] Operation to perform
2298 * context [I] Handle to the context (out)
2300 * Success: ERROR_SUCCESS
2301 * Failure: NTSTATUS error code
2303 DWORD WINAPI
OpenEncryptedFileRawW(LPCWSTR filename
, ULONG flags
, PVOID
*context
)
2305 FIXME("(%s, %x, %p): stub\n", debugstr_w(filename
), flags
, context
);
2306 return ERROR_CALL_NOT_IMPLEMENTED
;
2309 /******************************************************************************
2310 * ReadEncryptedFileRaw (ADVAPI32.@)
2312 * Export encrypted files
2315 * export [I] pointer to the export callback function
2316 * callback [I] pointer to the application defined context
2317 * context [I] pointer to the system context
2319 * Success: ERROR_SUCCESS
2320 * Failure: NTSTATUS error code
2322 DWORD WINAPI
ReadEncryptedFileRaw(PFE_EXPORT_FUNC export
, PVOID callback
, PVOID context
)
2324 FIXME("(%p, %p, %p): stub\n", export
, callback
, context
);
2325 return ERROR_CALL_NOT_IMPLEMENTED
;
2328 /******************************************************************************
2329 * SystemFunction030 (ADVAPI32.@)
2331 * Tests if two blocks of 16 bytes are equal
2334 * b1,b2 [I] block of 16 bytes
2337 * TRUE if blocks are the same
2338 * FALSE if blocks are different
2340 BOOL WINAPI
SystemFunction030(LPCVOID b1
, LPCVOID b2
)
2342 return !memcmp(b1
, b2
, 0x10);
2345 /******************************************************************************
2346 * SystemFunction035 (ADVAPI32.@)
2349 http://disc.server.com/discussion.cgi?disc=148775;article=942;title=Coding%2FASM%2FSystem
2352 * Stub, always return TRUE.
2354 BOOL WINAPI
SystemFunction035(LPCSTR lpszDllFilePath
)
2356 FIXME("%s: stub\n", debugstr_a(lpszDllFilePath
));
2360 /******************************************************************************
2361 * SystemFunction036 (ADVAPI32.@)
2363 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
2366 * pbBuffer [O] Pointer to memory to receive random bytes.
2367 * dwLen [I] Number of random bytes to fetch.
2374 BOOLEAN WINAPI
SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
2378 dev_random
= open("/dev/urandom", O_RDONLY
);
2379 if (dev_random
!= -1)
2381 if (read(dev_random
, pbBuffer
, dwLen
) == (ssize_t
)dwLen
)
2389 FIXME("couldn't open /dev/urandom\n");
2390 SetLastError(NTE_FAIL
);
2395 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory,
2399 /******************************************************************************
2400 * SystemFunction040 (ADVAPI32.@)
2402 * MSDN documents this function as RtlEncryptMemory and declares it in ntsecapi.h.
2405 * memory [I/O] Pointer to memory to encrypt.
2406 * length [I] Length of region to encrypt in bytes.
2407 * flags [I] Control whether other processes are able to decrypt the memory.
2408 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2409 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2410 * RTL_ENCRYPT_OPTION_SAME_LOGON
2413 * Success: STATUS_SUCCESS
2414 * Failure: NTSTATUS error code
2417 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2418 * If flags are specified when encrypting, the same flag value must be given
2419 * when decrypting the memory.
2421 NTSTATUS WINAPI
SystemFunction040(PVOID memory
, ULONG length
, ULONG flags
)
2423 FIXME("(%p, %x, %x): stub [RtlEncryptMemory]\n", memory
, length
, flags
);
2424 return STATUS_SUCCESS
;
2427 /******************************************************************************
2428 * SystemFunction041 (ADVAPI32.@)
2430 * MSDN documents this function as RtlDecryptMemory and declares it in ntsecapi.h.
2433 * memory [I/O] Pointer to memory to decrypt.
2434 * length [I] Length of region to decrypt in bytes.
2435 * flags [I] Control whether other processes are able to decrypt the memory.
2436 * RTL_ENCRYPT_OPTION_SAME_PROCESS
2437 * RTL_ENCRYPT_OPTION_CROSS_PROCESS
2438 * RTL_ENCRYPT_OPTION_SAME_LOGON
2441 * Success: STATUS_SUCCESS
2442 * Failure: NTSTATUS error code
2445 * length must be a multiple of RTL_ENCRYPT_MEMORY_SIZE.
2446 * If flags are specified when encrypting, the same flag value must be given
2447 * when decrypting the memory.
2449 NTSTATUS WINAPI
SystemFunction041(PVOID memory
, ULONG length
, ULONG flags
)
2451 FIXME("(%p, %x, %x): stub [RtlDecryptMemory]\n", memory
, length
, flags
);
2452 return STATUS_SUCCESS
;
2455 /******************************************************************************
2456 * WriteEncryptedFileRaw (ADVAPI32.@)
2458 * Import encrypted files
2461 * import [I] pointer to the import callback function
2462 * callback [I] pointer to the application defined context
2463 * context [I] pointer to the system context
2465 * Success: ERROR_SUCCESS
2466 * Failure: NTSTATUS error code
2468 DWORD WINAPI
WriteEncryptedFileRaw(PFE_IMPORT_FUNC import
, PVOID callback
, PVOID context
)
2470 FIXME("(%p, %p, %p): stub\n", import
, callback
, context
);
2471 return ERROR_CALL_NOT_IMPLEMENTED
;