2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
57 #include <openssl/crypto.h>
58 #include <openssl/buffer.h>
59 #include <openssl/rsa.h>
60 #include <openssl/bn.h>
62 #ifdef OPENSSL_SYS_WIN32
63 #ifndef OPENSSL_NO_CAPIENG
68 #undef X509_EXTENSIONS
71 #include <openssl/engine.h>
72 #include <openssl/pem.h>
74 #include "e_capi_err.h"
75 #include "e_capi_err.c"
78 static const char *engine_capi_id
= "capi";
79 static const char *engine_capi_name
= "CryptoAPI ENGINE";
81 typedef struct CAPI_CTX_st CAPI_CTX
;
82 typedef struct CAPI_KEY_st CAPI_KEY
;
84 static void capi_addlasterror(void);
85 static void capi_adderror(DWORD err
);
87 static void CAPI_trace(CAPI_CTX
*ctx
, char *format
, ...);
89 static int capi_list_providers(CAPI_CTX
*ctx
, BIO
*out
);
90 static int capi_list_containers(CAPI_CTX
*ctx
, BIO
*out
);
91 int capi_list_certs(CAPI_CTX
*ctx
, BIO
*out
, char *storename
);
92 void capi_free_key(CAPI_KEY
*key
);
94 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
*ctx
, const char *id
, HCERTSTORE hstore
);
96 CAPI_KEY
*capi_find_key(CAPI_CTX
*ctx
, const char *id
);
98 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
99 UI_METHOD
*ui_method
, void *callback_data
);
100 static int capi_rsa_sign(int dtype
, const unsigned char *m
, unsigned int m_len
,
101 unsigned char *sigret
, unsigned int *siglen
, const RSA
*rsa
);
102 static int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
103 unsigned char *to
, RSA
*rsa
, int padding
);
104 static int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
105 unsigned char *to
, RSA
*rsa
, int padding
);
106 static int capi_rsa_free(RSA
*rsa
);
108 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
110 static int capi_dsa_free(DSA
*dsa
);
112 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
113 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
, EVP_PKEY
**pkey
,
114 STACK_OF(X509
) **pother
, UI_METHOD
*ui_method
, void *callback_data
);
116 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
117 #ifdef OPENSSL_CAPIENG_DIALOG
118 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
121 typedef PCCERT_CONTEXT (WINAPI
*CERTDLG
)(HCERTSTORE
, HWND
, LPCWSTR
,
122 LPCWSTR
, DWORD
, DWORD
,
124 typedef HWND (WINAPI
*GETCONSWIN
)(void);
126 /* This structure contains CAPI ENGINE specific data:
127 * it contains various global options and affects how
128 * other functions behave.
131 #define CAPI_DBG_TRACE 2
132 #define CAPI_DBG_ERROR 1
137 /* Parameters to use for container lookup */
141 /* Certificate store name to use */
143 LPTSTR ssl_client_store
;
144 /* System store flags */
147 /* Lookup string meanings in load_private_key */
148 /* Substring of subject: uses "storename" */
149 #define CAPI_LU_SUBSTR 0
150 /* Friendly name: uses storename */
151 #define CAPI_LU_FNAME 1
152 /* Container name: uses cspname, keytype */
153 #define CAPI_LU_CONTNAME 2
155 /* Info to dump with dumpcerts option */
156 /* Issuer and serial name strings */
157 #define CAPI_DMP_SUMMARY 0x1
159 #define CAPI_DMP_FNAME 0x2
160 /* Full X509_print dump */
161 #define CAPI_DMP_FULL 0x4
162 /* Dump PEM format certificate */
163 #define CAPI_DMP_PEM 0x8
164 /* Dump pseudo key (if possible) */
165 #define CAPI_DMP_PSKEY 0x10
166 /* Dump key info (if possible) */
167 #define CAPI_DMP_PKEYINFO 0x20
170 int (*client_cert_select
)(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
172 CERTDLG certselectdlg
;
173 GETCONSWIN getconswindow
;
177 static CAPI_CTX
*capi_ctx_new();
178 static void capi_ctx_free(CAPI_CTX
*ctx
);
179 static int capi_ctx_set_provname(CAPI_CTX
*ctx
, LPSTR pname
, DWORD type
, int check
);
180 static int capi_ctx_set_provname_idx(CAPI_CTX
*ctx
, int idx
);
182 #define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
183 #define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
184 #define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
185 #define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
186 #define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
187 #define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
188 #define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
189 #define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
190 #define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
191 #define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
192 #define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
193 #define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
194 #define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
195 #define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13)
197 static const ENGINE_CMD_DEFN capi_cmd_defns
[] = {
198 {CAPI_CMD_LIST_CERTS
,
200 "List all certificates in store",
201 ENGINE_CMD_FLAG_NO_INPUT
},
202 {CAPI_CMD_LOOKUP_CERT
,
204 "Lookup and output certificates",
205 ENGINE_CMD_FLAG_STRING
},
206 {CAPI_CMD_DEBUG_LEVEL
,
208 "debug level (1=errors, 2=trace)",
209 ENGINE_CMD_FLAG_NUMERIC
},
210 {CAPI_CMD_DEBUG_FILE
,
212 "debugging filename)",
213 ENGINE_CMD_FLAG_STRING
},
216 "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
217 ENGINE_CMD_FLAG_NUMERIC
},
221 ENGINE_CMD_FLAG_NO_INPUT
},
222 {CAPI_CMD_SET_CSP_IDX
,
225 ENGINE_CMD_FLAG_NUMERIC
},
226 {CAPI_CMD_SET_CSP_NAME
,
228 "Set CSP name, (default CSP used if not specified)",
229 ENGINE_CMD_FLAG_STRING
},
230 {CAPI_CMD_SET_CSP_TYPE
,
232 "Set CSP type, (default RSA_PROV_FULL)",
233 ENGINE_CMD_FLAG_NUMERIC
},
234 {CAPI_CMD_LIST_CONTAINERS
,
236 "list container names",
237 ENGINE_CMD_FLAG_NO_INPUT
},
238 {CAPI_CMD_LIST_OPTIONS
,
240 "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
241 "32=private key info)",
242 ENGINE_CMD_FLAG_NUMERIC
},
243 {CAPI_CMD_LOOKUP_METHOD
,
245 "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
246 ENGINE_CMD_FLAG_NUMERIC
},
247 {CAPI_CMD_STORE_NAME
,
249 "certificate store name, default \"MY\"",
250 ENGINE_CMD_FLAG_STRING
},
251 {CAPI_CMD_STORE_FLAGS
,
253 "Certificate store flags: 1 = system store",
254 ENGINE_CMD_FLAG_NUMERIC
},
259 static int capi_idx
= -1;
260 static int rsa_capi_idx
= -1;
261 static int dsa_capi_idx
= -1;
262 static int cert_capi_idx
= -1;
264 static int capi_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)(void))
271 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_ENGINE_NOT_INITIALIZED
);
274 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
275 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
278 case CAPI_CMD_LIST_CSPS
:
279 ret
= capi_list_providers(ctx
, out
);
282 case CAPI_CMD_LIST_CERTS
:
283 ret
= capi_list_certs(ctx
, out
, NULL
);
286 case CAPI_CMD_LOOKUP_CERT
:
287 ret
= capi_list_certs(ctx
, out
, p
);
290 case CAPI_CMD_LIST_CONTAINERS
:
291 ret
= capi_list_containers(ctx
, out
);
294 case CAPI_CMD_STORE_NAME
:
296 OPENSSL_free(ctx
->storename
);
297 ctx
->storename
= BUF_strdup(p
);
298 CAPI_trace(ctx
, "Setting store name to %s\n", p
);
301 case CAPI_CMD_STORE_FLAGS
:
304 ctx
->store_flags
|= CERT_SYSTEM_STORE_LOCAL_MACHINE
;
305 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_CURRENT_USER
;
309 ctx
->store_flags
|= CERT_SYSTEM_STORE_CURRENT_USER
;
310 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_LOCAL_MACHINE
;
312 CAPI_trace(ctx
, "Setting flags to %d\n", i
);
315 case CAPI_CMD_DEBUG_LEVEL
:
316 ctx
->debug_level
= (int)i
;
317 CAPI_trace(ctx
, "Setting debug level to %d\n", ctx
->debug_level
);
320 case CAPI_CMD_DEBUG_FILE
:
321 ctx
->debug_file
= BUF_strdup(p
);
322 CAPI_trace(ctx
, "Setting debug file to %s\n", ctx
->debug_file
);
325 case CAPI_CMD_KEYTYPE
:
327 CAPI_trace(ctx
, "Setting key type to %d\n", ctx
->keytype
);
330 case CAPI_CMD_SET_CSP_IDX
:
331 ret
= capi_ctx_set_provname_idx(ctx
, i
);
334 case CAPI_CMD_LIST_OPTIONS
:
338 case CAPI_CMD_LOOKUP_METHOD
:
341 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_INVALID_LOOKUP_METHOD
);
344 ctx
->lookup_method
= i
;
347 case CAPI_CMD_SET_CSP_NAME
:
348 ret
= capi_ctx_set_provname(ctx
, p
, ctx
->csptype
, 1);
351 case CAPI_CMD_SET_CSP_TYPE
:
356 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_UNKNOWN_COMMAND
);
365 static RSA_METHOD capi_rsa_method
=
367 "CryptoAPI RSA method",
370 capi_rsa_priv_enc
, /* priv_enc */
371 capi_rsa_priv_dec
, /* priv_dec */
375 capi_rsa_free
, /* finish */
376 RSA_FLAG_SIGN_VER
, /* flags */
378 capi_rsa_sign
, /* rsa_sign */
382 static DSA_METHOD capi_dsa_method
=
384 "CryptoAPI DSA method",
385 capi_dsa_do_sign
, /* dsa_do_sign */
386 0, /* dsa_sign_setup */
387 0, /* dsa_do_verify */
391 capi_dsa_free
, /* finish */
394 0, /* dsa_paramgen */
398 static int capi_init(ENGINE
*e
)
401 const RSA_METHOD
*ossl_rsa_meth
;
402 const DSA_METHOD
*ossl_dsa_meth
;
403 capi_idx
= ENGINE_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
404 cert_capi_idx
= X509_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
406 ctx
= capi_ctx_new();
407 if (!ctx
|| (capi_idx
< 0))
410 ENGINE_set_ex_data(e
, capi_idx
, ctx
);
411 /* Setup RSA_METHOD */
412 rsa_capi_idx
= RSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
413 ossl_rsa_meth
= RSA_PKCS1_SSLeay();
414 capi_rsa_method
.rsa_pub_enc
= ossl_rsa_meth
->rsa_pub_enc
;
415 capi_rsa_method
.rsa_pub_dec
= ossl_rsa_meth
->rsa_pub_dec
;
416 capi_rsa_method
.rsa_mod_exp
= ossl_rsa_meth
->rsa_mod_exp
;
417 capi_rsa_method
.bn_mod_exp
= ossl_rsa_meth
->bn_mod_exp
;
419 /* Setup DSA Method */
420 dsa_capi_idx
= DSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
421 ossl_dsa_meth
= DSA_OpenSSL();
422 capi_dsa_method
.dsa_do_verify
= ossl_dsa_meth
->dsa_do_verify
;
423 capi_dsa_method
.dsa_mod_exp
= ossl_dsa_meth
->dsa_mod_exp
;
424 capi_dsa_method
.bn_mod_exp
= ossl_dsa_meth
->bn_mod_exp
;
426 #ifdef OPENSSL_CAPIENG_DIALOG
428 HMODULE cryptui
= LoadLibrary(TEXT("CRYPTUI.DLL"));
429 HMODULE kernel
= LoadLibrary(TEXT("KERNEL32.DLL"));
431 ctx
->certselectdlg
= (CERTDLG
)GetProcAddress(cryptui
, "CryptUIDlgSelectCertificateFromStore");
433 ctx
->getconswindow
= (GETCONSWIN
)GetProcAddress(kernel
, "GetConsoleWindow");
434 if (cryptui
&& !OPENSSL_isservice())
435 ctx
->client_cert_select
= cert_select_dialog
;
443 CAPIerr(CAPI_F_CAPI_INIT
, ERR_R_MALLOC_FAILURE
);
449 static int capi_destroy(ENGINE
*e
)
451 ERR_unload_CAPI_strings();
455 static int capi_finish(ENGINE
*e
)
458 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
460 ENGINE_set_ex_data(e
, capi_idx
, NULL
);
465 /* CryptoAPI key application data. This contains
466 * a handle to the private key container (for sign operations)
467 * and a handle to the key (for decrypt operations).
472 /* Associated certificate context (if any) */
473 PCCERT_CONTEXT pcert
;
479 static int bind_capi(ENGINE
*e
)
481 if (!ENGINE_set_id(e
, engine_capi_id
)
482 || !ENGINE_set_name(e
, engine_capi_name
)
483 || !ENGINE_set_init_function(e
, capi_init
)
484 || !ENGINE_set_finish_function(e
, capi_finish
)
485 || !ENGINE_set_destroy_function(e
, capi_destroy
)
486 || !ENGINE_set_RSA(e
, &capi_rsa_method
)
487 || !ENGINE_set_DSA(e
, &capi_dsa_method
)
488 || !ENGINE_set_load_privkey_function(e
, capi_load_privkey
)
489 || !ENGINE_set_load_ssl_client_cert_function(e
,
490 capi_load_ssl_client_cert
)
491 || !ENGINE_set_cmd_defns(e
, capi_cmd_defns
)
492 || !ENGINE_set_ctrl_function(e
, capi_ctrl
))
494 ERR_load_CAPI_strings();
500 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
501 static int bind_helper(ENGINE
*e
, const char *id
)
503 if(id
&& (strcmp(id
, engine_capi_id
) != 0))
509 IMPLEMENT_DYNAMIC_CHECK_FN()
510 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
512 static ENGINE
*engine_capi(void)
514 ENGINE
*ret
= ENGINE_new();
525 void ENGINE_load_capi(void)
527 /* Copied from eng_[openssl|dyn].c */
528 ENGINE
*toadd
= engine_capi();
537 static int lend_tobn(BIGNUM
*bn
, unsigned char *bin
, int binlen
)
540 /* Reverse buffer in place: since this is a keyblob structure
541 * that will be freed up after conversion anyway it doesn't
542 * matter if we change it.
544 for(i
= 0; i
< binlen
/ 2; i
++)
548 bin
[i
] = bin
[binlen
- i
- 1];
549 bin
[binlen
- i
- 1] = c
;
552 if (!BN_bin2bn(bin
, binlen
, bn
))
557 /* Given a CAPI_KEY get an EVP_PKEY structure */
559 static EVP_PKEY
*capi_get_pkey(ENGINE
*eng
, CAPI_KEY
*key
)
561 unsigned char *pubkey
= NULL
;
566 EVP_PKEY
*ret
= NULL
;
567 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, NULL
, &len
))
569 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR
);
574 pubkey
= OPENSSL_malloc(len
);
579 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, pubkey
, &len
))
581 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_ERROR
);
586 bh
= (BLOBHEADER
*)pubkey
;
587 if (bh
->bType
!= PUBLICKEYBLOB
)
589 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_INVALID_PUBLIC_KEY_BLOB
);
592 if (bh
->aiKeyAlg
== CALG_RSA_SIGN
|| bh
->aiKeyAlg
== CALG_RSA_KEYX
)
596 unsigned char *rsa_modulus
;
597 rp
= (RSAPUBKEY
*)(bh
+ 1);
598 if (rp
->magic
!= 0x31415352)
601 BIO_snprintf(magstr
, 10, "%lx", rp
->magic
);
602 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
603 ERR_add_error_data(2, "magic=0x", magstr
);
606 rsa_modulus
= (unsigned char *)(rp
+ 1);
607 rkey
= RSA_new_method(eng
);
614 if (!rkey
->e
|| !rkey
->n
)
617 if (!BN_set_word(rkey
->e
, rp
->pubexp
))
620 rsa_modlen
= rp
->bitlen
/ 8;
621 if (!lend_tobn(rkey
->n
, rsa_modulus
, rsa_modlen
))
624 RSA_set_ex_data(rkey
, rsa_capi_idx
, key
);
626 if (!(ret
= EVP_PKEY_new()))
629 EVP_PKEY_assign_RSA(ret
, rkey
);
633 else if (bh
->aiKeyAlg
== CALG_DSS_SIGN
)
638 dp
= (DSSPUBKEY
*)(bh
+ 1);
639 if (dp
->magic
!= 0x31535344)
642 BIO_snprintf(magstr
, 10, "%lx", dp
->magic
);
643 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
644 ERR_add_error_data(2, "magic=0x", magstr
);
647 dsa_plen
= dp
->bitlen
/ 8;
648 btmp
= (unsigned char *)(dp
+ 1);
649 dkey
= DSA_new_method(eng
);
655 dkey
->pub_key
= BN_new();
656 if (!dkey
->p
|| !dkey
->q
|| !dkey
->g
|| !dkey
->pub_key
)
658 if (!lend_tobn(dkey
->p
, btmp
, dsa_plen
))
661 if (!lend_tobn(dkey
->q
, btmp
, 20))
664 if (!lend_tobn(dkey
->g
, btmp
, dsa_plen
))
667 if (!lend_tobn(dkey
->pub_key
, btmp
, dsa_plen
))
671 DSA_set_ex_data(dkey
, dsa_capi_idx
, key
);
673 if (!(ret
= EVP_PKEY_new()))
676 EVP_PKEY_assign_DSA(ret
, dkey
);
682 BIO_snprintf(algstr
, 10, "%lx", bh
->aiKeyAlg
);
683 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM
);
684 ERR_add_error_data(2, "aiKeyAlg=0x", algstr
);
691 OPENSSL_free(pubkey
);
703 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY
, ERR_R_MALLOC_FAILURE
);
708 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
709 UI_METHOD
*ui_method
, void *callback_data
)
714 ctx
= ENGINE_get_ex_data(eng
, capi_idx
);
718 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY
, CAPI_R_CANT_FIND_CAPI_CONTEXT
);
722 key
= capi_find_key(ctx
, key_id
);
727 ret
= capi_get_pkey(eng
, key
);
735 /* CryptoAPI RSA operations */
737 int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
738 unsigned char *to
, RSA
*rsa
, int padding
)
740 CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC
, CAPI_R_FUNCTION_NOT_SUPPORTED
);
744 int capi_rsa_sign(int dtype
, const unsigned char *m
, unsigned int m_len
,
745 unsigned char *sigret
, unsigned int *siglen
, const RSA
*rsa
)
755 ctx
= ENGINE_get_ex_data(rsa
->engine
, capi_idx
);
757 CAPI_trace(ctx
, "Called CAPI_rsa_sign()\n");
759 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
762 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_GET_KEY
);
765 /* Convert the signature type to a CryptoAPI algorithm ID */
777 alg
= CALG_SSL3_SHAMD5
;
782 BIO_snprintf(algstr
, 10, "%lx", dtype
);
783 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_UNSUPPORTED_ALGORITHM_NID
);
784 ERR_add_error_data(2, "NID=0x", algstr
);
791 /* Create the hash object */
792 if(!CryptCreateHash(capi_key
->hprov
, alg
, 0, 0, &hash
))
794 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
798 /* Set the hash value to the value passed */
800 if(!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)m
, 0))
802 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
808 /* Finally sign it */
809 slen
= RSA_size(rsa
);
810 if(!CryptSignHash(hash
, capi_key
->keyspec
, NULL
, 0, sigret
, &slen
))
812 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
819 /* Inplace byte reversal of signature */
820 for(i
= 0; i
< slen
/ 2; i
++)
824 sigret
[i
] = sigret
[slen
- i
- 1];
825 sigret
[slen
- i
- 1] = c
;
833 CryptDestroyHash(hash
);
838 int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
839 unsigned char *to
, RSA
*rsa
, int padding
)
842 unsigned char *tmpbuf
;
845 ctx
= ENGINE_get_ex_data(rsa
->engine
, capi_idx
);
847 CAPI_trace(ctx
, "Called capi_rsa_priv_dec()\n");
850 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
853 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_CANT_GET_KEY
);
857 if(padding
!= RSA_PKCS1_PADDING
)
860 BIO_snprintf(errstr
, 10, "%d", padding
);
861 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_UNSUPPORTED_PADDING
);
862 ERR_add_error_data(2, "padding=", errstr
);
866 /* Create temp reverse order version of input */
867 if(!(tmpbuf
= OPENSSL_malloc(flen
)) )
869 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, ERR_R_MALLOC_FAILURE
);
872 for(i
= 0; i
< flen
; i
++)
873 tmpbuf
[flen
- i
- 1] = from
[i
];
875 /* Finally decrypt it */
876 if(!CryptDecrypt(capi_key
->key
, 0, TRUE
, 0, tmpbuf
, &flen
))
878 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_DECRYPT_ERROR
);
880 OPENSSL_free(tmpbuf
);
883 else memcpy(to
, tmpbuf
, flen
);
885 OPENSSL_free(tmpbuf
);
890 static int capi_rsa_free(RSA
*rsa
)
893 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
894 capi_free_key(capi_key
);
895 RSA_set_ex_data(rsa
, rsa_capi_idx
, 0);
899 /* CryptoAPI DSA operations */
901 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
909 unsigned char csigbuf
[40];
911 ctx
= ENGINE_get_ex_data(dsa
->engine
, capi_idx
);
913 CAPI_trace(ctx
, "Called CAPI_dsa_do_sign()\n");
915 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
919 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_GET_KEY
);
925 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_INVALID_DIGEST_LENGTH
);
929 /* Create the hash object */
930 if(!CryptCreateHash(capi_key
->hprov
, CALG_SHA1
, 0, 0, &hash
))
932 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
937 /* Set the hash value to the value passed */
938 if(!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)digest
, 0))
940 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
946 /* Finally sign it */
947 slen
= sizeof(csigbuf
);
948 if(!CryptSignHash(hash
, capi_key
->keyspec
, NULL
, 0, csigbuf
, &slen
))
950 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
961 if (!ret
->r
|| !ret
->s
)
963 if (!lend_tobn(ret
->r
, csigbuf
, 20)
964 || !lend_tobn(ret
->s
, csigbuf
+ 20, 20))
975 OPENSSL_cleanse(csigbuf
, 40);
976 CryptDestroyHash(hash
);
980 static int capi_dsa_free(DSA
*dsa
)
983 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
984 capi_free_key(capi_key
);
985 DSA_set_ex_data(dsa
, dsa_capi_idx
, 0);
989 static void capi_vtrace(CAPI_CTX
*ctx
, int level
, char *format
, va_list argptr
)
993 if (!ctx
|| (ctx
->debug_level
< level
) || (!ctx
->debug_file
))
995 out
= BIO_new_file(ctx
->debug_file
, "a+");
996 BIO_vprintf(out
, format
, argptr
);
1000 static void CAPI_trace(CAPI_CTX
*ctx
, char *format
, ...)
1003 va_start(args
, format
);
1004 capi_vtrace(ctx
, CAPI_DBG_TRACE
, format
, args
);
1008 static void capi_addlasterror(void)
1010 capi_adderror(GetLastError());
1013 static void capi_adderror(DWORD err
)
1016 BIO_snprintf(errstr
, 10, "%lX", err
);
1017 ERR_add_error_data(2, "Error code= 0x", errstr
);
1020 static char *wide_to_asc(LPWSTR wstr
)
1025 str
= OPENSSL_malloc(wcslen(wstr
) + 1);
1028 CAPIerr(CAPI_F_WIDE_TO_ASC
, ERR_R_MALLOC_FAILURE
);
1031 sprintf(str
, "%S", wstr
);
1035 static int capi_get_provname(CAPI_CTX
*ctx
, LPSTR
*pname
, DWORD
*ptype
, DWORD idx
)
1039 CAPI_trace(ctx
, "capi_get_provname, index=%d\n", idx
);
1040 if (!CryptEnumProviders(idx
, NULL
, 0, ptype
, NULL
, &len
))
1042 err
= GetLastError();
1043 if (err
== ERROR_NO_MORE_ITEMS
)
1045 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1049 name
= OPENSSL_malloc(len
);
1050 if (!CryptEnumProviders(idx
, NULL
, 0, ptype
, name
, &len
))
1052 err
= GetLastError();
1053 if (err
== ERROR_NO_MORE_ITEMS
)
1055 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1060 CAPI_trace(ctx
, "capi_get_provname, returned name=%s, type=%d\n", name
, *ptype
);
1065 static int capi_list_providers(CAPI_CTX
*ctx
, BIO
*out
)
1069 LPTSTR provname
= NULL
;
1070 CAPI_trace(ctx
, "capi_list_providers\n");
1071 BIO_printf(out
, "Available CSPs:\n");
1072 for(idx
= 0; ; idx
++)
1074 ret
= capi_get_provname(ctx
, &provname
, &ptype
, idx
);
1079 BIO_printf(out
, "%d. %s, type %d\n", idx
, provname
, ptype
);
1080 OPENSSL_free(provname
);
1085 static int capi_list_containers(CAPI_CTX
*ctx
, BIO
*out
)
1089 DWORD err
, idx
, flags
, buflen
= 0, clen
;
1091 CAPI_trace(ctx
, "Listing containers CSP=%s, type = %d\n", ctx
->cspname
, ctx
->csptype
);
1092 if (!CryptAcquireContext(&hprov
, NULL
, ctx
->cspname
, ctx
->csptype
, CRYPT_VERIFYCONTEXT
))
1094 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1095 capi_addlasterror();
1098 if (!CryptGetProvParam(hprov
, PP_ENUMCONTAINERS
, NULL
, &buflen
, CRYPT_FIRST
))
1100 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1101 capi_addlasterror();
1104 CAPI_trace(ctx
, "Got max container len %d\n", buflen
);
1107 cname
= OPENSSL_malloc(buflen
);
1110 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, ERR_R_MALLOC_FAILURE
);
1114 for (idx
= 0;;idx
++)
1120 flags
= CRYPT_FIRST
;
1123 if(!CryptGetProvParam(hprov
, PP_ENUMCONTAINERS
, cname
, &clen
, flags
))
1125 err
= GetLastError();
1126 if (err
== ERROR_NO_MORE_ITEMS
)
1128 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1132 CAPI_trace(ctx
, "Container name %s, len=%d, index=%d, flags=%d\n", cname
, clen
, idx
, flags
);
1133 if (!cname
[0] && (clen
== buflen
))
1135 CAPI_trace(ctx
, "Enumerate bug: using workaround\n");
1138 BIO_printf(out
, "%d. %s\n", idx
, cname
);
1146 OPENSSL_free(cname
);
1147 CryptReleaseContext(hprov
, 0);
1152 CRYPT_KEY_PROV_INFO
*capi_get_prov_info(CAPI_CTX
*ctx
, PCCERT_CONTEXT cert
)
1155 CRYPT_KEY_PROV_INFO
*pinfo
;
1157 if(!CertGetCertificateContextProperty(cert
, CERT_KEY_PROV_INFO_PROP_ID
, NULL
, &len
))
1159 pinfo
= OPENSSL_malloc(len
);
1162 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
, ERR_R_MALLOC_FAILURE
);
1165 if(!CertGetCertificateContextProperty(cert
, CERT_KEY_PROV_INFO_PROP_ID
, pinfo
, &len
))
1167 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
, CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO
);
1168 capi_addlasterror();
1169 OPENSSL_free(pinfo
);
1175 static void capi_dump_prov_info(CAPI_CTX
*ctx
, BIO
*out
, CRYPT_KEY_PROV_INFO
*pinfo
)
1177 char *provname
= NULL
, *contname
= NULL
;
1180 BIO_printf(out
, " No Private Key\n");
1183 provname
= wide_to_asc(pinfo
->pwszProvName
);
1184 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1185 if (!provname
|| !contname
)
1188 BIO_printf(out
, " Private Key Info:\n");
1189 BIO_printf(out
, " Provider Name: %s, Provider Type %d\n", provname
, pinfo
->dwProvType
);
1190 BIO_printf(out
, " Container Name: %s, Key Type %d\n", contname
, pinfo
->dwKeySpec
);
1193 OPENSSL_free(provname
);
1195 OPENSSL_free(contname
);
1198 char * capi_cert_get_fname(CAPI_CTX
*ctx
, PCCERT_CONTEXT cert
)
1203 CAPI_trace(ctx
, "capi_cert_get_fname\n");
1204 if (!CertGetCertificateContextProperty(cert
, CERT_FRIENDLY_NAME_PROP_ID
, NULL
, &dlen
))
1206 wfname
= OPENSSL_malloc(dlen
);
1207 if (CertGetCertificateContextProperty(cert
, CERT_FRIENDLY_NAME_PROP_ID
, wfname
, &dlen
))
1209 char *fname
= wide_to_asc(wfname
);
1210 OPENSSL_free(wfname
);
1213 CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME
, CAPI_R_ERROR_GETTING_FRIENDLY_NAME
);
1214 capi_addlasterror();
1216 OPENSSL_free(wfname
);
1221 void capi_dump_cert(CAPI_CTX
*ctx
, BIO
*out
, PCCERT_CONTEXT cert
)
1225 unsigned long flags
= ctx
->dump_flags
;
1226 if (flags
& CAPI_DMP_FNAME
)
1229 fname
= capi_cert_get_fname(ctx
, cert
);
1232 BIO_printf(out
, " Friendly Name \"%s\"\n", fname
);
1233 OPENSSL_free(fname
);
1236 BIO_printf(out
, " <No Friendly Name>\n");
1239 p
= cert
->pbCertEncoded
;
1240 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1242 BIO_printf(out
, " <Can't parse certificate>\n");
1243 if (flags
& CAPI_DMP_SUMMARY
)
1245 BIO_printf(out
, " Subject: ");
1246 X509_NAME_print_ex(out
, X509_get_subject_name(x
), 0, XN_FLAG_ONELINE
);
1247 BIO_printf(out
, "\n Issuer: ");
1248 X509_NAME_print_ex(out
, X509_get_issuer_name(x
), 0, XN_FLAG_ONELINE
);
1249 BIO_printf(out
, "\n");
1251 if (flags
& CAPI_DMP_FULL
)
1252 X509_print_ex(out
, x
, XN_FLAG_ONELINE
,0);
1254 if (flags
& CAPI_DMP_PKEYINFO
)
1256 CRYPT_KEY_PROV_INFO
*pinfo
;
1257 pinfo
= capi_get_prov_info(ctx
, cert
);
1258 capi_dump_prov_info(ctx
, out
, pinfo
);
1260 OPENSSL_free(pinfo
);
1263 if (flags
& CAPI_DMP_PEM
)
1264 PEM_write_bio_X509(out
, x
);
1268 HCERTSTORE
capi_open_store(CAPI_CTX
*ctx
, char *storename
)
1273 storename
= ctx
->storename
;
1276 CAPI_trace(ctx
, "Opening certificate store %s\n", storename
);
1278 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_A
, 0, 0,
1279 ctx
->store_flags
, storename
);
1282 CAPIerr(CAPI_F_CAPI_OPEN_STORE
, CAPI_R_ERROR_OPENING_STORE
);
1283 capi_addlasterror();
1288 int capi_list_certs(CAPI_CTX
*ctx
, BIO
*out
, char *id
)
1294 PCCERT_CONTEXT cert
= NULL
;
1296 storename
= ctx
->storename
;
1299 CAPI_trace(ctx
, "Listing certs for store %s\n", storename
);
1301 hstore
= capi_open_store(ctx
, storename
);
1306 cert
= capi_find_cert(ctx
, id
, hstore
);
1312 capi_dump_cert(ctx
, out
, cert
);
1313 CertFreeCertificateContext(cert
);
1319 LPWSTR fname
= NULL
;
1320 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1323 BIO_printf(out
, "Certificate %d\n", idx
);
1324 capi_dump_cert(ctx
, out
, cert
);
1328 CertCloseStore(hstore
, 0);
1332 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
*ctx
, const char *id
, HCERTSTORE hstore
)
1334 PCCERT_CONTEXT cert
= NULL
;
1337 switch(ctx
->lookup_method
)
1339 case CAPI_LU_SUBSTR
:
1340 return CertFindCertificateInStore(hstore
,
1341 X509_ASN_ENCODING
, 0,
1342 CERT_FIND_SUBJECT_STR_A
, id
, NULL
);
1346 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1349 fname
= capi_cert_get_fname(ctx
, cert
);
1352 if (strcmp(fname
, id
))
1356 OPENSSL_free(fname
);
1366 static CAPI_KEY
*capi_get_key(CAPI_CTX
*ctx
, const char *contname
, char *provname
, DWORD ptype
, DWORD keyspec
)
1369 key
= OPENSSL_malloc(sizeof(CAPI_KEY
));
1370 contname
, provname
, ptype
);
1371 CAPI_trace(ctx
, "capi_get_key, contname=%s, provname=%s, type=%d\n",
1372 contname
, provname
, ptype
);
1373 if (!CryptAcquireContext(&key
->hprov
, contname
, provname
, ptype
, 0))
1375 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1376 capi_addlasterror();
1379 if (!CryptGetUserKey(key
->hprov
, keyspec
, &key
->key
))
1381 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_GETUSERKEY_ERROR
);
1382 capi_addlasterror();
1383 CryptReleaseContext(key
->hprov
, 0);
1386 key
->keyspec
= keyspec
;
1395 static CAPI_KEY
*capi_get_cert_key(CAPI_CTX
*ctx
, PCCERT_CONTEXT cert
)
1397 CAPI_KEY
*key
= NULL
;
1398 CRYPT_KEY_PROV_INFO
*pinfo
= NULL
;
1399 char *provname
= NULL
, *contname
= NULL
;
1400 pinfo
= capi_get_prov_info(ctx
, cert
);
1403 provname
= wide_to_asc(pinfo
->pwszProvName
);
1404 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1405 if (!provname
|| !contname
)
1407 key
= capi_get_key(ctx
, contname
, provname
,
1408 pinfo
->dwProvType
, pinfo
->dwKeySpec
);
1412 OPENSSL_free(pinfo
);
1414 OPENSSL_free(provname
);
1416 OPENSSL_free(contname
);
1420 CAPI_KEY
*capi_find_key(CAPI_CTX
*ctx
, const char *id
)
1422 PCCERT_CONTEXT cert
;
1424 CAPI_KEY
*key
= NULL
;
1425 switch (ctx
->lookup_method
)
1427 case CAPI_LU_SUBSTR
:
1429 hstore
= capi_open_store(ctx
, NULL
);
1432 cert
= capi_find_cert(ctx
, id
, hstore
);
1435 key
= capi_get_cert_key(ctx
, cert
);
1436 CertFreeCertificateContext(cert
);
1438 CertCloseStore(hstore
, 0);
1441 case CAPI_LU_CONTNAME
:
1442 key
= capi_get_key(ctx
, id
, ctx
->cspname
, ctx
->csptype
,
1450 void capi_free_key(CAPI_KEY
*key
)
1454 CryptDestroyKey(key
->key
);
1455 CryptReleaseContext(key
->hprov
, 0);
1457 CertFreeCertificateContext(key
->pcert
);
1462 /* Initialize a CAPI_CTX structure */
1464 static CAPI_CTX
*capi_ctx_new()
1467 ctx
= OPENSSL_malloc(sizeof(CAPI_CTX
));
1470 CAPIerr(CAPI_F_CAPI_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
1473 ctx
->cspname
= NULL
;
1474 ctx
->csptype
= PROV_RSA_FULL
;
1475 ctx
->dump_flags
= CAPI_DMP_SUMMARY
|CAPI_DMP_FNAME
;
1476 ctx
->keytype
= AT_KEYEXCHANGE
;
1477 ctx
->storename
= NULL
;
1478 ctx
->ssl_client_store
= NULL
;
1479 ctx
->store_flags
= CERT_STORE_OPEN_EXISTING_FLAG
|
1480 CERT_STORE_READONLY_FLAG
|
1481 CERT_SYSTEM_STORE_CURRENT_USER
;
1482 ctx
->lookup_method
= CAPI_LU_SUBSTR
;
1483 ctx
->debug_level
= 0;
1484 ctx
->debug_file
= NULL
;
1485 ctx
->client_cert_select
= cert_select_simple
;
1489 static void capi_ctx_free(CAPI_CTX
*ctx
)
1491 CAPI_trace(ctx
, "Calling capi_ctx_free with %lx\n", ctx
);
1495 OPENSSL_free(ctx
->cspname
);
1496 if (ctx
->debug_file
)
1497 OPENSSL_free(ctx
->debug_file
);
1499 OPENSSL_free(ctx
->storename
);
1500 if (ctx
->ssl_client_store
)
1501 OPENSSL_free(ctx
->ssl_client_store
);
1505 static int capi_ctx_set_provname(CAPI_CTX
*ctx
, LPSTR pname
, DWORD type
, int check
)
1507 CAPI_trace(ctx
, "capi_ctx_set_provname, name=%s, type=%d\n", pname
, type
);
1511 if (!CryptAcquireContext(&hprov
, NULL
, pname
, type
,
1512 CRYPT_VERIFYCONTEXT
))
1514 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME
, CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1515 capi_addlasterror();
1518 CryptReleaseContext(hprov
, 0);
1520 ctx
->cspname
= BUF_strdup(pname
);
1521 ctx
->csptype
= type
;
1525 static int capi_ctx_set_provname_idx(CAPI_CTX
*ctx
, int idx
)
1529 if (capi_get_provname(ctx
, &pname
, &type
, idx
) != 1)
1531 return capi_ctx_set_provname(ctx
, pname
, type
, 0);
1534 static int cert_issuer_match(STACK_OF(X509_NAME
) *ca_dn
, X509
*x
)
1538 /* Special case: empty list: match anything */
1539 if (sk_X509_NAME_num(ca_dn
) <= 0)
1541 for (i
= 0; i
< sk_X509_NAME_num(ca_dn
); i
++)
1543 nm
= sk_X509_NAME_value(ca_dn
, i
);
1544 if (!X509_NAME_cmp(nm
, X509_get_issuer_name(x
)))
1552 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
1553 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
, EVP_PKEY
**pkey
,
1554 STACK_OF(X509
) **pother
, UI_METHOD
*ui_method
, void *callback_data
)
1556 STACK_OF(X509
) *certs
= NULL
;
1560 int i
, client_cert_idx
;
1562 PCCERT_CONTEXT cert
= NULL
, excert
= NULL
;
1565 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1570 storename
= ctx
->ssl_client_store
;
1574 hstore
= capi_open_store(ctx
, storename
);
1577 /* Enumerate all certificates collect any matches */
1580 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1583 p
= cert
->pbCertEncoded
;
1584 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1587 CAPI_trace(ctx
, "Can't Parse Certificate %d\n", i
);
1590 if (cert_issuer_match(ca_dn
, x
))
1592 key
= capi_get_cert_key(ctx
, cert
);
1595 /* Match found: attach extra data to it so
1596 * we can retrieve the key later.
1598 excert
= CertDuplicateCertificateContext(cert
);
1599 key
->pcert
= excert
;
1600 X509_set_ex_data(x
, cert_capi_idx
, key
);
1603 certs
= sk_X509_new_null();
1605 sk_X509_push(certs
, x
);
1613 CertFreeCertificateContext(cert
);
1615 CertCloseStore(hstore
, 0);
1621 /* Select the appropriate certificate */
1623 client_cert_idx
= ctx
->client_cert_select(e
, ssl
, certs
);
1625 /* Set the selected certificate and free the rest */
1627 for(i
= 0; i
< sk_X509_num(certs
); i
++)
1629 x
= sk_X509_value(certs
, i
);
1630 if (i
== client_cert_idx
)
1634 key
= X509_get_ex_data(x
, cert_capi_idx
);
1640 sk_X509_free(certs
);
1645 /* Setup key for selected certificate */
1647 key
= X509_get_ex_data(*pcert
, cert_capi_idx
);
1648 *pkey
= capi_get_pkey(e
, key
);
1649 X509_set_ex_data(*pcert
, cert_capi_idx
, NULL
);
1656 /* Simple client cert selection function: always select first */
1658 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1663 #ifdef OPENSSL_CAPIENG_DIALOG
1665 /* More complex cert selection function, using standard function
1666 * CryptUIDlgSelectCertificateFromStore() to produce a dialog box.
1670 #include <cryptuiapi.h>
1672 #define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
1673 #define dlg_prompt L"Select a certificate to use for authentication"
1674 #define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
1675 |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
1677 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1681 PCCERT_CONTEXT cert
;
1686 if (sk_X509_num(certs
) == 1)
1688 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1689 /* Create an in memory store of certificates */
1690 dstore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1691 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1694 CAPIerr(CAPI_F_CLIENT_CERT_SELECT
, CAPI_R_ERROR_CREATING_STORE
);
1695 capi_addlasterror();
1698 /* Add all certificates to store */
1699 for(i
= 0; i
< sk_X509_num(certs
); i
++)
1701 x
= sk_X509_value(certs
, i
);
1702 key
= X509_get_ex_data(x
, cert_capi_idx
);
1704 if (!CertAddCertificateContextToStore(dstore
, key
->pcert
,
1705 CERT_STORE_ADD_NEW
, NULL
))
1707 CAPIerr(CAPI_F_CLIENT_CERT_SELECT
, CAPI_R_ERROR_ADDING_CERT
);
1708 capi_addlasterror();
1713 hwnd
= GetActiveWindow();
1714 if (!hwnd
&& ctx
->getconswindow
)
1715 hwnd
= ctx
->getconswindow();
1716 /* Call dialog to select one */
1717 cert
= ctx
->certselectdlg(dstore
, hwnd
, dlg_title
, dlg_prompt
,
1718 dlg_columns
, 0, NULL
);
1720 /* Find matching cert from list */
1723 for(i
= 0; i
< sk_X509_num(certs
); i
++)
1725 x
= sk_X509_value(certs
, i
);
1726 key
= X509_get_ex_data(x
, cert_capi_idx
);
1727 if (CertCompareCertificate(
1728 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
1730 key
->pcert
->pCertInfo
))
1740 CertCloseStore(dstore
, 0);