3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
26 #include "wine/library.h"
27 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
42 /******************************************************************************
43 * CRYPTHASH - hash objects
45 #define RSAENH_MAGIC_HASH 0x85938417u
46 #define RSAENH_MAX_HASH_SIZE 36
47 #define RSAENH_HASHSTATE_IDLE 0
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct tagCRYPTHASH
59 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
63 /******************************************************************************
64 * CRYPTKEY - key objects
66 #define RSAENH_MAGIC_KEY 0x73620457u
67 #define RSAENH_MAX_KEY_SIZE 24
68 #define RSAENH_MAX_BLOCK_SIZE 24
69 #define RSAENH_KEYSTATE_IDLE 0
70 #define RSAENH_KEYSTATE_ENCRYPTING 1
71 #define RSAENH_KEYSTATE_DECRYPTING 2
72 typedef struct tagCRYPTKEY
85 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
86 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
87 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
90 /******************************************************************************
91 * KEYCONTAINER - key containers
93 #define RSAENH_PERSONALITY_BASE 0u
94 #define RSAENH_PERSONALITY_STRONG 1u
95 #define RSAENH_PERSONALITY_ENHANCED 2u
97 #define RSAENH_MAGIC_CONTAINER 0x26384993u
98 typedef struct tagKEYCONTAINER
104 CHAR szName
[MAX_PATH
];
105 CHAR szProvName
[MAX_PATH
];
106 HCRYPTKEY hKeyExchangeKeyPair
;
107 HCRYPTKEY hSignatureKeyPair
;
110 /******************************************************************************
111 * Some magic constants
113 #define RSAENH_ENCRYPT 1
114 #define RSAENH_DECRYPT 0
115 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
116 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
117 #define RSAENH_HMAC_DEF_PAD_LEN 64
118 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
119 #define RSAENH_DES_STORAGE_KEYLEN 64
120 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
121 #define RSAENH_3DES112_STORAGE_KEYLEN 128
122 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
123 #define RSAENH_3DES_STORAGE_KEYLEN 192
124 #define RSAENH_MAGIC_RSA2 0x32415352
125 #define RSAENH_MAGIC_RSA1 0x31415352
126 #define RSAENH_PKC_BLOCKTYPE 0x02
127 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
129 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
130 /******************************************************************************
131 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
133 #define RSAENH_MAX_ENUMALGS 14
134 PROV_ENUMALGS_EX aProvEnumAlgsEx
[3][RSAENH_MAX_ENUMALGS
+1] =
137 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
138 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
139 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
140 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
141 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
142 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
143 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
144 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
145 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
146 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
147 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",18,"RSA Key Exchange"},
148 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
149 {0, 0, 0, 0,0, 1,"", 1,""}
152 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
153 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
154 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
155 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
156 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
157 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
158 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
159 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
160 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
161 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
162 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
163 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
164 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",18,"RSA Key Exchange"},
165 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
166 {0, 0, 0, 0,0, 1,"", 1,""}
169 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
170 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
171 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
172 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
173 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
174 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
175 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
176 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
177 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
178 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
179 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
180 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
181 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",18,"RSA Key Exchange"},
182 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
183 {0, 0, 0, 0,0, 1,"", 1,""}
187 /******************************************************************************
188 * API forward declarations
191 RSAENH_CPGetKeyParam(
213 RSAENH_CPGetHashParam(
243 /******************************************************************************
244 * CSP's handle table (used by all acquired key containers)
246 static HANDLETABLE handle_table
;
248 /******************************************************************************
251 * Initializes and destroys the handle table for the CSP's handles.
253 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
257 case DLL_PROCESS_ATTACH
:
258 init_handle_table(&handle_table
);
261 case DLL_PROCESS_DETACH
:
262 destroy_handle_table(&handle_table
);
268 /******************************************************************************
269 * copy_param [Internal]
271 * Helper function that supports the standard WINAPI protocol for querying data
275 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
276 * May be NUL if the required buffer size is to be queried only.
277 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
278 * Out: Size of parameter pbParam
279 * pbParam [I] Parameter value.
280 * dwParamSize [I] Size of pbParam
283 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
284 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
286 static inline BOOL
copy_param(
287 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
291 if (dwParamSize
> *pdwBufferSize
)
293 SetLastError(ERROR_MORE_DATA
);
294 *pdwBufferSize
= dwParamSize
;
297 memcpy(pbBuffer
, pbParam
, dwParamSize
);
299 *pdwBufferSize
= dwParamSize
;
303 /******************************************************************************
304 * get_algid_info [Internal]
306 * Query CSP capabilities for a given crypto algorithm.
309 * pKeyContainer [I] Pointer to a key container of the CSP whose capabilities are to be queried.
310 * algid [I] Identifier of the crypto algorithm about which information is requested.
313 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
314 * Failure: NULL (algid not supported)
316 static inline const PROV_ENUMALGS_EX
* get_algid_info(KEYCONTAINER
*pKeyContainer
, ALG_ID algid
) {
317 PROV_ENUMALGS_EX
*iterator
;
319 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
320 if (iterator
->aiAlgid
== algid
) return iterator
;
326 /******************************************************************************
327 * free_hmac_info [Internal]
329 * Deeply free an HMAC_INFO struct.
332 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
335 * See Internet RFC 2104 for details on the HMAC algorithm.
337 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
338 if (!hmac_info
) return;
339 if (hmac_info
->pbInnerString
) HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
340 if (hmac_info
->pbOuterString
) HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
341 HeapFree(GetProcessHeap(), 0, hmac_info
);
344 /******************************************************************************
345 * copy_hmac_info [Internal]
347 * Deeply copy an HMAC_INFO struct
350 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
351 * src [I] Pointer to the HMAC_INFO struct to be copied.
358 * See Internet RFC 2104 for details on the HMAC algorithm.
360 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, PHMAC_INFO src
) {
361 if (!src
) return FALSE
;
362 *dst
= (PHMAC_INFO
)HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
363 if (!*dst
) return FALSE
;
364 memcpy(*dst
, src
, sizeof(HMAC_INFO
));
365 (*dst
)->pbInnerString
= NULL
;
366 (*dst
)->pbOuterString
= NULL
;
367 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
368 (*dst
)->pbInnerString
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
369 if (!(*dst
)->pbInnerString
) {
370 free_hmac_info(*dst
);
373 if (src
->cbInnerString
)
374 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
376 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
377 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
378 (*dst
)->pbOuterString
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
379 if (!(*dst
)->pbOuterString
) {
380 free_hmac_info(*dst
);
383 if (src
->cbOuterString
)
384 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
386 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
390 /******************************************************************************
391 * destroy_hash [Internal]
393 * Destructor for hash objects
396 * pCryptHash [I] Pointer to the hash object to be destroyed.
397 * Will be invalid after function returns!
399 static void destroy_hash(OBJECTHDR
*pCryptHash
)
401 free_hmac_info(((CRYPTHASH
*)pCryptHash
)->pHMACInfo
);
402 HeapFree(GetProcessHeap(), 0, pCryptHash
);
405 /******************************************************************************
406 * init_hash [Internal]
408 * Initialize (or reset) a hash object
411 * pKeyContainer [I] Pointer to the key container the hash object belongs to.
412 * pCryptHash [I] The hash object to be initialized.
414 static inline BOOL
init_hash(KEYCONTAINER
*pKeyContainer
, CRYPTHASH
*pCryptHash
) {
416 const PROV_ENUMALGS_EX
*pAlgInfo
;
418 switch (pCryptHash
->aiAlgid
)
421 if (pCryptHash
->pHMACInfo
) {
422 pAlgInfo
= get_algid_info(pKeyContainer
, pCryptHash
->pHMACInfo
->HashAlgid
);
423 if (pAlgInfo
) pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
424 return init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
429 dwLen
= sizeof(DWORD
);
430 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
431 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
432 pCryptHash
->dwHashSize
>>= 3;
436 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
440 /******************************************************************************
441 * update_hash [Internal]
443 * Hashes the given data and updates the hash object's state accordingly
446 * pCryptHash [I] Hash object to be updated.
447 * pbData [I] Pointer to data stream to be hashed.
448 * dwDataLen [I] Length of data stream.
450 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
453 switch (pCryptHash
->aiAlgid
)
456 if (pCryptHash
->pHMACInfo
)
457 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
462 pbTemp
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
464 memcpy(pbTemp
, pbData
, dwDataLen
);
465 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, FALSE
, 0,
466 pbTemp
, &dwDataLen
, dwDataLen
);
467 HeapFree(GetProcessHeap(), 0, pbTemp
);
471 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
475 /******************************************************************************
476 * finalize_hash [Internal]
478 * Finalizes the hash, after all data has been hashed with update_hash.
479 * No additional data can be hashed afterwards until the hash gets initialized again.
482 * pCryptHash [I] Hash object to be finalized.
484 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
487 switch (pCryptHash
->aiAlgid
)
490 if (pCryptHash
->pHMACInfo
)
491 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
492 pCryptHash
->abHashValue
);
497 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, (HCRYPTHASH
)NULL
, TRUE
, 0,
498 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
502 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
506 /******************************************************************************
507 * destroy_key [Internal]
509 * Destructor for key objects
512 * pCryptKey [I] Pointer to the key object to be destroyed.
513 * Will be invalid after function returns!
515 static void destroy_key(OBJECTHDR
*pCryptKey
)
517 free_key_impl(((CRYPTKEY
*)pCryptKey
)->aiAlgid
, &((CRYPTKEY
*)pCryptKey
)->context
);
518 HeapFree(GetProcessHeap(), 0, pCryptKey
);
521 /******************************************************************************
522 * setup_key [Internal]
524 * Initialize (or reset) a key object
527 * pCryptKey [I] The key object to be initialized.
529 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
530 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
531 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
532 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
533 pCryptKey
->dwSaltLen
, pCryptKey
->abKeyValue
);
536 /******************************************************************************
539 * Creates a new key object without assigning the actual binary key value.
540 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
543 * hProv [I] Handle to the provider to which the created key will belong.
544 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
545 * dwFlags [I] Upper 16 bits give the key length.
546 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
547 * ppCryptKey [O] Pointer to the created key
550 * Success: Handle to the created key.
551 * Failure: INVALID_HANDLE_VALUE
553 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
555 KEYCONTAINER
*pKeyContainer
;
558 DWORD dwKeyLen
= HIWORD(dwFlags
);
559 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
561 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
))
563 SetLastError(NTE_BAD_UID
);
564 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
568 * Retrieve the CSP's capabilities for the given ALG_ID value
570 peaAlgidInfo
= get_algid_info(pKeyContainer
, aiAlgid
);
572 SetLastError(NTE_BAD_ALGID
);
573 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
577 * Assume the default key length, if none is specified explicitly
579 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
582 * Check if the requested key length is supported by the current CSP.
583 * Adjust key length's for DES algorithms.
587 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
588 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
590 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
591 SetLastError(NTE_BAD_FLAGS
);
592 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
597 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
598 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
600 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
601 SetLastError(NTE_BAD_FLAGS
);
602 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
607 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
608 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
610 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
611 SetLastError(NTE_BAD_FLAGS
);
612 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
618 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
619 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
621 SetLastError(NTE_BAD_FLAGS
);
622 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
626 hCryptKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
627 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
628 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
630 pCryptKey
->aiAlgid
= aiAlgid
;
631 pCryptKey
->hProv
= hProv
;
632 pCryptKey
->dwModeBits
= 0;
633 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
635 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
636 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
637 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
639 pCryptKey
->dwSaltLen
= 0;
640 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
641 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
646 pCryptKey
->dwBlockLen
= 0;
647 pCryptKey
->dwMode
= 0;
654 pCryptKey
->dwBlockLen
= 8;
655 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
660 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
661 pCryptKey
->dwMode
= 0;
665 *ppCryptKey
= pCryptKey
;
671 /******************************************************************************
672 * destroy_key_container [Internal]
674 * Destructor for key containers. The user's signature and key exchange private
675 * keys are stored in the registry _IN_PLAINTEXT_.
678 * pObjectHdr [I] Pointer to the key container to be destroyed.
680 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
682 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
684 CHAR szRSABase
[MAX_PATH
];
689 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
)) {
690 /* On WinXP, persistent keys are stored in a file located at:
691 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
693 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
695 if (RegCreateKeyExA(HKEY_CURRENT_USER
, szRSABase
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
696 KEY_WRITE
, NULL
, &hKey
, NULL
) == ERROR_SUCCESS
)
698 if (lookup_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
701 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
702 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
704 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
707 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hKeyExchangeKeyPair
, 0,
708 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
710 RegSetValueExA(hKey
, "KeyExchangeKeyPair", 0, REG_BINARY
, pbKey
, dwLen
);
712 HeapFree(GetProcessHeap(), 0, pbKey
);
715 release_handle(&handle_table
, (unsigned int)pKeyContainer
->hKeyExchangeKeyPair
,
719 if (lookup_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
722 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
723 PRIVATEKEYBLOB
, 0, 0, &dwLen
))
725 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
728 if (RSAENH_CPExportKey(pKey
->hProv
, pKeyContainer
->hSignatureKeyPair
, 0,
729 PRIVATEKEYBLOB
, 0, pbKey
, &dwLen
))
731 RegSetValueExA(hKey
, "SignatureKeyPair", 0, REG_BINARY
, pbKey
, dwLen
);
733 HeapFree(GetProcessHeap(), 0, pbKey
);
736 release_handle(&handle_table
, (unsigned int)pKeyContainer
->hSignatureKeyPair
,
744 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
747 /******************************************************************************
748 * new_key_container [Internal]
750 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
751 * of the CSP is determined via the pVTable->pszProvName string.
754 * pszContainerName [I] Name of the key container.
755 * pVTable [I] Callback functions and context info provided by the OS
758 * Success: Handle to the new key container.
759 * Failure: INVALID_HANDLE_VALUE
761 static HCRYPTPROV
new_key_container(PCHAR pszContainerName
, DWORD dwFlags
, PVTableProvStruc pVTable
)
763 KEYCONTAINER
*pKeyContainer
;
764 HCRYPTPROV hKeyContainer
;
766 hKeyContainer
= (HCRYPTPROV
)new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
767 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
768 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
770 strncpy(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
771 pKeyContainer
->szName
[MAX_PATH
-1] = '\0';
772 pKeyContainer
->dwFlags
= dwFlags
;
773 pKeyContainer
->dwEnumAlgsCtr
= 0;
774 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
775 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
776 if (pVTable
&& pVTable
->pszProvName
) {
777 strncpy(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
778 pKeyContainer
->szProvName
[MAX_PATH
-1] = '\0';
779 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
780 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
781 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
782 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
784 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
789 return hKeyContainer
;
792 /******************************************************************************
793 * read_key_container [Internal]
795 * Tries to read the persistent state of the key container (mainly the signature
796 * and key exchange private keys) given by pszContainerName.
799 * pszContainerName [I] Name of the key container to read from the registry
800 * pVTable [I] Pointer to context data provided by the operating system
803 * Success: Handle to the key container read from the registry
804 * Failure: INVALID_HANDLE_VALUE
806 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, PVTableProvStruc pVTable
)
808 CHAR szRSABase
[MAX_PATH
];
811 DWORD dwValueType
, dwLen
;
812 KEYCONTAINER
*pKeyContainer
;
813 HCRYPTPROV hKeyContainer
;
815 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
817 if (RegOpenKeyExA(HKEY_CURRENT_USER
, szRSABase
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
819 SetLastError(NTE_BAD_KEYSET
);
820 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
823 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
824 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
826 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
827 (OBJECTHDR
**)&pKeyContainer
))
828 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
830 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
833 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
836 if (RegQueryValueExA(hKey
, "KeyExchangeKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
839 RSAENH_CPImportKey(hKeyContainer
, pbKey
, dwLen
, 0, 0,
840 &pKeyContainer
->hKeyExchangeKeyPair
);
842 HeapFree(GetProcessHeap(), 0, pbKey
);
846 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, NULL
, &dwLen
) ==
849 pbKey
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, dwLen
);
852 if (RegQueryValueExA(hKey
, "SignatureKeyPair", 0, &dwValueType
, pbKey
, &dwLen
) ==
855 RSAENH_CPImportKey(hKeyContainer
, pbKey
, dwLen
, 0, 0,
856 &pKeyContainer
->hSignatureKeyPair
);
858 HeapFree(GetProcessHeap(), 0, pbKey
);
863 return hKeyContainer
;
866 /******************************************************************************
867 * build_hash_signature [Internal]
869 * Builds a padded version of a hash to match the length of the RSA key modulus.
872 * pbSignature [O] The padded hash object is stored here.
873 * dwLen [I] Length of the pbSignature buffer.
874 * aiAlgid [I] Algorithm identifier of the hash to be padded.
875 * abHashValue [I] The value of the hash object.
876 * dwHashLen [I] Length of the hash value.
877 * dwFlags [I] Selection of padding algorithm.
881 * Failure: FALSE (NTE_BAD_ALGID)
883 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
884 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
886 /* These prefixes are meant to be concatenated with hash values of the
887 * respective kind to form a PKCS #7 DigestInfo. */
888 static const struct tagOIDDescriptor
{
891 CONST BYTE abOID
[18];
892 } aOIDDescriptor
[5] = {
893 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
894 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
895 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
896 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
897 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
898 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
899 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
900 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
903 DWORD dwIdxOID
, i
, j
;
905 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
906 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
909 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
910 SetLastError(NTE_BAD_ALGID
);
914 /* Build the padded signature */
915 if (dwFlags
& CRYPT_X931_FORMAT
) {
916 pbSignature
[0] = 0x6b;
917 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
918 pbSignature
[i
] = 0xbb;
920 pbSignature
[i
++] = 0xba;
921 for (j
=0; j
< dwHashLen
; j
++, i
++) {
922 pbSignature
[i
] = abHashValue
[j
];
924 pbSignature
[i
++] = 0x33;
925 pbSignature
[i
++] = 0xcc;
927 pbSignature
[0] = 0x00;
928 pbSignature
[1] = 0x01;
929 if (dwFlags
& CRYPT_NOHASHOID
) {
930 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
931 pbSignature
[i
] = 0xff;
933 pbSignature
[i
++] = 0x00;
935 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
936 pbSignature
[i
] = 0xff;
938 pbSignature
[i
++] = 0x00;
939 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
940 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
943 for (j
=0; j
< dwHashLen
; j
++) {
944 pbSignature
[i
++] = abHashValue
[j
];
951 /******************************************************************************
952 * CPAcquireContext (RSAENH.@)
954 * Acquire a handle to the key container specified by pszContainer
957 * phProv [O] Pointer to the location the acquired handle will be written to.
958 * pszContainer [I] Name of the desired key container. See Notes
959 * dwFlags [I] Flags. See Notes.
960 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
967 * If pszContainer is NULL or points to a zero length string the user's login
968 * name will be used as the key container name.
970 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
971 * If a keyset with the given name already exists, the function fails and sets
972 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
973 * key container does not exist, function fails and sets last error to
976 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
977 DWORD dwFlags
, PVTableProvStruc pVTable
)
980 CHAR szKeyContainerName
[MAX_PATH
] = "";
981 CHAR szRegKey
[MAX_PATH
];
983 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv
,
984 debugstr_a(pszContainer
), dwFlags
, pVTable
);
986 SetLastError(ERROR_SUCCESS
);
988 if (pszContainer
? strlen(pszContainer
) : 0)
990 strncpy(szKeyContainerName
, pszContainer
, MAX_PATH
);
991 szKeyContainerName
[MAX_PATH
-1] = '\0';
996 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
999 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1002 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1005 case CRYPT_DELETEKEYSET
:
1006 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainer
) >= MAX_PATH
) {
1007 SetLastError(NTE_BAD_KEYSET_PARAM
);
1010 RegDeleteKeyA(HKEY_CURRENT_USER
, szRegKey
);
1015 case CRYPT_NEWKEYSET
:
1016 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1017 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1019 release_handle(&handle_table
, (unsigned int)*phProv
, RSAENH_MAGIC_CONTAINER
);
1020 SetLastError(NTE_EXISTS
);
1023 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1026 case CRYPT_VERIFYCONTEXT
:
1028 SetLastError(NTE_BAD_FLAGS
);
1031 *phProv
= new_key_container("", dwFlags
, pVTable
);
1035 *phProv
= (unsigned int)INVALID_HANDLE_VALUE
;
1036 SetLastError(NTE_BAD_FLAGS
);
1040 return *phProv
!= (unsigned int)INVALID_HANDLE_VALUE
;
1043 /******************************************************************************
1044 * CPCreateHash (RSAENH.@)
1046 * CPCreateHash creates and initalizes a new hash object.
1049 * hProv [I] Handle to the key container to which the new hash will belong.
1050 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1051 * hKey [I] Handle to a session key applied for keyed hashes.
1052 * dwFlags [I] Currently no flags defined. Must be zero.
1053 * phHash [O] Points to the location where a handle to the new hash will be stored.
1060 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1061 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1063 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1066 KEYCONTAINER
*pKeyContainer
;
1067 CRYPTHASH
*pCryptHash
;
1068 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1070 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv
, Algid
, hKey
,
1073 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
))
1075 SetLastError(NTE_BAD_UID
);
1079 peaAlgidInfo
= get_algid_info(pKeyContainer
, Algid
);
1082 SetLastError(NTE_BAD_ALGID
);
1088 SetLastError(NTE_BAD_FLAGS
);
1092 if ((Algid
== CALG_MAC
|| Algid
== CALG_HMAC
)) {
1093 CRYPTKEY
*pCryptKey
;
1095 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1096 SetLastError(NTE_BAD_KEY
);
1100 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1101 SetLastError(NTE_BAD_KEY
);
1106 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1107 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1108 if (!pCryptHash
) return FALSE
;
1110 pCryptHash
->aiAlgid
= Algid
;
1111 pCryptHash
->hKey
= hKey
;
1112 pCryptHash
->hProv
= hProv
;
1113 pCryptHash
->dwState
= RSAENH_HASHSTATE_IDLE
;
1114 pCryptHash
->pHMACInfo
= (PHMAC_INFO
)NULL
;
1115 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1117 return init_hash(pKeyContainer
, pCryptHash
);
1120 /******************************************************************************
1121 * CPDestroyHash (RSAENH.@)
1123 * Releases the handle to a hash object. The object is destroyed if it's reference
1124 * count reaches zero.
1127 * hProv [I] Handle to the key container to which the hash object belongs.
1128 * hHash [I] Handle to the hash object to be released.
1134 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1136 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1138 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1140 SetLastError(NTE_BAD_UID
);
1144 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1146 SetLastError(NTE_BAD_HASH
);
1153 /******************************************************************************
1154 * CPDestroyKey (RSAENH.@)
1156 * Releases the handle to a key object. The object is destroyed if it's reference
1157 * count reaches zero.
1160 * hProv [I] Handle to the key container to which the key object belongs.
1161 * hKey [I] Handle to the key object to be released.
1167 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1169 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1171 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1173 SetLastError(NTE_BAD_UID
);
1177 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1179 SetLastError(NTE_BAD_KEY
);
1186 /******************************************************************************
1187 * CPDuplicateHash (RSAENH.@)
1189 * Clones a hash object including it's current state.
1192 * hUID [I] Handle to the key container the hash belongs to.
1193 * hHash [I] Handle to the hash object to be cloned.
1194 * pdwReserved [I] Reserved. Must be NULL.
1195 * dwFlags [I] No flags are currently defined. Must be 0.
1196 * phHash [O] Handle to the cloned hash object.
1202 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
1203 DWORD dwFlags
, HCRYPTHASH
*phHash
)
1205 CRYPTHASH
*pSrcHash
, *pDestHash
;
1207 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID
, hHash
,
1208 pdwReserved
, dwFlags
, phHash
);
1210 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1212 SetLastError(NTE_BAD_UID
);
1216 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
1218 SetLastError(NTE_BAD_HASH
);
1222 if (!phHash
|| pdwReserved
|| dwFlags
)
1224 SetLastError(ERROR_INVALID_PARAMETER
);
1228 *phHash
= (HCRYPTHASH
)new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1229 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
1230 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
1232 memcpy(pDestHash
, pSrcHash
, sizeof(CRYPTHASH
));
1233 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
1234 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
1237 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1240 /******************************************************************************
1241 * CPDuplicateKey (RSAENH.@)
1243 * Clones a key object including it's current state.
1246 * hUID [I] Handle to the key container the hash belongs to.
1247 * hKey [I] Handle to the key object to be cloned.
1248 * pdwReserved [I] Reserved. Must be NULL.
1249 * dwFlags [I] No flags are currently defined. Must be 0.
1250 * phHash [O] Handle to the cloned key object.
1256 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
1257 DWORD dwFlags
, HCRYPTKEY
*phKey
)
1259 CRYPTKEY
*pSrcKey
, *pDestKey
;
1261 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID
, hKey
,
1262 pdwReserved
, dwFlags
, phKey
);
1264 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1266 SetLastError(NTE_BAD_UID
);
1270 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
1272 SetLastError(NTE_BAD_KEY
);
1276 if (!phKey
|| pdwReserved
|| dwFlags
)
1278 SetLastError(ERROR_INVALID_PARAMETER
);
1282 *phKey
= (HCRYPTKEY
)new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
1283 (OBJECTHDR
**)&pDestKey
);
1284 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
1286 memcpy(pDestKey
, pSrcKey
, sizeof(CRYPTKEY
));
1287 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
1296 /******************************************************************************
1297 * CPEncrypt (RSAENH.@)
1302 * hProv [I] The key container hKey and hHash belong to.
1303 * hKey [I] The key used to encrypt the data.
1304 * hHash [I] An optional hash object for parallel hashing. See notes.
1305 * Final [I] Indicates if this is the last block of data to encrypt.
1306 * dwFlags [I] Currently no flags defined. Must be zero.
1307 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1308 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1309 * dwBufLen [I] Size of the buffer at pbData.
1316 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1317 * This is useful for message signatures.
1319 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1322 * Parallel hashing not yet implemented.
1324 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1325 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
1327 CRYPTKEY
*pCryptKey
;
1328 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1329 DWORD dwEncryptedLen
, i
, j
, k
;
1331 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1332 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
1335 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1337 SetLastError(NTE_BAD_UID
);
1343 SetLastError(NTE_BAD_FLAGS
);
1347 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1349 SetLastError(NTE_BAD_KEY
);
1353 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
1354 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
1356 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
1358 SetLastError(NTE_BAD_DATA
);
1362 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
1363 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
1364 SetLastError(NTE_BAD_DATA
);
1368 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
1369 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
1370 *pdwDataLen
= dwEncryptedLen
;
1372 if (*pdwDataLen
> dwBufLen
)
1374 SetLastError(ERROR_MORE_DATA
);
1378 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
1379 switch (pCryptKey
->dwMode
) {
1380 case CRYPT_MODE_ECB
:
1381 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1385 case CRYPT_MODE_CBC
:
1386 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
1387 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1389 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
1392 case CRYPT_MODE_CFB
:
1393 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
1394 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
,
1395 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
1396 out
[j
] = in
[j
] ^ o
[0];
1397 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
1398 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
1399 pCryptKey
->abChainVector
[k
] = out
[j
];
1404 SetLastError(NTE_BAD_ALGID
);
1407 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
1409 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
1410 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
1413 if (Final
) setup_key(pCryptKey
);
1418 /******************************************************************************
1419 * CPDecrypt (RSAENH.@)
1424 * hProv [I] The key container hKey and hHash belong to.
1425 * hKey [I] The key used to decrypt the data.
1426 * hHash [I] An optional hash object for parallel hashing. See notes.
1427 * Final [I] Indicates if this is the last block of data to decrypt.
1428 * dwFlags [I] Currently no flags defined. Must be zero.
1429 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1430 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1437 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1438 * This is useful for message signatures.
1440 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1443 * Parallel hashing not yet implemented.
1445 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
1446 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1448 CRYPTKEY
*pCryptKey
;
1449 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
1452 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1453 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
1455 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1457 SetLastError(NTE_BAD_UID
);
1463 SetLastError(NTE_BAD_FLAGS
);
1467 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1469 SetLastError(NTE_BAD_KEY
);
1473 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
1474 pCryptKey
->dwState
= RSAENH_KEYSTATE_DECRYPTING
;
1476 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_DECRYPTING
)
1478 SetLastError(NTE_BAD_DATA
);
1482 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
1483 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
1484 switch (pCryptKey
->dwMode
) {
1485 case CRYPT_MODE_ECB
:
1486 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1490 case CRYPT_MODE_CBC
:
1491 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, in
, out
,
1493 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
1494 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
1497 case CRYPT_MODE_CFB
:
1498 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
1499 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
,
1500 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
1501 out
[j
] = in
[j
] ^ o
[0];
1502 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
1503 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
1504 pCryptKey
->abChainVector
[k
] = in
[j
];
1509 SetLastError(NTE_BAD_ALGID
);
1512 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
1514 if (Final
) *pdwDataLen
-= pbData
[*pdwDataLen
-1];
1516 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
1517 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
1518 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
1519 encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
1521 if (Final
) setup_key(pCryptKey
);
1526 /******************************************************************************
1527 * CPExportKey (RSAENH.@)
1529 * Export a key into a binary large object (BLOB).
1532 * hProv [I] Key container from which a key is to be exported.
1533 * hKey [I] Key to be exported.
1534 * hPubKey [I] Key used to encrypt sensitive BLOB data.
1535 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
1536 * dwFlags [I] Currently none defined.
1537 * pbData [O] Pointer to a buffer where the BLOB will be written to.
1538 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
1544 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
1545 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
1547 CRYPTKEY
*pCryptKey
, *pPubKey
;
1548 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
1549 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
1550 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
1554 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
1555 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
1557 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1559 SetLastError(NTE_BAD_UID
);
1563 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
1565 SetLastError(NTE_BAD_KEY
);
1569 switch ((BYTE
)dwBlobType
)
1572 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
1573 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
1577 if (GET_ALG_CLASS(pCryptKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
) {
1578 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
1582 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
1584 if (*pdwDataLen
< dwDataLen
) {
1585 SetLastError(ERROR_MORE_DATA
);
1586 *pdwDataLen
= dwDataLen
;
1590 pBlobHeader
->bType
= SIMPLEBLOB
;
1591 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
1592 pBlobHeader
->reserved
= 0;
1593 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
1595 *pAlgid
= pPubKey
->aiAlgid
;
1597 pbRawData
= (BYTE
*)(pAlgid
+1);
1598 pbRawData
[0] = 0x00;
1599 pbRawData
[1] = RSAENH_PKC_BLOCKTYPE
;
1600 for (i
=2; i
< pPubKey
->dwBlockLen
- pCryptKey
->dwKeyLen
- 1; i
++)
1601 do gen_rand_impl(&pbRawData
[i
], 1); while (!pbRawData
[i
]);
1602 pbRawData
[i
] = 0x00;
1603 for (i
=0; i
<pCryptKey
->dwKeyLen
; i
++)
1604 pbRawData
[pPubKey
->dwBlockLen
- pCryptKey
->dwKeyLen
+ i
] =
1605 pCryptKey
->abKeyValue
[i
];
1607 encrypt_block_impl(pPubKey
->aiAlgid
, &pPubKey
->context
, pbRawData
, pbRawData
,
1610 *pdwDataLen
= dwDataLen
;
1614 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
1615 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
1619 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
1620 SetLastError(NTE_BAD_KEY
);
1624 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
1626 if (*pdwDataLen
< dwDataLen
) {
1627 SetLastError(ERROR_MORE_DATA
);
1628 *pdwDataLen
= dwDataLen
;
1632 pBlobHeader
->bType
= PUBLICKEYBLOB
;
1633 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
1634 pBlobHeader
->reserved
= 0;
1635 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
1637 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
1638 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
1640 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
1641 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
1643 *pdwDataLen
= dwDataLen
;
1646 case PRIVATEKEYBLOB
:
1647 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
1648 SetLastError(NTE_BAD_KEY
);
1652 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
1653 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
1655 if (*pdwDataLen
< dwDataLen
) {
1656 SetLastError(ERROR_MORE_DATA
);
1657 *pdwDataLen
= dwDataLen
;
1661 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
1662 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
1663 pBlobHeader
->reserved
= 0;
1664 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
1666 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
1667 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
1669 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
1670 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
1672 *pdwDataLen
= dwDataLen
;
1676 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
1681 /******************************************************************************
1682 * CPImportKey (RSAENH.@)
1684 * Import a BLOB'ed key into a key container.
1687 * hProv [I] Key container into which the key is to be imported.
1688 * pbData [I] Pointer to a buffer which holds the BLOB.
1689 * dwDataLen [I] Length of data in buffer at pbData.
1690 * hPubKey [I] Key used to decrypt sensitive BLOB data.
1691 * dwFlags [I] Currently none defined.
1692 * phKey [O] Handle to the imported key.
1698 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
1699 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1701 KEYCONTAINER
*pKeyContainer
;
1702 CRYPTKEY
*pCryptKey
, *pPubKey
;
1703 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
1704 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
1705 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
1706 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
1710 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
1711 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
1713 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
))
1715 SetLastError(NTE_BAD_UID
);
1719 if (dwDataLen
< sizeof(BLOBHEADER
) ||
1720 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
1721 pBlobHeader
->reserved
!= 0)
1723 SetLastError(NTE_BAD_DATA
);
1727 switch (pBlobHeader
->bType
)
1729 case PRIVATEKEYBLOB
:
1730 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
1731 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
) ||
1732 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
1733 (2 * pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
1735 SetLastError(NTE_BAD_DATA
);
1739 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
1740 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
1741 setup_key(pCryptKey
);
1742 return import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
1743 pRSAPubKey
->bitlen
/8, pRSAPubKey
->pubexp
);
1746 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
1747 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
1748 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
1750 SetLastError(NTE_BAD_DATA
);
1754 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
1755 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
1756 setup_key(pCryptKey
);
1757 return import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
1758 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
1761 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
1762 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
1764 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
1768 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
1770 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
1774 pbDecrypted
= (BYTE
*)HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
1775 if (!pbDecrypted
) return FALSE
;
1776 encrypt_block_impl(pPubKey
->aiAlgid
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
1779 for (i
=2; i
<pPubKey
->dwBlockLen
&& pbDecrypted
[i
]; i
++);
1780 if ((i
==pPubKey
->dwBlockLen
) ||
1781 (pbDecrypted
[0] != 0x00) ||
1782 (pbDecrypted
[1] != RSAENH_PKC_BLOCKTYPE
))
1784 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
1785 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
1789 dwKeyLen
= pPubKey
->dwBlockLen
-i
-1;
1790 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
1791 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
1793 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
1796 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
+i
+1, dwKeyLen
);
1797 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
1798 setup_key(pCryptKey
);
1802 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
1807 /******************************************************************************
1808 * CPGenKey (RSAENH.@)
1810 * Generate a key in the key container
1813 * hProv [I] Key container for which a key is to be generated.
1814 * Algid [I] Crypto algorithm identifier for the key to be generated.
1815 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
1816 * phKey [O] Handle to the generated key.
1823 * Flags currently not considered.
1826 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
1827 * and AT_SIGNATURE values.
1829 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
1831 KEYCONTAINER
*pKeyContainer
;
1832 CRYPTKEY
*pCryptKey
;
1834 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
1836 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
1837 (OBJECTHDR
**)&pKeyContainer
))
1839 /* MSDN: hProv not containing valid context handle */
1840 SetLastError(NTE_BAD_UID
);
1848 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
1850 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
1851 setup_key(pCryptKey
);
1852 if (Algid
== AT_SIGNATURE
) {
1853 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
1854 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
1855 (unsigned int*)&pKeyContainer
->hSignatureKeyPair
);
1860 case AT_KEYEXCHANGE
:
1862 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
1864 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
1865 setup_key(pCryptKey
);
1866 if (Algid
== AT_KEYEXCHANGE
) {
1867 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
1868 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
1869 (unsigned int*)&pKeyContainer
->hKeyExchangeKeyPair
);
1879 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
1881 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
1882 setup_key(pCryptKey
);
1887 /* MSDN: Algorithm not supported specified by Algid */
1888 SetLastError(NTE_BAD_ALGID
);
1892 return *phKey
!= (unsigned int)INVALID_HANDLE_VALUE
;
1895 /******************************************************************************
1896 * CPGenRandom (RSAENH.@)
1898 * Generate a random byte stream.
1901 * hProv [I] Key container that is used to generate random bytes.
1902 * dwLen [I] Specifies the number of requested random data bytes.
1903 * pbBuffer [O] Random bytes will be stored here.
1909 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
1911 KEYCONTAINER
*pKeyContainer
;
1913 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
1915 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
1916 (OBJECTHDR
**)&pKeyContainer
))
1918 /* MSDN: hProv not containing valid context handle */
1919 SetLastError(NTE_BAD_UID
);
1923 return gen_rand_impl(pbBuffer
, dwLen
);
1926 /******************************************************************************
1927 * CPGetHashParam (RSAENH.@)
1929 * Query parameters of an hash object.
1932 * hProv [I] The kea container, which the hash belongs to.
1933 * hHash [I] The hash object that is to be queried.
1934 * dwParam [I] Specifies the parameter that is to be queried.
1935 * pbData [I] Pointer to the buffer where the parameter value will be stored.
1936 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
1937 * dwFlags [I] None currently defined.
1944 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
1945 * finalized if HP_HASHVALUE is queried.
1947 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
1948 DWORD
*pdwDataLen
, DWORD dwFlags
)
1950 CRYPTHASH
*pCryptHash
;
1951 KEYCONTAINER
*pKeyContainer
;
1952 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
1954 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
1955 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
1957 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
1958 (OBJECTHDR
**)&pKeyContainer
))
1960 SetLastError(NTE_BAD_UID
);
1966 SetLastError(NTE_BAD_FLAGS
);
1970 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
1971 (OBJECTHDR
**)&pCryptHash
))
1973 SetLastError(NTE_BAD_HASH
);
1979 SetLastError(ERROR_INVALID_PARAMETER
);
1986 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
1990 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
1994 if (pCryptHash
->dwState
== RSAENH_HASHSTATE_IDLE
) {
1995 SetLastError(NTE_BAD_HASH_STATE
);
1999 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
2001 finalize_hash(pCryptHash
);
2002 if (pCryptHash
->aiAlgid
== CALG_HMAC
) {
2003 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
2004 init_hash(pKeyContainer
, pCryptHash
);
2005 update_hash(pCryptHash
, pCryptHash
->pHMACInfo
->pbOuterString
,
2006 pCryptHash
->pHMACInfo
->cbOuterString
);
2007 update_hash(pCryptHash
, abHashValue
, pCryptHash
->dwHashSize
);
2008 finalize_hash(pCryptHash
);
2011 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
2014 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptHash
->abHashValue
,
2015 pCryptHash
->dwHashSize
);
2018 SetLastError(NTE_BAD_TYPE
);
2023 /******************************************************************************
2024 * CPSetKeyParam (RSAENH.@)
2026 * Set a parameter of a key object
2029 * hProv [I] The key container to which the key belongs.
2030 * hKey [I] The key for which a parameter is to be set.
2031 * dwParam [I] Parameter type. See Notes.
2032 * pbData [I] Pointer to the parameter value.
2033 * dwFlags [I] Currently none defined.
2040 * Defined dwParam types are:
2041 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2042 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
2043 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2044 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2045 * - KP_IV: Initialization vector
2047 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2050 CRYPTKEY
*pCryptKey
;
2052 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv
, hKey
,
2053 dwParam
, pbData
, dwFlags
);
2055 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2057 SetLastError(NTE_BAD_UID
);
2062 SetLastError(NTE_BAD_FLAGS
);
2066 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2068 SetLastError(NTE_BAD_KEY
);
2074 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
2078 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
2081 case KP_PERMISSIONS
:
2082 pCryptKey
->dwPermissions
= *(DWORD
*)pbData
;
2086 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
2090 SetLastError(NTE_BAD_TYPE
);
2095 /******************************************************************************
2096 * CPGetKeyParam (RSAENH.@)
2098 * Query a key parameter.
2101 * hProv [I] The key container, which the key belongs to.
2102 * hHash [I] The key object that is to be queried.
2103 * dwParam [I] Specifies the parameter that is to be queried.
2104 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2105 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2106 * dwFlags [I] None currently defined.
2113 * Defined dwParam types are:
2114 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2115 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2116 * (Currently ignored by MS CSP's - always eight)
2117 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2118 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2119 * - KP_IV: Initialization vector.
2120 * - KP_KEYLEN: Bitwidth of the key.
2121 * - KP_BLOCKLEN: Size of a block cipher block.
2122 * - KP_SALT: Salt value.
2124 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
2125 DWORD
*pdwDataLen
, DWORD dwFlags
)
2127 CRYPTKEY
*pCryptKey
;
2130 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2131 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2133 if (!is_valid_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2135 SetLastError(NTE_BAD_UID
);
2140 SetLastError(NTE_BAD_FLAGS
);
2144 if (!lookup_handle(&handle_table
, (unsigned int)hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2146 SetLastError(NTE_BAD_KEY
);
2153 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pCryptKey
->abInitVector
,
2154 pCryptKey
->dwBlockLen
);
2157 return copy_param(pbData
, pdwDataLen
,
2158 (CONST BYTE
*)&pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
], pCryptKey
->dwSaltLen
);
2161 dwBitLen
= pCryptKey
->dwKeyLen
<< 3;
2162 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2165 dwBitLen
= pCryptKey
->dwBlockLen
<< 3;
2166 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwBitLen
, sizeof(DWORD
));
2169 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
2172 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
2175 case KP_PERMISSIONS
:
2176 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
2180 SetLastError(NTE_BAD_TYPE
);
2185 /******************************************************************************
2186 * CPGetProvParam (RSAENH.@)
2188 * Query a CSP parameter.
2191 * hProv [I] The key container that is to be queried.
2192 * dwParam [I] Specifies the parameter that is to be queried.
2193 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2194 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2195 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2201 * Defined dwParam types:
2202 * - PP_CONTAINER: Name of the key container.
2203 * - PP_NAME: Name of the cryptographic service provider.
2204 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2205 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2206 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2208 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
2209 DWORD
*pdwDataLen
, DWORD dwFlags
)
2211 KEYCONTAINER
*pKeyContainer
;
2212 PROV_ENUMALGS provEnumalgs
;
2215 /* This is for dwParam 41, which does not seem to be documented
2216 * on MSDN. IE6 SP1 asks for it in the 'About' dialog, however.
2217 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
2218 * to be 'don't care's. If you know anything more specific about
2219 * provider parameter 41, please report to wine-devel@winehq.org */
2220 static CONST BYTE abWTF
[96] = {
2221 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
2222 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
2223 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
2224 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
2225 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
2226 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
2227 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
2228 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
2229 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
2230 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
2231 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
2232 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
2235 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2236 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
2238 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2239 (OBJECTHDR
**)&pKeyContainer
))
2241 /* MSDN: hProv not containing valid context handle */
2242 SetLastError(NTE_BAD_UID
);
2249 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
2250 strlen(pKeyContainer
->szName
)+1);
2253 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
2254 strlen(pKeyContainer
->szProvName
)+1);
2256 case PP_SIG_KEYSIZE_INC
:
2257 case PP_KEYX_KEYSIZE_INC
:
2259 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
2262 case PP_ENUMALGS_EX
:
2263 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
2264 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
2265 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
2266 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
2268 SetLastError(ERROR_NO_MORE_ITEMS
);
2272 if (dwParam
== PP_ENUMALGS
) {
2273 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
2274 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
2275 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
2277 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
2278 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
2279 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
2280 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
2281 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
2282 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
2283 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
2284 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
2287 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
2288 sizeof(PROV_ENUMALGS
));
2290 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
2291 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
2292 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
2294 return copy_param(pbData
, pdwDataLen
,
2295 (CONST BYTE
*)&aProvEnumAlgsEx
2296 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
2297 sizeof(PROV_ENUMALGS_EX
));
2300 case 41: /* Undocumented. Asked for by IE About dialog */
2301 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
2304 /* MSDN: Unknown parameter number in dwParam */
2305 SetLastError(NTE_BAD_TYPE
);
2311 /******************************************************************************
2312 * CPDeriveKey (RSAENH.@)
2314 * Derives a key from a hash value.
2317 * hProv [I] Key container for which a key is to be generated.
2318 * Algid [I] Crypto algorithm identifier for the key to be generated.
2319 * hBaseData [I] Hash from whose value the key will be derived.
2320 * dwFlags [I] See Notes.
2321 * phKey [O] The generated key.
2329 * - CRYPT_EXPORTABLE: Key can be exported.
2330 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2331 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2333 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
2334 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2336 KEYCONTAINER
*pKeyContainer
;
2337 CRYPTKEY
*pCryptKey
;
2338 CRYPTHASH
*pCryptHash
;
2339 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
2342 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv
, Algid
,
2343 hBaseData
, dwFlags
, phKey
);
2345 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2346 (OBJECTHDR
**)&pKeyContainer
))
2348 SetLastError(NTE_BAD_UID
);
2352 if (!lookup_handle(&handle_table
, (unsigned int)hBaseData
, RSAENH_MAGIC_HASH
,
2353 (OBJECTHDR
**)&pCryptHash
))
2355 SetLastError(NTE_BAD_HASH
);
2361 SetLastError(ERROR_INVALID_PARAMETER
);
2365 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
2366 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2369 * We derive the key material from the hash.
2370 * If the hash value is not large enough for the claimed key, we have to construct
2371 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
2373 dwLen
= RSAENH_MAX_HASH_SIZE
;
2374 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
2376 if (dwLen
< pCryptKey
->dwKeyLen
) {
2377 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
], old_hashval
[RSAENH_MAX_HASH_SIZE
];
2380 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
2382 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
2383 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
2384 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
2387 init_hash(pKeyContainer
, pCryptHash
);
2388 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
2389 finalize_hash(pCryptHash
);
2390 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
2392 init_hash(pKeyContainer
, pCryptHash
);
2393 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
2394 finalize_hash(pCryptHash
);
2395 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
2396 pCryptHash
->dwHashSize
);
2398 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
2401 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
2402 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
2404 setup_key(pCryptKey
);
2408 /******************************************************************************
2409 * CPGetUserKey (RSAENH.@)
2411 * Returns a handle to the user's private key-exchange- or signature-key.
2414 * hProv [I] The key container from which a user key is requested.
2415 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
2416 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
2423 * A newly created key container does not contain private user key. Create them with CPGenKey.
2425 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
2427 KEYCONTAINER
*pKeyContainer
;
2429 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
2431 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2432 (OBJECTHDR
**)&pKeyContainer
))
2434 /* MSDN: hProv not containing valid context handle */
2435 SetLastError(NTE_BAD_UID
);
2441 case AT_KEYEXCHANGE
:
2442 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
2443 (unsigned int*)phUserKey
);
2447 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
2448 (unsigned int*)phUserKey
);
2452 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
2455 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2457 /* MSDN: dwKeySpec parameter specifies non existent key */
2458 SetLastError(NTE_NO_KEY
);
2465 /******************************************************************************
2466 * CPHashData (RSAENH.@)
2468 * Updates a hash object with the given data.
2471 * hProv [I] Key container to which the hash object belongs.
2472 * hHash [I] Hash object which is to be updated.
2473 * pbData [I] Pointer to data with which the hash object is to be updated.
2474 * dwDataLen [I] Length of the data.
2475 * dwFlags [I] Currently none defined.
2482 * The actual hash value is queried with CPGetHashParam, which will finalize
2483 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
2485 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
2486 DWORD dwDataLen
, DWORD dwFlags
)
2488 CRYPTHASH
*pCryptHash
;
2489 KEYCONTAINER
*pKeyContainer
;
2491 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
2492 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
2494 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2495 (OBJECTHDR
**)&pKeyContainer
))
2497 SetLastError(NTE_BAD_UID
);
2503 SetLastError(NTE_BAD_FLAGS
);
2507 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
2508 (OBJECTHDR
**)&pCryptHash
))
2510 SetLastError(NTE_BAD_HASH
);
2514 if (!get_algid_info(pKeyContainer
, pCryptHash
->aiAlgid
) ||
2515 pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
2517 SetLastError(NTE_BAD_ALGID
);
2521 if (pCryptHash
->dwState
== RSAENH_HASHSTATE_IDLE
)
2522 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
2524 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
2526 SetLastError(NTE_BAD_HASH_STATE
);
2530 update_hash(pCryptHash
, pbData
, dwDataLen
);
2534 /******************************************************************************
2535 * CPHashSessionKey (RSAENH.@)
2537 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
2544 /******************************************************************************
2545 * CPReleaseContext (RSAENH.@)
2547 * Release a key container.
2550 * hProv [I] Key container to be released.
2551 * dwFlags [I] Currently none defined.
2557 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
2559 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv
, dwFlags
);
2561 if (!release_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
))
2563 /* MSDN: hProv not containing valid context handle */
2564 SetLastError(NTE_BAD_UID
);
2571 /******************************************************************************
2572 * CPSetHashParam (RSAENH.@)
2574 * Set a parameter of a hash object
2577 * hProv [I] The key container to which the key belongs.
2578 * hHash [I] The hash object for which a parameter is to be set.
2579 * dwParam [I] Parameter type. See Notes.
2580 * pbData [I] Pointer to the parameter value.
2581 * dwFlags [I] Currently none defined.
2588 * Currently only the HP_HMAC_INFO dwParam type is defined.
2589 * The HMAC_INFO struct will be deep copied into the hash object.
2590 * See Internet RFC 2104 for details on the HMAC algorithm.
2592 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
2593 BYTE
*pbData
, DWORD dwFlags
)
2595 CRYPTHASH
*pCryptHash
;
2596 CRYPTKEY
*pCryptKey
;
2597 KEYCONTAINER
*pKeyContainer
;
2600 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
2601 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
2603 if (!lookup_handle(&handle_table
, (unsigned int)hProv
, RSAENH_MAGIC_CONTAINER
,
2604 (OBJECTHDR
**)&pKeyContainer
))
2606 SetLastError(NTE_BAD_UID
);
2611 SetLastError(NTE_BAD_FLAGS
);
2615 if (!lookup_handle(&handle_table
, (unsigned int)hHash
, RSAENH_MAGIC_HASH
,
2616 (OBJECTHDR
**)&pCryptHash
))
2618 SetLastError(NTE_BAD_HASH
);
2624 free_hmac_info(pCryptHash
->pHMACInfo
);
2625 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
2626 init_hash(pKeyContainer
, pCryptHash
);
2628 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
2629 (OBJECTHDR
**)&pCryptKey
))
2631 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
2635 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
2636 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
2638 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
2639 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
2642 return RSAENH_CPHashData(hProv
, hHash
, pCryptHash
->pHMACInfo
->pbInnerString
,
2643 pCryptHash
->pHMACInfo
->cbInnerString
, 0);
2646 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
2647 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
2651 SetLastError(NTE_BAD_TYPE
);
2656 /******************************************************************************
2657 * CPSetProvParam (RSAENH.@)
2659 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
2665 /******************************************************************************
2666 * CPSignHash (RSAENH.@)
2668 * Sign a hash object
2671 * hProv [I] The key container, to which the hash object belongs.
2672 * hHash [I] The hash object to be signed.
2673 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
2674 * sDescription [I] Should be NULL for security reasons.
2675 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
2676 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
2677 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
2684 * FIXME: sDescription is currently ignored.
2686 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
2687 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
2690 HCRYPTKEY hCryptKey
;
2691 CRYPTKEY
*pCryptKey
;
2693 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
2696 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
2697 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
2698 dwFlags
, pbSignature
, pdwSigLen
);
2700 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
2701 SetLastError(NTE_BAD_FLAGS
);
2705 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
2707 if (!lookup_handle(&handle_table
, (unsigned int)hCryptKey
, RSAENH_MAGIC_KEY
,
2708 (OBJECTHDR
**)&pCryptKey
))
2710 SetLastError(NTE_NO_KEY
);
2714 dwHashLen
= sizeof(DWORD
);
2715 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
2717 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
2718 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
2721 *pdwSigLen
= pCryptKey
->dwKeyLen
;
2724 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
2726 SetLastError(ERROR_MORE_DATA
);
2727 *pdwSigLen
= pCryptKey
->dwKeyLen
;
2730 *pdwSigLen
= pCryptKey
->dwKeyLen
;
2732 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
2736 return encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
2739 /******************************************************************************
2740 * CPVerifySignature (RSAENH.@)
2742 * Verify the signature of a hash object.
2745 * hProv [I] The key container, to which the hash belongs.
2746 * hHash [I] The hash for which the signature is verified.
2747 * pbSignature [I] The binary signature.
2748 * dwSigLen [I] Length of the signature BLOB.
2749 * hPubKey [I] Public key used to verify the signature.
2750 * sDescription [I] Should be NULL for security reasons.
2751 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
2754 * Success: TRUE (Signature is valid)
2755 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
2758 * FIXME: sDescription is currently ignored.
2760 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
2761 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
2764 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
2765 CRYPTKEY
*pCryptKey
;
2768 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
2771 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%ld, hPubKey=%08lx, sDescription=%s, "
2772 "dwFlags=%08lx)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
2775 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
2776 SetLastError(NTE_BAD_FLAGS
);
2780 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2782 SetLastError(NTE_BAD_UID
);
2786 if (!lookup_handle(&handle_table
, (unsigned int)hPubKey
, RSAENH_MAGIC_KEY
,
2787 (OBJECTHDR
**)&pCryptKey
))
2789 SetLastError(NTE_BAD_KEY
);
2793 dwHashLen
= sizeof(DWORD
);
2794 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
2796 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
2797 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
2799 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
2800 if (!pbConstructed
) {
2801 SetLastError(NTE_NO_MEMORY
);
2805 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
2807 SetLastError(NTE_NO_MEMORY
);
2811 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
2817 if (!build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
2821 if (memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
2822 SetLastError(NTE_BAD_SIGNATURE
);
2828 if (pbConstructed
) HeapFree(GetProcessHeap(), 0, pbConstructed
);
2829 if (pbDecrypted
) HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2833 static const WCHAR szProviderKeys
[3][97] = {
2834 { 'S','o','f','t','w','a','r','e','\\',
2835 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2836 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2837 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
2838 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2839 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2840 { 'S','o','f','t','w','a','r','e','\\',
2841 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2842 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2843 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
2844 'E','n','h','a','n','c','e','d',
2845 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2846 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2847 { 'S','o','f','t','w','a','r','e','\\',
2848 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2849 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2850 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
2851 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2852 'o','v','i','d','e','r',0 }
2854 static const WCHAR szDefaultKey
[] = { 'S','o','f','t','w','a','r','e','\\',
2855 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2856 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2857 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0};
2859 /******************************************************************************
2860 * DllRegisterServer (RSAENH.@)
2862 * Dll self registration.
2871 * Registers the following keys:
2872 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2873 * Microsoft Base Cryptographic Provider v1.0
2874 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2875 * Microsoft Enhanced Cryptographic Provider
2876 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2877 * Microsoft Strong Cryptographpic Provider
2878 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
2880 HRESULT WINAPI
RSAENH_DllRegisterServer()
2887 for (i
=0; i
<3; i
++) {
2888 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szProviderKeys
[i
], 0, NULL
,
2889 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
2891 if (apiRet
== ERROR_SUCCESS
)
2893 if (dp
== REG_CREATED_NEW_KEY
)
2895 static const WCHAR szImagePath
[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
2896 static const WCHAR szRSABase
[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
2897 static const WCHAR szType
[] = { 'T','y','p','e',0 };
2898 static const WCHAR szSignature
[] = { 'S','i','g','n','a','t','u','r','e',0 };
2900 DWORD sign
= 0xdeadbeef;
2901 RegSetValueExW(key
, szImagePath
, 0, REG_SZ
, (LPBYTE
)szRSABase
,
2902 (lstrlenW(szRSABase
) + 1) * sizeof(WCHAR
));
2903 RegSetValueExW(key
, szType
, 0, REG_DWORD
, (LPBYTE
)&type
, sizeof(type
));
2904 RegSetValueExW(key
, szSignature
, 0, REG_BINARY
, (LPBYTE
)&sign
, sizeof(sign
));
2909 if (apiRet
== ERROR_SUCCESS
)
2910 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szDefaultKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
2911 KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
2912 if (apiRet
== ERROR_SUCCESS
)
2914 if (dp
== REG_CREATED_NEW_KEY
)
2916 static const WCHAR szName
[] = { 'N','a','m','e',0 };
2917 static const WCHAR szRSAName
[] = {
2918 'M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',' ',
2919 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
2920 'P','r','o','v','i','d','e','r',0 };
2921 static const WCHAR szTypeName
[] = { 'T','y','p','e','N','a','m','e',0 };
2922 static const WCHAR szRSATypeName
[] = {
2923 'R','S','A',' ','F','u','l','l',' ',
2924 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
2925 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 };
2927 RegSetValueExW(key
, szName
, 0, REG_SZ
, (LPBYTE
)szRSAName
, sizeof(szRSAName
));
2928 RegSetValueExW(key
, szTypeName
, 0, REG_SZ
, (LPBYTE
)szRSATypeName
,sizeof(szRSATypeName
));
2932 return HRESULT_FROM_WIN32(apiRet
);
2935 /******************************************************************************
2936 * DllUnregisterServer (RSAENH.@)
2938 * Dll self unregistration.
2946 * For the relevant keys see DllRegisterServer.
2948 HRESULT WINAPI
RSAENH_DllUnregisterServer()
2950 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[0]);
2951 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[1]);
2952 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[2]);
2953 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKey
);