3 * RSAENH - RSA encryption for Wine
5 * Copyright 2002 TransGaming Technologies (David Hammerton)
6 * Copyright 2004 Mike McCormack for CodeWeavers
7 * Copyright 2004, 2005 Michael Jung
8 * Copyright 2007 Vijay Kiran Kamuju
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
45 static HINSTANCE instance
;
47 /******************************************************************************
48 * CRYPTHASH - hash objects
50 #define RSAENH_MAGIC_HASH 0x85938417u
51 #define RSAENH_HASHSTATE_HASHING 1
52 #define RSAENH_HASHSTATE_FINISHED 2
53 typedef struct _RSAENH_TLS1PRF_PARAMS
55 CRYPT_DATA_BLOB blobLabel
;
56 CRYPT_DATA_BLOB blobSeed
;
57 } RSAENH_TLS1PRF_PARAMS
;
59 typedef struct tagCRYPTHASH
68 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
70 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
73 /******************************************************************************
74 * CRYPTKEY - key objects
76 #define RSAENH_MAGIC_KEY 0x73620457u
77 #define RSAENH_MAX_KEY_SIZE 64
78 #define RSAENH_MAX_BLOCK_SIZE 24
79 #define RSAENH_KEYSTATE_IDLE 0
80 #define RSAENH_KEYSTATE_ENCRYPTING 1
81 #define RSAENH_KEYSTATE_MASTERKEY 2
82 typedef struct _RSAENH_SCHANNEL_INFO
84 SCHANNEL_ALG saEncAlg
;
85 SCHANNEL_ALG saMACAlg
;
86 CRYPT_DATA_BLOB blobClientRandom
;
87 CRYPT_DATA_BLOB blobServerRandom
;
88 } RSAENH_SCHANNEL_INFO
;
90 typedef struct tagCRYPTKEY
99 DWORD dwEffectiveKeyLen
;
104 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
105 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
106 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
107 RSAENH_SCHANNEL_INFO siSChannelInfo
;
108 CRYPT_DATA_BLOB blobHmacKey
;
111 /******************************************************************************
112 * KEYCONTAINER - key containers
114 #define RSAENH_PERSONALITY_BASE 0u
115 #define RSAENH_PERSONALITY_STRONG 1u
116 #define RSAENH_PERSONALITY_ENHANCED 2u
117 #define RSAENH_PERSONALITY_SCHANNEL 3u
118 #define RSAENH_PERSONALITY_AES 4u
120 #define RSAENH_MAGIC_CONTAINER 0x26384993u
121 typedef struct tagKEYCONTAINER
127 DWORD dwEnumContainersCtr
;
128 CHAR szName
[MAX_PATH
];
129 CHAR szProvName
[MAX_PATH
];
130 HCRYPTKEY hKeyExchangeKeyPair
;
131 HCRYPTKEY hSignatureKeyPair
;
134 /******************************************************************************
135 * Some magic constants
137 #define RSAENH_ENCRYPT 1
138 #define RSAENH_DECRYPT 0
139 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
140 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
141 #define RSAENH_HMAC_DEF_PAD_LEN 64
142 #define RSAENH_HMAC_BLOCK_LEN 64
143 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
144 #define RSAENH_DES_STORAGE_KEYLEN 64
145 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
146 #define RSAENH_3DES112_STORAGE_KEYLEN 128
147 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
148 #define RSAENH_3DES_STORAGE_KEYLEN 192
149 #define RSAENH_MAGIC_RSA2 0x32415352
150 #define RSAENH_MAGIC_RSA1 0x31415352
151 #define RSAENH_PKC_BLOCKTYPE 0x02
152 #define RSAENH_SSL3_VERSION_MAJOR 3
153 #define RSAENH_SSL3_VERSION_MINOR 0
154 #define RSAENH_TLS1_VERSION_MAJOR 3
155 #define RSAENH_TLS1_VERSION_MINOR 1
156 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
158 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
159 /******************************************************************************
160 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
162 #define RSAENH_MAX_ENUMALGS 24
163 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
164 #define S(s) sizeof(s), s
165 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
168 {CALG_RC2
, 40, 40, 56, 0, S("RC2"), S("RSA Data Security's RC2")},
169 {CALG_RC4
, 40, 40, 56, 0, S("RC4"), S("RSA Data Security's RC4")},
170 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
171 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
172 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
173 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
174 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
175 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
176 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
177 {CALG_RSA_SIGN
, 512, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
178 {CALG_RSA_KEYX
, 512, 384, 1024, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
179 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
180 {0, 0, 0, 0, 0, S(""), S("")}
183 {CALG_RC2
, 128, 40, 128, 0, S("RC2"), S("RSA Data Security's RC2")},
184 {CALG_RC4
, 128, 40, 128, 0, S("RC4"), S("RSA Data Security's RC4")},
185 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
186 {CALG_3DES_112
, 112, 112, 112, 0, S("3DES TWO KEY"), S("Two Key Triple DES")},
187 {CALG_3DES
, 168, 168, 168, 0, S("3DES"), S("Three Key Triple DES")},
188 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
189 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
190 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
191 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
192 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
193 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
194 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
195 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
196 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
197 {0, 0, 0, 0, 0, S(""), S("")}
200 {CALG_RC2
, 128, 40, 128, 0, S("RC2"), S("RSA Data Security's RC2")},
201 {CALG_RC4
, 128, 40, 128, 0, S("RC4"), S("RSA Data Security's RC4")},
202 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
203 {CALG_3DES_112
, 112, 112, 112, 0, S("3DES TWO KEY"), S("Two Key Triple DES")},
204 {CALG_3DES
, 168, 168, 168, 0, S("3DES"), S("Three Key Triple DES")},
205 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
206 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
207 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
208 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
209 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
210 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
211 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
212 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
213 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
214 {0, 0, 0, 0, 0, S(""), S("")}
217 {CALG_RC2
, 128, 40, 128, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RC2"), S("RSA Data Security's RC2")},
218 {CALG_RC4
, 128, 40, 128, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RC4"), S("RSA Data Security's RC4")},
219 {CALG_DES
, 56, 56, 56, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("DES"), S("Data Encryption Standard (DES)")},
220 {CALG_3DES_112
, 112, 112, 112, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("3DES TWO KEY"), S("Two Key Triple DES")},
221 {CALG_3DES
, 168, 168, 168, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("3DES"), S("Three Key Triple DES")},
222 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
223 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("MD5"), S("Message Digest 5 (MD5)")},
224 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
225 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
226 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RSA_SIGN"), S("RSA Signature")},
227 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RSA_KEYX"), S("RSA Key Exchange")},
228 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
229 {CALG_PCT1_MASTER
, 128, 128, 128, CRYPT_FLAG_PCT1
, S("PCT1 MASTER"), S("PCT1 Master")},
230 {CALG_SSL2_MASTER
, 40, 40, 192, CRYPT_FLAG_SSL2
, S("SSL2 MASTER"), S("SSL2 Master")},
231 {CALG_SSL3_MASTER
, 384, 384, 384, CRYPT_FLAG_SSL3
, S("SSL3 MASTER"), S("SSL3 Master")},
232 {CALG_TLS1_MASTER
, 384, 384, 384, CRYPT_FLAG_TLS1
, S("TLS1 MASTER"), S("TLS1 Master")},
233 {CALG_SCHANNEL_MASTER_HASH
, 0, 0, -1, 0, S("SCH MASTER HASH"), S("SChannel Master Hash")},
234 {CALG_SCHANNEL_MAC_KEY
, 0, 0, -1, 0, S("SCH MAC KEY"), S("SChannel MAC Key")},
235 {CALG_SCHANNEL_ENC_KEY
, 0, 0, -1, 0, S("SCH ENC KEY"), S("SChannel Encryption Key")},
236 {CALG_TLS1PRF
, 0, 0, -1, 0, S("TLS1 PRF"), S("TLS1 Pseudo Random Function")},
237 {0, 0, 0, 0, 0, S(""), S("")}
240 {CALG_RC2
, 128, 40, 128, 0, S("RC2"), S("RSA Data Security's RC2")},
241 {CALG_RC4
, 128, 40, 128, 0, S("RC4"), S("RSA Data Security's RC4")},
242 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
243 {CALG_3DES_112
, 112, 112, 112, 0, S("3DES TWO KEY"), S("Two Key Triple DES")},
244 {CALG_3DES
, 168, 168, 168, 0, S("3DES"), S("Three Key Triple DES")},
245 {CALG_AES
, 128, 128, 128, 0, S("AES"), S("Advanced Encryption Standard (AES)")},
246 {CALG_AES_128
, 128, 128, 128, 0, S("AES-128"), S("Advanced Encryption Standard (AES-128)")},
247 {CALG_AES_192
, 192, 192, 192, 0, S("AES-192"), S("Advanced Encryption Standard (AES-192)")},
248 {CALG_AES_256
, 256, 256, 256, 0, S("AES-256"), S("Advanced Encryption Standard (AES-256)")},
249 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
250 {CALG_SHA_256
, 256, 256, 256, CRYPT_FLAG_SIGNING
, S("SHA-256"), S("Secure Hash Algorithm (SHA-256)")},
251 {CALG_SHA_384
, 384, 384, 384, CRYPT_FLAG_SIGNING
, S("SHA-384"), S("Secure Hash Algorithm (SHA-384)")},
252 {CALG_SHA_512
, 512, 512, 512, CRYPT_FLAG_SIGNING
, S("SHA-512"), S("Secure Hash Algorithm (SHA-512)")},
253 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
254 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
255 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
256 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
257 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
258 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
259 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
260 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
261 {0, 0, 0, 0, 0, S(""), S("")}
266 /******************************************************************************
267 * API forward declarations
270 RSAENH_CPGetKeyParam(
301 RSAENH_CPSetHashParam(
305 BYTE
*pbData
, DWORD dwFlags
309 RSAENH_CPGetHashParam(
319 RSAENH_CPDestroyHash(
324 static BOOL
crypt_export_key(
334 static BOOL
import_key(
353 /******************************************************************************
354 * CSP's handle table (used by all acquired key containers)
356 static struct handle_table handle_table
;
358 /******************************************************************************
361 * Initializes and destroys the handle table for the CSP's handles.
363 BOOL WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID reserved
)
367 case DLL_PROCESS_ATTACH
:
368 instance
= hInstance
;
369 DisableThreadLibraryCalls(hInstance
);
370 init_handle_table(&handle_table
);
373 case DLL_PROCESS_DETACH
:
375 destroy_handle_table(&handle_table
);
381 /******************************************************************************
382 * copy_param [Internal]
384 * Helper function that supports the standard WINAPI protocol for querying data
388 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
389 * May be NUL if the required buffer size is to be queried only.
390 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
391 * Out: Size of parameter pbParam
392 * pbParam [I] Parameter value.
393 * dwParamSize [I] Size of pbParam
396 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
397 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
399 static inline BOOL
copy_param(BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, const BYTE
*pbParam
,
404 if (dwParamSize
> *pdwBufferSize
)
406 SetLastError(ERROR_MORE_DATA
);
407 *pdwBufferSize
= dwParamSize
;
410 memcpy(pbBuffer
, pbParam
, dwParamSize
);
412 *pdwBufferSize
= dwParamSize
;
416 static inline KEYCONTAINER
* get_key_container(HCRYPTPROV hProv
)
418 KEYCONTAINER
*pKeyContainer
;
420 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
421 (OBJECTHDR
**)&pKeyContainer
))
423 SetLastError(NTE_BAD_UID
);
426 return pKeyContainer
;
429 /******************************************************************************
430 * get_algid_info [Internal]
432 * Query CSP capabilities for a given crypto algorithm.
435 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
436 * algid [I] Identifier of the crypto algorithm about which information is requested.
439 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
440 * Failure: NULL (algid not supported)
442 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
443 const PROV_ENUMALGS_EX
*iterator
;
444 KEYCONTAINER
*pKeyContainer
;
446 if (!(pKeyContainer
= get_key_container(hProv
))) return NULL
;
448 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
449 if (iterator
->aiAlgid
== algid
) return iterator
;
452 SetLastError(NTE_BAD_ALGID
);
456 /******************************************************************************
457 * copy_data_blob [Internal]
459 * deeply copies a DATA_BLOB
462 * dst [O] That's where the blob will be copied to
463 * src [I] Source blob
467 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
470 * Use free_data_blob to release resources occupied by copy_data_blob.
472 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src
)
474 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, src
->cbData
);
476 SetLastError(NTE_NO_MEMORY
);
479 dst
->cbData
= src
->cbData
;
480 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
484 /******************************************************************************
485 * concat_data_blobs [Internal]
487 * Concatenates two blobs
490 * dst [O] The new blob will be copied here
491 * src1 [I] Prefix blob
492 * src2 [I] Appendix blob
496 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
499 * Release resources occupied by concat_data_blobs with free_data_blobs
501 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src1
,
502 const PCRYPT_DATA_BLOB src2
)
504 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
505 dst
->pbData
= HeapAlloc(GetProcessHeap(), 0, dst
->cbData
);
507 SetLastError(NTE_NO_MEMORY
);
510 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
511 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
515 /******************************************************************************
516 * free_data_blob [Internal]
518 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
521 * pBlob [I] Heap space occupied by pBlob->pbData is released
523 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
524 HeapFree(GetProcessHeap(), 0, pBlob
->pbData
);
527 /******************************************************************************
528 * init_data_blob [Internal]
530 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
531 pBlob
->pbData
= NULL
;
535 /******************************************************************************
536 * free_hmac_info [Internal]
538 * Deeply free an HMAC_INFO struct.
541 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
544 * See Internet RFC 2104 for details on the HMAC algorithm.
546 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
547 if (!hmac_info
) return;
548 HeapFree(GetProcessHeap(), 0, hmac_info
->pbInnerString
);
549 HeapFree(GetProcessHeap(), 0, hmac_info
->pbOuterString
);
550 HeapFree(GetProcessHeap(), 0, hmac_info
);
553 /******************************************************************************
554 * copy_hmac_info [Internal]
556 * Deeply copy an HMAC_INFO struct
559 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
560 * src [I] Pointer to the HMAC_INFO struct to be copied.
567 * See Internet RFC 2104 for details on the HMAC algorithm.
569 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
570 if (!src
) return FALSE
;
571 *dst
= HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO
));
572 if (!*dst
) return FALSE
;
574 (*dst
)->pbInnerString
= NULL
;
575 (*dst
)->pbOuterString
= NULL
;
576 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
577 (*dst
)->pbInnerString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbInnerString
);
578 if (!(*dst
)->pbInnerString
) {
579 free_hmac_info(*dst
);
582 if (src
->cbInnerString
)
583 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
585 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
586 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
587 (*dst
)->pbOuterString
= HeapAlloc(GetProcessHeap(), 0, (*dst
)->cbOuterString
);
588 if (!(*dst
)->pbOuterString
) {
589 free_hmac_info(*dst
);
592 if (src
->cbOuterString
)
593 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
595 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
599 /******************************************************************************
600 * destroy_hash [Internal]
602 * Destructor for hash objects
605 * pCryptHash [I] Pointer to the hash object to be destroyed.
606 * Will be invalid after function returns!
608 static void destroy_hash(OBJECTHDR
*pObject
)
610 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
612 free_hmac_info(pCryptHash
->pHMACInfo
);
613 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
614 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
615 HeapFree(GetProcessHeap(), 0, pCryptHash
);
618 /******************************************************************************
619 * init_hash [Internal]
621 * Initialize (or reset) a hash object
624 * pCryptHash [I] The hash object to be initialized.
626 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
629 switch (pCryptHash
->aiAlgid
)
632 if (pCryptHash
->pHMACInfo
) {
633 const PROV_ENUMALGS_EX
*pAlgInfo
;
635 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
636 if (!pAlgInfo
) return FALSE
;
637 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
638 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
639 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
640 pCryptHash
->pHMACInfo
->pbInnerString
,
641 pCryptHash
->pHMACInfo
->cbInnerString
);
646 dwLen
= sizeof(DWORD
);
647 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
648 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
649 pCryptHash
->dwHashSize
>>= 3;
653 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
);
657 /******************************************************************************
658 * update_hash [Internal]
660 * Hashes the given data and updates the hash object's state accordingly
663 * pCryptHash [I] Hash object to be updated.
664 * pbData [I] Pointer to data stream to be hashed.
665 * dwDataLen [I] Length of data stream.
667 static inline void update_hash(CRYPTHASH
*pCryptHash
, const BYTE
*pbData
, DWORD dwDataLen
)
671 switch (pCryptHash
->aiAlgid
)
674 if (pCryptHash
->pHMACInfo
)
675 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
680 pbTemp
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
682 memcpy(pbTemp
, pbData
, dwDataLen
);
683 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, FALSE
, 0,
684 pbTemp
, &dwDataLen
, dwDataLen
);
685 HeapFree(GetProcessHeap(), 0, pbTemp
);
689 update_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pbData
, dwDataLen
);
693 /******************************************************************************
694 * finalize_hash [Internal]
696 * Finalizes the hash, after all data has been hashed with update_hash.
697 * No additional data can be hashed afterwards until the hash gets initialized again.
700 * pCryptHash [I] Hash object to be finalized.
702 static inline void finalize_hash(CRYPTHASH
*pCryptHash
) {
705 switch (pCryptHash
->aiAlgid
)
708 if (pCryptHash
->pHMACInfo
) {
709 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
711 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
712 pCryptHash
->abHashValue
);
713 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
714 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
);
715 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
716 pCryptHash
->pHMACInfo
->pbOuterString
,
717 pCryptHash
->pHMACInfo
->cbOuterString
);
718 update_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
719 abHashValue
, pCryptHash
->dwHashSize
);
720 finalize_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->context
,
721 pCryptHash
->abHashValue
);
727 RSAENH_CPEncrypt(pCryptHash
->hProv
, pCryptHash
->hKey
, 0, TRUE
, 0,
728 pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
);
732 finalize_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->context
, pCryptHash
->abHashValue
);
736 /******************************************************************************
737 * destroy_key [Internal]
739 * Destructor for key objects
742 * pCryptKey [I] Pointer to the key object to be destroyed.
743 * Will be invalid after function returns!
745 static void destroy_key(OBJECTHDR
*pObject
)
747 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
749 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
750 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
751 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
752 free_data_blob(&pCryptKey
->blobHmacKey
);
753 HeapFree(GetProcessHeap(), 0, pCryptKey
);
756 /******************************************************************************
757 * setup_key [Internal]
759 * Initialize (or reset) a key object
762 * pCryptKey [I] The key object to be initialized.
764 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
765 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
766 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
767 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
768 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
769 pCryptKey
->abKeyValue
);
772 /******************************************************************************
775 * Creates a new key object without assigning the actual binary key value.
776 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
779 * hProv [I] Handle to the provider to which the created key will belong.
780 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
781 * dwFlags [I] Upper 16 bits give the key length.
782 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
784 * ppCryptKey [O] Pointer to the created key
787 * Success: Handle to the created key.
788 * Failure: INVALID_HANDLE_VALUE
790 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
794 DWORD dwKeyLen
= HIWORD(dwFlags
), bKeyLen
= dwKeyLen
;
795 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
800 * Retrieve the CSP's capabilities for the given ALG_ID value
802 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
803 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
805 TRACE("alg = %s, dwKeyLen = %d\n", debugstr_a(peaAlgidInfo
->szName
),
808 * Assume the default key length, if none is specified explicitly
810 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
813 * Check if the requested key length is supported by the current CSP.
814 * Adjust key length's for DES algorithms.
818 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
819 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
821 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
822 SetLastError(NTE_BAD_FLAGS
);
823 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
828 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
829 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
831 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
832 SetLastError(NTE_BAD_FLAGS
);
833 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
838 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
839 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
841 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
842 SetLastError(NTE_BAD_FLAGS
);
843 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
848 /* Avoid the key length check for HMAC keys, which have unlimited
856 TRACE("missing key len for CALG_AES\n");
857 SetLastError(NTE_BAD_ALGID
);
858 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
863 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
864 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
866 TRACE("key len %d out of bounds (%d, %d)\n", dwKeyLen
,
867 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
868 SetLastError(NTE_BAD_DATA
);
869 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
873 hCryptKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
874 destroy_key
, (OBJECTHDR
**)&pCryptKey
);
875 if (hCryptKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
877 KEYCONTAINER
*pKeyContainer
= get_key_container(hProv
);
878 pCryptKey
->aiAlgid
= aiAlgid
;
879 pCryptKey
->hProv
= hProv
;
880 pCryptKey
->dwModeBits
= 0;
881 pCryptKey
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
|
883 if (dwFlags
& CRYPT_EXPORTABLE
)
884 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
885 pCryptKey
->dwKeyLen
= dwKeyLen
>> 3;
886 pCryptKey
->dwEffectiveKeyLen
= 0;
889 * For compatibility reasons a 40 bit key on the Enhanced
890 * provider will not have salt
892 if (pKeyContainer
->dwPersonality
== RSAENH_PERSONALITY_ENHANCED
893 && (aiAlgid
== CALG_RC2
|| aiAlgid
== CALG_RC4
)
894 && (dwFlags
& CRYPT_CREATE_SALT
) && dwKeyLen
== 40)
895 pCryptKey
->dwSaltLen
= 0;
896 else if ((dwFlags
& CRYPT_CREATE_SALT
) || (dwKeyLen
== 40 && !(dwFlags
& CRYPT_NO_SALT
)))
897 pCryptKey
->dwSaltLen
= 16 /*FIXME*/ - pCryptKey
->dwKeyLen
;
899 pCryptKey
->dwSaltLen
= 0;
900 memset(pCryptKey
->abKeyValue
, 0, sizeof(pCryptKey
->abKeyValue
));
901 memset(pCryptKey
->abInitVector
, 0, sizeof(pCryptKey
->abInitVector
));
902 memset(&pCryptKey
->siSChannelInfo
.saEncAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saEncAlg
));
903 memset(&pCryptKey
->siSChannelInfo
.saMACAlg
, 0, sizeof(pCryptKey
->siSChannelInfo
.saMACAlg
));
904 init_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
905 init_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
906 init_data_blob(&pCryptKey
->blobHmacKey
);
910 case CALG_PCT1_MASTER
:
911 case CALG_SSL2_MASTER
:
912 case CALG_SSL3_MASTER
:
913 case CALG_TLS1_MASTER
:
915 pCryptKey
->dwBlockLen
= 0;
916 pCryptKey
->dwMode
= 0;
923 pCryptKey
->dwBlockLen
= 8;
924 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
931 pCryptKey
->dwBlockLen
= 16;
932 pCryptKey
->dwMode
= CRYPT_MODE_CBC
;
937 pCryptKey
->dwBlockLen
= dwKeyLen
>> 3;
938 pCryptKey
->dwMode
= 0;
942 pCryptKey
->dwBlockLen
= 0;
943 pCryptKey
->dwMode
= 0;
947 *ppCryptKey
= pCryptKey
;
953 /******************************************************************************
954 * map_key_spec_to_key_pair_name [Internal]
956 * Returns the name of the registry value associated with a key spec.
959 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
962 * Success: Name of registry value.
965 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
972 szValueName
= "KeyExchangeKeyPair";
975 szValueName
= "SignatureKeyPair";
978 WARN("invalid key spec %d\n", dwKeySpec
);
984 /******************************************************************************
985 * store_key_pair [Internal]
987 * Stores a key pair to the registry
990 * hCryptKey [I] Handle to the key to be stored
991 * hKey [I] Registry key where the key pair is to be stored
992 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
993 * dwFlags [I] Flags for protecting the key
995 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
998 DATA_BLOB blobIn
, blobOut
;
1003 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1005 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1006 (OBJECTHDR
**)&pKey
))
1008 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
1010 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1013 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
1016 blobIn
.pbData
= pbKey
;
1017 blobIn
.cbData
= dwLen
;
1019 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1022 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
1023 blobOut
.pbData
, blobOut
.cbData
);
1024 LocalFree(blobOut
.pbData
);
1027 HeapFree(GetProcessHeap(), 0, pbKey
);
1033 /******************************************************************************
1034 * map_key_spec_to_permissions_name [Internal]
1036 * Returns the name of the registry value associated with the permissions for
1040 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1043 * Success: Name of registry value.
1046 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1052 case AT_KEYEXCHANGE
:
1053 szValueName
= "KeyExchangePermissions";
1056 szValueName
= "SignaturePermissions";
1059 WARN("invalid key spec %d\n", dwKeySpec
);
1065 /******************************************************************************
1066 * store_key_permissions [Internal]
1068 * Stores a key's permissions to the registry
1071 * hCryptKey [I] Handle to the key whose permissions are to be stored
1072 * hKey [I] Registry key where the key permissions are to be stored
1073 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1075 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1080 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1082 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1083 (OBJECTHDR
**)&pKey
))
1084 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1085 (BYTE
*)&pKey
->dwPermissions
,
1086 sizeof(pKey
->dwPermissions
));
1089 /******************************************************************************
1090 * create_container_key [Internal]
1092 * Creates the registry key for a key container's persistent storage.
1095 * pKeyContainer [I] Pointer to the key container
1096 * sam [I] Desired registry access
1097 * phKey [O] Returned key
1099 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1101 CHAR szRSABase
[sizeof(RSAENH_REGKEY
) + MAX_PATH
];
1104 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1106 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1107 hRootKey
= HKEY_LOCAL_MACHINE
;
1109 hRootKey
= HKEY_CURRENT_USER
;
1111 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1112 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1113 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1114 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1118 /******************************************************************************
1119 * open_container_key [Internal]
1121 * Opens a key container's persistent storage for reading.
1124 * pszContainerName [I] Name of the container to be opened. May be the empty
1125 * string if the parent key of all containers is to be
1127 * dwFlags [I] Flags indicating which keyset to be opened.
1128 * phKey [O] Returned key
1130 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, REGSAM access
, HKEY
*phKey
)
1132 CHAR szRSABase
[sizeof(RSAENH_REGKEY
) + MAX_PATH
];
1135 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1137 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1138 hRootKey
= HKEY_LOCAL_MACHINE
;
1140 hRootKey
= HKEY_CURRENT_USER
;
1142 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1143 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1144 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, access
, phKey
) ==
1148 /******************************************************************************
1149 * delete_container_key [Internal]
1151 * Deletes a key container's persistent storage.
1154 * pszContainerName [I] Name of the container to be opened.
1155 * dwFlags [I] Flags indicating which keyset to be opened.
1157 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1159 CHAR szRegKey
[sizeof(RSAENH_REGKEY
) + MAX_PATH
];
1162 sprintf(szRegKey
, RSAENH_REGKEY
, pszContainerName
);
1164 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1165 hRootKey
= HKEY_LOCAL_MACHINE
;
1167 hRootKey
= HKEY_CURRENT_USER
;
1168 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1169 SetLastError(ERROR_SUCCESS
);
1172 SetLastError(NTE_BAD_KEYSET
);
1177 /******************************************************************************
1178 * store_key_container_keys [Internal]
1180 * Stores key container's keys in a persistent location.
1183 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1185 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1190 /* On WinXP, persistent keys are stored in a file located at:
1191 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1194 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1195 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1199 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1201 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1202 AT_KEYEXCHANGE
, dwFlags
);
1203 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1204 AT_SIGNATURE
, dwFlags
);
1209 /******************************************************************************
1210 * store_key_container_permissions [Internal]
1212 * Stores key container's key permissions in a persistent location.
1215 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1218 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1222 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1224 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1226 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1232 /******************************************************************************
1233 * release_key_container_keys [Internal]
1235 * Releases key container's keys.
1238 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1240 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1242 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1244 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1248 /******************************************************************************
1249 * destroy_key_container [Internal]
1251 * Destructor for key containers.
1254 * pObjectHdr [I] Pointer to the key container to be destroyed.
1256 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1258 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1260 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1262 store_key_container_keys(pKeyContainer
);
1263 store_key_container_permissions(pKeyContainer
);
1264 release_key_container_keys(pKeyContainer
);
1267 release_key_container_keys(pKeyContainer
);
1268 HeapFree( GetProcessHeap(), 0, pKeyContainer
);
1271 /******************************************************************************
1272 * new_key_container [Internal]
1274 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1275 * of the CSP is determined via the pVTable->pszProvName string.
1278 * pszContainerName [I] Name of the key container.
1279 * pVTable [I] Callback functions and context info provided by the OS
1282 * Success: Handle to the new key container.
1283 * Failure: INVALID_HANDLE_VALUE
1285 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1287 KEYCONTAINER
*pKeyContainer
;
1288 HCRYPTPROV hKeyContainer
;
1290 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1291 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1292 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1294 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1295 pKeyContainer
->dwFlags
= dwFlags
;
1296 pKeyContainer
->dwEnumAlgsCtr
= 0;
1297 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1298 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1299 if (pVTable
&& pVTable
->pszProvName
) {
1300 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1301 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1302 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1303 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1304 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1305 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1306 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1307 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
) ||
1308 !strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_XP_A
)) {
1309 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1311 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1315 /* The new key container has to be inserted into the CSP immediately
1316 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1317 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1320 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1325 return hKeyContainer
;
1328 /******************************************************************************
1329 * read_key_value [Internal]
1331 * Reads a key pair value from the registry
1334 * hKeyContainer [I] Crypt provider to use to import the key
1335 * hKey [I] Registry key from which to read the key pair
1336 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1337 * dwFlags [I] Flags for unprotecting the key
1338 * phCryptKey [O] Returned key
1340 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1343 DWORD dwValueType
, dwLen
;
1345 DATA_BLOB blobIn
, blobOut
;
1348 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1350 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1353 pbKey
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
1356 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1359 blobIn
.pbData
= pbKey
;
1360 blobIn
.cbData
= dwLen
;
1362 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1365 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1367 LocalFree(blobOut
.pbData
);
1370 HeapFree(GetProcessHeap(), 0, pbKey
);
1377 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1378 (OBJECTHDR
**)&pKey
))
1380 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1382 dwLen
= sizeof(pKey
->dwPermissions
);
1383 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1384 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1391 /******************************************************************************
1392 * read_key_container [Internal]
1394 * Tries to read the persistent state of the key container (mainly the signature
1395 * and key exchange private keys) given by pszContainerName.
1398 * pszContainerName [I] Name of the key container to read from the registry
1399 * pVTable [I] Pointer to context data provided by the operating system
1402 * Success: Handle to the key container read from the registry
1403 * Failure: INVALID_HANDLE_VALUE
1405 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1408 KEYCONTAINER
*pKeyContainer
;
1409 HCRYPTPROV hKeyContainer
;
1410 HCRYPTKEY hCryptKey
;
1412 if (!open_container_key(pszContainerName
, dwFlags
, KEY_READ
, &hKey
))
1414 SetLastError(NTE_BAD_KEYSET
);
1415 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1418 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1419 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1421 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1422 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1424 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1425 (OBJECTHDR
**)&pKeyContainer
))
1426 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1428 /* read_key_value calls import_key, which calls import_private_key,
1429 * which implicitly installs the key value into the appropriate key
1430 * container key. Thus the ref count is incremented twice, once for
1431 * the output key value, and once for the implicit install, and needs
1432 * to be decremented to balance the two.
1434 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1435 dwProtectFlags
, &hCryptKey
))
1436 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1437 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1438 dwProtectFlags
, &hCryptKey
))
1439 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1442 return hKeyContainer
;
1445 /******************************************************************************
1446 * build_hash_signature [Internal]
1448 * Builds a padded version of a hash to match the length of the RSA key modulus.
1451 * pbSignature [O] The padded hash object is stored here.
1452 * dwLen [I] Length of the pbSignature buffer.
1453 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1454 * abHashValue [I] The value of the hash object.
1455 * dwHashLen [I] Length of the hash value.
1456 * dwFlags [I] Selection of padding algorithm.
1460 * Failure: FALSE (NTE_BAD_ALGID)
1462 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1463 const BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1465 /* These prefixes are meant to be concatenated with hash values of the
1466 * respective kind to form a PKCS #7 DigestInfo. */
1467 static const struct tagOIDDescriptor
{
1470 const BYTE abOID
[19];
1471 } aOIDDescriptor
[] = {
1472 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1473 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1474 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1475 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1476 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1477 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1478 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1479 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1480 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1481 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1482 0x05, 0x00, 0x04, 0x20 } },
1483 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1484 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1485 0x05, 0x00, 0x04, 0x30 } },
1486 { CALG_SHA_512
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1487 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1488 0x05, 0x00, 0x04, 0x40 } },
1489 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1492 DWORD dwIdxOID
, i
, j
;
1494 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1495 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1498 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1499 SetLastError(NTE_BAD_ALGID
);
1503 /* Build the padded signature */
1504 if (dwFlags
& CRYPT_X931_FORMAT
) {
1505 pbSignature
[0] = 0x6b;
1506 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1507 pbSignature
[i
] = 0xbb;
1509 pbSignature
[i
++] = 0xba;
1510 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1511 pbSignature
[i
] = abHashValue
[j
];
1513 pbSignature
[i
++] = 0x33;
1514 pbSignature
[i
++] = 0xcc;
1516 pbSignature
[0] = 0x00;
1517 pbSignature
[1] = 0x01;
1518 if (dwFlags
& CRYPT_NOHASHOID
) {
1519 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1520 pbSignature
[i
] = 0xff;
1522 pbSignature
[i
++] = 0x00;
1524 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1525 pbSignature
[i
] = 0xff;
1527 pbSignature
[i
++] = 0x00;
1528 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1529 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1532 for (j
=0; j
< dwHashLen
; j
++) {
1533 pbSignature
[i
++] = abHashValue
[j
];
1540 /******************************************************************************
1543 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1544 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1545 * The pseudo random stream generated by this function is exclusive or'ed with
1546 * the data in pbBuffer.
1549 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1550 * pblobSeed [I] Seed value
1551 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1552 * dwBufferLen [I] Number of pseudo random bytes desired
1558 static BOOL
tls1_p(HCRYPTHASH hHMAC
, const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
,
1562 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1565 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1566 SetLastError(NTE_BAD_HASH
);
1570 /* compute A_1 = HMAC(seed) */
1572 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1573 finalize_hash(pHMAC
);
1574 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1577 /* compute HMAC(A_i + seed) */
1579 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1580 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1581 finalize_hash(pHMAC
);
1583 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1585 if (i
>= dwBufferLen
) break;
1586 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1588 } while (i
% pHMAC
->dwHashSize
);
1590 /* compute A_{i+1} = HMAC(A_i) */
1592 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1593 finalize_hash(pHMAC
);
1594 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1595 } while (i
< dwBufferLen
);
1600 /******************************************************************************
1601 * tls1_prf [Internal]
1603 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1606 * hProv [I] Key container used to compute the pseudo random stream
1607 * hSecret [I] Key that holds the (pre-)master secret
1608 * pblobLabel [I] Descriptive label
1609 * pblobSeed [I] Seed value
1610 * pbBuffer [O] Pseudo random numbers will be stored here
1611 * dwBufferLen [I] Number of pseudo random bytes desired
1617 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, const PCRYPT_DATA_BLOB pblobLabel
,
1618 const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
, DWORD dwBufferLen
)
1620 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1621 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1622 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1623 CRYPTKEY
*pHalfSecret
, *pSecret
;
1624 DWORD dwHalfSecretLen
;
1625 BOOL result
= FALSE
;
1626 CRYPT_DATA_BLOB blobLabelSeed
;
1628 TRACE("(hProv=%08lx, hSecret=%08lx, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%d)\n",
1629 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1631 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1632 SetLastError(NTE_FAIL
);
1636 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1638 /* concatenation of the label and the seed */
1639 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1641 /* zero out the buffer, since two random streams will be xor'ed into it. */
1642 memset(pbBuffer
, 0, dwBufferLen
);
1644 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1645 * the biggest range of valid key lengths. */
1646 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1647 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1649 /* Derive an HMAC_MD5 hash and call the helper function. */
1650 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1651 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1652 hmacInfo
.HashAlgid
= CALG_MD5
;
1653 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1654 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1656 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1657 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1658 hmacInfo
.HashAlgid
= CALG_SHA
;
1659 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1660 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1664 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1665 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1666 free_data_blob(&blobLabelSeed
);
1670 /******************************************************************************
1671 * pad_data [Internal]
1673 * Helper function for data padding according to PKCS1 #2
1676 * abData [I] The data to be padded
1677 * dwDataLen [I] Length of the data
1678 * abBuffer [O] Padded data will be stored here
1679 * dwBufferLen [I] Length of the buffer (also length of padded data)
1680 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1684 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1686 static BOOL
pad_data(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1691 /* Ensure there is enough space for PKCS1 #2 padding */
1692 if (dwDataLen
> dwBufferLen
-11) {
1693 SetLastError(NTE_BAD_LEN
);
1697 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1700 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1701 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1702 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1703 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1704 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1711 /******************************************************************************
1712 * unpad_data [Internal]
1714 * Remove the PKCS1 padding from RSA decrypted data
1717 * abData [I] The padded data
1718 * dwDataLen [I] Length of the padded data
1719 * abBuffer [O] Data without padding will be stored here
1720 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
1721 * dwFlags [I] Currently none defined
1725 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
1727 static BOOL
unpad_data(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
1734 SetLastError(NTE_BAD_DATA
);
1737 for (i
=2; i
<dwDataLen
; i
++)
1741 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
1742 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
1744 SetLastError(NTE_BAD_DATA
);
1748 *dwBufferLen
= dwDataLen
- i
- 1;
1749 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
1753 /******************************************************************************
1754 * CPAcquireContext (RSAENH.@)
1756 * Acquire a handle to the key container specified by pszContainer
1759 * phProv [O] Pointer to the location the acquired handle will be written to.
1760 * pszContainer [I] Name of the desired key container. See Notes
1761 * dwFlags [I] Flags. See Notes.
1762 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
1769 * If pszContainer is NULL or points to a zero length string the user's login
1770 * name will be used as the key container name.
1772 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
1773 * If a keyset with the given name already exists, the function fails and sets
1774 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
1775 * key container does not exist, function fails and sets last error to
1778 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
1779 DWORD dwFlags
, PVTableProvStruc pVTable
)
1781 CHAR szKeyContainerName
[MAX_PATH
];
1783 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08x, pVTable=%p)\n", phProv
,
1784 debugstr_a(pszContainer
), dwFlags
, pVTable
);
1786 if (pszContainer
&& *pszContainer
)
1788 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
1792 DWORD dwLen
= sizeof(szKeyContainerName
);
1793 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
1796 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
1799 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1802 case CRYPT_DELETEKEYSET
:
1803 return delete_container_key(szKeyContainerName
, dwFlags
);
1805 case CRYPT_NEWKEYSET
:
1806 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1807 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1809 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
1810 TRACE("Can't create new keyset, already exists\n");
1811 SetLastError(NTE_EXISTS
);
1814 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
1817 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
1818 case CRYPT_VERIFYCONTEXT
:
1819 if (pszContainer
&& *pszContainer
) {
1820 TRACE("pszContainer should be empty\n");
1821 SetLastError(NTE_BAD_FLAGS
);
1824 *phProv
= new_key_container("", dwFlags
, pVTable
);
1828 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1829 SetLastError(NTE_BAD_FLAGS
);
1833 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
1834 SetLastError(ERROR_SUCCESS
);
1841 /******************************************************************************
1842 * CPCreateHash (RSAENH.@)
1844 * CPCreateHash creates and initializes a new hash object.
1847 * hProv [I] Handle to the key container to which the new hash will belong.
1848 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
1849 * hKey [I] Handle to a session key applied for keyed hashes.
1850 * dwFlags [I] Currently no flags defined. Must be zero.
1851 * phHash [O] Points to the location where a handle to the new hash will be stored.
1858 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
1859 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
1861 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
1864 CRYPTKEY
*pCryptKey
;
1865 CRYPTHASH
*pCryptHash
;
1866 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
1868 TRACE("(hProv=%08lx, Algid=%08x, hKey=%08lx, dwFlags=%08x, phHash=%p)\n", hProv
, Algid
, hKey
,
1871 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
1872 if (!peaAlgidInfo
) return FALSE
;
1876 SetLastError(NTE_BAD_FLAGS
);
1880 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
1881 Algid
== CALG_TLS1PRF
)
1883 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
1884 SetLastError(NTE_BAD_KEY
);
1888 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
1889 SetLastError(NTE_BAD_KEY
);
1893 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
1894 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
1896 SetLastError(NTE_BAD_KEY
);
1899 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
1900 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
1901 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
1903 SetLastError(ERROR_INVALID_PARAMETER
);
1907 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
1908 SetLastError(NTE_BAD_KEY_STATE
);
1913 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
1914 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
1915 if (!pCryptHash
) return FALSE
;
1917 pCryptHash
->aiAlgid
= Algid
;
1918 pCryptHash
->hKey
= hKey
;
1919 pCryptHash
->hProv
= hProv
;
1920 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
1921 pCryptHash
->pHMACInfo
= NULL
;
1922 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
1923 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
1924 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
1926 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
1927 static const char keyex
[] = "key expansion";
1928 BYTE key_expansion
[sizeof keyex
];
1929 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
1931 memcpy( key_expansion
, keyex
, sizeof keyex
);
1933 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
1934 static const char msec
[] = "master secret";
1935 BYTE master_secret
[sizeof msec
];
1936 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
1937 BYTE abKeyValue
[48];
1939 memcpy( master_secret
, msec
, sizeof msec
);
1941 /* See RFC 2246, chapter 8.1 */
1942 if (!concat_data_blobs(&blobRandom
,
1943 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
1944 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
1948 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
1949 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
1950 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
1951 free_data_blob(&blobRandom
);
1954 /* See RFC 2246, chapter 6.3 */
1955 if (!concat_data_blobs(&blobRandom
,
1956 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
1957 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
1961 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
1962 RSAENH_MAX_HASH_SIZE
);
1963 free_data_blob(&blobRandom
);
1966 return init_hash(pCryptHash
);
1969 /******************************************************************************
1970 * CPDestroyHash (RSAENH.@)
1972 * Releases the handle to a hash object. The object is destroyed if its reference
1973 * count reaches zero.
1976 * hProv [I] Handle to the key container to which the hash object belongs.
1977 * hHash [I] Handle to the hash object to be released.
1983 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
1985 TRACE("(hProv=%08lx, hHash=%08lx)\n", hProv
, hHash
);
1987 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
1989 SetLastError(NTE_BAD_UID
);
1993 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
1995 SetLastError(NTE_BAD_HASH
);
2002 /******************************************************************************
2003 * CPDestroyKey (RSAENH.@)
2005 * Releases the handle to a key object. The object is destroyed if its reference
2006 * count reaches zero.
2009 * hProv [I] Handle to the key container to which the key object belongs.
2010 * hKey [I] Handle to the key object to be released.
2016 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
2018 TRACE("(hProv=%08lx, hKey=%08lx)\n", hProv
, hKey
);
2020 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2022 SetLastError(NTE_BAD_UID
);
2026 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
2028 SetLastError(NTE_BAD_KEY
);
2035 /******************************************************************************
2036 * CPDuplicateHash (RSAENH.@)
2038 * Clones a hash object including its current state.
2041 * hUID [I] Handle to the key container the hash belongs to.
2042 * hHash [I] Handle to the hash object to be cloned.
2043 * pdwReserved [I] Reserved. Must be NULL.
2044 * dwFlags [I] No flags are currently defined. Must be 0.
2045 * phHash [O] Handle to the cloned hash object.
2051 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2052 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2054 CRYPTHASH
*pSrcHash
, *pDestHash
;
2056 TRACE("(hUID=%08lx, hHash=%08lx, pdwReserved=%p, dwFlags=%08x, phHash=%p)\n", hUID
, hHash
,
2057 pdwReserved
, dwFlags
, phHash
);
2059 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2061 SetLastError(NTE_BAD_UID
);
2065 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2067 SetLastError(NTE_BAD_HASH
);
2071 if (!phHash
|| pdwReserved
|| dwFlags
)
2073 SetLastError(ERROR_INVALID_PARAMETER
);
2077 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2078 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2079 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2081 *pDestHash
= *pSrcHash
;
2082 duplicate_hash_impl(pSrcHash
->aiAlgid
, &pSrcHash
->context
, &pDestHash
->context
);
2083 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2084 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2085 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2088 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2091 /******************************************************************************
2092 * CPDuplicateKey (RSAENH.@)
2094 * Clones a key object including its current state.
2097 * hUID [I] Handle to the key container the hash belongs to.
2098 * hKey [I] Handle to the key object to be cloned.
2099 * pdwReserved [I] Reserved. Must be NULL.
2100 * dwFlags [I] No flags are currently defined. Must be 0.
2101 * phHash [O] Handle to the cloned key object.
2107 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2108 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2110 CRYPTKEY
*pSrcKey
, *pDestKey
;
2112 TRACE("(hUID=%08lx, hKey=%08lx, pdwReserved=%p, dwFlags=%08x, phKey=%p)\n", hUID
, hKey
,
2113 pdwReserved
, dwFlags
, phKey
);
2115 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2117 SetLastError(NTE_BAD_UID
);
2121 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2123 SetLastError(NTE_BAD_KEY
);
2127 if (!phKey
|| pdwReserved
|| dwFlags
)
2129 SetLastError(ERROR_INVALID_PARAMETER
);
2133 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2134 (OBJECTHDR
**)&pDestKey
);
2135 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2137 *pDestKey
= *pSrcKey
;
2138 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2139 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2140 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2141 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2142 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2151 /******************************************************************************
2152 * CPEncrypt (RSAENH.@)
2157 * hProv [I] The key container hKey and hHash belong to.
2158 * hKey [I] The key used to encrypt the data.
2159 * hHash [I] An optional hash object for parallel hashing. See notes.
2160 * Final [I] Indicates if this is the last block of data to encrypt.
2161 * dwFlags [I] Currently no flags defined. Must be zero.
2162 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2163 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2164 * dwBufLen [I] Size of the buffer at pbData.
2171 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2172 * This is useful for message signatures.
2174 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2176 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2177 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2179 CRYPTKEY
*pCryptKey
;
2180 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2181 DWORD dwEncryptedLen
, i
, j
, k
;
2183 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2184 "pdwDataLen=%p, dwBufLen=%d)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2187 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2189 SetLastError(NTE_BAD_UID
);
2195 SetLastError(NTE_BAD_FLAGS
);
2199 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2201 SetLastError(NTE_BAD_KEY
);
2205 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2206 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2208 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2210 SetLastError(NTE_BAD_DATA
);
2214 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2215 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2218 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2219 if (!Final
&& (*pdwDataLen
% pCryptKey
->dwBlockLen
)) {
2220 SetLastError(NTE_BAD_DATA
);
2224 dwEncryptedLen
= (*pdwDataLen
/pCryptKey
->dwBlockLen
+(Final
?1:0))*pCryptKey
->dwBlockLen
;
2226 if (pbData
== NULL
) {
2227 *pdwDataLen
= dwEncryptedLen
;
2230 else if (dwEncryptedLen
> dwBufLen
) {
2231 *pdwDataLen
= dwEncryptedLen
;
2232 SetLastError(ERROR_MORE_DATA
);
2236 /* Pad final block with length bytes */
2237 for (i
=*pdwDataLen
; i
<dwEncryptedLen
; i
++) pbData
[i
] = dwEncryptedLen
- *pdwDataLen
;
2238 *pdwDataLen
= dwEncryptedLen
;
2240 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2241 switch (pCryptKey
->dwMode
) {
2242 case CRYPT_MODE_ECB
:
2243 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2247 case CRYPT_MODE_CBC
:
2248 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) in
[j
] ^= pCryptKey
->abChainVector
[j
];
2249 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2251 memcpy(pCryptKey
->abChainVector
, out
, pCryptKey
->dwBlockLen
);
2254 case CRYPT_MODE_CFB
:
2255 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2256 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2257 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2258 out
[j
] = in
[j
] ^ o
[0];
2259 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2260 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2261 pCryptKey
->abChainVector
[k
] = out
[j
];
2266 SetLastError(NTE_BAD_ALGID
);
2269 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2271 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2272 if (pbData
== NULL
) {
2273 *pdwDataLen
= dwBufLen
;
2276 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2277 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2278 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2279 SetLastError(NTE_BAD_KEY
);
2283 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2286 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2287 SetLastError(ERROR_MORE_DATA
);
2290 if (!pad_data(pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2291 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2292 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2295 SetLastError(NTE_BAD_TYPE
);
2299 if (Final
) setup_key(pCryptKey
);
2304 /******************************************************************************
2305 * CPDecrypt (RSAENH.@)
2310 * hProv [I] The key container hKey and hHash belong to.
2311 * hKey [I] The key used to decrypt the data.
2312 * hHash [I] An optional hash object for parallel hashing. See notes.
2313 * Final [I] Indicates if this is the last block of data to decrypt.
2314 * dwFlags [I] Currently no flags defined. Must be zero.
2315 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2316 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2323 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2324 * This is useful for message signatures.
2326 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2328 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2329 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2331 CRYPTKEY
*pCryptKey
;
2332 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2336 TRACE("(hProv=%08lx, hKey=%08lx, hHash=%08lx, Final=%d, dwFlags=%08x, pbData=%p, "
2337 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2339 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2341 SetLastError(NTE_BAD_UID
);
2347 SetLastError(NTE_BAD_FLAGS
);
2351 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2353 SetLastError(NTE_BAD_KEY
);
2357 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2358 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2360 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2362 SetLastError(NTE_BAD_DATA
);
2368 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2369 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2370 switch (pCryptKey
->dwMode
) {
2371 case CRYPT_MODE_ECB
:
2372 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2376 case CRYPT_MODE_CBC
:
2377 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2379 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2380 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2383 case CRYPT_MODE_CFB
:
2384 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2385 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2386 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2387 out
[j
] = in
[j
] ^ o
[0];
2388 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2389 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2390 pCryptKey
->abChainVector
[k
] = in
[j
];
2395 SetLastError(NTE_BAD_ALGID
);
2398 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2401 if (pbData
[*pdwDataLen
-1] &&
2402 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2403 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2404 BOOL padOkay
= TRUE
;
2406 /* check that every bad byte has the same value */
2407 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2408 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2411 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2413 SetLastError(NTE_BAD_DATA
);
2414 setup_key(pCryptKey
);
2419 SetLastError(NTE_BAD_DATA
);
2420 setup_key(pCryptKey
);
2425 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2426 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2427 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2428 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2429 SetLastError(NTE_BAD_KEY
);
2432 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2433 if (!unpad_data(pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2436 SetLastError(NTE_BAD_TYPE
);
2440 if (Final
) setup_key(pCryptKey
);
2442 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2443 if (*pdwDataLen
>dwMax
||
2444 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2450 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2451 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2453 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2454 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2457 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2458 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2462 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2464 if (*pdwDataLen
< dwDataLen
) {
2465 SetLastError(ERROR_MORE_DATA
);
2466 *pdwDataLen
= dwDataLen
;
2470 pBlobHeader
->bType
= SIMPLEBLOB
;
2471 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2472 pBlobHeader
->reserved
= 0;
2473 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2475 *pAlgid
= pPubKey
->aiAlgid
;
2477 if (!pad_data(pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2478 pPubKey
->dwBlockLen
, dwFlags
))
2483 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2484 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2486 *pdwDataLen
= dwDataLen
;
2490 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2493 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2494 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2497 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2498 SetLastError(NTE_BAD_KEY
);
2502 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2504 if (*pdwDataLen
< dwDataLen
) {
2505 SetLastError(ERROR_MORE_DATA
);
2506 *pdwDataLen
= dwDataLen
;
2510 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2511 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2512 pBlobHeader
->reserved
= 0;
2513 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2515 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2516 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2518 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2519 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2521 *pdwDataLen
= dwDataLen
;
2525 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2526 BYTE
*pbData
, DWORD
*pdwDataLen
)
2528 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2529 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2532 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2533 SetLastError(NTE_BAD_KEY
);
2536 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2538 SetLastError(NTE_BAD_KEY_STATE
);
2542 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2543 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2545 if (*pdwDataLen
< dwDataLen
) {
2546 SetLastError(ERROR_MORE_DATA
);
2547 *pdwDataLen
= dwDataLen
;
2551 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2552 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2553 pBlobHeader
->reserved
= 0;
2554 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2556 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2557 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2559 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2560 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2562 *pdwDataLen
= dwDataLen
;
2566 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2569 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2570 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2571 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2574 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2576 if (*pdwDataLen
< dwDataLen
) {
2577 SetLastError(ERROR_MORE_DATA
);
2578 *pdwDataLen
= dwDataLen
;
2582 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2583 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2584 pBlobHeader
->reserved
= 0;
2585 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2587 *pKeyLen
= pCryptKey
->dwKeyLen
;
2588 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2590 *pdwDataLen
= dwDataLen
;
2593 /******************************************************************************
2594 * crypt_export_key [Internal]
2596 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2597 * by store_key_pair.
2600 * pCryptKey [I] Key to be exported.
2601 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2602 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2603 * dwFlags [I] Currently none defined.
2604 * force [I] If TRUE, the key is written no matter what the key's
2605 * permissions are. Otherwise the key's permissions are
2606 * checked before exporting.
2607 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2608 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2614 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
2615 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
2616 BYTE
*pbData
, DWORD
*pdwDataLen
)
2620 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
2621 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
2622 SetLastError(NTE_BAD_KEY
);
2627 switch ((BYTE
)dwBlobType
)
2630 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
2631 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
2634 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
2638 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
2639 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2643 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
2645 case PRIVATEKEYBLOB
:
2646 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
2648 case PLAINTEXTKEYBLOB
:
2649 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
2652 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
2657 /******************************************************************************
2658 * CPExportKey (RSAENH.@)
2660 * Export a key into a binary large object (BLOB).
2663 * hProv [I] Key container from which a key is to be exported.
2664 * hKey [I] Key to be exported.
2665 * hPubKey [I] Key used to encrypt sensitive BLOB data.
2666 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
2667 * dwFlags [I] Currently none defined.
2668 * pbData [O] Pointer to a buffer where the BLOB will be written to.
2669 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
2675 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
2676 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2678 CRYPTKEY
*pCryptKey
;
2680 TRACE("(hProv=%08lx, hKey=%08lx, hPubKey=%08lx, dwBlobType=%08x, dwFlags=%08x, pbData=%p,"
2681 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
2683 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2685 SetLastError(NTE_BAD_UID
);
2689 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2691 SetLastError(NTE_BAD_KEY
);
2695 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
2696 pbData
, pdwDataLen
);
2699 /******************************************************************************
2700 * release_and_install_key [Internal]
2702 * Release an existing key, if present, and replaces it with a new one.
2705 * hProv [I] Key container into which the key is to be imported.
2706 * src [I] Key which will replace *dest
2707 * dest [I] Points to key to be released and replaced with src
2708 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
2710 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
2711 HCRYPTKEY
*dest
, DWORD fStoreKey
)
2713 RSAENH_CPDestroyKey(hProv
, *dest
);
2714 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
2717 KEYCONTAINER
*pKeyContainer
;
2719 if ((pKeyContainer
= get_key_container(hProv
)))
2721 store_key_container_keys(pKeyContainer
);
2722 store_key_container_permissions(pKeyContainer
);
2727 /******************************************************************************
2728 * import_private_key [Internal]
2730 * Import a BLOB'ed private key into a key container.
2733 * hProv [I] Key container into which the private key is to be imported.
2734 * pbData [I] Pointer to a buffer which holds the private key BLOB.
2735 * dwDataLen [I] Length of data in buffer at pbData.
2736 * dwFlags [I] One of:
2737 * CRYPT_EXPORTABLE: the imported key is marked exportable
2738 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
2739 * phKey [O] Handle to the imported key.
2743 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2744 * it's a PRIVATEKEYBLOB.
2750 static BOOL
import_private_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2751 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
2753 KEYCONTAINER
*pKeyContainer
;
2754 CRYPTKEY
*pCryptKey
;
2755 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2756 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
2759 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2761 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2762 SetLastError(NTE_BAD_FLAGS
);
2765 if (!(pKeyContainer
= get_key_container(hProv
)))
2768 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
2770 ERR("datalen %d not long enough for a BLOBHEADER + RSAPUBKEY\n",
2772 SetLastError(NTE_BAD_DATA
);
2775 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
2777 ERR("unexpected magic %08x\n", pRSAPubKey
->magic
);
2778 SetLastError(NTE_BAD_DATA
);
2781 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2782 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
2784 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2785 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
2787 ERR("blob too short for pub key: expect %d, got %d\n",
2788 expectedLen
, dwDataLen
);
2789 SetLastError(NTE_BAD_DATA
);
2793 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2794 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2795 setup_key(pCryptKey
);
2796 ret
= import_private_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2797 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
2799 if (dwFlags
& CRYPT_EXPORTABLE
)
2800 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2801 switch (pBlobHeader
->aiKeyAlg
)
2805 TRACE("installing signing key\n");
2806 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
2809 case AT_KEYEXCHANGE
:
2811 TRACE("installing key exchange key\n");
2812 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
2820 /******************************************************************************
2821 * import_public_key [Internal]
2823 * Import a BLOB'ed public key.
2827 * pbData [I] Pointer to a buffer which holds the public key BLOB.
2828 * dwDataLen [I] Length of data in buffer at pbData.
2829 * dwFlags [I] One of:
2830 * CRYPT_EXPORTABLE: the imported key is marked exportable
2831 * phKey [O] Handle to the imported key.
2835 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2836 * it's a PUBLICKEYBLOB.
2842 static BOOL
import_public_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2843 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2845 CRYPTKEY
*pCryptKey
;
2846 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2847 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
2851 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2853 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2854 SetLastError(NTE_BAD_FLAGS
);
2858 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
2859 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
2860 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
2862 SetLastError(NTE_BAD_DATA
);
2866 /* Since this is a public key blob, only the public key is
2867 * available, so only signature verification is possible.
2869 algID
= pBlobHeader
->aiKeyAlg
;
2870 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
2871 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
2872 setup_key(pCryptKey
);
2873 ret
= import_public_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2874 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
2876 if (dwFlags
& CRYPT_EXPORTABLE
)
2877 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2882 /******************************************************************************
2883 * import_symmetric_key [Internal]
2885 * Import a BLOB'ed symmetric key into a key container.
2888 * hProv [I] Key container into which the symmetric key is to be imported.
2889 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
2890 * dwDataLen [I] Length of data in buffer at pbData.
2891 * hPubKey [I] Key used to decrypt sensitive BLOB data.
2892 * dwFlags [I] One of:
2893 * CRYPT_EXPORTABLE: the imported key is marked exportable
2894 * phKey [O] Handle to the imported key.
2898 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2899 * it's a SIMPLEBLOB.
2905 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2906 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
2908 CRYPTKEY
*pCryptKey
, *pPubKey
;
2909 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2910 const ALG_ID
*pAlgid
= (const ALG_ID
*)(pBlobHeader
+1);
2911 const BYTE
*pbKeyStream
= (const BYTE
*)(pAlgid
+ 1);
2915 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2917 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
2918 SetLastError(NTE_BAD_FLAGS
);
2921 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
2922 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
2924 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
2928 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
2930 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2934 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, pPubKey
->dwBlockLen
);
2935 if (!pbDecrypted
) return FALSE
;
2936 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
2939 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
2940 if (!unpad_data(pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
2941 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2945 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
2946 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2948 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2951 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
2952 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
2953 setup_key(pCryptKey
);
2954 if (dwFlags
& CRYPT_EXPORTABLE
)
2955 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
2959 /******************************************************************************
2960 * import_plaintext_key [Internal]
2962 * Import a plaintext key into a key container.
2965 * hProv [I] Key container into which the symmetric key is to be imported.
2966 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
2967 * dwDataLen [I] Length of data in buffer at pbData.
2968 * dwFlags [I] One of:
2969 * CRYPT_EXPORTABLE: the imported key is marked exportable
2970 * phKey [O] Handle to the imported key.
2974 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
2975 * it's a PLAINTEXTKEYBLOB.
2981 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
2982 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2984 CRYPTKEY
*pCryptKey
;
2985 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
2986 const DWORD
*pKeyLen
= (const DWORD
*)(pBlobHeader
+ 1);
2987 const BYTE
*pbKeyStream
= (const BYTE
*)(pKeyLen
+ 1);
2989 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
2991 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
2995 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
2997 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
2998 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3000 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
3002 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3003 pCryptKey
->dwKeyLen
= *pKeyLen
;
3007 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
3009 /* In order to initialize an HMAC key, the key material is hashed,
3010 * and the output of the hash function is used as the key material.
3011 * Unfortunately, the way the Crypto API is designed, we don't know
3012 * the hash algorithm yet, so we have to copy the entire key
3015 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3017 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3018 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3022 setup_key(pCryptKey
);
3023 if (dwFlags
& CRYPT_EXPORTABLE
)
3024 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3028 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3029 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3031 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3032 setup_key(pCryptKey
);
3033 if (dwFlags
& CRYPT_EXPORTABLE
)
3034 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3039 /******************************************************************************
3040 * import_key [Internal]
3042 * Import a BLOB'ed key into a key container, optionally storing the key's
3043 * value to the registry.
3046 * hProv [I] Key container into which the key is to be imported.
3047 * pbData [I] Pointer to a buffer which holds the BLOB.
3048 * dwDataLen [I] Length of data in buffer at pbData.
3049 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3050 * dwFlags [I] One of:
3051 * CRYPT_EXPORTABLE: the imported key is marked exportable
3052 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3053 * phKey [O] Handle to the imported key.
3059 static BOOL
import_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
, HCRYPTKEY hPubKey
,
3060 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
3062 KEYCONTAINER
*pKeyContainer
;
3063 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3065 if (!(pKeyContainer
= get_key_container(hProv
)))
3068 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3069 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3070 pBlobHeader
->reserved
!= 0)
3072 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3073 pBlobHeader
->reserved
);
3074 SetLastError(NTE_BAD_DATA
);
3078 /* If this is a verify-only context, the key is not persisted regardless of
3079 * fStoreKey's original value.
3081 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3082 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3083 switch (pBlobHeader
->bType
)
3085 case PRIVATEKEYBLOB
:
3086 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3090 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3094 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3097 case PLAINTEXTKEYBLOB
:
3098 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3102 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3107 /******************************************************************************
3108 * CPImportKey (RSAENH.@)
3110 * Import a BLOB'ed key into a key container.
3113 * hProv [I] Key container into which the key is to be imported.
3114 * pbData [I] Pointer to a buffer which holds the BLOB.
3115 * dwDataLen [I] Length of data in buffer at pbData.
3116 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3117 * dwFlags [I] One of:
3118 * CRYPT_EXPORTABLE: the imported key is marked exportable
3119 * phKey [O] Handle to the imported key.
3125 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3126 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3128 TRACE("(hProv=%08lx, pbData=%p, dwDataLen=%d, hPubKey=%08lx, dwFlags=%08x, phKey=%p)\n",
3129 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3131 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3134 /******************************************************************************
3135 * CPGenKey (RSAENH.@)
3137 * Generate a key in the key container
3140 * hProv [I] Key container for which a key is to be generated.
3141 * Algid [I] Crypto algorithm identifier for the key to be generated.
3142 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3143 * phKey [O] Handle to the generated key.
3150 * Flags currently not considered.
3153 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3154 * and AT_SIGNATURE values.
3156 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3158 KEYCONTAINER
*pKeyContainer
;
3159 CRYPTKEY
*pCryptKey
;
3161 TRACE("(hProv=%08lx, aiAlgid=%d, dwFlags=%08x, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3163 if (!(pKeyContainer
= get_key_container(hProv
)))
3165 /* MSDN: hProv not containing valid context handle */
3173 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3175 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3176 setup_key(pCryptKey
);
3177 release_and_install_key(hProv
, *phKey
,
3178 &pKeyContainer
->hSignatureKeyPair
,
3183 case AT_KEYEXCHANGE
:
3185 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3187 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3188 setup_key(pCryptKey
);
3189 release_and_install_key(hProv
, *phKey
,
3190 &pKeyContainer
->hKeyExchangeKeyPair
,
3204 case CALG_PCT1_MASTER
:
3205 case CALG_SSL2_MASTER
:
3206 case CALG_SSL3_MASTER
:
3207 case CALG_TLS1_MASTER
:
3208 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3210 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3212 case CALG_SSL3_MASTER
:
3213 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3214 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3217 case CALG_TLS1_MASTER
:
3218 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3219 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3222 setup_key(pCryptKey
);
3227 /* MSDN: Algorithm not supported specified by Algid */
3228 SetLastError(NTE_BAD_ALGID
);
3232 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3235 /******************************************************************************
3236 * CPGenRandom (RSAENH.@)
3238 * Generate a random byte stream.
3241 * hProv [I] Key container that is used to generate random bytes.
3242 * dwLen [I] Specifies the number of requested random data bytes.
3243 * pbBuffer [O] Random bytes will be stored here.
3249 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3251 TRACE("(hProv=%08lx, dwLen=%d, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3253 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3255 /* MSDN: hProv not containing valid context handle */
3256 SetLastError(NTE_BAD_UID
);
3260 return gen_rand_impl(pbBuffer
, dwLen
);
3263 /******************************************************************************
3264 * CPGetHashParam (RSAENH.@)
3266 * Query parameters of an hash object.
3269 * hProv [I] The kea container, which the hash belongs to.
3270 * hHash [I] The hash object that is to be queried.
3271 * dwParam [I] Specifies the parameter that is to be queried.
3272 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3273 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3274 * dwFlags [I] None currently defined.
3281 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3282 * finalized if HP_HASHVALUE is queried.
3284 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3285 DWORD
*pdwDataLen
, DWORD dwFlags
)
3287 CRYPTHASH
*pCryptHash
;
3289 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3290 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3292 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3294 SetLastError(NTE_BAD_UID
);
3300 SetLastError(NTE_BAD_FLAGS
);
3304 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3305 (OBJECTHDR
**)&pCryptHash
))
3307 SetLastError(NTE_BAD_HASH
);
3313 SetLastError(ERROR_INVALID_PARAMETER
);
3320 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->aiAlgid
,
3324 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->dwHashSize
,
3328 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3329 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3330 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3333 if ( pbData
== NULL
) {
3334 *pdwDataLen
= pCryptHash
->dwHashSize
;
3338 if (pbData
&& (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
))
3340 finalize_hash(pCryptHash
);
3341 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3344 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3345 pCryptHash
->dwHashSize
);
3348 SetLastError(NTE_BAD_TYPE
);
3353 /******************************************************************************
3354 * CPSetKeyParam (RSAENH.@)
3356 * Set a parameter of a key object
3359 * hProv [I] The key container to which the key belongs.
3360 * hKey [I] The key for which a parameter is to be set.
3361 * dwParam [I] Parameter type. See Notes.
3362 * pbData [I] Pointer to the parameter value.
3363 * dwFlags [I] Currently none defined.
3370 * Defined dwParam types are:
3371 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3372 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3373 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3374 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3375 * - KP_IV: Initialization vector
3377 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3380 CRYPTKEY
*pCryptKey
;
3382 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, hKey
,
3383 dwParam
, pbData
, dwFlags
);
3385 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3387 SetLastError(NTE_BAD_UID
);
3392 SetLastError(NTE_BAD_FLAGS
);
3396 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3398 SetLastError(NTE_BAD_KEY
);
3404 /* The MS providers only support PKCS5_PADDING */
3405 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3406 SetLastError(NTE_BAD_DATA
);
3412 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3416 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3419 case KP_PERMISSIONS
:
3421 DWORD perms
= *(DWORD
*)pbData
;
3423 if ((perms
& CRYPT_EXPORT
) &&
3424 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3426 SetLastError(NTE_BAD_DATA
);
3429 else if (!(perms
& CRYPT_EXPORT
) &&
3430 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3432 /* Clearing the export permission appears to be ignored,
3435 perms
|= CRYPT_EXPORT
;
3437 pCryptKey
->dwPermissions
= perms
;
3442 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3443 setup_key(pCryptKey
);
3447 switch (pCryptKey
->aiAlgid
) {
3451 KEYCONTAINER
*pKeyContainer
= get_key_container(pCryptKey
->hProv
);
3454 SetLastError(ERROR_INVALID_PARAMETER
);
3457 /* MSDN: the base provider always sets eleven bytes of
3460 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3462 pCryptKey
->dwSaltLen
= 11;
3463 setup_key(pCryptKey
);
3464 /* After setting the salt value if the provider is not base or
3465 * strong the salt length will be reset. */
3466 if (pKeyContainer
->dwPersonality
!= RSAENH_PERSONALITY_BASE
&&
3467 pKeyContainer
->dwPersonality
!= RSAENH_PERSONALITY_STRONG
)
3468 pCryptKey
->dwSaltLen
= 0;
3472 SetLastError(NTE_BAD_KEY
);
3479 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3481 /* salt length can't be greater than 184 bits = 24 bytes */
3482 if (blob
->cbData
> 24)
3484 SetLastError(NTE_BAD_DATA
);
3487 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3489 pCryptKey
->dwSaltLen
= blob
->cbData
;
3490 setup_key(pCryptKey
);
3494 case KP_EFFECTIVE_KEYLEN
:
3495 switch (pCryptKey
->aiAlgid
) {
3498 DWORD keylen
, deflen
;
3500 KEYCONTAINER
*pKeyContainer
= get_key_container(pCryptKey
->hProv
);
3504 SetLastError(ERROR_INVALID_PARAMETER
);
3507 keylen
= *(DWORD
*)pbData
;
3508 if (!keylen
|| keylen
> 1024)
3510 SetLastError(NTE_BAD_DATA
);
3515 * The Base provider will force the key length to default
3516 * and set an error state if a key length different from
3517 * the default is tried.
3519 deflen
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]->dwDefaultLen
;
3520 if (pKeyContainer
->dwPersonality
== RSAENH_PERSONALITY_BASE
3521 && keylen
!= deflen
)
3524 SetLastError(NTE_BAD_DATA
);
3527 pCryptKey
->dwEffectiveKeyLen
= keylen
;
3528 setup_key(pCryptKey
);
3532 SetLastError(NTE_BAD_TYPE
);
3537 case KP_SCHANNEL_ALG
:
3538 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3539 case SCHANNEL_ENC_KEY
:
3540 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3543 case SCHANNEL_MAC_KEY
:
3544 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3548 SetLastError(NTE_FAIL
); /* FIXME: error code */
3553 case KP_CLIENT_RANDOM
:
3554 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3556 case KP_SERVER_RANDOM
:
3557 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3560 SetLastError(NTE_BAD_TYPE
);
3565 /******************************************************************************
3566 * CPGetKeyParam (RSAENH.@)
3568 * Query a key parameter.
3571 * hProv [I] The key container, which the key belongs to.
3572 * hHash [I] The key object that is to be queried.
3573 * dwParam [I] Specifies the parameter that is to be queried.
3574 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3575 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3576 * dwFlags [I] None currently defined.
3583 * Defined dwParam types are:
3584 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3585 * - KP_MODE_BITS: Shift width for cipher feedback mode.
3586 * (Currently ignored by MS CSP's - always eight)
3587 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3588 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3589 * - KP_IV: Initialization vector.
3590 * - KP_KEYLEN: Bitwidth of the key.
3591 * - KP_BLOCKLEN: Size of a block cipher block.
3592 * - KP_SALT: Salt value.
3594 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3595 DWORD
*pdwDataLen
, DWORD dwFlags
)
3597 CRYPTKEY
*pCryptKey
;
3600 TRACE("(hProv=%08lx, hKey=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p dwFlags=%08x)\n",
3601 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3603 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3605 SetLastError(NTE_BAD_UID
);
3610 SetLastError(NTE_BAD_FLAGS
);
3614 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3616 SetLastError(NTE_BAD_KEY
);
3623 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
3624 pCryptKey
->dwBlockLen
);
3627 switch (pCryptKey
->aiAlgid
) {
3630 return copy_param(pbData
, pdwDataLen
,
3631 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
3632 pCryptKey
->dwSaltLen
);
3634 SetLastError(NTE_BAD_KEY
);
3639 dwValue
= PKCS5_PADDING
;
3640 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3643 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3644 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3646 case KP_EFFECTIVE_KEYLEN
:
3647 if (pCryptKey
->dwEffectiveKeyLen
)
3648 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
3650 dwValue
= pCryptKey
->dwKeyLen
<< 3;
3651 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3654 dwValue
= pCryptKey
->dwBlockLen
<< 3;
3655 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
3658 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
3661 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwModeBits
,
3664 case KP_PERMISSIONS
:
3665 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwPermissions
,
3669 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
3672 SetLastError(NTE_BAD_TYPE
);
3677 /******************************************************************************
3678 * CPGetProvParam (RSAENH.@)
3680 * Query a CSP parameter.
3683 * hProv [I] The key container that is to be queried.
3684 * dwParam [I] Specifies the parameter that is to be queried.
3685 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3686 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3687 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
3693 * Defined dwParam types:
3694 * - PP_CONTAINER: Name of the key container.
3695 * - PP_NAME: Name of the cryptographic service provider.
3696 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
3697 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
3698 * - PP_ENUMALGS{_EX}: Query provider capabilities.
3699 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
3701 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
3702 DWORD
*pdwDataLen
, DWORD dwFlags
)
3704 KEYCONTAINER
*pKeyContainer
;
3705 PROV_ENUMALGS provEnumalgs
;
3709 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
3710 * IE6 SP1 asks for it in the 'About' dialog.
3711 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
3712 * to be 'don't care's. If you know anything more specific about
3713 * this provider parameter, please report to wine-devel@winehq.org */
3714 static const BYTE abWTF
[96] = {
3715 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
3716 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
3717 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
3718 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
3719 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
3720 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
3721 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
3722 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
3723 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
3724 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
3725 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
3726 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
3729 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, pdwDataLen=%p, dwFlags=%08x)\n",
3730 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3733 SetLastError(ERROR_INVALID_PARAMETER
);
3737 if (!(pKeyContainer
= get_key_container(hProv
)))
3739 /* MSDN: hProv not containing valid context handle */
3746 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
3747 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szName
,
3748 strlen(pKeyContainer
->szName
)+1);
3751 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szProvName
,
3752 strlen(pKeyContainer
->szProvName
)+1);
3755 dwTemp
= PROV_RSA_FULL
;
3756 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3759 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
3760 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3762 case PP_KEYSET_TYPE
:
3763 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
3764 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3767 dwTemp
= CRYPT_SEC_DESCR
;
3768 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3770 case PP_SIG_KEYSIZE_INC
:
3771 case PP_KEYX_KEYSIZE_INC
:
3773 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3776 dwTemp
= CRYPT_IMPL_SOFTWARE
;
3777 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3780 dwTemp
= 0x00000200;
3781 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
3783 case PP_ENUMCONTAINERS
:
3784 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
3787 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3791 if (!open_container_key("", dwFlags
, KEY_READ
, &hKey
))
3793 SetLastError(ERROR_NO_MORE_ITEMS
);
3797 dwTemp
= *pdwDataLen
;
3798 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
3799 NULL
, NULL
, NULL
, NULL
))
3801 case ERROR_MORE_DATA
:
3802 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
3805 pKeyContainer
->dwEnumContainersCtr
++;
3809 case ERROR_NO_MORE_ITEMS
:
3811 SetLastError(ERROR_NO_MORE_ITEMS
);
3817 case PP_ENUMALGS_EX
:
3818 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
3819 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
3820 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
3821 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
3823 SetLastError(ERROR_NO_MORE_ITEMS
);
3827 if (dwParam
== PP_ENUMALGS
) {
3828 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
3829 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3830 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3832 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
3833 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
3834 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
3835 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
3836 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
3837 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
3838 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
3839 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
3842 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&provEnumalgs
,
3843 sizeof(PROV_ENUMALGS
));
3845 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
3846 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
3847 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
3849 return copy_param(pbData
, pdwDataLen
,
3850 (const BYTE
*)&aProvEnumAlgsEx
3851 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
3852 sizeof(PROV_ENUMALGS_EX
));
3855 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
3856 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
3858 case PP_KEYSET_SEC_DESCR
:
3860 SECURITY_DESCRIPTOR
*sd
;
3861 DWORD err
, len
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
3863 if (!open_container_key(pKeyContainer
->szName
, flags
, KEY_READ
, &hKey
))
3865 SetLastError(NTE_BAD_KEYSET
);
3869 err
= GetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, NULL
, NULL
, NULL
, NULL
, (void **)&sd
);
3877 len
= GetSecurityDescriptorLength(sd
);
3878 if (*pdwDataLen
>= len
) memcpy(pbData
, sd
, len
);
3879 else SetLastError(ERROR_INSUFFICIENT_BUFFER
);
3887 /* MSDN: Unknown parameter number in dwParam */
3888 SetLastError(NTE_BAD_TYPE
);
3893 /******************************************************************************
3894 * CPDeriveKey (RSAENH.@)
3896 * Derives a key from a hash value.
3899 * hProv [I] Key container for which a key is to be generated.
3900 * Algid [I] Crypto algorithm identifier for the key to be generated.
3901 * hBaseData [I] Hash from whose value the key will be derived.
3902 * dwFlags [I] See Notes.
3903 * phKey [O] The generated key.
3911 * - CRYPT_EXPORTABLE: Key can be exported.
3912 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
3913 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
3915 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
3916 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3918 CRYPTKEY
*pCryptKey
, *pMasterKey
;
3919 CRYPTHASH
*pCryptHash
;
3920 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
3923 TRACE("(hProv=%08lx, Algid=%d, hBaseData=%08lx, dwFlags=%08x phKey=%p)\n", hProv
, Algid
,
3924 hBaseData
, dwFlags
, phKey
);
3926 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3928 SetLastError(NTE_BAD_UID
);
3932 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
3933 (OBJECTHDR
**)&pCryptHash
))
3935 SetLastError(NTE_BAD_HASH
);
3941 SetLastError(ERROR_INVALID_PARAMETER
);
3945 switch (GET_ALG_CLASS(Algid
))
3947 case ALG_CLASS_DATA_ENCRYPT
:
3949 int need_padding
, copy_len
;
3950 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3951 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3954 * We derive the key material from the hash.
3955 * If the hash value is not large enough for the claimed key, we have to construct
3956 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
3958 dwLen
= RSAENH_MAX_HASH_SIZE
;
3959 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
3962 * The usage of padding seems to vary from algorithm to algorithm.
3963 * For now the only different case found was for AES with 128 bit key.
3968 /* To reduce the chance of regressions we will only deviate
3969 * from the old behavior for the tested hash lengths */
3970 if (dwLen
== 16 || dwLen
== 20)
3976 need_padding
= dwLen
< pCryptKey
->dwKeyLen
;
3979 copy_len
= pCryptKey
->dwKeyLen
;
3982 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
3983 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
3986 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
3988 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
3989 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3990 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
3993 init_hash(pCryptHash
);
3994 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
3995 finalize_hash(pCryptHash
);
3996 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
3998 init_hash(pCryptHash
);
3999 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
4000 finalize_hash(pCryptHash
);
4001 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
4002 pCryptHash
->dwHashSize
);
4004 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
4007 * Padding was not required, we have more hash than needed.
4008 * Do we need to use the remaining hash as salt?
4010 else if((dwFlags
& CRYPT_CREATE_SALT
) &&
4011 (Algid
== CALG_RC2
|| Algid
== CALG_RC4
))
4013 copy_len
+= pCryptKey
->dwSaltLen
;
4016 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
4017 RSAENH_MIN(copy_len
, sizeof(pCryptKey
->abKeyValue
)));
4020 case ALG_CLASS_MSG_ENCRYPT
:
4021 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4022 (OBJECTHDR
**)&pMasterKey
))
4024 SetLastError(NTE_FAIL
); /* FIXME error code */
4030 /* See RFC 2246, chapter 6.3 Key calculation */
4031 case CALG_SCHANNEL_ENC_KEY
:
4032 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
4033 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
4035 SetLastError(NTE_BAD_FLAGS
);
4038 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
4039 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
4041 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4042 memcpy(pCryptKey
->abKeyValue
,
4043 pCryptHash
->abHashValue
+ (
4044 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4045 ((dwFlags
& CRYPT_SERVER
) ?
4046 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
4047 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
4048 memcpy(pCryptKey
->abInitVector
,
4049 pCryptHash
->abHashValue
+ (
4050 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4051 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
4052 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
4053 pCryptKey
->dwBlockLen
);
4056 case CALG_SCHANNEL_MAC_KEY
:
4057 *phKey
= new_key(hProv
, Algid
,
4058 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
4060 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4061 memcpy(pCryptKey
->abKeyValue
,
4062 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
4063 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
4064 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
4068 SetLastError(NTE_BAD_ALGID
);
4074 SetLastError(NTE_BAD_ALGID
);
4078 setup_key(pCryptKey
);
4082 /******************************************************************************
4083 * CPGetUserKey (RSAENH.@)
4085 * Returns a handle to the user's private key-exchange- or signature-key.
4088 * hProv [I] The key container from which a user key is requested.
4089 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4090 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4097 * A newly created key container does not contain private user key. Create them with CPGenKey.
4099 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4101 KEYCONTAINER
*pKeyContainer
;
4103 TRACE("(hProv=%08lx, dwKeySpec=%08x, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4105 if (!(pKeyContainer
= get_key_container(hProv
)))
4107 /* MSDN: hProv not containing valid context handle */
4113 case AT_KEYEXCHANGE
:
4114 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4119 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4124 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4127 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4129 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4130 SetLastError(NTE_NO_KEY
);
4137 /******************************************************************************
4138 * CPHashData (RSAENH.@)
4140 * Updates a hash object with the given data.
4143 * hProv [I] Key container to which the hash object belongs.
4144 * hHash [I] Hash object which is to be updated.
4145 * pbData [I] Pointer to data with which the hash object is to be updated.
4146 * dwDataLen [I] Length of the data.
4147 * dwFlags [I] Currently none defined.
4154 * The actual hash value is queried with CPGetHashParam, which will finalize
4155 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4157 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbData
,
4158 DWORD dwDataLen
, DWORD dwFlags
)
4160 CRYPTHASH
*pCryptHash
;
4162 TRACE("(hProv=%08lx, hHash=%08lx, pbData=%p, dwDataLen=%d, dwFlags=%08x)\n",
4163 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4165 if (dwFlags
& ~CRYPT_USERDATA
)
4167 SetLastError(NTE_BAD_FLAGS
);
4171 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4172 (OBJECTHDR
**)&pCryptHash
))
4174 SetLastError(NTE_BAD_HASH
);
4178 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4180 SetLastError(NTE_BAD_ALGID
);
4184 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4186 SetLastError(NTE_BAD_HASH_STATE
);
4190 update_hash(pCryptHash
, pbData
, dwDataLen
);
4194 /******************************************************************************
4195 * CPHashSessionKey (RSAENH.@)
4197 * Updates a hash object with the binary representation of a symmetric key.
4200 * hProv [I] Key container to which the hash object belongs.
4201 * hHash [I] Hash object which is to be updated.
4202 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4203 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4209 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4212 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4216 TRACE("(hProv=%08lx, hHash=%08lx, hKey=%08lx, dwFlags=%08x)\n", hProv
, hHash
, hKey
, dwFlags
);
4218 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4219 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4221 SetLastError(NTE_BAD_KEY
);
4225 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4226 SetLastError(NTE_BAD_FLAGS
);
4230 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4231 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4232 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4233 bTemp
= abKeyValue
[i
];
4234 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4235 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4239 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4242 /******************************************************************************
4243 * CPReleaseContext (RSAENH.@)
4245 * Release a key container.
4248 * hProv [I] Key container to be released.
4249 * dwFlags [I] Currently none defined.
4255 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4257 TRACE("(hProv=%08lx, dwFlags=%08x)\n", hProv
, dwFlags
);
4259 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4261 /* MSDN: hProv not containing valid context handle */
4262 SetLastError(NTE_BAD_UID
);
4267 SetLastError(NTE_BAD_FLAGS
);
4274 /******************************************************************************
4275 * CPSetHashParam (RSAENH.@)
4277 * Set a parameter of a hash object
4280 * hProv [I] The key container to which the key belongs.
4281 * hHash [I] The hash object for which a parameter is to be set.
4282 * dwParam [I] Parameter type. See Notes.
4283 * pbData [I] Pointer to the parameter value.
4284 * dwFlags [I] Currently none defined.
4291 * Currently only the HP_HMAC_INFO dwParam type is defined.
4292 * The HMAC_INFO struct will be deep copied into the hash object.
4293 * See Internet RFC 2104 for details on the HMAC algorithm.
4295 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4296 BYTE
*pbData
, DWORD dwFlags
)
4298 CRYPTHASH
*pCryptHash
;
4299 CRYPTKEY
*pCryptKey
;
4302 TRACE("(hProv=%08lx, hHash=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n",
4303 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4305 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4307 SetLastError(NTE_BAD_UID
);
4312 SetLastError(NTE_BAD_FLAGS
);
4316 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4317 (OBJECTHDR
**)&pCryptHash
))
4319 SetLastError(NTE_BAD_HASH
);
4325 free_hmac_info(pCryptHash
->pHMACInfo
);
4326 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4328 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4329 (OBJECTHDR
**)&pCryptKey
))
4331 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4335 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4336 HCRYPTHASH hKeyHash
;
4339 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4342 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4343 pCryptKey
->blobHmacKey
.cbData
, 0))
4345 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4348 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4349 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4352 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4355 pCryptKey
->dwKeyLen
= keyLen
;
4356 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4358 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4359 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4361 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4362 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4365 init_hash(pCryptHash
);
4369 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4370 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4373 case HP_TLS1PRF_SEED
:
4374 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4376 case HP_TLS1PRF_LABEL
:
4377 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4380 SetLastError(NTE_BAD_TYPE
);
4385 /******************************************************************************
4386 * CPSetProvParam (RSAENH.@)
4388 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4390 KEYCONTAINER
*pKeyContainer
;
4393 TRACE("(hProv=%08lx, dwParam=%08x, pbData=%p, dwFlags=%08x)\n", hProv
, dwParam
, pbData
, dwFlags
);
4395 if (!(pKeyContainer
= get_key_container(hProv
)))
4400 case PP_KEYSET_SEC_DESCR
:
4402 SECURITY_DESCRIPTOR
*sd
= (SECURITY_DESCRIPTOR
*)pbData
;
4403 DWORD err
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
4405 REGSAM access
= WRITE_DAC
| WRITE_OWNER
| ACCESS_SYSTEM_SECURITY
;
4406 PSID owner
= NULL
, group
= NULL
;
4407 PACL dacl
= NULL
, sacl
= NULL
;
4409 if (!open_container_key(pKeyContainer
->szName
, flags
, access
, &hKey
))
4411 SetLastError(NTE_BAD_KEYSET
);
4415 if ((dwFlags
& OWNER_SECURITY_INFORMATION
&& !GetSecurityDescriptorOwner(sd
, &owner
, &def
)) ||
4416 (dwFlags
& GROUP_SECURITY_INFORMATION
&& !GetSecurityDescriptorGroup(sd
, &group
, &def
)) ||
4417 (dwFlags
& DACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorDacl(sd
, &present
, &dacl
, &def
)) ||
4418 (dwFlags
& SACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorSacl(sd
, &present
, &sacl
, &def
)))
4424 err
= SetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, owner
, group
, dacl
, sacl
);
4434 FIXME("unimplemented parameter %08x\n", dwParam
);
4439 /******************************************************************************
4440 * CPSignHash (RSAENH.@)
4442 * Sign a hash object
4445 * hProv [I] The key container, to which the hash object belongs.
4446 * hHash [I] The hash object to be signed.
4447 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4448 * sDescription [I] Should be NULL for security reasons.
4449 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4450 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4451 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4457 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4458 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4461 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4462 CRYPTKEY
*pCryptKey
;
4464 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4468 TRACE("(hProv=%08lx, hHash=%08lx, dwKeySpec=%08x, sDescription=%s, dwFlags=%08x, "
4469 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4470 dwFlags
, pbSignature
, pdwSigLen
);
4472 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4473 SetLastError(NTE_BAD_FLAGS
);
4477 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4479 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4480 (OBJECTHDR
**)&pCryptKey
))
4482 SetLastError(NTE_NO_KEY
);
4487 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4491 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4493 SetLastError(ERROR_MORE_DATA
);
4494 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4497 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4500 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4501 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4507 dwHashLen
= sizeof(DWORD
);
4508 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4510 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4511 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4514 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4518 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4520 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4524 /******************************************************************************
4525 * CPVerifySignature (RSAENH.@)
4527 * Verify the signature of a hash object.
4530 * hProv [I] The key container, to which the hash belongs.
4531 * hHash [I] The hash for which the signature is verified.
4532 * pbSignature [I] The binary signature.
4533 * dwSigLen [I] Length of the signature BLOB.
4534 * hPubKey [I] Public key used to verify the signature.
4535 * sDescription [I] Should be NULL for security reasons.
4536 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4539 * Success: TRUE (Signature is valid)
4540 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4542 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbSignature
,
4543 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4546 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4547 CRYPTKEY
*pCryptKey
;
4550 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4553 TRACE("(hProv=%08lx, hHash=%08lx, pbSignature=%p, dwSigLen=%d, hPubKey=%08lx, sDescription=%s, "
4554 "dwFlags=%08x)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4557 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4558 SetLastError(NTE_BAD_FLAGS
);
4562 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4564 SetLastError(NTE_BAD_UID
);
4568 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4569 (OBJECTHDR
**)&pCryptKey
))
4571 SetLastError(NTE_BAD_KEY
);
4575 /* in Microsoft implementation, the signature length is checked before
4576 * the signature pointer.
4578 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
4580 SetLastError(NTE_BAD_SIGNATURE
);
4584 if (!hHash
|| !pbSignature
)
4586 SetLastError(ERROR_INVALID_PARAMETER
);
4591 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4592 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4598 dwHashLen
= sizeof(DWORD
);
4599 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
4601 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4602 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
4604 pbConstructed
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4605 if (!pbConstructed
) {
4606 SetLastError(NTE_NO_MEMORY
);
4610 pbDecrypted
= HeapAlloc(GetProcessHeap(), 0, dwSigLen
);
4612 SetLastError(NTE_NO_MEMORY
);
4616 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
4622 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
4623 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4628 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
4629 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
4630 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
4635 SetLastError(NTE_BAD_SIGNATURE
);
4638 HeapFree(GetProcessHeap(), 0, pbConstructed
);
4639 HeapFree(GetProcessHeap(), 0, pbDecrypted
);
4643 /******************************************************************************
4644 * DllRegisterServer (RSAENH.@)
4646 HRESULT WINAPI
DllRegisterServer(void)
4648 return __wine_register_resources( instance
);
4651 /******************************************************************************
4652 * DllUnregisterServer (RSAENH.@)
4654 HRESULT WINAPI
DllUnregisterServer(void)
4656 return __wine_unregister_resources( instance
);