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
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
43 #define RSAENH_MAGIC_KEY 0x73620457u
44 #define RSAENH_MAX_KEY_SIZE 64
45 #define RSAENH_MAX_BLOCK_SIZE 24
46 #define RSAENH_KEYSTATE_IDLE 0
47 #define RSAENH_KEYSTATE_ENCRYPTING 1
48 #define RSAENH_KEYSTATE_MASTERKEY 2
50 /******************************************************************************
51 * CRYPTHASH - hash objects
53 #define RSAENH_MAGIC_HASH 0x85938417u
54 #define RSAENH_HASHSTATE_HASHING 1
55 #define RSAENH_HASHSTATE_FINISHED 2
56 typedef struct _RSAENH_TLS1PRF_PARAMS
58 CRYPT_DATA_BLOB blobLabel
;
59 CRYPT_DATA_BLOB blobSeed
;
60 } RSAENH_TLS1PRF_PARAMS
;
62 typedef struct tagCRYPTHASH
70 BCRYPT_HASH_HANDLE hash_handle
;
71 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
73 RSAENH_TLS1PRF_PARAMS tpPRFParams
;
74 DWORD buffered_hash_bytes
;
76 KEY_CONTEXT key_context
;
77 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
80 /******************************************************************************
81 * CRYPTKEY - key objects
83 typedef struct _RSAENH_SCHANNEL_INFO
85 SCHANNEL_ALG saEncAlg
;
86 SCHANNEL_ALG saMACAlg
;
87 CRYPT_DATA_BLOB blobClientRandom
;
88 CRYPT_DATA_BLOB blobServerRandom
;
89 } RSAENH_SCHANNEL_INFO
;
91 typedef struct tagCRYPTKEY
100 DWORD dwEffectiveKeyLen
;
105 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
];
106 BYTE abInitVector
[RSAENH_MAX_BLOCK_SIZE
];
107 BYTE abChainVector
[RSAENH_MAX_BLOCK_SIZE
];
108 RSAENH_SCHANNEL_INFO siSChannelInfo
;
109 CRYPT_DATA_BLOB blobHmacKey
;
112 /******************************************************************************
113 * KEYCONTAINER - key containers
115 #define RSAENH_PERSONALITY_BASE 0u
116 #define RSAENH_PERSONALITY_STRONG 1u
117 #define RSAENH_PERSONALITY_ENHANCED 2u
118 #define RSAENH_PERSONALITY_SCHANNEL 3u
119 #define RSAENH_PERSONALITY_AES 4u
121 #define RSAENH_MAGIC_CONTAINER 0x26384993u
122 typedef struct tagKEYCONTAINER
128 DWORD dwEnumContainersCtr
;
129 CHAR szName
[MAX_PATH
];
130 CHAR szProvName
[MAX_PATH
];
131 HCRYPTKEY hKeyExchangeKeyPair
;
132 HCRYPTKEY hSignatureKeyPair
;
135 /******************************************************************************
136 * Some magic constants
138 #define RSAENH_ENCRYPT 1
139 #define RSAENH_DECRYPT 0
140 #define RSAENH_HMAC_DEF_IPAD_CHAR 0x36
141 #define RSAENH_HMAC_DEF_OPAD_CHAR 0x5c
142 #define RSAENH_HMAC_DEF_PAD_LEN 64
143 #define RSAENH_HMAC_BLOCK_LEN 64
144 #define RSAENH_DES_EFFECTIVE_KEYLEN 56
145 #define RSAENH_DES_STORAGE_KEYLEN 64
146 #define RSAENH_3DES112_EFFECTIVE_KEYLEN 112
147 #define RSAENH_3DES112_STORAGE_KEYLEN 128
148 #define RSAENH_3DES_EFFECTIVE_KEYLEN 168
149 #define RSAENH_3DES_STORAGE_KEYLEN 192
150 #define RSAENH_MAGIC_RSA2 0x32415352
151 #define RSAENH_MAGIC_RSA1 0x31415352
152 #define RSAENH_PKC_BLOCKTYPE 0x02
153 #define RSAENH_SSL3_VERSION_MAJOR 3
154 #define RSAENH_SSL3_VERSION_MINOR 0
155 #define RSAENH_TLS1_VERSION_MAJOR 3
156 #define RSAENH_TLS1_VERSION_MINOR 1
157 #define RSAENH_REGKEY "Software\\Wine\\Crypto\\RSA\\%s"
159 #define RSAENH_MIN(a,b) ((a)<(b)?(a):(b))
160 /******************************************************************************
161 * aProvEnumAlgsEx - Defines the capabilities of the CSP personalities.
163 #define RSAENH_MAX_ENUMALGS 24
164 #define RSAENH_PCT1_SSL2_SSL3_TLS1 (CRYPT_FLAG_PCT1|CRYPT_FLAG_SSL2|CRYPT_FLAG_SSL3|CRYPT_FLAG_TLS1)
165 #define S(s) sizeof(s), s
166 static const PROV_ENUMALGS_EX aProvEnumAlgsEx
[5][RSAENH_MAX_ENUMALGS
+1] =
169 {CALG_RC2
, 40, 40, 56, 0, S("RC2"), S("RSA Data Security's RC2")},
170 {CALG_RC4
, 40, 40, 56, 0, S("RC4"), S("RSA Data Security's RC4")},
171 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
172 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
173 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
174 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
175 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
176 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
177 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
178 {CALG_RSA_SIGN
, 512, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
179 {CALG_RSA_KEYX
, 512, 384, 1024, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
180 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
181 {0, 0, 0, 0, 0, S(""), S("")}
184 {CALG_RC2
, 128, 40, 128, 0, S("RC2"), S("RSA Data Security's RC2")},
185 {CALG_RC4
, 128, 40, 128, 0, S("RC4"), S("RSA Data Security's RC4")},
186 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
187 {CALG_3DES_112
, 112, 112, 112, 0, S("3DES TWO KEY"), S("Two Key Triple DES")},
188 {CALG_3DES
, 168, 168, 168, 0, S("3DES"), S("Three Key Triple DES")},
189 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
190 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
191 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
192 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
193 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
194 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
195 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
196 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
197 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
198 {0, 0, 0, 0, 0, S(""), S("")}
201 {CALG_RC2
, 128, 40, 128, 0, S("RC2"), S("RSA Data Security's RC2")},
202 {CALG_RC4
, 128, 40, 128, 0, S("RC4"), S("RSA Data Security's RC4")},
203 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
204 {CALG_3DES_112
, 112, 112, 112, 0, S("3DES TWO KEY"), S("Two Key Triple DES")},
205 {CALG_3DES
, 168, 168, 168, 0, S("3DES"), S("Three Key Triple DES")},
206 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
207 {CALG_MD2
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD2"), S("Message Digest 2 (MD2)")},
208 {CALG_MD4
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD4"), S("Message Digest 4 (MD4)")},
209 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
, S("MD5"), S("Message Digest 5 (MD5)")},
210 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
211 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
212 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_SIGN"), S("RSA Signature")},
213 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|CRYPT_FLAG_IPSEC
, S("RSA_KEYX"), S("RSA Key Exchange")},
214 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
215 {0, 0, 0, 0, 0, S(""), S("")}
218 {CALG_RC2
, 128, 40, 128, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RC2"), S("RSA Data Security's RC2")},
219 {CALG_RC4
, 128, 40, 128, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RC4"), S("RSA Data Security's RC4")},
220 {CALG_DES
, 56, 56, 56, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("DES"), S("Data Encryption Standard (DES)")},
221 {CALG_3DES_112
, 112, 112, 112, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("3DES TWO KEY"), S("Two Key Triple DES")},
222 {CALG_3DES
, 168, 168, 168, RSAENH_PCT1_SSL2_SSL3_TLS1
, S("3DES"), S("Three Key Triple DES")},
223 {CALG_SHA
, 160, 160, 160, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("SHA-1"), S("Secure Hash Algorithm (SHA-1)")},
224 {CALG_MD5
, 128, 128, 128, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("MD5"), S("Message Digest 5 (MD5)")},
225 {CALG_SSL3_SHAMD5
, 288, 288, 288, 0, S("SSL3 SHAMD5"), S("SSL3 SHAMD5")},
226 {CALG_MAC
, 0, 0, 0, 0, S("MAC"), S("Message Authentication Code")},
227 {CALG_RSA_SIGN
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RSA_SIGN"), S("RSA Signature")},
228 {CALG_RSA_KEYX
, 1024, 384, 16384, CRYPT_FLAG_SIGNING
|RSAENH_PCT1_SSL2_SSL3_TLS1
, S("RSA_KEYX"), S("RSA Key Exchange")},
229 {CALG_HMAC
, 0, 0, 0, 0, S("HMAC"), S("Hugo's MAC (HMAC)")},
230 {CALG_PCT1_MASTER
, 128, 128, 128, CRYPT_FLAG_PCT1
, S("PCT1 MASTER"), S("PCT1 Master")},
231 {CALG_SSL2_MASTER
, 40, 40, 192, CRYPT_FLAG_SSL2
, S("SSL2 MASTER"), S("SSL2 Master")},
232 {CALG_SSL3_MASTER
, 384, 384, 384, CRYPT_FLAG_SSL3
, S("SSL3 MASTER"), S("SSL3 Master")},
233 {CALG_TLS1_MASTER
, 384, 384, 384, CRYPT_FLAG_TLS1
, S("TLS1 MASTER"), S("TLS1 Master")},
234 {CALG_SCHANNEL_MASTER_HASH
, 0, 0, -1, 0, S("SCH MASTER HASH"), S("SChannel Master Hash")},
235 {CALG_SCHANNEL_MAC_KEY
, 0, 0, -1, 0, S("SCH MAC KEY"), S("SChannel MAC Key")},
236 {CALG_SCHANNEL_ENC_KEY
, 0, 0, -1, 0, S("SCH ENC KEY"), S("SChannel Encryption Key")},
237 {CALG_TLS1PRF
, 0, 0, -1, 0, S("TLS1 PRF"), S("TLS1 Pseudo Random Function")},
238 {0, 0, 0, 0, 0, S(""), S("")}
241 {CALG_RC2
, 128, 40, 128, 0, S("RC2"), S("RSA Data Security's RC2")},
242 {CALG_RC4
, 128, 40, 128, 0, S("RC4"), S("RSA Data Security's RC4")},
243 {CALG_DES
, 56, 56, 56, 0, S("DES"), S("Data Encryption Standard (DES)")},
244 {CALG_3DES_112
, 112, 112, 112, 0, S("3DES TWO KEY"), S("Two Key Triple DES")},
245 {CALG_3DES
, 168, 168, 168, 0, S("3DES"), S("Three Key Triple DES")},
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(
289 RSAENH_CPSetHashParam(
293 BYTE
*pbData
, DWORD dwFlags
297 RSAENH_CPGetHashParam(
307 RSAENH_CPDestroyHash(
312 static BOOL
crypt_export_key(
322 static BOOL
import_key(
341 /******************************************************************************
342 * CSP's handle table (used by all acquired key containers)
344 static struct handle_table handle_table
;
346 /******************************************************************************
349 * Initializes and destroys the handle table for the CSP's handles.
351 BOOL WINAPI
DllMain(HINSTANCE hInstance
, DWORD fdwReason
, PVOID reserved
)
355 case DLL_PROCESS_ATTACH
:
356 DisableThreadLibraryCalls(hInstance
);
357 init_handle_table(&handle_table
);
360 case DLL_PROCESS_DETACH
:
362 destroy_handle_table(&handle_table
);
368 /******************************************************************************
369 * copy_param [Internal]
371 * Helper function that supports the standard WINAPI protocol for querying data
375 * pbBuffer [O] Buffer where the queried parameter is copied to, if it is large enough.
376 * May be NUL if the required buffer size is to be queried only.
377 * pdwBufferSize [I/O] In: Size of the buffer at pbBuffer
378 * Out: Size of parameter pbParam
379 * pbParam [I] Parameter value.
380 * dwParamSize [I] Size of pbParam
383 * Success: TRUE (pbParam was copied into pbBuffer or pbBuffer is NULL)
384 * Failure: FALSE (pbBuffer is not large enough to hold pbParam). Last error: ERROR_MORE_DATA
386 static inline BOOL
copy_param(BYTE
*pbBuffer
, DWORD
*pdwBufferSize
, const BYTE
*pbParam
,
391 if (dwParamSize
> *pdwBufferSize
)
393 SetLastError(ERROR_MORE_DATA
);
394 *pdwBufferSize
= dwParamSize
;
397 memcpy(pbBuffer
, pbParam
, dwParamSize
);
399 *pdwBufferSize
= dwParamSize
;
403 static inline KEYCONTAINER
* get_key_container(HCRYPTPROV hProv
)
405 KEYCONTAINER
*pKeyContainer
;
407 if (!lookup_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
,
408 (OBJECTHDR
**)&pKeyContainer
))
410 SetLastError(NTE_BAD_UID
);
413 return pKeyContainer
;
416 /******************************************************************************
417 * get_algid_info [Internal]
419 * Query CSP capabilities for a given crypto algorithm.
422 * hProv [I] Handle to a key container of the CSP whose capabilities are to be queried.
423 * algid [I] Identifier of the crypto algorithm about which information is requested.
426 * Success: Pointer to a PROV_ENUMALGS_EX struct containing information about the crypto algorithm.
427 * Failure: NULL (algid not supported)
429 static inline const PROV_ENUMALGS_EX
* get_algid_info(HCRYPTPROV hProv
, ALG_ID algid
) {
430 const PROV_ENUMALGS_EX
*iterator
;
431 KEYCONTAINER
*pKeyContainer
;
433 if (!(pKeyContainer
= get_key_container(hProv
))) return NULL
;
435 for (iterator
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]; iterator
->aiAlgid
; iterator
++) {
436 if (iterator
->aiAlgid
== algid
) return iterator
;
439 SetLastError(NTE_BAD_ALGID
);
443 /******************************************************************************
444 * copy_data_blob [Internal]
446 * deeply copies a DATA_BLOB
449 * dst [O] That's where the blob will be copied to
450 * src [I] Source blob
454 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY
457 * Use free_data_blob to release resources occupied by copy_data_blob.
459 static inline BOOL
copy_data_blob(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src
)
461 dst
->pbData
= malloc(src
->cbData
);
463 SetLastError(NTE_NO_MEMORY
);
466 dst
->cbData
= src
->cbData
;
467 memcpy(dst
->pbData
, src
->pbData
, src
->cbData
);
471 /******************************************************************************
472 * concat_data_blobs [Internal]
474 * Concatenates two blobs
477 * dst [O] The new blob will be copied here
478 * src1 [I] Prefix blob
479 * src2 [I] Appendix blob
483 * Failure: FALSE (GetLastError() == NTE_NO_MEMORY)
486 * Release resources occupied by concat_data_blobs with free_data_blobs
488 static inline BOOL
concat_data_blobs(PCRYPT_DATA_BLOB dst
, const PCRYPT_DATA_BLOB src1
,
489 const PCRYPT_DATA_BLOB src2
)
491 dst
->cbData
= src1
->cbData
+ src2
->cbData
;
492 dst
->pbData
= malloc(dst
->cbData
);
494 SetLastError(NTE_NO_MEMORY
);
497 memcpy(dst
->pbData
, src1
->pbData
, src1
->cbData
);
498 memcpy(dst
->pbData
+ src1
->cbData
, src2
->pbData
, src2
->cbData
);
502 /******************************************************************************
503 * free_data_blob [Internal]
505 * releases resource occupied by a dynamically allocated CRYPT_DATA_BLOB
508 * pBlob [I] Heap space occupied by pBlob->pbData is released
510 static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob
) {
514 /******************************************************************************
515 * init_data_blob [Internal]
517 static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob
) {
518 pBlob
->pbData
= NULL
;
522 /******************************************************************************
523 * block_encrypt [Internal]
525 BOOL
block_encrypt(CRYPTKEY
*key
, BYTE
*data
, DWORD
*data_len
, DWORD buf_len
,
526 BOOL final
, KEY_CONTEXT
*context
, BYTE
*chain_vector
)
528 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
529 unsigned int encrypted_len
, i
, j
, k
;
531 if (!final
&& (*data_len
% key
->dwBlockLen
))
533 SetLastError(NTE_BAD_DATA
);
537 encrypted_len
= (*data_len
/ key
->dwBlockLen
+ (final
? 1 : 0)) * key
->dwBlockLen
;
541 *data_len
= encrypted_len
;
544 if (encrypted_len
> buf_len
)
546 *data_len
= encrypted_len
;
547 SetLastError(ERROR_MORE_DATA
);
551 /* Pad final block with length bytes */
552 for (i
= *data_len
; i
< encrypted_len
; i
++) data
[i
] = encrypted_len
- *data_len
;
553 *data_len
= encrypted_len
;
555 for (i
= 0, in
= data
; i
< *data_len
; i
+= key
->dwBlockLen
, in
+= key
->dwBlockLen
)
557 switch (key
->dwMode
) {
559 encrypt_block_impl(key
->aiAlgid
, 0, context
, in
, out
,
563 for (j
= 0; j
< key
->dwBlockLen
; j
++)
564 in
[j
] ^= chain_vector
[j
];
565 encrypt_block_impl(key
->aiAlgid
, 0, context
, in
, out
,
567 memcpy(chain_vector
, out
, key
->dwBlockLen
);
570 for (j
= 0; j
< key
->dwBlockLen
; j
++)
572 encrypt_block_impl(key
->aiAlgid
, 0, context
,
573 chain_vector
, o
, RSAENH_ENCRYPT
);
574 out
[j
] = in
[j
] ^ o
[0];
575 for (k
= 0; k
< key
->dwBlockLen
- 1; k
++)
576 chain_vector
[k
] = chain_vector
[k
+1];
577 chain_vector
[k
] = out
[j
];
581 SetLastError(NTE_BAD_ALGID
);
584 memcpy(in
, out
, key
->dwBlockLen
);
589 /******************************************************************************
590 * free_hmac_info [Internal]
592 * Deeply free an HMAC_INFO struct.
595 * hmac_info [I] Pointer to the HMAC_INFO struct to be freed.
598 * See Internet RFC 2104 for details on the HMAC algorithm.
600 static inline void free_hmac_info(PHMAC_INFO hmac_info
) {
601 if (!hmac_info
) return;
602 free(hmac_info
->pbInnerString
);
603 free(hmac_info
->pbOuterString
);
607 /******************************************************************************
608 * copy_hmac_info [Internal]
610 * Deeply copy an HMAC_INFO struct
613 * dst [O] Pointer to a location where the pointer to the HMAC_INFO copy will be stored.
614 * src [I] Pointer to the HMAC_INFO struct to be copied.
621 * See Internet RFC 2104 for details on the HMAC algorithm.
623 static BOOL
copy_hmac_info(PHMAC_INFO
*dst
, const HMAC_INFO
*src
) {
624 if (!src
) return FALSE
;
625 *dst
= malloc(sizeof(HMAC_INFO
));
626 if (!*dst
) return FALSE
;
628 (*dst
)->pbInnerString
= NULL
;
629 (*dst
)->pbOuterString
= NULL
;
630 if ((*dst
)->cbInnerString
== 0) (*dst
)->cbInnerString
= RSAENH_HMAC_DEF_PAD_LEN
;
631 (*dst
)->pbInnerString
= malloc((*dst
)->cbInnerString
);
632 if (!(*dst
)->pbInnerString
) {
633 free_hmac_info(*dst
);
636 if (src
->cbInnerString
)
637 memcpy((*dst
)->pbInnerString
, src
->pbInnerString
, src
->cbInnerString
);
639 memset((*dst
)->pbInnerString
, RSAENH_HMAC_DEF_IPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
640 if ((*dst
)->cbOuterString
== 0) (*dst
)->cbOuterString
= RSAENH_HMAC_DEF_PAD_LEN
;
641 (*dst
)->pbOuterString
= malloc((*dst
)->cbOuterString
);
642 if (!(*dst
)->pbOuterString
) {
643 free_hmac_info(*dst
);
646 if (src
->cbOuterString
)
647 memcpy((*dst
)->pbOuterString
, src
->pbOuterString
, src
->cbOuterString
);
649 memset((*dst
)->pbOuterString
, RSAENH_HMAC_DEF_OPAD_CHAR
, RSAENH_HMAC_DEF_PAD_LEN
);
653 /******************************************************************************
654 * destroy_hash [Internal]
656 * Destructor for hash objects
659 * pCryptHash [I] Pointer to the hash object to be destroyed.
660 * Will be invalid after function returns!
662 static void destroy_hash(OBJECTHDR
*pObject
)
664 CRYPTHASH
*pCryptHash
= (CRYPTHASH
*)pObject
;
666 BCryptDestroyHash(pCryptHash
->hash_handle
);
667 free_hmac_info(pCryptHash
->pHMACInfo
);
668 free_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
669 free_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
670 if (pCryptHash
->aiAlgid
== CALG_MAC
)
671 free_key_impl(pCryptHash
->key_alg_id
, &pCryptHash
->key_context
);
675 /******************************************************************************
676 * init_hash [Internal]
678 * Initialize (or reset) a hash object
681 * pCryptHash [I] The hash object to be initialized.
683 static inline BOOL
init_hash(CRYPTHASH
*pCryptHash
) {
686 switch (pCryptHash
->aiAlgid
)
689 if (pCryptHash
->pHMACInfo
) {
690 const PROV_ENUMALGS_EX
*pAlgInfo
;
692 pAlgInfo
= get_algid_info(pCryptHash
->hProv
, pCryptHash
->pHMACInfo
->HashAlgid
);
695 /* A number of hash algorithms (e. g., _SHA256) are supported for HMAC even for providers
696 * which don't list the algorithm, so print a fixme here. */
697 FIXME("Hash algroithm %#x not found.\n", pCryptHash
->pHMACInfo
->HashAlgid
);
700 pCryptHash
->dwHashSize
= pAlgInfo
->dwDefaultLen
>> 3;
701 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->hash_handle
);
702 update_hash_impl(pCryptHash
->hash_handle
,
703 pCryptHash
->pHMACInfo
->pbInnerString
,
704 pCryptHash
->pHMACInfo
->cbInnerString
);
709 dwLen
= sizeof(DWORD
);
710 RSAENH_CPGetKeyParam(pCryptHash
->hProv
, pCryptHash
->hKey
, KP_BLOCKLEN
,
711 (BYTE
*)&pCryptHash
->dwHashSize
, &dwLen
, 0);
712 pCryptHash
->dwHashSize
>>= 3;
716 return init_hash_impl(pCryptHash
->aiAlgid
, &pCryptHash
->hash_handle
);
720 /******************************************************************************
721 * update_hash [Internal]
723 * Hashes the given data and updates the hash object's state accordingly
726 * pCryptHash [I] Hash object to be updated.
727 * pbData [I] Pointer to data stream to be hashed.
728 * dwDataLen [I] Length of data stream.
730 static inline void update_hash(CRYPTHASH
*pCryptHash
, const BYTE
*pbData
, DWORD dwDataLen
)
736 switch (pCryptHash
->aiAlgid
)
739 if (pCryptHash
->pHMACInfo
)
740 update_hash_impl(pCryptHash
->hash_handle
, pbData
, dwDataLen
);
744 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&key
))
746 FIXME("Key lookup failed.\n");
749 if (pCryptHash
->buffered_hash_bytes
)
751 len
= min(pCryptHash
->dwHashSize
- pCryptHash
->buffered_hash_bytes
, dwDataLen
);
752 memcpy(pCryptHash
->abHashValue
+ pCryptHash
->buffered_hash_bytes
, pbData
, len
);
755 pCryptHash
->buffered_hash_bytes
+= len
;
756 if (pCryptHash
->buffered_hash_bytes
< pCryptHash
->dwHashSize
)
758 pCryptHash
->buffered_hash_bytes
= 0;
759 len
= pCryptHash
->dwHashSize
;
760 if (!block_encrypt(key
, pCryptHash
->abHashValue
, &len
, len
, FALSE
,
761 &pCryptHash
->key_context
, pCryptHash
->abChainVector
))
763 FIXME("block_encrypt failed.\n");
767 len
= dwDataLen
- dwDataLen
% pCryptHash
->dwHashSize
;
770 pbTemp
= malloc(len
);
776 memcpy(pbTemp
, pbData
, len
);
779 if (!block_encrypt(key
, pbTemp
, &len
, len
, FALSE
,
780 &pCryptHash
->key_context
, pCryptHash
->abChainVector
))
782 FIXME("block_encrypt failed.\n");
789 memcpy(pCryptHash
->abHashValue
, pbData
, dwDataLen
);
790 pCryptHash
->buffered_hash_bytes
= dwDataLen
;
795 update_hash_impl(pCryptHash
->hash_handle
, pbData
, dwDataLen
);
799 /******************************************************************************
800 * finalize_hash [Internal]
802 * Finalizes the hash, after all data has been hashed with update_hash.
803 * No additional data can be hashed afterwards until the hash gets initialized again.
806 * pCryptHash [I] Hash object to be finalized.
808 static inline void finalize_hash(CRYPTHASH
*pCryptHash
)
813 switch (pCryptHash
->aiAlgid
)
816 if (pCryptHash
->pHMACInfo
) {
817 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
819 finalize_hash_impl(pCryptHash
->hash_handle
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
820 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
821 init_hash_impl(pCryptHash
->pHMACInfo
->HashAlgid
, &pCryptHash
->hash_handle
);
822 update_hash_impl(pCryptHash
->hash_handle
,
823 pCryptHash
->pHMACInfo
->pbOuterString
,
824 pCryptHash
->pHMACInfo
->cbOuterString
);
825 update_hash_impl(pCryptHash
->hash_handle
,
826 abHashValue
, pCryptHash
->dwHashSize
);
827 finalize_hash_impl(pCryptHash
->hash_handle
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
828 pCryptHash
->hash_handle
= NULL
;
833 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&key
))
835 FIXME("Key lookup failed.\n");
838 dwDataLen
= pCryptHash
->buffered_hash_bytes
;
839 if (!block_encrypt(key
, pCryptHash
->abHashValue
, &dwDataLen
, pCryptHash
->dwHashSize
, TRUE
,
840 &pCryptHash
->key_context
, pCryptHash
->abChainVector
))
841 FIXME("block_encrypt failed.\n");
845 finalize_hash_impl(pCryptHash
->hash_handle
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
846 pCryptHash
->hash_handle
= NULL
;
850 /******************************************************************************
851 * destroy_key [Internal]
853 * Destructor for key objects
856 * pCryptKey [I] Pointer to the key object to be destroyed.
857 * Will be invalid after function returns!
859 static void destroy_key(OBJECTHDR
*pObject
)
861 CRYPTKEY
*pCryptKey
= (CRYPTKEY
*)pObject
;
863 free_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
);
864 free_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
);
865 free_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
);
866 free_data_blob(&pCryptKey
->blobHmacKey
);
870 /******************************************************************************
871 * setup_key [Internal]
873 * Initialize (or reset) a key object
876 * pCryptKey [I] The key object to be initialized.
878 static inline void setup_key(CRYPTKEY
*pCryptKey
) {
879 pCryptKey
->dwState
= RSAENH_KEYSTATE_IDLE
;
880 memcpy(pCryptKey
->abChainVector
, pCryptKey
->abInitVector
, sizeof(pCryptKey
->abChainVector
));
881 setup_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
,
882 pCryptKey
->dwEffectiveKeyLen
, pCryptKey
->dwSaltLen
,
883 pCryptKey
->abKeyValue
);
886 /******************************************************************************
887 * alloc_key [Internal]
890 static HCRYPTKEY
alloc_key(HCRYPTPROV hprov
, ALG_ID alg_id
, DWORD flags
, DWORD key_len
, CRYPTKEY
**ret_key
)
892 KEYCONTAINER
*key_container
= get_key_container(hprov
);
896 hkey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
,
897 destroy_key
, (OBJECTHDR
**)&key
);
898 if (hkey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
901 key
->aiAlgid
= alg_id
;
904 key
->dwPermissions
= CRYPT_ENCRYPT
| CRYPT_DECRYPT
| CRYPT_READ
| CRYPT_WRITE
| CRYPT_MAC
;
905 if (flags
& CRYPT_EXPORTABLE
)
906 key
->dwPermissions
|= CRYPT_EXPORT
;
907 key
->dwKeyLen
= key_len
>> 3;
908 key
->dwEffectiveKeyLen
= 0;
911 * For compatibility reasons a 40 bit key on the Enhanced
912 * provider will not have salt
914 if (key_container
->dwPersonality
== RSAENH_PERSONALITY_ENHANCED
915 && (alg_id
== CALG_RC2
|| alg_id
== CALG_RC4
)
916 && (flags
& CRYPT_CREATE_SALT
) && key_len
== 40)
918 else if ((flags
& CRYPT_CREATE_SALT
) || (key_len
== 40 && !(flags
& CRYPT_NO_SALT
)))
919 key
->dwSaltLen
= 16 /*FIXME*/ - key
->dwKeyLen
;
922 memset(key
->abKeyValue
, 0, sizeof(key
->abKeyValue
));
923 memset(key
->abInitVector
, 0, sizeof(key
->abInitVector
));
924 memset(&key
->siSChannelInfo
.saEncAlg
, 0, sizeof(key
->siSChannelInfo
.saEncAlg
));
925 memset(&key
->siSChannelInfo
.saMACAlg
, 0, sizeof(key
->siSChannelInfo
.saMACAlg
));
926 init_data_blob(&key
->siSChannelInfo
.blobClientRandom
);
927 init_data_blob(&key
->siSChannelInfo
.blobServerRandom
);
928 init_data_blob(&key
->blobHmacKey
);
932 case CALG_PCT1_MASTER
:
933 case CALG_SSL2_MASTER
:
934 case CALG_SSL3_MASTER
:
935 case CALG_TLS1_MASTER
:
946 key
->dwMode
= CRYPT_MODE_CBC
;
952 key
->dwBlockLen
= 16;
953 key
->dwMode
= CRYPT_MODE_CBC
;
958 key
->dwBlockLen
= key_len
>> 3;
972 /******************************************************************************
975 * Creates a new key object without assigning the actual binary key value.
976 * This is done by CPDeriveKey, CPGenKey or CPImportKey, which call this function.
979 * hProv [I] Handle to the provider to which the created key will belong.
980 * aiAlgid [I] The new key shall use the crypto algorithm identified by aiAlgid.
981 * dwFlags [I] Upper 16 bits give the key length.
982 * Lower 16 bits: CRYPT_EXPORTABLE, CRYPT_CREATE_SALT,
984 * ppCryptKey [O] Pointer to the created key
987 * Success: Handle to the created key.
988 * Failure: INVALID_HANDLE_VALUE
990 static HCRYPTKEY
new_key(HCRYPTPROV hProv
, ALG_ID aiAlgid
, DWORD dwFlags
, CRYPTKEY
**ppCryptKey
)
992 DWORD dwKeyLen
= HIWORD(dwFlags
);
993 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
998 * Retrieve the CSP's capabilities for the given ALG_ID value
1000 peaAlgidInfo
= get_algid_info(hProv
, aiAlgid
);
1001 if (!peaAlgidInfo
) return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1003 TRACE("alg = %s, dwKeyLen = %ld\n", debugstr_a(peaAlgidInfo
->szName
),
1006 * Assume the default key length, if none is specified explicitly
1008 if (dwKeyLen
== 0) dwKeyLen
= peaAlgidInfo
->dwDefaultLen
;
1011 * Check if the requested key length is supported by the current CSP.
1012 * Adjust key length's for DES algorithms.
1016 if (dwKeyLen
== RSAENH_DES_EFFECTIVE_KEYLEN
) {
1017 dwKeyLen
= RSAENH_DES_STORAGE_KEYLEN
;
1019 if (dwKeyLen
!= RSAENH_DES_STORAGE_KEYLEN
) {
1020 SetLastError(NTE_BAD_FLAGS
);
1021 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1026 if (dwKeyLen
== RSAENH_3DES112_EFFECTIVE_KEYLEN
) {
1027 dwKeyLen
= RSAENH_3DES112_STORAGE_KEYLEN
;
1029 if (dwKeyLen
!= RSAENH_3DES112_STORAGE_KEYLEN
) {
1030 SetLastError(NTE_BAD_FLAGS
);
1031 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1036 if (dwKeyLen
== RSAENH_3DES_EFFECTIVE_KEYLEN
) {
1037 dwKeyLen
= RSAENH_3DES_STORAGE_KEYLEN
;
1039 if (dwKeyLen
!= RSAENH_3DES_STORAGE_KEYLEN
) {
1040 SetLastError(NTE_BAD_FLAGS
);
1041 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1046 /* Avoid the key length check for HMAC keys, which have unlimited
1053 dwKeyLen
> peaAlgidInfo
->dwMaxLen
||
1054 dwKeyLen
< peaAlgidInfo
->dwMinLen
)
1056 TRACE("key len %ld out of bounds (%ld, %ld)\n", dwKeyLen
,
1057 peaAlgidInfo
->dwMinLen
, peaAlgidInfo
->dwMaxLen
);
1058 SetLastError(NTE_BAD_DATA
);
1059 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1063 return alloc_key(hProv
, aiAlgid
, dwFlags
, dwKeyLen
, ppCryptKey
);
1066 /******************************************************************************
1067 * map_key_spec_to_key_pair_name [Internal]
1069 * Returns the name of the registry value associated with a key spec.
1072 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1075 * Success: Name of registry value.
1078 static LPCSTR
map_key_spec_to_key_pair_name(DWORD dwKeySpec
)
1084 case AT_KEYEXCHANGE
:
1085 szValueName
= "KeyExchangeKeyPair";
1088 szValueName
= "SignatureKeyPair";
1091 WARN("invalid key spec %ld\n", dwKeySpec
);
1097 /******************************************************************************
1098 * store_key_pair [Internal]
1100 * Stores a key pair to the registry
1103 * hCryptKey [I] Handle to the key to be stored
1104 * hKey [I] Registry key where the key pair is to be stored
1105 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1106 * dwFlags [I] Flags for protecting the key
1108 static void store_key_pair(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
)
1111 DATA_BLOB blobIn
, blobOut
;
1116 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1118 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1119 (OBJECTHDR
**)&pKey
))
1121 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, 0, &dwLen
))
1123 pbKey
= malloc(dwLen
);
1126 if (crypt_export_key(pKey
, 0, PRIVATEKEYBLOB
, 0, TRUE
, pbKey
,
1129 blobIn
.pbData
= pbKey
;
1130 blobIn
.cbData
= dwLen
;
1132 if (CryptProtectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1135 RegSetValueExA(hKey
, szValueName
, 0, REG_BINARY
,
1136 blobOut
.pbData
, blobOut
.cbData
);
1137 LocalFree(blobOut
.pbData
);
1146 /******************************************************************************
1147 * map_key_spec_to_permissions_name [Internal]
1149 * Returns the name of the registry value associated with the permissions for
1153 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1156 * Success: Name of registry value.
1159 static LPCSTR
map_key_spec_to_permissions_name(DWORD dwKeySpec
)
1165 case AT_KEYEXCHANGE
:
1166 szValueName
= "KeyExchangePermissions";
1169 szValueName
= "SignaturePermissions";
1172 WARN("invalid key spec %ld\n", dwKeySpec
);
1178 /******************************************************************************
1179 * store_key_permissions [Internal]
1181 * Stores a key's permissions to the registry
1184 * hCryptKey [I] Handle to the key whose permissions are to be stored
1185 * hKey [I] Registry key where the key permissions are to be stored
1186 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1188 static void store_key_permissions(HCRYPTKEY hCryptKey
, HKEY hKey
, DWORD dwKeySpec
)
1193 if (!(szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1195 if (lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
1196 (OBJECTHDR
**)&pKey
))
1197 RegSetValueExA(hKey
, szValueName
, 0, REG_DWORD
,
1198 (BYTE
*)&pKey
->dwPermissions
,
1199 sizeof(pKey
->dwPermissions
));
1202 /******************************************************************************
1203 * create_container_key [Internal]
1205 * Creates the registry key for a key container's persistent storage.
1208 * pKeyContainer [I] Pointer to the key container
1209 * sam [I] Desired registry access
1210 * phKey [O] Returned key
1212 static BOOL
create_container_key(KEYCONTAINER
*pKeyContainer
, REGSAM sam
, HKEY
*phKey
)
1214 CHAR szRSABase
[sizeof(RSAENH_REGKEY
) + MAX_PATH
];
1217 sprintf(szRSABase
, RSAENH_REGKEY
, pKeyContainer
->szName
);
1219 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1220 hRootKey
= HKEY_LOCAL_MACHINE
;
1222 hRootKey
= HKEY_CURRENT_USER
;
1224 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1225 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1226 return RegCreateKeyExA(hRootKey
, szRSABase
, 0, NULL
,
1227 REG_OPTION_NON_VOLATILE
, sam
, NULL
, phKey
, NULL
)
1231 /******************************************************************************
1232 * open_container_key [Internal]
1234 * Opens a key container's persistent storage for reading.
1237 * pszContainerName [I] Name of the container to be opened. May be the empty
1238 * string if the parent key of all containers is to be
1240 * dwFlags [I] Flags indicating which keyset to be opened.
1241 * phKey [O] Returned key
1243 static BOOL
open_container_key(LPCSTR pszContainerName
, DWORD dwFlags
, REGSAM access
, HKEY
*phKey
)
1245 CHAR szRSABase
[sizeof(RSAENH_REGKEY
) + MAX_PATH
];
1248 sprintf(szRSABase
, RSAENH_REGKEY
, pszContainerName
);
1250 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1251 hRootKey
= HKEY_LOCAL_MACHINE
;
1253 hRootKey
= HKEY_CURRENT_USER
;
1255 /* @@ Wine registry key: HKLM\Software\Wine\Crypto\RSA */
1256 /* @@ Wine registry key: HKCU\Software\Wine\Crypto\RSA */
1257 return RegOpenKeyExA(hRootKey
, szRSABase
, 0, access
, phKey
) ==
1261 /******************************************************************************
1262 * delete_container_key [Internal]
1264 * Deletes a key container's persistent storage.
1267 * pszContainerName [I] Name of the container to be opened.
1268 * dwFlags [I] Flags indicating which keyset to be opened.
1270 static BOOL
delete_container_key(LPCSTR pszContainerName
, DWORD dwFlags
)
1272 CHAR szRegKey
[sizeof(RSAENH_REGKEY
) + MAX_PATH
];
1275 sprintf(szRegKey
, RSAENH_REGKEY
, pszContainerName
);
1277 if (dwFlags
& CRYPT_MACHINE_KEYSET
)
1278 hRootKey
= HKEY_LOCAL_MACHINE
;
1280 hRootKey
= HKEY_CURRENT_USER
;
1281 if (!RegDeleteKeyA(hRootKey
, szRegKey
)) {
1282 SetLastError(ERROR_SUCCESS
);
1285 SetLastError(NTE_BAD_KEYSET
);
1290 /******************************************************************************
1291 * store_key_container_keys [Internal]
1293 * Stores key container's keys in a persistent location.
1296 * pKeyContainer [I] Pointer to the key container whose keys are to be saved
1298 static void store_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1303 /* On WinXP, persistent keys are stored in a file located at:
1304 * $AppData$\\Microsoft\\Crypto\\RSA\\$SID$\\some_hex_string
1307 if (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
)
1308 dwFlags
= CRYPTPROTECT_LOCAL_MACHINE
;
1312 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1314 store_key_pair(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1315 AT_KEYEXCHANGE
, dwFlags
);
1316 store_key_pair(pKeyContainer
->hSignatureKeyPair
, hKey
,
1317 AT_SIGNATURE
, dwFlags
);
1322 /******************************************************************************
1323 * store_key_container_permissions [Internal]
1325 * Stores key container's key permissions in a persistent location.
1328 * pKeyContainer [I] Pointer to the key container whose key permissions are to
1331 static void store_key_container_permissions(KEYCONTAINER
*pKeyContainer
)
1335 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1337 store_key_permissions(pKeyContainer
->hKeyExchangeKeyPair
, hKey
,
1339 store_key_permissions(pKeyContainer
->hSignatureKeyPair
, hKey
,
1345 /******************************************************************************
1346 * release_key_container_keys [Internal]
1348 * Releases key container's keys.
1351 * pKeyContainer [I] Pointer to the key container whose keys are to be released.
1353 static void release_key_container_keys(KEYCONTAINER
*pKeyContainer
)
1355 release_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
,
1357 release_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
,
1361 /******************************************************************************
1362 * destroy_key_container [Internal]
1364 * Destructor for key containers.
1367 * pObjectHdr [I] Pointer to the key container to be destroyed.
1369 static void destroy_key_container(OBJECTHDR
*pObjectHdr
)
1371 KEYCONTAINER
*pKeyContainer
= (KEYCONTAINER
*)pObjectHdr
;
1373 if (!(pKeyContainer
->dwFlags
& CRYPT_VERIFYCONTEXT
))
1375 store_key_container_keys(pKeyContainer
);
1376 store_key_container_permissions(pKeyContainer
);
1377 release_key_container_keys(pKeyContainer
);
1380 release_key_container_keys(pKeyContainer
);
1381 free( pKeyContainer
);
1384 /******************************************************************************
1385 * new_key_container [Internal]
1387 * Create a new key container. The personality (RSA Base, Strong or Enhanced CP)
1388 * of the CSP is determined via the pVTable->pszProvName string.
1391 * pszContainerName [I] Name of the key container.
1392 * pVTable [I] Callback functions and context info provided by the OS
1395 * Success: Handle to the new key container.
1396 * Failure: INVALID_HANDLE_VALUE
1398 static HCRYPTPROV
new_key_container(PCCH pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1400 KEYCONTAINER
*pKeyContainer
;
1401 HCRYPTPROV hKeyContainer
;
1403 hKeyContainer
= new_object(&handle_table
, sizeof(KEYCONTAINER
), RSAENH_MAGIC_CONTAINER
,
1404 destroy_key_container
, (OBJECTHDR
**)&pKeyContainer
);
1405 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1407 lstrcpynA(pKeyContainer
->szName
, pszContainerName
, MAX_PATH
);
1408 pKeyContainer
->dwFlags
= dwFlags
;
1409 pKeyContainer
->dwEnumAlgsCtr
= 0;
1410 pKeyContainer
->hKeyExchangeKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1411 pKeyContainer
->hSignatureKeyPair
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1412 if (pVTable
&& pVTable
->pszProvName
) {
1413 lstrcpynA(pKeyContainer
->szProvName
, pVTable
->pszProvName
, MAX_PATH
);
1414 if (!strcmp(pVTable
->pszProvName
, MS_DEF_PROV_A
)) {
1415 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_BASE
;
1416 } else if (!strcmp(pVTable
->pszProvName
, MS_ENHANCED_PROV_A
)) {
1417 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_ENHANCED
;
1418 } else if (!strcmp(pVTable
->pszProvName
, MS_DEF_RSA_SCHANNEL_PROV_A
)) {
1419 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_SCHANNEL
;
1420 } else if (!strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_A
) ||
1421 !strcmp(pVTable
->pszProvName
, MS_ENH_RSA_AES_PROV_XP_A
)) {
1422 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_AES
;
1424 pKeyContainer
->dwPersonality
= RSAENH_PERSONALITY_STRONG
;
1428 /* The new key container has to be inserted into the CSP immediately
1429 * after creation to be available for CPGetProvParam's PP_ENUMCONTAINERS. */
1430 if (!(dwFlags
& CRYPT_VERIFYCONTEXT
)) {
1433 if (create_container_key(pKeyContainer
, KEY_WRITE
, &hKey
))
1438 return hKeyContainer
;
1441 /******************************************************************************
1442 * read_key_value [Internal]
1444 * Reads a key pair value from the registry
1447 * hKeyContainer [I] Crypt provider to use to import the key
1448 * hKey [I] Registry key from which to read the key pair
1449 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
1450 * dwFlags [I] Flags for unprotecting the key
1451 * phCryptKey [O] Returned key
1453 static BOOL
read_key_value(HCRYPTPROV hKeyContainer
, HKEY hKey
, DWORD dwKeySpec
, DWORD dwFlags
, HCRYPTKEY
*phCryptKey
)
1456 DWORD dwValueType
, dwLen
;
1458 DATA_BLOB blobIn
, blobOut
;
1461 if (!(szValueName
= map_key_spec_to_key_pair_name(dwKeySpec
)))
1463 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, NULL
, &dwLen
) ==
1466 pbKey
= malloc(dwLen
);
1469 if (RegQueryValueExA(hKey
, szValueName
, 0, &dwValueType
, pbKey
, &dwLen
) ==
1472 blobIn
.pbData
= pbKey
;
1473 blobIn
.cbData
= dwLen
;
1475 if (CryptUnprotectData(&blobIn
, NULL
, NULL
, NULL
, NULL
,
1478 ret
= import_key(hKeyContainer
, blobOut
.pbData
, blobOut
.cbData
, 0, 0,
1480 LocalFree(blobOut
.pbData
);
1490 if (lookup_handle(&handle_table
, *phCryptKey
, RSAENH_MAGIC_KEY
,
1491 (OBJECTHDR
**)&pKey
))
1493 if ((szValueName
= map_key_spec_to_permissions_name(dwKeySpec
)))
1495 dwLen
= sizeof(pKey
->dwPermissions
);
1496 RegQueryValueExA(hKey
, szValueName
, 0, NULL
,
1497 (BYTE
*)&pKey
->dwPermissions
, &dwLen
);
1504 /******************************************************************************
1505 * read_key_container [Internal]
1507 * Tries to read the persistent state of the key container (mainly the signature
1508 * and key exchange private keys) given by pszContainerName.
1511 * pszContainerName [I] Name of the key container to read from the registry
1512 * pVTable [I] Pointer to context data provided by the operating system
1515 * Success: Handle to the key container read from the registry
1516 * Failure: INVALID_HANDLE_VALUE
1518 static HCRYPTPROV
read_key_container(PCHAR pszContainerName
, DWORD dwFlags
, const VTableProvStruc
*pVTable
)
1521 KEYCONTAINER
*pKeyContainer
;
1522 HCRYPTPROV hKeyContainer
;
1523 HCRYPTKEY hCryptKey
;
1525 if (!open_container_key(pszContainerName
, dwFlags
, KEY_READ
, &hKey
))
1527 SetLastError(NTE_BAD_KEYSET
);
1528 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1531 hKeyContainer
= new_key_container(pszContainerName
, dwFlags
, pVTable
);
1532 if (hKeyContainer
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
1534 DWORD dwProtectFlags
= (dwFlags
& CRYPT_MACHINE_KEYSET
) ?
1535 CRYPTPROTECT_LOCAL_MACHINE
: 0;
1537 if (!lookup_handle(&handle_table
, hKeyContainer
, RSAENH_MAGIC_CONTAINER
,
1538 (OBJECTHDR
**)&pKeyContainer
))
1539 return (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
1541 /* read_key_value calls import_key, which calls import_private_key,
1542 * which implicitly installs the key value into the appropriate key
1543 * container key. Thus the ref count is incremented twice, once for
1544 * the output key value, and once for the implicit install, and needs
1545 * to be decremented to balance the two.
1547 if (read_key_value(hKeyContainer
, hKey
, AT_KEYEXCHANGE
,
1548 dwProtectFlags
, &hCryptKey
))
1549 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1550 if (read_key_value(hKeyContainer
, hKey
, AT_SIGNATURE
,
1551 dwProtectFlags
, &hCryptKey
))
1552 release_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
);
1555 return hKeyContainer
;
1558 /******************************************************************************
1559 * build_hash_signature [Internal]
1561 * Builds a padded version of a hash to match the length of the RSA key modulus.
1564 * pbSignature [O] The padded hash object is stored here.
1565 * dwLen [I] Length of the pbSignature buffer.
1566 * aiAlgid [I] Algorithm identifier of the hash to be padded.
1567 * abHashValue [I] The value of the hash object.
1568 * dwHashLen [I] Length of the hash value.
1569 * dwFlags [I] Selection of padding algorithm.
1573 * Failure: FALSE (NTE_BAD_ALGID)
1575 static BOOL
build_hash_signature(BYTE
*pbSignature
, DWORD dwLen
, ALG_ID aiAlgid
,
1576 const BYTE
*abHashValue
, DWORD dwHashLen
, DWORD dwFlags
)
1578 /* These prefixes are meant to be concatenated with hash values of the
1579 * respective kind to form a PKCS #7 DigestInfo. */
1580 static const struct tagOIDDescriptor
{
1583 const BYTE abOID
[19];
1584 } aOIDDescriptor
[] = {
1585 { CALG_MD2
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1586 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 } },
1587 { CALG_MD4
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1588 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 } },
1589 { CALG_MD5
, 18, { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48,
1590 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 } },
1591 { CALG_SHA
, 15, { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
1592 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 } },
1593 { CALG_SHA_256
, 19, { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1594 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
1595 0x05, 0x00, 0x04, 0x20 } },
1596 { CALG_SHA_384
, 19, { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1597 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
1598 0x05, 0x00, 0x04, 0x30 } },
1599 { CALG_SHA_512
, 19, { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
1600 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
1601 0x05, 0x00, 0x04, 0x40 } },
1602 { CALG_SSL3_SHAMD5
, 0, { 0 } },
1605 DWORD dwIdxOID
, i
, j
;
1607 for (dwIdxOID
= 0; aOIDDescriptor
[dwIdxOID
].aiAlgid
; dwIdxOID
++) {
1608 if (aOIDDescriptor
[dwIdxOID
].aiAlgid
== aiAlgid
) break;
1611 if (!aOIDDescriptor
[dwIdxOID
].aiAlgid
) {
1612 SetLastError(NTE_BAD_ALGID
);
1616 /* Build the padded signature */
1617 if (dwFlags
& CRYPT_X931_FORMAT
) {
1618 pbSignature
[0] = 0x6b;
1619 for (i
=1; i
< dwLen
- dwHashLen
- 3; i
++) {
1620 pbSignature
[i
] = 0xbb;
1622 pbSignature
[i
++] = 0xba;
1623 for (j
=0; j
< dwHashLen
; j
++, i
++) {
1624 pbSignature
[i
] = abHashValue
[j
];
1626 pbSignature
[i
++] = 0x33;
1627 pbSignature
[i
++] = 0xcc;
1629 pbSignature
[0] = 0x00;
1630 pbSignature
[1] = 0x01;
1631 if (dwFlags
& CRYPT_NOHASHOID
) {
1632 for (i
=2; i
< dwLen
- 1 - dwHashLen
; i
++) {
1633 pbSignature
[i
] = 0xff;
1635 pbSignature
[i
++] = 0x00;
1637 for (i
=2; i
< dwLen
- 1 - aOIDDescriptor
[dwIdxOID
].dwLen
- dwHashLen
; i
++) {
1638 pbSignature
[i
] = 0xff;
1640 pbSignature
[i
++] = 0x00;
1641 for (j
=0; j
< aOIDDescriptor
[dwIdxOID
].dwLen
; j
++) {
1642 pbSignature
[i
++] = aOIDDescriptor
[dwIdxOID
].abOID
[j
];
1645 for (j
=0; j
< dwHashLen
; j
++) {
1646 pbSignature
[i
++] = abHashValue
[j
];
1653 /******************************************************************************
1656 * This is an implementation of the 'P_hash' helper function for TLS1's PRF.
1657 * It is used exclusively by tls1_prf. For details see RFC 2246, chapter 5.
1658 * The pseudo random stream generated by this function is exclusive or'ed with
1659 * the data in pbBuffer.
1662 * hHMAC [I] HMAC object, which will be used in pseudo random generation
1663 * pblobSeed [I] Seed value
1664 * pbBuffer [I/O] Pseudo random stream will be xor'ed to the provided data
1665 * dwBufferLen [I] Number of pseudo random bytes desired
1671 static BOOL
tls1_p(HCRYPTHASH hHMAC
, const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
,
1675 BYTE abAi
[RSAENH_MAX_HASH_SIZE
];
1678 if (!lookup_handle(&handle_table
, hHMAC
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pHMAC
)) {
1679 SetLastError(NTE_BAD_HASH
);
1683 /* compute A_1 = HMAC(seed) */
1685 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1686 finalize_hash(pHMAC
);
1687 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1690 /* compute HMAC(A_i + seed) */
1692 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1693 update_hash(pHMAC
, pblobSeed
->pbData
, pblobSeed
->cbData
);
1694 finalize_hash(pHMAC
);
1696 /* pseudo random stream := CONCAT_{i=1..n} ( HMAC(A_i + seed) ) */
1698 if (i
>= dwBufferLen
) break;
1699 pbBuffer
[i
] ^= pHMAC
->abHashValue
[i
% pHMAC
->dwHashSize
];
1701 } while (i
% pHMAC
->dwHashSize
);
1703 /* compute A_{i+1} = HMAC(A_i) */
1705 update_hash(pHMAC
, abAi
, pHMAC
->dwHashSize
);
1706 finalize_hash(pHMAC
);
1707 memcpy(abAi
, pHMAC
->abHashValue
, pHMAC
->dwHashSize
);
1708 } while (i
< dwBufferLen
);
1713 /******************************************************************************
1714 * tls1_prf [Internal]
1716 * TLS1 pseudo random function as specified in RFC 2246, chapter 5
1719 * hProv [I] Key container used to compute the pseudo random stream
1720 * hSecret [I] Key that holds the (pre-)master secret
1721 * pblobLabel [I] Descriptive label
1722 * pblobSeed [I] Seed value
1723 * pbBuffer [O] Pseudo random numbers will be stored here
1724 * dwBufferLen [I] Number of pseudo random bytes desired
1730 static BOOL
tls1_prf(HCRYPTPROV hProv
, HCRYPTPROV hSecret
, const PCRYPT_DATA_BLOB pblobLabel
,
1731 const PCRYPT_DATA_BLOB pblobSeed
, BYTE
*pbBuffer
, DWORD dwBufferLen
)
1733 HMAC_INFO hmacInfo
= { 0, NULL
, 0, NULL
, 0 };
1734 HCRYPTHASH hHMAC
= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
1735 HCRYPTKEY hHalfSecret
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
1736 CRYPTKEY
*pHalfSecret
, *pSecret
;
1737 DWORD dwHalfSecretLen
;
1738 BOOL result
= FALSE
;
1739 CRYPT_DATA_BLOB blobLabelSeed
;
1741 TRACE("(hProv=%08Ix, hSecret=%08Ix, pblobLabel=%p, pblobSeed=%p, pbBuffer=%p, dwBufferLen=%ld)\n",
1742 hProv
, hSecret
, pblobLabel
, pblobSeed
, pbBuffer
, dwBufferLen
);
1744 if (!lookup_handle(&handle_table
, hSecret
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSecret
)) {
1745 SetLastError(NTE_FAIL
);
1749 dwHalfSecretLen
= (pSecret
->dwKeyLen
+1)/2;
1751 /* concatenation of the label and the seed */
1752 if (!concat_data_blobs(&blobLabelSeed
, pblobLabel
, pblobSeed
)) goto exit
;
1754 /* zero out the buffer, since two random streams will be xor'ed into it. */
1755 memset(pbBuffer
, 0, dwBufferLen
);
1757 /* build a 'fake' key, to hold the secret. CALG_SSL2_MASTER is used since it provides
1758 * the biggest range of valid key lengths. */
1759 hHalfSecret
= new_key(hProv
, CALG_SSL2_MASTER
, MAKELONG(0,dwHalfSecretLen
*8), &pHalfSecret
);
1760 if (hHalfSecret
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) goto exit
;
1762 /* Derive an HMAC_MD5 hash and call the helper function. */
1763 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
, dwHalfSecretLen
);
1764 if (!RSAENH_CPCreateHash(hProv
, CALG_HMAC
, hHalfSecret
, 0, &hHMAC
)) goto exit
;
1765 hmacInfo
.HashAlgid
= CALG_MD5
;
1766 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1767 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1769 /* Reconfigure to HMAC_SHA hash and call helper function again. */
1770 memcpy(pHalfSecret
->abKeyValue
, pSecret
->abKeyValue
+ (pSecret
->dwKeyLen
/2), dwHalfSecretLen
);
1771 hmacInfo
.HashAlgid
= CALG_SHA
;
1772 if (!RSAENH_CPSetHashParam(hProv
, hHMAC
, HP_HMAC_INFO
, (BYTE
*)&hmacInfo
, 0)) goto exit
;
1773 if (!tls1_p(hHMAC
, &blobLabelSeed
, pbBuffer
, dwBufferLen
)) goto exit
;
1777 release_handle(&handle_table
, hHalfSecret
, RSAENH_MAGIC_KEY
);
1778 if (hHMAC
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
) RSAENH_CPDestroyHash(hProv
, hHMAC
);
1779 free_data_blob(&blobLabelSeed
);
1783 /******************************************************************************
1784 * pad_data_pkcs1 [Internal]
1786 * Helper function for data padding according to PKCS1 #2
1789 * abData [I] The data to be padded
1790 * dwDataLen [I] Length of the data
1791 * abBuffer [O] Padded data will be stored here
1792 * dwBufferLen [I] Length of the buffer (also length of padded data)
1793 * dwFlags [I] Padding format (CRYPT_SSL2_FALLBACK)
1797 * Failure: FALSE (NTE_BAD_LEN, too much data to pad)
1799 static BOOL
pad_data_pkcs1(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
, DWORD dwFlags
)
1803 /* Ensure there is enough space for PKCS1 #2 padding */
1804 if (dwDataLen
> dwBufferLen
-11) {
1805 SetLastError(NTE_BAD_LEN
);
1809 memmove(abBuffer
+ dwBufferLen
- dwDataLen
, abData
, dwDataLen
);
1812 abBuffer
[1] = RSAENH_PKC_BLOCKTYPE
;
1813 for (i
=2; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1814 do gen_rand_impl(&abBuffer
[i
], 1); while (!abBuffer
[i
]);
1815 if (dwFlags
& CRYPT_SSL2_FALLBACK
)
1816 for (i
-=8; i
< dwBufferLen
- dwDataLen
- 1; i
++)
1823 /******************************************************************************
1824 * pkcs1_mgf1 [Internal]
1826 * MGF function for RSA EM-OAEP as specified in RFC 8017 PKCS #1 V2.2, Appendix B.2.1. MGF1
1829 * hProv [I] Cryptographic provider handle
1830 * pbSeed [I] Seed from which mask is generated
1831 * dwSeedLength [I] Length of pbSeed
1832 * dwLength [I] Intended length in octets of the mask
1833 * pbMask [O] Generated mask if success. Caller is responsible for freeing the mask when it's done
1839 static BOOL
pkcs1_mgf1(HCRYPTPROV hProv
, const BYTE
*pbSeed
, DWORD dwSeedLength
, DWORD dwLength
, PCRYPT_DATA_BLOB pbMask
)
1842 BYTE
*pbHashInput
, *pbCounter
;
1844 DWORD dwLen
, dwHashLen
;
1846 RSAENH_CPCreateHash(hProv
, CALG_SHA1
, 0, 0, &hHash
);
1847 RSAENH_CPHashData(hProv
, hHash
, 0, 0, 0);
1848 dwLen
= sizeof(dwHashLen
);
1849 RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHSIZE
, (BYTE
*)&dwHashLen
, &dwLen
, 0);
1850 RSAENH_CPDestroyHash(hProv
, hHash
);
1852 /* Allocate multiples of hash value */
1853 pbMask
->pbData
= malloc((dwLength
+ dwHashLen
- 1) / dwHashLen
* dwHashLen
);
1854 if (!pbMask
->pbData
)
1856 SetLastError(NTE_NO_MEMORY
);
1859 pbMask
->cbData
= dwLength
;
1861 pbHashInput
= malloc(dwSeedLength
+ sizeof(DWORD
));
1864 free_data_blob(pbMask
);
1865 SetLastError(NTE_NO_MEMORY
);
1870 memcpy(pbHashInput
, pbSeed
, dwSeedLength
);
1871 pbCounter
= pbHashInput
+ dwSeedLength
;
1872 for (dwCounter
= 0; dwCounter
< (dwLength
+ dwHashLen
- 1) / dwHashLen
; dwCounter
++)
1874 *(pbCounter
) = (BYTE
)((dwCounter
>> 24) & 0xff);
1875 *(pbCounter
+ 1) = (BYTE
)((dwCounter
>> 16) & 0xff);
1876 *(pbCounter
+ 2) = (BYTE
)((dwCounter
>> 8) & 0xff);
1877 *(pbCounter
+ 3) = (BYTE
)(dwCounter
& 0xff);
1878 RSAENH_CPCreateHash(hProv
, CALG_SHA1
, 0, 0, &hHash
);
1879 RSAENH_CPHashData(hProv
, hHash
, pbHashInput
, dwSeedLength
+ sizeof(DWORD
), 0);
1880 /* pbMask->pbData = old pbMask->pbData || Hash(Seed || Counter) */
1881 RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, pbMask
->pbData
+ dwCounter
* dwHashLen
, &dwLen
, 0);
1882 RSAENH_CPDestroyHash(hProv
, hHash
);
1889 /******************************************************************************
1890 * pad_data_oaep [Internal]
1892 * Helper function for data OAEP padding scheme according to RFC 8017 PKCS #1 V2.2
1895 * hProv [I] Cryptographic provider handle
1896 * abData [I] The data to be padded
1897 * dwDataLen [I] Length of the data
1898 * abBuffer [O] Padded data will be stored here
1899 * dwBufferLen [I] Length of the buffer (also length of padded data)
1900 * dwFlags [I] Currently only CRYPT_OAEP is defined
1906 static BOOL
pad_data_oaep(HCRYPTPROV hProv
, const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
1909 CRYPT_DATA_BLOB blobDbMask
= {0}, blobSeedMask
= {0};
1911 BYTE
*pbPadded
= NULL
, *pbDb
, *pbSeed
;
1912 DWORD dwLen
, dwHashLen
;
1913 DWORD dwDbLen
, dwSeedLen
;
1914 BOOL result
, ret
= FALSE
;
1917 RSAENH_CPCreateHash(hProv
, CALG_SHA1
, 0, 0, &hHash
);
1919 RSAENH_CPHashData(hProv
, hHash
, 0, 0, 0);
1920 dwLen
= sizeof(dwHashLen
);
1921 RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHSIZE
, (BYTE
*)&dwHashLen
, &dwLen
, 0);
1923 if (dwDataLen
> dwBufferLen
- 2 * dwHashLen
- 2)
1925 SetLastError(NTE_BAD_LEN
);
1929 if (dwBufferLen
< 2 * dwHashLen
+ 2)
1931 SetLastError(ERROR_MORE_DATA
);
1935 pbPadded
= malloc(dwBufferLen
);
1938 SetLastError(NTE_NO_MEMORY
);
1942 /* EM = 00 || maskedSeed || maskedDB */
1944 pbSeed
= pbPadded
+ 1;
1945 dwSeedLen
= dwHashLen
;
1946 pbDb
= pbPadded
+ 1 + dwHashLen
;
1947 dwDbLen
= dwBufferLen
- dwSeedLen
- 1;
1949 /* DB = pHash || PS || 01 || M */
1950 /* Set pHash in DB */
1952 RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, pbDb
, &dwLen
, 0);
1953 /* Set PS(zeros) in DB */
1954 memset(pbDb
+ dwHashLen
, 0, dwDbLen
- dwHashLen
- 1 - dwDataLen
);
1956 pbDb
[dwDbLen
- dwDataLen
- 1] = 1;
1958 memcpy(pbDb
+ dwDbLen
- dwDataLen
, abData
, dwDataLen
);
1961 gen_rand_impl(pbSeed
, dwHashLen
);
1963 result
= pkcs1_mgf1(hProv
, pbSeed
, dwHashLen
, dwDbLen
, &blobDbMask
);
1964 if (!result
) goto done
;
1965 for (i
= 0; i
< dwDbLen
; i
++) pbDb
[i
] ^= blobDbMask
.pbData
[i
];
1967 /* Get masked seed */
1968 result
= pkcs1_mgf1(hProv
, pbDb
, dwDbLen
, dwHashLen
, &blobSeedMask
);
1969 if (!result
) goto done
;
1970 for (i
= 0; i
< dwHashLen
; i
++) pbSeed
[i
] ^= blobSeedMask
.pbData
[i
];
1972 memcpy(abBuffer
, pbPadded
, dwBufferLen
);
1975 RSAENH_CPDestroyHash(hProv
, hHash
);
1977 free_data_blob(&blobDbMask
);
1978 free_data_blob(&blobSeedMask
);
1982 /******************************************************************************
1983 * pad_data [Internal]
1985 * Helper function for data padding according to padding format
1988 * hProv [I] Cryptographic provider handle
1989 * abData [I] The data to be padded
1990 * dwDataLen [I] Length of the data
1991 * abBuffer [O] Padded data will be stored here
1992 * dwBufferLen [I] Length of the buffer (also length of padded data)
1993 * dwFlags [I] 0, CRYPT_SSL2_FALLBACK or CRYPT_OAEP
1999 static BOOL
pad_data(HCRYPTPROV hProv
, const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD dwBufferLen
,
2002 if (dwFlags
== CRYPT_OAEP
)
2003 return pad_data_oaep(hProv
, abData
, dwDataLen
, abBuffer
, dwBufferLen
, dwFlags
);
2005 return pad_data_pkcs1(abData
, dwDataLen
, abBuffer
, dwBufferLen
, dwFlags
);
2008 /******************************************************************************
2009 * unpad_data_pkcs1 [Internal]
2011 * Remove the PKCS1 padding from RSA decrypted data
2014 * abData [I] The padded data
2015 * dwDataLen [I] Length of the padded data
2016 * abBuffer [O] Data without padding will be stored here
2017 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
2018 * dwFlags [I] Currently none defined
2022 * Failure: FALSE, (NTE_BAD_DATA, no valid PKCS1 padding or buffer too small)
2024 static BOOL
unpad_data_pkcs1(const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
, DWORD dwFlags
)
2030 SetLastError(NTE_BAD_DATA
);
2033 for (i
=2; i
<dwDataLen
; i
++)
2037 if ((i
== dwDataLen
) || (*dwBufferLen
< dwDataLen
- i
- 1) ||
2038 (abData
[0] != 0x00) || (abData
[1] != RSAENH_PKC_BLOCKTYPE
))
2040 SetLastError(NTE_BAD_DATA
);
2044 *dwBufferLen
= dwDataLen
- i
- 1;
2045 memmove(abBuffer
, abData
+ i
+ 1, *dwBufferLen
);
2049 /******************************************************************************
2050 * unpad_data_oaep [Internal]
2052 * Remove the OAEP padding from RSA decrypted data
2055 * hProv [I] Cryptographic provider handle
2056 * abData [I] The padded data
2057 * dwDataLen [I] Length of the padded data
2058 * abBuffer [O] Data without padding will be stored here
2059 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
2060 * dwFlags [I] Currently only CRYPT_OAEP is defined
2066 static BOOL
unpad_data_oaep(HCRYPTPROV hProv
, const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
2069 CRYPT_DATA_BLOB blobDbMask
= {0}, blobSeedMask
= {0};
2071 BYTE
*pbBuffer
= NULL
, *pbHashValue
= NULL
;
2072 const BYTE
*pbPaddedSeed
, *pbPaddedDb
;
2073 BYTE
*pbUnpaddedSeed
, *pbUnpaddedDb
;
2074 DWORD dwLen
, dwHashLen
;
2075 DWORD dwSeedLen
, dwDbLen
;
2076 DWORD dwZeroCount
, dwMsgCount
;
2077 BOOL result
, ret
= FALSE
;
2080 RSAENH_CPCreateHash(hProv
, CALG_SHA1
, 0, 0, &hHash
);
2081 RSAENH_CPHashData(hProv
, hHash
, 0, 0, 0);
2082 dwLen
= sizeof(dwHashLen
);
2083 RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHSIZE
, (BYTE
*)&dwHashLen
, &dwLen
, 0);
2084 if (dwDataLen
< 2 * dwHashLen
+ 2)
2086 SetLastError(NTE_BAD_DATA
);
2090 /* Get default hash value */
2091 pbHashValue
= malloc(dwHashLen
);
2094 SetLastError(NTE_NO_MEMORY
);
2098 RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, pbHashValue
, &dwLen
, 0);
2100 /* Store seed and DB */
2101 pbBuffer
= malloc(dwDataLen
- 1);
2104 SetLastError(NTE_NO_MEMORY
);
2108 pbPaddedSeed
= abData
+ 1;
2109 pbPaddedDb
= abData
+ 1 + dwHashLen
;
2110 pbUnpaddedSeed
= pbBuffer
;
2111 pbUnpaddedDb
= pbBuffer
+ dwHashLen
;
2112 dwSeedLen
= dwHashLen
;
2113 dwDbLen
= dwDataLen
- dwHashLen
- 1;
2115 /* Get unpadded seed */
2116 result
= pkcs1_mgf1(hProv
, pbPaddedDb
, dwDbLen
, dwSeedLen
, &blobSeedMask
);
2117 if (!result
) goto done
;
2118 for (i
= 0; i
< dwSeedLen
; i
++) pbUnpaddedSeed
[i
] = pbPaddedSeed
[i
] ^ blobSeedMask
.pbData
[i
];
2120 /* Get unpadded DB */
2121 result
= pkcs1_mgf1(hProv
, pbUnpaddedSeed
, dwSeedLen
, dwDbLen
, &blobDbMask
);
2122 if (!result
) goto done
;
2123 for (i
= 0; i
< dwDbLen
; i
++) pbUnpaddedDb
[i
] = pbPaddedDb
[i
] ^ blobDbMask
.pbData
[i
];
2125 /* Compare hash in DB */
2126 result
= memcmp(pbUnpaddedDb
, pbHashValue
, dwHashLen
);
2128 /* Get count of zero paddings(PS) */
2130 while (dwHashLen
+ dwZeroCount
+ 1 <= dwDbLen
&& pbUnpaddedDb
[dwHashLen
+ dwZeroCount
] == 0) dwZeroCount
++;
2131 dwMsgCount
= dwDbLen
- dwHashLen
- dwZeroCount
- 1;
2133 if (dwHashLen
+ dwZeroCount
+ 1 > dwDbLen
|| abData
[0] || result
|| pbUnpaddedDb
[dwHashLen
+ dwZeroCount
] != 1
2134 || *dwBufferLen
< dwMsgCount
)
2136 SetLastError(NTE_BAD_DATA
);
2140 *dwBufferLen
= dwMsgCount
;
2141 memcpy(abBuffer
, pbUnpaddedDb
+ dwHashLen
+ dwZeroCount
+ 1, dwMsgCount
);
2144 RSAENH_CPDestroyHash(hProv
, hHash
);
2147 free_data_blob(&blobDbMask
);
2148 free_data_blob(&blobSeedMask
);
2152 /******************************************************************************
2153 * unpad_data [Internal]
2155 * Remove the padding from RSA decrypted data according to padding format
2158 * hProv [I] Cryptographic provider handle
2159 * abData [I] The padded data
2160 * dwDataLen [I] Length of the padded data
2161 * abBuffer [O] Data without padding will be stored here
2162 * dwBufferLen [I/O] I: Length of the buffer, O: Length of unpadded data
2163 * dwFlags [I] 0 or CRYPT_OAEP
2169 static BOOL
unpad_data(HCRYPTPROV hProv
, const BYTE
*abData
, DWORD dwDataLen
, BYTE
*abBuffer
, DWORD
*dwBufferLen
,
2172 if (dwFlags
== CRYPT_OAEP
)
2173 return unpad_data_oaep(hProv
, abData
, dwDataLen
, abBuffer
, dwBufferLen
, dwFlags
);
2175 return unpad_data_pkcs1(abData
, dwDataLen
, abBuffer
, dwBufferLen
, dwFlags
);
2178 /******************************************************************************
2179 * CPAcquireContext (RSAENH.@)
2181 * Acquire a handle to the key container specified by pszContainer
2184 * phProv [O] Pointer to the location the acquired handle will be written to.
2185 * pszContainer [I] Name of the desired key container. See Notes
2186 * dwFlags [I] Flags. See Notes.
2187 * pVTable [I] Pointer to a PVTableProvStruct containing callbacks.
2194 * If pszContainer is NULL or points to a zero length string the user's login
2195 * name will be used as the key container name.
2197 * If the CRYPT_NEW_KEYSET flag is set in dwFlags a new keyset will be created.
2198 * If a keyset with the given name already exists, the function fails and sets
2199 * last error to NTE_EXISTS. If CRYPT_NEW_KEYSET is not set and the specified
2200 * key container does not exist, function fails and sets last error to
2203 BOOL WINAPI
RSAENH_CPAcquireContext(HCRYPTPROV
*phProv
, LPSTR pszContainer
,
2204 DWORD dwFlags
, PVTableProvStruc pVTable
)
2206 CHAR szKeyContainerName
[MAX_PATH
];
2208 TRACE("(phProv=%p, pszContainer=%s, dwFlags=%08lx, pVTable=%p)\n", phProv
,
2209 debugstr_a(pszContainer
), dwFlags
, pVTable
);
2211 if (pszContainer
&& *pszContainer
)
2213 lstrcpynA(szKeyContainerName
, pszContainer
, MAX_PATH
);
2217 DWORD dwLen
= sizeof(szKeyContainerName
);
2218 if (!GetUserNameA(szKeyContainerName
, &dwLen
)) return FALSE
;
2221 switch (dwFlags
& (CRYPT_NEWKEYSET
|CRYPT_VERIFYCONTEXT
|CRYPT_DELETEKEYSET
))
2224 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
2227 case CRYPT_DELETEKEYSET
:
2228 return delete_container_key(szKeyContainerName
, dwFlags
);
2230 case CRYPT_NEWKEYSET
:
2231 *phProv
= read_key_container(szKeyContainerName
, dwFlags
, pVTable
);
2232 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
)
2234 release_handle(&handle_table
, *phProv
, RSAENH_MAGIC_CONTAINER
);
2235 TRACE("Can't create new keyset, already exists\n");
2236 SetLastError(NTE_EXISTS
);
2239 *phProv
= new_key_container(szKeyContainerName
, dwFlags
, pVTable
);
2242 case CRYPT_VERIFYCONTEXT
|CRYPT_NEWKEYSET
:
2243 case CRYPT_VERIFYCONTEXT
:
2244 if (pszContainer
&& *pszContainer
) {
2245 TRACE("pszContainer should be empty\n");
2246 SetLastError(NTE_BAD_FLAGS
);
2249 *phProv
= new_key_container("", dwFlags
, pVTable
);
2253 *phProv
= (HCRYPTPROV
)INVALID_HANDLE_VALUE
;
2254 SetLastError(NTE_BAD_FLAGS
);
2258 if (*phProv
!= (HCRYPTPROV
)INVALID_HANDLE_VALUE
) {
2259 SetLastError(ERROR_SUCCESS
);
2266 /******************************************************************************
2267 * CPCreateHash (RSAENH.@)
2269 * CPCreateHash creates and initializes a new hash object.
2272 * hProv [I] Handle to the key container to which the new hash will belong.
2273 * Algid [I] Identifies the hash algorithm, which will be used for the hash.
2274 * hKey [I] Handle to a session key applied for keyed hashes.
2275 * dwFlags [I] Currently no flags defined. Must be zero.
2276 * phHash [O] Points to the location where a handle to the new hash will be stored.
2283 * hKey is a handle to a session key applied in keyed hashes like MAC and HMAC.
2284 * If a normal hash object is to be created (like e.g. MD2 or SHA1) hKey must be zero.
2286 BOOL WINAPI
RSAENH_CPCreateHash(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTKEY hKey
, DWORD dwFlags
,
2289 CRYPTKEY
*pCryptKey
= NULL
;
2290 CRYPTHASH
*pCryptHash
;
2291 const PROV_ENUMALGS_EX
*peaAlgidInfo
;
2293 TRACE("(hProv=%08Ix, Algid=%08x, hKey=%08Ix, dwFlags=%08lx, phHash=%p)\n", hProv
, Algid
, hKey
,
2296 peaAlgidInfo
= get_algid_info(hProv
, Algid
);
2297 if (!peaAlgidInfo
) return FALSE
;
2301 SetLastError(NTE_BAD_FLAGS
);
2305 if (Algid
== CALG_MAC
|| Algid
== CALG_HMAC
|| Algid
== CALG_SCHANNEL_MASTER_HASH
||
2306 Algid
== CALG_TLS1PRF
)
2308 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
)) {
2309 SetLastError(NTE_BAD_KEY
);
2313 if ((Algid
== CALG_MAC
) && (GET_ALG_TYPE(pCryptKey
->aiAlgid
) != ALG_TYPE_BLOCK
)) {
2314 SetLastError(NTE_BAD_KEY
);
2318 if ((Algid
== CALG_SCHANNEL_MASTER_HASH
|| Algid
== CALG_TLS1PRF
) &&
2319 (pCryptKey
->aiAlgid
!= CALG_TLS1_MASTER
))
2321 SetLastError(NTE_BAD_KEY
);
2324 if (Algid
== CALG_SCHANNEL_MASTER_HASH
&&
2325 ((!pCryptKey
->siSChannelInfo
.blobClientRandom
.cbData
) ||
2326 (!pCryptKey
->siSChannelInfo
.blobServerRandom
.cbData
)))
2328 SetLastError(ERROR_INVALID_PARAMETER
);
2332 if ((Algid
== CALG_TLS1PRF
) && (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
)) {
2333 SetLastError(NTE_BAD_KEY_STATE
);
2338 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2339 destroy_hash
, (OBJECTHDR
**)&pCryptHash
);
2340 if (!pCryptHash
) return FALSE
;
2342 pCryptHash
->aiAlgid
= Algid
;
2343 pCryptHash
->hKey
= hKey
;
2344 if (Algid
== CALG_MAC
)
2346 pCryptHash
->key_alg_id
= pCryptKey
->aiAlgid
;
2347 setup_key(pCryptKey
);
2348 duplicate_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, &pCryptHash
->key_context
);
2349 memcpy(pCryptHash
->abChainVector
, pCryptKey
->abChainVector
, sizeof(pCryptHash
->abChainVector
));
2351 pCryptHash
->hProv
= hProv
;
2352 pCryptHash
->dwState
= RSAENH_HASHSTATE_HASHING
;
2353 pCryptHash
->pHMACInfo
= NULL
;
2354 pCryptHash
->hash_handle
= NULL
;
2355 pCryptHash
->dwHashSize
= peaAlgidInfo
->dwDefaultLen
>> 3;
2356 pCryptHash
->buffered_hash_bytes
= 0;
2357 init_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
);
2358 init_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
);
2360 if (Algid
== CALG_SCHANNEL_MASTER_HASH
) {
2361 static const char keyex
[] = "key expansion";
2362 BYTE key_expansion
[sizeof keyex
];
2363 CRYPT_DATA_BLOB blobRandom
, blobKeyExpansion
= { 13, key_expansion
};
2365 memcpy( key_expansion
, keyex
, sizeof keyex
);
2367 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_MASTERKEY
) {
2368 static const char msec
[] = "master secret";
2369 BYTE master_secret
[sizeof msec
];
2370 CRYPT_DATA_BLOB blobLabel
= { 13, master_secret
};
2371 BYTE abKeyValue
[48];
2373 memcpy( master_secret
, msec
, sizeof msec
);
2375 /* See RFC 2246, chapter 8.1 */
2376 if (!concat_data_blobs(&blobRandom
,
2377 &pCryptKey
->siSChannelInfo
.blobClientRandom
,
2378 &pCryptKey
->siSChannelInfo
.blobServerRandom
))
2382 tls1_prf(hProv
, hKey
, &blobLabel
, &blobRandom
, abKeyValue
, 48);
2383 pCryptKey
->dwState
= RSAENH_KEYSTATE_MASTERKEY
;
2384 memcpy(pCryptKey
->abKeyValue
, abKeyValue
, 48);
2385 free_data_blob(&blobRandom
);
2388 /* See RFC 2246, chapter 6.3 */
2389 if (!concat_data_blobs(&blobRandom
,
2390 &pCryptKey
->siSChannelInfo
.blobServerRandom
,
2391 &pCryptKey
->siSChannelInfo
.blobClientRandom
))
2395 tls1_prf(hProv
, hKey
, &blobKeyExpansion
, &blobRandom
, pCryptHash
->abHashValue
,
2396 RSAENH_MAX_HASH_SIZE
);
2397 free_data_blob(&blobRandom
);
2400 return init_hash(pCryptHash
);
2403 /******************************************************************************
2404 * CPDestroyHash (RSAENH.@)
2406 * Releases the handle to a hash object. The object is destroyed if its reference
2407 * count reaches zero.
2410 * hProv [I] Handle to the key container to which the hash object belongs.
2411 * hHash [I] Handle to the hash object to be released.
2417 BOOL WINAPI
RSAENH_CPDestroyHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
)
2419 TRACE("(hProv=%08Ix, hHash=%08Ix)\n", hProv
, hHash
);
2421 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2423 SetLastError(NTE_BAD_UID
);
2427 if (!release_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
))
2429 SetLastError(NTE_BAD_HASH
);
2436 /******************************************************************************
2437 * CPDestroyKey (RSAENH.@)
2439 * Releases the handle to a key object. The object is destroyed if its reference
2440 * count reaches zero.
2443 * hProv [I] Handle to the key container to which the key object belongs.
2444 * hKey [I] Handle to the key object to be released.
2450 BOOL WINAPI
RSAENH_CPDestroyKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
)
2452 TRACE("(hProv=%08Ix, hKey=%08Ix)\n", hProv
, hKey
);
2454 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2456 SetLastError(NTE_BAD_UID
);
2460 if (!release_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
))
2462 SetLastError(NTE_BAD_KEY
);
2469 /******************************************************************************
2470 * CPDuplicateHash (RSAENH.@)
2472 * Clones a hash object including its current state.
2475 * hUID [I] Handle to the key container the hash belongs to.
2476 * hHash [I] Handle to the hash object to be cloned.
2477 * pdwReserved [I] Reserved. Must be NULL.
2478 * dwFlags [I] No flags are currently defined. Must be 0.
2479 * phHash [O] Handle to the cloned hash object.
2485 BOOL WINAPI
RSAENH_CPDuplicateHash(HCRYPTPROV hUID
, HCRYPTHASH hHash
, DWORD
*pdwReserved
,
2486 DWORD dwFlags
, HCRYPTHASH
*phHash
)
2488 CRYPTHASH
*pSrcHash
, *pDestHash
;
2490 TRACE("(hUID=%08Ix, hHash=%08Ix, pdwReserved=%p, dwFlags=%08lx, phHash=%p)\n", hUID
, hHash
,
2491 pdwReserved
, dwFlags
, phHash
);
2493 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2495 SetLastError(NTE_BAD_UID
);
2499 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
, (OBJECTHDR
**)&pSrcHash
))
2501 SetLastError(NTE_BAD_HASH
);
2505 if (!phHash
|| pdwReserved
|| dwFlags
)
2507 SetLastError(ERROR_INVALID_PARAMETER
);
2511 *phHash
= new_object(&handle_table
, sizeof(CRYPTHASH
), RSAENH_MAGIC_HASH
,
2512 destroy_hash
, (OBJECTHDR
**)&pDestHash
);
2513 if (*phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
)
2515 *pDestHash
= *pSrcHash
;
2516 duplicate_hash_impl(pSrcHash
->hash_handle
, &pDestHash
->hash_handle
);
2517 copy_hmac_info(&pDestHash
->pHMACInfo
, pSrcHash
->pHMACInfo
);
2518 copy_data_blob(&pDestHash
->tpPRFParams
.blobLabel
, &pSrcHash
->tpPRFParams
.blobLabel
);
2519 copy_data_blob(&pDestHash
->tpPRFParams
.blobSeed
, &pSrcHash
->tpPRFParams
.blobSeed
);
2522 return *phHash
!= (HCRYPTHASH
)INVALID_HANDLE_VALUE
;
2525 /******************************************************************************
2526 * CPDuplicateKey (RSAENH.@)
2528 * Clones a key object including its current state.
2531 * hUID [I] Handle to the key container the hash belongs to.
2532 * hKey [I] Handle to the key object to be cloned.
2533 * pdwReserved [I] Reserved. Must be NULL.
2534 * dwFlags [I] No flags are currently defined. Must be 0.
2535 * phHash [O] Handle to the cloned key object.
2541 BOOL WINAPI
RSAENH_CPDuplicateKey(HCRYPTPROV hUID
, HCRYPTKEY hKey
, DWORD
*pdwReserved
,
2542 DWORD dwFlags
, HCRYPTKEY
*phKey
)
2544 CRYPTKEY
*pSrcKey
, *pDestKey
;
2546 TRACE("(hUID=%08Ix, hKey=%08Ix, pdwReserved=%p, dwFlags=%08lx, phKey=%p)\n", hUID
, hKey
,
2547 pdwReserved
, dwFlags
, phKey
);
2549 if (!is_valid_handle(&handle_table
, hUID
, RSAENH_MAGIC_CONTAINER
))
2551 SetLastError(NTE_BAD_UID
);
2555 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pSrcKey
))
2557 SetLastError(NTE_BAD_KEY
);
2561 if (!phKey
|| pdwReserved
|| dwFlags
)
2563 SetLastError(ERROR_INVALID_PARAMETER
);
2567 *phKey
= new_object(&handle_table
, sizeof(CRYPTKEY
), RSAENH_MAGIC_KEY
, destroy_key
,
2568 (OBJECTHDR
**)&pDestKey
);
2569 if (*phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
2571 *pDestKey
= *pSrcKey
;
2572 copy_data_blob(&pDestKey
->siSChannelInfo
.blobServerRandom
,
2573 &pSrcKey
->siSChannelInfo
.blobServerRandom
);
2574 copy_data_blob(&pDestKey
->siSChannelInfo
.blobClientRandom
,
2575 &pSrcKey
->siSChannelInfo
.blobClientRandom
);
2576 duplicate_key_impl(pSrcKey
->aiAlgid
, &pSrcKey
->context
, &pDestKey
->context
);
2585 /******************************************************************************
2586 * CPEncrypt (RSAENH.@)
2591 * hProv [I] The key container hKey and hHash belong to.
2592 * hKey [I] The key used to encrypt the data.
2593 * hHash [I] An optional hash object for parallel hashing. See notes.
2594 * Final [I] Indicates if this is the last block of data to encrypt.
2595 * dwFlags [I] Must be zero or CRYPT_OAEP
2596 * pbData [I/O] Pointer to the data to encrypt. Encrypted data will also be stored there.
2597 * pdwDataLen [I/O] I: Length of data to encrypt, O: Length of encrypted data.
2598 * dwBufLen [I] Size of the buffer at pbData.
2605 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2606 * This is useful for message signatures.
2608 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2610 BOOL WINAPI
RSAENH_CPEncrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2611 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
, DWORD dwBufLen
)
2613 CRYPTKEY
*pCryptKey
;
2615 TRACE("(hProv=%08Ix, hKey=%08Ix, hHash=%08Ix, Final=%d, dwFlags=%08lx, pbData=%p, "
2616 "pdwDataLen=%p, dwBufLen=%ld)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
,
2619 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2621 SetLastError(NTE_BAD_UID
);
2625 if (dwFlags
!= 0 && dwFlags
!= CRYPT_OAEP
)
2627 SetLastError(NTE_BAD_FLAGS
);
2631 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2633 SetLastError(NTE_BAD_KEY
);
2637 if (pCryptKey
->aiAlgid
== CALG_RC2
)
2639 const PROV_ENUMALGS_EX
*info
;
2641 if (!(info
= get_algid_info(hProv
, pCryptKey
->aiAlgid
)))
2643 FIXME("Can't get algid info.\n");
2644 SetLastError(NTE_BAD_KEY
);
2647 if (pCryptKey
->dwKeyLen
> info
->dwMaxLen
/ 8)
2649 SetLastError(NTE_BAD_KEY
);
2654 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2655 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2657 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2659 SetLastError(NTE_BAD_DATA
);
2663 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2664 if (!RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2667 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2668 if (!block_encrypt(pCryptKey
, pbData
, pdwDataLen
, dwBufLen
, Final
,
2669 &pCryptKey
->context
, pCryptKey
->abChainVector
))
2671 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2672 if (pbData
== NULL
) {
2673 *pdwDataLen
= dwBufLen
;
2676 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2677 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2678 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2679 SetLastError(NTE_BAD_KEY
);
2683 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2686 if (dwBufLen
< pCryptKey
->dwBlockLen
) {
2687 SetLastError(ERROR_MORE_DATA
);
2690 if (!pad_data(hProv
, pbData
, *pdwDataLen
, pbData
, pCryptKey
->dwBlockLen
, dwFlags
)) return FALSE
;
2691 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_ENCRYPT
);
2692 *pdwDataLen
= pCryptKey
->dwBlockLen
;
2695 SetLastError(NTE_BAD_TYPE
);
2699 if (Final
) setup_key(pCryptKey
);
2704 /******************************************************************************
2705 * CPDecrypt (RSAENH.@)
2710 * hProv [I] The key container hKey and hHash belong to.
2711 * hKey [I] The key used to decrypt the data.
2712 * hHash [I] An optional hash object for parallel hashing. See notes.
2713 * Final [I] Indicates if this is the last block of data to decrypt.
2714 * dwFlags [I] Must be zero or CRYPT_OAEP
2715 * pbData [I/O] Pointer to the data to decrypt. Plaintext will also be stored there.
2716 * pdwDataLen [I/O] I: Length of ciphertext, O: Length of plaintext.
2723 * If a hash object handle is provided in hHash, it will be updated with the plaintext.
2724 * This is useful for message signatures.
2726 * This function uses the standard WINAPI protocol for querying data of dynamic length.
2728 BOOL WINAPI
RSAENH_CPDecrypt(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTHASH hHash
, BOOL Final
,
2729 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2731 CRYPTKEY
*pCryptKey
;
2732 BYTE
*in
, out
[RSAENH_MAX_BLOCK_SIZE
], o
[RSAENH_MAX_BLOCK_SIZE
];
2736 TRACE("(hProv=%08Ix, hKey=%08Ix, hHash=%08Ix, Final=%d, dwFlags=%08lx, pbData=%p, "
2737 "pdwDataLen=%p)\n", hProv
, hKey
, hHash
, Final
, dwFlags
, pbData
, pdwDataLen
);
2739 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
2741 SetLastError(NTE_BAD_UID
);
2745 if (dwFlags
!= 0 && dwFlags
!= CRYPT_OAEP
)
2747 SetLastError(NTE_BAD_FLAGS
);
2751 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
2753 SetLastError(NTE_BAD_KEY
);
2757 if (pCryptKey
->dwState
== RSAENH_KEYSTATE_IDLE
)
2758 pCryptKey
->dwState
= RSAENH_KEYSTATE_ENCRYPTING
;
2760 if (pCryptKey
->dwState
!= RSAENH_KEYSTATE_ENCRYPTING
)
2762 SetLastError(NTE_BAD_DATA
);
2768 if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_BLOCK
) {
2769 for (i
=0, in
=pbData
; i
<*pdwDataLen
; i
+=pCryptKey
->dwBlockLen
, in
+=pCryptKey
->dwBlockLen
) {
2770 switch (pCryptKey
->dwMode
) {
2771 case CRYPT_MODE_ECB
:
2772 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2776 case CRYPT_MODE_CBC
:
2777 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
, in
, out
,
2779 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) out
[j
] ^= pCryptKey
->abChainVector
[j
];
2780 memcpy(pCryptKey
->abChainVector
, in
, pCryptKey
->dwBlockLen
);
2783 case CRYPT_MODE_CFB
:
2784 for (j
=0; j
<pCryptKey
->dwBlockLen
; j
++) {
2785 encrypt_block_impl(pCryptKey
->aiAlgid
, 0, &pCryptKey
->context
,
2786 pCryptKey
->abChainVector
, o
, RSAENH_ENCRYPT
);
2787 out
[j
] = in
[j
] ^ o
[0];
2788 for (k
=0; k
<pCryptKey
->dwBlockLen
-1; k
++)
2789 pCryptKey
->abChainVector
[k
] = pCryptKey
->abChainVector
[k
+1];
2790 pCryptKey
->abChainVector
[k
] = in
[j
];
2795 SetLastError(NTE_BAD_ALGID
);
2798 memcpy(in
, out
, pCryptKey
->dwBlockLen
);
2801 if (pbData
[*pdwDataLen
-1] &&
2802 pbData
[*pdwDataLen
-1] <= pCryptKey
->dwBlockLen
&&
2803 pbData
[*pdwDataLen
-1] <= *pdwDataLen
) {
2804 BOOL padOkay
= TRUE
;
2806 /* check that every pad byte has the same value */
2807 for (i
= 1; padOkay
&& i
< pbData
[*pdwDataLen
-1]; i
++)
2808 if (pbData
[*pdwDataLen
- i
- 1] != pbData
[*pdwDataLen
- 1])
2811 *pdwDataLen
-= pbData
[*pdwDataLen
-1];
2813 SetLastError(NTE_BAD_DATA
);
2814 setup_key(pCryptKey
);
2819 SetLastError(NTE_BAD_DATA
);
2820 setup_key(pCryptKey
);
2825 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_STREAM
) {
2826 encrypt_stream_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pbData
, *pdwDataLen
);
2827 } else if (GET_ALG_TYPE(pCryptKey
->aiAlgid
) == ALG_TYPE_RSA
) {
2828 if (pCryptKey
->aiAlgid
== CALG_RSA_SIGN
) {
2829 SetLastError(NTE_BAD_KEY
);
2832 encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbData
, pbData
, RSAENH_DECRYPT
);
2833 if (!unpad_data(hProv
, pbData
, pCryptKey
->dwBlockLen
, pbData
, pdwDataLen
, dwFlags
)) return FALSE
;
2836 SetLastError(NTE_BAD_TYPE
);
2840 if (Final
) setup_key(pCryptKey
);
2842 if (is_valid_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
)) {
2843 if (*pdwDataLen
>dwMax
||
2844 !RSAENH_CPHashData(hProv
, hHash
, pbData
, *pdwDataLen
, 0)) return FALSE
;
2850 static BOOL
crypt_export_simple(CRYPTKEY
*pCryptKey
, CRYPTKEY
*pPubKey
,
2851 DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
2853 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2854 ALG_ID
*pAlgid
= (ALG_ID
*)(pBlobHeader
+1);
2857 if (!(GET_ALG_CLASS(pCryptKey
->aiAlgid
)&(ALG_CLASS_DATA_ENCRYPT
|ALG_CLASS_MSG_ENCRYPT
))) {
2858 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
2862 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(ALG_ID
) + pPubKey
->dwBlockLen
;
2864 if (*pdwDataLen
< dwDataLen
) {
2865 SetLastError(ERROR_MORE_DATA
);
2866 *pdwDataLen
= dwDataLen
;
2870 pBlobHeader
->bType
= SIMPLEBLOB
;
2871 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2872 pBlobHeader
->reserved
= 0;
2873 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2875 *pAlgid
= pPubKey
->aiAlgid
;
2877 if (!pad_data(pCryptKey
->hProv
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
, (BYTE
*)(pAlgid
+1),
2878 pPubKey
->dwBlockLen
, dwFlags
))
2883 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PUBLIC
, &pPubKey
->context
, (BYTE
*)(pAlgid
+1),
2884 (BYTE
*)(pAlgid
+1), RSAENH_ENCRYPT
);
2886 *pdwDataLen
= dwDataLen
;
2890 static BOOL
crypt_export_public_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2893 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2894 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2897 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2898 SetLastError(NTE_BAD_KEY
);
2902 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + pCryptKey
->dwKeyLen
;
2904 if (*pdwDataLen
< dwDataLen
) {
2905 SetLastError(ERROR_MORE_DATA
);
2906 *pdwDataLen
= dwDataLen
;
2910 pBlobHeader
->bType
= PUBLICKEYBLOB
;
2911 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2912 pBlobHeader
->reserved
= 0;
2913 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2915 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA1
;
2916 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2918 export_public_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2919 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2921 *pdwDataLen
= dwDataLen
;
2925 static BOOL
crypt_export_private_key(CRYPTKEY
*pCryptKey
, BOOL force
,
2926 BYTE
*pbData
, DWORD
*pdwDataLen
)
2928 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2929 RSAPUBKEY
*pRSAPubKey
= (RSAPUBKEY
*)(pBlobHeader
+1);
2932 if ((pCryptKey
->aiAlgid
!= CALG_RSA_KEYX
) && (pCryptKey
->aiAlgid
!= CALG_RSA_SIGN
)) {
2933 SetLastError(NTE_BAD_KEY
);
2936 if (!force
&& !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
2938 SetLastError(NTE_BAD_KEY_STATE
);
2942 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
2943 2 * pCryptKey
->dwKeyLen
+ 5 * ((pCryptKey
->dwKeyLen
+ 1) >> 1);
2945 if (*pdwDataLen
< dwDataLen
) {
2946 SetLastError(ERROR_MORE_DATA
);
2947 *pdwDataLen
= dwDataLen
;
2951 pBlobHeader
->bType
= PRIVATEKEYBLOB
;
2952 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2953 pBlobHeader
->reserved
= 0;
2954 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2956 pRSAPubKey
->magic
= RSAENH_MAGIC_RSA2
;
2957 pRSAPubKey
->bitlen
= pCryptKey
->dwKeyLen
<< 3;
2959 export_private_key_impl((BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
2960 pCryptKey
->dwKeyLen
, &pRSAPubKey
->pubexp
);
2962 *pdwDataLen
= dwDataLen
;
2966 static BOOL
crypt_export_plaintext_key(CRYPTKEY
*pCryptKey
, BYTE
*pbData
,
2969 BLOBHEADER
*pBlobHeader
= (BLOBHEADER
*)pbData
;
2970 DWORD
*pKeyLen
= (DWORD
*)(pBlobHeader
+1);
2971 BYTE
*pbKey
= (BYTE
*)(pKeyLen
+1);
2974 dwDataLen
= sizeof(BLOBHEADER
) + sizeof(DWORD
) + pCryptKey
->dwKeyLen
;
2976 if (*pdwDataLen
< dwDataLen
) {
2977 SetLastError(ERROR_MORE_DATA
);
2978 *pdwDataLen
= dwDataLen
;
2982 pBlobHeader
->bType
= PLAINTEXTKEYBLOB
;
2983 pBlobHeader
->bVersion
= CUR_BLOB_VERSION
;
2984 pBlobHeader
->reserved
= 0;
2985 pBlobHeader
->aiKeyAlg
= pCryptKey
->aiAlgid
;
2987 *pKeyLen
= pCryptKey
->dwKeyLen
;
2988 memcpy(pbKey
, pCryptKey
->abKeyValue
, pCryptKey
->dwKeyLen
);
2990 *pdwDataLen
= dwDataLen
;
2993 /******************************************************************************
2994 * crypt_export_key [Internal]
2996 * Export a key into a binary large object (BLOB). Called by CPExportKey and
2997 * by store_key_pair.
3000 * pCryptKey [I] Key to be exported.
3001 * hPubKey [I] Key used to encrypt sensitive BLOB data.
3002 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
3003 * dwFlags [I] Currently none defined.
3004 * force [I] If TRUE, the key is written no matter what the key's
3005 * permissions are. Otherwise the key's permissions are
3006 * checked before exporting.
3007 * pbData [O] Pointer to a buffer where the BLOB will be written to.
3008 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
3014 static BOOL
crypt_export_key(CRYPTKEY
*pCryptKey
, HCRYPTKEY hPubKey
,
3015 DWORD dwBlobType
, DWORD dwFlags
, BOOL force
,
3016 BYTE
*pbData
, DWORD
*pdwDataLen
)
3020 if (dwFlags
& CRYPT_SSL2_FALLBACK
) {
3021 if (pCryptKey
->aiAlgid
!= CALG_SSL2_MASTER
) {
3022 SetLastError(NTE_BAD_KEY
);
3027 switch ((BYTE
)dwBlobType
)
3030 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
)){
3031 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error_code? */
3034 return crypt_export_simple(pCryptKey
, pPubKey
, dwFlags
, pbData
,
3038 if (is_valid_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
)) {
3039 SetLastError(NTE_BAD_KEY
); /* FIXME: error code? */
3043 return crypt_export_public_key(pCryptKey
, pbData
, pdwDataLen
);
3045 case PRIVATEKEYBLOB
:
3046 return crypt_export_private_key(pCryptKey
, force
, pbData
, pdwDataLen
);
3048 case PLAINTEXTKEYBLOB
:
3049 return crypt_export_plaintext_key(pCryptKey
, pbData
, pdwDataLen
);
3052 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3057 /******************************************************************************
3058 * CPExportKey (RSAENH.@)
3060 * Export a key into a binary large object (BLOB).
3063 * hProv [I] Key container from which a key is to be exported.
3064 * hKey [I] Key to be exported.
3065 * hPubKey [I] Key used to encrypt sensitive BLOB data.
3066 * dwBlobType [I] SIMPLEBLOB, PUBLICKEYBLOB or PRIVATEKEYBLOB.
3067 * dwFlags [I] Currently none defined.
3068 * pbData [O] Pointer to a buffer where the BLOB will be written to.
3069 * pdwDataLen [I/O] I: Size of buffer at pbData, O: Size of BLOB
3075 BOOL WINAPI
RSAENH_CPExportKey(HCRYPTPROV hProv
, HCRYPTKEY hKey
, HCRYPTKEY hPubKey
,
3076 DWORD dwBlobType
, DWORD dwFlags
, BYTE
*pbData
, DWORD
*pdwDataLen
)
3078 CRYPTKEY
*pCryptKey
;
3080 TRACE("(hProv=%08Ix, hKey=%08Ix, hPubKey=%08Ix, dwBlobType=%08lx, dwFlags=%08lx, pbData=%p,"
3081 "pdwDataLen=%p)\n", hProv
, hKey
, hPubKey
, dwBlobType
, dwFlags
, pbData
, pdwDataLen
);
3083 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3085 SetLastError(NTE_BAD_UID
);
3089 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3091 SetLastError(NTE_BAD_KEY
);
3095 return crypt_export_key(pCryptKey
, hPubKey
, dwBlobType
, dwFlags
, FALSE
,
3096 pbData
, pdwDataLen
);
3099 /******************************************************************************
3100 * release_and_install_key [Internal]
3102 * Release an existing key, if present, and replaces it with a new one.
3105 * hProv [I] Key container into which the key is to be imported.
3106 * src [I] Key which will replace *dest
3107 * dest [I] Points to key to be released and replaced with src
3108 * fStoreKey [I] If TRUE, the newly installed key is stored to the registry.
3110 static void release_and_install_key(HCRYPTPROV hProv
, HCRYPTKEY src
,
3111 HCRYPTKEY
*dest
, DWORD fStoreKey
)
3113 RSAENH_CPDestroyKey(hProv
, *dest
);
3114 copy_handle(&handle_table
, src
, RSAENH_MAGIC_KEY
, dest
);
3117 KEYCONTAINER
*pKeyContainer
;
3119 if ((pKeyContainer
= get_key_container(hProv
)))
3121 store_key_container_keys(pKeyContainer
);
3122 store_key_container_permissions(pKeyContainer
);
3127 /******************************************************************************
3128 * import_private_key [Internal]
3130 * Import a BLOB'ed private key into a key container.
3133 * hProv [I] Key container into which the private key is to be imported.
3134 * pbData [I] Pointer to a buffer which holds the private key BLOB.
3135 * dwDataLen [I] Length of data in buffer at pbData.
3136 * dwFlags [I] One of:
3137 * CRYPT_EXPORTABLE: the imported key is marked exportable
3138 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3139 * phKey [O] Handle to the imported key.
3143 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
3144 * it's a PRIVATEKEYBLOB.
3150 static BOOL
import_private_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3151 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
3153 KEYCONTAINER
*pKeyContainer
;
3154 CRYPTKEY
*pCryptKey
;
3155 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3156 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
3159 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
3161 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
3162 SetLastError(NTE_BAD_FLAGS
);
3165 if (!(pKeyContainer
= get_key_container(hProv
)))
3168 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)))
3170 ERR("datalen %ld not long enough for a BLOBHEADER + RSAPUBKEY\n",
3172 SetLastError(NTE_BAD_DATA
);
3175 if (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA2
)
3177 ERR("unexpected magic %08lx\n", pRSAPubKey
->magic
);
3178 SetLastError(NTE_BAD_DATA
);
3181 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
3182 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4))))
3184 DWORD expectedLen
= sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) +
3185 (pRSAPubKey
->bitlen
>> 3) + (5 * ((pRSAPubKey
->bitlen
+8)>>4));
3187 ERR("blob too short for pub key: expect %ld, got %ld\n",
3188 expectedLen
, dwDataLen
);
3189 SetLastError(NTE_BAD_DATA
);
3193 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
3194 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3195 setup_key(pCryptKey
);
3196 ret
= import_private_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
3197 pRSAPubKey
->bitlen
/8, dwDataLen
, pRSAPubKey
->pubexp
);
3199 if (dwFlags
& CRYPT_EXPORTABLE
)
3200 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3201 switch (pBlobHeader
->aiKeyAlg
)
3205 TRACE("installing signing key\n");
3206 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hSignatureKeyPair
,
3209 case AT_KEYEXCHANGE
:
3211 TRACE("installing key exchange key\n");
3212 release_and_install_key(hProv
, *phKey
, &pKeyContainer
->hKeyExchangeKeyPair
,
3220 /******************************************************************************
3221 * import_public_key [Internal]
3223 * Import a BLOB'ed public key.
3227 * pbData [I] Pointer to a buffer which holds the public key BLOB.
3228 * dwDataLen [I] Length of data in buffer at pbData.
3229 * dwFlags [I] One of:
3230 * CRYPT_EXPORTABLE: the imported key is marked exportable
3231 * phKey [O] Handle to the imported key.
3235 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
3236 * it's a PUBLICKEYBLOB.
3242 static BOOL
import_public_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3243 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3245 CRYPTKEY
*pCryptKey
;
3246 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3247 const RSAPUBKEY
*pRSAPubKey
= (const RSAPUBKEY
*)(pBlobHeader
+1);
3251 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
3253 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
3254 SetLastError(NTE_BAD_FLAGS
);
3258 if ((dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
)) ||
3259 (pRSAPubKey
->magic
!= RSAENH_MAGIC_RSA1
) ||
3260 (dwDataLen
< sizeof(BLOBHEADER
) + sizeof(RSAPUBKEY
) + (pRSAPubKey
->bitlen
>> 3)))
3262 SetLastError(NTE_BAD_DATA
);
3266 /* Since this is a public key blob, only the public key is
3267 * available, so only signature verification is possible.
3269 algID
= pBlobHeader
->aiKeyAlg
;
3270 *phKey
= new_key(hProv
, algID
, MAKELONG(0,pRSAPubKey
->bitlen
), &pCryptKey
);
3271 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
3272 setup_key(pCryptKey
);
3273 ret
= import_public_key_impl((const BYTE
*)(pRSAPubKey
+1), &pCryptKey
->context
,
3274 pRSAPubKey
->bitlen
>> 3, pRSAPubKey
->pubexp
);
3276 if (dwFlags
& CRYPT_EXPORTABLE
)
3277 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3282 /******************************************************************************
3283 * import_symmetric_key [Internal]
3285 * Import a BLOB'ed symmetric key into a key container.
3288 * hProv [I] Key container into which the symmetric key is to be imported.
3289 * pbData [I] Pointer to a buffer which holds the symmetric key BLOB.
3290 * dwDataLen [I] Length of data in buffer at pbData.
3291 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3292 * dwFlags [I] One of:
3293 * CRYPT_EXPORTABLE: the imported key is marked exportable
3294 * phKey [O] Handle to the imported key.
3298 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
3299 * it's a SIMPLEBLOB.
3305 static BOOL
import_symmetric_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3306 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3308 CRYPTKEY
*pCryptKey
, *pPubKey
;
3309 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3310 const ALG_ID
*pAlgid
= (const ALG_ID
*)(pBlobHeader
+1);
3311 const BYTE
*pbKeyStream
= (const BYTE
*)(pAlgid
+ 1);
3315 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
3317 FIXME("unimplemented for CRYPT_IPSEC_HMAC_KEY\n");
3318 SetLastError(NTE_BAD_FLAGS
);
3321 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pPubKey
) ||
3322 pPubKey
->aiAlgid
!= CALG_RSA_KEYX
)
3324 SetLastError(NTE_BAD_PUBLIC_KEY
); /* FIXME: error code? */
3328 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(ALG_ID
)+pPubKey
->dwBlockLen
)
3330 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
3334 pbDecrypted
= malloc(pPubKey
->dwBlockLen
);
3335 if (!pbDecrypted
) return FALSE
;
3336 encrypt_block_impl(pPubKey
->aiAlgid
, PK_PRIVATE
, &pPubKey
->context
, pbKeyStream
, pbDecrypted
,
3339 dwKeyLen
= RSAENH_MAX_KEY_SIZE
;
3340 if (!unpad_data(hProv
, pbDecrypted
, pPubKey
->dwBlockLen
, pbDecrypted
, &dwKeyLen
, dwFlags
)) {
3345 if (pBlobHeader
->aiKeyAlg
== CALG_RC2
)
3347 const PROV_ENUMALGS_EX
*info
;
3349 info
= get_algid_info(hProv
, CALG_RC2
);
3352 dwKeyLen
= info
->dwDefaultLen
;
3353 if (dwKeyLen
< info
->dwMinLen
/ 8 || dwKeyLen
> 128)
3355 WARN("Invalid RC2 key, len %ld.\n", dwKeyLen
);
3356 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3357 SetLastError(NTE_BAD_DATA
);
3359 else if ((*phKey
= alloc_key(hProv
, pBlobHeader
->aiKeyAlg
, 0, dwKeyLen
<< 3, &pCryptKey
))
3360 != (HCRYPTKEY
)INVALID_HANDLE_VALUE
&& info
->dwDefaultLen
== 40 && dwKeyLen
> info
->dwMaxLen
/ 8)
3362 pCryptKey
->dwEffectiveKeyLen
= 40;
3367 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, dwKeyLen
<<19, &pCryptKey
);
3369 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3374 memcpy(pCryptKey
->abKeyValue
, pbDecrypted
, dwKeyLen
);
3376 setup_key(pCryptKey
);
3377 if (dwFlags
& CRYPT_EXPORTABLE
)
3378 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3382 /******************************************************************************
3383 * import_plaintext_key [Internal]
3385 * Import a plaintext key into a key container.
3388 * hProv [I] Key container into which the symmetric key is to be imported.
3389 * pbData [I] Pointer to a buffer which holds the plaintext key BLOB.
3390 * dwDataLen [I] Length of data in buffer at pbData.
3391 * dwFlags [I] One of:
3392 * CRYPT_EXPORTABLE: the imported key is marked exportable
3393 * phKey [O] Handle to the imported key.
3397 * Assumes the caller has already checked the BLOBHEADER at pbData to ensure
3398 * it's a PLAINTEXTKEYBLOB.
3404 static BOOL
import_plaintext_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3405 DWORD dwFlags
, HCRYPTKEY
*phKey
)
3407 CRYPTKEY
*pCryptKey
;
3408 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3409 const DWORD
*pKeyLen
= (const DWORD
*)(pBlobHeader
+ 1);
3410 const BYTE
*pbKeyStream
= (const BYTE
*)(pKeyLen
+ 1);
3412 if (dwDataLen
< sizeof(BLOBHEADER
)+sizeof(DWORD
)+*pKeyLen
)
3414 SetLastError(NTE_BAD_DATA
); /* FIXME: error code */
3418 if (dwFlags
& CRYPT_IPSEC_HMAC_KEY
)
3420 *phKey
= new_key(hProv
, CALG_HMAC
, 0, &pCryptKey
);
3421 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3423 if (*pKeyLen
<= RSAENH_MIN(sizeof(pCryptKey
->abKeyValue
), RSAENH_HMAC_BLOCK_LEN
))
3425 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3426 pCryptKey
->dwKeyLen
= *pKeyLen
;
3430 CRYPT_DATA_BLOB blobHmacKey
= { *pKeyLen
, (BYTE
*)pbKeyStream
};
3432 /* In order to initialize an HMAC key, the key material is hashed,
3433 * and the output of the hash function is used as the key material.
3434 * Unfortunately, the way the Crypto API is designed, we don't know
3435 * the hash algorithm yet, so we have to copy the entire key
3438 if (!copy_data_blob(&pCryptKey
->blobHmacKey
, &blobHmacKey
))
3440 release_handle(&handle_table
, *phKey
, RSAENH_MAGIC_KEY
);
3441 *phKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3445 setup_key(pCryptKey
);
3446 if (dwFlags
& CRYPT_EXPORTABLE
)
3447 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3451 *phKey
= new_key(hProv
, pBlobHeader
->aiKeyAlg
, *pKeyLen
<<19, &pCryptKey
);
3452 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
3454 memcpy(pCryptKey
->abKeyValue
, pbKeyStream
, *pKeyLen
);
3455 setup_key(pCryptKey
);
3456 if (dwFlags
& CRYPT_EXPORTABLE
)
3457 pCryptKey
->dwPermissions
|= CRYPT_EXPORT
;
3462 /******************************************************************************
3463 * import_key [Internal]
3465 * Import a BLOB'ed key into a key container, optionally storing the key's
3466 * value to the registry.
3469 * hProv [I] Key container into which the key is to be imported.
3470 * pbData [I] Pointer to a buffer which holds the BLOB.
3471 * dwDataLen [I] Length of data in buffer at pbData.
3472 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3473 * dwFlags [I] One of:
3474 * CRYPT_EXPORTABLE: the imported key is marked exportable
3475 * fStoreKey [I] If TRUE, the imported key is stored to the registry.
3476 * phKey [O] Handle to the imported key.
3482 static BOOL
import_key(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
, HCRYPTKEY hPubKey
,
3483 DWORD dwFlags
, BOOL fStoreKey
, HCRYPTKEY
*phKey
)
3485 KEYCONTAINER
*pKeyContainer
;
3486 const BLOBHEADER
*pBlobHeader
= (const BLOBHEADER
*)pbData
;
3488 if (!(pKeyContainer
= get_key_container(hProv
)))
3491 if (dwDataLen
< sizeof(BLOBHEADER
) ||
3492 pBlobHeader
->bVersion
!= CUR_BLOB_VERSION
||
3493 pBlobHeader
->reserved
!= 0)
3495 TRACE("bVersion = %d, reserved = %d\n", pBlobHeader
->bVersion
,
3496 pBlobHeader
->reserved
);
3497 SetLastError(NTE_BAD_DATA
);
3501 /* If this is a verify-only context, the key is not persisted regardless of
3502 * fStoreKey's original value.
3504 fStoreKey
= fStoreKey
&& !(dwFlags
& CRYPT_VERIFYCONTEXT
);
3505 TRACE("blob type: %x\n", pBlobHeader
->bType
);
3506 switch (pBlobHeader
->bType
)
3508 case PRIVATEKEYBLOB
:
3509 return import_private_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3513 return import_public_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3517 return import_symmetric_key(hProv
, pbData
, dwDataLen
, hPubKey
,
3520 case PLAINTEXTKEYBLOB
:
3521 return import_plaintext_key(hProv
, pbData
, dwDataLen
, dwFlags
,
3525 SetLastError(NTE_BAD_TYPE
); /* FIXME: error code? */
3530 /******************************************************************************
3531 * CPImportKey (RSAENH.@)
3533 * Import a BLOB'ed key into a key container.
3536 * hProv [I] Key container into which the key is to be imported.
3537 * pbData [I] Pointer to a buffer which holds the BLOB.
3538 * dwDataLen [I] Length of data in buffer at pbData.
3539 * hPubKey [I] Key used to decrypt sensitive BLOB data.
3540 * dwFlags [I] One of:
3541 * CRYPT_EXPORTABLE: the imported key is marked exportable
3542 * phKey [O] Handle to the imported key.
3548 BOOL WINAPI
RSAENH_CPImportKey(HCRYPTPROV hProv
, const BYTE
*pbData
, DWORD dwDataLen
,
3549 HCRYPTKEY hPubKey
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3551 TRACE("(hProv=%08Ix, pbData=%p, dwDataLen=%ld, hPubKey=%08Ix, dwFlags=%08lx, phKey=%p)\n",
3552 hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, phKey
);
3554 return import_key(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, TRUE
, phKey
);
3557 /******************************************************************************
3558 * CPGenKey (RSAENH.@)
3560 * Generate a key in the key container
3563 * hProv [I] Key container for which a key is to be generated.
3564 * Algid [I] Crypto algorithm identifier for the key to be generated.
3565 * dwFlags [I] Upper 16 bits: Binary length of key. Lower 16 bits: Flags. See Notes
3566 * phKey [O] Handle to the generated key.
3573 * Flags currently not considered.
3576 * Private key-exchange- and signature-keys can be generated with Algid AT_KEYEXCHANGE
3577 * and AT_SIGNATURE values.
3579 BOOL WINAPI
RSAENH_CPGenKey(HCRYPTPROV hProv
, ALG_ID Algid
, DWORD dwFlags
, HCRYPTKEY
*phKey
)
3581 KEYCONTAINER
*pKeyContainer
;
3582 CRYPTKEY
*pCryptKey
;
3584 TRACE("(hProv=%08Ix, aiAlgid=%d, dwFlags=%08lx, phKey=%p)\n", hProv
, Algid
, dwFlags
, phKey
);
3586 if (!(pKeyContainer
= get_key_container(hProv
)))
3588 /* MSDN: hProv not containing valid context handle */
3596 *phKey
= new_key(hProv
, CALG_RSA_SIGN
, dwFlags
, &pCryptKey
);
3598 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3599 setup_key(pCryptKey
);
3600 release_and_install_key(hProv
, *phKey
,
3601 &pKeyContainer
->hSignatureKeyPair
,
3606 case AT_KEYEXCHANGE
:
3608 *phKey
= new_key(hProv
, CALG_RSA_KEYX
, dwFlags
, &pCryptKey
);
3610 new_key_impl(pCryptKey
->aiAlgid
, &pCryptKey
->context
, pCryptKey
->dwKeyLen
);
3611 setup_key(pCryptKey
);
3612 release_and_install_key(hProv
, *phKey
,
3613 &pKeyContainer
->hKeyExchangeKeyPair
,
3626 case CALG_PCT1_MASTER
:
3627 case CALG_SSL2_MASTER
:
3628 case CALG_SSL3_MASTER
:
3629 case CALG_TLS1_MASTER
:
3630 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
3632 gen_rand_impl(pCryptKey
->abKeyValue
, RSAENH_MAX_KEY_SIZE
);
3634 case CALG_SSL3_MASTER
:
3635 pCryptKey
->abKeyValue
[0] = RSAENH_SSL3_VERSION_MAJOR
;
3636 pCryptKey
->abKeyValue
[1] = RSAENH_SSL3_VERSION_MINOR
;
3639 case CALG_TLS1_MASTER
:
3640 pCryptKey
->abKeyValue
[0] = RSAENH_TLS1_VERSION_MAJOR
;
3641 pCryptKey
->abKeyValue
[1] = RSAENH_TLS1_VERSION_MINOR
;
3644 setup_key(pCryptKey
);
3649 /* MSDN: Algorithm not supported specified by Algid */
3650 SetLastError(NTE_BAD_ALGID
);
3654 return *phKey
!= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
3657 /******************************************************************************
3658 * CPGenRandom (RSAENH.@)
3660 * Generate a random byte stream.
3663 * hProv [I] Key container that is used to generate random bytes.
3664 * dwLen [I] Specifies the number of requested random data bytes.
3665 * pbBuffer [O] Random bytes will be stored here.
3671 BOOL WINAPI
RSAENH_CPGenRandom(HCRYPTPROV hProv
, DWORD dwLen
, BYTE
*pbBuffer
)
3673 TRACE("(hProv=%08Ix, dwLen=%ld, pbBuffer=%p)\n", hProv
, dwLen
, pbBuffer
);
3675 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3677 /* MSDN: hProv not containing valid context handle */
3678 SetLastError(NTE_BAD_UID
);
3682 return gen_rand_impl(pbBuffer
, dwLen
);
3685 /******************************************************************************
3686 * CPGetHashParam (RSAENH.@)
3688 * Query parameters of an hash object.
3691 * hProv [I] The kea container, which the hash belongs to.
3692 * hHash [I] The hash object that is to be queried.
3693 * dwParam [I] Specifies the parameter that is to be queried.
3694 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3695 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3696 * dwFlags [I] None currently defined.
3703 * Valid dwParams are: HP_ALGID, HP_HASHSIZE, HP_HASHVALUE. The hash will be
3704 * finalized if HP_HASHVALUE is queried.
3706 BOOL WINAPI
RSAENH_CPGetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
, BYTE
*pbData
,
3707 DWORD
*pdwDataLen
, DWORD dwFlags
)
3709 CRYPTHASH
*pCryptHash
;
3711 TRACE("(hProv=%08Ix, hHash=%08Ix, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
3712 hProv
, hHash
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
3714 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3716 SetLastError(NTE_BAD_UID
);
3722 SetLastError(NTE_BAD_FLAGS
);
3726 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
3727 (OBJECTHDR
**)&pCryptHash
))
3729 SetLastError(NTE_BAD_HASH
);
3735 SetLastError(ERROR_INVALID_PARAMETER
);
3742 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->aiAlgid
,
3746 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptHash
->dwHashSize
,
3750 if (pCryptHash
->aiAlgid
== CALG_TLS1PRF
) {
3751 return tls1_prf(hProv
, pCryptHash
->hKey
, &pCryptHash
->tpPRFParams
.blobLabel
,
3752 &pCryptHash
->tpPRFParams
.blobSeed
, pbData
, *pdwDataLen
);
3755 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_FINISHED
)
3757 finalize_hash(pCryptHash
);
3758 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
3763 *pdwDataLen
= pCryptHash
->dwHashSize
;
3767 return copy_param(pbData
, pdwDataLen
, pCryptHash
->abHashValue
,
3768 pCryptHash
->dwHashSize
);
3771 SetLastError(NTE_BAD_TYPE
);
3776 /******************************************************************************
3777 * CPSetKeyParam (RSAENH.@)
3779 * Set a parameter of a key object
3782 * hProv [I] The key container to which the key belongs.
3783 * hKey [I] The key for which a parameter is to be set.
3784 * dwParam [I] Parameter type. See Notes.
3785 * pbData [I] Pointer to the parameter value.
3786 * dwFlags [I] Currently none defined.
3793 * Defined dwParam types are:
3794 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
3795 * - KP_MODE_BITS: Shift width for cipher feedback mode. (Currently ignored by MS CSP's)
3796 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
3797 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
3798 * - KP_IV: Initialization vector
3800 BOOL WINAPI
RSAENH_CPSetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
3803 CRYPTKEY
*pCryptKey
;
3805 TRACE("(hProv=%08Ix, hKey=%08Ix, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv
, hKey
,
3806 dwParam
, pbData
, dwFlags
);
3808 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
3810 SetLastError(NTE_BAD_UID
);
3815 SetLastError(NTE_BAD_FLAGS
);
3819 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
3821 SetLastError(NTE_BAD_KEY
);
3827 /* The MS providers only support PKCS5_PADDING */
3828 if (*(DWORD
*)pbData
!= PKCS5_PADDING
) {
3829 SetLastError(NTE_BAD_DATA
);
3835 pCryptKey
->dwMode
= *(DWORD
*)pbData
;
3839 pCryptKey
->dwModeBits
= *(DWORD
*)pbData
;
3842 case KP_PERMISSIONS
:
3844 DWORD perms
= *(DWORD
*)pbData
;
3846 if ((perms
& CRYPT_EXPORT
) &&
3847 !(pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3849 SetLastError(NTE_BAD_DATA
);
3852 else if (!(perms
& CRYPT_EXPORT
) &&
3853 (pCryptKey
->dwPermissions
& CRYPT_EXPORT
))
3855 /* Clearing the export permission appears to be ignored,
3858 perms
|= CRYPT_EXPORT
;
3860 pCryptKey
->dwPermissions
= perms
;
3865 memcpy(pCryptKey
->abInitVector
, pbData
, pCryptKey
->dwBlockLen
);
3866 setup_key(pCryptKey
);
3870 switch (pCryptKey
->aiAlgid
) {
3874 KEYCONTAINER
*pKeyContainer
= get_key_container(pCryptKey
->hProv
);
3877 SetLastError(ERROR_INVALID_PARAMETER
);
3880 /* MSDN: the base provider always sets eleven bytes of
3883 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
,
3885 pCryptKey
->dwSaltLen
= 11;
3886 setup_key(pCryptKey
);
3887 /* After setting the salt value if the provider is not base or
3888 * strong the salt length will be reset. */
3889 if (pKeyContainer
->dwPersonality
!= RSAENH_PERSONALITY_BASE
&&
3890 pKeyContainer
->dwPersonality
!= RSAENH_PERSONALITY_STRONG
)
3891 pCryptKey
->dwSaltLen
= 0;
3895 SetLastError(NTE_BAD_KEY
);
3902 CRYPT_INTEGER_BLOB
*blob
= (CRYPT_INTEGER_BLOB
*)pbData
;
3904 /* salt length can't be greater than 184 bits = 24 bytes */
3905 if (blob
->cbData
> 24)
3907 SetLastError(NTE_BAD_DATA
);
3910 memcpy(pCryptKey
->abKeyValue
+ pCryptKey
->dwKeyLen
, blob
->pbData
,
3912 pCryptKey
->dwSaltLen
= blob
->cbData
;
3913 setup_key(pCryptKey
);
3917 case KP_EFFECTIVE_KEYLEN
:
3918 switch (pCryptKey
->aiAlgid
) {
3921 DWORD keylen
, deflen
;
3923 KEYCONTAINER
*pKeyContainer
= get_key_container(pCryptKey
->hProv
);
3927 SetLastError(ERROR_INVALID_PARAMETER
);
3930 keylen
= *(DWORD
*)pbData
;
3931 if (!keylen
|| keylen
> 1024)
3933 SetLastError(NTE_BAD_DATA
);
3938 * The Base provider will force the key length to default
3939 * and set an error state if a key length different from
3940 * the default is tried.
3942 deflen
= aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]->dwDefaultLen
;
3943 if (pKeyContainer
->dwPersonality
== RSAENH_PERSONALITY_BASE
3944 && keylen
!= deflen
)
3947 SetLastError(NTE_BAD_DATA
);
3950 pCryptKey
->dwEffectiveKeyLen
= keylen
;
3951 setup_key(pCryptKey
);
3955 SetLastError(NTE_BAD_TYPE
);
3960 case KP_SCHANNEL_ALG
:
3961 switch (((PSCHANNEL_ALG
)pbData
)->dwUse
) {
3962 case SCHANNEL_ENC_KEY
:
3963 memcpy(&pCryptKey
->siSChannelInfo
.saEncAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3966 case SCHANNEL_MAC_KEY
:
3967 memcpy(&pCryptKey
->siSChannelInfo
.saMACAlg
, pbData
, sizeof(SCHANNEL_ALG
));
3971 SetLastError(NTE_FAIL
); /* FIXME: error code */
3976 case KP_CLIENT_RANDOM
:
3977 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobClientRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3979 case KP_SERVER_RANDOM
:
3980 return copy_data_blob(&pCryptKey
->siSChannelInfo
.blobServerRandom
, (PCRYPT_DATA_BLOB
)pbData
);
3983 SetLastError(NTE_BAD_TYPE
);
3988 /******************************************************************************
3989 * CPGetKeyParam (RSAENH.@)
3991 * Query a key parameter.
3994 * hProv [I] The key container, which the key belongs to.
3995 * hHash [I] The key object that is to be queried.
3996 * dwParam [I] Specifies the parameter that is to be queried.
3997 * pbData [I] Pointer to the buffer where the parameter value will be stored.
3998 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
3999 * dwFlags [I] None currently defined.
4006 * Defined dwParam types are:
4007 * - KP_MODE: Values MODE_CBC, MODE_ECB, MODE_CFB.
4008 * - KP_MODE_BITS: Shift width for cipher feedback mode.
4009 * (Currently ignored by MS CSP's - always eight)
4010 * - KP_PERMISSIONS: Or'ed combination of CRYPT_ENCRYPT, CRYPT_DECRYPT,
4011 * CRYPT_EXPORT, CRYPT_READ, CRYPT_WRITE, CRYPT_MAC
4012 * - KP_IV: Initialization vector.
4013 * - KP_KEYLEN: Bitwidth of the key.
4014 * - KP_BLOCKLEN: Size of a block cipher block.
4015 * - KP_SALT: Salt value.
4017 BOOL WINAPI
RSAENH_CPGetKeyParam(HCRYPTPROV hProv
, HCRYPTKEY hKey
, DWORD dwParam
, BYTE
*pbData
,
4018 DWORD
*pdwDataLen
, DWORD dwFlags
)
4020 CRYPTKEY
*pCryptKey
;
4023 TRACE("(hProv=%08Ix, hKey=%08Ix, dwParam=%08lx, pbData=%p, pdwDataLen=%p dwFlags=%08lx)\n",
4024 hProv
, hKey
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
4026 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4028 SetLastError(NTE_BAD_UID
);
4033 SetLastError(NTE_BAD_FLAGS
);
4037 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pCryptKey
))
4039 SetLastError(NTE_BAD_KEY
);
4046 return copy_param(pbData
, pdwDataLen
, pCryptKey
->abInitVector
,
4047 pCryptKey
->dwBlockLen
);
4050 switch (pCryptKey
->aiAlgid
) {
4053 return copy_param(pbData
, pdwDataLen
,
4054 &pCryptKey
->abKeyValue
[pCryptKey
->dwKeyLen
],
4055 pCryptKey
->dwSaltLen
);
4057 SetLastError(NTE_BAD_KEY
);
4062 dwValue
= PKCS5_PADDING
;
4063 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
4066 dwValue
= pCryptKey
->dwKeyLen
<< 3;
4067 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
4069 case KP_EFFECTIVE_KEYLEN
:
4070 if (pCryptKey
->dwEffectiveKeyLen
)
4071 dwValue
= pCryptKey
->dwEffectiveKeyLen
;
4073 dwValue
= pCryptKey
->dwKeyLen
<< 3;
4074 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
4077 dwValue
= pCryptKey
->dwBlockLen
<< 3;
4078 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwValue
, sizeof(DWORD
));
4081 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwMode
, sizeof(DWORD
));
4084 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwModeBits
,
4087 case KP_PERMISSIONS
:
4088 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->dwPermissions
,
4092 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&pCryptKey
->aiAlgid
, sizeof(DWORD
));
4095 SetLastError(NTE_BAD_TYPE
);
4100 /******************************************************************************
4101 * CPGetProvParam (RSAENH.@)
4103 * Query a CSP parameter.
4106 * hProv [I] The key container that is to be queried.
4107 * dwParam [I] Specifies the parameter that is to be queried.
4108 * pbData [I] Pointer to the buffer where the parameter value will be stored.
4109 * pdwDataLen [I/O] I: Buffer length at pbData, O: Length of the parameter value.
4110 * dwFlags [I] CRYPT_FIRST: Start enumeration (for PP_ENUMALGS{_EX}).
4116 * Defined dwParam types:
4117 * - PP_CONTAINER: Name of the key container.
4118 * - PP_NAME: Name of the cryptographic service provider.
4119 * - PP_SIG_KEYSIZE_INC: RSA signature keywidth granularity in bits.
4120 * - PP_KEYX_KEYSIZE_INC: RSA key-exchange keywidth granularity in bits.
4121 * - PP_ENUMALGS{_EX}: Query provider capabilities.
4122 * - PP_KEYSET_SEC_DESCR: Retrieve security descriptor on container.
4124 BOOL WINAPI
RSAENH_CPGetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
,
4125 DWORD
*pdwDataLen
, DWORD dwFlags
)
4127 KEYCONTAINER
*pKeyContainer
;
4128 PROV_ENUMALGS provEnumalgs
;
4132 /* This is for dwParam PP_CRYPT_COUNT_KEY_USE.
4133 * IE6 SP1 asks for it in the 'About' dialog.
4134 * Returning this BLOB seems to satisfy IE. The marked 0x00 seem
4135 * to be 'don't care's. If you know anything more specific about
4136 * this provider parameter, please contact the Wine developers */
4137 static const BYTE abWTF
[96] = {
4138 0xb0, 0x25, 0x63, 0x86, 0x9c, 0xab, 0xb6, 0x37,
4139 0xe8, 0x82, /**/0x00,/**/ 0x72, 0x06, 0xb2, /**/0x00,/**/ 0x3b,
4140 0x60, 0x35, /**/0x00,/**/ 0x3b, 0x88, 0xce, /**/0x00,/**/ 0x82,
4141 0xbc, 0x7a, /**/0x00,/**/ 0xb7, 0x4f, 0x7e, /**/0x00,/**/ 0xde,
4142 0x92, 0xf1, /**/0x00,/**/ 0x83, 0xea, 0x5e, /**/0x00,/**/ 0xc8,
4143 0x12, 0x1e, 0xd4, 0x06, 0xf7, 0x66, /**/0x00,/**/ 0x01,
4144 0x29, 0xa4, /**/0x00,/**/ 0xf8, 0x24, 0x0c, /**/0x00,/**/ 0x33,
4145 0x06, 0x80, /**/0x00,/**/ 0x02, 0x46, 0x0b, /**/0x00,/**/ 0x6d,
4146 0x5b, 0xca, /**/0x00,/**/ 0x9a, 0x10, 0xf0, /**/0x00,/**/ 0x05,
4147 0x19, 0xd0, /**/0x00,/**/ 0x2c, 0xf6, 0x27, /**/0x00,/**/ 0xaa,
4148 0x7c, 0x6f, /**/0x00,/**/ 0xb9, 0xd8, 0x72, /**/0x00,/**/ 0x03,
4149 0xf3, 0x81, /**/0x00,/**/ 0xfa, 0xe8, 0x26, /**/0x00,/**/ 0xca
4152 TRACE("(hProv=%08Ix, dwParam=%08lx, pbData=%p, pdwDataLen=%p, dwFlags=%08lx)\n",
4153 hProv
, dwParam
, pbData
, pdwDataLen
, dwFlags
);
4156 SetLastError(ERROR_INVALID_PARAMETER
);
4160 if (!(pKeyContainer
= get_key_container(hProv
)))
4162 /* MSDN: hProv not containing valid context handle */
4169 case PP_UNIQUE_CONTAINER
:/* MSDN says we can return the same value as PP_CONTAINER */
4170 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szName
,
4171 strlen(pKeyContainer
->szName
)+1);
4174 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)pKeyContainer
->szProvName
,
4175 strlen(pKeyContainer
->szProvName
)+1);
4178 dwTemp
= PROV_RSA_FULL
;
4179 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4182 dwTemp
= AT_SIGNATURE
| AT_KEYEXCHANGE
;
4183 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4185 case PP_KEYSET_TYPE
:
4186 dwTemp
= pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
;
4187 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4190 dwTemp
= CRYPT_SEC_DESCR
;
4191 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4193 case PP_SIG_KEYSIZE_INC
:
4194 case PP_KEYX_KEYSIZE_INC
:
4196 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4199 dwTemp
= CRYPT_IMPL_SOFTWARE
;
4200 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4203 dwTemp
= 0x00000200;
4204 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&dwTemp
, sizeof(dwTemp
));
4206 case PP_ENUMCONTAINERS
:
4207 if ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) pKeyContainer
->dwEnumContainersCtr
= 0;
4210 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
4214 if (!open_container_key("", dwFlags
, KEY_READ
, &hKey
))
4216 SetLastError(ERROR_NO_MORE_ITEMS
);
4220 dwTemp
= *pdwDataLen
;
4221 switch (RegEnumKeyExA(hKey
, pKeyContainer
->dwEnumContainersCtr
, (LPSTR
)pbData
, &dwTemp
,
4222 NULL
, NULL
, NULL
, NULL
))
4224 case ERROR_MORE_DATA
:
4225 *pdwDataLen
= (DWORD
)MAX_PATH
+ 1;
4228 pKeyContainer
->dwEnumContainersCtr
++;
4232 case ERROR_NO_MORE_ITEMS
:
4234 SetLastError(ERROR_NO_MORE_ITEMS
);
4240 case PP_ENUMALGS_EX
:
4241 if (((pKeyContainer
->dwEnumAlgsCtr
>= RSAENH_MAX_ENUMALGS
-1) ||
4242 (!aProvEnumAlgsEx
[pKeyContainer
->dwPersonality
]
4243 [pKeyContainer
->dwEnumAlgsCtr
+1].aiAlgid
)) &&
4244 ((dwFlags
& CRYPT_FIRST
) != CRYPT_FIRST
))
4246 SetLastError(ERROR_NO_MORE_ITEMS
);
4250 if (dwParam
== PP_ENUMALGS
) {
4251 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS
)))
4252 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
4253 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
4255 provEnumalgs
.aiAlgid
= aProvEnumAlgsEx
4256 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].aiAlgid
;
4257 provEnumalgs
.dwBitLen
= aProvEnumAlgsEx
4258 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwDefaultLen
;
4259 provEnumalgs
.dwNameLen
= aProvEnumAlgsEx
4260 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].dwNameLen
;
4261 memcpy(provEnumalgs
.szName
, aProvEnumAlgsEx
4262 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
].szName
,
4265 return copy_param(pbData
, pdwDataLen
, (const BYTE
*)&provEnumalgs
,
4266 sizeof(PROV_ENUMALGS
));
4268 if (pbData
&& (*pdwDataLen
>= sizeof(PROV_ENUMALGS_EX
)))
4269 pKeyContainer
->dwEnumAlgsCtr
= ((dwFlags
& CRYPT_FIRST
) == CRYPT_FIRST
) ?
4270 0 : pKeyContainer
->dwEnumAlgsCtr
+1;
4272 return copy_param(pbData
, pdwDataLen
,
4273 (const BYTE
*)&aProvEnumAlgsEx
4274 [pKeyContainer
->dwPersonality
][pKeyContainer
->dwEnumAlgsCtr
],
4275 sizeof(PROV_ENUMALGS_EX
));
4278 case PP_CRYPT_COUNT_KEY_USE
: /* Asked for by IE About dialog */
4279 return copy_param(pbData
, pdwDataLen
, abWTF
, sizeof(abWTF
));
4281 case PP_KEYSET_SEC_DESCR
:
4283 SECURITY_DESCRIPTOR
*sd
;
4284 DWORD err
, len
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
4286 if (!open_container_key(pKeyContainer
->szName
, flags
, KEY_READ
, &hKey
))
4288 SetLastError(NTE_BAD_KEYSET
);
4292 err
= GetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, NULL
, NULL
, NULL
, NULL
, (void **)&sd
);
4300 len
= GetSecurityDescriptorLength(sd
);
4301 if (*pdwDataLen
>= len
) memcpy(pbData
, sd
, len
);
4302 else SetLastError(ERROR_INSUFFICIENT_BUFFER
);
4310 /* MSDN: Unknown parameter number in dwParam */
4311 SetLastError(NTE_BAD_TYPE
);
4316 /******************************************************************************
4317 * CPDeriveKey (RSAENH.@)
4319 * Derives a key from a hash value.
4322 * hProv [I] Key container for which a key is to be generated.
4323 * Algid [I] Crypto algorithm identifier for the key to be generated.
4324 * hBaseData [I] Hash from whose value the key will be derived.
4325 * dwFlags [I] See Notes.
4326 * phKey [O] The generated key.
4334 * - CRYPT_EXPORTABLE: Key can be exported.
4335 * - CRYPT_NO_SALT: No salt is used for 40 bit keys.
4336 * - CRYPT_CREATE_SALT: Use remaining bits as salt value.
4338 BOOL WINAPI
RSAENH_CPDeriveKey(HCRYPTPROV hProv
, ALG_ID Algid
, HCRYPTHASH hBaseData
,
4339 DWORD dwFlags
, HCRYPTKEY
*phKey
)
4341 CRYPTKEY
*pCryptKey
, *pMasterKey
;
4342 CRYPTHASH
*pCryptHash
;
4343 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
*2];
4346 TRACE("(hProv=%08Ix, Algid=%d, hBaseData=%08Ix, dwFlags=%08lx phKey=%p)\n", hProv
, Algid
,
4347 hBaseData
, dwFlags
, phKey
);
4349 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4351 SetLastError(NTE_BAD_UID
);
4355 if (!lookup_handle(&handle_table
, hBaseData
, RSAENH_MAGIC_HASH
,
4356 (OBJECTHDR
**)&pCryptHash
))
4358 SetLastError(NTE_BAD_HASH
);
4364 SetLastError(ERROR_INVALID_PARAMETER
);
4368 switch (GET_ALG_CLASS(Algid
))
4370 case ALG_CLASS_DATA_ENCRYPT
:
4372 int need_padding
, copy_len
;
4373 *phKey
= new_key(hProv
, Algid
, dwFlags
, &pCryptKey
);
4374 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4377 * We derive the key material from the hash.
4378 * If the hash value is not large enough for the claimed key, we have to construct
4379 * a larger binary value based on the hash. This is documented in MSDN: CryptDeriveKey.
4381 dwLen
= RSAENH_MAX_HASH_SIZE
;
4382 RSAENH_CPGetHashParam(pCryptHash
->hProv
, hBaseData
, HP_HASHVAL
, abHashValue
, &dwLen
, 0);
4385 * The usage of padding seems to vary from algorithm to algorithm.
4386 * For now the only different case found was for AES with 128 bit key.
4391 /* To reduce the chance of regressions we will only deviate
4392 * from the old behavior for the tested hash lengths */
4393 if (dwLen
== 16 || dwLen
== 20)
4399 need_padding
= dwLen
< pCryptKey
->dwKeyLen
;
4402 copy_len
= pCryptKey
->dwKeyLen
;
4405 BYTE pad1
[RSAENH_HMAC_DEF_PAD_LEN
], pad2
[RSAENH_HMAC_DEF_PAD_LEN
];
4406 BYTE old_hashval
[RSAENH_MAX_HASH_SIZE
];
4409 memcpy(old_hashval
, pCryptHash
->abHashValue
, RSAENH_MAX_HASH_SIZE
);
4411 for (i
=0; i
<RSAENH_HMAC_DEF_PAD_LEN
; i
++) {
4412 pad1
[i
] = RSAENH_HMAC_DEF_IPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
4413 pad2
[i
] = RSAENH_HMAC_DEF_OPAD_CHAR
^ (i
<dwLen
? abHashValue
[i
] : 0);
4416 init_hash(pCryptHash
);
4417 update_hash(pCryptHash
, pad1
, RSAENH_HMAC_DEF_PAD_LEN
);
4418 finalize_hash(pCryptHash
);
4419 memcpy(abHashValue
, pCryptHash
->abHashValue
, pCryptHash
->dwHashSize
);
4421 init_hash(pCryptHash
);
4422 update_hash(pCryptHash
, pad2
, RSAENH_HMAC_DEF_PAD_LEN
);
4423 finalize_hash(pCryptHash
);
4424 memcpy(abHashValue
+pCryptHash
->dwHashSize
, pCryptHash
->abHashValue
,
4425 pCryptHash
->dwHashSize
);
4427 memcpy(pCryptHash
->abHashValue
, old_hashval
, RSAENH_MAX_HASH_SIZE
);
4430 * Padding was not required, we have more hash than needed.
4431 * Do we need to use the remaining hash as salt?
4433 else if((dwFlags
& CRYPT_CREATE_SALT
) &&
4434 (Algid
== CALG_RC2
|| Algid
== CALG_RC4
))
4436 copy_len
+= pCryptKey
->dwSaltLen
;
4439 memcpy(pCryptKey
->abKeyValue
, abHashValue
,
4440 RSAENH_MIN(copy_len
, sizeof(pCryptKey
->abKeyValue
)));
4443 case ALG_CLASS_MSG_ENCRYPT
:
4444 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4445 (OBJECTHDR
**)&pMasterKey
))
4447 SetLastError(NTE_FAIL
); /* FIXME error code */
4453 /* See RFC 2246, chapter 6.3 Key calculation */
4454 case CALG_SCHANNEL_ENC_KEY
:
4455 if (!pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
||
4456 !pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
)
4458 SetLastError(NTE_BAD_FLAGS
);
4461 *phKey
= new_key(hProv
, pMasterKey
->siSChannelInfo
.saEncAlg
.Algid
,
4462 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
),
4464 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4465 memcpy(pCryptKey
->abKeyValue
,
4466 pCryptHash
->abHashValue
+ (
4467 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4468 ((dwFlags
& CRYPT_SERVER
) ?
4469 (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) : 0)),
4470 pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8);
4471 memcpy(pCryptKey
->abInitVector
,
4472 pCryptHash
->abHashValue
+ (
4473 2 * (pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8) +
4474 2 * (pMasterKey
->siSChannelInfo
.saEncAlg
.cBits
/ 8) +
4475 ((dwFlags
& CRYPT_SERVER
) ? pCryptKey
->dwBlockLen
: 0)),
4476 pCryptKey
->dwBlockLen
);
4479 case CALG_SCHANNEL_MAC_KEY
:
4480 *phKey
= new_key(hProv
, Algid
,
4481 MAKELONG(LOWORD(dwFlags
),pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
),
4483 if (*phKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
) return FALSE
;
4484 memcpy(pCryptKey
->abKeyValue
,
4485 pCryptHash
->abHashValue
+ ((dwFlags
& CRYPT_SERVER
) ?
4486 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8 : 0),
4487 pMasterKey
->siSChannelInfo
.saMACAlg
.cBits
/ 8);
4491 SetLastError(NTE_BAD_ALGID
);
4497 SetLastError(NTE_BAD_ALGID
);
4501 setup_key(pCryptKey
);
4505 /******************************************************************************
4506 * CPGetUserKey (RSAENH.@)
4508 * Returns a handle to the user's private key-exchange- or signature-key.
4511 * hProv [I] The key container from which a user key is requested.
4512 * dwKeySpec [I] AT_KEYEXCHANGE or AT_SIGNATURE
4513 * phUserKey [O] Handle to the requested key or INVALID_HANDLE_VALUE in case of failure.
4520 * A newly created key container does not contain private user key. Create them with CPGenKey.
4522 BOOL WINAPI
RSAENH_CPGetUserKey(HCRYPTPROV hProv
, DWORD dwKeySpec
, HCRYPTKEY
*phUserKey
)
4524 KEYCONTAINER
*pKeyContainer
;
4526 TRACE("(hProv=%08Ix, dwKeySpec=%08lx, phUserKey=%p)\n", hProv
, dwKeySpec
, phUserKey
);
4528 if (!(pKeyContainer
= get_key_container(hProv
)))
4530 /* MSDN: hProv not containing valid context handle */
4536 case AT_KEYEXCHANGE
:
4537 copy_handle(&handle_table
, pKeyContainer
->hKeyExchangeKeyPair
, RSAENH_MAGIC_KEY
,
4542 copy_handle(&handle_table
, pKeyContainer
->hSignatureKeyPair
, RSAENH_MAGIC_KEY
,
4547 *phUserKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4550 if (*phUserKey
== (HCRYPTKEY
)INVALID_HANDLE_VALUE
)
4552 /* MSDN: dwKeySpec parameter specifies nonexistent key */
4553 SetLastError(NTE_NO_KEY
);
4560 /******************************************************************************
4561 * CPHashData (RSAENH.@)
4563 * Updates a hash object with the given data.
4566 * hProv [I] Key container to which the hash object belongs.
4567 * hHash [I] Hash object which is to be updated.
4568 * pbData [I] Pointer to data with which the hash object is to be updated.
4569 * dwDataLen [I] Length of the data.
4570 * dwFlags [I] Currently none defined.
4577 * The actual hash value is queried with CPGetHashParam, which will finalize
4578 * the hash. Updating a finalized hash will fail with a last error NTE_BAD_HASH_STATE.
4580 BOOL WINAPI
RSAENH_CPHashData(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbData
,
4581 DWORD dwDataLen
, DWORD dwFlags
)
4583 CRYPTHASH
*pCryptHash
;
4585 TRACE("(hProv=%08Ix, hHash=%08Ix, pbData=%p, dwDataLen=%ld, dwFlags=%08lx)\n",
4586 hProv
, hHash
, pbData
, dwDataLen
, dwFlags
);
4588 if (dwFlags
& ~CRYPT_USERDATA
)
4590 SetLastError(NTE_BAD_FLAGS
);
4594 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4595 (OBJECTHDR
**)&pCryptHash
))
4597 SetLastError(NTE_BAD_HASH
);
4601 if (!get_algid_info(hProv
, pCryptHash
->aiAlgid
) || pCryptHash
->aiAlgid
== CALG_SSL3_SHAMD5
)
4603 SetLastError(NTE_BAD_ALGID
);
4607 if (pCryptHash
->dwState
!= RSAENH_HASHSTATE_HASHING
)
4609 SetLastError(NTE_BAD_HASH_STATE
);
4613 update_hash(pCryptHash
, pbData
, dwDataLen
);
4617 /******************************************************************************
4618 * CPHashSessionKey (RSAENH.@)
4620 * Updates a hash object with the binary representation of a symmetric key.
4623 * hProv [I] Key container to which the hash object belongs.
4624 * hHash [I] Hash object which is to be updated.
4625 * hKey [I] The symmetric key, whose binary value will be added to the hash.
4626 * dwFlags [I] CRYPT_LITTLE_ENDIAN, if the binary key value shall be interpreted as little endian.
4632 BOOL WINAPI
RSAENH_CPHashSessionKey(HCRYPTPROV hProv
, HCRYPTHASH hHash
, HCRYPTKEY hKey
,
4635 BYTE abKeyValue
[RSAENH_MAX_KEY_SIZE
], bTemp
;
4639 TRACE("(hProv=%08Ix, hHash=%08Ix, hKey=%08Ix, dwFlags=%08lx)\n", hProv
, hHash
, hKey
, dwFlags
);
4641 if (!lookup_handle(&handle_table
, hKey
, RSAENH_MAGIC_KEY
, (OBJECTHDR
**)&pKey
) ||
4642 (GET_ALG_CLASS(pKey
->aiAlgid
) != ALG_CLASS_DATA_ENCRYPT
))
4644 SetLastError(NTE_BAD_KEY
);
4648 if (dwFlags
& ~CRYPT_LITTLE_ENDIAN
) {
4649 SetLastError(NTE_BAD_FLAGS
);
4653 memcpy(abKeyValue
, pKey
->abKeyValue
, pKey
->dwKeyLen
);
4654 if (!(dwFlags
& CRYPT_LITTLE_ENDIAN
)) {
4655 for (i
=0; i
<pKey
->dwKeyLen
/2; i
++) {
4656 bTemp
= abKeyValue
[i
];
4657 abKeyValue
[i
] = abKeyValue
[pKey
->dwKeyLen
-i
-1];
4658 abKeyValue
[pKey
->dwKeyLen
-i
-1] = bTemp
;
4662 return RSAENH_CPHashData(hProv
, hHash
, abKeyValue
, pKey
->dwKeyLen
, 0);
4665 /******************************************************************************
4666 * CPReleaseContext (RSAENH.@)
4668 * Release a key container.
4671 * hProv [I] Key container to be released.
4672 * dwFlags [I] Currently none defined.
4678 BOOL WINAPI
RSAENH_CPReleaseContext(HCRYPTPROV hProv
, DWORD dwFlags
)
4680 TRACE("(hProv=%08Ix, dwFlags=%08lx)\n", hProv
, dwFlags
);
4682 if (!release_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4684 /* MSDN: hProv not containing valid context handle */
4685 SetLastError(NTE_BAD_UID
);
4690 SetLastError(NTE_BAD_FLAGS
);
4697 /******************************************************************************
4698 * CPSetHashParam (RSAENH.@)
4700 * Set a parameter of a hash object
4703 * hProv [I] The key container to which the key belongs.
4704 * hHash [I] The hash object for which a parameter is to be set.
4705 * dwParam [I] Parameter type. See Notes.
4706 * pbData [I] Pointer to the parameter value.
4707 * dwFlags [I] Currently none defined.
4714 * Currently only the HP_HMAC_INFO dwParam type is defined.
4715 * The HMAC_INFO struct will be deep copied into the hash object.
4716 * See Internet RFC 2104 for details on the HMAC algorithm.
4718 BOOL WINAPI
RSAENH_CPSetHashParam(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwParam
,
4719 BYTE
*pbData
, DWORD dwFlags
)
4721 CRYPTHASH
*pCryptHash
;
4722 CRYPTKEY
*pCryptKey
;
4725 TRACE("(hProv=%08Ix, hHash=%08Ix, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n",
4726 hProv
, hHash
, dwParam
, pbData
, dwFlags
);
4728 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4730 SetLastError(NTE_BAD_UID
);
4735 SetLastError(NTE_BAD_FLAGS
);
4739 if (!lookup_handle(&handle_table
, hHash
, RSAENH_MAGIC_HASH
,
4740 (OBJECTHDR
**)&pCryptHash
))
4742 SetLastError(NTE_BAD_HASH
);
4748 free_hmac_info(pCryptHash
->pHMACInfo
);
4749 if (!copy_hmac_info(&pCryptHash
->pHMACInfo
, (PHMAC_INFO
)pbData
)) return FALSE
;
4751 if (!lookup_handle(&handle_table
, pCryptHash
->hKey
, RSAENH_MAGIC_KEY
,
4752 (OBJECTHDR
**)&pCryptKey
))
4754 SetLastError(NTE_FAIL
); /* FIXME: correct error code? */
4758 if (pCryptKey
->aiAlgid
== CALG_HMAC
&& !pCryptKey
->dwKeyLen
) {
4759 HCRYPTHASH hKeyHash
;
4762 if (!RSAENH_CPCreateHash(hProv
, ((PHMAC_INFO
)pbData
)->HashAlgid
, 0, 0,
4765 if (!RSAENH_CPHashData(hProv
, hKeyHash
, pCryptKey
->blobHmacKey
.pbData
,
4766 pCryptKey
->blobHmacKey
.cbData
, 0))
4768 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4771 keyLen
= sizeof(pCryptKey
->abKeyValue
);
4772 if (!RSAENH_CPGetHashParam(hProv
, hKeyHash
, HP_HASHVAL
, pCryptKey
->abKeyValue
,
4775 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4778 pCryptKey
->dwKeyLen
= keyLen
;
4779 RSAENH_CPDestroyHash(hProv
, hKeyHash
);
4781 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbInnerString
); i
++) {
4782 pCryptHash
->pHMACInfo
->pbInnerString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4784 for (i
=0; i
<RSAENH_MIN(pCryptKey
->dwKeyLen
,pCryptHash
->pHMACInfo
->cbOuterString
); i
++) {
4785 pCryptHash
->pHMACInfo
->pbOuterString
[i
] ^= pCryptKey
->abKeyValue
[i
];
4788 init_hash(pCryptHash
);
4792 memcpy(pCryptHash
->abHashValue
, pbData
, pCryptHash
->dwHashSize
);
4793 pCryptHash
->dwState
= RSAENH_HASHSTATE_FINISHED
;
4796 case HP_TLS1PRF_SEED
:
4797 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobSeed
, (PCRYPT_DATA_BLOB
)pbData
);
4799 case HP_TLS1PRF_LABEL
:
4800 return copy_data_blob(&pCryptHash
->tpPRFParams
.blobLabel
, (PCRYPT_DATA_BLOB
)pbData
);
4803 SetLastError(NTE_BAD_TYPE
);
4808 /******************************************************************************
4809 * CPSetProvParam (RSAENH.@)
4811 BOOL WINAPI
RSAENH_CPSetProvParam(HCRYPTPROV hProv
, DWORD dwParam
, BYTE
*pbData
, DWORD dwFlags
)
4813 KEYCONTAINER
*pKeyContainer
;
4816 TRACE("(hProv=%08Ix, dwParam=%08lx, pbData=%p, dwFlags=%08lx)\n", hProv
, dwParam
, pbData
, dwFlags
);
4818 if (!(pKeyContainer
= get_key_container(hProv
)))
4823 case PP_KEYSET_SEC_DESCR
:
4825 SECURITY_DESCRIPTOR
*sd
= (SECURITY_DESCRIPTOR
*)pbData
;
4826 DWORD err
, flags
= (pKeyContainer
->dwFlags
& CRYPT_MACHINE_KEYSET
);
4828 REGSAM access
= WRITE_DAC
| WRITE_OWNER
| ACCESS_SYSTEM_SECURITY
;
4829 PSID owner
= NULL
, group
= NULL
;
4830 PACL dacl
= NULL
, sacl
= NULL
;
4832 if (!open_container_key(pKeyContainer
->szName
, flags
, access
, &hKey
))
4834 SetLastError(NTE_BAD_KEYSET
);
4838 if ((dwFlags
& OWNER_SECURITY_INFORMATION
&& !GetSecurityDescriptorOwner(sd
, &owner
, &def
)) ||
4839 (dwFlags
& GROUP_SECURITY_INFORMATION
&& !GetSecurityDescriptorGroup(sd
, &group
, &def
)) ||
4840 (dwFlags
& DACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorDacl(sd
, &present
, &dacl
, &def
)) ||
4841 (dwFlags
& SACL_SECURITY_INFORMATION
&& !GetSecurityDescriptorSacl(sd
, &present
, &sacl
, &def
)))
4847 err
= SetSecurityInfo(hKey
, SE_REGISTRY_KEY
, dwFlags
, owner
, group
, dacl
, sacl
);
4857 FIXME("unimplemented parameter %08lx\n", dwParam
);
4862 /******************************************************************************
4863 * CPSignHash (RSAENH.@)
4865 * Sign a hash object
4868 * hProv [I] The key container, to which the hash object belongs.
4869 * hHash [I] The hash object to be signed.
4870 * dwKeySpec [I] AT_SIGNATURE or AT_KEYEXCHANGE: Key used to generate the signature.
4871 * sDescription [I] Should be NULL for security reasons.
4872 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4873 * pbSignature [O] Buffer, to which the signature will be stored. May be NULL to query SigLen.
4874 * pdwSigLen [I/O] Size of the buffer (in), Length of the signature (out)
4880 BOOL WINAPI
RSAENH_CPSignHash(HCRYPTPROV hProv
, HCRYPTHASH hHash
, DWORD dwKeySpec
,
4881 LPCWSTR sDescription
, DWORD dwFlags
, BYTE
*pbSignature
,
4884 HCRYPTKEY hCryptKey
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
4885 CRYPTKEY
*pCryptKey
;
4887 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4891 TRACE("(hProv=%08Ix, hHash=%08Ix, dwKeySpec=%08lx, sDescription=%s, dwFlags=%08lx, "
4892 "pbSignature=%p, pdwSigLen=%p)\n", hProv
, hHash
, dwKeySpec
, debugstr_w(sDescription
),
4893 dwFlags
, pbSignature
, pdwSigLen
);
4895 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4896 SetLastError(NTE_BAD_FLAGS
);
4900 if (!RSAENH_CPGetUserKey(hProv
, dwKeySpec
, &hCryptKey
)) return FALSE
;
4902 if (!lookup_handle(&handle_table
, hCryptKey
, RSAENH_MAGIC_KEY
,
4903 (OBJECTHDR
**)&pCryptKey
))
4905 SetLastError(NTE_NO_KEY
);
4910 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4914 if (pCryptKey
->dwKeyLen
> *pdwSigLen
)
4916 SetLastError(ERROR_MORE_DATA
);
4917 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4920 *pdwSigLen
= pCryptKey
->dwKeyLen
;
4923 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
4924 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
4930 dwHashLen
= sizeof(DWORD
);
4931 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) goto out
;
4933 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
4934 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) goto out
;
4937 if (!build_hash_signature(pbSignature
, *pdwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
)) {
4941 ret
= encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PRIVATE
, &pCryptKey
->context
, pbSignature
, pbSignature
, RSAENH_ENCRYPT
);
4943 RSAENH_CPDestroyKey(hProv
, hCryptKey
);
4947 /******************************************************************************
4948 * CPVerifySignature (RSAENH.@)
4950 * Verify the signature of a hash object.
4953 * hProv [I] The key container, to which the hash belongs.
4954 * hHash [I] The hash for which the signature is verified.
4955 * pbSignature [I] The binary signature.
4956 * dwSigLen [I] Length of the signature BLOB.
4957 * hPubKey [I] Public key used to verify the signature.
4958 * sDescription [I] Should be NULL for security reasons.
4959 * dwFlags [I] 0, CRYPT_NOHASHOID or CRYPT_X931_FORMAT: Format of the signature.
4962 * Success: TRUE (Signature is valid)
4963 * Failure: FALSE (GetLastError() == NTE_BAD_SIGNATURE, if signature is invalid)
4965 BOOL WINAPI
RSAENH_CPVerifySignature(HCRYPTPROV hProv
, HCRYPTHASH hHash
, const BYTE
*pbSignature
,
4966 DWORD dwSigLen
, HCRYPTKEY hPubKey
, LPCWSTR sDescription
,
4969 BYTE
*pbConstructed
= NULL
, *pbDecrypted
= NULL
;
4970 CRYPTKEY
*pCryptKey
;
4973 BYTE abHashValue
[RSAENH_MAX_HASH_SIZE
];
4976 TRACE("(hProv=%08Ix, hHash=%08Ix, pbSignature=%p, dwSigLen=%ld, hPubKey=%08Ix, sDescription=%s, "
4977 "dwFlags=%08lx)\n", hProv
, hHash
, pbSignature
, dwSigLen
, hPubKey
, debugstr_w(sDescription
),
4980 if (dwFlags
& ~(CRYPT_NOHASHOID
|CRYPT_X931_FORMAT
)) {
4981 SetLastError(NTE_BAD_FLAGS
);
4985 if (!is_valid_handle(&handle_table
, hProv
, RSAENH_MAGIC_CONTAINER
))
4987 SetLastError(NTE_BAD_UID
);
4991 if (!lookup_handle(&handle_table
, hPubKey
, RSAENH_MAGIC_KEY
,
4992 (OBJECTHDR
**)&pCryptKey
))
4994 SetLastError(NTE_BAD_KEY
);
4998 /* in Microsoft implementation, the signature length is checked before
4999 * the signature pointer.
5001 if (dwSigLen
!= pCryptKey
->dwKeyLen
)
5003 SetLastError(NTE_BAD_SIGNATURE
);
5007 if (!hHash
|| !pbSignature
)
5009 SetLastError(ERROR_INVALID_PARAMETER
);
5014 if (!RSAENH_CPHashData(hProv
, hHash
, (const BYTE
*)sDescription
,
5015 (DWORD
)lstrlenW(sDescription
)*sizeof(WCHAR
), 0))
5021 dwHashLen
= sizeof(DWORD
);
5022 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_ALGID
, (BYTE
*)&aiAlgid
, &dwHashLen
, 0)) return FALSE
;
5024 dwHashLen
= RSAENH_MAX_HASH_SIZE
;
5025 if (!RSAENH_CPGetHashParam(hProv
, hHash
, HP_HASHVAL
, abHashValue
, &dwHashLen
, 0)) return FALSE
;
5027 pbConstructed
= malloc(dwSigLen
);
5028 if (!pbConstructed
) {
5029 SetLastError(NTE_NO_MEMORY
);
5033 pbDecrypted
= malloc(dwSigLen
);
5035 SetLastError(NTE_NO_MEMORY
);
5039 if (!encrypt_block_impl(pCryptKey
->aiAlgid
, PK_PUBLIC
, &pCryptKey
->context
, pbSignature
, pbDecrypted
,
5045 if (build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
) &&
5046 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
5051 if (!(dwFlags
& CRYPT_NOHASHOID
) &&
5052 build_hash_signature(pbConstructed
, dwSigLen
, aiAlgid
, abHashValue
, dwHashLen
, dwFlags
|CRYPT_NOHASHOID
) &&
5053 !memcmp(pbDecrypted
, pbConstructed
, dwSigLen
)) {
5058 SetLastError(NTE_BAD_SIGNATURE
);
5061 free(pbConstructed
);