3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
8 * Copyright 2007 Vijay Kiran Kamuju
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
43 /******************************************************************************
44 * CRYPTHASH - hash objects
46 #define RSAENH_MAGIC_HASH 0x85938417u
47 #define RSAENH_MAX_HASH_SIZE 104
48 #define RSAENH_HASHSTATE_HASHING 1
49 #define RSAENH_HASHSTATE_FINISHED 2
50 typedef struct _RSAENH_TLS1PRF_PARAMS
52 CRYPT_DATA_BLOB blobLabel
;
53 CRYPT_DATA_BLOB blobSeed
;
54 } RSAENH_TLS1PRF_PARAMS
;
56 typedef struct tagCRYPTHASH
65 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
67 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
70 /******************************************************************************
71 * CRYPTKEY - key objects
73 #define RSAENH_MAGIC_KEY 0x73620457u
74 #define RSAENH_MAX_KEY_SIZE 48
75 #define RSAENH_MAX_BLOCK_SIZE 24
76 #define RSAENH_KEYSTATE_IDLE 0
77 #define RSAENH_KEYSTATE_ENCRYPTING 1
78 #define RSAENH_KEYSTATE_MASTERKEY 2
79 typedef struct _RSAENH_SCHANNEL_INFO
81 SCHANNEL_ALG saEncAlg
;
82 SCHANNEL_ALG saMACAlg
;
83 CRYPT_DATA_BLOB blobClientRandom
;
84 CRYPT_DATA_BLOB blobServerRandom
;
85 } RSAENH_SCHANNEL_INFO
;
87 typedef struct tagCRYPTKEY
96 DWORD dwEffectiveKeyLen
;
101 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
102 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
103 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
104 RSAENH_SCHANNEL_INFO siSChannelInfo
;
107 /******************************************************************************
108 * KEYCONTAINER - key containers
110 #define RSAENH_PERSONALITY_BASE 0u
111 #define RSAENH_PERSONALITY_STRONG 1u
112 #define RSAENH_PERSONALITY_ENHANCED 2u
113 #define RSAENH_PERSONALITY_SCHANNEL 3u
114 #define RSAENH_PERSONALITY_AES 4u
116 #define RSAENH_MAGIC_CONTAINER 0x26384993u
117 typedef struct tagKEYCONTAINER
123 DWORD dwEnumContainersCtr
;
124 CHAR szName
[MAX_PATH
];
125 CHAR szProvName
[MAX_PATH
];
126 HCRYPTKEY hKeyExchangeKeyPair
;
127 HCRYPTKEY hSignatureKeyPair
;
130 /******************************************************************************
131 * Some magic constants
133 #define RSAENH_ENCRYPT 1
134 #define RSAENH_DECRYPT 0
135 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
136 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
137 #define RSAENH_HMAC_DEF_PAD_LEN 64
138 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
139 #define RSAENH_DES_STORAGE_KEYLEN 64
140 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
141 #define RSAENH_3DES112_STORAGE_KEYLEN 128
142 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
143 #define RSAENH_3DES_STORAGE_KEYLEN 192
144 #define RSAENH_MAGIC_RSA2 0x32415352
145 #define RSAENH_MAGIC_RSA1 0x31415352
146 #define RSAENH_PKC_BLOCKTYPE 0x02
147 #define RSAENH_SSL3_VERSION_MAJOR 3
148 #define RSAENH_SSL3_VERSION_MINOR 0
149 #define RSAENH_TLS1_VERSION_MAJOR 3
150 #define RSAENH_TLS1_VERSION_MINOR 1
151 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
153 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
154 /******************************************************************************
155 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
157 #define RSAENH_MAX_ENUMALGS 24
158 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
159 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
162 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
163 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
164 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
165 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
166 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
167 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
168 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
169 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
170 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
171 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
172 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
173 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
174 {0, 0, 0, 0,0, 1,"", 1,""}
177 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
178 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
179 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
180 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
181 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
182 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
183 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
184 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
185 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
186 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
187 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
188 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
189 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
190 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
191 {0, 0, 0, 0,0, 1,"", 1,""}
194 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
195 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
196 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
197 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
198 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
199 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
200 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
201 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
202 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
203 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
204 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
205 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
206 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
207 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
208 {0, 0, 0, 0,0, 1,"", 1,""}
211 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
212 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
213 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
214 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
215 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
216 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
217 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
218 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
219 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
220 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
221 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
222 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
223 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
224 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
225 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
226 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
227 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
228 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
229 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
230 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
231 {0, 0, 0, 0,0, 1,"", 1,""}
234 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
235 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
236 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
237 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
238 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
239 {CALG_AES
, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
240 {CALG_AES_128
, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
241 {CALG_AES_192
, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
242 {CALG_AES_256
, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
243 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
244 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
245 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
246 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
247 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
248 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
249 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
250 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
251 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
252 {0, 0, 0, 0,0, 1,"", 1,""}
256 /******************************************************************************
257 * API forward declarations
260 RSAENH_CPGetKeyParam(
291 RSAENH_CPSetHashParam(
295 BYTE
*pbData
, DWORD dwFlags
299 RSAENH_CPGetHashParam(
309 RSAENH_CPDestroyHash(
314 static BOOL
crypt_export_key(
324 static BOOL
import_key(
343 /******************************************************************************
344 * CSP's handle table (used by all acquired key containers)
346 static struct handle_table handle_table
;
348 /******************************************************************************
351 * Initializes and destroys the handle table for the CSP's handles.
353 int WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID pvReserved
)
357 case DLL_PROCESS_ATTACH
:
358 DisableThreadLibraryCalls(hInstance
);
359 init_handle_table(&handle_table
);
362 case DLL_PROCESS_DETACH
:
363 destroy_handle_table(&handle_table
);
369 /******************************************************************************
370 * copy_param [Internal]
372 * Helper function that supports the standard WINAPI protocol for querying data
376 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
377 * May be NUL if the required buffer size is to be queried only.
378 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
379 * Out: Size of parameter pbParam
380 * pbParam [I] Parameter value.
381 * dwParamSize [I] Size of pbParam
384 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
385 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
387 static inline BOOL
copy_param(
388 BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, CONST BYTE
*pbParam
, DWORD dwParamSize
)
392 if (dwParamSize
> *pdwBufferSize
)
394 SetLastError(ERROR_MORE_DATA
);
395 *pdwBufferSize
= dwParamSize
;
398 memcpy(pbBuffer
, pbParam
, dwParamSize
);
400 *pdwBufferSize
= dwParamSize
;
404 /******************************************************************************
405 * get_algid_info [Internal]
407 * Query CSP capabilities for a given crypto algorithm.
410 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
411 * algid [I] Identifier of the crypto algorithm about which information is requested.
414 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
415 * Failure: NULL (algid not supported)
417 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
418 const PROV_ENUMALGS_EX
*iterator
;
419 KEYCONTAINER
*pKeyContainer
;
421 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
, (OBJECTHDR
**)&pKeyContainer
)) {
422 SetLastError(NTE_BAD_UID
);
426 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
427 if (iterator
->aiAlgid
== algid
) return iterator
;
430 SetLastError(NTE_BAD_ALGID
);
434 /******************************************************************************
435 * copy_data_blob [Internal]
437 * deeply copies a DATA_BLOB
440 * dst [O] That's where the blob will be copied to
441 * src [I] Source blob
445 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
448 * Use free_data_blob to release resources occupied by copy_data_blob.
450 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src
) {
451 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
453 SetLastError(NTE_NO_MEMORY
);
456 dst
->cbData
= src
->cbData
;
457 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
461 /******************************************************************************
462 * concat_data_blobs [Internal]
464 * Concatenates two blobs
467 * dst [O] The new blob will be copied here
468 * src1 [I] Prefix blob
469 * src2 [I] Appendix blob
473 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
476 * Release resources occupied by concat_data_blobs with free_data_blobs
478 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, CONST PCRYPT_DATA_BLOB src1
,
479 CONST PCRYPT_DATA_BLOB src2
)
481 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
482 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
484 SetLastError(NTE_NO_MEMORY
);
487 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
488 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
492 /******************************************************************************
493 * free_data_blob [Internal]
495 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
498 * pBlob [I] Heap space occupied by pBlob->pbData is released
500 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
501 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
504 /******************************************************************************
505 * init_data_blob [Internal]
507 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
508 pBlob
->pbData
= NULL
;
512 /******************************************************************************
513 * free_hmac_info [Internal]
515 * Deeply free an HMAC_INFO struct.
518 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
521 * See Internet RFC 2104 for details on the HMAC algorithm.
523 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
524 if (!hmac_info
) return;
525 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
526 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
527 HeapFree(GetProcessHeap(), 0, hmac_info
);
530 /******************************************************************************
531 * copy_hmac_info [Internal]
533 * Deeply copy an HMAC_INFO struct
536 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
537 * src [I] Pointer to the HMAC_INFO struct to be copied.
544 * See Internet RFC 2104 for details on the HMAC algorithm.
546 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
547 if (!src
) return FALSE
;
548 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
549 if (!*dst
) return FALSE
;
551 (*dst
)->pbInnerString
= NULL
;
552 (*dst
)->pbOuterString
= NULL
;
553 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
554 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
555 if (!(*dst
)->pbInnerString
) {
556 free_hmac_info(*dst
);
559 if (src
->cbInnerString
)
560 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
562 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
563 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
564 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
565 if (!(*dst
)->pbOuterString
) {
566 free_hmac_info(*dst
);
569 if (src
->cbOuterString
)
570 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
572 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
576 /******************************************************************************
577 * destroy_hash [Internal]
579 * Destructor for hash objects
582 * pCryptHash [I] Pointer to the hash object to be destroyed.
583 * Will be invalid after function returns!
585 static void destroy_hash(OBJECTHDR
*pObject
)
587 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
589 free_hmac_info(pCryptHash
->pHMACInfo
);
590 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
591 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
592 HeapFree(GetProcessHeap(), 0, pCryptHash
);
595 /******************************************************************************
596 * init_hash [Internal]
598 * Initialize (or reset) a hash object
601 * pCryptHash [I] The hash object to be initialized.
603 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
606 switch (pCryptHash
->aiAlgid
)
609 if (pCryptHash
->pHMACInfo
) {
610 const PROV_ENUMALGS_EX
*pAlgInfo
;
612 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
613 if (!pAlgInfo
) return FALSE
;
614 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
615 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
616 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
617 pCryptHash
->pHMACInfo
->pbInnerString
,
618 pCryptHash
->pHMACInfo
->cbInnerString
);
623 dwLen
= sizeof(DWORD
);
624 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
625 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
626 pCryptHash
->dwHashSize
>>= 3;
630 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
634 /******************************************************************************
635 * update_hash [Internal]
637 * Hashes the given data and updates the hash object's state accordingly
640 * pCryptHash [I] Hash object to be updated.
641 * pbData [I] Pointer to data stream to be hashed.
642 * dwDataLen [I] Length of data stream.
644 static inline void update_hash(CRYPTHASH
*pCryptHash
, CONST BYTE
*pbData
, DWORD dwDataLen
) {
647 switch (pCryptHash
->aiAlgid
)
650 if (pCryptHash
->pHMACInfo
)
651 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
656 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
658 memcpy(pbTemp
, pbData
, dwDataLen
);
659 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
660 pbTemp
, &dwDataLen
, dwDataLen
);
661 HeapFree(GetProcessHeap(), 0, pbTemp
);
665 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
669 /******************************************************************************
670 * finalize_hash [Internal]
672 * Finalizes the hash, after all data has been hashed with update_hash.
673 * No additional data can be hashed afterwards until the hash gets initialized again.
676 * pCryptHash [I] Hash object to be finalized.
678 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
681 switch (pCryptHash
->aiAlgid
)
684 if (pCryptHash
->pHMACInfo
) {
685 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
687 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
688 pCryptHash
->abHashValue
);
689 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
690 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
691 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
692 pCryptHash
->pHMACInfo
->pbOuterString
,
693 pCryptHash
->pHMACInfo
->cbOuterString
);
694 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
695 abHashValue
, pCryptHash
->dwHashSize
);
696 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
697 pCryptHash
->abHashValue
);
703 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
704 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
708 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
712 /******************************************************************************
713 * destroy_key [Internal]
715 * Destructor for key objects
718 * pCryptKey [I] Pointer to the key object to be destroyed.
719 * Will be invalid after function returns!
721 static void destroy_key(OBJECTHDR
*pObject
)
723 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
725 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
726 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
727 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
728 HeapFree(GetProcessHeap(), 0, pCryptKey
);
731 /******************************************************************************
732 * setup_key [Internal]
734 * Initialize (or reset) a key object
737 * pCryptKey [I] The key object to be initialized.
739 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
740 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
741 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
742 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
743 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
744 pCryptKey
->abKeyValue
);
747 /******************************************************************************
750 * Creates a new key object without assigning the actual binary key value.
751 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
754 * hProv [I] Handle to the provider to which the created key will belong.
755 * aiAlgid [I] The new key shall use the crypto algorithm idenfied by aiAlgid.
756 * dwFlags [I] Upper 16 bits give the key length.
757 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
759 * ppCryptKey [O] Pointer to the created key
762 * Success: Handle to the created key.
763 * Failure: INVALID_HANDLE_VALUE
765 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
769 DWORD dwKeyLen
= HIWORD(dwFlags
);
770 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
775 * Retrieve the CSP's capabilities for the given ALG_ID value
777 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
778 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
781 * Assume the default key length, if none is specified explicitly
783 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
786 * Check if the requested key length is supported by the current CSP.
787 * Adjust key length's for DES algorithms.
791 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
792 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
794 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
795 SetLastError(NTE_BAD_FLAGS
);
796 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
801 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
802 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
804 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
805 SetLastError(NTE_BAD_FLAGS
);
806 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
811 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
812 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
814 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
815 SetLastError(NTE_BAD_FLAGS
);
816 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
822 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
823 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
825 SetLastError(NTE_BAD_FLAGS
);
826 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
830 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
831 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
832 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
834 pCryptKey
->aiAlgid
= aiAlgid
;
835 pCryptKey
->hProv
= hProv
;
836 pCryptKey
->dwModeBits
= 0;
837 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
839 if (dwFlags
& CRYPT_EXPORTABLE
)
840 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
841 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
842 pCryptKey
->dwEffectiveKeyLen
= 0;
843 if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
844 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
846 pCryptKey
->dwSaltLen
= 0;
847 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
848 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
849 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
850 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
854 case CALG_PCT1_MASTER
:
855 case CALG_SSL2_MASTER
:
856 case CALG_SSL3_MASTER
:
857 case CALG_TLS1_MASTER
:
859 pCryptKey
->dwBlockLen
= 0;
860 pCryptKey
->dwMode
= 0;
867 pCryptKey
->dwBlockLen
= 8;
868 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
875 pCryptKey
->dwBlockLen
= 16;
876 pCryptKey
->dwMode
= CRYPT_MODE_ECB
;
881 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
882 pCryptKey
->dwMode
= 0;
886 *ppCryptKey
= pCryptKey
;
892 /******************************************************************************
893 * map_key_spec_to_key_pair_name [Internal]
895 * Returns the name of the registry value associated with a key spec.
898 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
901 * Success: Name of registry value.
904 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
911 szValueName
= "KeyExchangeKeyPair";
914 szValueName
= "SignatureKeyPair";
917 WARN("invalid key spec %d\n", dwKeySpec
);
923 /******************************************************************************
924 * store_key_pair [Internal]
926 * Stores a key pair to the registry
929 * hCryptKey [I] Handle to the key to be stored
930 * hKey [I] Registry key where the key pair is to be stored
931 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
932 * dwFlags [I] Flags for protecting the key
934 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
937 DATA_BLOB blobIn
, blobOut
;
942 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
944 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
947 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
949 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
952 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
955 blobIn
.pbData
= pbKey
;
956 blobIn
.cbData
= dwLen
;
958 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
961 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
962 blobOut
.pbData
, blobOut
.cbData
);
963 LocalFree(blobOut
.pbData
);
966 HeapFree(GetProcessHeap(), 0, pbKey
);
972 /******************************************************************************
973 * map_key_spec_to_permissions_name [Internal]
975 * Returns the name of the registry value associated with the permissions for
979 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
982 * Success: Name of registry value.
985 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
992 szValueName
= "KeyExchangePermissions";
995 szValueName
= "SignaturePermissions";
998 WARN("invalid key spec %d\n", dwKeySpec
);
1004 /******************************************************************************
1005 * store_key_permissions [Internal]
1007 * Stores a key's permissions to the registry
1010 * hCryptKey [I] Handle to the key whose permissions are to be stored
1011 * hKey [I] Registry key where the key permissions are to be stored
1012 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1014 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1019 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1021 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1022 (OBJECTHDR
**)&pKey
))
1023 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1024 (BYTE
*)&pKey
->dwPermissions
,
1025 sizeof(pKey
->dwPermissions
));
1028 /******************************************************************************
1029 * create_container_key [Internal]
1031 * Creates the registry key for a key container's persistent storage.
1034 * pKeyContainer [I] Pointer to the key container
1035 * sam [I] Desired registry access
1036 * phKey [O] Returned key
1038 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1040 CHAR szRSABase
[MAX_PATH
];
1043 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1045 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1046 hRootKey
= HKEY_LOCAL_MACHINE
;
1048 hRootKey
= HKEY_CURRENT_USER
;
1050 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1051 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1052 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1053 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1057 /******************************************************************************
1058 * open_container_key [Internal]
1060 * Opens a key container's persistent storage for reading.
1063 * pszContainerName [I] Name of the container to be opened. May be the empty
1064 * string if the parent key of all containers is to be
1066 * dwFlags [I] Flags indicating which keyset to be opened.
1067 * phKey [O] Returned key
1069 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, HKEY
*phKey
)
1071 CHAR szRSABase
[MAX_PATH
];
1074 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1076 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1077 hRootKey
= HKEY_LOCAL_MACHINE
;
1079 hRootKey
= HKEY_CURRENT_USER
;
1081 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1082 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1083 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, KEY_READ
, phKey
) ==
1087 /******************************************************************************
1088 * delete_container_key [Internal]
1090 * Deletes a key container's persistent storage.
1093 * pszContainerName [I] Name of the container to be opened.
1094 * dwFlags [I] Flags indicating which keyset to be opened.
1096 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1098 CHAR szRegKey
[MAX_PATH
];
1100 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1101 SetLastError(NTE_BAD_KEYSET_PARAM
);
1105 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1106 hRootKey
= HKEY_LOCAL_MACHINE
;
1108 hRootKey
= HKEY_CURRENT_USER
;
1109 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1110 SetLastError(ERROR_SUCCESS
);
1113 SetLastError(NTE_BAD_KEYSET
);
1119 /******************************************************************************
1120 * store_key_container_keys [Internal]
1122 * Stores key container's keys in a persistent location.
1125 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1127 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1132 /* On WinXP, persistent keys are stored in a file located at:
1133 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1136 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1137 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1141 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1143 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1144 AT_KEYEXCHANGE
, dwFlags
);
1145 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1146 AT_SIGNATURE
, dwFlags
);
1151 /******************************************************************************
1152 * store_key_container_permissions [Internal]
1154 * Stores key container's key permissions in a persistent location.
1157 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1160 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1165 /* On WinXP, persistent keys are stored in a file located at:
1166 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1169 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1170 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1174 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1176 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1178 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1184 /******************************************************************************
1185 * release_key_container_keys [Internal]
1187 * Releases key container's keys.
1190 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1192 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1194 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1196 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1200 /******************************************************************************
1201 * destroy_key_container [Internal]
1203 * Destructor for key containers.
1206 * pObjectHdr [I] Pointer to the key container to be destroyed.
1208 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1210 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1212 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1214 store_key_container_keys(pKeyContainer
);
1215 store_key_container_permissions(pKeyContainer
);
1216 release_key_container_keys(pKeyContainer
);
1218 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1221 /******************************************************************************
1222 * new_key_container [Internal]
1224 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1225 * of the CSP is determined via the pVTable->pszProvName string.
1228 * pszContainerName [I] Name of the key container.
1229 * pVTable [I] Callback functions and context info provided by the OS
1232 * Success: Handle to the new key container.
1233 * Failure: INVALID_HANDLE_VALUE
1235 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1237 KEYCONTAINER
*pKeyContainer
;
1238 HCRYPTPROV hKeyContainer
;
1240 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1241 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1242 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1244 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1245 pKeyContainer
->dwFlags
= dwFlags
;
1246 pKeyContainer
->dwEnumAlgsCtr
= 0;
1247 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1248 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1249 if (pVTable
&& pVTable
->pszProvName
) {
1250 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1251 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1252 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1253 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1254 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1255 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1256 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1257 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
)) {
1258 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1260 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1264 /* The new key container has to be inserted into the CSP immediately
1265 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1266 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1269 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1274 return hKeyContainer
;
1277 /******************************************************************************
1278 * read_key_value [Internal]
1280 * Reads a key pair value from the registry
1283 * hKeyContainer [I] Crypt provider to use to import the key
1284 * hKey [I] Registry key from which to read the key pair
1285 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1286 * dwFlags [I] Flags for unprotecting the key
1287 * phCryptKey [O] Returned key
1289 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1292 DWORD dwValueType
, dwLen
;
1294 DATA_BLOB blobIn
, blobOut
;
1297 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1299 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1302 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1305 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1308 blobIn
.pbData
= pbKey
;
1309 blobIn
.cbData
= dwLen
;
1311 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1314 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1316 LocalFree(blobOut
.pbData
);
1319 HeapFree(GetProcessHeap(), 0, pbKey
);
1326 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1327 (OBJECTHDR
**)&pKey
))
1329 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1331 dwLen
= sizeof(pKey
->dwPermissions
);
1332 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1333 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1340 /******************************************************************************
1341 * read_key_container [Internal]
1343 * Tries to read the persistent state of the key container (mainly the signature
1344 * and key exchange private keys) given by pszContainerName.
1347 * pszContainerName [I] Name of the key container to read from the registry
1348 * pVTable [I] Pointer to context data provided by the operating system
1351 * Success: Handle to the key container read from the registry
1352 * Failure: INVALID_HANDLE_VALUE
1354 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1357 KEYCONTAINER
*pKeyContainer
;
1358 HCRYPTPROV hKeyContainer
;
1359 HCRYPTKEY hCryptKey
;
1361 if (!open_container_key(pszContainerName
, dwFlags
, &hKey
))
1363 SetLastError(NTE_BAD_KEYSET
);
1364 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1367 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1368 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1370 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1371 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1373 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1374 (OBJECTHDR
**)&pKeyContainer
))
1375 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1377 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1378 dwProtectFlags
, &hCryptKey
))
1379 pKeyContainer
->hKeyExchangeKeyPair
= hCryptKey
;
1380 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1381 dwProtectFlags
, &hCryptKey
))
1382 pKeyContainer
->hSignatureKeyPair
= hCryptKey
;
1385 return hKeyContainer
;
1388 /******************************************************************************
1389 * build_hash_signature [Internal]
1391 * Builds a padded version of a hash to match the length of the RSA key modulus.
1394 * pbSignature [O] The padded hash object is stored here.
1395 * dwLen [I] Length of the pbSignature buffer.
1396 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1397 * abHashValue [I] The value of the hash object.
1398 * dwHashLen [I] Length of the hash value.
1399 * dwFlags [I] Selection of padding algorithm.
1403 * Failure: FALSE (NTE_BAD_ALGID)
1405 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1406 CONST BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1408 /* These prefixes are meant to be concatenated with hash values of the
1409 * respective kind to form a PKCS #7 DigestInfo. */
1410 static const struct tagOIDDescriptor
{
1413 CONST BYTE abOID
[18];
1414 } aOIDDescriptor
[5] = {
1415 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1416 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1417 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1418 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1419 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1420 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1421 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1422 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1425 DWORD dwIdxOID
, i
, j
;
1427 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1428 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1431 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1432 SetLastError(NTE_BAD_ALGID
);
1436 /* Build the padded signature */
1437 if (dwFlags
& CRYPT_X931_FORMAT
) {
1438 pbSignature
[0] = 0x6b;
1439 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1440 pbSignature
[i
] = 0xbb;
1442 pbSignature
[i
++] = 0xba;
1443 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1444 pbSignature
[i
] = abHashValue
[j
];
1446 pbSignature
[i
++] = 0x33;
1447 pbSignature
[i
++] = 0xcc;
1449 pbSignature
[0] = 0x00;
1450 pbSignature
[1] = 0x01;
1451 if (dwFlags
& CRYPT_NOHASHOID
) {
1452 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1453 pbSignature
[i
] = 0xff;
1455 pbSignature
[i
++] = 0x00;
1457 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1458 pbSignature
[i
] = 0xff;
1460 pbSignature
[i
++] = 0x00;
1461 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1462 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1465 for (j
=0; j
< dwHashLen
; j
++) {
1466 pbSignature
[i
++] = abHashValue
[j
];
1473 /******************************************************************************
1476 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1477 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1478 * The pseudo random stream generated by this function is exclusive or'ed with
1479 * the data in pbBuffer.
1482 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1483 * pblobSeed [I] Seed value
1484 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1485 * dwBufferLen [I] Number of pseudo random bytes desired
1491 static BOOL
tls1_p(HCRYPTHASH hHMAC
, CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1494 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1497 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1498 SetLastError(NTE_BAD_HASH
);
1502 /* compute A_1 = HMAC(seed) */
1504 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1505 finalize_hash(pHMAC
);
1506 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1509 /* compute HMAC(A_i + seed) */
1511 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1512 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1513 finalize_hash(pHMAC
);
1515 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1517 if (i
>= dwBufferLen
) break;
1518 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1520 } while (i
% pHMAC
->dwHashSize
);
1522 /* compute A_{i+1} = HMAC(A_i) */
1524 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1525 finalize_hash(pHMAC
);
1526 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1527 } while (i
< dwBufferLen
);
1532 /******************************************************************************
1533 * tls1_prf [Internal]
1535 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1538 * hProv [I] Key container used to compute the pseudo random stream
1539 * hSecret [I] Key that holds the (pre-)master secret
1540 * pblobLabel [I] Descriptive label
1541 * pblobSeed [I] Seed value
1542 * pbBuffer [O] Pseudo random numbers will be stored here
1543 * dwBufferLen [I] Number of pseudo random bytes desired
1549 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, CONST PCRYPT_DATA_BLOB pblobLabel
,
1550 CONST PCRYPT_DATA_BLOB pblobSeed
, PBYTE pbBuffer
, DWORD dwBufferLen
)
1552 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1553 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1554 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1555 CRYPTKEY
*pHalfSecret
, *pSecret
;
1556 DWORD dwHalfSecretLen
;
1557 BOOL result
= FALSE
;
1558 CRYPT_DATA_BLOB blobLabelSeed
;
1560 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1561 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1563 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1564 SetLastError(NTE_FAIL
);
1568 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1570 /* concatenation of the label and the seed */
1571 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1573 /* zero out the buffer, since two random streams will be xor'ed into it. */
1574 memset(pbBuffer
, 0, dwBufferLen
);
1576 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1577 * the biggest range of valid key lengths. */
1578 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1579 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1581 /* Derive an HMAC_MD5 hash and call the helper function. */
1582 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1583 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1584 hmacInfo
.HashAlgid
= CALG_MD5
;
1585 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1586 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1588 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1589 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1590 hmacInfo
.HashAlgid
= CALG_SHA
;
1591 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1592 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1596 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1597 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1598 free_data_blob(&blobLabelSeed
);
1602 /******************************************************************************
1603 * pad_data [Internal]
1605 * Helper function for data padding according to PKCS1 #2
1608 * abData [I] The data to be padded
1609 * dwDataLen [I] Length of the data
1610 * abBuffer [O] Padded data will be stored here
1611 * dwBufferLen [I] Length of the buffer (also length of padded data)
1612 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1616 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1618 static BOOL
pad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1623 /* Ensure there is enough space for PKCS1 #2 padding */
1624 if (dwDataLen
> dwBufferLen
-11) {
1625 SetLastError(NTE_BAD_LEN
);
1629 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1632 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1633 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1634 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1635 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1636 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1643 /******************************************************************************
1644 * unpad_data [Internal]
1646 * Remove the PKCS1 padding from RSA decrypted data
1649 * abData [I] The padded data
1650 * dwDataLen [I] Length of the padded data
1651 * abBuffer [O] Data without padding will be stored here
1652 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1653 * dwFlags [I] Currently none defined
1657 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1659 static BOOL
unpad_data(CONST BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1664 for (i
=2; i
<dwDataLen
; i
++)
1668 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1669 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1671 SetLastError(NTE_BAD_DATA
);
1675 *dwBufferLen
= dwDataLen
- i
- 1;
1676 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1680 /******************************************************************************
1681 * CPAcquireContext (RSAENH.@)
1683 * Acquire a handle to the key container specified by pszContainer
1686 * phProv [O] Pointer to the location the acquired handle will be written to.
1687 * pszContainer [I] Name of the desired key container. See Notes
1688 * dwFlags [I] Flags. See Notes.
1689 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1696 * If pszContainer is NULL or points to a zero length string the user's login
1697 * name will be used as the key container name.
1699 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1700 * If a keyset with the given name already exists, the function fails and sets
1701 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1702 * key container does not exist, function fails and sets last error to
1705 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1706 DWORD dwFlags
, PVTableProvStruc pVTable
)
1708 CHAR szKeyContainerName
[MAX_PATH
];
1710 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1711 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1713 if (pszContainer
&& *pszContainer
)
1715 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1719 DWORD dwLen
= sizeof(szKeyContainerName
);
1720 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1723 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1726 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1729 case CRYPT_DELETEKEYSET
:
1730 return delete_container_key(szKeyContainerName
, dwFlags
);
1732 case CRYPT_NEWKEYSET
:
1733 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1734 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1736 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1737 TRACE("Can't create new keyset, already exists\n");
1738 SetLastError(NTE_EXISTS
);
1741 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1744 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1745 case CRYPT_VERIFYCONTEXT
:
1746 if (pszContainer
&& *pszContainer
) {
1747 TRACE("pszContainer should be empty\n");
1748 SetLastError(NTE_BAD_FLAGS
);
1751 *phProv
= new_key_container("", dwFlags
, pVTable
);
1755 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1756 SetLastError(NTE_BAD_FLAGS
);
1760 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1761 SetLastError(ERROR_SUCCESS
);
1768 /******************************************************************************
1769 * CPCreateHash (RSAENH.@)
1771 * CPCreateHash creates and initalizes a new hash object.
1774 * hProv [I] Handle to the key container to which the new hash will belong.
1775 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1776 * hKey [I] Handle to a session key applied for keyed hashes.
1777 * dwFlags [I] Currently no flags defined. Must be zero.
1778 * phHash [O] Points to the location where a handle to the new hash will be stored.
1785 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1786 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1788 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1791 CRYPTKEY
*pCryptKey
;
1792 CRYPTHASH
*pCryptHash
;
1793 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1795 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1798 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1799 if (!peaAlgidInfo
) return FALSE
;
1803 SetLastError(NTE_BAD_FLAGS
);
1807 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1808 Algid
== CALG_TLS1PRF
)
1810 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1811 SetLastError(NTE_BAD_KEY
);
1815 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1816 SetLastError(NTE_BAD_KEY
);
1820 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1821 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1823 SetLastError(NTE_BAD_KEY
);
1827 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1828 SetLastError(NTE_BAD_KEY_STATE
);
1833 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1834 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1835 if (!pCryptHash
) return FALSE
;
1837 pCryptHash
->aiAlgid
= Algid
;
1838 pCryptHash
->hKey
= hKey
;
1839 pCryptHash
->hProv
= hProv
;
1840 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1841 pCryptHash
->pHMACInfo
= NULL
;
1842 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1843 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1844 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1846 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1847 static const char keyex
[] = "key expansion";
1848 BYTE key_expansion
[sizeof keyex
];
1849 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1851 memcpy( key_expansion
, keyex
, sizeof keyex
);
1853 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1854 static const char msec
[] = "master secret";
1855 BYTE master_secret
[sizeof msec
];
1856 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1857 BYTE abKeyValue
[48];
1859 memcpy( master_secret
, msec
, sizeof msec
);
1861 /* See RFC 2246, chapter 8.1 */
1862 if (!concat_data_blobs(&blobRandom
,
1863 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1864 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1868 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1869 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1870 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1871 free_data_blob(&blobRandom
);
1874 /* See RFC 2246, chapter 6.3 */
1875 if (!concat_data_blobs(&blobRandom
,
1876 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1877 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1881 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1882 RSAENH_MAX_HASH_SIZE
);
1883 free_data_blob(&blobRandom
);
1886 return init_hash(pCryptHash
);
1889 /******************************************************************************
1890 * CPDestroyHash (RSAENH.@)
1892 * Releases the handle to a hash object. The object is destroyed if it's reference
1893 * count reaches zero.
1896 * hProv [I] Handle to the key container to which the hash object belongs.
1897 * hHash [I] Handle to the hash object to be released.
1903 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1905 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1907 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1909 SetLastError(NTE_BAD_UID
);
1913 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1915 SetLastError(NTE_BAD_HASH
);
1922 /******************************************************************************
1923 * CPDestroyKey (RSAENH.@)
1925 * Releases the handle to a key object. The object is destroyed if it's reference
1926 * count reaches zero.
1929 * hProv [I] Handle to the key container to which the key object belongs.
1930 * hKey [I] Handle to the key object to be released.
1936 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
1938 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
1940 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1942 SetLastError(NTE_BAD_UID
);
1946 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
1948 SetLastError(NTE_BAD_KEY
);
1955 /******************************************************************************
1956 * CPDuplicateHash (RSAENH.@)
1958 * Clones a hash object including it's current state.
1961 * hUID [I] Handle to the key container the hash belongs to.
1962 * hHash [I] Handle to the hash object to be cloned.
1963 * pdwReserved [I] Reserved. Must be NULL.
1964 * dwFlags [I] No flags are currently defined. Must be 0.
1965 * phHash [O] Handle to the cloned hash object.
1971 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
1972 DWORD dwFlags
, HCRYPTHASH
*phHash
)
1974 CRYPTHASH
*pSrcHash
, *pDestHash
;
1976 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
1977 pdwReserved
, dwFlags
, phHash
);
1979 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
1981 SetLastError(NTE_BAD_UID
);
1985 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
1987 SetLastError(NTE_BAD_HASH
);
1991 if (!phHash
|| pdwReserved
|| dwFlags
)
1993 SetLastError(ERROR_INVALID_PARAMETER
);
1997 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1998 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
1999 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2001 *pDestHash
= *pSrcHash
;
2002 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2003 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2004 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2005 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2008 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2011 /******************************************************************************
2012 * CPDuplicateKey (RSAENH.@)
2014 * Clones a key object including it's current state.
2017 * hUID [I] Handle to the key container the hash belongs to.
2018 * hKey [I] Handle to the key object to be cloned.
2019 * pdwReserved [I] Reserved. Must be NULL.
2020 * dwFlags [I] No flags are currently defined. Must be 0.
2021 * phHash [O] Handle to the cloned key object.
2027 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2028 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2030 CRYPTKEY
*pSrcKey
, *pDestKey
;
2032 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2033 pdwReserved
, dwFlags
, phKey
);
2035 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2037 SetLastError(NTE_BAD_UID
);
2041 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2043 SetLastError(NTE_BAD_KEY
);
2047 if (!phKey
|| pdwReserved
|| dwFlags
)
2049 SetLastError(ERROR_INVALID_PARAMETER
);
2053 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2054 (OBJECTHDR
**)&pDestKey
);
2055 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2057 *pDestKey
= *pSrcKey
;
2058 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2059 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2060 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2061 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2062 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2071 /******************************************************************************
2072 * CPEncrypt (RSAENH.@)
2077 * hProv [I] The key container hKey and hHash belong to.
2078 * hKey [I] The key used to encrypt the data.
2079 * hHash [I] An optional hash object for parallel hashing. See notes.
2080 * Final [I] Indicates if this is the last block of data to encrypt.
2081 * dwFlags [I] Currently no flags defined. Must be zero.
2082 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2083 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2084 * dwBufLen [I] Size of the buffer at pbData.
2091 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2092 * This is useful for message signatures.
2094 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2096 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2097 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2099 CRYPTKEY
*pCryptKey
;
2100 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2101 DWORD dwEncryptedLen
, i
, j
, k
;
2103 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2104 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2107 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2109 SetLastError(NTE_BAD_UID
);
2115 SetLastError(NTE_BAD_FLAGS
);
2119 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2121 SetLastError(NTE_BAD_KEY
);
2125 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2126 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2128 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2130 SetLastError(NTE_BAD_DATA
);
2134 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2135 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2138 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2139 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2140 SetLastError(NTE_BAD_DATA
);
2144 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2146 if (pbData
== NULL
) {
2147 *pdwDataLen
= dwEncryptedLen
;
2150 else if (dwEncryptedLen
> dwBufLen
) {
2151 *pdwDataLen
= dwEncryptedLen
;
2152 SetLastError(ERROR_MORE_DATA
);
2156 /* Pad final block with length bytes */
2157 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2158 *pdwDataLen
= dwEncryptedLen
;
2160 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2161 switch (pCryptKey
->dwMode
) {
2162 case CRYPT_MODE_ECB
:
2163 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2167 case CRYPT_MODE_CBC
:
2168 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2169 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2171 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2174 case CRYPT_MODE_CFB
:
2175 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2176 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2177 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2178 out
[j
] = in
[j
] ^ o
[0];
2179 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2180 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2181 pCryptKey
->abChainVector
[k
] = out
[j
];
2186 SetLastError(NTE_BAD_ALGID
);
2189 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2191 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2192 if (pbData
== NULL
) {
2193 *pdwDataLen
= dwBufLen
;
2196 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2197 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2198 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2199 SetLastError(NTE_BAD_KEY
);
2203 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2206 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2207 SetLastError(ERROR_MORE_DATA
);
2210 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2211 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2212 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2215 SetLastError(NTE_BAD_TYPE
);
2219 if (Final
) setup_key(pCryptKey
);
2224 /******************************************************************************
2225 * CPDecrypt (RSAENH.@)
2230 * hProv [I] The key container hKey and hHash belong to.
2231 * hKey [I] The key used to decrypt the data.
2232 * hHash [I] An optional hash object for parallel hashing. See notes.
2233 * Final [I] Indicates if this is the last block of data to decrypt.
2234 * dwFlags [I] Currently no flags defined. Must be zero.
2235 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2236 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2243 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2244 * This is useful for message signatures.
2246 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2248 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2249 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2251 CRYPTKEY
*pCryptKey
;
2252 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2256 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2257 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2259 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2261 SetLastError(NTE_BAD_UID
);
2267 SetLastError(NTE_BAD_FLAGS
);
2271 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2273 SetLastError(NTE_BAD_KEY
);
2277 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2278 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2280 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2282 SetLastError(NTE_BAD_DATA
);
2288 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2289 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2290 switch (pCryptKey
->dwMode
) {
2291 case CRYPT_MODE_ECB
:
2292 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2296 case CRYPT_MODE_CBC
:
2297 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2299 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2300 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2303 case CRYPT_MODE_CFB
:
2304 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2305 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2306 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2307 out
[j
] = in
[j
] ^ o
[0];
2308 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2309 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2310 pCryptKey
->abChainVector
[k
] = in
[j
];
2315 SetLastError(NTE_BAD_ALGID
);
2318 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2321 if (pbData
[*pdwDataLen
-1] &&
2322 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2323 pbData
[*pdwDataLen
-1] < *pdwDataLen
) {
2324 BOOL padOkay
= TRUE
;
2326 /* check that every bad byte has the same value */
2327 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2328 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2331 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2333 SetLastError(NTE_BAD_DATA
);
2338 SetLastError(NTE_BAD_DATA
);
2343 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2344 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2345 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2346 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2347 SetLastError(NTE_BAD_KEY
);
2350 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2351 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2354 SetLastError(NTE_BAD_TYPE
);
2358 if (Final
) setup_key(pCryptKey
);
2360 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2361 if (*pdwDataLen
>dwMax
||
2362 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2368 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2369 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2371 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2372 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2375 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2376 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2380 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2382 if (*pdwDataLen
< dwDataLen
) {
2383 SetLastError(ERROR_MORE_DATA
);
2384 *pdwDataLen
= dwDataLen
;
2388 pBlobHeader
->bType
= SIMPLEBLOB
;
2389 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2390 pBlobHeader
->reserved
= 0;
2391 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2393 *pAlgid
= pPubKey
->aiAlgid
;
2395 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2396 pPubKey
->dwBlockLen
, dwFlags
))
2401 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2402 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2404 *pdwDataLen
= dwDataLen
;
2408 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2411 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2412 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2415 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2416 SetLastError(NTE_BAD_KEY
);
2420 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2422 if (*pdwDataLen
< dwDataLen
) {
2423 SetLastError(ERROR_MORE_DATA
);
2424 *pdwDataLen
= dwDataLen
;
2428 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2429 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2430 pBlobHeader
->reserved
= 0;
2431 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2433 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2434 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2436 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2437 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2439 *pdwDataLen
= dwDataLen
;
2443 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2444 BYTE
*pbData
, DWORD
*pdwDataLen
)
2446 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2447 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2450 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2451 SetLastError(NTE_BAD_KEY
);
2454 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2456 SetLastError(NTE_BAD_KEY_STATE
);
2460 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2461 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2463 if (*pdwDataLen
< dwDataLen
) {
2464 SetLastError(ERROR_MORE_DATA
);
2465 *pdwDataLen
= dwDataLen
;
2469 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2470 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2471 pBlobHeader
->reserved
= 0;
2472 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2474 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2475 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2477 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2478 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2480 *pdwDataLen
= dwDataLen
;
2484 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2487 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2488 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2489 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2492 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2494 if (*pdwDataLen
< dwDataLen
) {
2495 SetLastError(ERROR_MORE_DATA
);
2496 *pdwDataLen
= dwDataLen
;
2500 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2501 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2502 pBlobHeader
->reserved
= 0;
2503 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2505 *pKeyLen
= pCryptKey
->dwKeyLen
;
2506 memcpy(pbKey
, &pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2508 *pdwDataLen
= dwDataLen
;
2511 /******************************************************************************
2512 * crypt_export_key [Internal]
2514 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2515 * by store_key_pair.
2518 * pCryptKey [I] Key to be exported.
2519 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2520 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2521 * dwFlags [I] Currently none defined.
2522 * force [I] If TRUE, the key is written no matter what the key's
2523 * permissions are. Otherwise the key's permissions are
2524 * checked before exporting.
2525 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2526 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2532 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2533 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2534 BYTE
*pbData
, DWORD
*pdwDataLen
)
2538 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2539 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2540 SetLastError(NTE_BAD_KEY
);
2545 switch ((BYTE
)dwBlobType
)
2548 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2549 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2552 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2556 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2557 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2561 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2563 case PRIVATEKEYBLOB
:
2564 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2566 case PLAINTEXTKEYBLOB
:
2567 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2570 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2575 /******************************************************************************
2576 * CPExportKey (RSAENH.@)
2578 * Export a key into a binary large object (BLOB).
2581 * hProv [I] Key container from which a key is to be exported.
2582 * hKey [I] Key to be exported.
2583 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2584 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2585 * dwFlags [I] Currently none defined.
2586 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2587 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2593 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2594 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2596 CRYPTKEY
*pCryptKey
;
2598 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2599 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2601 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2603 SetLastError(NTE_BAD_UID
);
2607 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2609 SetLastError(NTE_BAD_KEY
);
2613 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2614 pbData
, pdwDataLen
);
2617 /******************************************************************************
2618 * release_and_install_key [Internal]
2620 * Release an existing key, if present, and replaces it with a new one.
2623 * hProv [I] Key container into which the key is to be imported.
2624 * src [I] Key which will replace *dest
2625 * dest [I] Points to key to be released and replaced with src
2626 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2628 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2629 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2631 RSAENH_CPDestroyKey(hProv
, *dest
);
2632 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2635 KEYCONTAINER
*pKeyContainer
;
2637 if (lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2638 (OBJECTHDR
**)&pKeyContainer
))
2640 store_key_container_keys(pKeyContainer
);
2641 store_key_container_permissions(pKeyContainer
);
2646 /******************************************************************************
2647 * import_private_key [Internal]
2649 * Import a BLOB'ed private key into a key container.
2652 * hProv [I] Key container into which the private key is to be imported.
2653 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2654 * dwDataLen [I] Length of data in buffer at pbData.
2655 * dwFlags [I] One of:
2656 * CRYPT_EXPORTABLE: the imported key is marked exportable
2657 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2658 * phKey [O] Handle to the imported key.
2662 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2663 * it's a PRIVATEKEYBLOB.
2669 static BOOL
import_private_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2670 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2672 KEYCONTAINER
*pKeyContainer
;
2673 CRYPTKEY
*pCryptKey
;
2674 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2675 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2678 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2679 (OBJECTHDR
**)&pKeyContainer
))
2681 SetLastError(NTE_BAD_UID
);
2685 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2686 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
) ||
2687 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2688 (2 * pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2690 SetLastError(NTE_BAD_DATA
);
2694 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2695 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2696 setup_key(pCryptKey
);
2697 ret
= import_private_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2698 pRSAPubKey
->bitlen
/8, pRSAPubKey
->pubexp
);
2700 if (dwFlags
& CRYPT_EXPORTABLE
)
2701 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2702 switch (pBlobHeader
->aiKeyAlg
)
2706 TRACE("installing signing key\n");
2707 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2710 case AT_KEYEXCHANGE
:
2712 TRACE("installing key exchange key\n");
2713 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2721 /******************************************************************************
2722 * import_public_key [Internal]
2724 * Import a BLOB'ed public key into a key container.
2727 * hProv [I] Key container into which the public key is to be imported.
2728 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2729 * dwDataLen [I] Length of data in buffer at pbData.
2730 * dwFlags [I] One of:
2731 * CRYPT_EXPORTABLE: the imported key is marked exportable
2732 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2733 * phKey [O] Handle to the imported key.
2737 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2738 * it's a PUBLICKEYBLOB.
2744 static BOOL
import_public_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2745 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2747 KEYCONTAINER
*pKeyContainer
;
2748 CRYPTKEY
*pCryptKey
;
2749 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2750 CONST RSAPUBKEY
*pRSAPubKey
= (CONST RSAPUBKEY
*)(pBlobHeader
+1);
2754 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2755 (OBJECTHDR
**)&pKeyContainer
))
2757 SetLastError(NTE_BAD_UID
);
2761 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2762 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2763 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2765 SetLastError(NTE_BAD_DATA
);
2769 /* Since this is a public key blob, only the public key is
2770 * available, so only signature verification is possible.
2772 algID
= pBlobHeader
->aiKeyAlg
;
2773 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2774 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2775 setup_key(pCryptKey
);
2776 ret
= import_public_key_impl((CONST BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2777 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2779 if (dwFlags
& CRYPT_EXPORTABLE
)
2780 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2781 switch (pBlobHeader
->aiKeyAlg
)
2783 case AT_KEYEXCHANGE
:
2785 TRACE("installing public key\n");
2786 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2794 /******************************************************************************
2795 * import_symmetric_key [Internal]
2797 * Import a BLOB'ed symmetric key into a key container.
2800 * hProv [I] Key container into which the symmetric key is to be imported.
2801 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2802 * dwDataLen [I] Length of data in buffer at pbData.
2803 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2804 * dwFlags [I] One of:
2805 * CRYPT_EXPORTABLE: the imported key is marked exportable
2806 * phKey [O] Handle to the imported key.
2810 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2811 * it's a SIMPLEBLOB.
2817 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2818 DWORD dwDataLen
, HCRYPTKEY hPubKey
,
2819 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2821 CRYPTKEY
*pCryptKey
, *pPubKey
;
2822 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2823 CONST ALG_ID
*pAlgid
= (CONST ALG_ID
*)(pBlobHeader
+1);
2824 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pAlgid
+ 1);
2828 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2829 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2831 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2835 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2837 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2841 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2842 if (!pbDecrypted
) return FALSE
;
2843 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2846 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2847 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2848 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2852 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2853 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2855 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2858 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2859 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2860 setup_key(pCryptKey
);
2861 if (dwFlags
& CRYPT_EXPORTABLE
)
2862 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2866 /******************************************************************************
2867 * import_plaintext_key [Internal]
2869 * Import a plaintext key into a key container.
2872 * hProv [I] Key container into which the symmetric key is to be imported.
2873 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2874 * dwDataLen [I] Length of data in buffer at pbData.
2875 * dwFlags [I] One of:
2876 * CRYPT_EXPORTABLE: the imported key is marked exportable
2877 * phKey [O] Handle to the imported key.
2881 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2882 * it's a PLAINTEXTKEYBLOB.
2888 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
,
2889 DWORD dwDataLen
, DWORD dwFlags
,
2892 CRYPTKEY
*pCryptKey
;
2893 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2894 CONST DWORD
*pKeyLen
= (CONST DWORD
*)(pBlobHeader
+ 1);
2895 CONST BYTE
*pbKeyStream
= (CONST BYTE
*)(pKeyLen
+ 1);
2897 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2899 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2903 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
2904 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2906 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
2907 setup_key(pCryptKey
);
2908 if (dwFlags
& CRYPT_EXPORTABLE
)
2909 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2913 /******************************************************************************
2914 * import_key [Internal]
2916 * Import a BLOB'ed key into a key container, optionally storing the key's
2917 * value to the registry.
2920 * hProv [I] Key container into which the key is to be imported.
2921 * pbData [I] Pointer to a buffer which holds the BLOB.
2922 * dwDataLen [I] Length of data in buffer at pbData.
2923 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2924 * dwFlags [I] One of:
2925 * CRYPT_EXPORTABLE: the imported key is marked exportable
2926 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2927 * phKey [O] Handle to the imported key.
2933 static BOOL
import_key(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
2934 HCRYPTKEY hPubKey
, DWORD dwFlags
, BOOL fStoreKey
,
2937 KEYCONTAINER
*pKeyContainer
;
2938 CONST BLOBHEADER
*pBlobHeader
= (CONST BLOBHEADER
*)pbData
;
2940 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
2941 (OBJECTHDR
**)&pKeyContainer
))
2943 SetLastError(NTE_BAD_UID
);
2947 if (dwDataLen
< sizeof(BLOBHEADER
) ||
2948 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
2949 pBlobHeader
->reserved
!= 0)
2951 SetLastError(NTE_BAD_DATA
);
2955 /* If this is a verify-only context, the key is not persisted regardless of
2956 * fStoreKey's original value.
2958 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
2959 switch (pBlobHeader
->bType
)
2961 case PRIVATEKEYBLOB
:
2962 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
2966 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
2970 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
2973 case PLAINTEXTKEYBLOB
:
2974 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
2978 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2983 /******************************************************************************
2984 * CPImportKey (RSAENH.@)
2986 * Import a BLOB'ed key into a key container.
2989 * hProv [I] Key container into which the key is to be imported.
2990 * pbData [I] Pointer to a buffer which holds the BLOB.
2991 * dwDataLen [I] Length of data in buffer at pbData.
2992 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2993 * dwFlags [I] One of:
2994 * CRYPT_EXPORTABLE: the imported key is marked exportable
2995 * phKey [O] Handle to the imported key.
3001 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, CONST BYTE
*pbData
, DWORD dwDataLen
,
3002 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3004 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3005 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3007 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3010 /******************************************************************************
3011 * CPGenKey (RSAENH.@)
3013 * Generate a key in the key container
3016 * hProv [I] Key container for which a key is to be generated.
3017 * Algid [I] Crypto algorithm identifier for the key to be generated.
3018 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3019 * phKey [O] Handle to the generated key.
3026 * Flags currently not considered.
3029 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3030 * and AT_SIGNATURE values.
3032 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3034 KEYCONTAINER
*pKeyContainer
;
3035 CRYPTKEY
*pCryptKey
;
3037 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3039 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3040 (OBJECTHDR
**)&pKeyContainer
))
3042 /* MSDN: hProv not containing valid context handle */
3043 SetLastError(NTE_BAD_UID
);
3051 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3053 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3054 setup_key(pCryptKey
);
3055 if (Algid
== AT_SIGNATURE
) {
3056 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hSignatureKeyPair
);
3057 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
3058 &pKeyContainer
->hSignatureKeyPair
);
3063 case AT_KEYEXCHANGE
:
3065 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3067 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3068 setup_key(pCryptKey
);
3069 if (Algid
== AT_KEYEXCHANGE
) {
3070 RSAENH_CPDestroyKey(hProv
, pKeyContainer
->hKeyExchangeKeyPair
);
3071 copy_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
,
3072 &pKeyContainer
->hKeyExchangeKeyPair
);
3086 case CALG_PCT1_MASTER
:
3087 case CALG_SSL2_MASTER
:
3088 case CALG_SSL3_MASTER
:
3089 case CALG_TLS1_MASTER
:
3090 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3092 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3094 case CALG_SSL3_MASTER
:
3095 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3096 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3099 case CALG_TLS1_MASTER
:
3100 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3101 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3104 setup_key(pCryptKey
);
3109 /* MSDN: Algorithm not supported specified by Algid */
3110 SetLastError(NTE_BAD_ALGID
);
3114 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3117 /******************************************************************************
3118 * CPGenRandom (RSAENH.@)
3120 * Generate a random byte stream.
3123 * hProv [I] Key container that is used to generate random bytes.
3124 * dwLen [I] Specifies the number of requested random data bytes.
3125 * pbBuffer [O] Random bytes will be stored here.
3131 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3133 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3135 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3137 /* MSDN: hProv not containing valid context handle */
3138 SetLastError(NTE_BAD_UID
);
3142 return gen_rand_impl(pbBuffer
, dwLen
);
3145 /******************************************************************************
3146 * CPGetHashParam (RSAENH.@)
3148 * Query parameters of an hash object.
3151 * hProv [I] The kea container, which the hash belongs to.
3152 * hHash [I] The hash object that is to be queried.
3153 * dwParam [I] Specifies the parameter that is to be queried.
3154 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3155 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3156 * dwFlags [I] None currently defined.
3163 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3164 * finalized if HP_HASHVALUE is queried.
3166 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3167 DWORD
*pdwDataLen
, DWORD dwFlags
)
3169 CRYPTHASH
*pCryptHash
;
3171 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3172 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3174 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3176 SetLastError(NTE_BAD_UID
);
3182 SetLastError(NTE_BAD_FLAGS
);
3186 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3187 (OBJECTHDR
**)&pCryptHash
))
3189 SetLastError(NTE_BAD_HASH
);
3195 SetLastError(ERROR_INVALID_PARAMETER
);
3202 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->aiAlgid
,
3206 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptHash
->dwHashSize
,
3210 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3211 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3212 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3215 if ( pbData
== NULL
) {
3216 *pdwDataLen
= pCryptHash
->dwHashSize
;
3220 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3222 finalize_hash(pCryptHash
);
3223 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3226 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3227 pCryptHash
->dwHashSize
);
3230 SetLastError(NTE_BAD_TYPE
);
3235 /******************************************************************************
3236 * CPSetKeyParam (RSAENH.@)
3238 * Set a parameter of a key object
3241 * hProv [I] The key container to which the key belongs.
3242 * hKey [I] The key for which a parameter is to be set.
3243 * dwParam [I] Parameter type. See Notes.
3244 * pbData [I] Pointer to the parameter value.
3245 * dwFlags [I] Currently none defined.
3252 * Defined dwParam types are:
3253 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3254 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3255 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3256 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3257 * - KP_IV: Initialization vector
3259 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3262 CRYPTKEY
*pCryptKey
;
3264 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3265 dwParam
, pbData
, dwFlags
);
3267 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3269 SetLastError(NTE_BAD_UID
);
3274 SetLastError(NTE_BAD_FLAGS
);
3278 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3280 SetLastError(NTE_BAD_KEY
);
3286 /* The MS providers only support PKCS5_PADDING */
3287 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3288 SetLastError(NTE_BAD_DATA
);
3294 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3298 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3301 case KP_PERMISSIONS
:
3303 DWORD perms
= *(DWORD
*)pbData
;
3305 if ((perms
& CRYPT_EXPORT
) &&
3306 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3308 SetLastError(NTE_BAD_DATA
);
3311 else if (!(perms
& CRYPT_EXPORT
) &&
3312 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3314 /* Clearing the export permission appears to be ignored,
3317 perms
|= CRYPT_EXPORT
;
3319 pCryptKey
->dwPermissions
= perms
;
3324 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3325 setup_key(pCryptKey
);
3330 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3332 /* salt length can't be greater than 184 bits = 24 bytes */
3333 if (blob
->cbData
> 24)
3335 SetLastError(NTE_BAD_DATA
);
3338 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3340 pCryptKey
->dwSaltLen
= blob
->cbData
;
3341 setup_key(pCryptKey
);
3345 case KP_EFFECTIVE_KEYLEN
:
3346 switch (pCryptKey
->aiAlgid
) {
3350 SetLastError(ERROR_INVALID_PARAMETER
);
3353 else if (!*(DWORD
*)pbData
|| *(DWORD
*)pbData
> 1024)
3355 SetLastError(NTE_BAD_DATA
);
3360 pCryptKey
->dwEffectiveKeyLen
= *(DWORD
*)pbData
;
3361 setup_key(pCryptKey
);
3365 SetLastError(NTE_BAD_TYPE
);
3370 case KP_SCHANNEL_ALG
:
3371 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3372 case SCHANNEL_ENC_KEY
:
3373 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3376 case SCHANNEL_MAC_KEY
:
3377 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3381 SetLastError(NTE_FAIL
); /* FIXME: error code */
3386 case KP_CLIENT_RANDOM
:
3387 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3389 case KP_SERVER_RANDOM
:
3390 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3393 SetLastError(NTE_BAD_TYPE
);
3398 /******************************************************************************
3399 * CPGetKeyParam (RSAENH.@)
3401 * Query a key parameter.
3404 * hProv [I] The key container, which the key belongs to.
3405 * hHash [I] The key object that is to be queried.
3406 * dwParam [I] Specifies the parameter that is to be queried.
3407 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3408 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3409 * dwFlags [I] None currently defined.
3416 * Defined dwParam types are:
3417 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3418 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3419 * (Currently ignored by MS CSP's - always eight)
3420 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3421 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3422 * - KP_IV: Initialization vector.
3423 * - KP_KEYLEN: Bitwidth of the key.
3424 * - KP_BLOCKLEN: Size of a block cipher block.
3425 * - KP_SALT: Salt value.
3427 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3428 DWORD
*pdwDataLen
, DWORD dwFlags
)
3430 CRYPTKEY
*pCryptKey
;
3433 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3434 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3436 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3438 SetLastError(NTE_BAD_UID
);
3443 SetLastError(NTE_BAD_FLAGS
);
3447 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3449 SetLastError(NTE_BAD_KEY
);
3456 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3457 pCryptKey
->dwBlockLen
);
3460 return copy_param(pbData
, pdwDataLen
,
3461 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
], pCryptKey
->dwSaltLen
);
3464 dwValue
= PKCS5_PADDING
;
3465 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3468 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3469 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3471 case KP_EFFECTIVE_KEYLEN
:
3472 if (pCryptKey
->dwEffectiveKeyLen
)
3473 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3475 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3476 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3479 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3480 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwValue
, sizeof(DWORD
));
3483 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3486 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwModeBits
,
3489 case KP_PERMISSIONS
:
3490 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->dwPermissions
,
3494 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3497 SetLastError(NTE_BAD_TYPE
);
3502 /******************************************************************************
3503 * CPGetProvParam (RSAENH.@)
3505 * Query a CSP parameter.
3508 * hProv [I] The key container that is to be queried.
3509 * dwParam [I] Specifies the parameter that is to be queried.
3510 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3511 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3512 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3518 * Defined dwParam types:
3519 * - PP_CONTAINER: Name of the key container.
3520 * - PP_NAME: Name of the cryptographic service provider.
3521 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3522 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3523 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3525 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3526 DWORD
*pdwDataLen
, DWORD dwFlags
)
3528 KEYCONTAINER
*pKeyContainer
;
3529 PROV_ENUMALGS provEnumalgs
;
3533 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3534 * IE6 SP1 asks for it in the 'About' dialog.
3535 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3536 * to be 'don't care's. If you know anything more specific about
3537 * this provider parameter, please report to wine-devel@winehq.org */
3538 static CONST BYTE abWTF
[96] = {
3539 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3540 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3541 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3542 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3543 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3544 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3545 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3546 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3547 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3548 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3549 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3550 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3553 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3554 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3557 SetLastError(ERROR_INVALID_PARAMETER
);
3561 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3562 (OBJECTHDR
**)&pKeyContainer
))
3564 /* MSDN: hProv not containing valid context handle */
3565 SetLastError(NTE_BAD_UID
);
3572 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3573 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szName
,
3574 strlen(pKeyContainer
->szName
)+1);
3577 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)pKeyContainer
->szProvName
,
3578 strlen(pKeyContainer
->szProvName
)+1);
3581 dwTemp
= PROV_RSA_FULL
;
3582 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3585 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3586 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3588 case PP_KEYSET_TYPE
:
3589 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3590 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3593 dwTemp
= CRYPT_SEC_DESCR
;
3594 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3596 case PP_SIG_KEYSIZE_INC
:
3597 case PP_KEYX_KEYSIZE_INC
:
3599 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3602 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3603 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3606 dwTemp
= 0x00000200;
3607 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&dwTemp
, sizeof(dwTemp
));
3609 case PP_ENUMCONTAINERS
:
3610 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3613 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3617 if (!open_container_key("", dwFlags
, &hKey
))
3619 SetLastError(ERROR_NO_MORE_ITEMS
);
3623 dwTemp
= *pdwDataLen
;
3624 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3625 NULL
, NULL
, NULL
, NULL
))
3627 case ERROR_MORE_DATA
:
3628 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3631 pKeyContainer
->dwEnumContainersCtr
++;
3635 case ERROR_NO_MORE_ITEMS
:
3637 SetLastError(ERROR_NO_MORE_ITEMS
);
3643 case PP_ENUMALGS_EX
:
3644 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3645 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3646 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3647 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3649 SetLastError(ERROR_NO_MORE_ITEMS
);
3653 if (dwParam
== PP_ENUMALGS
) {
3654 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3655 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3656 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3658 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3659 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3660 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3661 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3662 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3663 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3664 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3665 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3668 return copy_param(pbData
, pdwDataLen
, (CONST BYTE
*)&provEnumalgs
,
3669 sizeof(PROV_ENUMALGS
));
3671 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3672 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3673 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3675 return copy_param(pbData
, pdwDataLen
,
3676 (CONST BYTE
*)&aProvEnumAlgsEx
3677 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3678 sizeof(PROV_ENUMALGS_EX
));
3681 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3682 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3685 /* MSDN: Unknown parameter number in dwParam */
3686 SetLastError(NTE_BAD_TYPE
);
3691 /******************************************************************************
3692 * CPDeriveKey (RSAENH.@)
3694 * Derives a key from a hash value.
3697 * hProv [I] Key container for which a key is to be generated.
3698 * Algid [I] Crypto algorithm identifier for the key to be generated.
3699 * hBaseData [I] Hash from whose value the key will be derived.
3700 * dwFlags [I] See Notes.
3701 * phKey [O] The generated key.
3709 * - CRYPT_EXPORTABLE: Key can be exported.
3710 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3711 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3713 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3714 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3716 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3717 CRYPTHASH
*pCryptHash
;
3718 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3721 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3722 hBaseData
, dwFlags
, phKey
);
3724 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3726 SetLastError(NTE_BAD_UID
);
3730 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3731 (OBJECTHDR
**)&pCryptHash
))
3733 SetLastError(NTE_BAD_HASH
);
3739 SetLastError(ERROR_INVALID_PARAMETER
);
3743 switch (GET_ALG_CLASS(Algid
))
3745 case ALG_CLASS_DATA_ENCRYPT
:
3746 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3747 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3750 * We derive the key material from the hash.
3751 * If the hash value is not large enough for the claimed key, we have to construct
3752 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3754 dwLen
= RSAENH_MAX_HASH_SIZE
;
3755 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3757 if (dwLen
< pCryptKey
->dwKeyLen
) {
3758 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3759 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3762 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3764 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3765 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3766 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3769 init_hash(pCryptHash
);
3770 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3771 finalize_hash(pCryptHash
);
3772 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3774 init_hash(pCryptHash
);
3775 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
3776 finalize_hash(pCryptHash
);
3777 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
3778 pCryptHash
->dwHashSize
);
3780 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
3783 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
3784 RSAENH_MIN(pCryptKey
->dwKeyLen
, sizeof(pCryptKey
->abKeyValue
)));
3787 case ALG_CLASS_MSG_ENCRYPT
:
3788 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
3789 (OBJECTHDR
**)&pMasterKey
))
3791 SetLastError(NTE_FAIL
); /* FIXME error code */
3797 /* See RFC 2246, chapter 6.3 Key calculation */
3798 case CALG_SCHANNEL_ENC_KEY
:
3799 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
3800 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
3802 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3803 memcpy(pCryptKey
->abKeyValue
,
3804 pCryptHash
->abHashValue
+ (
3805 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3806 ((dwFlags
& CRYPT_SERVER
) ?
3807 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
3808 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
3809 memcpy(pCryptKey
->abInitVector
,
3810 pCryptHash
->abHashValue
+ (
3811 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
3812 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
3813 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
3814 pCryptKey
->dwBlockLen
);
3817 case CALG_SCHANNEL_MAC_KEY
:
3818 *phKey
= new_key(hProv
, Algid
,
3819 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
3821 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3822 memcpy(pCryptKey
->abKeyValue
,
3823 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
3824 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
3825 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
3829 SetLastError(NTE_BAD_ALGID
);
3835 SetLastError(NTE_BAD_ALGID
);
3839 setup_key(pCryptKey
);
3843 /******************************************************************************
3844 * CPGetUserKey (RSAENH.@)
3846 * Returns a handle to the user's private key-exchange- or signature-key.
3849 * hProv [I] The key container from which a user key is requested.
3850 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
3851 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
3858 * A newly created key container does not contain private user key. Create them with CPGenKey.
3860 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
3862 KEYCONTAINER
*pKeyContainer
;
3864 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
3866 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
3867 (OBJECTHDR
**)&pKeyContainer
))
3869 /* MSDN: hProv not containing valid context handle */
3870 SetLastError(NTE_BAD_UID
);
3876 case AT_KEYEXCHANGE
:
3877 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
3882 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
3887 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3890 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3892 /* MSDN: dwKeySpec parameter specifies nonexistent key */
3893 SetLastError(NTE_NO_KEY
);
3900 /******************************************************************************
3901 * CPHashData (RSAENH.@)
3903 * Updates a hash object with the given data.
3906 * hProv [I] Key container to which the hash object belongs.
3907 * hHash [I] Hash object which is to be updated.
3908 * pbData [I] Pointer to data with which the hash object is to be updated.
3909 * dwDataLen [I] Length of the data.
3910 * dwFlags [I] Currently none defined.
3917 * The actual hash value is queried with CPGetHashParam, which will finalize
3918 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
3920 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbData
,
3921 DWORD dwDataLen
, DWORD dwFlags
)
3923 CRYPTHASH
*pCryptHash
;
3925 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
3926 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
3930 SetLastError(NTE_BAD_FLAGS
);
3934 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3935 (OBJECTHDR
**)&pCryptHash
))
3937 SetLastError(NTE_BAD_HASH
);
3941 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
3943 SetLastError(NTE_BAD_ALGID
);
3947 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
3949 SetLastError(NTE_BAD_HASH_STATE
);
3953 update_hash(pCryptHash
, pbData
, dwDataLen
);
3957 /******************************************************************************
3958 * CPHashSessionKey (RSAENH.@)
3960 * Updates a hash object with the binary representation of a symmetric key.
3963 * hProv [I] Key container to which the hash object belongs.
3964 * hHash [I] Hash object which is to be updated.
3965 * hKey [I] The symmetric key, whose binary value will be added to the hash.
3966 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
3972 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
3975 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
3979 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
3981 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
3982 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
3984 SetLastError(NTE_BAD_KEY
);
3988 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
3989 SetLastError(NTE_BAD_FLAGS
);
3993 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
3994 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
3995 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
3996 bTemp
= abKeyValue
[i
];
3997 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
3998 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4002 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4005 /******************************************************************************
4006 * CPReleaseContext (RSAENH.@)
4008 * Release a key container.
4011 * hProv [I] Key container to be released.
4012 * dwFlags [I] Currently none defined.
4018 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4020 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4022 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4024 /* MSDN: hProv not containing valid context handle */
4025 SetLastError(NTE_BAD_UID
);
4030 SetLastError(NTE_BAD_FLAGS
);
4037 /******************************************************************************
4038 * CPSetHashParam (RSAENH.@)
4040 * Set a parameter of a hash object
4043 * hProv [I] The key container to which the key belongs.
4044 * hHash [I] The hash object for which a parameter is to be set.
4045 * dwParam [I] Parameter type. See Notes.
4046 * pbData [I] Pointer to the parameter value.
4047 * dwFlags [I] Currently none defined.
4054 * Currently only the HP_HMAC_INFO dwParam type is defined.
4055 * The HMAC_INFO struct will be deep copied into the hash object.
4056 * See Internet RFC 2104 for details on the HMAC algorithm.
4058 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4059 BYTE
*pbData
, DWORD dwFlags
)
4061 CRYPTHASH
*pCryptHash
;
4062 CRYPTKEY
*pCryptKey
;
4065 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4066 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4068 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4070 SetLastError(NTE_BAD_UID
);
4075 SetLastError(NTE_BAD_FLAGS
);
4079 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4080 (OBJECTHDR
**)&pCryptHash
))
4082 SetLastError(NTE_BAD_HASH
);
4088 free_hmac_info(pCryptHash
->pHMACInfo
);
4089 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4091 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4092 (OBJECTHDR
**)&pCryptKey
))
4094 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4098 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4099 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4101 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4102 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4105 init_hash(pCryptHash
);
4109 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4110 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4113 case HP_TLS1PRF_SEED
:
4114 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4116 case HP_TLS1PRF_LABEL
:
4117 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4120 SetLastError(NTE_BAD_TYPE
);
4125 /******************************************************************************
4126 * CPSetProvParam (RSAENH.@)
4128 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4134 /******************************************************************************
4135 * CPSignHash (RSAENH.@)
4137 * Sign a hash object
4140 * hProv [I] The key container, to which the hash object belongs.
4141 * hHash [I] The hash object to be signed.
4142 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4143 * sDescription [I] Should be NULL for security reasons.
4144 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4145 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4146 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4152 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4153 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4156 HCRYPTKEY hCryptKey
;
4157 CRYPTKEY
*pCryptKey
;
4159 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4162 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4163 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4164 dwFlags
, pbSignature
, pdwSigLen
);
4166 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4167 SetLastError(NTE_BAD_FLAGS
);
4171 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4173 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4174 (OBJECTHDR
**)&pCryptKey
))
4176 SetLastError(NTE_NO_KEY
);
4181 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4184 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4186 SetLastError(ERROR_MORE_DATA
);
4187 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4190 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4193 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4194 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4200 dwHashLen
= sizeof(DWORD
);
4201 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4203 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4204 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4207 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4211 return encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4214 /******************************************************************************
4215 * CPVerifySignature (RSAENH.@)
4217 * Verify the signature of a hash object.
4220 * hProv [I] The key container, to which the hash belongs.
4221 * hHash [I] The hash for which the signature is verified.
4222 * pbSignature [I] The binary signature.
4223 * dwSigLen [I] Length of the signature BLOB.
4224 * hPubKey [I] Public key used to verify the signature.
4225 * sDescription [I] Should be NULL for security reasons.
4226 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4229 * Success: TRUE (Signature is valid)
4230 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4232 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, CONST BYTE
*pbSignature
,
4233 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4236 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4237 CRYPTKEY
*pCryptKey
;
4240 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4243 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4244 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4247 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4248 SetLastError(NTE_BAD_FLAGS
);
4252 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4254 SetLastError(NTE_BAD_UID
);
4258 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4259 (OBJECTHDR
**)&pCryptKey
))
4261 SetLastError(NTE_BAD_KEY
);
4265 /* in Microsoft implementation, the signature length is checked before
4266 * the signature pointer.
4268 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4270 SetLastError(NTE_BAD_SIGNATURE
);
4274 if (!hHash
|| !pbSignature
)
4276 SetLastError(ERROR_INVALID_PARAMETER
);
4281 if (!RSAENH_CPHashData(hProv
, hHash
, (CONST BYTE
*)sDescription
,
4282 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4288 dwHashLen
= sizeof(DWORD
);
4289 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4291 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4292 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4294 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4295 if (!pbConstructed
) {
4296 SetLastError(NTE_NO_MEMORY
);
4300 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4302 SetLastError(NTE_NO_MEMORY
);
4306 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4312 if (!build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4316 if (memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4317 SetLastError(NTE_BAD_SIGNATURE
);
4323 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4324 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4328 static const WCHAR szProviderKeys
[6][116] = {
4329 { 'S','o','f','t','w','a','r','e','\\',
4330 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4331 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4332 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','B','a','s',
4333 'e',' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4334 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4335 { 'S','o','f','t','w','a','r','e','\\',
4336 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4337 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4338 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4339 'E','n','h','a','n','c','e','d',
4340 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4341 'o','v','i','d','e','r',' ','v','1','.','0',0 },
4342 { 'S','o','f','t','w','a','r','e','\\',
4343 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4344 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4345 'i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ','S','t','r','o','n','g',
4346 ' ','C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r',
4347 'o','v','i','d','e','r',0 },
4348 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4349 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4350 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4351 'R','S','A',' ','S','C','h','a','n','n','e','l',' ',
4352 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4353 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4354 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4355 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4356 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4357 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',0 },
4358 { 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
4359 'C','r','y','p','t','o','g','r','a','p','h','y','\\','D','e','f','a','u','l','t','s','\\',
4360 'P','r','o','v','i','d','e','r','\\','M','i','c','r','o','s','o','f','t',' ',
4361 'E','n','h','a','n','c','e','d',' ','R','S','A',' ','a','n','d',' ','A','E','S',' ',
4362 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ','P','r','o','v','i','d','e','r',
4363 ' ','(','P','r','o','t','o','t','y','p','e',')',0 }
4365 static const WCHAR szDefaultKeys
[3][65] = {
4366 { 'S','o','f','t','w','a','r','e','\\',
4367 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4368 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4369 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','0','1',0 },
4370 { 'S','o','f','t','w','a','r','e','\\',
4371 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4372 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4373 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','1','2',0 },
4374 { 'S','o','f','t','w','a','r','e','\\',
4375 'M','i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r',
4376 'a','p','h','y','\\','D','e','f','a','u','l','t','s','\\','P','r','o','v',
4377 'i','d','e','r',' ','T','y','p','e','s','\\','T','y','p','e',' ','0','2','4',0 }
4381 /******************************************************************************
4382 * DllRegisterServer (RSAENH.@)
4384 * Dll self registration.
4393 * Registers the following keys:
4394 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4395 * Microsoft Base Cryptographic Provider v1.0
4396 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4397 * Microsoft Enhanced Cryptographic Provider
4398 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider\
4399 * Microsoft Strong Cryptographpic Provider
4400 * - HKLM\Software\Microsoft\Cryptography\Defaults\Provider Types\Type 001
4402 HRESULT WINAPI
DllRegisterServer(void)
4409 for (i
=0; i
<6; i
++) {
4410 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szProviderKeys
[i
], 0, NULL
,
4411 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
4413 if (apiRet
== ERROR_SUCCESS
)
4415 if (dp
== REG_CREATED_NEW_KEY
)
4417 static const WCHAR szImagePath
[] = { 'I','m','a','g','e',' ','P','a','t','h',0 };
4418 static const WCHAR szRSABase
[] = { 'r','s','a','e','n','h','.','d','l','l',0 };
4419 static const WCHAR szType
[] = { 'T','y','p','e',0 };
4420 static const WCHAR szSignature
[] = { 'S','i','g','n','a','t','u','r','e',0 };
4426 type
=PROV_RSA_SCHANNEL
;
4437 RegSetValueExW(key
, szImagePath
, 0, REG_SZ
, (const BYTE
*)szRSABase
,
4438 (lstrlenW(szRSABase
) + 1) * sizeof(WCHAR
));
4439 RegSetValueExW(key
, szType
, 0, REG_DWORD
, (LPBYTE
)&type
, sizeof(type
));
4440 RegSetValueExW(key
, szSignature
, 0, REG_BINARY
, (LPBYTE
)&sign
, sizeof(sign
));
4446 for (i
=0; i
<3; i
++) {
4447 apiRet
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[i
], 0, NULL
,
4448 REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &key
, &dp
);
4449 if (apiRet
== ERROR_SUCCESS
)
4451 if (dp
== REG_CREATED_NEW_KEY
)
4453 static const WCHAR szName
[] = { 'N','a','m','e',0 };
4454 static const WCHAR szRSAName
[3][54] = {
4455 { 'M','i','c','r','o','s','o','f','t',' ', 'B','a','s','e',' ',
4456 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4457 'P','r','o','v','i','d','e','r',' ','v','1','.','0',0 },
4458 { 'M','i','c','r','o','s','o','f','t',' ','R','S','A',' ',
4459 'S','C','h','a','n','n','e','l',' ',
4460 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4461 'P','r','o','v','i','d','e','r',0 },
4462 { 'M','i','c','r','o','s','o','f','t',' ','E','n','h','a','n','c','e','d',' ',
4463 'R','S','A',' ','a','n','d',' ','A','E','S',' ',
4464 'C','r','y','p','t','o','g','r','a','p','h','i','c',' ',
4465 'P','r','o','v','i','d','e','r',0 } };
4466 static const WCHAR szTypeName
[] = { 'T','y','p','e','N','a','m','e',0 };
4467 static const WCHAR szRSATypeName
[3][38] = {
4468 { 'R','S','A',' ','F','u','l','l',' ',
4469 '(','S','i','g','n','a','t','u','r','e',' ','a','n','d',' ',
4470 'K','e','y',' ','E','x','c','h','a','n','g','e',')',0 },
4471 { 'R','S','A',' ','S','C','h','a','n','n','e','l',0 },
4472 { 'R','S','A',' ','F','u','l','l',' ','a','n','d',' ','A','E','S',0 } };
4474 RegSetValueExW(key
, szName
, 0, REG_SZ
,
4475 (const BYTE
*)szRSAName
[i
], lstrlenW(szRSAName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
4476 RegSetValueExW(key
, szTypeName
, 0, REG_SZ
,
4477 (const BYTE
*)szRSATypeName
[i
], lstrlenW(szRSATypeName
[i
])*sizeof(WCHAR
)+sizeof(WCHAR
));
4483 return HRESULT_FROM_WIN32(apiRet
);
4486 /******************************************************************************
4487 * DllUnregisterServer (RSAENH.@)
4489 * Dll self unregistration.
4497 * For the relevant keys see DllRegisterServer.
4499 HRESULT WINAPI
DllUnregisterServer(void)
4501 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[0]);
4502 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[1]);
4503 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[2]);
4504 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[3]);
4505 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[4]);
4506 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szProviderKeys
[5]);
4507 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[0]);
4508 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[1]);
4509 RegDeleteKeyW(HKEY_LOCAL_MACHINE
, szDefaultKeys
[2]);