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
25 #define WIN32_NO_STATUS
27 #define COM_NO_WINDOWS_H
30 //#include "wine/port.h"
31 #include <wine/library.h>
32 #include <wine/debug.h>
38 //#include "winbase.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
49 static HINSTANCE instance
;
51 /******************************************************************************
52 * CRYPTHASH - hash objects
54 #define RSAENH_MAGIC_HASH 0x85938417u
55 #define RSAENH_MAX_HASH_SIZE 104
56 #define RSAENH_HASHSTATE_HASHING 1
57 #define RSAENH_HASHSTATE_FINISHED 2
58 typedef struct _RSAENH_TLS1PRF_PARAMS
60 CRYPT_DATA_BLOB blobLabel
;
61 CRYPT_DATA_BLOB blobSeed
;
62 } RSAENH_TLS1PRF_PARAMS
;
64 typedef struct tagCRYPTHASH
73 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
75 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
78 /******************************************************************************
79 * CRYPTKEY - key objects
81 #define RSAENH_MAGIC_KEY 0x73620457u
82 #define RSAENH_MAX_KEY_SIZE 64
83 #define RSAENH_MAX_BLOCK_SIZE 24
84 #define RSAENH_KEYSTATE_IDLE 0
85 #define RSAENH_KEYSTATE_ENCRYPTING 1
86 #define RSAENH_KEYSTATE_MASTERKEY 2
87 typedef struct _RSAENH_SCHANNEL_INFO
89 SCHANNEL_ALG saEncAlg
;
90 SCHANNEL_ALG saMACAlg
;
91 CRYPT_DATA_BLOB blobClientRandom
;
92 CRYPT_DATA_BLOB blobServerRandom
;
93 } RSAENH_SCHANNEL_INFO
;
95 typedef struct tagCRYPTKEY
104 DWORD dwEffectiveKeyLen
;
109 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
110 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
111 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
112 RSAENH_SCHANNEL_INFO siSChannelInfo
;
113 CRYPT_DATA_BLOB blobHmacKey
;
116 /******************************************************************************
117 * KEYCONTAINER - key containers
119 #define RSAENH_PERSONALITY_BASE 0u
120 #define RSAENH_PERSONALITY_STRONG 1u
121 #define RSAENH_PERSONALITY_ENHANCED 2u
122 #define RSAENH_PERSONALITY_SCHANNEL 3u
123 #define RSAENH_PERSONALITY_AES 4u
125 #define RSAENH_MAGIC_CONTAINER 0x26384993u
126 typedef struct tagKEYCONTAINER
132 DWORD dwEnumContainersCtr
;
133 CHAR szName
[MAX_PATH
];
134 CHAR szProvName
[MAX_PATH
];
135 HCRYPTKEY hKeyExchangeKeyPair
;
136 HCRYPTKEY hSignatureKeyPair
;
139 /******************************************************************************
140 * Some magic constants
142 #define RSAENH_ENCRYPT 1
143 #define RSAENH_DECRYPT 0
144 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
145 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
146 #define RSAENH_HMAC_DEF_PAD_LEN 64
147 #define RSAENH_HMAC_BLOCK_LEN 64
148 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
149 #define RSAENH_DES_STORAGE_KEYLEN 64
150 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
151 #define RSAENH_3DES112_STORAGE_KEYLEN 128
152 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
153 #define RSAENH_3DES_STORAGE_KEYLEN 192
154 #define RSAENH_MAGIC_RSA2 0x32415352
155 #define RSAENH_MAGIC_RSA1 0x31415352
156 #define RSAENH_PKC_BLOCKTYPE 0x02
157 #define RSAENH_SSL3_VERSION_MAJOR 3
158 #define RSAENH_SSL3_VERSION_MINOR 0
159 #define RSAENH_TLS1_VERSION_MAJOR 3
160 #define RSAENH_TLS1_VERSION_MINOR 1
161 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
163 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
164 /******************************************************************************
165 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
167 #define RSAENH_MAX_ENUMALGS 24
168 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
169 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
172 {CALG_RC2
, 40, 40, 56,0, 4,"RC2", 24,"RSA Data Security's RC2"},
173 {CALG_RC4
, 40, 40, 56,0, 4,"RC4", 24,"RSA Data Security's RC4"},
174 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
175 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
176 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
177 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
178 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
179 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
180 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
181 {CALG_RSA_SIGN
, 512,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
182 {CALG_RSA_KEYX
, 512,384, 1024,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
183 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
184 {0, 0, 0, 0,0, 1,"", 1,""}
187 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
188 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
189 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
190 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
191 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
192 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
193 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
194 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
195 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
196 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
197 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
198 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
199 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
200 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
201 {0, 0, 0, 0,0, 1,"", 1,""}
204 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
205 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
206 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
207 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
208 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
209 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
210 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
211 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
212 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
213 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
214 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
215 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
216 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
217 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
218 {0, 0, 0, 0,0, 1,"", 1,""}
221 {CALG_RC2
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC2", 24,"RSA Data Security's RC2"},
222 {CALG_RC4
, 128, 40, 128,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"RC4", 24,"RSA Data Security's RC4"},
223 {CALG_DES
, 56, 56, 56,RSAENH_PCT1_SSL2_SSL3_TLS1
, 4,"DES", 31,"Data Encryption Standard (DES)"},
224 {CALG_3DES_112
, 112,112, 112,RSAENH_PCT1_SSL2_SSL3_TLS1
,13,"3DES TWO KEY",19,"Two Key Triple DES"},
225 {CALG_3DES
, 168,168, 168,RSAENH_PCT1_SSL2_SSL3_TLS1
, 5,"3DES", 21,"Three Key Triple DES"},
226 {CALG_SHA
,160,160,160,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,6,"SHA-1",30,"Secure Hash Algorithm (SHA-1)"},
227 {CALG_MD5
,128,128,128,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,4,"MD5",23,"Message Digest 5 (MD5)"},
228 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
229 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
230 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_SIGN",14,"RSA Signature"},
231 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
,9,"RSA_KEYX",17,"RSA Key Exchange"},
232 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
233 {CALG_PCT1_MASTER
,128,128,128,CRYPT_FLAG_PCT1
, 12,"PCT1 MASTER",12,"PCT1 Master"},
234 {CALG_SSL2_MASTER
,40,40, 192,CRYPT_FLAG_SSL2
, 12,"SSL2 MASTER",12,"SSL2 Master"},
235 {CALG_SSL3_MASTER
,384,384,384,CRYPT_FLAG_SSL3
, 12,"SSL3 MASTER",12,"SSL3 Master"},
236 {CALG_TLS1_MASTER
,384,384,384,CRYPT_FLAG_TLS1
, 12,"TLS1 MASTER",12,"TLS1 Master"},
237 {CALG_SCHANNEL_MASTER_HASH
,0,0,-1,0, 16,"SCH MASTER HASH",21,"SChannel Master Hash"},
238 {CALG_SCHANNEL_MAC_KEY
,0,0,-1,0, 12,"SCH MAC KEY",17,"SChannel MAC Key"},
239 {CALG_SCHANNEL_ENC_KEY
,0,0,-1,0, 12,"SCH ENC KEY",24,"SChannel Encryption Key"},
240 {CALG_TLS1PRF
, 0, 0, -1,0, 9,"TLS1 PRF", 28,"TLS1 Pseudo Random Function"},
241 {0, 0, 0, 0,0, 1,"", 1,""}
244 {CALG_RC2
, 128, 40, 128,0, 4,"RC2", 24,"RSA Data Security's RC2"},
245 {CALG_RC4
, 128, 40, 128,0, 4,"RC4", 24,"RSA Data Security's RC4"},
246 {CALG_DES
, 56, 56, 56,0, 4,"DES", 31,"Data Encryption Standard (DES)"},
247 {CALG_3DES_112
, 112,112, 112,0, 13,"3DES TWO KEY",19,"Two Key Triple DES"},
248 {CALG_3DES
, 168,168, 168,0, 5,"3DES", 21,"Three Key Triple DES"},
249 {CALG_AES
, 128,128, 128,0, 4,"AES", 35,"Advanced Encryption Standard (AES)"},
250 {CALG_AES_128
, 128,128, 128,0, 8,"AES-128", 39,"Advanced Encryption Standard (AES-128)"},
251 {CALG_AES_192
, 192,192, 192,0, 8,"AES-192", 39,"Advanced Encryption Standard (AES-192)"},
252 {CALG_AES_256
, 256,256, 256,0, 8,"AES-256", 39,"Advanced Encryption Standard (AES-256)"},
253 {CALG_SHA
, 160,160, 160,CRYPT_FLAG_SIGNING
, 6,"SHA-1", 30,"Secure Hash Algorithm (SHA-1)"},
254 {CALG_SHA_256
, 256,256, 256,CRYPT_FLAG_SIGNING
, 6,"SHA-256", 30,"Secure Hash Algorithm (SHA-256)"},
255 {CALG_SHA_384
, 384,384, 384,CRYPT_FLAG_SIGNING
, 6,"SHA-384", 30,"Secure Hash Algorithm (SHA-384)"},
256 {CALG_SHA_512
, 512,512, 512,CRYPT_FLAG_SIGNING
, 6,"SHA-512", 30,"Secure Hash Algorithm (SHA-512)"},
257 {CALG_MD2
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD2", 23,"Message Digest 2 (MD2)"},
258 {CALG_MD4
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD4", 23,"Message Digest 4 (MD4)"},
259 {CALG_MD5
, 128,128, 128,CRYPT_FLAG_SIGNING
, 4,"MD5", 23,"Message Digest 5 (MD5)"},
260 {CALG_SSL3_SHAMD5
,288,288,288,0, 12,"SSL3 SHAMD5",12,"SSL3 SHAMD5"},
261 {CALG_MAC
, 0, 0, 0,0, 4,"MAC", 28,"Message Authentication Code"},
262 {CALG_RSA_SIGN
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_SIGN",14,"RSA Signature"},
263 {CALG_RSA_KEYX
,1024,384,16384,CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
,9,"RSA_KEYX",17,"RSA Key Exchange"},
264 {CALG_HMAC
, 0, 0, 0,0, 5,"HMAC", 18,"Hugo's MAC (HMAC)"},
265 {0, 0, 0, 0,0, 1,"", 1,""}
269 /******************************************************************************
270 * API forward declarations
273 RSAENH_CPGetKeyParam(
304 RSAENH_CPSetHashParam(
308 BYTE
*pbData
, DWORD dwFlags
312 RSAENH_CPGetHashParam(
322 RSAENH_CPDestroyHash(
327 static BOOL
crypt_export_key(
337 static BOOL
import_key(
356 /******************************************************************************
357 * CSP's handle table (used by all acquired key containers)
359 static struct handle_table handle_table
;
361 /******************************************************************************
364 * Initializes and destroys the handle table for the CSP's handles.
366 BOOL WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID reserved
)
370 case DLL_PROCESS_ATTACH
:
371 instance
= hInstance
;
372 DisableThreadLibraryCalls(hInstance
);
373 init_handle_table(&handle_table
);
376 case DLL_PROCESS_DETACH
:
378 destroy_handle_table(&handle_table
);
384 /******************************************************************************
385 * copy_param [Internal]
387 * Helper function that supports the standard WINAPI protocol for querying data
391 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
392 * May be NUL if the required buffer size is to be queried only.
393 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
394 * Out: Size of parameter pbParam
395 * pbParam [I] Parameter value.
396 * dwParamSize [I] Size of pbParam
399 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
400 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
402 static inline BOOL
copy_param(BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, const BYTE
*pbParam
,
407 if (dwParamSize
> *pdwBufferSize
)
409 SetLastError(ERROR_MORE_DATA
);
410 *pdwBufferSize
= dwParamSize
;
413 memcpy(pbBuffer
, pbParam
, dwParamSize
);
415 *pdwBufferSize
= dwParamSize
;
419 static inline KEYCONTAINER
* get_key_container(HCRYPTPROV hProv
)
421 KEYCONTAINER
*pKeyContainer
;
423 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
424 (OBJECTHDR
**)&pKeyContainer
))
426 SetLastError(NTE_BAD_UID
);
429 return pKeyContainer
;
432 /******************************************************************************
433 * get_algid_info [Internal]
435 * Query CSP capabilities for a given crypto algorithm.
438 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
439 * algid [I] Identifier of the crypto algorithm about which information is requested.
442 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
443 * Failure: NULL (algid not supported)
445 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
446 const PROV_ENUMALGS_EX
*iterator
;
447 KEYCONTAINER
*pKeyContainer
;
449 if (!(pKeyContainer
= get_key_container(hProv
))) return NULL
;
451 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
452 if (iterator
->aiAlgid
== algid
) return iterator
;
455 SetLastError(NTE_BAD_ALGID
);
459 /******************************************************************************
460 * copy_data_blob [Internal]
462 * deeply copies a DATA_BLOB
465 * dst [O] That's where the blob will be copied to
466 * src [I] Source blob
470 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
473 * Use free_data_blob to release resources occupied by copy_data_blob.
475 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src
)
477 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
479 SetLastError(NTE_NO_MEMORY
);
482 dst
->cbData
= src
->cbData
;
483 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
487 /******************************************************************************
488 * concat_data_blobs [Internal]
490 * Concatenates two blobs
493 * dst [O] The new blob will be copied here
494 * src1 [I] Prefix blob
495 * src2 [I] Appendix blob
499 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
502 * Release resources occupied by concat_data_blobs with free_data_blobs
504 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src1
,
505 const PCRYPT_DATA_BLOB src2
)
507 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
508 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
510 SetLastError(NTE_NO_MEMORY
);
513 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
514 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
518 /******************************************************************************
519 * free_data_blob [Internal]
521 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
524 * pBlob [I] Heap space occupied by pBlob->pbData is released
526 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
527 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
530 /******************************************************************************
531 * init_data_blob [Internal]
533 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
534 pBlob
->pbData
= NULL
;
538 /******************************************************************************
539 * free_hmac_info [Internal]
541 * Deeply free an HMAC_INFO struct.
544 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
547 * See Internet RFC 2104 for details on the HMAC algorithm.
549 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
550 if (!hmac_info
) return;
551 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
552 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
553 HeapFree(GetProcessHeap(), 0, hmac_info
);
556 /******************************************************************************
557 * copy_hmac_info [Internal]
559 * Deeply copy an HMAC_INFO struct
562 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
563 * src [I] Pointer to the HMAC_INFO struct to be copied.
570 * See Internet RFC 2104 for details on the HMAC algorithm.
572 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
573 if (!src
) return FALSE
;
574 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
575 if (!*dst
) return FALSE
;
577 (*dst
)->pbInnerString
= NULL
;
578 (*dst
)->pbOuterString
= NULL
;
579 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
580 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
581 if (!(*dst
)->pbInnerString
) {
582 free_hmac_info(*dst
);
585 if (src
->cbInnerString
)
586 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
588 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
589 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
590 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
591 if (!(*dst
)->pbOuterString
) {
592 free_hmac_info(*dst
);
595 if (src
->cbOuterString
)
596 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
598 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
602 /******************************************************************************
603 * destroy_hash [Internal]
605 * Destructor for hash objects
608 * pCryptHash [I] Pointer to the hash object to be destroyed.
609 * Will be invalid after function returns!
611 static void destroy_hash(OBJECTHDR
*pObject
)
613 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
615 free_hmac_info(pCryptHash
->pHMACInfo
);
616 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
617 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
618 HeapFree(GetProcessHeap(), 0, pCryptHash
);
621 /******************************************************************************
622 * init_hash [Internal]
624 * Initialize (or reset) a hash object
627 * pCryptHash [I] The hash object to be initialized.
629 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
632 switch (pCryptHash
->aiAlgid
)
635 if (pCryptHash
->pHMACInfo
) {
636 const PROV_ENUMALGS_EX
*pAlgInfo
;
638 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
639 if (!pAlgInfo
) return FALSE
;
640 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
641 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
642 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
643 pCryptHash
->pHMACInfo
->pbInnerString
,
644 pCryptHash
->pHMACInfo
->cbInnerString
);
649 dwLen
= sizeof(DWORD
);
650 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
651 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
652 pCryptHash
->dwHashSize
>>= 3;
656 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
660 /******************************************************************************
661 * update_hash [Internal]
663 * Hashes the given data and updates the hash object's state accordingly
666 * pCryptHash [I] Hash object to be updated.
667 * pbData [I] Pointer to data stream to be hashed.
668 * dwDataLen [I] Length of data stream.
670 static inline void update_hash(CRYPTHASH
*pCryptHash
, const BYTE
*pbData
, DWORD dwDataLen
)
674 switch (pCryptHash
->aiAlgid
)
677 if (pCryptHash
->pHMACInfo
)
678 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
683 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
685 memcpy(pbTemp
, pbData
, dwDataLen
);
686 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
687 pbTemp
, &dwDataLen
, dwDataLen
);
688 HeapFree(GetProcessHeap(), 0, pbTemp
);
692 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
696 /******************************************************************************
697 * finalize_hash [Internal]
699 * Finalizes the hash, after all data has been hashed with update_hash.
700 * No additional data can be hashed afterwards until the hash gets initialized again.
703 * pCryptHash [I] Hash object to be finalized.
705 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
708 switch (pCryptHash
->aiAlgid
)
711 if (pCryptHash
->pHMACInfo
) {
712 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
714 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
715 pCryptHash
->abHashValue
);
716 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
717 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
718 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
719 pCryptHash
->pHMACInfo
->pbOuterString
,
720 pCryptHash
->pHMACInfo
->cbOuterString
);
721 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
722 abHashValue
, pCryptHash
->dwHashSize
);
723 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
724 pCryptHash
->abHashValue
);
730 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
731 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
735 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
739 /******************************************************************************
740 * destroy_key [Internal]
742 * Destructor for key objects
745 * pCryptKey [I] Pointer to the key object to be destroyed.
746 * Will be invalid after function returns!
748 static void destroy_key(OBJECTHDR
*pObject
)
750 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
752 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
753 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
754 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
755 free_data_blob(&pCryptKey
->blobHmacKey
);
756 HeapFree(GetProcessHeap(), 0, pCryptKey
);
759 /******************************************************************************
760 * setup_key [Internal]
762 * Initialize (or reset) a key object
765 * pCryptKey [I] The key object to be initialized.
767 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
768 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
769 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
770 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
771 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
772 pCryptKey
->abKeyValue
);
775 /******************************************************************************
778 * Creates a new key object without assigning the actual binary key value.
779 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
782 * hProv [I] Handle to the provider to which the created key will belong.
783 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
784 * dwFlags [I] Upper 16 bits give the key length.
785 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
787 * ppCryptKey [O] Pointer to the created key
790 * Success: Handle to the created key.
791 * Failure: INVALID_HANDLE_VALUE
793 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
797 DWORD dwKeyLen
= HIWORD(dwFlags
), bKeyLen
= dwKeyLen
;
798 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
803 * Retrieve the CSP's capabilities for the given ALG_ID value
805 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
806 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
808 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo
->szName
),
811 * Assume the default key length, if none is specified explicitly
813 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
816 * Check if the requested key length is supported by the current CSP.
817 * Adjust key length's for DES algorithms.
821 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
822 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
824 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
825 SetLastError(NTE_BAD_FLAGS
);
826 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
831 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
832 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
834 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
835 SetLastError(NTE_BAD_FLAGS
);
836 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
841 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
842 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
844 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
845 SetLastError(NTE_BAD_FLAGS
);
846 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
851 /* Avoid the key length check for HMAC keys, which have unlimited
859 TRACE("missing key len for CALG_AES\n");
860 SetLastError(NTE_BAD_ALGID
);
861 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
866 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
867 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
869 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen
,
870 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
871 SetLastError(NTE_BAD_DATA
);
872 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
876 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
877 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
878 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
880 KEYCONTAINER
*pKeyContainer
= get_key_container(hProv
);
881 pCryptKey
->aiAlgid
= aiAlgid
;
882 pCryptKey
->hProv
= hProv
;
883 pCryptKey
->dwModeBits
= 0;
884 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
886 if (dwFlags
& CRYPT_EXPORTABLE
)
887 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
888 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
889 pCryptKey
->dwEffectiveKeyLen
= 0;
892 * For compatibility reasons a 40 bit key on the Enhanced
893 * provider will not have salt
895 if (pKeyContainer
->dwPersonality
== RSAENH_PERSONALITY_ENHANCED
896 && (aiAlgid
== CALG_RC2
|| aiAlgid
== CALG_RC4
)
897 && (dwFlags
& CRYPT_CREATE_SALT
) && dwKeyLen
== 40)
898 pCryptKey
->dwSaltLen
= 0;
899 else if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
900 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
902 pCryptKey
->dwSaltLen
= 0;
903 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
904 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
905 memset(&pCryptKey
->siSChannelInfo
.saEncAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saEncAlg
));
906 memset(&pCryptKey
->siSChannelInfo
.saMACAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saMACAlg
));
907 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
908 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
909 init_data_blob(&pCryptKey
->blobHmacKey
);
913 case CALG_PCT1_MASTER
:
914 case CALG_SSL2_MASTER
:
915 case CALG_SSL3_MASTER
:
916 case CALG_TLS1_MASTER
:
918 pCryptKey
->dwBlockLen
= 0;
919 pCryptKey
->dwMode
= 0;
926 pCryptKey
->dwBlockLen
= 8;
927 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
934 pCryptKey
->dwBlockLen
= 16;
935 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
940 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
941 pCryptKey
->dwMode
= 0;
945 pCryptKey
->dwBlockLen
= 0;
946 pCryptKey
->dwMode
= 0;
950 *ppCryptKey
= pCryptKey
;
956 /******************************************************************************
957 * map_key_spec_to_key_pair_name [Internal]
959 * Returns the name of the registry value associated with a key spec.
962 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
965 * Success: Name of registry value.
968 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
975 szValueName
= "KeyExchangeKeyPair";
978 szValueName
= "SignatureKeyPair";
981 WARN("invalid key spec %d\n", dwKeySpec
);
987 /******************************************************************************
988 * store_key_pair [Internal]
990 * Stores a key pair to the registry
993 * hCryptKey [I] Handle to the key to be stored
994 * hKey [I] Registry key where the key pair is to be stored
995 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
996 * dwFlags [I] Flags for protecting the key
998 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
1001 DATA_BLOB blobIn
, blobOut
;
1006 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1008 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1009 (OBJECTHDR
**)&pKey
))
1011 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
1013 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1016 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
1019 blobIn
.pbData
= pbKey
;
1020 blobIn
.cbData
= dwLen
;
1022 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1025 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
1026 blobOut
.pbData
, blobOut
.cbData
);
1027 LocalFree(blobOut
.pbData
);
1030 HeapFree(GetProcessHeap(), 0, pbKey
);
1036 /******************************************************************************
1037 * map_key_spec_to_permissions_name [Internal]
1039 * Returns the name of the registry value associated with the permissions for
1043 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1046 * Success: Name of registry value.
1049 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1055 case AT_KEYEXCHANGE
:
1056 szValueName
= "KeyExchangePermissions";
1059 szValueName
= "SignaturePermissions";
1062 WARN("invalid key spec %d\n", dwKeySpec
);
1068 /******************************************************************************
1069 * store_key_permissions [Internal]
1071 * Stores a key's permissions to the registry
1074 * hCryptKey [I] Handle to the key whose permissions are to be stored
1075 * hKey [I] Registry key where the key permissions are to be stored
1076 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1078 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1083 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1085 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1086 (OBJECTHDR
**)&pKey
))
1087 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1088 (BYTE
*)&pKey
->dwPermissions
,
1089 sizeof(pKey
->dwPermissions
));
1092 /******************************************************************************
1093 * create_container_key [Internal]
1095 * Creates the registry key for a key container's persistent storage.
1098 * pKeyContainer [I] Pointer to the key container
1099 * sam [I] Desired registry access
1100 * phKey [O] Returned key
1102 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1104 CHAR szRSABase
[MAX_PATH
];
1107 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1109 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1110 hRootKey
= HKEY_LOCAL_MACHINE
;
1112 hRootKey
= HKEY_CURRENT_USER
;
1114 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1115 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1116 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1117 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1121 /******************************************************************************
1122 * open_container_key [Internal]
1124 * Opens a key container's persistent storage for reading.
1127 * pszContainerName [I] Name of the container to be opened. May be the empty
1128 * string if the parent key of all containers is to be
1130 * dwFlags [I] Flags indicating which keyset to be opened.
1131 * phKey [O] Returned key
1133 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, REGSAM access
, HKEY
*phKey
)
1135 CHAR szRSABase
[MAX_PATH
];
1138 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1140 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1141 hRootKey
= HKEY_LOCAL_MACHINE
;
1143 hRootKey
= HKEY_CURRENT_USER
;
1145 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1146 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1147 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, access
, phKey
) ==
1151 /******************************************************************************
1152 * delete_container_key [Internal]
1154 * Deletes a key container's persistent storage.
1157 * pszContainerName [I] Name of the container to be opened.
1158 * dwFlags [I] Flags indicating which keyset to be opened.
1160 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1162 CHAR szRegKey
[MAX_PATH
];
1164 if (snprintf(szRegKey
, MAX_PATH
, RSAENH_REGKEY
, pszContainerName
) >= MAX_PATH
) {
1165 SetLastError(NTE_BAD_KEYSET_PARAM
);
1169 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1170 hRootKey
= HKEY_LOCAL_MACHINE
;
1172 hRootKey
= HKEY_CURRENT_USER
;
1173 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1174 SetLastError(ERROR_SUCCESS
);
1177 SetLastError(NTE_BAD_KEYSET
);
1183 /******************************************************************************
1184 * store_key_container_keys [Internal]
1186 * Stores key container's keys in a persistent location.
1189 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1191 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1196 /* On WinXP, persistent keys are stored in a file located at:
1197 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1200 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1201 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1205 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1207 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1208 AT_KEYEXCHANGE
, dwFlags
);
1209 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1210 AT_SIGNATURE
, dwFlags
);
1215 /******************************************************************************
1216 * store_key_container_permissions [Internal]
1218 * Stores key container's key permissions in a persistent location.
1221 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1224 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1228 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1230 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1232 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1238 /******************************************************************************
1239 * release_key_container_keys [Internal]
1241 * Releases key container's keys.
1244 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1246 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1248 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1250 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1254 /******************************************************************************
1255 * destroy_key_container [Internal]
1257 * Destructor for key containers.
1260 * pObjectHdr [I] Pointer to the key container to be destroyed.
1262 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1264 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1266 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1268 store_key_container_keys(pKeyContainer
);
1269 store_key_container_permissions(pKeyContainer
);
1270 release_key_container_keys(pKeyContainer
);
1273 release_key_container_keys(pKeyContainer
);
1274 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1277 /******************************************************************************
1278 * new_key_container [Internal]
1280 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1281 * of the CSP is determined via the pVTable->pszProvName string.
1284 * pszContainerName [I] Name of the key container.
1285 * pVTable [I] Callback functions and context info provided by the OS
1288 * Success: Handle to the new key container.
1289 * Failure: INVALID_HANDLE_VALUE
1291 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1293 KEYCONTAINER
*pKeyContainer
;
1294 HCRYPTPROV hKeyContainer
;
1296 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1297 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1298 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1300 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1301 pKeyContainer
->dwFlags
= dwFlags
;
1302 pKeyContainer
->dwEnumAlgsCtr
= 0;
1303 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1304 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1305 if (pVTable
&& pVTable
->pszProvName
) {
1306 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1307 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1308 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1309 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1310 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1311 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1312 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1313 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
) ||
1314 !strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_XP_A
)) {
1315 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1317 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1321 /* The new key container has to be inserted into the CSP immediately
1322 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1323 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1326 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1331 return hKeyContainer
;
1334 /******************************************************************************
1335 * read_key_value [Internal]
1337 * Reads a key pair value from the registry
1340 * hKeyContainer [I] Crypt provider to use to import the key
1341 * hKey [I] Registry key from which to read the key pair
1342 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1343 * dwFlags [I] Flags for unprotecting the key
1344 * phCryptKey [O] Returned key
1346 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1349 DWORD dwValueType
, dwLen
;
1351 DATA_BLOB blobIn
, blobOut
;
1354 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1356 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1359 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1362 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1365 blobIn
.pbData
= pbKey
;
1366 blobIn
.cbData
= dwLen
;
1368 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1371 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1373 LocalFree(blobOut
.pbData
);
1376 HeapFree(GetProcessHeap(), 0, pbKey
);
1383 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1384 (OBJECTHDR
**)&pKey
))
1386 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1388 dwLen
= sizeof(pKey
->dwPermissions
);
1389 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1390 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1397 /******************************************************************************
1398 * read_key_container [Internal]
1400 * Tries to read the persistent state of the key container (mainly the signature
1401 * and key exchange private keys) given by pszContainerName.
1404 * pszContainerName [I] Name of the key container to read from the registry
1405 * pVTable [I] Pointer to context data provided by the operating system
1408 * Success: Handle to the key container read from the registry
1409 * Failure: INVALID_HANDLE_VALUE
1411 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1414 KEYCONTAINER
*pKeyContainer
;
1415 HCRYPTPROV hKeyContainer
;
1416 HCRYPTKEY hCryptKey
;
1418 if (!open_container_key(pszContainerName
, dwFlags
, KEY_READ
, &hKey
))
1420 SetLastError(NTE_BAD_KEYSET
);
1421 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1424 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1425 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1427 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1428 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1430 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1431 (OBJECTHDR
**)&pKeyContainer
))
1432 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1434 /* read_key_value calls import_key, which calls import_private_key,
1435 * which implicitly installs the key value into the appropriate key
1436 * container key. Thus the ref count is incremented twice, once for
1437 * the output key value, and once for the implicit install, and needs
1438 * to be decremented to balance the two.
1440 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1441 dwProtectFlags
, &hCryptKey
))
1442 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1443 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1444 dwProtectFlags
, &hCryptKey
))
1445 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1448 return hKeyContainer
;
1451 /******************************************************************************
1452 * build_hash_signature [Internal]
1454 * Builds a padded version of a hash to match the length of the RSA key modulus.
1457 * pbSignature [O] The padded hash object is stored here.
1458 * dwLen [I] Length of the pbSignature buffer.
1459 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1460 * abHashValue [I] The value of the hash object.
1461 * dwHashLen [I] Length of the hash value.
1462 * dwFlags [I] Selection of padding algorithm.
1466 * Failure: FALSE (NTE_BAD_ALGID)
1468 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1469 const BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1471 /* These prefixes are meant to be concatenated with hash values of the
1472 * respective kind to form a PKCS #7 DigestInfo. */
1473 static const struct tagOIDDescriptor
{
1476 const BYTE abOID
[19];
1477 } aOIDDescriptor
[] = {
1478 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1479 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1480 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1481 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1482 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1483 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1484 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1485 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1486 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1487 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1488 0x05, 0x00, 0x04, 0x20 } },
1489 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1490 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1491 0x05, 0x00, 0x04, 0x30 } },
1492 { CALG_SHA_512
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1493 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1494 0x05, 0x00, 0x04, 0x40 } },
1495 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1498 DWORD dwIdxOID
, i
, j
;
1500 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1501 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1504 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1505 SetLastError(NTE_BAD_ALGID
);
1509 /* Build the padded signature */
1510 if (dwFlags
& CRYPT_X931_FORMAT
) {
1511 pbSignature
[0] = 0x6b;
1512 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1513 pbSignature
[i
] = 0xbb;
1515 pbSignature
[i
++] = 0xba;
1516 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1517 pbSignature
[i
] = abHashValue
[j
];
1519 pbSignature
[i
++] = 0x33;
1520 pbSignature
[i
++] = 0xcc;
1522 pbSignature
[0] = 0x00;
1523 pbSignature
[1] = 0x01;
1524 if (dwFlags
& CRYPT_NOHASHOID
) {
1525 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1526 pbSignature
[i
] = 0xff;
1528 pbSignature
[i
++] = 0x00;
1530 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1531 pbSignature
[i
] = 0xff;
1533 pbSignature
[i
++] = 0x00;
1534 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1535 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1538 for (j
=0; j
< dwHashLen
; j
++) {
1539 pbSignature
[i
++] = abHashValue
[j
];
1546 /******************************************************************************
1549 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1550 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1551 * The pseudo random stream generated by this function is exclusive or'ed with
1552 * the data in pbBuffer.
1555 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1556 * pblobSeed [I] Seed value
1557 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1558 * dwBufferLen [I] Number of pseudo random bytes desired
1564 static BOOL
tls1_p(HCRYPTHASH hHMAC
, const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
,
1568 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1571 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1572 SetLastError(NTE_BAD_HASH
);
1576 /* compute A_1 = HMAC(seed) */
1578 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1579 finalize_hash(pHMAC
);
1580 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1583 /* compute HMAC(A_i + seed) */
1585 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1586 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1587 finalize_hash(pHMAC
);
1589 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1591 if (i
>= dwBufferLen
) break;
1592 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1594 } while (i
% pHMAC
->dwHashSize
);
1596 /* compute A_{i+1} = HMAC(A_i) */
1598 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1599 finalize_hash(pHMAC
);
1600 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1601 } while (i
< dwBufferLen
);
1606 /******************************************************************************
1607 * tls1_prf [Internal]
1609 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1612 * hProv [I] Key container used to compute the pseudo random stream
1613 * hSecret [I] Key that holds the (pre-)master secret
1614 * pblobLabel [I] Descriptive label
1615 * pblobSeed [I] Seed value
1616 * pbBuffer [O] Pseudo random numbers will be stored here
1617 * dwBufferLen [I] Number of pseudo random bytes desired
1623 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, const PCRYPT_DATA_BLOB pblobLabel
,
1624 const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
, DWORD dwBufferLen
)
1626 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1627 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1628 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1629 CRYPTKEY
*pHalfSecret
, *pSecret
;
1630 DWORD dwHalfSecretLen
;
1631 BOOL result
= FALSE
;
1632 CRYPT_DATA_BLOB blobLabelSeed
;
1634 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1635 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1637 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1638 SetLastError(NTE_FAIL
);
1642 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1644 /* concatenation of the label and the seed */
1645 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1647 /* zero out the buffer, since two random streams will be xor'ed into it. */
1648 memset(pbBuffer
, 0, dwBufferLen
);
1650 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1651 * the biggest range of valid key lengths. */
1652 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1653 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1655 /* Derive an HMAC_MD5 hash and call the helper function. */
1656 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1657 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1658 hmacInfo
.HashAlgid
= CALG_MD5
;
1659 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1660 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1662 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1663 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1664 hmacInfo
.HashAlgid
= CALG_SHA
;
1665 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1666 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1670 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1671 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1672 free_data_blob(&blobLabelSeed
);
1676 /******************************************************************************
1677 * pad_data [Internal]
1679 * Helper function for data padding according to PKCS1 #2
1682 * abData [I] The data to be padded
1683 * dwDataLen [I] Length of the data
1684 * abBuffer [O] Padded data will be stored here
1685 * dwBufferLen [I] Length of the buffer (also length of padded data)
1686 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1690 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1692 static BOOL
pad_data(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1697 /* Ensure there is enough space for PKCS1 #2 padding */
1698 if (dwDataLen
> dwBufferLen
-11) {
1699 SetLastError(NTE_BAD_LEN
);
1703 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1706 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1707 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1708 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1709 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1710 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1717 /******************************************************************************
1718 * unpad_data [Internal]
1720 * Remove the PKCS1 padding from RSA decrypted data
1723 * abData [I] The padded data
1724 * dwDataLen [I] Length of the padded data
1725 * abBuffer [O] Data without padding will be stored here
1726 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1727 * dwFlags [I] Currently none defined
1731 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1733 static BOOL
unpad_data(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1740 SetLastError(NTE_BAD_DATA
);
1743 for (i
=2; i
<dwDataLen
; i
++)
1747 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1748 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1750 SetLastError(NTE_BAD_DATA
);
1754 *dwBufferLen
= dwDataLen
- i
- 1;
1755 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1759 /******************************************************************************
1760 * CPAcquireContext (RSAENH.@)
1762 * Acquire a handle to the key container specified by pszContainer
1765 * phProv [O] Pointer to the location the acquired handle will be written to.
1766 * pszContainer [I] Name of the desired key container. See Notes
1767 * dwFlags [I] Flags. See Notes.
1768 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1775 * If pszContainer is NULL or points to a zero length string the user's login
1776 * name will be used as the key container name.
1778 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1779 * If a keyset with the given name already exists, the function fails and sets
1780 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1781 * key container does not exist, function fails and sets last error to
1784 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1785 DWORD dwFlags
, PVTableProvStruc pVTable
)
1787 CHAR szKeyContainerName
[MAX_PATH
];
1789 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1790 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1792 if (pszContainer
&& *pszContainer
)
1794 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1798 DWORD dwLen
= sizeof(szKeyContainerName
);
1799 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1802 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1805 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1808 case CRYPT_DELETEKEYSET
:
1809 return delete_container_key(szKeyContainerName
, dwFlags
);
1811 case CRYPT_NEWKEYSET
:
1812 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1813 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1815 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1816 TRACE("Can't create new keyset, already exists\n");
1817 SetLastError(NTE_EXISTS
);
1820 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1823 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1824 case CRYPT_VERIFYCONTEXT
:
1825 if (pszContainer
&& *pszContainer
) {
1826 TRACE("pszContainer should be empty\n");
1827 SetLastError(NTE_BAD_FLAGS
);
1830 *phProv
= new_key_container("", dwFlags
, pVTable
);
1834 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1835 SetLastError(NTE_BAD_FLAGS
);
1839 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1840 SetLastError(ERROR_SUCCESS
);
1847 /******************************************************************************
1848 * CPCreateHash (RSAENH.@)
1850 * CPCreateHash creates and initializes a new hash object.
1853 * hProv [I] Handle to the key container to which the new hash will belong.
1854 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1855 * hKey [I] Handle to a session key applied for keyed hashes.
1856 * dwFlags [I] Currently no flags defined. Must be zero.
1857 * phHash [O] Points to the location where a handle to the new hash will be stored.
1864 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1865 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1867 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1870 CRYPTKEY
*pCryptKey
;
1871 CRYPTHASH
*pCryptHash
;
1872 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1874 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1877 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1878 if (!peaAlgidInfo
) return FALSE
;
1882 SetLastError(NTE_BAD_FLAGS
);
1886 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1887 Algid
== CALG_TLS1PRF
)
1889 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1890 SetLastError(NTE_BAD_KEY
);
1894 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1895 SetLastError(NTE_BAD_KEY
);
1899 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1900 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1902 SetLastError(NTE_BAD_KEY
);
1905 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
1906 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
1907 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
1909 SetLastError(ERROR_INVALID_PARAMETER
);
1913 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1914 SetLastError(NTE_BAD_KEY_STATE
);
1919 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1920 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1921 if (!pCryptHash
) return FALSE
;
1923 pCryptHash
->aiAlgid
= Algid
;
1924 pCryptHash
->hKey
= hKey
;
1925 pCryptHash
->hProv
= hProv
;
1926 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1927 pCryptHash
->pHMACInfo
= NULL
;
1928 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1929 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1930 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1932 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1933 static const char keyex
[] = "key expansion";
1934 BYTE key_expansion
[sizeof keyex
];
1935 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1937 memcpy( key_expansion
, keyex
, sizeof keyex
);
1939 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1940 static const char msec
[] = "master secret";
1941 BYTE master_secret
[sizeof msec
];
1942 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1943 BYTE abKeyValue
[48];
1945 memcpy( master_secret
, msec
, sizeof msec
);
1947 /* See RFC 2246, chapter 8.1 */
1948 if (!concat_data_blobs(&blobRandom
,
1949 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1950 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1954 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1955 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1956 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1957 free_data_blob(&blobRandom
);
1960 /* See RFC 2246, chapter 6.3 */
1961 if (!concat_data_blobs(&blobRandom
,
1962 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1963 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1967 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1968 RSAENH_MAX_HASH_SIZE
);
1969 free_data_blob(&blobRandom
);
1972 return init_hash(pCryptHash
);
1975 /******************************************************************************
1976 * CPDestroyHash (RSAENH.@)
1978 * Releases the handle to a hash object. The object is destroyed if its reference
1979 * count reaches zero.
1982 * hProv [I] Handle to the key container to which the hash object belongs.
1983 * hHash [I] Handle to the hash object to be released.
1989 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1991 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1993 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1995 SetLastError(NTE_BAD_UID
);
1999 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
2001 SetLastError(NTE_BAD_HASH
);
2008 /******************************************************************************
2009 * CPDestroyKey (RSAENH.@)
2011 * Releases the handle to a key object. The object is destroyed if its reference
2012 * count reaches zero.
2015 * hProv [I] Handle to the key container to which the key object belongs.
2016 * hKey [I] Handle to the key object to be released.
2022 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
2024 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
2026 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2028 SetLastError(NTE_BAD_UID
);
2032 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
2034 SetLastError(NTE_BAD_KEY
);
2041 /******************************************************************************
2042 * CPDuplicateHash (RSAENH.@)
2044 * Clones a hash object including its current state.
2047 * hUID [I] Handle to the key container the hash belongs to.
2048 * hHash [I] Handle to the hash object to be cloned.
2049 * pdwReserved [I] Reserved. Must be NULL.
2050 * dwFlags [I] No flags are currently defined. Must be 0.
2051 * phHash [O] Handle to the cloned hash object.
2057 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2058 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2060 CRYPTHASH
*pSrcHash
, *pDestHash
;
2062 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
2063 pdwReserved
, dwFlags
, phHash
);
2065 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2067 SetLastError(NTE_BAD_UID
);
2071 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2073 SetLastError(NTE_BAD_HASH
);
2077 if (!phHash
|| pdwReserved
|| dwFlags
)
2079 SetLastError(ERROR_INVALID_PARAMETER
);
2083 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2084 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2085 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2087 *pDestHash
= *pSrcHash
;
2088 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2089 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2090 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2091 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2094 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2097 /******************************************************************************
2098 * CPDuplicateKey (RSAENH.@)
2100 * Clones a key object including its current state.
2103 * hUID [I] Handle to the key container the hash belongs to.
2104 * hKey [I] Handle to the key object to be cloned.
2105 * pdwReserved [I] Reserved. Must be NULL.
2106 * dwFlags [I] No flags are currently defined. Must be 0.
2107 * phHash [O] Handle to the cloned key object.
2113 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2114 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2116 CRYPTKEY
*pSrcKey
, *pDestKey
;
2118 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2119 pdwReserved
, dwFlags
, phKey
);
2121 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2123 SetLastError(NTE_BAD_UID
);
2127 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2129 SetLastError(NTE_BAD_KEY
);
2133 if (!phKey
|| pdwReserved
|| dwFlags
)
2135 SetLastError(ERROR_INVALID_PARAMETER
);
2139 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2140 (OBJECTHDR
**)&pDestKey
);
2141 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2143 *pDestKey
= *pSrcKey
;
2144 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2145 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2146 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2147 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2148 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2157 /******************************************************************************
2158 * CPEncrypt (RSAENH.@)
2163 * hProv [I] The key container hKey and hHash belong to.
2164 * hKey [I] The key used to encrypt the data.
2165 * hHash [I] An optional hash object for parallel hashing. See notes.
2166 * Final [I] Indicates if this is the last block of data to encrypt.
2167 * dwFlags [I] Currently no flags defined. Must be zero.
2168 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2169 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2170 * dwBufLen [I] Size of the buffer at pbData.
2177 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2178 * This is useful for message signatures.
2180 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2182 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2183 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2185 CRYPTKEY
*pCryptKey
;
2186 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2187 DWORD dwEncryptedLen
, i
, j
, k
;
2189 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2190 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2193 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2195 SetLastError(NTE_BAD_UID
);
2201 SetLastError(NTE_BAD_FLAGS
);
2205 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2207 SetLastError(NTE_BAD_KEY
);
2211 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2212 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2214 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2216 SetLastError(NTE_BAD_DATA
);
2220 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2221 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2224 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2225 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2226 SetLastError(NTE_BAD_DATA
);
2230 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2232 if (pbData
== NULL
) {
2233 *pdwDataLen
= dwEncryptedLen
;
2236 else if (dwEncryptedLen
> dwBufLen
) {
2237 *pdwDataLen
= dwEncryptedLen
;
2238 SetLastError(ERROR_MORE_DATA
);
2242 /* Pad final block with length bytes */
2243 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2244 *pdwDataLen
= dwEncryptedLen
;
2246 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2247 switch (pCryptKey
->dwMode
) {
2248 case CRYPT_MODE_ECB
:
2249 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2253 case CRYPT_MODE_CBC
:
2254 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2255 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2257 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2260 case CRYPT_MODE_CFB
:
2261 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2262 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2263 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2264 out
[j
] = in
[j
] ^ o
[0];
2265 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2266 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2267 pCryptKey
->abChainVector
[k
] = out
[j
];
2272 SetLastError(NTE_BAD_ALGID
);
2275 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2277 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2278 if (pbData
== NULL
) {
2279 *pdwDataLen
= dwBufLen
;
2282 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2283 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2284 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2285 SetLastError(NTE_BAD_KEY
);
2289 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2292 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2293 SetLastError(ERROR_MORE_DATA
);
2296 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2297 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2298 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2301 SetLastError(NTE_BAD_TYPE
);
2305 if (Final
) setup_key(pCryptKey
);
2310 /******************************************************************************
2311 * CPDecrypt (RSAENH.@)
2316 * hProv [I] The key container hKey and hHash belong to.
2317 * hKey [I] The key used to decrypt the data.
2318 * hHash [I] An optional hash object for parallel hashing. See notes.
2319 * Final [I] Indicates if this is the last block of data to decrypt.
2320 * dwFlags [I] Currently no flags defined. Must be zero.
2321 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2322 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2329 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2330 * This is useful for message signatures.
2332 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2334 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2335 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2337 CRYPTKEY
*pCryptKey
;
2338 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2342 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2343 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2345 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2347 SetLastError(NTE_BAD_UID
);
2353 SetLastError(NTE_BAD_FLAGS
);
2357 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2359 SetLastError(NTE_BAD_KEY
);
2363 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2364 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2366 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2368 SetLastError(NTE_BAD_DATA
);
2374 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2375 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2376 switch (pCryptKey
->dwMode
) {
2377 case CRYPT_MODE_ECB
:
2378 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2382 case CRYPT_MODE_CBC
:
2383 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2385 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2386 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2389 case CRYPT_MODE_CFB
:
2390 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2391 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2392 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2393 out
[j
] = in
[j
] ^ o
[0];
2394 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2395 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2396 pCryptKey
->abChainVector
[k
] = in
[j
];
2401 SetLastError(NTE_BAD_ALGID
);
2404 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2407 if (pbData
[*pdwDataLen
-1] &&
2408 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2409 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2410 BOOL padOkay
= TRUE
;
2412 /* check that every bad byte has the same value */
2413 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2414 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2417 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2419 SetLastError(NTE_BAD_DATA
);
2420 setup_key(pCryptKey
);
2425 SetLastError(NTE_BAD_DATA
);
2426 setup_key(pCryptKey
);
2431 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2432 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2433 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2434 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2435 SetLastError(NTE_BAD_KEY
);
2438 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2439 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2442 SetLastError(NTE_BAD_TYPE
);
2446 if (Final
) setup_key(pCryptKey
);
2448 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2449 if (*pdwDataLen
>dwMax
||
2450 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2456 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2457 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2459 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2460 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2463 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2464 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2468 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2470 if (*pdwDataLen
< dwDataLen
) {
2471 SetLastError(ERROR_MORE_DATA
);
2472 *pdwDataLen
= dwDataLen
;
2476 pBlobHeader
->bType
= SIMPLEBLOB
;
2477 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2478 pBlobHeader
->reserved
= 0;
2479 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2481 *pAlgid
= pPubKey
->aiAlgid
;
2483 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2484 pPubKey
->dwBlockLen
, dwFlags
))
2489 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2490 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2492 *pdwDataLen
= dwDataLen
;
2496 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2499 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2500 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2503 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2504 SetLastError(NTE_BAD_KEY
);
2508 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2510 if (*pdwDataLen
< dwDataLen
) {
2511 SetLastError(ERROR_MORE_DATA
);
2512 *pdwDataLen
= dwDataLen
;
2516 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2517 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2518 pBlobHeader
->reserved
= 0;
2519 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2521 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2522 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2524 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2525 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2527 *pdwDataLen
= dwDataLen
;
2531 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2532 BYTE
*pbData
, DWORD
*pdwDataLen
)
2534 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2535 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2538 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2539 SetLastError(NTE_BAD_KEY
);
2542 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2544 SetLastError(NTE_BAD_KEY_STATE
);
2548 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2549 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2551 if (*pdwDataLen
< dwDataLen
) {
2552 SetLastError(ERROR_MORE_DATA
);
2553 *pdwDataLen
= dwDataLen
;
2557 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2558 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2559 pBlobHeader
->reserved
= 0;
2560 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2562 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2563 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2565 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2566 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2568 *pdwDataLen
= dwDataLen
;
2572 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2575 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2576 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2577 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2580 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2582 if (*pdwDataLen
< dwDataLen
) {
2583 SetLastError(ERROR_MORE_DATA
);
2584 *pdwDataLen
= dwDataLen
;
2588 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2589 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2590 pBlobHeader
->reserved
= 0;
2591 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2593 *pKeyLen
= pCryptKey
->dwKeyLen
;
2594 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2596 *pdwDataLen
= dwDataLen
;
2599 /******************************************************************************
2600 * crypt_export_key [Internal]
2602 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2603 * by store_key_pair.
2606 * pCryptKey [I] Key to be exported.
2607 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2608 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2609 * dwFlags [I] Currently none defined.
2610 * force [I] If TRUE, the key is written no matter what the key's
2611 * permissions are. Otherwise the key's permissions are
2612 * checked before exporting.
2613 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2614 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2620 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2621 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2622 BYTE
*pbData
, DWORD
*pdwDataLen
)
2626 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2627 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2628 SetLastError(NTE_BAD_KEY
);
2633 switch ((BYTE
)dwBlobType
)
2636 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2637 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2640 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2644 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2645 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2649 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2651 case PRIVATEKEYBLOB
:
2652 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2654 case PLAINTEXTKEYBLOB
:
2655 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2658 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2663 /******************************************************************************
2664 * CPExportKey (RSAENH.@)
2666 * Export a key into a binary large object (BLOB).
2669 * hProv [I] Key container from which a key is to be exported.
2670 * hKey [I] Key to be exported.
2671 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2672 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2673 * dwFlags [I] Currently none defined.
2674 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2675 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2681 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2682 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2684 CRYPTKEY
*pCryptKey
;
2686 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2687 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2689 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2691 SetLastError(NTE_BAD_UID
);
2695 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2697 SetLastError(NTE_BAD_KEY
);
2701 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2702 pbData
, pdwDataLen
);
2705 /******************************************************************************
2706 * release_and_install_key [Internal]
2708 * Release an existing key, if present, and replaces it with a new one.
2711 * hProv [I] Key container into which the key is to be imported.
2712 * src [I] Key which will replace *dest
2713 * dest [I] Points to key to be released and replaced with src
2714 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2716 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2717 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2719 RSAENH_CPDestroyKey(hProv
, *dest
);
2720 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2723 KEYCONTAINER
*pKeyContainer
;
2725 if ((pKeyContainer
= get_key_container(hProv
)))
2727 store_key_container_keys(pKeyContainer
);
2728 store_key_container_permissions(pKeyContainer
);
2733 /******************************************************************************
2734 * import_private_key [Internal]
2736 * Import a BLOB'ed private key into a key container.
2739 * hProv [I] Key container into which the private key is to be imported.
2740 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2741 * dwDataLen [I] Length of data in buffer at pbData.
2742 * dwFlags [I] One of:
2743 * CRYPT_EXPORTABLE: the imported key is marked exportable
2744 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2745 * phKey [O] Handle to the imported key.
2749 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2750 * it's a PRIVATEKEYBLOB.
2756 static BOOL
import_private_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2757 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2759 KEYCONTAINER
*pKeyContainer
;
2760 CRYPTKEY
*pCryptKey
;
2761 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2762 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
2765 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2767 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2768 SetLastError(NTE_BAD_FLAGS
);
2771 if (!(pKeyContainer
= get_key_container(hProv
)))
2774 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
2776 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2778 SetLastError(NTE_BAD_DATA
);
2781 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
2783 ERR("unexpected magic %08x\n", pRSAPubKey
->magic
);
2784 SetLastError(NTE_BAD_DATA
);
2787 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2788 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2790 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2791 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
2793 ERR("blob too short for pub key: expect %d, got %d\n",
2794 expectedLen
, dwDataLen
);
2795 SetLastError(NTE_BAD_DATA
);
2799 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2800 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2801 setup_key(pCryptKey
);
2802 ret
= import_private_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2803 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
2805 if (dwFlags
& CRYPT_EXPORTABLE
)
2806 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2807 switch (pBlobHeader
->aiKeyAlg
)
2811 TRACE("installing signing key\n");
2812 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2815 case AT_KEYEXCHANGE
:
2817 TRACE("installing key exchange key\n");
2818 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2826 /******************************************************************************
2827 * import_public_key [Internal]
2829 * Import a BLOB'ed public key.
2833 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2834 * dwDataLen [I] Length of data in buffer at pbData.
2835 * dwFlags [I] One of:
2836 * CRYPT_EXPORTABLE: the imported key is marked exportable
2837 * phKey [O] Handle to the imported key.
2841 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2842 * it's a PUBLICKEYBLOB.
2848 static BOOL
import_public_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2849 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2851 CRYPTKEY
*pCryptKey
;
2852 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2853 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
2857 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2859 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2860 SetLastError(NTE_BAD_FLAGS
);
2864 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2865 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2866 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2868 SetLastError(NTE_BAD_DATA
);
2872 /* Since this is a public key blob, only the public key is
2873 * available, so only signature verification is possible.
2875 algID
= pBlobHeader
->aiKeyAlg
;
2876 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2877 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2878 setup_key(pCryptKey
);
2879 ret
= import_public_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2880 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2882 if (dwFlags
& CRYPT_EXPORTABLE
)
2883 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2888 /******************************************************************************
2889 * import_symmetric_key [Internal]
2891 * Import a BLOB'ed symmetric key into a key container.
2894 * hProv [I] Key container into which the symmetric key is to be imported.
2895 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2896 * dwDataLen [I] Length of data in buffer at pbData.
2897 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2898 * dwFlags [I] One of:
2899 * CRYPT_EXPORTABLE: the imported key is marked exportable
2900 * phKey [O] Handle to the imported key.
2904 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2905 * it's a SIMPLEBLOB.
2911 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2912 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2914 CRYPTKEY
*pCryptKey
, *pPubKey
;
2915 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2916 const ALG_ID
*pAlgid
= (const ALG_ID
*)(pBlobHeader
+1);
2917 const BYTE
*pbKeyStream
= (const BYTE
*)(pAlgid
+ 1);
2921 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2923 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2924 SetLastError(NTE_BAD_FLAGS
);
2927 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2928 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2930 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2934 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2936 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2940 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2941 if (!pbDecrypted
) return FALSE
;
2942 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2945 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2946 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2947 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2951 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2952 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2954 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2957 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2958 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2959 setup_key(pCryptKey
);
2960 if (dwFlags
& CRYPT_EXPORTABLE
)
2961 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2965 /******************************************************************************
2966 * import_plaintext_key [Internal]
2968 * Import a plaintext key into a key container.
2971 * hProv [I] Key container into which the symmetric key is to be imported.
2972 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2973 * dwDataLen [I] Length of data in buffer at pbData.
2974 * dwFlags [I] One of:
2975 * CRYPT_EXPORTABLE: the imported key is marked exportable
2976 * phKey [O] Handle to the imported key.
2980 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2981 * it's a PLAINTEXTKEYBLOB.
2987 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2988 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2990 CRYPTKEY
*pCryptKey
;
2991 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2992 const DWORD
*pKeyLen
= (const DWORD
*)(pBlobHeader
+ 1);
2993 const BYTE
*pbKeyStream
= (const BYTE
*)(pKeyLen
+ 1);
2995 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2997 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
3001 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
3003 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
3004 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3006 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
3008 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3009 pCryptKey
->dwKeyLen
= *pKeyLen
;
3013 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
3015 /* In order to initialize an HMAC key, the key material is hashed,
3016 * and the output of the hash function is used as the key material.
3017 * Unfortunately, the way the Crypto API is designed, we don't know
3018 * the hash algorithm yet, so we have to copy the entire key
3021 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3023 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3024 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3028 setup_key(pCryptKey
);
3029 if (dwFlags
& CRYPT_EXPORTABLE
)
3030 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3034 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3035 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3037 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3038 setup_key(pCryptKey
);
3039 if (dwFlags
& CRYPT_EXPORTABLE
)
3040 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3045 /******************************************************************************
3046 * import_key [Internal]
3048 * Import a BLOB'ed key into a key container, optionally storing the key's
3049 * value to the registry.
3052 * hProv [I] Key container into which the key is to be imported.
3053 * pbData [I] Pointer to a buffer which holds the BLOB.
3054 * dwDataLen [I] Length of data in buffer at pbData.
3055 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3056 * dwFlags [I] One of:
3057 * CRYPT_EXPORTABLE: the imported key is marked exportable
3058 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3059 * phKey [O] Handle to the imported key.
3065 static BOOL
import_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
, HCRYPTKEY hPubKey
,
3066 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
3068 KEYCONTAINER
*pKeyContainer
;
3069 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3071 if (!(pKeyContainer
= get_key_container(hProv
)))
3074 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3075 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3076 pBlobHeader
->reserved
!= 0)
3078 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3079 pBlobHeader
->reserved
);
3080 SetLastError(NTE_BAD_DATA
);
3084 /* If this is a verify-only context, the key is not persisted regardless of
3085 * fStoreKey's original value.
3087 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3088 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3089 switch (pBlobHeader
->bType
)
3091 case PRIVATEKEYBLOB
:
3092 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3096 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3100 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3103 case PLAINTEXTKEYBLOB
:
3104 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3108 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3113 /******************************************************************************
3114 * CPImportKey (RSAENH.@)
3116 * Import a BLOB'ed key into a key container.
3119 * hProv [I] Key container into which the key is to be imported.
3120 * pbData [I] Pointer to a buffer which holds the BLOB.
3121 * dwDataLen [I] Length of data in buffer at pbData.
3122 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3123 * dwFlags [I] One of:
3124 * CRYPT_EXPORTABLE: the imported key is marked exportable
3125 * phKey [O] Handle to the imported key.
3131 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3132 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3134 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3135 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3137 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3140 /******************************************************************************
3141 * CPGenKey (RSAENH.@)
3143 * Generate a key in the key container
3146 * hProv [I] Key container for which a key is to be generated.
3147 * Algid [I] Crypto algorithm identifier for the key to be generated.
3148 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3149 * phKey [O] Handle to the generated key.
3156 * Flags currently not considered.
3159 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3160 * and AT_SIGNATURE values.
3162 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3164 KEYCONTAINER
*pKeyContainer
;
3165 CRYPTKEY
*pCryptKey
;
3167 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3169 if (!(pKeyContainer
= get_key_container(hProv
)))
3171 /* MSDN: hProv not containing valid context handle */
3179 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3181 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3182 setup_key(pCryptKey
);
3183 release_and_install_key(hProv
, *phKey
,
3184 &pKeyContainer
->hSignatureKeyPair
,
3189 case AT_KEYEXCHANGE
:
3191 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3193 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3194 setup_key(pCryptKey
);
3195 release_and_install_key(hProv
, *phKey
,
3196 &pKeyContainer
->hKeyExchangeKeyPair
,
3210 case CALG_PCT1_MASTER
:
3211 case CALG_SSL2_MASTER
:
3212 case CALG_SSL3_MASTER
:
3213 case CALG_TLS1_MASTER
:
3214 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3216 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3218 case CALG_SSL3_MASTER
:
3219 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3220 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3223 case CALG_TLS1_MASTER
:
3224 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3225 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3228 setup_key(pCryptKey
);
3233 /* MSDN: Algorithm not supported specified by Algid */
3234 SetLastError(NTE_BAD_ALGID
);
3238 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3241 /******************************************************************************
3242 * CPGenRandom (RSAENH.@)
3244 * Generate a random byte stream.
3247 * hProv [I] Key container that is used to generate random bytes.
3248 * dwLen [I] Specifies the number of requested random data bytes.
3249 * pbBuffer [O] Random bytes will be stored here.
3255 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3257 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3259 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3261 /* MSDN: hProv not containing valid context handle */
3262 SetLastError(NTE_BAD_UID
);
3266 return gen_rand_impl(pbBuffer
, dwLen
);
3269 /******************************************************************************
3270 * CPGetHashParam (RSAENH.@)
3272 * Query parameters of an hash object.
3275 * hProv [I] The kea container, which the hash belongs to.
3276 * hHash [I] The hash object that is to be queried.
3277 * dwParam [I] Specifies the parameter that is to be queried.
3278 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3279 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3280 * dwFlags [I] None currently defined.
3287 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3288 * finalized if HP_HASHVALUE is queried.
3290 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3291 DWORD
*pdwDataLen
, DWORD dwFlags
)
3293 CRYPTHASH
*pCryptHash
;
3295 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3296 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3298 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3300 SetLastError(NTE_BAD_UID
);
3306 SetLastError(NTE_BAD_FLAGS
);
3310 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3311 (OBJECTHDR
**)&pCryptHash
))
3313 SetLastError(NTE_BAD_HASH
);
3319 SetLastError(ERROR_INVALID_PARAMETER
);
3326 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->aiAlgid
,
3330 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->dwHashSize
,
3334 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3335 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3336 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3339 if ( pbData
== NULL
) {
3340 *pdwDataLen
= pCryptHash
->dwHashSize
;
3344 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3346 finalize_hash(pCryptHash
);
3347 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3350 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3351 pCryptHash
->dwHashSize
);
3354 SetLastError(NTE_BAD_TYPE
);
3359 /******************************************************************************
3360 * CPSetKeyParam (RSAENH.@)
3362 * Set a parameter of a key object
3365 * hProv [I] The key container to which the key belongs.
3366 * hKey [I] The key for which a parameter is to be set.
3367 * dwParam [I] Parameter type. See Notes.
3368 * pbData [I] Pointer to the parameter value.
3369 * dwFlags [I] Currently none defined.
3376 * Defined dwParam types are:
3377 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3378 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3379 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3380 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3381 * - KP_IV: Initialization vector
3383 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3386 CRYPTKEY
*pCryptKey
;
3388 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3389 dwParam
, pbData
, dwFlags
);
3391 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3393 SetLastError(NTE_BAD_UID
);
3398 SetLastError(NTE_BAD_FLAGS
);
3402 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3404 SetLastError(NTE_BAD_KEY
);
3410 /* The MS providers only support PKCS5_PADDING */
3411 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3412 SetLastError(NTE_BAD_DATA
);
3418 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3422 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3425 case KP_PERMISSIONS
:
3427 DWORD perms
= *(DWORD
*)pbData
;
3429 if ((perms
& CRYPT_EXPORT
) &&
3430 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3432 SetLastError(NTE_BAD_DATA
);
3435 else if (!(perms
& CRYPT_EXPORT
) &&
3436 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3438 /* Clearing the export permission appears to be ignored,
3441 perms
|= CRYPT_EXPORT
;
3443 pCryptKey
->dwPermissions
= perms
;
3448 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3449 setup_key(pCryptKey
);
3453 switch (pCryptKey
->aiAlgid
) {
3457 KEYCONTAINER
*pKeyContainer
= get_key_container(pCryptKey
->hProv
);
3460 SetLastError(ERROR_INVALID_PARAMETER
);
3463 /* MSDN: the base provider always sets eleven bytes of
3466 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3468 pCryptKey
->dwSaltLen
= 11;
3469 setup_key(pCryptKey
);
3470 /* After setting the salt value if the provider is not base or
3471 * strong the salt length will be reset. */
3472 if (pKeyContainer
->dwPersonality
!= RSAENH_PERSONALITY_BASE
&&
3473 pKeyContainer
->dwPersonality
!= RSAENH_PERSONALITY_STRONG
)
3474 pCryptKey
->dwSaltLen
= 0;
3478 SetLastError(NTE_BAD_KEY
);
3485 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3487 /* salt length can't be greater than 184 bits = 24 bytes */
3488 if (blob
->cbData
> 24)
3490 SetLastError(NTE_BAD_DATA
);
3493 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3495 pCryptKey
->dwSaltLen
= blob
->cbData
;
3496 setup_key(pCryptKey
);
3500 case KP_EFFECTIVE_KEYLEN
:
3501 switch (pCryptKey
->aiAlgid
) {
3504 DWORD keylen
, deflen
;
3506 KEYCONTAINER
*pKeyContainer
= get_key_container(pCryptKey
->hProv
);
3510 SetLastError(ERROR_INVALID_PARAMETER
);
3513 keylen
= *(DWORD
*)pbData
;
3514 if (!keylen
|| keylen
> 1024)
3516 SetLastError(NTE_BAD_DATA
);
3521 * The Base provider will force the key length to default
3522 * and set an error state if a key length different from
3523 * the default is tried.
3525 deflen
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]->dwDefaultLen
;
3526 if (pKeyContainer
->dwPersonality
== RSAENH_PERSONALITY_BASE
3527 && keylen
!= deflen
)
3530 SetLastError(NTE_BAD_DATA
);
3533 pCryptKey
->dwEffectiveKeyLen
= keylen
;
3534 setup_key(pCryptKey
);
3538 SetLastError(NTE_BAD_TYPE
);
3543 case KP_SCHANNEL_ALG
:
3544 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3545 case SCHANNEL_ENC_KEY
:
3546 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3549 case SCHANNEL_MAC_KEY
:
3550 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3554 SetLastError(NTE_FAIL
); /* FIXME: error code */
3559 case KP_CLIENT_RANDOM
:
3560 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3562 case KP_SERVER_RANDOM
:
3563 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3566 SetLastError(NTE_BAD_TYPE
);
3571 /******************************************************************************
3572 * CPGetKeyParam (RSAENH.@)
3574 * Query a key parameter.
3577 * hProv [I] The key container, which the key belongs to.
3578 * hHash [I] The key object that is to be queried.
3579 * dwParam [I] Specifies the parameter that is to be queried.
3580 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3581 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3582 * dwFlags [I] None currently defined.
3589 * Defined dwParam types are:
3590 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3591 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3592 * (Currently ignored by MS CSP's - always eight)
3593 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3594 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3595 * - KP_IV: Initialization vector.
3596 * - KP_KEYLEN: Bitwidth of the key.
3597 * - KP_BLOCKLEN: Size of a block cipher block.
3598 * - KP_SALT: Salt value.
3600 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3601 DWORD
*pdwDataLen
, DWORD dwFlags
)
3603 CRYPTKEY
*pCryptKey
;
3606 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3607 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3609 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3611 SetLastError(NTE_BAD_UID
);
3616 SetLastError(NTE_BAD_FLAGS
);
3620 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3622 SetLastError(NTE_BAD_KEY
);
3629 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3630 pCryptKey
->dwBlockLen
);
3633 switch (pCryptKey
->aiAlgid
) {
3636 return copy_param(pbData
, pdwDataLen
,
3637 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
3638 pCryptKey
->dwSaltLen
);
3640 SetLastError(NTE_BAD_KEY
);
3645 dwValue
= PKCS5_PADDING
;
3646 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3649 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3650 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3652 case KP_EFFECTIVE_KEYLEN
:
3653 if (pCryptKey
->dwEffectiveKeyLen
)
3654 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3656 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3657 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3660 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3661 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3664 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3667 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwModeBits
,
3670 case KP_PERMISSIONS
:
3671 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwPermissions
,
3675 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3678 SetLastError(NTE_BAD_TYPE
);
3683 /******************************************************************************
3684 * CPGetProvParam (RSAENH.@)
3686 * Query a CSP parameter.
3689 * hProv [I] The key container that is to be queried.
3690 * dwParam [I] Specifies the parameter that is to be queried.
3691 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3692 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3693 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3699 * Defined dwParam types:
3700 * - PP_CONTAINER: Name of the key container.
3701 * - PP_NAME: Name of the cryptographic service provider.
3702 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3703 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3704 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3705 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
3707 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3708 DWORD
*pdwDataLen
, DWORD dwFlags
)
3710 KEYCONTAINER
*pKeyContainer
;
3711 PROV_ENUMALGS provEnumalgs
;
3715 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3716 * IE6 SP1 asks for it in the 'About' dialog.
3717 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3718 * to be 'don't care's. If you know anything more specific about
3719 * this provider parameter, please report to wine-devel@winehq.org */
3720 static const BYTE abWTF
[96] = {
3721 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3722 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3723 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3724 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3725 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3726 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3727 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3728 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3729 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3730 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3731 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3732 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3735 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3736 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3739 SetLastError(ERROR_INVALID_PARAMETER
);
3743 if (!(pKeyContainer
= get_key_container(hProv
)))
3745 /* MSDN: hProv not containing valid context handle */
3752 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3753 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szName
,
3754 strlen(pKeyContainer
->szName
)+1);
3757 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szProvName
,
3758 strlen(pKeyContainer
->szProvName
)+1);
3761 dwTemp
= PROV_RSA_FULL
;
3762 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3765 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3766 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3768 case PP_KEYSET_TYPE
:
3769 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3770 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3773 dwTemp
= CRYPT_SEC_DESCR
;
3774 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3776 case PP_SIG_KEYSIZE_INC
:
3777 case PP_KEYX_KEYSIZE_INC
:
3779 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3782 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3783 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3786 dwTemp
= 0x00000200;
3787 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3789 case PP_ENUMCONTAINERS
:
3790 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3793 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3797 if (!open_container_key("", dwFlags
, KEY_READ
, &hKey
))
3799 SetLastError(ERROR_NO_MORE_ITEMS
);
3803 dwTemp
= *pdwDataLen
;
3804 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3805 NULL
, NULL
, NULL
, NULL
))
3807 case ERROR_MORE_DATA
:
3808 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3811 pKeyContainer
->dwEnumContainersCtr
++;
3815 case ERROR_NO_MORE_ITEMS
:
3817 SetLastError(ERROR_NO_MORE_ITEMS
);
3823 case PP_ENUMALGS_EX
:
3824 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3825 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3826 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3827 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3829 SetLastError(ERROR_NO_MORE_ITEMS
);
3833 if (dwParam
== PP_ENUMALGS
) {
3834 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3835 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3836 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3838 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3839 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3840 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3841 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3842 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3843 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3844 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3845 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3848 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&provEnumalgs
,
3849 sizeof(PROV_ENUMALGS
));
3851 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3852 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3853 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3855 return copy_param(pbData
, pdwDataLen
,
3856 (const BYTE
*)&aProvEnumAlgsEx
3857 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3858 sizeof(PROV_ENUMALGS_EX
));
3861 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3862 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3864 case PP_KEYSET_SEC_DESCR
:
3866 SECURITY_DESCRIPTOR
*sd
;
3867 DWORD err
, len
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
3869 if (!open_container_key(pKeyContainer
->szName
, flags
, KEY_READ
, &hKey
))
3871 SetLastError(NTE_BAD_KEYSET
);
3875 err
= GetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, NULL
, NULL
, NULL
, NULL
, (void **)&sd
);
3883 len
= GetSecurityDescriptorLength(sd
);
3884 if (*pdwDataLen
>= len
) memcpy(pbData
, sd
, len
);
3885 else SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3893 /* MSDN: Unknown parameter number in dwParam */
3894 SetLastError(NTE_BAD_TYPE
);
3899 /******************************************************************************
3900 * CPDeriveKey (RSAENH.@)
3902 * Derives a key from a hash value.
3905 * hProv [I] Key container for which a key is to be generated.
3906 * Algid [I] Crypto algorithm identifier for the key to be generated.
3907 * hBaseData [I] Hash from whose value the key will be derived.
3908 * dwFlags [I] See Notes.
3909 * phKey [O] The generated key.
3917 * - CRYPT_EXPORTABLE: Key can be exported.
3918 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3919 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3921 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3922 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3924 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3925 CRYPTHASH
*pCryptHash
;
3926 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3929 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3930 hBaseData
, dwFlags
, phKey
);
3932 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3934 SetLastError(NTE_BAD_UID
);
3938 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3939 (OBJECTHDR
**)&pCryptHash
))
3941 SetLastError(NTE_BAD_HASH
);
3947 SetLastError(ERROR_INVALID_PARAMETER
);
3951 switch (GET_ALG_CLASS(Algid
))
3953 case ALG_CLASS_DATA_ENCRYPT
:
3955 int need_padding
, copy_len
;
3956 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3957 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3960 * We derive the key material from the hash.
3961 * If the hash value is not large enough for the claimed key, we have to construct
3962 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3964 dwLen
= RSAENH_MAX_HASH_SIZE
;
3965 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3968 * The usage of padding seems to vary from algorithm to algorithm.
3969 * For now the only different case found was for AES with 128 bit key.
3974 /* To reduce the chance of regressions we will only deviate
3975 * from the old behavior for the tested hash lengths */
3976 if (dwLen
== 16 || dwLen
== 20)
3982 need_padding
= dwLen
< pCryptKey
->dwKeyLen
;
3985 copy_len
= pCryptKey
->dwKeyLen
;
3988 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3989 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3992 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3994 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3995 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3996 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3999 init_hash(pCryptHash
);
4000 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
4001 finalize_hash(pCryptHash
);
4002 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
4004 init_hash(pCryptHash
);
4005 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
4006 finalize_hash(pCryptHash
);
4007 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
4008 pCryptHash
->dwHashSize
);
4010 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
4013 * Padding was not required, we have more hash than needed.
4014 * Do we need to use the remaining hash as salt?
4016 else if((dwFlags
& CRYPT_CREATE_SALT
) &&
4017 (Algid
== CALG_RC2
|| Algid
== CALG_RC4
))
4019 copy_len
+= pCryptKey
->dwSaltLen
;
4022 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
4023 RSAENH_MIN(copy_len
, sizeof(pCryptKey
->abKeyValue
)));
4026 case ALG_CLASS_MSG_ENCRYPT
:
4027 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4028 (OBJECTHDR
**)&pMasterKey
))
4030 SetLastError(NTE_FAIL
); /* FIXME error code */
4036 /* See RFC 2246, chapter 6.3 Key calculation */
4037 case CALG_SCHANNEL_ENC_KEY
:
4038 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
4039 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
4041 SetLastError(NTE_BAD_FLAGS
);
4044 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
4045 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
4047 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4048 memcpy(pCryptKey
->abKeyValue
,
4049 pCryptHash
->abHashValue
+ (
4050 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4051 ((dwFlags
& CRYPT_SERVER
) ?
4052 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
4053 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
4054 memcpy(pCryptKey
->abInitVector
,
4055 pCryptHash
->abHashValue
+ (
4056 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4057 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
4058 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
4059 pCryptKey
->dwBlockLen
);
4062 case CALG_SCHANNEL_MAC_KEY
:
4063 *phKey
= new_key(hProv
, Algid
,
4064 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
4066 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4067 memcpy(pCryptKey
->abKeyValue
,
4068 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
4069 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
4070 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
4074 SetLastError(NTE_BAD_ALGID
);
4080 SetLastError(NTE_BAD_ALGID
);
4084 setup_key(pCryptKey
);
4088 /******************************************************************************
4089 * CPGetUserKey (RSAENH.@)
4091 * Returns a handle to the user's private key-exchange- or signature-key.
4094 * hProv [I] The key container from which a user key is requested.
4095 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4096 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4103 * A newly created key container does not contain private user key. Create them with CPGenKey.
4105 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4107 KEYCONTAINER
*pKeyContainer
;
4109 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4111 if (!(pKeyContainer
= get_key_container(hProv
)))
4113 /* MSDN: hProv not containing valid context handle */
4119 case AT_KEYEXCHANGE
:
4120 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4125 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4130 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4133 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4135 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4136 SetLastError(NTE_NO_KEY
);
4143 /******************************************************************************
4144 * CPHashData (RSAENH.@)
4146 * Updates a hash object with the given data.
4149 * hProv [I] Key container to which the hash object belongs.
4150 * hHash [I] Hash object which is to be updated.
4151 * pbData [I] Pointer to data with which the hash object is to be updated.
4152 * dwDataLen [I] Length of the data.
4153 * dwFlags [I] Currently none defined.
4160 * The actual hash value is queried with CPGetHashParam, which will finalize
4161 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4163 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbData
,
4164 DWORD dwDataLen
, DWORD dwFlags
)
4166 CRYPTHASH
*pCryptHash
;
4168 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4169 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4171 if (dwFlags
& ~CRYPT_USERDATA
)
4173 SetLastError(NTE_BAD_FLAGS
);
4177 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4178 (OBJECTHDR
**)&pCryptHash
))
4180 SetLastError(NTE_BAD_HASH
);
4184 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4186 SetLastError(NTE_BAD_ALGID
);
4190 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4192 SetLastError(NTE_BAD_HASH_STATE
);
4196 update_hash(pCryptHash
, pbData
, dwDataLen
);
4200 /******************************************************************************
4201 * CPHashSessionKey (RSAENH.@)
4203 * Updates a hash object with the binary representation of a symmetric key.
4206 * hProv [I] Key container to which the hash object belongs.
4207 * hHash [I] Hash object which is to be updated.
4208 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4209 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4215 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4218 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4222 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
4224 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4225 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4227 SetLastError(NTE_BAD_KEY
);
4231 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4232 SetLastError(NTE_BAD_FLAGS
);
4236 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4237 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4238 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4239 bTemp
= abKeyValue
[i
];
4240 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4241 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4245 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4248 /******************************************************************************
4249 * CPReleaseContext (RSAENH.@)
4251 * Release a key container.
4254 * hProv [I] Key container to be released.
4255 * dwFlags [I] Currently none defined.
4261 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4263 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4265 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4267 /* MSDN: hProv not containing valid context handle */
4268 SetLastError(NTE_BAD_UID
);
4273 SetLastError(NTE_BAD_FLAGS
);
4280 /******************************************************************************
4281 * CPSetHashParam (RSAENH.@)
4283 * Set a parameter of a hash object
4286 * hProv [I] The key container to which the key belongs.
4287 * hHash [I] The hash object for which a parameter is to be set.
4288 * dwParam [I] Parameter type. See Notes.
4289 * pbData [I] Pointer to the parameter value.
4290 * dwFlags [I] Currently none defined.
4297 * Currently only the HP_HMAC_INFO dwParam type is defined.
4298 * The HMAC_INFO struct will be deep copied into the hash object.
4299 * See Internet RFC 2104 for details on the HMAC algorithm.
4301 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4302 BYTE
*pbData
, DWORD dwFlags
)
4304 CRYPTHASH
*pCryptHash
;
4305 CRYPTKEY
*pCryptKey
;
4308 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4309 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4311 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4313 SetLastError(NTE_BAD_UID
);
4318 SetLastError(NTE_BAD_FLAGS
);
4322 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4323 (OBJECTHDR
**)&pCryptHash
))
4325 SetLastError(NTE_BAD_HASH
);
4331 free_hmac_info(pCryptHash
->pHMACInfo
);
4332 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4334 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4335 (OBJECTHDR
**)&pCryptKey
))
4337 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4341 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4342 HCRYPTHASH hKeyHash
;
4345 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4348 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4349 pCryptKey
->blobHmacKey
.cbData
, 0))
4351 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4354 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4355 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4358 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4361 pCryptKey
->dwKeyLen
= keyLen
;
4362 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4364 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4365 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4367 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4368 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4371 init_hash(pCryptHash
);
4375 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4376 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4379 case HP_TLS1PRF_SEED
:
4380 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4382 case HP_TLS1PRF_LABEL
:
4383 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4386 SetLastError(NTE_BAD_TYPE
);
4391 /******************************************************************************
4392 * CPSetProvParam (RSAENH.@)
4394 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4396 KEYCONTAINER
*pKeyContainer
;
4399 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
4401 if (!(pKeyContainer
= get_key_container(hProv
)))
4406 case PP_KEYSET_SEC_DESCR
:
4408 SECURITY_DESCRIPTOR
*sd
= (SECURITY_DESCRIPTOR
*)pbData
;
4409 DWORD err
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
4411 REGSAM access
= WRITE_DAC
| WRITE_OWNER
| ACCESS_SYSTEM_SECURITY
;
4412 PSID owner
= NULL
, group
= NULL
;
4413 PACL dacl
= NULL
, sacl
= NULL
;
4415 if (!open_container_key(pKeyContainer
->szName
, flags
, access
, &hKey
))
4417 SetLastError(NTE_BAD_KEYSET
);
4421 if ((dwFlags
& OWNER_SECURITY_INFORMATION
&& !GetSecurityDescriptorOwner(sd
, &owner
, &def
)) ||
4422 (dwFlags
& GROUP_SECURITY_INFORMATION
&& !GetSecurityDescriptorGroup(sd
, &group
, &def
)) ||
4423 (dwFlags
& DACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorDacl(sd
, &present
, &dacl
, &def
)) ||
4424 (dwFlags
& SACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorSacl(sd
, &present
, &sacl
, &def
)))
4430 err
= SetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, owner
, group
, dacl
, sacl
);
4440 FIXME("unimplemented parameter %08x\n", dwParam
);
4445 /******************************************************************************
4446 * CPSignHash (RSAENH.@)
4448 * Sign a hash object
4451 * hProv [I] The key container, to which the hash object belongs.
4452 * hHash [I] The hash object to be signed.
4453 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4454 * sDescription [I] Should be NULL for security reasons.
4455 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4456 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4457 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4463 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4464 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4467 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4468 CRYPTKEY
*pCryptKey
;
4470 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4474 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4475 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4476 dwFlags
, pbSignature
, pdwSigLen
);
4478 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4479 SetLastError(NTE_BAD_FLAGS
);
4483 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4485 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4486 (OBJECTHDR
**)&pCryptKey
))
4488 SetLastError(NTE_NO_KEY
);
4493 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4497 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4499 SetLastError(ERROR_MORE_DATA
);
4500 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4503 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4506 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4507 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4513 dwHashLen
= sizeof(DWORD
);
4514 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4516 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4517 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4520 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4524 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4526 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4530 /******************************************************************************
4531 * CPVerifySignature (RSAENH.@)
4533 * Verify the signature of a hash object.
4536 * hProv [I] The key container, to which the hash belongs.
4537 * hHash [I] The hash for which the signature is verified.
4538 * pbSignature [I] The binary signature.
4539 * dwSigLen [I] Length of the signature BLOB.
4540 * hPubKey [I] Public key used to verify the signature.
4541 * sDescription [I] Should be NULL for security reasons.
4542 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4545 * Success: TRUE (Signature is valid)
4546 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4548 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbSignature
,
4549 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4552 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4553 CRYPTKEY
*pCryptKey
;
4556 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4559 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4560 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4563 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4564 SetLastError(NTE_BAD_FLAGS
);
4568 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4570 SetLastError(NTE_BAD_UID
);
4574 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4575 (OBJECTHDR
**)&pCryptKey
))
4577 SetLastError(NTE_BAD_KEY
);
4581 /* in Microsoft implementation, the signature length is checked before
4582 * the signature pointer.
4584 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4586 SetLastError(NTE_BAD_SIGNATURE
);
4590 if (!hHash
|| !pbSignature
)
4592 SetLastError(ERROR_INVALID_PARAMETER
);
4597 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4598 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4604 dwHashLen
= sizeof(DWORD
);
4605 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4607 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4608 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4610 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4611 if (!pbConstructed
) {
4612 SetLastError(NTE_NO_MEMORY
);
4616 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4618 SetLastError(NTE_NO_MEMORY
);
4622 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4628 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
4629 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4634 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
4635 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
4636 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4641 SetLastError(NTE_BAD_SIGNATURE
);
4644 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4645 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4649 /******************************************************************************
4650 * DllRegisterServer (RSAENH.@)
4652 HRESULT WINAPI
DllRegisterServer(void)
4654 return __wine_register_resources( instance
);
4657 /******************************************************************************
4658 * DllUnregisterServer (RSAENH.@)
4660 HRESULT WINAPI
DllUnregisterServer(void)
4662 return __wine_unregister_resources( instance
);