3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
57 #include <openssl/crypto.h>
58 #include <openssl/buffer.h>
59 #include <openssl/bn.h>
61 #ifdef OPENSSL_SYS_WIN32
62 # ifndef OPENSSL_NO_CAPIENG
64 # include <openssl/rsa.h>
69 # define _WIN32_WINNT 0x0400
72 # include <wincrypt.h>
74 # undef X509_EXTENSIONS
75 # undef X509_CERT_PAIR
77 /* Definitions which may be missing from earlier version of headers */
78 # ifndef CERT_STORE_OPEN_EXISTING_FLAG
79 # define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
82 # ifndef CERT_STORE_CREATE_NEW_FLAG
83 # define CERT_STORE_CREATE_NEW_FLAG 0x00002000
86 # ifndef CERT_SYSTEM_STORE_CURRENT_USER
87 # define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000
90 # include <openssl/engine.h>
91 # include <openssl/pem.h>
92 # include <openssl/x509v3.h>
94 # include "e_capi_err.h"
95 # include "e_capi_err.c"
97 static const char *engine_capi_id
= "capi";
98 static const char *engine_capi_name
= "CryptoAPI ENGINE";
100 typedef struct CAPI_CTX_st CAPI_CTX
;
101 typedef struct CAPI_KEY_st CAPI_KEY
;
103 static void capi_addlasterror(void);
104 static void capi_adderror(DWORD err
);
106 static void CAPI_trace(CAPI_CTX
* ctx
, char *format
, ...);
108 static int capi_list_providers(CAPI_CTX
* ctx
, BIO
*out
);
109 static int capi_list_containers(CAPI_CTX
* ctx
, BIO
*out
);
110 int capi_list_certs(CAPI_CTX
* ctx
, BIO
*out
, char *storename
);
111 void capi_free_key(CAPI_KEY
* key
);
113 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
* ctx
, const char *id
,
116 CAPI_KEY
*capi_find_key(CAPI_CTX
* ctx
, const char *id
);
118 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
119 UI_METHOD
*ui_method
, void *callback_data
);
120 static int capi_rsa_sign(int dtype
, const unsigned char *m
,
121 unsigned int m_len
, unsigned char *sigret
,
122 unsigned int *siglen
, const RSA
*rsa
);
123 static int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
124 unsigned char *to
, RSA
*rsa
, int padding
);
125 static int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
126 unsigned char *to
, RSA
*rsa
, int padding
);
127 static int capi_rsa_free(RSA
*rsa
);
129 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
131 static int capi_dsa_free(DSA
*dsa
);
133 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
134 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
,
135 EVP_PKEY
**pkey
, STACK_OF(X509
) **pother
,
136 UI_METHOD
*ui_method
,
137 void *callback_data
);
139 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
140 # ifdef OPENSSL_CAPIENG_DIALOG
141 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
144 typedef PCCERT_CONTEXT(WINAPI
*CERTDLG
) (HCERTSTORE
, HWND
, LPCWSTR
,
145 LPCWSTR
, DWORD
, DWORD
, void *);
146 typedef HWND(WINAPI
*GETCONSWIN
) (void);
149 * This structure contains CAPI ENGINE specific data: it contains various
150 * global options and affects how other functions behave.
153 # define CAPI_DBG_TRACE 2
154 # define CAPI_DBG_ERROR 1
159 /* Parameters to use for container lookup */
163 /* Certificate store name to use */
165 LPSTR ssl_client_store
;
166 /* System store flags */
168 /* Lookup string meanings in load_private_key */
169 /* Substring of subject: uses "storename" */
170 # define CAPI_LU_SUBSTR 1
171 /* Friendly name: uses storename */
172 # define CAPI_LU_FNAME 2
173 /* Container name: uses cspname, keytype */
174 # define CAPI_LU_CONTNAME 3
176 /* Info to dump with dumpcerts option */
177 /* Issuer and serial name strings */
178 # define CAPI_DMP_SUMMARY 0x1
180 # define CAPI_DMP_FNAME 0x2
181 /* Full X509_print dump */
182 # define CAPI_DMP_FULL 0x4
183 /* Dump PEM format certificate */
184 # define CAPI_DMP_PEM 0x8
185 /* Dump pseudo key (if possible) */
186 # define CAPI_DMP_PSKEY 0x10
187 /* Dump key info (if possible) */
188 # define CAPI_DMP_PKEYINFO 0x20
190 int (*client_cert_select
) (ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
191 CERTDLG certselectdlg
;
192 GETCONSWIN getconswindow
;
195 static CAPI_CTX
*capi_ctx_new();
196 static void capi_ctx_free(CAPI_CTX
* ctx
);
197 static int capi_ctx_set_provname(CAPI_CTX
* ctx
, LPSTR pname
, DWORD type
,
199 static int capi_ctx_set_provname_idx(CAPI_CTX
* ctx
, int idx
);
201 # define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
202 # define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
203 # define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
204 # define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
205 # define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
206 # define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
207 # define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
208 # define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
209 # define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
210 # define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
211 # define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
212 # define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
213 # define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
214 # define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13)
216 static const ENGINE_CMD_DEFN capi_cmd_defns
[] = {
217 {CAPI_CMD_LIST_CERTS
,
219 "List all certificates in store",
220 ENGINE_CMD_FLAG_NO_INPUT
},
221 {CAPI_CMD_LOOKUP_CERT
,
223 "Lookup and output certificates",
224 ENGINE_CMD_FLAG_STRING
},
225 {CAPI_CMD_DEBUG_LEVEL
,
227 "debug level (1=errors, 2=trace)",
228 ENGINE_CMD_FLAG_NUMERIC
},
229 {CAPI_CMD_DEBUG_FILE
,
231 "debugging filename)",
232 ENGINE_CMD_FLAG_STRING
},
235 "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
236 ENGINE_CMD_FLAG_NUMERIC
},
240 ENGINE_CMD_FLAG_NO_INPUT
},
241 {CAPI_CMD_SET_CSP_IDX
,
244 ENGINE_CMD_FLAG_NUMERIC
},
245 {CAPI_CMD_SET_CSP_NAME
,
247 "Set CSP name, (default CSP used if not specified)",
248 ENGINE_CMD_FLAG_STRING
},
249 {CAPI_CMD_SET_CSP_TYPE
,
251 "Set CSP type, (default RSA_PROV_FULL)",
252 ENGINE_CMD_FLAG_NUMERIC
},
253 {CAPI_CMD_LIST_CONTAINERS
,
255 "list container names",
256 ENGINE_CMD_FLAG_NO_INPUT
},
257 {CAPI_CMD_LIST_OPTIONS
,
259 "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
260 "32=private key info)",
261 ENGINE_CMD_FLAG_NUMERIC
},
262 {CAPI_CMD_LOOKUP_METHOD
,
264 "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
265 ENGINE_CMD_FLAG_NUMERIC
},
266 {CAPI_CMD_STORE_NAME
,
268 "certificate store name, default \"MY\"",
269 ENGINE_CMD_FLAG_STRING
},
270 {CAPI_CMD_STORE_FLAGS
,
272 "Certificate store flags: 1 = system store",
273 ENGINE_CMD_FLAG_NUMERIC
},
278 static int capi_idx
= -1;
279 static int rsa_capi_idx
= -1;
280 static int dsa_capi_idx
= -1;
281 static int cert_capi_idx
= -1;
283 static int capi_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void))
288 if (capi_idx
== -1) {
289 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_ENGINE_NOT_INITIALIZED
);
292 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
293 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
295 case CAPI_CMD_LIST_CSPS
:
296 ret
= capi_list_providers(ctx
, out
);
299 case CAPI_CMD_LIST_CERTS
:
300 ret
= capi_list_certs(ctx
, out
, NULL
);
303 case CAPI_CMD_LOOKUP_CERT
:
304 ret
= capi_list_certs(ctx
, out
, p
);
307 case CAPI_CMD_LIST_CONTAINERS
:
308 ret
= capi_list_containers(ctx
, out
);
311 case CAPI_CMD_STORE_NAME
:
313 OPENSSL_free(ctx
->storename
);
314 ctx
->storename
= BUF_strdup(p
);
315 CAPI_trace(ctx
, "Setting store name to %s\n", p
);
318 case CAPI_CMD_STORE_FLAGS
:
320 ctx
->store_flags
|= CERT_SYSTEM_STORE_LOCAL_MACHINE
;
321 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_CURRENT_USER
;
323 ctx
->store_flags
|= CERT_SYSTEM_STORE_CURRENT_USER
;
324 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_LOCAL_MACHINE
;
326 CAPI_trace(ctx
, "Setting flags to %d\n", i
);
329 case CAPI_CMD_DEBUG_LEVEL
:
330 ctx
->debug_level
= (int)i
;
331 CAPI_trace(ctx
, "Setting debug level to %d\n", ctx
->debug_level
);
334 case CAPI_CMD_DEBUG_FILE
:
335 ctx
->debug_file
= BUF_strdup(p
);
336 CAPI_trace(ctx
, "Setting debug file to %s\n", ctx
->debug_file
);
339 case CAPI_CMD_KEYTYPE
:
341 CAPI_trace(ctx
, "Setting key type to %d\n", ctx
->keytype
);
344 case CAPI_CMD_SET_CSP_IDX
:
345 ret
= capi_ctx_set_provname_idx(ctx
, i
);
348 case CAPI_CMD_LIST_OPTIONS
:
352 case CAPI_CMD_LOOKUP_METHOD
:
353 if (i
< 1 || i
> 3) {
354 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_INVALID_LOOKUP_METHOD
);
357 ctx
->lookup_method
= i
;
360 case CAPI_CMD_SET_CSP_NAME
:
361 ret
= capi_ctx_set_provname(ctx
, p
, ctx
->csptype
, 1);
364 case CAPI_CMD_SET_CSP_TYPE
:
369 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_UNKNOWN_COMMAND
);
378 static RSA_METHOD capi_rsa_method
= {
379 "CryptoAPI RSA method",
382 capi_rsa_priv_enc
, /* priv_enc */
383 capi_rsa_priv_dec
, /* priv_dec */
387 capi_rsa_free
, /* finish */
388 RSA_FLAG_SIGN_VER
, /* flags */
390 capi_rsa_sign
, /* rsa_sign */
394 static DSA_METHOD capi_dsa_method
= {
395 "CryptoAPI DSA method",
396 capi_dsa_do_sign
, /* dsa_do_sign */
397 0, /* dsa_sign_setup */
398 0, /* dsa_do_verify */
402 capi_dsa_free
, /* finish */
405 0, /* dsa_paramgen */
409 static int capi_init(ENGINE
*e
)
412 const RSA_METHOD
*ossl_rsa_meth
;
413 const DSA_METHOD
*ossl_dsa_meth
;
416 capi_idx
= ENGINE_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
420 cert_capi_idx
= X509_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
422 /* Setup RSA_METHOD */
423 rsa_capi_idx
= RSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
424 ossl_rsa_meth
= RSA_PKCS1_SSLeay();
425 capi_rsa_method
.rsa_pub_enc
= ossl_rsa_meth
->rsa_pub_enc
;
426 capi_rsa_method
.rsa_pub_dec
= ossl_rsa_meth
->rsa_pub_dec
;
427 capi_rsa_method
.rsa_mod_exp
= ossl_rsa_meth
->rsa_mod_exp
;
428 capi_rsa_method
.bn_mod_exp
= ossl_rsa_meth
->bn_mod_exp
;
430 /* Setup DSA Method */
431 dsa_capi_idx
= DSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
432 ossl_dsa_meth
= DSA_OpenSSL();
433 capi_dsa_method
.dsa_do_verify
= ossl_dsa_meth
->dsa_do_verify
;
434 capi_dsa_method
.dsa_mod_exp
= ossl_dsa_meth
->dsa_mod_exp
;
435 capi_dsa_method
.bn_mod_exp
= ossl_dsa_meth
->bn_mod_exp
;
438 ctx
= capi_ctx_new();
442 ENGINE_set_ex_data(e
, capi_idx
, ctx
);
444 # ifdef OPENSSL_CAPIENG_DIALOG
446 HMODULE cryptui
= LoadLibrary(TEXT("CRYPTUI.DLL"));
447 HMODULE kernel
= GetModuleHandle(TEXT("KERNEL32.DLL"));
450 (CERTDLG
) GetProcAddress(cryptui
,
451 "CryptUIDlgSelectCertificateFromStore");
454 (GETCONSWIN
) GetProcAddress(kernel
, "GetConsoleWindow");
455 if (cryptui
&& !OPENSSL_isservice())
456 ctx
->client_cert_select
= cert_select_dialog
;
463 CAPIerr(CAPI_F_CAPI_INIT
, ERR_R_MALLOC_FAILURE
);
469 static int capi_destroy(ENGINE
*e
)
471 ERR_unload_CAPI_strings();
475 static int capi_finish(ENGINE
*e
)
478 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
480 ENGINE_set_ex_data(e
, capi_idx
, NULL
);
485 * CryptoAPI key application data. This contains a handle to the private key
486 * container (for sign operations) and a handle to the key (for decrypt
491 /* Associated certificate context (if any) */
492 PCCERT_CONTEXT pcert
;
498 static int bind_capi(ENGINE
*e
)
500 if (!ENGINE_set_id(e
, engine_capi_id
)
501 || !ENGINE_set_name(e
, engine_capi_name
)
502 || !ENGINE_set_init_function(e
, capi_init
)
503 || !ENGINE_set_finish_function(e
, capi_finish
)
504 || !ENGINE_set_destroy_function(e
, capi_destroy
)
505 || !ENGINE_set_RSA(e
, &capi_rsa_method
)
506 || !ENGINE_set_DSA(e
, &capi_dsa_method
)
507 || !ENGINE_set_load_privkey_function(e
, capi_load_privkey
)
508 || !ENGINE_set_load_ssl_client_cert_function(e
,
509 capi_load_ssl_client_cert
)
510 || !ENGINE_set_cmd_defns(e
, capi_cmd_defns
)
511 || !ENGINE_set_ctrl_function(e
, capi_ctrl
))
513 ERR_load_CAPI_strings();
519 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
520 static int bind_helper(ENGINE
*e
, const char *id
)
522 if (id
&& (strcmp(id
, engine_capi_id
) != 0))
529 IMPLEMENT_DYNAMIC_CHECK_FN()
530 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
532 static ENGINE
*engine_capi(void)
534 ENGINE
*ret
= ENGINE_new();
537 if (!bind_capi(ret
)) {
544 void ENGINE_load_capi(void)
546 /* Copied from eng_[openssl|dyn].c */
547 ENGINE
*toadd
= engine_capi();
556 static int lend_tobn(BIGNUM
*bn
, unsigned char *bin
, int binlen
)
560 * Reverse buffer in place: since this is a keyblob structure that will
561 * be freed up after conversion anyway it doesn't matter if we change
564 for (i
= 0; i
< binlen
/ 2; i
++) {
567 bin
[i
] = bin
[binlen
- i
- 1];
568 bin
[binlen
- i
- 1] = c
;
571 if (!BN_bin2bn(bin
, binlen
, bn
))
576 /* Given a CAPI_KEY get an EVP_PKEY structure */
578 static EVP_PKEY
*capi_get_pkey(ENGINE
*eng
, CAPI_KEY
* key
)
580 unsigned char *pubkey
= NULL
;
585 EVP_PKEY
*ret
= NULL
;
586 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, NULL
, &len
)) {
587 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR
);
592 pubkey
= OPENSSL_malloc(len
);
597 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, pubkey
, &len
)) {
598 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_ERROR
);
603 bh
= (BLOBHEADER
*) pubkey
;
604 if (bh
->bType
!= PUBLICKEYBLOB
) {
605 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_INVALID_PUBLIC_KEY_BLOB
);
608 if (bh
->aiKeyAlg
== CALG_RSA_SIGN
|| bh
->aiKeyAlg
== CALG_RSA_KEYX
) {
611 unsigned char *rsa_modulus
;
612 rp
= (RSAPUBKEY
*) (bh
+ 1);
613 if (rp
->magic
!= 0x31415352) {
615 BIO_snprintf(magstr
, 10, "%lx", rp
->magic
);
616 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
617 CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
618 ERR_add_error_data(2, "magic=0x", magstr
);
621 rsa_modulus
= (unsigned char *)(rp
+ 1);
622 rkey
= RSA_new_method(eng
);
629 if (!rkey
->e
|| !rkey
->n
)
632 if (!BN_set_word(rkey
->e
, rp
->pubexp
))
635 rsa_modlen
= rp
->bitlen
/ 8;
636 if (!lend_tobn(rkey
->n
, rsa_modulus
, rsa_modlen
))
639 RSA_set_ex_data(rkey
, rsa_capi_idx
, key
);
641 if (!(ret
= EVP_PKEY_new()))
644 EVP_PKEY_assign_RSA(ret
, rkey
);
647 } else if (bh
->aiKeyAlg
== CALG_DSS_SIGN
) {
651 dp
= (DSSPUBKEY
*) (bh
+ 1);
652 if (dp
->magic
!= 0x31535344) {
654 BIO_snprintf(magstr
, 10, "%lx", dp
->magic
);
655 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
656 CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
657 ERR_add_error_data(2, "magic=0x", magstr
);
660 dsa_plen
= dp
->bitlen
/ 8;
661 btmp
= (unsigned char *)(dp
+ 1);
662 dkey
= DSA_new_method(eng
);
668 dkey
->pub_key
= BN_new();
669 if (!dkey
->p
|| !dkey
->q
|| !dkey
->g
|| !dkey
->pub_key
)
671 if (!lend_tobn(dkey
->p
, btmp
, dsa_plen
))
674 if (!lend_tobn(dkey
->q
, btmp
, 20))
677 if (!lend_tobn(dkey
->g
, btmp
, dsa_plen
))
680 if (!lend_tobn(dkey
->pub_key
, btmp
, dsa_plen
))
684 DSA_set_ex_data(dkey
, dsa_capi_idx
, key
);
686 if (!(ret
= EVP_PKEY_new()))
689 EVP_PKEY_assign_DSA(ret
, dkey
);
693 BIO_snprintf(algstr
, 10, "%lx", bh
->aiKeyAlg
);
694 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
695 CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM
);
696 ERR_add_error_data(2, "aiKeyAlg=0x", algstr
);
702 OPENSSL_free(pubkey
);
713 CAPIerr(CAPI_F_CAPI_GET_PKEY
, ERR_R_MALLOC_FAILURE
);
718 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
719 UI_METHOD
*ui_method
, void *callback_data
)
724 ctx
= ENGINE_get_ex_data(eng
, capi_idx
);
727 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY
, CAPI_R_CANT_FIND_CAPI_CONTEXT
);
731 key
= capi_find_key(ctx
, key_id
);
736 ret
= capi_get_pkey(eng
, key
);
744 /* CryptoAPI RSA operations */
746 int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
747 unsigned char *to
, RSA
*rsa
, int padding
)
749 CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC
, CAPI_R_FUNCTION_NOT_SUPPORTED
);
753 int capi_rsa_sign(int dtype
, const unsigned char *m
, unsigned int m_len
,
754 unsigned char *sigret
, unsigned int *siglen
, const RSA
*rsa
)
764 ctx
= ENGINE_get_ex_data(rsa
->engine
, capi_idx
);
766 CAPI_trace(ctx
, "Called CAPI_rsa_sign()\n");
768 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
770 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_GET_KEY
);
773 /* Convert the signature type to a CryptoAPI algorithm ID */
784 alg
= CALG_SSL3_SHAMD5
;
789 BIO_snprintf(algstr
, 10, "%lx", dtype
);
790 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_UNSUPPORTED_ALGORITHM_NID
);
791 ERR_add_error_data(2, "NID=0x", algstr
);
796 /* Create the hash object */
797 if (!CryptCreateHash(capi_key
->hprov
, alg
, 0, 0, &hash
)) {
798 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
802 /* Set the hash value to the value passed */
804 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)m
, 0)) {
805 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
810 /* Finally sign it */
811 slen
= RSA_size(rsa
);
812 if (!CryptSignHashA(hash
, capi_key
->keyspec
, NULL
, 0, sigret
, &slen
)) {
813 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
818 /* Inplace byte reversal of signature */
819 for (i
= 0; i
< slen
/ 2; i
++) {
822 sigret
[i
] = sigret
[slen
- i
- 1];
823 sigret
[slen
- i
- 1] = c
;
831 CryptDestroyHash(hash
);
836 int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
837 unsigned char *to
, RSA
*rsa
, int padding
)
840 unsigned char *tmpbuf
;
843 ctx
= ENGINE_get_ex_data(rsa
->engine
, capi_idx
);
845 CAPI_trace(ctx
, "Called capi_rsa_priv_dec()\n");
847 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
849 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_CANT_GET_KEY
);
853 if (padding
!= RSA_PKCS1_PADDING
) {
855 BIO_snprintf(errstr
, 10, "%d", padding
);
856 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_UNSUPPORTED_PADDING
);
857 ERR_add_error_data(2, "padding=", errstr
);
861 /* Create temp reverse order version of input */
862 if (!(tmpbuf
= OPENSSL_malloc(flen
))) {
863 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, ERR_R_MALLOC_FAILURE
);
866 for (i
= 0; i
< flen
; i
++)
867 tmpbuf
[flen
- i
- 1] = from
[i
];
869 /* Finally decrypt it */
870 if (!CryptDecrypt(capi_key
->key
, 0, TRUE
, 0, tmpbuf
, &flen
)) {
871 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_DECRYPT_ERROR
);
873 OPENSSL_free(tmpbuf
);
876 memcpy(to
, tmpbuf
, flen
);
878 OPENSSL_free(tmpbuf
);
883 static int capi_rsa_free(RSA
*rsa
)
886 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
887 capi_free_key(capi_key
);
888 RSA_set_ex_data(rsa
, rsa_capi_idx
, 0);
892 /* CryptoAPI DSA operations */
894 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
902 unsigned char csigbuf
[40];
904 ctx
= ENGINE_get_ex_data(dsa
->engine
, capi_idx
);
906 CAPI_trace(ctx
, "Called CAPI_dsa_do_sign()\n");
908 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
911 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_GET_KEY
);
916 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_INVALID_DIGEST_LENGTH
);
920 /* Create the hash object */
921 if (!CryptCreateHash(capi_key
->hprov
, CALG_SHA1
, 0, 0, &hash
)) {
922 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
927 /* Set the hash value to the value passed */
928 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)digest
, 0)) {
929 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
934 /* Finally sign it */
935 slen
= sizeof(csigbuf
);
936 if (!CryptSignHashA(hash
, capi_key
->keyspec
, NULL
, 0, csigbuf
, &slen
)) {
937 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
946 if (!ret
->r
|| !ret
->s
)
948 if (!lend_tobn(ret
->r
, csigbuf
, 20)
949 || !lend_tobn(ret
->s
, csigbuf
+ 20, 20)) {
959 OPENSSL_cleanse(csigbuf
, 40);
960 CryptDestroyHash(hash
);
964 static int capi_dsa_free(DSA
*dsa
)
967 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
968 capi_free_key(capi_key
);
969 DSA_set_ex_data(dsa
, dsa_capi_idx
, 0);
973 static void capi_vtrace(CAPI_CTX
* ctx
, int level
, char *format
,
978 if (!ctx
|| (ctx
->debug_level
< level
) || (!ctx
->debug_file
))
980 out
= BIO_new_file(ctx
->debug_file
, "a+");
981 BIO_vprintf(out
, format
, argptr
);
985 static void CAPI_trace(CAPI_CTX
* ctx
, char *format
, ...)
988 va_start(args
, format
);
989 capi_vtrace(ctx
, CAPI_DBG_TRACE
, format
, args
);
993 static void capi_addlasterror(void)
995 capi_adderror(GetLastError());
998 static void capi_adderror(DWORD err
)
1001 BIO_snprintf(errstr
, 10, "%lX", err
);
1002 ERR_add_error_data(2, "Error code= 0x", errstr
);
1005 static char *wide_to_asc(LPWSTR wstr
)
1012 len_0
= (int)wcslen(wstr
) + 1; /* WideCharToMultiByte expects int */
1013 sz
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len_0
, NULL
, 0, NULL
, NULL
);
1015 CAPIerr(CAPI_F_WIDE_TO_ASC
, CAPI_R_WIN32_ERROR
);
1018 str
= OPENSSL_malloc(sz
);
1020 CAPIerr(CAPI_F_WIDE_TO_ASC
, ERR_R_MALLOC_FAILURE
);
1023 if (!WideCharToMultiByte(CP_ACP
, 0, wstr
, len_0
, str
, sz
, NULL
, NULL
)) {
1025 CAPIerr(CAPI_F_WIDE_TO_ASC
, CAPI_R_WIN32_ERROR
);
1031 static int capi_get_provname(CAPI_CTX
* ctx
, LPSTR
* pname
, DWORD
* ptype
,
1036 CAPI_trace(ctx
, "capi_get_provname, index=%d\n", idx
);
1037 if (!CryptEnumProvidersA(idx
, NULL
, 0, ptype
, NULL
, &len
)) {
1038 err
= GetLastError();
1039 if (err
== ERROR_NO_MORE_ITEMS
)
1041 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1045 name
= OPENSSL_malloc(len
);
1046 if (!CryptEnumProvidersA(idx
, NULL
, 0, ptype
, name
, &len
)) {
1047 err
= GetLastError();
1048 if (err
== ERROR_NO_MORE_ITEMS
)
1050 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1055 CAPI_trace(ctx
, "capi_get_provname, returned name=%s, type=%d\n", name
,
1061 static int capi_list_providers(CAPI_CTX
* ctx
, BIO
*out
)
1065 LPSTR provname
= NULL
;
1066 CAPI_trace(ctx
, "capi_list_providers\n");
1067 BIO_printf(out
, "Available CSPs:\n");
1068 for (idx
= 0;; idx
++) {
1069 ret
= capi_get_provname(ctx
, &provname
, &ptype
, idx
);
1074 BIO_printf(out
, "%d. %s, type %d\n", idx
, provname
, ptype
);
1075 OPENSSL_free(provname
);
1080 static int capi_list_containers(CAPI_CTX
* ctx
, BIO
*out
)
1084 DWORD err
, idx
, flags
, buflen
= 0, clen
;
1086 CAPI_trace(ctx
, "Listing containers CSP=%s, type = %d\n", ctx
->cspname
,
1088 if (!CryptAcquireContextA
1089 (&hprov
, NULL
, ctx
->cspname
, ctx
->csptype
, CRYPT_VERIFYCONTEXT
)) {
1090 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
,
1091 CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1092 capi_addlasterror();
1095 if (!CryptGetProvParam
1096 (hprov
, PP_ENUMCONTAINERS
, NULL
, &buflen
, CRYPT_FIRST
)) {
1097 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1098 capi_addlasterror();
1099 CryptReleaseContext(hprov
, 0);
1102 CAPI_trace(ctx
, "Got max container len %d\n", buflen
);
1105 cname
= OPENSSL_malloc(buflen
);
1107 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, ERR_R_MALLOC_FAILURE
);
1111 for (idx
= 0;; idx
++) {
1116 flags
= CRYPT_FIRST
;
1119 if (!CryptGetProvParam(hprov
, PP_ENUMCONTAINERS
, cname
, &clen
, flags
)) {
1120 err
= GetLastError();
1121 if (err
== ERROR_NO_MORE_ITEMS
)
1123 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1127 CAPI_trace(ctx
, "Container name %s, len=%d, index=%d, flags=%d\n",
1128 cname
, clen
, idx
, flags
);
1129 if (!cname
[0] && (clen
== buflen
)) {
1130 CAPI_trace(ctx
, "Enumerate bug: using workaround\n");
1133 BIO_printf(out
, "%d. %s\n", idx
, cname
);
1141 OPENSSL_free(cname
);
1142 CryptReleaseContext(hprov
, 0);
1147 CRYPT_KEY_PROV_INFO
*capi_get_prov_info(CAPI_CTX
* ctx
, PCCERT_CONTEXT cert
)
1150 CRYPT_KEY_PROV_INFO
*pinfo
;
1152 if (!CertGetCertificateContextProperty
1153 (cert
, CERT_KEY_PROV_INFO_PROP_ID
, NULL
, &len
))
1155 pinfo
= OPENSSL_malloc(len
);
1157 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
, ERR_R_MALLOC_FAILURE
);
1160 if (!CertGetCertificateContextProperty
1161 (cert
, CERT_KEY_PROV_INFO_PROP_ID
, pinfo
, &len
)) {
1162 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
,
1163 CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO
);
1164 capi_addlasterror();
1165 OPENSSL_free(pinfo
);
1171 static void capi_dump_prov_info(CAPI_CTX
* ctx
, BIO
*out
,
1172 CRYPT_KEY_PROV_INFO
* pinfo
)
1174 char *provname
= NULL
, *contname
= NULL
;
1176 BIO_printf(out
, " No Private Key\n");
1179 provname
= wide_to_asc(pinfo
->pwszProvName
);
1180 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1181 if (!provname
|| !contname
)
1184 BIO_printf(out
, " Private Key Info:\n");
1185 BIO_printf(out
, " Provider Name: %s, Provider Type %d\n", provname
,
1187 BIO_printf(out
, " Container Name: %s, Key Type %d\n", contname
,
1191 OPENSSL_free(provname
);
1193 OPENSSL_free(contname
);
1196 char *capi_cert_get_fname(CAPI_CTX
* ctx
, PCCERT_CONTEXT cert
)
1201 CAPI_trace(ctx
, "capi_cert_get_fname\n");
1202 if (!CertGetCertificateContextProperty
1203 (cert
, CERT_FRIENDLY_NAME_PROP_ID
, NULL
, &dlen
))
1205 wfname
= OPENSSL_malloc(dlen
);
1206 if (CertGetCertificateContextProperty
1207 (cert
, CERT_FRIENDLY_NAME_PROP_ID
, wfname
, &dlen
)) {
1208 char *fname
= wide_to_asc(wfname
);
1209 OPENSSL_free(wfname
);
1212 CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME
, CAPI_R_ERROR_GETTING_FRIENDLY_NAME
);
1213 capi_addlasterror();
1215 OPENSSL_free(wfname
);
1219 void capi_dump_cert(CAPI_CTX
* ctx
, BIO
*out
, PCCERT_CONTEXT cert
)
1223 unsigned long flags
= ctx
->dump_flags
;
1224 if (flags
& CAPI_DMP_FNAME
) {
1226 fname
= capi_cert_get_fname(ctx
, cert
);
1228 BIO_printf(out
, " Friendly Name \"%s\"\n", fname
);
1229 OPENSSL_free(fname
);
1231 BIO_printf(out
, " <No Friendly Name>\n");
1234 p
= cert
->pbCertEncoded
;
1235 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1237 BIO_printf(out
, " <Can't parse certificate>\n");
1238 if (flags
& CAPI_DMP_SUMMARY
) {
1239 BIO_printf(out
, " Subject: ");
1240 X509_NAME_print_ex(out
, X509_get_subject_name(x
), 0, XN_FLAG_ONELINE
);
1241 BIO_printf(out
, "\n Issuer: ");
1242 X509_NAME_print_ex(out
, X509_get_issuer_name(x
), 0, XN_FLAG_ONELINE
);
1243 BIO_printf(out
, "\n");
1245 if (flags
& CAPI_DMP_FULL
)
1246 X509_print_ex(out
, x
, XN_FLAG_ONELINE
, 0);
1248 if (flags
& CAPI_DMP_PKEYINFO
) {
1249 CRYPT_KEY_PROV_INFO
*pinfo
;
1250 pinfo
= capi_get_prov_info(ctx
, cert
);
1251 capi_dump_prov_info(ctx
, out
, pinfo
);
1253 OPENSSL_free(pinfo
);
1256 if (flags
& CAPI_DMP_PEM
)
1257 PEM_write_bio_X509(out
, x
);
1261 HCERTSTORE
capi_open_store(CAPI_CTX
* ctx
, char *storename
)
1266 storename
= ctx
->storename
;
1269 CAPI_trace(ctx
, "Opening certificate store %s\n", storename
);
1271 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_A
, 0, 0,
1272 ctx
->store_flags
, storename
);
1274 CAPIerr(CAPI_F_CAPI_OPEN_STORE
, CAPI_R_ERROR_OPENING_STORE
);
1275 capi_addlasterror();
1280 int capi_list_certs(CAPI_CTX
* ctx
, BIO
*out
, char *id
)
1286 PCCERT_CONTEXT cert
= NULL
;
1288 storename
= ctx
->storename
;
1291 CAPI_trace(ctx
, "Listing certs for store %s\n", storename
);
1293 hstore
= capi_open_store(ctx
, storename
);
1297 cert
= capi_find_cert(ctx
, id
, hstore
);
1302 capi_dump_cert(ctx
, out
, cert
);
1303 CertFreeCertificateContext(cert
);
1305 for (idx
= 0;; idx
++) {
1306 LPWSTR fname
= NULL
;
1307 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1310 BIO_printf(out
, "Certificate %d\n", idx
);
1311 capi_dump_cert(ctx
, out
, cert
);
1315 CertCloseStore(hstore
, 0);
1319 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
* ctx
, const char *id
,
1322 PCCERT_CONTEXT cert
= NULL
;
1325 switch (ctx
->lookup_method
) {
1326 case CAPI_LU_SUBSTR
:
1327 return CertFindCertificateInStore(hstore
,
1328 X509_ASN_ENCODING
, 0,
1329 CERT_FIND_SUBJECT_STR_A
, id
, NULL
);
1332 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1335 fname
= capi_cert_get_fname(ctx
, cert
);
1337 if (strcmp(fname
, id
))
1341 OPENSSL_free(fname
);
1351 static CAPI_KEY
*capi_get_key(CAPI_CTX
* ctx
, const char *contname
,
1352 char *provname
, DWORD ptype
, DWORD keyspec
)
1356 key
= OPENSSL_malloc(sizeof(CAPI_KEY
));
1357 CAPI_trace(ctx
, "capi_get_key, contname=%s, provname=%s, type=%d\n",
1358 contname
, provname
, ptype
);
1359 if (ctx
->store_flags
& CERT_SYSTEM_STORE_LOCAL_MACHINE
)
1360 dwFlags
= CRYPT_MACHINE_KEYSET
;
1361 if (!CryptAcquireContextA
1362 (&key
->hprov
, contname
, provname
, ptype
, dwFlags
)) {
1363 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1364 capi_addlasterror();
1367 if (!CryptGetUserKey(key
->hprov
, keyspec
, &key
->key
)) {
1368 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_GETUSERKEY_ERROR
);
1369 capi_addlasterror();
1370 CryptReleaseContext(key
->hprov
, 0);
1373 key
->keyspec
= keyspec
;
1382 static CAPI_KEY
*capi_get_cert_key(CAPI_CTX
* ctx
, PCCERT_CONTEXT cert
)
1384 CAPI_KEY
*key
= NULL
;
1385 CRYPT_KEY_PROV_INFO
*pinfo
= NULL
;
1386 char *provname
= NULL
, *contname
= NULL
;
1387 pinfo
= capi_get_prov_info(ctx
, cert
);
1390 provname
= wide_to_asc(pinfo
->pwszProvName
);
1391 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1392 if (!provname
|| !contname
)
1394 key
= capi_get_key(ctx
, contname
, provname
,
1395 pinfo
->dwProvType
, pinfo
->dwKeySpec
);
1399 OPENSSL_free(pinfo
);
1401 OPENSSL_free(provname
);
1403 OPENSSL_free(contname
);
1407 CAPI_KEY
*capi_find_key(CAPI_CTX
* ctx
, const char *id
)
1409 PCCERT_CONTEXT cert
;
1411 CAPI_KEY
*key
= NULL
;
1412 switch (ctx
->lookup_method
) {
1413 case CAPI_LU_SUBSTR
:
1415 hstore
= capi_open_store(ctx
, NULL
);
1418 cert
= capi_find_cert(ctx
, id
, hstore
);
1420 key
= capi_get_cert_key(ctx
, cert
);
1421 CertFreeCertificateContext(cert
);
1423 CertCloseStore(hstore
, 0);
1426 case CAPI_LU_CONTNAME
:
1427 key
= capi_get_key(ctx
, id
, ctx
->cspname
, ctx
->csptype
, ctx
->keytype
);
1434 void capi_free_key(CAPI_KEY
* key
)
1438 CryptDestroyKey(key
->key
);
1439 CryptReleaseContext(key
->hprov
, 0);
1441 CertFreeCertificateContext(key
->pcert
);
1445 /* Initialize a CAPI_CTX structure */
1447 static CAPI_CTX
*capi_ctx_new()
1450 ctx
= OPENSSL_malloc(sizeof(CAPI_CTX
));
1452 CAPIerr(CAPI_F_CAPI_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
1455 ctx
->cspname
= NULL
;
1456 ctx
->csptype
= PROV_RSA_FULL
;
1457 ctx
->dump_flags
= CAPI_DMP_SUMMARY
| CAPI_DMP_FNAME
;
1458 ctx
->keytype
= AT_KEYEXCHANGE
;
1459 ctx
->storename
= NULL
;
1460 ctx
->ssl_client_store
= NULL
;
1461 ctx
->store_flags
= CERT_STORE_OPEN_EXISTING_FLAG
|
1462 CERT_STORE_READONLY_FLAG
| CERT_SYSTEM_STORE_CURRENT_USER
;
1463 ctx
->lookup_method
= CAPI_LU_SUBSTR
;
1464 ctx
->debug_level
= 0;
1465 ctx
->debug_file
= NULL
;
1466 ctx
->client_cert_select
= cert_select_simple
;
1470 static void capi_ctx_free(CAPI_CTX
* ctx
)
1472 CAPI_trace(ctx
, "Calling capi_ctx_free with %lx\n", ctx
);
1476 OPENSSL_free(ctx
->cspname
);
1477 if (ctx
->debug_file
)
1478 OPENSSL_free(ctx
->debug_file
);
1480 OPENSSL_free(ctx
->storename
);
1481 if (ctx
->ssl_client_store
)
1482 OPENSSL_free(ctx
->ssl_client_store
);
1486 static int capi_ctx_set_provname(CAPI_CTX
* ctx
, LPSTR pname
, DWORD type
,
1489 CAPI_trace(ctx
, "capi_ctx_set_provname, name=%s, type=%d\n", pname
, type
);
1492 if (!CryptAcquireContextA(&hprov
, NULL
, pname
, type
,
1493 CRYPT_VERIFYCONTEXT
)) {
1494 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME
,
1495 CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1496 capi_addlasterror();
1499 CryptReleaseContext(hprov
, 0);
1502 OPENSSL_free(ctx
->cspname
);
1503 ctx
->cspname
= BUF_strdup(pname
);
1504 ctx
->csptype
= type
;
1508 static int capi_ctx_set_provname_idx(CAPI_CTX
* ctx
, int idx
)
1513 if (capi_get_provname(ctx
, &pname
, &type
, idx
) != 1)
1515 res
= capi_ctx_set_provname(ctx
, pname
, type
, 0);
1516 OPENSSL_free(pname
);
1520 static int cert_issuer_match(STACK_OF(X509_NAME
) *ca_dn
, X509
*x
)
1524 /* Special case: empty list: match anything */
1525 if (sk_X509_NAME_num(ca_dn
) <= 0)
1527 for (i
= 0; i
< sk_X509_NAME_num(ca_dn
); i
++) {
1528 nm
= sk_X509_NAME_value(ca_dn
, i
);
1529 if (!X509_NAME_cmp(nm
, X509_get_issuer_name(x
)))
1535 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
1536 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
,
1537 EVP_PKEY
**pkey
, STACK_OF(X509
) **pother
,
1538 UI_METHOD
*ui_method
,
1539 void *callback_data
)
1541 STACK_OF(X509
) *certs
= NULL
;
1545 int i
, client_cert_idx
;
1547 PCCERT_CONTEXT cert
= NULL
, excert
= NULL
;
1550 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1555 storename
= ctx
->ssl_client_store
;
1559 hstore
= capi_open_store(ctx
, storename
);
1562 /* Enumerate all certificates collect any matches */
1564 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1567 p
= cert
->pbCertEncoded
;
1568 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1570 CAPI_trace(ctx
, "Can't Parse Certificate %d\n", i
);
1573 if (cert_issuer_match(ca_dn
, x
)
1574 && X509_check_purpose(x
, X509_PURPOSE_SSL_CLIENT
, 0)) {
1575 key
= capi_get_cert_key(ctx
, cert
);
1581 * Match found: attach extra data to it so we can retrieve the
1584 excert
= CertDuplicateCertificateContext(cert
);
1585 key
->pcert
= excert
;
1586 X509_set_ex_data(x
, cert_capi_idx
, key
);
1589 certs
= sk_X509_new_null();
1591 sk_X509_push(certs
, x
);
1598 CertFreeCertificateContext(cert
);
1600 CertCloseStore(hstore
, 0);
1605 /* Select the appropriate certificate */
1607 client_cert_idx
= ctx
->client_cert_select(e
, ssl
, certs
);
1609 /* Set the selected certificate and free the rest */
1611 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1612 x
= sk_X509_value(certs
, i
);
1613 if (i
== client_cert_idx
)
1616 key
= X509_get_ex_data(x
, cert_capi_idx
);
1622 sk_X509_free(certs
);
1627 /* Setup key for selected certificate */
1629 key
= X509_get_ex_data(*pcert
, cert_capi_idx
);
1630 *pkey
= capi_get_pkey(e
, key
);
1631 X509_set_ex_data(*pcert
, cert_capi_idx
, NULL
);
1637 /* Simple client cert selection function: always select first */
1639 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1644 # ifdef OPENSSL_CAPIENG_DIALOG
1647 * More complex cert selection function, using standard function
1648 * CryptUIDlgSelectCertificateFromStore() to produce a dialog box.
1652 * Definitions which are in cryptuiapi.h but this is not present in older
1653 * versions of headers.
1656 # ifndef CRYPTUI_SELECT_LOCATION_COLUMN
1657 # define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010
1658 # define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004
1661 # define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
1662 # define dlg_prompt L"Select a certificate to use for authentication"
1663 # define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
1664 |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
1666 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1670 PCCERT_CONTEXT cert
;
1675 if (sk_X509_num(certs
) == 1)
1677 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1678 /* Create an in memory store of certificates */
1679 dstore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1680 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1682 CAPIerr(CAPI_F_CERT_SELECT_DIALOG
, CAPI_R_ERROR_CREATING_STORE
);
1683 capi_addlasterror();
1686 /* Add all certificates to store */
1687 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1688 x
= sk_X509_value(certs
, i
);
1689 key
= X509_get_ex_data(x
, cert_capi_idx
);
1691 if (!CertAddCertificateContextToStore(dstore
, key
->pcert
,
1692 CERT_STORE_ADD_NEW
, NULL
)) {
1693 CAPIerr(CAPI_F_CERT_SELECT_DIALOG
, CAPI_R_ERROR_ADDING_CERT
);
1694 capi_addlasterror();
1699 hwnd
= GetForegroundWindow();
1701 hwnd
= GetActiveWindow();
1702 if (!hwnd
&& ctx
->getconswindow
)
1703 hwnd
= ctx
->getconswindow();
1704 /* Call dialog to select one */
1705 cert
= ctx
->certselectdlg(dstore
, hwnd
, dlg_title
, dlg_prompt
,
1706 dlg_columns
, 0, NULL
);
1708 /* Find matching cert from list */
1710 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1711 x
= sk_X509_value(certs
, i
);
1712 key
= X509_get_ex_data(x
, cert_capi_idx
);
1713 if (CertCompareCertificate
1714 (X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
, cert
->pCertInfo
,
1715 key
->pcert
->pCertInfo
)) {
1724 CertCloseStore(dstore
, 0);
1732 # include <openssl/engine.h>
1733 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1735 int bind_engine(ENGINE
*e
, const char *id
, const dynamic_fns
*fns
)
1740 IMPLEMENT_DYNAMIC_CHECK_FN()