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
128 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
129 /******************************************************************************
130 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
132 #define RSAENH_MAX_ENUMALGS 14
133 PROV_ENUMALGS_EX aProvEnumAlgsEx[3][RSAENH_MAX_ENUMALGS+1] =
136 {CALG_RC2, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
137 {CALG_RC4, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
138 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
139 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
140 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
141 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
142 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
143 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
144 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
145 {CALG_RSA_SIGN, 512,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
146 {CALG_RSA_KEYX, 512,384, 1024,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"},
147 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
148 {0, 0, 0, 0,0, 1,"", 1,""}
151 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
152 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
153 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
154 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
155 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
156 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
157 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
158 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
159 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
160 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
161 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
162 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
163 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"},
164 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
165 {0, 0, 0, 0,0, 1,"", 1,""}
168 {CALG_RC2, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
169 {CALG_RC4, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
170 {CALG_DES, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
171 {CALG_3DES_112, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
172 {CALG_3DES, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
173 {CALG_SHA, 160,160, 160,CRYPT_FLAG_SIGNING, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
174 {CALG_MD2, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD2", 27,"MD2 Message Digest 2 (MD2)"},
175 {CALG_MD4, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD4", 27,"MD4 Message Digest 4 (MD4)"},
176 {CALG_MD5, 128,128, 128,CRYPT_FLAG_SIGNING, 4,"MD5", 27,"MD5 Message Digest 5 (MD5)"},
177 {CALG_SSL3_SHAMD5,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
178 {CALG_MAC, 0, 0, 0,0, 4,"MAC", 27,"Message Authentication Code"},
179 {CALG_RSA_SIGN,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_SIGN",14,"RSA Signature"},
180 {CALG_RSA_KEYX,1024,384,16384,CRYPT_FLAG_SIGNING|CRYPT_FLAG_IPSEC,9,"RSA_KEYX",18,"RSA Key Exchange"},
181 {CALG_HMAC, 0, 0, 0,0, 5,"HMAC", 23,"HMAC Hugo's MAC (HMAC)"},
182 {0, 0, 0, 0,0, 1,"", 1,""}
186 /******************************************************************************
187 * API forward declarations
190 RSAENH_CPGetKeyParam(
212 RSAENH_CPGetHashParam(
242 /******************************************************************************
243 * CSP's handle table (used by all acquired key containers)
245 static HANDLETABLE handle_table;
247 /******************************************************************************
250 * Initializes and destroys the handle table for the CSP's handles.
252 int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
256 case DLL_PROCESS_ATTACH:
257 init_handle_table(&handle_table);
260 case DLL_PROCESS_DETACH:
261 destroy_handle_table(&handle_table);
267 /******************************************************************************
268 * copy_param [Internal]
270 * Helper function that supports the standard WINAPI protocol for querying data
274 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
275 * May be NUL if the required buffer size is to be queried only.
276 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
277 * Out: Size of parameter pbParam
278 * pbParam [I] Parameter value.
279 * dwParamSize [I] Size of pbParam
282 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
283 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
285 static inline BOOL copy_param(
286 BYTE *pbBuffer, DWORD *pdwBufferSize, CONST BYTE *pbParam, DWORD dwParamSize)
290 if (dwParamSize > *pdwBufferSize)
292 SetLastError(ERROR_MORE_DATA);
293 *pdwBufferSize = dwParamSize;
296 memcpy(pbBuffer, pbParam, dwParamSize);
298 *pdwBufferSize = dwParamSize;
302 /******************************************************************************
303 * get_algid_info [Internal]
305 * Query CSP capabilities for a given crypto algorithm.
308 * pKeyContainer [I] Pointer to a key container of the CSP whose capabilities are to be queried.
309 * algid [I] Identifier of the crypto algorithm about which information is requested.
312 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
313 * Failure: NULL (algid not supported)
315 static inline const PROV_ENUMALGS_EX* get_algid_info(KEYCONTAINER *pKeyContainer, ALG_ID algid) {
316 PROV_ENUMALGS_EX *iterator;
318 for (iterator = aProvEnumAlgsEx[pKeyContainer->dwPersonality]; iterator->aiAlgid; iterator++) {
319 if (iterator->aiAlgid == algid) return iterator;
325 /******************************************************************************
326 * free_hmac_info [Internal]
328 * Deeply free an HMAC_INFO struct.
331 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
334 * See Internet RFC 2104 for details on the HMAC algorithm.
336 static inline void free_hmac_info(PHMAC_INFO hmac_info) {
337 if (!hmac_info) return;
338 if (hmac_info->pbInnerString) HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
339 if (hmac_info->pbOuterString) HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
340 HeapFree(GetProcessHeap(), 0, hmac_info);
343 /******************************************************************************
344 * copy_hmac_info [Internal]
346 * Deeply copy an HMAC_INFO struct
349 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
350 * src [I] Pointer to the HMAC_INFO struct to be copied.
357 * See Internet RFC 2104 for details on the HMAC algorithm.
359 static BOOL copy_hmac_info(PHMAC_INFO *dst, PHMAC_INFO src) {
360 if (!src) return FALSE;
361 *dst = (PHMAC_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
362 if (!*dst) return FALSE;
363 memcpy(*dst, src, sizeof(HMAC_INFO));
364 (*dst)->pbInnerString = NULL;
365 (*dst)->pbOuterString = NULL;
366 if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
367 (*dst)->pbInnerString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
368 if (!(*dst)->pbInnerString) {
369 free_hmac_info(*dst);
372 if (src->cbInnerString)
373 memcpy((*dst)->pbInnerString, src->pbInnerString, src->cbInnerString);
375 memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
376 if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
377 (*dst)->pbOuterString = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
378 if (!(*dst)->pbOuterString) {
379 free_hmac_info(*dst);
382 if (src->cbOuterString)
383 memcpy((*dst)->pbOuterString, src->pbOuterString, src->cbOuterString);
385 memset((*dst)->pbOuterString, RSAENH_HMAC_DEF_OPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
389 /******************************************************************************
390 * destroy_hash [Internal]
392 * Destructor for hash objects
395 * pCryptHash [I] Pointer to the hash object to be destroyed.
396 * Will be invalid after function returns!
398 static void destroy_hash(OBJECTHDR *pCryptHash)
400 free_hmac_info(((CRYPTHASH*)pCryptHash)->pHMACInfo);
401 HeapFree(GetProcessHeap(), 0, pCryptHash);
404 /******************************************************************************
405 * init_hash [Internal]
407 * Initialize (or reset) a hash object
410 * pKeyContainer [I] Pointer to the key container the hash object belongs to.
411 * pCryptHash [I] The hash object to be initialized.
413 static inline BOOL init_hash(KEYCONTAINER *pKeyContainer, CRYPTHASH *pCryptHash) {
415 const PROV_ENUMALGS_EX *pAlgInfo;
417 switch (pCryptHash->aiAlgid)
420 if (pCryptHash->pHMACInfo) {
421 pAlgInfo = get_algid_info(pKeyContainer, pCryptHash->pHMACInfo->HashAlgid);
422 if (pAlgInfo) pCryptHash->dwHashSize = pAlgInfo->dwDefaultLen >> 3;
423 return init_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context);
428 dwLen = sizeof(DWORD);
429 RSAENH_CPGetKeyParam(pCryptHash->hProv, pCryptHash->hKey, KP_BLOCKLEN,
430 (BYTE*)&pCryptHash->dwHashSize, &dwLen, 0);
431 pCryptHash->dwHashSize >>= 3;
435 return init_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context);
439 /******************************************************************************
440 * update_hash [Internal]
442 * Hashes the given data and updates the hash object's state accordingly
445 * pCryptHash [I] Hash object to be updated.
446 * pbData [I] Pointer to data stream to be hashed.
447 * dwDataLen [I] Length of data stream.
449 static inline void update_hash(CRYPTHASH *pCryptHash, CONST BYTE *pbData, DWORD dwDataLen) {
452 switch (pCryptHash->aiAlgid)
455 if (pCryptHash->pHMACInfo)
456 update_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
461 pbTemp = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
463 memcpy(pbTemp, pbData, dwDataLen);
464 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, FALSE, 0,
465 pbTemp, &dwDataLen, dwDataLen);
466 HeapFree(GetProcessHeap(), 0, pbTemp);
470 update_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pbData, dwDataLen);
474 /******************************************************************************
475 * finalize_hash [Internal]
477 * Finalizes the hash, after all data has been hashed with update_hash.
478 * No additional data can be hashed afterwards until the hash gets initialized again.
481 * pCryptHash [I] Hash object to be finalized.
483 static inline void finalize_hash(CRYPTHASH *pCryptHash) {
486 switch (pCryptHash->aiAlgid)
489 if (pCryptHash->pHMACInfo)
490 finalize_hash_impl(pCryptHash->pHMACInfo->HashAlgid, &pCryptHash->context,
491 pCryptHash->abHashValue);
496 RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, (HCRYPTHASH)NULL, TRUE, 0,
497 pCryptHash->abHashValue, &dwDataLen, pCryptHash->dwHashSize);
501 finalize_hash_impl(pCryptHash->aiAlgid, &pCryptHash->context, pCryptHash->abHashValue);
505 /******************************************************************************
506 * destroy_key [Internal]
508 * Destructor for key objects
511 * pCryptKey [I] Pointer to the key object to be destroyed.
512 * Will be invalid after function returns!
514 static void destroy_key(OBJECTHDR *pCryptKey)
516 free_key_impl(((CRYPTKEY*)pCryptKey)->aiAlgid, &((CRYPTKEY*)pCryptKey)->context);
517 HeapFree(GetProcessHeap(), 0, pCryptKey);
520 /******************************************************************************
521 * setup_key [Internal]
523 * Initialize (or reset) a key object
526 * pCryptKey [I] The key object to be initialized.
528 static inline void setup_key(CRYPTKEY *pCryptKey) {
529 pCryptKey->dwState = RSAENH_KEYSTATE_IDLE;
530 memcpy(pCryptKey->abChainVector, pCryptKey->abInitVector, sizeof(pCryptKey->abChainVector));
531 setup_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen,
532 pCryptKey->dwSaltLen, pCryptKey->abKeyValue);
535 /******************************************************************************
538 * Creates a new key object. If neither pbKey nor hHash is given a random key
542 * hProv [I] Handle to the provider to which the created key will belong.
543 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
544 * dwFlags [I] Upper 16 bits give the key length.
545 * Lower 16 bits: CRYPT_CREATE_SALT, CRYPT_NO_SALT
546 * pbKey [I] Byte stream to be used as key material. May be NULL.
547 * hHash [I] Handle to a hash object whose value will be used as key material. May be zero.
550 * Success: Handle to the created key.
551 * Failure: INVALID_HANDLE_VALUE
553 static HCRYPTKEY new_key(HCRYPTPROV hProv, ALG_ID aiAlgid, DWORD dwFlags, BYTE *pbKey,
556 KEYCONTAINER *pKeyContainer;
559 CRYPTHASH *pCryptHash;
560 DWORD dwKeyLen = HIWORD(dwFlags), i;
561 const PROV_ENUMALGS_EX *peaAlgidInfo;
562 BYTE abHashValue[RSAENH_MAX_HASH_SIZE*2];
564 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
566 SetLastError(NTE_BAD_UID);
567 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
571 * Retrieve the CSP's capabilities for the given ALG_ID value
573 peaAlgidInfo = get_algid_info(pKeyContainer, aiAlgid);
575 SetLastError(NTE_BAD_ALGID);
576 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
580 * Assume the default key length, if none is specified explicitly
582 if (dwKeyLen == 0) dwKeyLen = peaAlgidInfo->dwDefaultLen;
585 * Check if the requested key length is supported by the current CSP.
586 * Adjust key length's for DES algorithms.
590 if (dwKeyLen == RSAENH_DES_EFFECTIVE_KEYLEN) {
591 dwKeyLen = RSAENH_DES_STORAGE_KEYLEN;
593 if (dwKeyLen != RSAENH_DES_STORAGE_KEYLEN) {
594 SetLastError(NTE_BAD_FLAGS);
595 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
600 if (dwKeyLen == RSAENH_3DES112_EFFECTIVE_KEYLEN) {
601 dwKeyLen = RSAENH_3DES112_STORAGE_KEYLEN;
603 if (dwKeyLen != RSAENH_3DES112_STORAGE_KEYLEN) {
604 SetLastError(NTE_BAD_FLAGS);
605 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
610 if (dwKeyLen == RSAENH_3DES_EFFECTIVE_KEYLEN) {
611 dwKeyLen = RSAENH_3DES_STORAGE_KEYLEN;
613 if (dwKeyLen != RSAENH_3DES_STORAGE_KEYLEN) {
614 SetLastError(NTE_BAD_FLAGS);
615 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
621 dwKeyLen > peaAlgidInfo->dwMaxLen ||
622 dwKeyLen < peaAlgidInfo->dwMinLen)
624 SetLastError(NTE_BAD_FLAGS);
625 return (HCRYPTKEY)INVALID_HANDLE_VALUE;
630 * If a valid hash handle is supplied, we derive the key material from the hash.
631 * If the hash value is not large enough for the claimed key, we have to construct
632 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
634 if (lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pCryptHash)) {
635 DWORD dwLen = RSAENH_MAX_HASH_SIZE;
637 RSAENH_CPGetHashParam(pCryptHash->hProv, hHash, HP_HASHVAL, abHashValue, &dwLen, 0);
639 if (dwLen < (dwKeyLen >> 3)) {
640 BYTE pad1[RSAENH_HMAC_DEF_PAD_LEN], pad2[RSAENH_HMAC_DEF_PAD_LEN], old_hashval[RSAENH_MAX_HASH_SIZE];
642 memcpy(old_hashval, pCryptHash->abHashValue, RSAENH_MAX_HASH_SIZE);
644 for (i=0; i<RSAENH_HMAC_DEF_PAD_LEN; i++) {
645 pad1[i] = RSAENH_HMAC_DEF_IPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
646 pad2[i] = RSAENH_HMAC_DEF_OPAD_CHAR ^ (i<dwLen ? abHashValue[i] : 0);
649 init_hash(pKeyContainer, pCryptHash);
650 update_hash(pCryptHash, pad1, RSAENH_HMAC_DEF_PAD_LEN);
651 finalize_hash(pCryptHash);
652 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
654 init_hash(pKeyContainer, pCryptHash);
655 update_hash(pCryptHash, pad2, RSAENH_HMAC_DEF_PAD_LEN);
656 finalize_hash(pCryptHash);
657 memcpy(abHashValue+pCryptHash->dwHashSize, pCryptHash->abHashValue,
658 pCryptHash->dwHashSize);
660 memcpy(pCryptHash->abHashValue, old_hashval, RSAENH_MAX_HASH_SIZE);
666 hCryptKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY,
667 destroy_key, (OBJECTHDR**)&pCryptKey);
668 if (hCryptKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
670 pCryptKey->aiAlgid = aiAlgid;
671 pCryptKey->hProv = hProv;
672 pCryptKey->dwModeBits = 0;
673 pCryptKey->dwPermissions = CRYPT_ENCRYPT | CRYPT_DECRYPT | CRYPT_READ | CRYPT_WRITE |
675 pCryptKey->dwKeyLen = dwKeyLen >> 3;
676 if ((dwFlags & CRYPT_CREATE_SALT) || (dwKeyLen == 40 && !(dwFlags & CRYPT_NO_SALT)))
677 pCryptKey->dwSaltLen = 16 - pCryptKey->dwKeyLen;
679 pCryptKey->dwSaltLen = 0;
680 memset(pCryptKey->abKeyValue, 0, sizeof(pCryptKey->abKeyValue));
681 if (pbKey) memcpy(pCryptKey->abKeyValue, pbKey, RSAENH_MIN(pCryptKey->dwKeyLen,
682 sizeof(pCryptKey->abKeyValue)));
683 memset(pCryptKey->abInitVector, 0, sizeof(pCryptKey->abInitVector));
688 pCryptKey->dwBlockLen = 0;
689 pCryptKey->dwMode = 0;
696 pCryptKey->dwBlockLen = 8;
697 pCryptKey->dwMode = CRYPT_MODE_CBC;
702 pCryptKey->dwBlockLen = dwKeyLen >> 3;
703 pCryptKey->dwMode = 0;
707 new_key_impl(pCryptKey->aiAlgid, &pCryptKey->context, pCryptKey->dwKeyLen);
708 setup_key(pCryptKey);
714 /******************************************************************************
715 * destroy_key_container [Internal]
717 * Destructor for key containers. The user's signature and key exchange private
718 * keys are stored in the registry _IN_PLAINTEXT_.
721 * pObjectHdr [I] Pointer to the key container to be destroyed.
723 static void destroy_key_container(OBJECTHDR *pObjectHdr)
725 KEYCONTAINER *pKeyContainer = (KEYCONTAINER*)pObjectHdr;
727 CHAR szRSABase[MAX_PATH];
732 /* On WinXP, persistent keys are stored in a file located at:
733 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
735 sprintf(szRSABase, "Software\\Wine\\Crypto\\RSA\\%s", pKeyContainer->szName);
737 if (RegCreateKeyExA(HKEY_CURRENT_USER, szRSABase, 0, NULL, REG_OPTION_NON_VOLATILE,
738 KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
740 if (lookup_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
743 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
744 PRIVATEKEYBLOB, 0, 0, &dwLen))
746 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
749 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hKeyExchangeKeyPair, 0,
750 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
752 RegSetValueExA(hKey, "KeyExchangeKeyPair", 0, REG_BINARY, pbKey, dwLen);
754 HeapFree(GetProcessHeap(), 0, pbKey);
757 release_handle(&handle_table, (unsigned int)pKeyContainer->hKeyExchangeKeyPair,
761 if (lookup_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
764 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0, PRIVATEKEYBLOB,
767 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
770 if (RSAENH_CPExportKey(pKey->hProv, pKeyContainer->hSignatureKeyPair, 0,
771 PRIVATEKEYBLOB, 0, pbKey, &dwLen))
773 RegSetValueExA(hKey, "SignatureKeyPair", 0, REG_BINARY, pbKey, dwLen);
775 HeapFree(GetProcessHeap(), 0, pbKey);
778 release_handle(&handle_table, (unsigned int)pKeyContainer->hSignatureKeyPair,
785 HeapFree( GetProcessHeap(), 0, pKeyContainer );
788 /******************************************************************************
789 * new_key_container [Internal]
791 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
792 * of the CSP is determined via the pVTable->pszProvName string.
795 * pszContainerName [I] Name of the key container.
796 * pVTable [I] Callback functions and context info provided by the OS
799 * Success: Handle to the new key container.
800 * Failure: INVALID_HANDLE_VALUE
802 static HCRYPTPROV new_key_container(PCHAR pszContainerName, PVTableProvStruc pVTable)
804 KEYCONTAINER *pKeyContainer;
805 HCRYPTPROV hKeyContainer;
807 hKeyContainer = (HCRYPTPROV)new_object(&handle_table, sizeof(KEYCONTAINER), RSAENH_MAGIC_CONTAINER,
808 destroy_key_container, (OBJECTHDR**)&pKeyContainer);
809 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
811 strncpy(pKeyContainer->szName, pszContainerName, MAX_PATH);
812 pKeyContainer->szName[MAX_PATH-1] = '\0';
813 pKeyContainer->dwMode = 0;
814 pKeyContainer->dwEnumAlgsCtr = 0;
815 pKeyContainer->hKeyExchangeKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
816 pKeyContainer->hSignatureKeyPair = (HCRYPTKEY)INVALID_HANDLE_VALUE;
817 if (pVTable && pVTable->pszProvName) {
818 strncpy(pKeyContainer->szProvName, pVTable->pszProvName, MAX_PATH);
819 pKeyContainer->szProvName[MAX_PATH-1] = '\0';
820 if (!strcmp(pVTable->pszProvName, MS_DEF_PROV_A)) {
821 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_BASE;
822 } else if (!strcmp(pVTable->pszProvName, MS_ENHANCED_PROV_A)) {
823 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_ENHANCED;
825 pKeyContainer->dwPersonality = RSAENH_PERSONALITY_STRONG;
830 return hKeyContainer;
833 /******************************************************************************
834 * read_key_container [Internal]
836 * Tries to read the persistent state of the key container (mainly the signature
837 * and key exchange private keys) given by pszContainerName.
840 * pszContainerName [I] Name of the key container to read from the registry
841 * pVTable [I] Pointer to context data provided by the operating system
844 * Success: Handle to the key container read from the registry
845 * Failure: INVALID_HANDLE_VALUE
847 static HCRYPTPROV read_key_container(PCHAR pszContainerName, PVTableProvStruc pVTable)
849 CHAR szRSABase[MAX_PATH];
852 DWORD dwValueType, dwLen;
853 KEYCONTAINER *pKeyContainer;
854 HCRYPTPROV hKeyContainer;
856 sprintf(szRSABase, "Software\\Wine\\Crypto\\RSA\\%s", pszContainerName);
858 if (RegOpenKeyExA(HKEY_CURRENT_USER, szRSABase, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
860 SetLastError(NTE_BAD_KEYSET);
861 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
864 hKeyContainer = new_key_container(pszContainerName, pVTable);
865 if (hKeyContainer != (HCRYPTPROV)INVALID_HANDLE_VALUE)
867 if (!lookup_handle(&handle_table, hKeyContainer, RSAENH_MAGIC_CONTAINER,
868 (OBJECTHDR**)&pKeyContainer))
869 return (HCRYPTPROV)INVALID_HANDLE_VALUE;
871 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, NULL, &dwLen) ==
874 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
877 if (RegQueryValueExA(hKey, "KeyExchangeKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
880 RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0,
881 &pKeyContainer->hKeyExchangeKeyPair);
883 HeapFree(GetProcessHeap(), 0, pbKey);
887 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, NULL, &dwLen) ==
890 pbKey = (BYTE*)HeapAlloc(GetProcessHeap(), 0, dwLen);
893 if (RegQueryValueExA(hKey, "SignatureKeyPair", 0, &dwValueType, pbKey, &dwLen) ==
896 RSAENH_CPImportKey(hKeyContainer, pbKey, dwLen, 0, 0,
897 &pKeyContainer->hSignatureKeyPair);
899 HeapFree(GetProcessHeap(), 0, pbKey);
904 return hKeyContainer;
907 /******************************************************************************
908 * CPAcquireContext (RSAENH.@)
910 * Acquire a handle to the key container specified by pszContainer
913 * phProv [O] Pointer to the location the acquired handle will be written to.
914 * pszContainer [I] Name of the desired key container. See Notes
915 * dwFlags [I] Flags. See Notes.
916 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
923 * If pszContainer is NULL or points to a zero length string the user's login
924 * name will be used as the key container name.
926 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
927 * If a keyset with the given name already exists, the function fails and sets
928 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
929 * key container does not exist, function fails and sets last error to
932 BOOL WINAPI RSAENH_CPAcquireContext(HCRYPTPROV *phProv, LPSTR pszContainer,
933 DWORD dwFlags, PVTableProvStruc pVTable)
936 CHAR szKeyContainerName[MAX_PATH] = "";
938 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv,
939 debugstr_a(pszContainer), dwFlags, pVTable);
941 SetLastError(ERROR_SUCCESS);
943 if (!load_lib()) return FALSE;
945 if (pszContainer ? strlen(pszContainer) : 0)
947 strncpy(szKeyContainerName, pszContainer, MAX_PATH);
948 szKeyContainerName[MAX_PATH-1] = '\0';
953 if (!GetUserNameA(szKeyContainerName, &dwLen)) return FALSE;
959 *phProv = read_key_container(szKeyContainerName, pVTable);
962 case CRYPT_NEWKEYSET:
963 *phProv = read_key_container(szKeyContainerName, pVTable);
964 if (*phProv != (HCRYPTPROV)INVALID_HANDLE_VALUE)
966 release_handle(&handle_table, (unsigned int)*phProv, RSAENH_MAGIC_CONTAINER);
967 SetLastError(NTE_EXISTS);
970 *phProv = new_key_container(szKeyContainerName, pVTable);
974 *phProv = (unsigned int)INVALID_HANDLE_VALUE;
975 SetLastError(NTE_BAD_FLAGS);
979 return *phProv != (unsigned int)INVALID_HANDLE_VALUE;
982 /******************************************************************************
983 * CPCreateHash (RSAENH.@)
985 * CPCreateHash creates and initalizes a new hash object.
988 * hProv [I] Handle to the key container to which the new hash will belong.
989 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
990 * hKey [I] Handle to a session key applied for keyed hashes.
991 * dwFlags [I] Currently no flags defined. Must be zero.
992 * phHash [O] Points to the location where a handle to the new hash will be stored.
999 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1000 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1002 BOOL WINAPI RSAENH_CPCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags,
1005 KEYCONTAINER *pKeyContainer;
1006 CRYPTHASH *pCryptHash;
1007 const PROV_ENUMALGS_EX *peaAlgidInfo;
1009 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08lx, phHash=%p)\n", hProv, Algid, hKey,
1012 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
1014 SetLastError(NTE_BAD_UID);
1018 peaAlgidInfo = get_algid_info(pKeyContainer, Algid);
1021 SetLastError(NTE_BAD_ALGID);
1027 SetLastError(NTE_BAD_FLAGS);
1031 if ((Algid == CALG_MAC || Algid == CALG_HMAC)) {
1032 CRYPTKEY *pCryptKey;
1034 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1035 SetLastError(NTE_BAD_KEY);
1039 if ((Algid == CALG_MAC) && (GET_ALG_TYPE(pCryptKey->aiAlgid) != ALG_TYPE_BLOCK)) {
1040 SetLastError(NTE_BAD_KEY);
1045 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1046 destroy_hash, (OBJECTHDR**)&pCryptHash);
1047 if (!pCryptHash) return FALSE;
1049 pCryptHash->aiAlgid = Algid;
1050 pCryptHash->hKey = hKey;
1051 pCryptHash->hProv = hProv;
1052 pCryptHash->dwState = RSAENH_HASHSTATE_IDLE;
1053 pCryptHash->pHMACInfo = (PHMAC_INFO)NULL;
1054 pCryptHash->dwHashSize = peaAlgidInfo->dwDefaultLen >> 3;
1056 return init_hash(pKeyContainer, pCryptHash);
1059 /******************************************************************************
1060 * CPDestroyHash (RSAENH.@)
1062 * Releases the handle to a hash object. The object is destroyed if it's reference
1063 * count reaches zero.
1066 * hProv [I] Handle to the key container to which the hash object belongs.
1067 * hHash [I] Handle to the hash object to be released.
1073 BOOL WINAPI RSAENH_CPDestroyHash(HCRYPTPROV hProv, HCRYPTHASH hHash)
1075 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv, hHash);
1077 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1079 SetLastError(NTE_BAD_UID);
1083 if (!release_handle(&handle_table, hHash, RSAENH_MAGIC_HASH))
1085 SetLastError(NTE_BAD_HASH);
1092 /******************************************************************************
1093 * CPDestroyKey (RSAENH.@)
1095 * Releases the handle to a key object. The object is destroyed if it's reference
1096 * count reaches zero.
1099 * hProv [I] Handle to the key container to which the key object belongs.
1100 * hKey [I] Handle to the key object to be released.
1106 BOOL WINAPI RSAENH_CPDestroyKey(HCRYPTPROV hProv, HCRYPTKEY hKey)
1108 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv, hKey);
1110 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1112 SetLastError(NTE_BAD_UID);
1116 if (!release_handle(&handle_table, hKey, RSAENH_MAGIC_KEY))
1118 SetLastError(NTE_BAD_KEY);
1125 /******************************************************************************
1126 * CPDuplicateHash (RSAENH.@)
1128 * Clones a hash object including it's current state.
1131 * hUID [I] Handle to the key container the hash belongs to.
1132 * hHash [I] Handle to the hash object to be cloned.
1133 * pdwReserved [I] Reserved. Must be NULL.
1134 * dwFlags [I] No flags are currently defined. Must be 0.
1135 * phHash [O] Handle to the cloned hash object.
1141 BOOL WINAPI RSAENH_CPDuplicateHash(HCRYPTPROV hUID, HCRYPTHASH hHash, DWORD *pdwReserved,
1142 DWORD dwFlags, HCRYPTHASH *phHash)
1144 CRYPTHASH *pSrcHash, *pDestHash;
1146 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID, hHash,
1147 pdwReserved, dwFlags, phHash);
1149 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1151 SetLastError(NTE_BAD_UID);
1155 if (!lookup_handle(&handle_table, hHash, RSAENH_MAGIC_HASH, (OBJECTHDR**)&pSrcHash))
1157 SetLastError(NTE_BAD_HASH);
1161 if (!phHash || pdwReserved || dwFlags)
1163 SetLastError(ERROR_INVALID_PARAMETER);
1167 *phHash = (HCRYPTHASH)new_object(&handle_table, sizeof(CRYPTHASH), RSAENH_MAGIC_HASH,
1168 destroy_hash, (OBJECTHDR**)&pDestHash);
1169 if (*phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE)
1171 memcpy(pDestHash, pSrcHash, sizeof(CRYPTHASH));
1172 duplicate_hash_impl(pSrcHash->aiAlgid, &pSrcHash->context, &pDestHash->context);
1173 copy_hmac_info(&pDestHash->pHMACInfo, pSrcHash->pHMACInfo);
1176 return *phHash != (HCRYPTHASH)INVALID_HANDLE_VALUE;
1179 /******************************************************************************
1180 * CPDuplicateKey (RSAENH.@)
1182 * Clones a key object including it's current state.
1185 * hUID [I] Handle to the key container the hash belongs to.
1186 * hKey [I] Handle to the key object to be cloned.
1187 * pdwReserved [I] Reserved. Must be NULL.
1188 * dwFlags [I] No flags are currently defined. Must be 0.
1189 * phHash [O] Handle to the cloned key object.
1195 BOOL WINAPI RSAENH_CPDuplicateKey(HCRYPTPROV hUID, HCRYPTKEY hKey, DWORD *pdwReserved,
1196 DWORD dwFlags, HCRYPTKEY *phKey)
1198 CRYPTKEY *pSrcKey, *pDestKey;
1200 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID, hKey,
1201 pdwReserved, dwFlags, phKey);
1203 if (!is_valid_handle(&handle_table, hUID, RSAENH_MAGIC_CONTAINER))
1205 SetLastError(NTE_BAD_UID);
1209 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pSrcKey))
1211 SetLastError(NTE_BAD_KEY);
1215 if (!phKey || pdwReserved || dwFlags)
1217 SetLastError(ERROR_INVALID_PARAMETER);
1221 *phKey = (HCRYPTKEY)new_object(&handle_table, sizeof(CRYPTKEY), RSAENH_MAGIC_KEY, destroy_key,
1222 (OBJECTHDR**)&pDestKey);
1223 if (*phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE)
1225 memcpy(pDestKey, pSrcKey, sizeof(CRYPTKEY));
1226 duplicate_key_impl(pSrcKey->aiAlgid, &pSrcKey->context, &pDestKey->context);
1235 /******************************************************************************
1236 * CPEncrypt (RSAENH.@)
1241 * hProv [I] The key container hKey and hHash belong to.
1242 * hKey [I] The key used to encrypt the data.
1243 * hHash [I] An optional hash object for parallel hashing. See notes.
1244 * Final [I] Indicates if this is the last block of data to encrypt.
1245 * dwFlags [I] Currently no flags defined. Must be zero.
1246 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
1247 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
1248 * dwBufLen [I] Size of the buffer at pbData.
1255 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1256 * This is useful for message signatures.
1258 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1261 * Parallel hashing not yet implemented.
1263 BOOL WINAPI RSAENH_CPEncrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1264 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen)
1266 CRYPTKEY *pCryptKey;
1267 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1268 DWORD dwEncryptedLen, i, j, k;
1270 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1271 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen,
1274 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1276 SetLastError(NTE_BAD_UID);
1282 SetLastError(NTE_BAD_FLAGS);
1286 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1288 SetLastError(NTE_BAD_KEY);
1292 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1293 pCryptKey->dwState = RSAENH_KEYSTATE_ENCRYPTING;
1295 if (pCryptKey->dwState != RSAENH_KEYSTATE_ENCRYPTING)
1297 SetLastError(NTE_BAD_DATA);
1301 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1302 if (!Final && (*pdwDataLen % pCryptKey->dwBlockLen)) {
1303 SetLastError(NTE_BAD_DATA);
1307 dwEncryptedLen = (*pdwDataLen/pCryptKey->dwBlockLen+(Final?1:0))*pCryptKey->dwBlockLen;
1308 for (i=*pdwDataLen; i<dwEncryptedLen; i++) pbData[i] = dwEncryptedLen - *pdwDataLen;
1309 *pdwDataLen = dwEncryptedLen;
1311 if (*pdwDataLen > dwBufLen)
1313 SetLastError(ERROR_MORE_DATA);
1317 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1318 switch (pCryptKey->dwMode) {
1319 case CRYPT_MODE_ECB:
1320 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1324 case CRYPT_MODE_CBC:
1325 for (j=0; j<pCryptKey->dwBlockLen; j++) in[j] ^= pCryptKey->abChainVector[j];
1326 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1328 memcpy(pCryptKey->abChainVector, out, pCryptKey->dwBlockLen);
1331 case CRYPT_MODE_CFB:
1332 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1333 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1334 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1335 out[j] = in[j] ^ o[0];
1336 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1337 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1338 pCryptKey->abChainVector[k] = out[j];
1343 SetLastError(NTE_BAD_ALGID);
1346 memcpy(in, out, pCryptKey->dwBlockLen);
1348 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1349 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1352 if (Final) setup_key(pCryptKey);
1357 /******************************************************************************
1358 * CPDecrypt (RSAENH.@)
1363 * hProv [I] The key container hKey and hHash belong to.
1364 * hKey [I] The key used to decrypt the data.
1365 * hHash [I] An optional hash object for parallel hashing. See notes.
1366 * Final [I] Indicates if this is the last block of data to decrypt.
1367 * dwFlags [I] Currently no flags defined. Must be zero.
1368 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
1369 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
1376 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
1377 * This is useful for message signatures.
1379 * This function uses the standard WINAPI protocol for querying data of dynamic length.
1382 * Parallel hashing not yet implemented.
1384 BOOL WINAPI RSAENH_CPDecrypt(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
1385 DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1387 CRYPTKEY *pCryptKey;
1388 BYTE *in, out[RSAENH_MAX_BLOCK_SIZE], o[RSAENH_MAX_BLOCK_SIZE];
1391 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08lx, pbData=%p, "
1392 "pdwDataLen=%p)\n", hProv, hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
1394 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1396 SetLastError(NTE_BAD_UID);
1402 SetLastError(NTE_BAD_FLAGS);
1406 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1408 SetLastError(NTE_BAD_KEY);
1412 if (pCryptKey->dwState == RSAENH_KEYSTATE_IDLE)
1413 pCryptKey->dwState = RSAENH_KEYSTATE_DECRYPTING;
1415 if (pCryptKey->dwState != RSAENH_KEYSTATE_DECRYPTING)
1417 SetLastError(NTE_BAD_DATA);
1421 if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_BLOCK) {
1422 for (i=0, in=pbData; i<*pdwDataLen; i+=pCryptKey->dwBlockLen, in+=pCryptKey->dwBlockLen) {
1423 switch (pCryptKey->dwMode) {
1424 case CRYPT_MODE_ECB:
1425 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1429 case CRYPT_MODE_CBC:
1430 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context, in, out,
1432 for (j=0; j<pCryptKey->dwBlockLen; j++) out[j] ^= pCryptKey->abChainVector[j];
1433 memcpy(pCryptKey->abChainVector, in, pCryptKey->dwBlockLen);
1436 case CRYPT_MODE_CFB:
1437 for (j=0; j<pCryptKey->dwBlockLen; j++) {
1438 encrypt_block_impl(pCryptKey->aiAlgid, &pCryptKey->context,
1439 pCryptKey->abChainVector, o, RSAENH_ENCRYPT);
1440 out[j] = in[j] ^ o[0];
1441 for (k=0; k<pCryptKey->dwBlockLen-1; k++)
1442 pCryptKey->abChainVector[k] = pCryptKey->abChainVector[k+1];
1443 pCryptKey->abChainVector[k] = in[j];
1448 SetLastError(NTE_BAD_ALGID);
1451 memcpy(in, out, pCryptKey->dwBlockLen);
1453 if (Final) *pdwDataLen -= pbData[*pdwDataLen-1];
1455 } else if (GET_ALG_TYPE(pCryptKey->aiAlgid) == ALG_TYPE_STREAM) {
1456 encrypt_stream_impl(pCryptKey->aiAlgid, &pCryptKey->context, pbData, *pdwDataLen);
1459 if (Final) setup_key(pCryptKey);
1464 /******************************************************************************
1465 * CPExportKey (RSAENH.@)
1467 * Export a key into a binary large object (BLOB).
1470 * hProv [I] Key container from which a key is to be exported.
1471 * hKey [I] Key to be exported.
1472 * hPubKey [I] Key used to encrypt sensitive BLOB data.
1473 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
1474 * dwFlags [I] Currently none defined.
1475 * pbData [O] Pointer to a buffer where the BLOB will be written to.
1476 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
1482 BOOL WINAPI RSAENH_CPExportKey(HCRYPTPROV hProv, HCRYPTKEY hKey, HCRYPTKEY hPubKey,
1483 DWORD dwBlobType, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen)
1485 CRYPTKEY *pCryptKey, *pPubKey;
1486 BLOBHEADER *pBlobHeader = (BLOBHEADER*)pbData;
1487 RSAPUBKEY *pRSAPubKey = (RSAPUBKEY*)(pBlobHeader+1);
1488 ALG_ID *pAlgid = (ALG_ID*)(pBlobHeader+1);
1492 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
1493 "pdwDataLen=%p)\n", hProv, hKey, hPubKey, dwBlobType, dwFlags, pbData, pdwDataLen);
1495 if (!is_valid_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER))
1497 SetLastError(NTE_BAD_UID);
1501 if (!lookup_handle(&handle_table, hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1503 SetLastError(NTE_BAD_KEY);
1507 switch ((BYTE)dwBlobType)
1510 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey)){
1511 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error_code? */
1515 if (GET_ALG_CLASS(pCryptKey->aiAlgid) != ALG_CLASS_DATA_ENCRYPT) {
1516 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
1520 dwDataLen = sizeof(BLOBHEADER) + sizeof(ALG_ID) + pPubKey->dwBlockLen;
1522 if (*pdwDataLen < dwDataLen) {
1523 SetLastError(ERROR_MORE_DATA);
1524 *pdwDataLen = dwDataLen;
1528 pBlobHeader->bType = SIMPLEBLOB;
1529 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1530 pBlobHeader->reserved = 0;
1531 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1533 *pAlgid = pPubKey->aiAlgid;
1535 pbRawData = (BYTE*)(pAlgid+1);
1536 pbRawData[0] = 0x00;
1537 pbRawData[1] = RSAENH_PKC_BLOCKTYPE;
1538 for (i=2; i < pPubKey->dwBlockLen - pCryptKey->dwKeyLen - 1; i++)
1539 do gen_rand_impl(&pbRawData[i], 1); while (!pbRawData[i]);
1540 pbRawData[i] = 0x00;
1541 for (i=0; i<pCryptKey->dwKeyLen; i++)
1542 pbRawData[pPubKey->dwBlockLen - pCryptKey->dwKeyLen + i] =
1543 pCryptKey->abKeyValue[i];
1545 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbRawData, pbRawData,
1548 *pdwDataLen = dwDataLen;
1552 if (is_valid_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY)) {
1553 SetLastError(NTE_BAD_KEY); /* FIXME: error code? */
1557 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
1558 SetLastError(NTE_BAD_KEY);
1562 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + pCryptKey->dwKeyLen;
1564 if (*pdwDataLen < dwDataLen) {
1565 SetLastError(ERROR_MORE_DATA);
1566 *pdwDataLen = dwDataLen;
1570 pBlobHeader->bType = PUBLICKEYBLOB;
1571 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1572 pBlobHeader->reserved = 0;
1573 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1575 pRSAPubKey->magic = RSAENH_MAGIC_RSA1;
1576 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
1578 export_public_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1579 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
1581 *pdwDataLen = dwDataLen;
1584 case PRIVATEKEYBLOB:
1585 if ((pCryptKey->aiAlgid != CALG_RSA_KEYX) && (pCryptKey->aiAlgid != CALG_RSA_SIGN)) {
1586 SetLastError(NTE_BAD_KEY);
1590 dwDataLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
1591 2 * pCryptKey->dwKeyLen + 5 * ((pCryptKey->dwKeyLen + 1) >> 1);
1593 if (*pdwDataLen < dwDataLen) {
1594 SetLastError(ERROR_MORE_DATA);
1595 *pdwDataLen = dwDataLen;
1599 pBlobHeader->bType = PRIVATEKEYBLOB;
1600 pBlobHeader->bVersion = CUR_BLOB_VERSION;
1601 pBlobHeader->reserved = 0;
1602 pBlobHeader->aiKeyAlg = pCryptKey->aiAlgid;
1604 pRSAPubKey->magic = RSAENH_MAGIC_RSA2;
1605 pRSAPubKey->bitlen = pCryptKey->dwKeyLen << 3;
1607 export_private_key_impl((BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1608 pCryptKey->dwKeyLen, &pRSAPubKey->pubexp);
1610 *pdwDataLen = dwDataLen;
1614 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
1619 /******************************************************************************
1620 * CPImportKey (RSAENH.@)
1622 * Import a BLOB'ed key into a key container.
1625 * hProv [I] Key container into which the key is to be imported.
1626 * pbData [I] Pointer to a buffer which holds the BLOB.
1627 * dwDataLen [I] Length of data in buffer at pbData.
1628 * hPubKey [I] Key used to decrypt sensitive BLOB data.
1629 * dwFlags [I] Currently none defined.
1630 * phKey [O] Handle to the imported key.
1636 BOOL WINAPI RSAENH_CPImportKey(HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLen,
1637 HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY *phKey)
1639 KEYCONTAINER *pKeyContainer;
1640 CRYPTKEY *pCryptKey, *pPubKey;
1641 CONST BLOBHEADER *pBlobHeader = (CONST BLOBHEADER*)pbData;
1642 CONST RSAPUBKEY *pRSAPubKey = (CONST RSAPUBKEY*)(pBlobHeader+1);
1643 CONST ALG_ID *pAlgid = (CONST ALG_ID*)(pBlobHeader+1);
1644 CONST BYTE *pbKeyStream = (CONST BYTE*)(pAlgid + 1);
1645 BYTE *pbDecrypted, abKeyValue[RSAENH_MAX_KEY_SIZE];
1648 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%ld, hPubKey=%08lx, dwFlags=%08lx, phKey=%p)\n",
1649 hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
1651 if (!lookup_handle(&handle_table, hProv, RSAENH_MAGIC_CONTAINER, (OBJECTHDR**)&pKeyContainer))
1653 SetLastError(NTE_BAD_UID);
1657 if (dwDataLen < sizeof(BLOBHEADER) ||
1658 pBlobHeader->bVersion != CUR_BLOB_VERSION ||
1659 pBlobHeader->reserved != 0)
1661 SetLastError(NTE_BAD_DATA);
1665 switch (pBlobHeader->bType)
1667 case PRIVATEKEYBLOB:
1668 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
1669 (pRSAPubKey->magic != RSAENH_MAGIC_RSA2) ||
1670 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) +
1671 (2 * pRSAPubKey->bitlen >> 3) + (5 * ((pRSAPubKey->bitlen+8)>>4))))
1673 SetLastError(NTE_BAD_DATA);
1677 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), NULL,
1679 if (!lookup_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1680 SetLastError(NTE_FAIL);
1684 return import_private_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1685 pRSAPubKey->bitlen/8, pRSAPubKey->pubexp);
1688 if ((dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)) ||
1689 (pRSAPubKey->magic != RSAENH_MAGIC_RSA1) ||
1690 (dwDataLen < sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + (pRSAPubKey->bitlen >> 3)))
1692 SetLastError(NTE_BAD_DATA);
1696 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, MAKELONG(0,pRSAPubKey->bitlen), NULL,
1698 if (!lookup_handle(&handle_table, *phKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey)) {
1699 SetLastError(NTE_FAIL);
1703 return import_public_key_impl((CONST BYTE*)(pRSAPubKey+1), &pCryptKey->context,
1704 pRSAPubKey->bitlen >> 3, pRSAPubKey->pubexp);
1707 if (!lookup_handle(&handle_table, hPubKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pPubKey) ||
1708 pPubKey->aiAlgid != CALG_RSA_KEYX)
1710 SetLastError(NTE_BAD_PUBLIC_KEY); /* FIXME: error code? */
1714 if (dwDataLen < sizeof(BLOBHEADER)+sizeof(ALG_ID)+pPubKey->dwBlockLen)
1716 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
1720 pbDecrypted = (BYTE*)HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
1721 if (!pbDecrypted) return FALSE;
1722 encrypt_block_impl(pPubKey->aiAlgid, &pPubKey->context, pbKeyStream, pbDecrypted,
1725 for (i=2; i<pPubKey->dwBlockLen && pbDecrypted[i]; i++);
1726 if ((i==pPubKey->dwBlockLen) ||
1727 (pbDecrypted[0] != 0x00) ||
1728 (pbDecrypted[1] != RSAENH_PKC_BLOCKTYPE))
1730 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1731 SetLastError(NTE_BAD_DATA); /* FIXME: error code */
1735 dwKeyLen = pPubKey->dwBlockLen-i-1;
1736 memcpy(abKeyValue, pbDecrypted+i+1, dwKeyLen);
1737 HeapFree(GetProcessHeap(), 0, pbDecrypted);
1739 *phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, abKeyValue, 0);
1740 return *phKey != (HCRYPTKEY)INVALID_HANDLE_VALUE;
1743 SetLastError(NTE_BAD_TYPE); /* FIXME: error code? */
1748 /******************************************************************************
1749 * CPGenKey (RSAENH.@)
1751 * Generate a key in the key container
1754 * hProv [I] Key container for which a key is to be generated.
1755 * Algid [I] Crypto algorithm identifier for the key to be generated.
1756 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
1757 * phKey [O] Handle to the generated key.
1764 * Flags currently not considered.
1767 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
1768 * and AT_SIGNATURE values.
1770 BOOL WINAPI RSAENH_CPGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey)
1772 KEYCONTAINER *pKeyContainer;
1773 BYTE abKeyValue[2048];
1775 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv, Algid, dwFlags, phKey);
1777 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1778 (OBJECTHDR**)&pKeyContainer))
1780 /* MSDN: hProv not containing valid context handle */
1781 SetLastError(NTE_BAD_UID);
1788 RSAENH_CPDestroyKey(hProv, pKeyContainer->hSignatureKeyPair);
1789 pKeyContainer->hSignatureKeyPair =
1790 new_key(hProv, CALG_RSA_SIGN, dwFlags, abKeyValue, 0);
1791 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
1792 (unsigned int*)phKey);
1795 case AT_KEYEXCHANGE:
1796 RSAENH_CPDestroyKey(hProv, pKeyContainer->hKeyExchangeKeyPair);
1797 pKeyContainer->hKeyExchangeKeyPair = new_key(hProv, CALG_RSA_KEYX, dwFlags, abKeyValue, 0);
1798 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
1799 (unsigned int*)phKey);
1805 gen_rand_impl(abKeyValue, 2048);
1806 *phKey = new_key(hProv, Algid, dwFlags, abKeyValue, 0);
1810 /* MSDN: Algorithm not supported specified by Algid */
1811 SetLastError(NTE_BAD_ALGID);
1815 return *phKey != (unsigned int)INVALID_HANDLE_VALUE;
1818 /******************************************************************************
1819 * CPGenRandom (RSAENH.@)
1821 * Generate a random byte stream.
1824 * hProv [I] Key container that is used to generate random bytes.
1825 * dwLen [I] Specifies the number of requested random data bytes.
1826 * pbBuffer [O] Random bytes will be stored here.
1832 BOOL WINAPI RSAENH_CPGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
1834 KEYCONTAINER *pKeyContainer;
1836 TRACE("(hProv=%08lx, dwLen=%ld, pbBuffer=%p)\n", hProv, dwLen, pbBuffer);
1838 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1839 (OBJECTHDR**)&pKeyContainer))
1841 /* MSDN: hProv not containing valid context handle */
1842 SetLastError(NTE_BAD_UID);
1846 return gen_rand_impl(pbBuffer, dwLen);
1849 /******************************************************************************
1850 * CPGetHashParam (RSAENH.@)
1852 * Query parameters of an hash object.
1855 * hProv [I] The kea container, which the hash belongs to.
1856 * hHash [I] The hash object that is to be queried.
1857 * dwParam [I] Specifies the parameter that is to be queried.
1858 * pbData [I] Pointer to the buffer where the parameter value will be stored.
1859 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
1860 * dwFlags [I] None currently defined.
1867 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
1868 * finalized if HP_HASHVALUE is queried.
1870 BOOL WINAPI RSAENH_CPGetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
1871 DWORD *pdwDataLen, DWORD dwFlags)
1873 CRYPTHASH *pCryptHash;
1874 KEYCONTAINER *pKeyContainer;
1875 BYTE abHashValue[RSAENH_MAX_HASH_SIZE];
1877 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
1878 hProv, hHash, dwParam, pbData, pdwDataLen, dwFlags);
1880 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
1881 (OBJECTHDR**)&pKeyContainer))
1883 SetLastError(NTE_BAD_UID);
1889 SetLastError(NTE_BAD_FLAGS);
1893 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
1894 (OBJECTHDR**)&pCryptHash))
1896 SetLastError(NTE_BAD_HASH);
1902 SetLastError(ERROR_INVALID_PARAMETER);
1909 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->aiAlgid,
1913 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptHash->dwHashSize,
1917 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE) {
1918 SetLastError(NTE_BAD_HASH_STATE);
1922 if (pbData && (pCryptHash->dwState != RSAENH_HASHSTATE_FINISHED))
1924 finalize_hash(pCryptHash);
1925 if (pCryptHash->aiAlgid == CALG_HMAC) {
1926 memcpy(abHashValue, pCryptHash->abHashValue, pCryptHash->dwHashSize);
1927 init_hash(pKeyContainer, pCryptHash);
1928 update_hash(pCryptHash, pCryptHash->pHMACInfo->pbOuterString,
1929 pCryptHash->pHMACInfo->cbOuterString);
1930 update_hash(pCryptHash, abHashValue, pCryptHash->dwHashSize);
1931 finalize_hash(pCryptHash);
1934 pCryptHash->dwState = RSAENH_HASHSTATE_FINISHED;
1937 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptHash->abHashValue,
1938 pCryptHash->dwHashSize);
1941 SetLastError(NTE_BAD_TYPE);
1946 /******************************************************************************
1947 * CPSetKeyParam (RSAENH.@)
1949 * Set a parameter of a key object
1952 * hProv [I] The key container to which the key belongs.
1953 * hKey [I] The key for which a parameter is to be set.
1954 * dwParam [I] Parameter type. See Notes.
1955 * pbData [I] Pointer to the parameter value.
1956 * dwFlags [I] Currently none defined.
1963 * Defined dwParam types are:
1964 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
1965 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
1966 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
1967 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
1968 * - KP_IV: Initialization vector
1970 BOOL WINAPI RSAENH_CPSetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
1973 CRYPTKEY *pCryptKey;
1975 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv, hKey,
1976 dwParam, pbData, dwFlags);
1978 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
1980 SetLastError(NTE_BAD_UID);
1985 SetLastError(NTE_BAD_FLAGS);
1989 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
1991 SetLastError(NTE_BAD_KEY);
1997 pCryptKey->dwMode = *(DWORD*)pbData;
2001 pCryptKey->dwModeBits = *(DWORD*)pbData;
2004 case KP_PERMISSIONS:
2005 pCryptKey->dwPermissions = *(DWORD*)pbData;
2009 memcpy(pCryptKey->abInitVector, pbData, pCryptKey->dwBlockLen);
2013 SetLastError(NTE_BAD_TYPE);
2018 /******************************************************************************
2019 * CPGetKeyParam (RSAENH.@)
2021 * Query a key parameter.
2024 * hProv [I] The key container, which the key belongs to.
2025 * hHash [I] The key object that is to be queried.
2026 * dwParam [I] Specifies the parameter that is to be queried.
2027 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2028 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2029 * dwFlags [I] None currently defined.
2036 * Defined dwParam types are:
2037 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
2038 * - KP_MODE_BITS: Shift width for cipher feedback mode.
2039 * (Currently ignored by MS CSP's - always eight)
2040 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
2041 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
2042 * - KP_IV: Initialization vector.
2043 * - KP_KEYLEN: Bitwidth of the key.
2044 * - KP_BLOCKLEN: Size of a block cipher block.
2045 * - KP_SALT: Salt value.
2047 BOOL WINAPI RSAENH_CPGetKeyParam(HCRYPTPROV hProv, HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
2048 DWORD *pdwDataLen, DWORD dwFlags)
2050 CRYPTKEY *pCryptKey;
2053 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
2054 hProv, hKey, dwParam, pbData, pdwDataLen, dwFlags);
2056 if (!is_valid_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2058 SetLastError(NTE_BAD_UID);
2063 SetLastError(NTE_BAD_FLAGS);
2067 if (!lookup_handle(&handle_table, (unsigned int)hKey, RSAENH_MAGIC_KEY, (OBJECTHDR**)&pCryptKey))
2069 SetLastError(NTE_BAD_KEY);
2076 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pCryptKey->abInitVector,
2077 pCryptKey->dwBlockLen);
2080 return copy_param(pbData, pdwDataLen,
2081 (CONST BYTE*)&pCryptKey->abKeyValue[pCryptKey->dwKeyLen], pCryptKey->dwSaltLen);
2084 dwBitLen = pCryptKey->dwKeyLen << 3;
2085 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2088 dwBitLen = pCryptKey->dwBlockLen << 3;
2089 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwBitLen, sizeof(DWORD));
2092 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwMode, sizeof(DWORD));
2095 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwModeBits,
2098 case KP_PERMISSIONS:
2099 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&pCryptKey->dwPermissions,
2103 SetLastError(NTE_BAD_TYPE);
2108 /******************************************************************************
2109 * CPGetProvParam (RSAENH.@)
2111 * Query a CSP parameter.
2114 * hProv [I] The key container that is to be queried.
2115 * dwParam [I] Specifies the parameter that is to be queried.
2116 * pbData [I] Pointer to the buffer where the parameter value will be stored.
2117 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
2118 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
2124 * Defined dwParam types:
2125 * - PP_CONTAINER: Name of the key container.
2126 * - PP_NAME: Name of the cryptographic service provider.
2127 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
2128 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
2129 * - PP_ENUMALGS{_EX}: Query provider capabilities.
2131 BOOL WINAPI RSAENH_CPGetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData,
2132 DWORD *pdwDataLen, DWORD dwFlags)
2134 KEYCONTAINER *pKeyContainer;
2135 PROV_ENUMALGS provEnumalgs;
2138 TRACE("(hProv=%08lx, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
2139 hProv, dwParam, pbData, pdwDataLen, dwFlags);
2141 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2142 (OBJECTHDR**)&pKeyContainer))
2144 /* MSDN: hProv not containing valid context handle */
2145 SetLastError(NTE_BAD_UID);
2152 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szName,
2153 strlen(pKeyContainer->szName)+1);
2156 return copy_param(pbData, pdwDataLen, (CONST BYTE*)pKeyContainer->szProvName,
2157 strlen(pKeyContainer->szProvName)+1);
2159 case PP_SIG_KEYSIZE_INC:
2160 case PP_KEYX_KEYSIZE_INC:
2162 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&dwTemp, sizeof(dwTemp));
2165 case PP_ENUMALGS_EX:
2166 if (((pKeyContainer->dwEnumAlgsCtr >= RSAENH_MAX_ENUMALGS-1) ||
2167 (!aProvEnumAlgsEx[pKeyContainer->dwPersonality]
2168 [pKeyContainer->dwEnumAlgsCtr+1].aiAlgid)) &&
2169 ((dwFlags & CRYPT_FIRST) != CRYPT_FIRST))
2171 SetLastError(ERROR_NO_MORE_ITEMS);
2175 if (dwParam == PP_ENUMALGS) {
2176 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS)))
2177 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2178 0 : pKeyContainer->dwEnumAlgsCtr+1;
2180 provEnumalgs.aiAlgid = aProvEnumAlgsEx
2181 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].aiAlgid;
2182 provEnumalgs.dwBitLen = aProvEnumAlgsEx
2183 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwDefaultLen;
2184 provEnumalgs.dwNameLen = aProvEnumAlgsEx
2185 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].dwNameLen;
2186 memcpy(provEnumalgs.szName, aProvEnumAlgsEx
2187 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr].szName,
2190 return copy_param(pbData, pdwDataLen, (CONST BYTE*)&provEnumalgs,
2191 sizeof(PROV_ENUMALGS));
2193 if (pbData && (*pdwDataLen >= sizeof(PROV_ENUMALGS_EX)))
2194 pKeyContainer->dwEnumAlgsCtr = ((dwFlags & CRYPT_FIRST) == CRYPT_FIRST) ?
2195 0 : pKeyContainer->dwEnumAlgsCtr+1;
2197 return copy_param(pbData, pdwDataLen,
2198 (CONST BYTE*)&aProvEnumAlgsEx
2199 [pKeyContainer->dwPersonality][pKeyContainer->dwEnumAlgsCtr],
2200 sizeof(PROV_ENUMALGS_EX));
2204 /* MSDN: Unknown parameter number in dwParam */
2205 SetLastError(NTE_BAD_TYPE);
2211 /******************************************************************************
2212 * CPDeriveKey (RSAENH.@)
2214 * Derives a key from a hash value.
2217 * hProv [I] Key container for which a key is to be generated.
2218 * Algid [I] Crypto algorithm identifier for the key to be generated.
2219 * hBaseData [I] Hash from whose value the key will be derived.
2220 * dwFlags [I] See Notes.
2221 * phKey [O] The generated key.
2229 * - CRYPT_EXPORTABLE: Key can be exported.
2230 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
2231 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
2233 BOOL WINAPI RSAENH_CPDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
2234 DWORD dwFlags, HCRYPTKEY *phKey)
2236 KEYCONTAINER *pKeyContainer;
2238 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08lx phKey=%p)\n", hProv, Algid,
2239 hBaseData, dwFlags, phKey);
2241 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2242 (OBJECTHDR**)&pKeyContainer))
2244 SetLastError(NTE_BAD_UID);
2248 if (!is_valid_handle(&handle_table, (unsigned int)hBaseData, RSAENH_MAGIC_HASH))
2250 SetLastError(NTE_BAD_HASH);
2256 SetLastError(ERROR_INVALID_PARAMETER);
2260 *phKey = new_key(hProv, Algid, dwFlags, NULL, hBaseData);
2261 return *phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE ? FALSE : TRUE;
2264 /******************************************************************************
2265 * CPGetUserKey (RSAENH.@)
2267 * Returns a handle to the user's private key-exchange- or signature-key.
2270 * hProv [I] The key container from which a user key is requested.
2271 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
2272 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
2279 * A newly created key container does not contain private user key. Create them with CPGenKey.
2281 BOOL WINAPI RSAENH_CPGetUserKey(HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUserKey)
2283 KEYCONTAINER *pKeyContainer;
2285 TRACE("(hProv=%08lx, dwKeySpec=%08lx, phUserKey=%p)\n", hProv, dwKeySpec, phUserKey);
2287 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2288 (OBJECTHDR**)&pKeyContainer))
2290 /* MSDN: hProv not containing valid context handle */
2291 SetLastError(NTE_BAD_UID);
2297 case AT_KEYEXCHANGE:
2298 copy_handle(&handle_table, pKeyContainer->hKeyExchangeKeyPair, RSAENH_MAGIC_KEY,
2299 (unsigned int*)phUserKey);
2303 copy_handle(&handle_table, pKeyContainer->hSignatureKeyPair, RSAENH_MAGIC_KEY,
2304 (unsigned int*)phUserKey);
2308 *phUserKey = (HCRYPTKEY)INVALID_HANDLE_VALUE;
2311 if (*phUserKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
2313 /* MSDN: dwKeySpec parameter specifies non existent key */
2314 SetLastError(NTE_NO_KEY);
2321 /******************************************************************************
2322 * CPHashData (RSAENH.@)
2324 * Updates a hash object with the given data.
2327 * hProv [I] Key container to which the hash object belongs.
2328 * hHash [I] Hash object which is to be updated.
2329 * pbData [I] Pointer to data with which the hash object is to be updated.
2330 * dwDataLen [I] Length of the data.
2331 * dwFlags [I] Currently none defined.
2338 * The actual hash value is queried with CPGetHashParam, which will finalize
2339 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
2341 BOOL WINAPI RSAENH_CPHashData(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbData,
2342 DWORD dwDataLen, DWORD dwFlags)
2344 CRYPTHASH *pCryptHash;
2345 KEYCONTAINER *pKeyContainer;
2347 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
2348 hProv, hHash, pbData, dwDataLen, dwFlags);
2350 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2351 (OBJECTHDR**)&pKeyContainer))
2353 SetLastError(NTE_BAD_UID);
2359 SetLastError(NTE_BAD_FLAGS);
2363 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2364 (OBJECTHDR**)&pCryptHash))
2366 SetLastError(NTE_BAD_HASH);
2370 if (!get_algid_info(pKeyContainer, pCryptHash->aiAlgid) ||
2371 pCryptHash->aiAlgid == CALG_SSL3_SHAMD5)
2373 SetLastError(NTE_BAD_ALGID);
2377 if (pCryptHash->dwState == RSAENH_HASHSTATE_IDLE)
2378 pCryptHash->dwState = RSAENH_HASHSTATE_HASHING;
2380 if (pCryptHash->dwState != RSAENH_HASHSTATE_HASHING)
2382 SetLastError(NTE_BAD_HASH_STATE);
2386 update_hash(pCryptHash, pbData, dwDataLen);
2390 /******************************************************************************
2391 * CPHashSessionKey (RSAENH.@)
2393 BOOL WINAPI RSAENH_CPHashSessionKey(HCRYPTPROV hProv, HCRYPTHASH hHash, HCRYPTKEY hKey,
2400 /******************************************************************************
2401 * CPReleaseContext (RSAENH.@)
2403 * Release a key container.
2406 * hProv [I] Key container to be released.
2407 * dwFlags [I] Currently none defined.
2413 BOOL WINAPI RSAENH_CPReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
2415 TRACE("(hProv=%08lx, dwFlags=%08lx)\n", hProv, dwFlags);
2417 if (!release_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER))
2419 /* MSDN: hProv not containing valid context handle */
2420 SetLastError(NTE_BAD_UID);
2427 /******************************************************************************
2428 * CPSetHashParam (RSAENH.@)
2430 * Set a parameter of a hash object
2433 * hProv [I] The key container to which the key belongs.
2434 * hHash [I] The hash object for which a parameter is to be set.
2435 * dwParam [I] Parameter type. See Notes.
2436 * pbData [I] Pointer to the parameter value.
2437 * dwFlags [I] Currently none defined.
2444 * Currently only the HP_HMAC_INFO dwParam type is defined.
2445 * The HMAC_INFO struct will be deep copied into the hash object.
2446 * See Internet RFC 2104 for details on the HMAC algorithm.
2448 BOOL WINAPI RSAENH_CPSetHashParam(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwParam,
2449 BYTE *pbData, DWORD dwFlags)
2451 CRYPTHASH *pCryptHash;
2452 CRYPTKEY *pCryptKey;
2453 KEYCONTAINER *pKeyContainer;
2456 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
2457 hProv, hHash, dwParam, pbData, dwFlags);
2459 if (!lookup_handle(&handle_table, (unsigned int)hProv, RSAENH_MAGIC_CONTAINER,
2460 (OBJECTHDR**)&pKeyContainer))
2462 SetLastError(NTE_BAD_UID);
2467 SetLastError(NTE_BAD_FLAGS);
2471 if (!lookup_handle(&handle_table, (unsigned int)hHash, RSAENH_MAGIC_HASH,
2472 (OBJECTHDR**)&pCryptHash))
2474 SetLastError(NTE_BAD_HASH);
2480 free_hmac_info(pCryptHash->pHMACInfo);
2481 if (!copy_hmac_info(&pCryptHash->pHMACInfo, (PHMAC_INFO)pbData)) return FALSE;
2482 init_hash(pKeyContainer, pCryptHash);
2484 if (!lookup_handle(&handle_table, pCryptHash->hKey, RSAENH_MAGIC_KEY,
2485 (OBJECTHDR**)&pCryptKey))
2487 SetLastError(NTE_FAIL); /* FIXME: correct error code? */
2491 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbInnerString); i++) {
2492 pCryptHash->pHMACInfo->pbInnerString[i] ^= pCryptKey->abKeyValue[i];
2494 for (i=0; i<RSAENH_MIN(pCryptKey->dwKeyLen,pCryptHash->pHMACInfo->cbOuterString); i++) {
2495 pCryptHash->pHMACInfo->pbOuterString[i] ^= pCryptKey->abKeyValue[i];
2498 return RSAENH_CPHashData(hProv, hHash, pCryptHash->pHMACInfo->pbInnerString,
2499 pCryptHash->pHMACInfo->cbInnerString, 0);
2502 SetLastError(NTE_BAD_TYPE);
2507 /******************************************************************************
2508 * CPSetProvParam (RSAENH.@)
2510 BOOL WINAPI RSAENH_CPSetProvParam(HCRYPTPROV hProv, DWORD dwParam, BYTE *pbData, DWORD dwFlags)
2516 /******************************************************************************
2517 * CPSignHash (RSAENH.@)
2519 BOOL WINAPI RSAENH_CPSignHash(HCRYPTPROV hProv, HCRYPTHASH hHash, DWORD dwKeySpec,
2520 LPCWSTR sDescription, DWORD dwFlags, BYTE *pbSignature,
2527 /******************************************************************************
2528 * CPVerifySignature (RSAENH.@)
2530 BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, CONST BYTE *pbSignature,
2531 DWORD dwSigLen, HCRYPTKEY hPubKey, LPCWSTR sDescription,
2538 static const WCHAR szProviderKeys[3][97] = {
2539 { 'S','o','f','t','w','a','r','e','\\',
2540 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2541 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2542 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
2543 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2544 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2545 { 'S','o','f','t','w','a','r','e','\\',
2546 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2547 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2548 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
2549 'E','n','h','a','n','c','e','d',
2550 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2551 'o','v','i','d','e','r',' ','v','1','.','0',0 },
2552 { 'S','o','f','t','w','a','r','e','\\',
2553 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2554 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2555 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
2556 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
2557 'o','v','i','d','e','r',0 }
2559 static const WCHAR szDefaultKey[] = { 'S','o','f','t','w','a','r','e','\\',
2560 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
2561 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
2562 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0};
2564 /******************************************************************************
2565 * DllRegisterServer (RSAENH.@)
2567 * Dll self registration.
2576 * Registers the following keys:
2577 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2578 * Microsoft Base Cryptographic Provider v1.0
2579 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2580 * Microsoft Enhanced Cryptographic Provider
2581 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
2582 * Microsoft Strong Cryptographpic Provider
2583 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
2585 HRESULT WINAPI RSAENH_DllRegisterServer()
2592 for (i=0; i<3; i++) {
2593 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szProviderKeys[i], 0, NULL,
2594 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &dp);
2596 if (apiRet == ERROR_SUCCESS)
2598 if (dp == REG_CREATED_NEW_KEY)
2600 static const WCHAR szImagePath[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
2601 static const WCHAR szRSABase[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
2602 static const WCHAR szType[] = { 'T','y','p','e',0 };
2603 static const WCHAR szSignature[] = { 'S','i','g','n','a','t','u','r','e',0 };
2605 DWORD sign = 0xdeadbeef;
2606 RegSetValueExW(key, szImagePath, 0, REG_SZ, (LPBYTE)szRSABase,
2607 (lstrlenW(szRSABase) + 1) * sizeof(WCHAR));
2608 RegSetValueExW(key, szType, 0, REG_DWORD, (LPBYTE)&type, sizeof(type));
2609 RegSetValueExW(key, szSignature, 0, REG_BINARY, (LPBYTE)&sign, sizeof(sign));
2614 if (apiRet == ERROR_SUCCESS)
2615 apiRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szDefaultKey, 0, NULL, REG_OPTION_NON_VOLATILE,
2616 KEY_ALL_ACCESS, NULL, &key, &dp);
2617 if (apiRet == ERROR_SUCCESS)
2619 if (dp == REG_CREATED_NEW_KEY)
2621 static const WCHAR szName[] = { 'N','a','m','e',0 };
2622 static const WCHAR szRSAName[] = {
2623 'M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',' ',
2624 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
2625 'P','r','o','v','i','d','e','r',0 };
2626 static const WCHAR szTypeName[] = { 'T','y','p','e','N','a','m','e',0 };
2627 static const WCHAR szRSATypeName[] = {
2628 'R','S','A',' ','F','u','l','l',' ',
2629 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
2630 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 };
2632 RegSetValueExW(key, szName, 0, REG_SZ, (LPBYTE)szRSAName, sizeof(szRSAName));
2633 RegSetValueExW(key, szTypeName, 0, REG_SZ, (LPBYTE)szRSATypeName,sizeof(szRSATypeName));
2637 return HRESULT_FROM_WIN32(apiRet);
2640 /******************************************************************************
2641 * DllUnregisterServer (RSAENH.@)
2643 * Dll self unregistration.
2651 * For the relevant keys see DllRegisterServer.
2653 HRESULT WINAPI RSAENH_DllUnregisterServer()
2655 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[0]);
2656 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[1]);
2657 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderKeys[2]);
2658 RegDeleteKeyW(HKEY_LOCAL_MACHINE, szDefaultKey);