2 * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 # define _WIN32_WINNT 0x0400
15 # include <wincrypt.h>
22 # define alloca _alloca
25 # include <openssl/crypto.h>
27 # ifndef OPENSSL_NO_CAPIENG
29 # include <openssl/buffer.h>
30 # include <openssl/bn.h>
31 # include <openssl/rsa.h>
32 # include <openssl/dsa.h>
35 DEFINE_STACK_OF(X509_NAME
)
38 * This module uses several "new" interfaces, among which is
39 * CertGetCertificateContextProperty. CERT_KEY_PROV_INFO_PROP_ID is
40 * one of possible values you can pass to function in question. By
41 * checking if it's defined we can see if wincrypt.h and accompanying
42 * crypt32.lib are in shape. The native MingW32 headers up to and
43 * including __W32API_VERSION 3.14 lack of struct DSSPUBKEY and the
44 * defines CERT_STORE_PROV_SYSTEM_A and CERT_STORE_READONLY_FLAG,
45 * so we check for these too and avoid compiling.
46 * Yes, it's rather "weak" test and if compilation fails,
47 * then re-configure with -DOPENSSL_NO_CAPIENG.
49 # if defined(CERT_KEY_PROV_INFO_PROP_ID) && \
50 defined(CERT_STORE_PROV_SYSTEM_A) && \
51 defined(CERT_STORE_READONLY_FLAG)
52 # define __COMPILE_CAPIENG
53 # endif /* CERT_KEY_PROV_INFO_PROP_ID */
54 # endif /* OPENSSL_NO_CAPIENG */
57 #ifdef __COMPILE_CAPIENG
59 # undef X509_EXTENSIONS
61 /* Definitions which may be missing from earlier version of headers */
62 # ifndef CERT_STORE_OPEN_EXISTING_FLAG
63 # define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
66 # ifndef CERT_STORE_CREATE_NEW_FLAG
67 # define CERT_STORE_CREATE_NEW_FLAG 0x00002000
70 # ifndef CERT_SYSTEM_STORE_CURRENT_USER
71 # define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000
74 # ifndef ALG_SID_SHA_256
75 # define ALG_SID_SHA_256 12
77 # ifndef ALG_SID_SHA_384
78 # define ALG_SID_SHA_384 13
80 # ifndef ALG_SID_SHA_512
81 # define ALG_SID_SHA_512 14
85 # define CALG_SHA_256 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256)
88 # define CALG_SHA_384 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_384)
91 # define CALG_SHA_512 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_512)
95 # define PROV_RSA_AES 24
98 # include <openssl/engine.h>
99 # include <openssl/pem.h>
100 # include <openssl/x509v3.h>
102 # include "e_capi_err.h"
103 # include "e_capi_err.c"
105 static const char *engine_capi_id
= "capi";
106 static const char *engine_capi_name
= "CryptoAPI ENGINE";
108 typedef struct CAPI_CTX_st CAPI_CTX
;
109 typedef struct CAPI_KEY_st CAPI_KEY
;
111 static void capi_addlasterror(void);
112 static void capi_adderror(DWORD err
);
114 static void CAPI_trace(CAPI_CTX
*ctx
, char *format
, ...);
116 static int capi_list_providers(CAPI_CTX
*ctx
, BIO
*out
);
117 static int capi_list_containers(CAPI_CTX
*ctx
, BIO
*out
);
118 int capi_list_certs(CAPI_CTX
*ctx
, BIO
*out
, char *storename
);
119 void capi_free_key(CAPI_KEY
*key
);
121 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
*ctx
, const char *id
,
124 CAPI_KEY
*capi_find_key(CAPI_CTX
*ctx
, const char *id
);
126 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
127 UI_METHOD
*ui_method
, void *callback_data
);
128 static int capi_rsa_sign(int dtype
, const unsigned char *m
,
129 unsigned int m_len
, unsigned char *sigret
,
130 unsigned int *siglen
, const RSA
*rsa
);
131 static int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
132 unsigned char *to
, RSA
*rsa
, int padding
);
133 static int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
134 unsigned char *to
, RSA
*rsa
, int padding
);
135 static int capi_rsa_free(RSA
*rsa
);
137 # ifndef OPENSSL_NO_DSA
138 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
140 static int capi_dsa_free(DSA
*dsa
);
143 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
144 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
,
145 EVP_PKEY
**pkey
, STACK_OF(X509
) **pother
,
146 UI_METHOD
*ui_method
,
147 void *callback_data
);
149 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
150 # ifdef OPENSSL_CAPIENG_DIALOG
151 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
154 void engine_load_capi_int(void);
156 typedef PCCERT_CONTEXT(WINAPI
*CERTDLG
)(HCERTSTORE
, HWND
, LPCWSTR
,
157 LPCWSTR
, DWORD
, DWORD
, void *);
158 typedef HWND(WINAPI
*GETCONSWIN
)(void);
161 * This structure contains CAPI ENGINE specific data: it contains various
162 * global options and affects how other functions behave.
165 # define CAPI_DBG_TRACE 2
166 # define CAPI_DBG_ERROR 1
171 /* Parameters to use for container lookup */
175 /* Certificate store name to use */
177 LPSTR ssl_client_store
;
178 /* System store flags */
180 /* Lookup string meanings in load_private_key */
181 # define CAPI_LU_SUBSTR 1 /* Substring of subject: uses "storename" */
182 # define CAPI_LU_FNAME 2 /* Friendly name: uses storename */
183 # define CAPI_LU_CONTNAME 3 /* Container name: uses cspname, keytype */
185 /* Info to dump with dumpcerts option */
186 # define CAPI_DMP_SUMMARY 0x1 /* Issuer and serial name strings */
187 # define CAPI_DMP_FNAME 0x2 /* Friendly name */
188 # define CAPI_DMP_FULL 0x4 /* Full X509_print dump */
189 # define CAPI_DMP_PEM 0x8 /* Dump PEM format certificate */
190 # define CAPI_DMP_PSKEY 0x10 /* Dump pseudo key (if possible) */
191 # define CAPI_DMP_PKEYINFO 0x20 /* Dump key info (if possible) */
193 int (*client_cert_select
) (ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
194 CERTDLG certselectdlg
;
195 GETCONSWIN getconswindow
;
198 static CAPI_CTX
*capi_ctx_new(void);
199 static void capi_ctx_free(CAPI_CTX
*ctx
);
200 static int capi_ctx_set_provname(CAPI_CTX
*ctx
, LPSTR pname
, DWORD type
,
202 static int capi_ctx_set_provname_idx(CAPI_CTX
*ctx
, int idx
);
204 # define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
205 # define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
206 # define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
207 # define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
208 # define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
209 # define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
210 # define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
211 # define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
212 # define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
213 # define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
214 # define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
215 # define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
216 # define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
217 # define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13)
219 static const ENGINE_CMD_DEFN capi_cmd_defns
[] = {
220 {CAPI_CMD_LIST_CERTS
,
222 "List all certificates in store",
223 ENGINE_CMD_FLAG_NO_INPUT
},
224 {CAPI_CMD_LOOKUP_CERT
,
226 "Lookup and output certificates",
227 ENGINE_CMD_FLAG_STRING
},
228 {CAPI_CMD_DEBUG_LEVEL
,
230 "debug level (1=errors, 2=trace)",
231 ENGINE_CMD_FLAG_NUMERIC
},
232 {CAPI_CMD_DEBUG_FILE
,
234 "debugging filename)",
235 ENGINE_CMD_FLAG_STRING
},
238 "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
239 ENGINE_CMD_FLAG_NUMERIC
},
243 ENGINE_CMD_FLAG_NO_INPUT
},
244 {CAPI_CMD_SET_CSP_IDX
,
247 ENGINE_CMD_FLAG_NUMERIC
},
248 {CAPI_CMD_SET_CSP_NAME
,
250 "Set CSP name, (default CSP used if not specified)",
251 ENGINE_CMD_FLAG_STRING
},
252 {CAPI_CMD_SET_CSP_TYPE
,
254 "Set CSP type, (default RSA_PROV_FULL)",
255 ENGINE_CMD_FLAG_NUMERIC
},
256 {CAPI_CMD_LIST_CONTAINERS
,
258 "list container names",
259 ENGINE_CMD_FLAG_NO_INPUT
},
260 {CAPI_CMD_LIST_OPTIONS
,
262 "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
263 "32=private key info)",
264 ENGINE_CMD_FLAG_NUMERIC
},
265 {CAPI_CMD_LOOKUP_METHOD
,
267 "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
268 ENGINE_CMD_FLAG_NUMERIC
},
269 {CAPI_CMD_STORE_NAME
,
271 "certificate store name, default \"MY\"",
272 ENGINE_CMD_FLAG_STRING
},
273 {CAPI_CMD_STORE_FLAGS
,
275 "Certificate store flags: 1 = system store",
276 ENGINE_CMD_FLAG_NUMERIC
},
281 static int capi_idx
= -1;
282 static int rsa_capi_idx
= -1;
283 static int dsa_capi_idx
= -1;
284 static int cert_capi_idx
= -1;
286 static int capi_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void))
292 if (capi_idx
== -1) {
293 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_ENGINE_NOT_INITIALIZED
);
296 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
297 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
299 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_FILE_OPEN_ERROR
);
303 case CAPI_CMD_LIST_CSPS
:
304 ret
= capi_list_providers(ctx
, out
);
307 case CAPI_CMD_LIST_CERTS
:
308 ret
= capi_list_certs(ctx
, out
, NULL
);
311 case CAPI_CMD_LOOKUP_CERT
:
312 ret
= capi_list_certs(ctx
, out
, p
);
315 case CAPI_CMD_LIST_CONTAINERS
:
316 ret
= capi_list_containers(ctx
, out
);
319 case CAPI_CMD_STORE_NAME
:
320 tmpstr
= OPENSSL_strdup(p
);
321 if (tmpstr
!= NULL
) {
322 OPENSSL_free(ctx
->storename
);
323 ctx
->storename
= tmpstr
;
324 CAPI_trace(ctx
, "Setting store name to %s\n", p
);
326 CAPIerr(CAPI_F_CAPI_CTRL
, ERR_R_MALLOC_FAILURE
);
331 case CAPI_CMD_STORE_FLAGS
:
333 ctx
->store_flags
|= CERT_SYSTEM_STORE_LOCAL_MACHINE
;
334 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_CURRENT_USER
;
336 ctx
->store_flags
|= CERT_SYSTEM_STORE_CURRENT_USER
;
337 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_LOCAL_MACHINE
;
339 CAPI_trace(ctx
, "Setting flags to %d\n", i
);
342 case CAPI_CMD_DEBUG_LEVEL
:
343 ctx
->debug_level
= (int)i
;
344 CAPI_trace(ctx
, "Setting debug level to %d\n", ctx
->debug_level
);
347 case CAPI_CMD_DEBUG_FILE
:
348 tmpstr
= OPENSSL_strdup(p
);
349 if (tmpstr
!= NULL
) {
350 ctx
->debug_file
= tmpstr
;
351 CAPI_trace(ctx
, "Setting debug file to %s\n", ctx
->debug_file
);
353 CAPIerr(CAPI_F_CAPI_CTRL
, ERR_R_MALLOC_FAILURE
);
358 case CAPI_CMD_KEYTYPE
:
360 CAPI_trace(ctx
, "Setting key type to %d\n", ctx
->keytype
);
363 case CAPI_CMD_SET_CSP_IDX
:
364 ret
= capi_ctx_set_provname_idx(ctx
, i
);
367 case CAPI_CMD_LIST_OPTIONS
:
371 case CAPI_CMD_LOOKUP_METHOD
:
372 if (i
< 1 || i
> 3) {
373 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_INVALID_LOOKUP_METHOD
);
377 ctx
->lookup_method
= i
;
380 case CAPI_CMD_SET_CSP_NAME
:
381 ret
= capi_ctx_set_provname(ctx
, p
, ctx
->csptype
, 1);
384 case CAPI_CMD_SET_CSP_TYPE
:
389 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_UNKNOWN_COMMAND
);
398 static RSA_METHOD
*capi_rsa_method
= NULL
;
399 # ifndef OPENSSL_NO_DSA
400 static DSA_METHOD
*capi_dsa_method
= NULL
;
403 static int use_aes_csp
= 0;
404 static const WCHAR rsa_aes_cspname
[] =
405 L
"Microsoft Enhanced RSA and AES Cryptographic Provider";
406 static const WCHAR rsa_enh_cspname
[] =
407 L
"Microsoft Enhanced Cryptographic Provider v1.0";
409 static int capi_init(ENGINE
*e
)
412 const RSA_METHOD
*ossl_rsa_meth
;
413 # ifndef OPENSSL_NO_DSA
414 const DSA_METHOD
*ossl_dsa_meth
;
419 capi_idx
= ENGINE_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
423 cert_capi_idx
= X509_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
425 /* Setup RSA_METHOD */
426 rsa_capi_idx
= RSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
427 ossl_rsa_meth
= RSA_PKCS1_OpenSSL();
428 if ( !RSA_meth_set_pub_enc(capi_rsa_method
,
429 RSA_meth_get_pub_enc(ossl_rsa_meth
))
430 || !RSA_meth_set_pub_dec(capi_rsa_method
,
431 RSA_meth_get_pub_dec(ossl_rsa_meth
))
432 || !RSA_meth_set_priv_enc(capi_rsa_method
, capi_rsa_priv_enc
)
433 || !RSA_meth_set_priv_dec(capi_rsa_method
, capi_rsa_priv_dec
)
434 || !RSA_meth_set_mod_exp(capi_rsa_method
,
435 RSA_meth_get_mod_exp(ossl_rsa_meth
))
436 || !RSA_meth_set_bn_mod_exp(capi_rsa_method
,
437 RSA_meth_get_bn_mod_exp(ossl_rsa_meth
))
438 || !RSA_meth_set_finish(capi_rsa_method
, capi_rsa_free
)
439 || !RSA_meth_set_sign(capi_rsa_method
, capi_rsa_sign
)) {
443 # ifndef OPENSSL_NO_DSA
444 /* Setup DSA Method */
445 dsa_capi_idx
= DSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
446 ossl_dsa_meth
= DSA_OpenSSL();
447 if ( !DSA_meth_set_sign(capi_dsa_method
, capi_dsa_do_sign
)
448 || !DSA_meth_set_verify(capi_dsa_method
,
449 DSA_meth_get_verify(ossl_dsa_meth
))
450 || !DSA_meth_set_finish(capi_dsa_method
, capi_dsa_free
)
451 || !DSA_meth_set_mod_exp(capi_dsa_method
,
452 DSA_meth_get_mod_exp(ossl_dsa_meth
))
453 || !DSA_meth_set_bn_mod_exp(capi_dsa_method
,
454 DSA_meth_get_bn_mod_exp(ossl_dsa_meth
))) {
460 ctx
= capi_ctx_new();
464 ENGINE_set_ex_data(e
, capi_idx
, ctx
);
466 # ifdef OPENSSL_CAPIENG_DIALOG
468 HMODULE cryptui
= LoadLibrary(TEXT("CRYPTUI.DLL"));
469 HMODULE kernel
= GetModuleHandle(TEXT("KERNEL32.DLL"));
472 (CERTDLG
) GetProcAddress(cryptui
,
473 "CryptUIDlgSelectCertificateFromStore");
476 (GETCONSWIN
) GetProcAddress(kernel
, "GetConsoleWindow");
477 if (cryptui
&& !OPENSSL_isservice())
478 ctx
->client_cert_select
= cert_select_dialog
;
482 /* See if there is RSA+AES CSP */
483 if (CryptAcquireContextW(&hprov
, NULL
, rsa_aes_cspname
, PROV_RSA_AES
,
484 CRYPT_VERIFYCONTEXT
)) {
486 CryptReleaseContext(hprov
, 0);
492 CAPIerr(CAPI_F_CAPI_INIT
, ERR_R_MALLOC_FAILURE
);
498 static int capi_destroy(ENGINE
*e
)
500 RSA_meth_free(capi_rsa_method
);
501 capi_rsa_method
= NULL
;
502 # ifndef OPENSSL_NO_DSA
503 DSA_meth_free(capi_dsa_method
);
504 capi_dsa_method
= NULL
;
506 ERR_unload_CAPI_strings();
510 static int capi_finish(ENGINE
*e
)
513 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
515 ENGINE_set_ex_data(e
, capi_idx
, NULL
);
520 * CryptoAPI key application data. This contains a handle to the private key
521 * container (for sign operations) and a handle to the key (for decrypt
526 /* Associated certificate context (if any) */
527 PCCERT_CONTEXT pcert
;
533 static int bind_capi(ENGINE
*e
)
535 capi_rsa_method
= RSA_meth_new("CryptoAPI RSA method", 0);
536 if (capi_rsa_method
== NULL
)
538 # ifndef OPENSSL_NO_DSA
539 capi_dsa_method
= DSA_meth_new("CryptoAPI DSA method", 0);
540 if (capi_dsa_method
== NULL
)
543 if (!ENGINE_set_id(e
, engine_capi_id
)
544 || !ENGINE_set_name(e
, engine_capi_name
)
545 || !ENGINE_set_flags(e
, ENGINE_FLAGS_NO_REGISTER_ALL
)
546 || !ENGINE_set_init_function(e
, capi_init
)
547 || !ENGINE_set_finish_function(e
, capi_finish
)
548 || !ENGINE_set_destroy_function(e
, capi_destroy
)
549 || !ENGINE_set_RSA(e
, capi_rsa_method
)
550 # ifndef OPENSSL_NO_DSA
551 || !ENGINE_set_DSA(e
, capi_dsa_method
)
553 || !ENGINE_set_load_privkey_function(e
, capi_load_privkey
)
554 || !ENGINE_set_load_ssl_client_cert_function(e
,
555 capi_load_ssl_client_cert
)
556 || !ENGINE_set_cmd_defns(e
, capi_cmd_defns
)
557 || !ENGINE_set_ctrl_function(e
, capi_ctrl
))
559 ERR_load_CAPI_strings();
563 RSA_meth_free(capi_rsa_method
);
564 capi_rsa_method
= NULL
;
565 # ifndef OPENSSL_NO_DSA
566 DSA_meth_free(capi_dsa_method
);
567 capi_dsa_method
= NULL
;
572 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
573 static int bind_helper(ENGINE
*e
, const char *id
)
575 if (id
&& (strcmp(id
, engine_capi_id
) != 0))
582 IMPLEMENT_DYNAMIC_CHECK_FN()
583 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
585 static ENGINE
*engine_capi(void)
587 ENGINE
*ret
= ENGINE_new();
590 if (!bind_capi(ret
)) {
597 void engine_load_capi_int(void)
599 /* Copied from eng_[openssl|dyn].c */
600 ENGINE
*toadd
= engine_capi();
609 static int lend_tobn(BIGNUM
*bn
, unsigned char *bin
, int binlen
)
613 * Reverse buffer in place: since this is a keyblob structure that will
614 * be freed up after conversion anyway it doesn't matter if we change
617 for (i
= 0; i
< binlen
/ 2; i
++) {
620 bin
[i
] = bin
[binlen
- i
- 1];
621 bin
[binlen
- i
- 1] = c
;
624 if (!BN_bin2bn(bin
, binlen
, bn
))
629 /* Given a CAPI_KEY get an EVP_PKEY structure */
631 static EVP_PKEY
*capi_get_pkey(ENGINE
*eng
, CAPI_KEY
*key
)
633 unsigned char *pubkey
= NULL
;
638 EVP_PKEY
*ret
= NULL
;
639 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, NULL
, &len
)) {
640 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR
);
645 pubkey
= OPENSSL_malloc(len
);
650 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, pubkey
, &len
)) {
651 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_ERROR
);
656 bh
= (BLOBHEADER
*) pubkey
;
657 if (bh
->bType
!= PUBLICKEYBLOB
) {
658 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_INVALID_PUBLIC_KEY_BLOB
);
661 if (bh
->aiKeyAlg
== CALG_RSA_SIGN
|| bh
->aiKeyAlg
== CALG_RSA_KEYX
) {
664 BIGNUM
*e
= NULL
, *n
= NULL
;
665 unsigned char *rsa_modulus
;
666 rp
= (RSAPUBKEY
*) (bh
+ 1);
667 if (rp
->magic
!= 0x31415352) {
669 BIO_snprintf(magstr
, 10, "%lx", rp
->magic
);
670 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
671 CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
672 ERR_add_error_data(2, "magic=0x", magstr
);
675 rsa_modulus
= (unsigned char *)(rp
+ 1);
676 rkey
= RSA_new_method(eng
);
683 if (e
== NULL
|| n
== NULL
) {
689 RSA_set0_key(rkey
, n
, e
, NULL
);
691 if (!BN_set_word(e
, rp
->pubexp
))
694 rsa_modlen
= rp
->bitlen
/ 8;
695 if (!lend_tobn(n
, rsa_modulus
, rsa_modlen
))
698 RSA_set_ex_data(rkey
, rsa_capi_idx
, key
);
700 if ((ret
= EVP_PKEY_new()) == NULL
)
703 EVP_PKEY_assign_RSA(ret
, rkey
);
706 # ifndef OPENSSL_NO_DSA
707 } else if (bh
->aiKeyAlg
== CALG_DSS_SIGN
) {
711 BIGNUM
*p
, *q
, *g
, *pub_key
;
712 dp
= (DSSPUBKEY
*) (bh
+ 1);
713 if (dp
->magic
!= 0x31535344) {
715 BIO_snprintf(magstr
, 10, "%lx", dp
->magic
);
716 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
717 CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
718 ERR_add_error_data(2, "magic=0x", magstr
);
721 dsa_plen
= dp
->bitlen
/ 8;
722 btmp
= (unsigned char *)(dp
+ 1);
723 dkey
= DSA_new_method(eng
);
730 if (p
== NULL
|| q
== NULL
|| g
== NULL
|| pub_key
== NULL
) {
737 DSA_set0_pqg(dkey
, p
, q
, g
);
738 DSA_set0_key(dkey
, pub_key
, NULL
);
739 if (!lend_tobn(p
, btmp
, dsa_plen
))
742 if (!lend_tobn(q
, btmp
, 20))
745 if (!lend_tobn(g
, btmp
, dsa_plen
))
748 if (!lend_tobn(pub_key
, btmp
, dsa_plen
))
752 DSA_set_ex_data(dkey
, dsa_capi_idx
, key
);
754 if ((ret
= EVP_PKEY_new()) == NULL
)
757 EVP_PKEY_assign_DSA(ret
, dkey
);
762 BIO_snprintf(algstr
, 10, "%ux", bh
->aiKeyAlg
);
763 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
764 CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM
);
765 ERR_add_error_data(2, "aiKeyAlg=0x", algstr
);
770 OPENSSL_free(pubkey
);
773 # ifndef OPENSSL_NO_DSA
781 CAPIerr(CAPI_F_CAPI_GET_PKEY
, ERR_R_MALLOC_FAILURE
);
786 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
787 UI_METHOD
*ui_method
, void *callback_data
)
792 ctx
= ENGINE_get_ex_data(eng
, capi_idx
);
795 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY
, CAPI_R_CANT_FIND_CAPI_CONTEXT
);
799 key
= capi_find_key(ctx
, key_id
);
804 ret
= capi_get_pkey(eng
, key
);
812 /* CryptoAPI RSA operations */
814 int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
815 unsigned char *to
, RSA
*rsa
, int padding
)
817 CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC
, CAPI_R_FUNCTION_NOT_SUPPORTED
);
821 int capi_rsa_sign(int dtype
, const unsigned char *m
, unsigned int m_len
,
822 unsigned char *sigret
, unsigned int *siglen
, const RSA
*rsa
)
832 ctx
= ENGINE_get_ex_data(RSA_get0_engine(rsa
), capi_idx
);
834 CAPI_trace(ctx
, "Called CAPI_rsa_sign()\n");
836 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
838 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_GET_KEY
);
841 /* Convert the signature type to a CryptoAPI algorithm ID */
864 alg
= CALG_SSL3_SHAMD5
;
869 BIO_snprintf(algstr
, 10, "%x", dtype
);
870 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_UNSUPPORTED_ALGORITHM_NID
);
871 ERR_add_error_data(2, "NID=0x", algstr
);
876 /* Create the hash object */
877 if (!CryptCreateHash(capi_key
->hprov
, alg
, 0, 0, &hash
)) {
878 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
882 /* Set the hash value to the value passed */
884 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)m
, 0)) {
885 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
890 /* Finally sign it */
891 slen
= RSA_size(rsa
);
892 if (!CryptSignHash(hash
, capi_key
->keyspec
, NULL
, 0, sigret
, &slen
)) {
893 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
898 /* Inplace byte reversal of signature */
899 for (i
= 0; i
< slen
/ 2; i
++) {
902 sigret
[i
] = sigret
[slen
- i
- 1];
903 sigret
[slen
- i
- 1] = c
;
911 CryptDestroyHash(hash
);
916 int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
917 unsigned char *to
, RSA
*rsa
, int padding
)
920 unsigned char *tmpbuf
;
929 ctx
= ENGINE_get_ex_data(RSA_get0_engine(rsa
), capi_idx
);
931 CAPI_trace(ctx
, "Called capi_rsa_priv_dec()\n");
933 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
935 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_CANT_GET_KEY
);
940 case RSA_PKCS1_PADDING
:
943 #ifdef CRYPT_DECRYPT_RSA_NO_PADDING_CHECK
945 flags
= CRYPT_DECRYPT_RSA_NO_PADDING_CHECK
;
951 BIO_snprintf(errstr
, 10, "%d", padding
);
952 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_UNSUPPORTED_PADDING
);
953 ERR_add_error_data(2, "padding=", errstr
);
958 /* Create temp reverse order version of input */
959 if ((tmpbuf
= OPENSSL_malloc(flen
)) == NULL
) {
960 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, ERR_R_MALLOC_FAILURE
);
963 for (i
= 0; i
< flen
; i
++)
964 tmpbuf
[flen
- i
- 1] = from
[i
];
966 /* Finally decrypt it */
968 if (!CryptDecrypt(capi_key
->key
, 0, TRUE
, flags
, tmpbuf
, &dlen
)) {
969 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_DECRYPT_ERROR
);
971 OPENSSL_cleanse(tmpbuf
, dlen
);
972 OPENSSL_free(tmpbuf
);
975 memcpy(to
, tmpbuf
, (flen
= (int)dlen
));
977 OPENSSL_cleanse(tmpbuf
, flen
);
978 OPENSSL_free(tmpbuf
);
983 static int capi_rsa_free(RSA
*rsa
)
986 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
987 capi_free_key(capi_key
);
988 RSA_set_ex_data(rsa
, rsa_capi_idx
, 0);
992 # ifndef OPENSSL_NO_DSA
993 /* CryptoAPI DSA operations */
995 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
1000 DSA_SIG
*ret
= NULL
;
1003 unsigned char csigbuf
[40];
1005 ctx
= ENGINE_get_ex_data(DSA_get0_engine(dsa
), capi_idx
);
1007 CAPI_trace(ctx
, "Called CAPI_dsa_do_sign()\n");
1009 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
1012 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_GET_KEY
);
1017 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_INVALID_DIGEST_LENGTH
);
1021 /* Create the hash object */
1022 if (!CryptCreateHash(capi_key
->hprov
, CALG_SHA1
, 0, 0, &hash
)) {
1023 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
1024 capi_addlasterror();
1028 /* Set the hash value to the value passed */
1029 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)digest
, 0)) {
1030 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
1031 capi_addlasterror();
1035 /* Finally sign it */
1036 slen
= sizeof(csigbuf
);
1037 if (!CryptSignHash(hash
, capi_key
->keyspec
, NULL
, 0, csigbuf
, &slen
)) {
1038 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
1039 capi_addlasterror();
1042 BIGNUM
*r
= BN_new(), *s
= BN_new();
1044 if (r
== NULL
|| s
== NULL
1045 || !lend_tobn(r
, csigbuf
, 20)
1046 || !lend_tobn(s
, csigbuf
+ 20, 20)
1047 || (ret
= DSA_SIG_new()) == NULL
) {
1048 BN_free(r
); /* BN_free checks for BIGNUM * being NULL */
1052 DSA_SIG_set0(ret
, r
, s
);
1058 OPENSSL_cleanse(csigbuf
, 40);
1059 CryptDestroyHash(hash
);
1063 static int capi_dsa_free(DSA
*dsa
)
1066 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
1067 capi_free_key(capi_key
);
1068 DSA_set_ex_data(dsa
, dsa_capi_idx
, 0);
1073 static void capi_vtrace(CAPI_CTX
*ctx
, int level
, char *format
,
1078 if (!ctx
|| (ctx
->debug_level
< level
) || (!ctx
->debug_file
))
1080 out
= BIO_new_file(ctx
->debug_file
, "a+");
1082 CAPIerr(CAPI_F_CAPI_VTRACE
, CAPI_R_FILE_OPEN_ERROR
);
1085 BIO_vprintf(out
, format
, argptr
);
1089 static void CAPI_trace(CAPI_CTX
*ctx
, char *format
, ...)
1092 va_start(args
, format
);
1093 capi_vtrace(ctx
, CAPI_DBG_TRACE
, format
, args
);
1097 static void capi_addlasterror(void)
1099 capi_adderror(GetLastError());
1102 static void capi_adderror(DWORD err
)
1105 BIO_snprintf(errstr
, 10, "%lX", err
);
1106 ERR_add_error_data(2, "Error code= 0x", errstr
);
1109 static char *wide_to_asc(LPCWSTR wstr
)
1116 len_0
= (int)wcslen(wstr
) + 1; /* WideCharToMultiByte expects int */
1117 sz
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len_0
, NULL
, 0, NULL
, NULL
);
1119 CAPIerr(CAPI_F_WIDE_TO_ASC
, CAPI_R_WIN32_ERROR
);
1122 str
= OPENSSL_malloc(sz
);
1124 CAPIerr(CAPI_F_WIDE_TO_ASC
, ERR_R_MALLOC_FAILURE
);
1127 if (!WideCharToMultiByte(CP_ACP
, 0, wstr
, len_0
, str
, sz
, NULL
, NULL
)) {
1129 CAPIerr(CAPI_F_WIDE_TO_ASC
, CAPI_R_WIN32_ERROR
);
1135 static int capi_get_provname(CAPI_CTX
*ctx
, LPSTR
*pname
, DWORD
*ptype
,
1140 CAPI_trace(ctx
, "capi_get_provname, index=%d\n", idx
);
1141 if (!CryptEnumProviders(idx
, NULL
, 0, ptype
, NULL
, &len
)) {
1142 err
= GetLastError();
1143 if (err
== ERROR_NO_MORE_ITEMS
)
1145 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1149 name
= OPENSSL_malloc(len
);
1151 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, ERR_R_MALLOC_FAILURE
);
1154 if (!CryptEnumProviders(idx
, NULL
, 0, ptype
, name
, &len
)) {
1155 err
= GetLastError();
1157 if (err
== ERROR_NO_MORE_ITEMS
)
1159 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1163 if (sizeof(TCHAR
) != sizeof(char)) {
1164 *pname
= wide_to_asc((WCHAR
*)name
);
1169 *pname
= (char *)name
;
1171 CAPI_trace(ctx
, "capi_get_provname, returned name=%s, type=%d\n", *pname
,
1177 static int capi_list_providers(CAPI_CTX
*ctx
, BIO
*out
)
1181 LPSTR provname
= NULL
;
1182 CAPI_trace(ctx
, "capi_list_providers\n");
1183 BIO_printf(out
, "Available CSPs:\n");
1184 for (idx
= 0;; idx
++) {
1185 ret
= capi_get_provname(ctx
, &provname
, &ptype
, idx
);
1190 BIO_printf(out
, "%lu. %s, type %lu\n", idx
, provname
, ptype
);
1191 OPENSSL_free(provname
);
1196 static int capi_list_containers(CAPI_CTX
*ctx
, BIO
*out
)
1200 DWORD err
, idx
, flags
, buflen
= 0, clen
;
1202 LPWSTR cspname
= NULL
;
1204 CAPI_trace(ctx
, "Listing containers CSP=%s, type = %d\n", ctx
->cspname
,
1206 if (ctx
->cspname
!= NULL
) {
1207 if ((clen
= MultiByteToWideChar(CP_ACP
, 0, ctx
->cspname
, -1,
1209 cspname
= alloca(clen
* sizeof(WCHAR
));
1210 MultiByteToWideChar(CP_ACP
, 0, ctx
->cspname
, -1, (WCHAR
*)cspname
,
1213 if (cspname
== NULL
) {
1214 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, ERR_R_MALLOC_FAILURE
);
1215 capi_addlasterror();
1219 if (!CryptAcquireContextW(&hprov
, NULL
, cspname
, ctx
->csptype
,
1220 CRYPT_VERIFYCONTEXT
)) {
1221 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
,
1222 CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1223 capi_addlasterror();
1226 if (!CryptGetProvParam(hprov
, PP_ENUMCONTAINERS
, NULL
, &buflen
,
1228 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1229 capi_addlasterror();
1230 CryptReleaseContext(hprov
, 0);
1233 CAPI_trace(ctx
, "Got max container len %d\n", buflen
);
1236 cname
= OPENSSL_malloc(buflen
);
1237 if (cname
== NULL
) {
1238 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, ERR_R_MALLOC_FAILURE
);
1242 for (idx
= 0;; idx
++) {
1247 flags
= CRYPT_FIRST
;
1250 if (!CryptGetProvParam(hprov
, PP_ENUMCONTAINERS
, (BYTE
*)cname
,
1252 err
= GetLastError();
1253 if (err
== ERROR_NO_MORE_ITEMS
)
1255 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1259 CAPI_trace(ctx
, "Container name %s, len=%d, index=%d, flags=%d\n",
1260 cname
, clen
, idx
, flags
);
1261 if (!cname
[0] && (clen
== buflen
)) {
1262 CAPI_trace(ctx
, "Enumerate bug: using workaround\n");
1265 BIO_printf(out
, "%lu. %s\n", idx
, cname
);
1272 OPENSSL_free(cname
);
1273 CryptReleaseContext(hprov
, 0);
1278 static CRYPT_KEY_PROV_INFO
*capi_get_prov_info(CAPI_CTX
*ctx
,
1279 PCCERT_CONTEXT cert
)
1282 CRYPT_KEY_PROV_INFO
*pinfo
;
1284 if (!CertGetCertificateContextProperty(cert
, CERT_KEY_PROV_INFO_PROP_ID
,
1287 pinfo
= OPENSSL_malloc(len
);
1288 if (pinfo
== NULL
) {
1289 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
, ERR_R_MALLOC_FAILURE
);
1292 if (!CertGetCertificateContextProperty(cert
, CERT_KEY_PROV_INFO_PROP_ID
,
1294 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
,
1295 CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO
);
1296 capi_addlasterror();
1297 OPENSSL_free(pinfo
);
1303 static void capi_dump_prov_info(CAPI_CTX
*ctx
, BIO
*out
,
1304 CRYPT_KEY_PROV_INFO
*pinfo
)
1306 char *provname
= NULL
, *contname
= NULL
;
1308 if (pinfo
== NULL
) {
1309 BIO_printf(out
, " No Private Key\n");
1312 provname
= wide_to_asc(pinfo
->pwszProvName
);
1313 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1314 if (provname
== NULL
|| contname
== NULL
)
1317 BIO_printf(out
, " Private Key Info:\n");
1318 BIO_printf(out
, " Provider Name: %s, Provider Type %lu\n", provname
,
1320 BIO_printf(out
, " Container Name: %s, Key Type %lu\n", contname
,
1323 OPENSSL_free(provname
);
1324 OPENSSL_free(contname
);
1327 static char *capi_cert_get_fname(CAPI_CTX
*ctx
, PCCERT_CONTEXT cert
)
1332 CAPI_trace(ctx
, "capi_cert_get_fname\n");
1333 if (!CertGetCertificateContextProperty(cert
, CERT_FRIENDLY_NAME_PROP_ID
,
1336 wfname
= OPENSSL_malloc(dlen
);
1339 if (CertGetCertificateContextProperty(cert
, CERT_FRIENDLY_NAME_PROP_ID
,
1341 char *fname
= wide_to_asc(wfname
);
1342 OPENSSL_free(wfname
);
1345 CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME
, CAPI_R_ERROR_GETTING_FRIENDLY_NAME
);
1346 capi_addlasterror();
1348 OPENSSL_free(wfname
);
1352 static void capi_dump_cert(CAPI_CTX
*ctx
, BIO
*out
, PCCERT_CONTEXT cert
)
1355 const unsigned char *p
;
1356 unsigned long flags
= ctx
->dump_flags
;
1357 if (flags
& CAPI_DMP_FNAME
) {
1359 fname
= capi_cert_get_fname(ctx
, cert
);
1361 BIO_printf(out
, " Friendly Name \"%s\"\n", fname
);
1362 OPENSSL_free(fname
);
1364 BIO_printf(out
, " <No Friendly Name>\n");
1368 p
= cert
->pbCertEncoded
;
1369 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1371 BIO_printf(out
, " <Can't parse certificate>\n");
1372 if (flags
& CAPI_DMP_SUMMARY
) {
1373 BIO_printf(out
, " Subject: ");
1374 X509_NAME_print_ex(out
, X509_get_subject_name(x
), 0, XN_FLAG_ONELINE
);
1375 BIO_printf(out
, "\n Issuer: ");
1376 X509_NAME_print_ex(out
, X509_get_issuer_name(x
), 0, XN_FLAG_ONELINE
);
1377 BIO_printf(out
, "\n");
1379 if (flags
& CAPI_DMP_FULL
)
1380 X509_print_ex(out
, x
, XN_FLAG_ONELINE
, 0);
1382 if (flags
& CAPI_DMP_PKEYINFO
) {
1383 CRYPT_KEY_PROV_INFO
*pinfo
;
1384 pinfo
= capi_get_prov_info(ctx
, cert
);
1385 capi_dump_prov_info(ctx
, out
, pinfo
);
1386 OPENSSL_free(pinfo
);
1389 if (flags
& CAPI_DMP_PEM
)
1390 PEM_write_bio_X509(out
, x
);
1394 static HCERTSTORE
capi_open_store(CAPI_CTX
*ctx
, char *storename
)
1399 storename
= ctx
->storename
;
1402 CAPI_trace(ctx
, "Opening certificate store %s\n", storename
);
1404 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_A
, 0, 0,
1405 ctx
->store_flags
, storename
);
1407 CAPIerr(CAPI_F_CAPI_OPEN_STORE
, CAPI_R_ERROR_OPENING_STORE
);
1408 capi_addlasterror();
1413 int capi_list_certs(CAPI_CTX
*ctx
, BIO
*out
, char *id
)
1419 PCCERT_CONTEXT cert
= NULL
;
1421 storename
= ctx
->storename
;
1424 CAPI_trace(ctx
, "Listing certs for store %s\n", storename
);
1426 hstore
= capi_open_store(ctx
, storename
);
1430 cert
= capi_find_cert(ctx
, id
, hstore
);
1435 capi_dump_cert(ctx
, out
, cert
);
1436 CertFreeCertificateContext(cert
);
1438 for (idx
= 0;; idx
++) {
1439 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1442 BIO_printf(out
, "Certificate %d\n", idx
);
1443 capi_dump_cert(ctx
, out
, cert
);
1447 CertCloseStore(hstore
, 0);
1451 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
*ctx
, const char *id
,
1454 PCCERT_CONTEXT cert
= NULL
;
1457 switch (ctx
->lookup_method
) {
1458 case CAPI_LU_SUBSTR
:
1459 return CertFindCertificateInStore(hstore
, X509_ASN_ENCODING
, 0,
1460 CERT_FIND_SUBJECT_STR_A
, id
, NULL
);
1463 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1466 fname
= capi_cert_get_fname(ctx
, cert
);
1468 if (strcmp(fname
, id
))
1472 OPENSSL_free(fname
);
1482 static CAPI_KEY
*capi_get_key(CAPI_CTX
*ctx
, const WCHAR
*contname
,
1483 const WCHAR
*provname
, DWORD ptype
,
1487 CAPI_KEY
*key
= OPENSSL_malloc(sizeof(*key
));
1491 /* If PROV_RSA_AES supported use it instead */
1492 if (ptype
== PROV_RSA_FULL
&& use_aes_csp
&&
1493 wcscmp(provname
, rsa_enh_cspname
) == 0) {
1494 provname
= rsa_aes_cspname
;
1495 ptype
= PROV_RSA_AES
;
1497 if (ctx
&& ctx
->debug_level
>= CAPI_DBG_TRACE
&& ctx
->debug_file
) {
1499 * above 'if' is [complementary] copy from CAPI_trace and serves
1500 * as optimization to minimize [below] malloc-ations
1502 char *_contname
= wide_to_asc(contname
);
1503 char *_provname
= wide_to_asc(provname
);
1505 CAPI_trace(ctx
, "capi_get_key, contname=%s, provname=%s, type=%d\n",
1506 _contname
, _provname
, ptype
);
1507 OPENSSL_free(_provname
);
1508 OPENSSL_free(_contname
);
1510 if (ctx
->store_flags
& CERT_SYSTEM_STORE_LOCAL_MACHINE
)
1511 dwFlags
= CRYPT_MACHINE_KEYSET
;
1512 if (!CryptAcquireContextW(&key
->hprov
, contname
, provname
, ptype
,
1514 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1515 capi_addlasterror();
1518 if (!CryptGetUserKey(key
->hprov
, keyspec
, &key
->key
)) {
1519 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_GETUSERKEY_ERROR
);
1520 capi_addlasterror();
1521 CryptReleaseContext(key
->hprov
, 0);
1524 key
->keyspec
= keyspec
;
1533 static CAPI_KEY
*capi_get_cert_key(CAPI_CTX
*ctx
, PCCERT_CONTEXT cert
)
1535 CAPI_KEY
*key
= NULL
;
1536 CRYPT_KEY_PROV_INFO
*pinfo
= NULL
;
1538 pinfo
= capi_get_prov_info(ctx
, cert
);
1541 key
= capi_get_key(ctx
, pinfo
->pwszContainerName
, pinfo
->pwszProvName
,
1542 pinfo
->dwProvType
, pinfo
->dwKeySpec
);
1544 OPENSSL_free(pinfo
);
1548 CAPI_KEY
*capi_find_key(CAPI_CTX
*ctx
, const char *id
)
1550 PCCERT_CONTEXT cert
;
1552 CAPI_KEY
*key
= NULL
;
1554 switch (ctx
->lookup_method
) {
1555 case CAPI_LU_SUBSTR
:
1557 hstore
= capi_open_store(ctx
, NULL
);
1560 cert
= capi_find_cert(ctx
, id
, hstore
);
1562 key
= capi_get_cert_key(ctx
, cert
);
1563 CertFreeCertificateContext(cert
);
1565 CertCloseStore(hstore
, 0);
1568 case CAPI_LU_CONTNAME
:
1570 WCHAR
*contname
, *provname
;
1573 if ((len
= MultiByteToWideChar(CP_ACP
, 0, id
, -1, NULL
, 0)) &&
1574 (contname
= alloca(len
* sizeof(WCHAR
)),
1575 MultiByteToWideChar(CP_ACP
, 0, id
, -1, contname
, len
)) &&
1576 (len
= MultiByteToWideChar(CP_ACP
, 0, ctx
->cspname
, -1,
1578 (provname
= alloca(len
* sizeof(WCHAR
)),
1579 MultiByteToWideChar(CP_ACP
, 0, ctx
->cspname
, -1,
1581 key
= capi_get_key(ctx
, contname
, provname
,
1582 ctx
->csptype
, ctx
->keytype
);
1590 void capi_free_key(CAPI_KEY
*key
)
1594 CryptDestroyKey(key
->key
);
1595 CryptReleaseContext(key
->hprov
, 0);
1597 CertFreeCertificateContext(key
->pcert
);
1601 /* Initialize a CAPI_CTX structure */
1603 static CAPI_CTX
*capi_ctx_new(void)
1605 CAPI_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
1608 CAPIerr(CAPI_F_CAPI_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
1611 ctx
->csptype
= PROV_RSA_FULL
;
1612 ctx
->dump_flags
= CAPI_DMP_SUMMARY
| CAPI_DMP_FNAME
;
1613 ctx
->keytype
= AT_KEYEXCHANGE
;
1614 ctx
->store_flags
= CERT_STORE_OPEN_EXISTING_FLAG
|
1615 CERT_STORE_READONLY_FLAG
| CERT_SYSTEM_STORE_CURRENT_USER
;
1616 ctx
->lookup_method
= CAPI_LU_SUBSTR
;
1617 ctx
->client_cert_select
= cert_select_simple
;
1621 static void capi_ctx_free(CAPI_CTX
*ctx
)
1623 CAPI_trace(ctx
, "Calling capi_ctx_free with %lx\n", ctx
);
1626 OPENSSL_free(ctx
->cspname
);
1627 OPENSSL_free(ctx
->debug_file
);
1628 OPENSSL_free(ctx
->storename
);
1629 OPENSSL_free(ctx
->ssl_client_store
);
1633 static int capi_ctx_set_provname(CAPI_CTX
*ctx
, LPSTR pname
, DWORD type
,
1638 CAPI_trace(ctx
, "capi_ctx_set_provname, name=%s, type=%d\n", pname
, type
);
1644 if ((len
= MultiByteToWideChar(CP_ACP
, 0, pname
, -1, NULL
, 0))) {
1645 name
= alloca(len
* sizeof(WCHAR
));
1646 MultiByteToWideChar(CP_ACP
, 0, pname
, -1, (WCHAR
*)name
, len
);
1648 if (name
== NULL
|| !CryptAcquireContextW(&hprov
, NULL
, name
, type
,
1649 CRYPT_VERIFYCONTEXT
)) {
1650 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME
,
1651 CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1652 capi_addlasterror();
1655 CryptReleaseContext(hprov
, 0);
1657 tmpcspname
= OPENSSL_strdup(pname
);
1658 if (tmpcspname
== NULL
) {
1659 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME
, ERR_R_MALLOC_FAILURE
);
1662 OPENSSL_free(ctx
->cspname
);
1663 ctx
->cspname
= tmpcspname
;
1664 ctx
->csptype
= type
;
1668 static int capi_ctx_set_provname_idx(CAPI_CTX
*ctx
, int idx
)
1673 if (capi_get_provname(ctx
, &pname
, &type
, idx
) != 1)
1675 res
= capi_ctx_set_provname(ctx
, pname
, type
, 0);
1676 OPENSSL_free(pname
);
1680 static int cert_issuer_match(STACK_OF(X509_NAME
) *ca_dn
, X509
*x
)
1684 /* Special case: empty list: match anything */
1685 if (sk_X509_NAME_num(ca_dn
) <= 0)
1687 for (i
= 0; i
< sk_X509_NAME_num(ca_dn
); i
++) {
1688 nm
= sk_X509_NAME_value(ca_dn
, i
);
1689 if (!X509_NAME_cmp(nm
, X509_get_issuer_name(x
)))
1695 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
1696 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
,
1697 EVP_PKEY
**pkey
, STACK_OF(X509
) **pother
,
1698 UI_METHOD
*ui_method
,
1699 void *callback_data
)
1701 STACK_OF(X509
) *certs
= NULL
;
1704 const unsigned char *p
;
1705 int i
, client_cert_idx
;
1707 PCCERT_CONTEXT cert
= NULL
, excert
= NULL
;
1710 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1715 storename
= ctx
->ssl_client_store
;
1719 hstore
= capi_open_store(ctx
, storename
);
1722 /* Enumerate all certificates collect any matches */
1724 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1727 p
= cert
->pbCertEncoded
;
1728 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1730 CAPI_trace(ctx
, "Can't Parse Certificate %d\n", i
);
1733 if (cert_issuer_match(ca_dn
, x
)
1734 && X509_check_purpose(x
, X509_PURPOSE_SSL_CLIENT
, 0)) {
1735 key
= capi_get_cert_key(ctx
, cert
);
1741 * Match found: attach extra data to it so we can retrieve the
1744 excert
= CertDuplicateCertificateContext(cert
);
1745 key
->pcert
= excert
;
1746 X509_set_ex_data(x
, cert_capi_idx
, key
);
1749 certs
= sk_X509_new_null();
1751 sk_X509_push(certs
, x
);
1758 CertFreeCertificateContext(cert
);
1760 CertCloseStore(hstore
, 0);
1765 /* Select the appropriate certificate */
1767 client_cert_idx
= ctx
->client_cert_select(e
, ssl
, certs
);
1769 /* Set the selected certificate and free the rest */
1771 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1772 x
= sk_X509_value(certs
, i
);
1773 if (i
== client_cert_idx
)
1776 key
= X509_get_ex_data(x
, cert_capi_idx
);
1782 sk_X509_free(certs
);
1787 /* Setup key for selected certificate */
1789 key
= X509_get_ex_data(*pcert
, cert_capi_idx
);
1790 *pkey
= capi_get_pkey(e
, key
);
1791 X509_set_ex_data(*pcert
, cert_capi_idx
, NULL
);
1797 /* Simple client cert selection function: always select first */
1799 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1804 # ifdef OPENSSL_CAPIENG_DIALOG
1807 * More complex cert selection function, using standard function
1808 * CryptUIDlgSelectCertificateFromStore() to produce a dialog box.
1812 * Definitions which are in cryptuiapi.h but this is not present in older
1813 * versions of headers.
1816 # ifndef CRYPTUI_SELECT_LOCATION_COLUMN
1817 # define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010
1818 # define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004
1821 # define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
1822 # define dlg_prompt L"Select a certificate to use for authentication"
1823 # define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
1824 |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
1826 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1830 PCCERT_CONTEXT cert
;
1835 if (sk_X509_num(certs
) == 1)
1837 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1838 /* Create an in memory store of certificates */
1839 dstore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1840 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1842 CAPIerr(CAPI_F_CERT_SELECT_DIALOG
, CAPI_R_ERROR_CREATING_STORE
);
1843 capi_addlasterror();
1846 /* Add all certificates to store */
1847 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1848 x
= sk_X509_value(certs
, i
);
1849 key
= X509_get_ex_data(x
, cert_capi_idx
);
1851 if (!CertAddCertificateContextToStore(dstore
, key
->pcert
,
1852 CERT_STORE_ADD_NEW
, NULL
)) {
1853 CAPIerr(CAPI_F_CERT_SELECT_DIALOG
, CAPI_R_ERROR_ADDING_CERT
);
1854 capi_addlasterror();
1859 hwnd
= GetForegroundWindow();
1861 hwnd
= GetActiveWindow();
1862 if (!hwnd
&& ctx
->getconswindow
)
1863 hwnd
= ctx
->getconswindow();
1864 /* Call dialog to select one */
1865 cert
= ctx
->certselectdlg(dstore
, hwnd
, dlg_title
, dlg_prompt
,
1866 dlg_columns
, 0, NULL
);
1868 /* Find matching cert from list */
1870 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1871 x
= sk_X509_value(certs
, i
);
1872 key
= X509_get_ex_data(x
, cert_capi_idx
);
1873 if (CertCompareCertificate
1874 (X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
, cert
->pCertInfo
,
1875 key
->pcert
->pCertInfo
)) {
1884 CertCloseStore(dstore
, 0);
1890 #else /* !__COMPILE_CAPIENG */
1891 # include <openssl/engine.h>
1892 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1894 int bind_engine(ENGINE
*e
, const char *id
, const dynamic_fns
*fns
);
1896 int bind_engine(ENGINE
*e
, const char *id
, const dynamic_fns
*fns
)
1901 IMPLEMENT_DYNAMIC_CHECK_FN()
1903 void engine_load_capi_int(void);
1904 void engine_load_capi_int(void)