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>
75 * This module uses several "new" interfaces, among which is
76 * CertGetCertificateContextProperty. CERT_KEY_PROV_INFO_PROP_ID is
77 * one of possible values you can pass to function in question. By
78 * checking if it's defined we can see if wincrypt.h and accompanying
79 * crypt32.lib are in shape. The native MingW32 headers up to and
80 * including __W32API_VERSION 3.14 lack of struct DSSPUBKEY and the
81 * defines CERT_STORE_PROV_SYSTEM_A and CERT_STORE_READONLY_FLAG,
82 * so we check for these too and avoid compiling.
83 * Yes, it's rather "weak" test and if compilation fails,
84 * then re-configure with -DOPENSSL_NO_CAPIENG.
86 # if defined(CERT_KEY_PROV_INFO_PROP_ID) && \
87 defined(CERT_STORE_PROV_SYSTEM_A) && \
88 defined(CERT_STORE_READONLY_FLAG)
89 # define __COMPILE_CAPIENG
90 # endif /* CERT_KEY_PROV_INFO_PROP_ID */
91 # endif /* OPENSSL_NO_CAPIENG */
92 #endif /* OPENSSL_SYS_WIN32 */
94 #ifdef __COMPILE_CAPIENG
96 # undef X509_EXTENSIONS
97 # undef X509_CERT_PAIR
99 /* Definitions which may be missing from earlier version of headers */
100 # ifndef CERT_STORE_OPEN_EXISTING_FLAG
101 # define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
104 # ifndef CERT_STORE_CREATE_NEW_FLAG
105 # define CERT_STORE_CREATE_NEW_FLAG 0x00002000
108 # ifndef CERT_SYSTEM_STORE_CURRENT_USER
109 # define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000
112 # include <openssl/engine.h>
113 # include <openssl/pem.h>
114 # include <openssl/x509v3.h>
116 # include "e_capi_err.h"
117 # include "e_capi_err.c"
119 static const char *engine_capi_id
= "capi";
120 static const char *engine_capi_name
= "CryptoAPI ENGINE";
122 typedef struct CAPI_CTX_st CAPI_CTX
;
123 typedef struct CAPI_KEY_st CAPI_KEY
;
125 static void capi_addlasterror(void);
126 static void capi_adderror(DWORD err
);
128 static void CAPI_trace(CAPI_CTX
* ctx
, char *format
, ...);
130 static int capi_list_providers(CAPI_CTX
* ctx
, BIO
*out
);
131 static int capi_list_containers(CAPI_CTX
* ctx
, BIO
*out
);
132 int capi_list_certs(CAPI_CTX
* ctx
, BIO
*out
, char *storename
);
133 void capi_free_key(CAPI_KEY
* key
);
135 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
* ctx
, const char *id
,
138 CAPI_KEY
*capi_find_key(CAPI_CTX
* ctx
, const char *id
);
140 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
141 UI_METHOD
*ui_method
, void *callback_data
);
142 static int capi_rsa_sign(int dtype
, const unsigned char *m
,
143 unsigned int m_len
, unsigned char *sigret
,
144 unsigned int *siglen
, const RSA
*rsa
);
145 static int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
146 unsigned char *to
, RSA
*rsa
, int padding
);
147 static int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
148 unsigned char *to
, RSA
*rsa
, int padding
);
149 static int capi_rsa_free(RSA
*rsa
);
151 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
153 static int capi_dsa_free(DSA
*dsa
);
155 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
156 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
,
157 EVP_PKEY
**pkey
, STACK_OF(X509
) **pother
,
158 UI_METHOD
*ui_method
,
159 void *callback_data
);
161 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
162 # ifdef OPENSSL_CAPIENG_DIALOG
163 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
166 typedef PCCERT_CONTEXT(WINAPI
*CERTDLG
) (HCERTSTORE
, HWND
, LPCWSTR
,
167 LPCWSTR
, DWORD
, DWORD
, void *);
168 typedef HWND(WINAPI
*GETCONSWIN
) (void);
171 * This structure contains CAPI ENGINE specific data: it contains various
172 * global options and affects how other functions behave.
175 # define CAPI_DBG_TRACE 2
176 # define CAPI_DBG_ERROR 1
181 /* Parameters to use for container lookup */
185 /* Certificate store name to use */
187 LPSTR ssl_client_store
;
188 /* System store flags */
190 /* Lookup string meanings in load_private_key */
191 /* Substring of subject: uses "storename" */
192 # define CAPI_LU_SUBSTR 1
193 /* Friendly name: uses storename */
194 # define CAPI_LU_FNAME 2
195 /* Container name: uses cspname, keytype */
196 # define CAPI_LU_CONTNAME 3
198 /* Info to dump with dumpcerts option */
199 /* Issuer and serial name strings */
200 # define CAPI_DMP_SUMMARY 0x1
202 # define CAPI_DMP_FNAME 0x2
203 /* Full X509_print dump */
204 # define CAPI_DMP_FULL 0x4
205 /* Dump PEM format certificate */
206 # define CAPI_DMP_PEM 0x8
207 /* Dump pseudo key (if possible) */
208 # define CAPI_DMP_PSKEY 0x10
209 /* Dump key info (if possible) */
210 # define CAPI_DMP_PKEYINFO 0x20
212 int (*client_cert_select
) (ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
);
213 CERTDLG certselectdlg
;
214 GETCONSWIN getconswindow
;
217 static CAPI_CTX
*capi_ctx_new();
218 static void capi_ctx_free(CAPI_CTX
* ctx
);
219 static int capi_ctx_set_provname(CAPI_CTX
* ctx
, LPSTR pname
, DWORD type
,
221 static int capi_ctx_set_provname_idx(CAPI_CTX
* ctx
, int idx
);
223 # define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
224 # define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
225 # define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
226 # define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
227 # define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
228 # define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
229 # define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
230 # define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
231 # define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
232 # define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
233 # define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
234 # define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
235 # define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
236 # define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13)
238 static const ENGINE_CMD_DEFN capi_cmd_defns
[] = {
239 {CAPI_CMD_LIST_CERTS
,
241 "List all certificates in store",
242 ENGINE_CMD_FLAG_NO_INPUT
},
243 {CAPI_CMD_LOOKUP_CERT
,
245 "Lookup and output certificates",
246 ENGINE_CMD_FLAG_STRING
},
247 {CAPI_CMD_DEBUG_LEVEL
,
249 "debug level (1=errors, 2=trace)",
250 ENGINE_CMD_FLAG_NUMERIC
},
251 {CAPI_CMD_DEBUG_FILE
,
253 "debugging filename)",
254 ENGINE_CMD_FLAG_STRING
},
257 "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
258 ENGINE_CMD_FLAG_NUMERIC
},
262 ENGINE_CMD_FLAG_NO_INPUT
},
263 {CAPI_CMD_SET_CSP_IDX
,
266 ENGINE_CMD_FLAG_NUMERIC
},
267 {CAPI_CMD_SET_CSP_NAME
,
269 "Set CSP name, (default CSP used if not specified)",
270 ENGINE_CMD_FLAG_STRING
},
271 {CAPI_CMD_SET_CSP_TYPE
,
273 "Set CSP type, (default RSA_PROV_FULL)",
274 ENGINE_CMD_FLAG_NUMERIC
},
275 {CAPI_CMD_LIST_CONTAINERS
,
277 "list container names",
278 ENGINE_CMD_FLAG_NO_INPUT
},
279 {CAPI_CMD_LIST_OPTIONS
,
281 "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
282 "32=private key info)",
283 ENGINE_CMD_FLAG_NUMERIC
},
284 {CAPI_CMD_LOOKUP_METHOD
,
286 "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
287 ENGINE_CMD_FLAG_NUMERIC
},
288 {CAPI_CMD_STORE_NAME
,
290 "certificate store name, default \"MY\"",
291 ENGINE_CMD_FLAG_STRING
},
292 {CAPI_CMD_STORE_FLAGS
,
294 "Certificate store flags: 1 = system store",
295 ENGINE_CMD_FLAG_NUMERIC
},
300 static int capi_idx
= -1;
301 static int rsa_capi_idx
= -1;
302 static int dsa_capi_idx
= -1;
303 static int cert_capi_idx
= -1;
305 static int capi_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void))
310 if (capi_idx
== -1) {
311 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_ENGINE_NOT_INITIALIZED
);
314 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
315 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
317 case CAPI_CMD_LIST_CSPS
:
318 ret
= capi_list_providers(ctx
, out
);
321 case CAPI_CMD_LIST_CERTS
:
322 ret
= capi_list_certs(ctx
, out
, NULL
);
325 case CAPI_CMD_LOOKUP_CERT
:
326 ret
= capi_list_certs(ctx
, out
, p
);
329 case CAPI_CMD_LIST_CONTAINERS
:
330 ret
= capi_list_containers(ctx
, out
);
333 case CAPI_CMD_STORE_NAME
:
335 OPENSSL_free(ctx
->storename
);
336 ctx
->storename
= BUF_strdup(p
);
337 CAPI_trace(ctx
, "Setting store name to %s\n", p
);
340 case CAPI_CMD_STORE_FLAGS
:
342 ctx
->store_flags
|= CERT_SYSTEM_STORE_LOCAL_MACHINE
;
343 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_CURRENT_USER
;
345 ctx
->store_flags
|= CERT_SYSTEM_STORE_CURRENT_USER
;
346 ctx
->store_flags
&= ~CERT_SYSTEM_STORE_LOCAL_MACHINE
;
348 CAPI_trace(ctx
, "Setting flags to %d\n", i
);
351 case CAPI_CMD_DEBUG_LEVEL
:
352 ctx
->debug_level
= (int)i
;
353 CAPI_trace(ctx
, "Setting debug level to %d\n", ctx
->debug_level
);
356 case CAPI_CMD_DEBUG_FILE
:
357 ctx
->debug_file
= BUF_strdup(p
);
358 CAPI_trace(ctx
, "Setting debug file to %s\n", ctx
->debug_file
);
361 case CAPI_CMD_KEYTYPE
:
363 CAPI_trace(ctx
, "Setting key type to %d\n", ctx
->keytype
);
366 case CAPI_CMD_SET_CSP_IDX
:
367 ret
= capi_ctx_set_provname_idx(ctx
, i
);
370 case CAPI_CMD_LIST_OPTIONS
:
374 case CAPI_CMD_LOOKUP_METHOD
:
375 if (i
< 1 || i
> 3) {
376 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_INVALID_LOOKUP_METHOD
);
379 ctx
->lookup_method
= i
;
382 case CAPI_CMD_SET_CSP_NAME
:
383 ret
= capi_ctx_set_provname(ctx
, p
, ctx
->csptype
, 1);
386 case CAPI_CMD_SET_CSP_TYPE
:
391 CAPIerr(CAPI_F_CAPI_CTRL
, CAPI_R_UNKNOWN_COMMAND
);
400 static RSA_METHOD capi_rsa_method
= {
401 "CryptoAPI RSA method",
404 capi_rsa_priv_enc
, /* priv_enc */
405 capi_rsa_priv_dec
, /* priv_dec */
409 capi_rsa_free
, /* finish */
410 RSA_FLAG_SIGN_VER
, /* flags */
412 capi_rsa_sign
, /* rsa_sign */
416 static DSA_METHOD capi_dsa_method
= {
417 "CryptoAPI DSA method",
418 capi_dsa_do_sign
, /* dsa_do_sign */
419 0, /* dsa_sign_setup */
420 0, /* dsa_do_verify */
424 capi_dsa_free
, /* finish */
427 0, /* dsa_paramgen */
431 static int capi_init(ENGINE
*e
)
434 const RSA_METHOD
*ossl_rsa_meth
;
435 const DSA_METHOD
*ossl_dsa_meth
;
438 capi_idx
= ENGINE_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
442 cert_capi_idx
= X509_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
444 /* Setup RSA_METHOD */
445 rsa_capi_idx
= RSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
446 ossl_rsa_meth
= RSA_PKCS1_SSLeay();
447 capi_rsa_method
.rsa_pub_enc
= ossl_rsa_meth
->rsa_pub_enc
;
448 capi_rsa_method
.rsa_pub_dec
= ossl_rsa_meth
->rsa_pub_dec
;
449 capi_rsa_method
.rsa_mod_exp
= ossl_rsa_meth
->rsa_mod_exp
;
450 capi_rsa_method
.bn_mod_exp
= ossl_rsa_meth
->bn_mod_exp
;
452 /* Setup DSA Method */
453 dsa_capi_idx
= DSA_get_ex_new_index(0, NULL
, NULL
, NULL
, 0);
454 ossl_dsa_meth
= DSA_OpenSSL();
455 capi_dsa_method
.dsa_do_verify
= ossl_dsa_meth
->dsa_do_verify
;
456 capi_dsa_method
.dsa_mod_exp
= ossl_dsa_meth
->dsa_mod_exp
;
457 capi_dsa_method
.bn_mod_exp
= ossl_dsa_meth
->bn_mod_exp
;
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
;
485 CAPIerr(CAPI_F_CAPI_INIT
, ERR_R_MALLOC_FAILURE
);
491 static int capi_destroy(ENGINE
*e
)
493 ERR_unload_CAPI_strings();
497 static int capi_finish(ENGINE
*e
)
500 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
502 ENGINE_set_ex_data(e
, capi_idx
, NULL
);
507 * CryptoAPI key application data. This contains a handle to the private key
508 * container (for sign operations) and a handle to the key (for decrypt
513 /* Associated certificate context (if any) */
514 PCCERT_CONTEXT pcert
;
520 static int bind_capi(ENGINE
*e
)
522 if (!ENGINE_set_id(e
, engine_capi_id
)
523 || !ENGINE_set_name(e
, engine_capi_name
)
524 || !ENGINE_set_flags(e
, ENGINE_FLAGS_NO_REGISTER_ALL
)
525 || !ENGINE_set_init_function(e
, capi_init
)
526 || !ENGINE_set_finish_function(e
, capi_finish
)
527 || !ENGINE_set_destroy_function(e
, capi_destroy
)
528 || !ENGINE_set_RSA(e
, &capi_rsa_method
)
529 || !ENGINE_set_DSA(e
, &capi_dsa_method
)
530 || !ENGINE_set_load_privkey_function(e
, capi_load_privkey
)
531 || !ENGINE_set_load_ssl_client_cert_function(e
,
532 capi_load_ssl_client_cert
)
533 || !ENGINE_set_cmd_defns(e
, capi_cmd_defns
)
534 || !ENGINE_set_ctrl_function(e
, capi_ctrl
))
536 ERR_load_CAPI_strings();
542 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
543 static int bind_helper(ENGINE
*e
, const char *id
)
545 if (id
&& (strcmp(id
, engine_capi_id
) != 0))
552 IMPLEMENT_DYNAMIC_CHECK_FN()
553 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper
)
555 static ENGINE
*engine_capi(void)
557 ENGINE
*ret
= ENGINE_new();
560 if (!bind_capi(ret
)) {
567 void ENGINE_load_capi(void)
569 /* Copied from eng_[openssl|dyn].c */
570 ENGINE
*toadd
= engine_capi();
579 static int lend_tobn(BIGNUM
*bn
, unsigned char *bin
, int binlen
)
583 * Reverse buffer in place: since this is a keyblob structure that will
584 * be freed up after conversion anyway it doesn't matter if we change
587 for (i
= 0; i
< binlen
/ 2; i
++) {
590 bin
[i
] = bin
[binlen
- i
- 1];
591 bin
[binlen
- i
- 1] = c
;
594 if (!BN_bin2bn(bin
, binlen
, bn
))
599 /* Given a CAPI_KEY get an EVP_PKEY structure */
601 static EVP_PKEY
*capi_get_pkey(ENGINE
*eng
, CAPI_KEY
* key
)
603 unsigned char *pubkey
= NULL
;
608 EVP_PKEY
*ret
= NULL
;
609 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, NULL
, &len
)) {
610 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR
);
615 pubkey
= OPENSSL_malloc(len
);
620 if (!CryptExportKey(key
->key
, 0, PUBLICKEYBLOB
, 0, pubkey
, &len
)) {
621 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_PUBKEY_EXPORT_ERROR
);
626 bh
= (BLOBHEADER
*) pubkey
;
627 if (bh
->bType
!= PUBLICKEYBLOB
) {
628 CAPIerr(CAPI_F_CAPI_GET_PKEY
, CAPI_R_INVALID_PUBLIC_KEY_BLOB
);
631 if (bh
->aiKeyAlg
== CALG_RSA_SIGN
|| bh
->aiKeyAlg
== CALG_RSA_KEYX
) {
634 unsigned char *rsa_modulus
;
635 rp
= (RSAPUBKEY
*) (bh
+ 1);
636 if (rp
->magic
!= 0x31415352) {
638 BIO_snprintf(magstr
, 10, "%lx", rp
->magic
);
639 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
640 CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
641 ERR_add_error_data(2, "magic=0x", magstr
);
644 rsa_modulus
= (unsigned char *)(rp
+ 1);
645 rkey
= RSA_new_method(eng
);
652 if (!rkey
->e
|| !rkey
->n
)
655 if (!BN_set_word(rkey
->e
, rp
->pubexp
))
658 rsa_modlen
= rp
->bitlen
/ 8;
659 if (!lend_tobn(rkey
->n
, rsa_modulus
, rsa_modlen
))
662 RSA_set_ex_data(rkey
, rsa_capi_idx
, key
);
664 if (!(ret
= EVP_PKEY_new()))
667 EVP_PKEY_assign_RSA(ret
, rkey
);
670 } else if (bh
->aiKeyAlg
== CALG_DSS_SIGN
) {
674 dp
= (DSSPUBKEY
*) (bh
+ 1);
675 if (dp
->magic
!= 0x31535344) {
677 BIO_snprintf(magstr
, 10, "%lx", dp
->magic
);
678 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
679 CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER
);
680 ERR_add_error_data(2, "magic=0x", magstr
);
683 dsa_plen
= dp
->bitlen
/ 8;
684 btmp
= (unsigned char *)(dp
+ 1);
685 dkey
= DSA_new_method(eng
);
691 dkey
->pub_key
= BN_new();
692 if (!dkey
->p
|| !dkey
->q
|| !dkey
->g
|| !dkey
->pub_key
)
694 if (!lend_tobn(dkey
->p
, btmp
, dsa_plen
))
697 if (!lend_tobn(dkey
->q
, btmp
, 20))
700 if (!lend_tobn(dkey
->g
, btmp
, dsa_plen
))
703 if (!lend_tobn(dkey
->pub_key
, btmp
, dsa_plen
))
707 DSA_set_ex_data(dkey
, dsa_capi_idx
, key
);
709 if (!(ret
= EVP_PKEY_new()))
712 EVP_PKEY_assign_DSA(ret
, dkey
);
716 BIO_snprintf(algstr
, 10, "%lx", bh
->aiKeyAlg
);
717 CAPIerr(CAPI_F_CAPI_GET_PKEY
,
718 CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM
);
719 ERR_add_error_data(2, "aiKeyAlg=0x", algstr
);
725 OPENSSL_free(pubkey
);
736 CAPIerr(CAPI_F_CAPI_GET_PKEY
, ERR_R_MALLOC_FAILURE
);
741 static EVP_PKEY
*capi_load_privkey(ENGINE
*eng
, const char *key_id
,
742 UI_METHOD
*ui_method
, void *callback_data
)
747 ctx
= ENGINE_get_ex_data(eng
, capi_idx
);
750 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY
, CAPI_R_CANT_FIND_CAPI_CONTEXT
);
754 key
= capi_find_key(ctx
, key_id
);
759 ret
= capi_get_pkey(eng
, key
);
767 /* CryptoAPI RSA operations */
769 int capi_rsa_priv_enc(int flen
, const unsigned char *from
,
770 unsigned char *to
, RSA
*rsa
, int padding
)
772 CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC
, CAPI_R_FUNCTION_NOT_SUPPORTED
);
776 int capi_rsa_sign(int dtype
, const unsigned char *m
, unsigned int m_len
,
777 unsigned char *sigret
, unsigned int *siglen
, const RSA
*rsa
)
787 ctx
= ENGINE_get_ex_data(rsa
->engine
, capi_idx
);
789 CAPI_trace(ctx
, "Called CAPI_rsa_sign()\n");
791 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
793 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_GET_KEY
);
796 /* Convert the signature type to a CryptoAPI algorithm ID */
807 alg
= CALG_SSL3_SHAMD5
;
812 BIO_snprintf(algstr
, 10, "%lx", dtype
);
813 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_UNSUPPORTED_ALGORITHM_NID
);
814 ERR_add_error_data(2, "NID=0x", algstr
);
819 /* Create the hash object */
820 if (!CryptCreateHash(capi_key
->hprov
, alg
, 0, 0, &hash
)) {
821 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
825 /* Set the hash value to the value passed */
827 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)m
, 0)) {
828 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
833 /* Finally sign it */
834 slen
= RSA_size(rsa
);
835 if (!CryptSignHashA(hash
, capi_key
->keyspec
, NULL
, 0, sigret
, &slen
)) {
836 CAPIerr(CAPI_F_CAPI_RSA_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
841 /* Inplace byte reversal of signature */
842 for (i
= 0; i
< slen
/ 2; i
++) {
845 sigret
[i
] = sigret
[slen
- i
- 1];
846 sigret
[slen
- i
- 1] = c
;
854 CryptDestroyHash(hash
);
859 int capi_rsa_priv_dec(int flen
, const unsigned char *from
,
860 unsigned char *to
, RSA
*rsa
, int padding
)
863 unsigned char *tmpbuf
;
866 ctx
= ENGINE_get_ex_data(rsa
->engine
, capi_idx
);
868 CAPI_trace(ctx
, "Called capi_rsa_priv_dec()\n");
870 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
872 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_CANT_GET_KEY
);
876 if (padding
!= RSA_PKCS1_PADDING
) {
878 BIO_snprintf(errstr
, 10, "%d", padding
);
879 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_UNSUPPORTED_PADDING
);
880 ERR_add_error_data(2, "padding=", errstr
);
884 /* Create temp reverse order version of input */
885 if (!(tmpbuf
= OPENSSL_malloc(flen
))) {
886 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, ERR_R_MALLOC_FAILURE
);
889 for (i
= 0; i
< flen
; i
++)
890 tmpbuf
[flen
- i
- 1] = from
[i
];
892 /* Finally decrypt it */
893 if (!CryptDecrypt(capi_key
->key
, 0, TRUE
, 0, tmpbuf
, &flen
)) {
894 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC
, CAPI_R_DECRYPT_ERROR
);
896 OPENSSL_free(tmpbuf
);
899 memcpy(to
, tmpbuf
, flen
);
901 OPENSSL_free(tmpbuf
);
906 static int capi_rsa_free(RSA
*rsa
)
909 capi_key
= RSA_get_ex_data(rsa
, rsa_capi_idx
);
910 capi_free_key(capi_key
);
911 RSA_set_ex_data(rsa
, rsa_capi_idx
, 0);
915 /* CryptoAPI DSA operations */
917 static DSA_SIG
*capi_dsa_do_sign(const unsigned char *digest
, int dlen
,
925 unsigned char csigbuf
[40];
927 ctx
= ENGINE_get_ex_data(dsa
->engine
, capi_idx
);
929 CAPI_trace(ctx
, "Called CAPI_dsa_do_sign()\n");
931 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
934 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_GET_KEY
);
939 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_INVALID_DIGEST_LENGTH
);
943 /* Create the hash object */
944 if (!CryptCreateHash(capi_key
->hprov
, CALG_SHA1
, 0, 0, &hash
)) {
945 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_CREATE_HASH_OBJECT
);
950 /* Set the hash value to the value passed */
951 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (unsigned char *)digest
, 0)) {
952 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_CANT_SET_HASH_VALUE
);
957 /* Finally sign it */
958 slen
= sizeof(csigbuf
);
959 if (!CryptSignHashA(hash
, capi_key
->keyspec
, NULL
, 0, csigbuf
, &slen
)) {
960 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN
, CAPI_R_ERROR_SIGNING_HASH
);
969 if (!ret
->r
|| !ret
->s
)
971 if (!lend_tobn(ret
->r
, csigbuf
, 20)
972 || !lend_tobn(ret
->s
, csigbuf
+ 20, 20)) {
982 OPENSSL_cleanse(csigbuf
, 40);
983 CryptDestroyHash(hash
);
987 static int capi_dsa_free(DSA
*dsa
)
990 capi_key
= DSA_get_ex_data(dsa
, dsa_capi_idx
);
991 capi_free_key(capi_key
);
992 DSA_set_ex_data(dsa
, dsa_capi_idx
, 0);
996 static void capi_vtrace(CAPI_CTX
* ctx
, int level
, char *format
,
1001 if (!ctx
|| (ctx
->debug_level
< level
) || (!ctx
->debug_file
))
1003 out
= BIO_new_file(ctx
->debug_file
, "a+");
1004 BIO_vprintf(out
, format
, argptr
);
1008 static void CAPI_trace(CAPI_CTX
* ctx
, char *format
, ...)
1011 va_start(args
, format
);
1012 capi_vtrace(ctx
, CAPI_DBG_TRACE
, format
, args
);
1016 static void capi_addlasterror(void)
1018 capi_adderror(GetLastError());
1021 static void capi_adderror(DWORD err
)
1024 BIO_snprintf(errstr
, 10, "%lX", err
);
1025 ERR_add_error_data(2, "Error code= 0x", errstr
);
1028 static char *wide_to_asc(LPWSTR wstr
)
1035 len_0
= (int)wcslen(wstr
) + 1; /* WideCharToMultiByte expects int */
1036 sz
= WideCharToMultiByte(CP_ACP
, 0, wstr
, len_0
, NULL
, 0, NULL
, NULL
);
1038 CAPIerr(CAPI_F_WIDE_TO_ASC
, CAPI_R_WIN32_ERROR
);
1041 str
= OPENSSL_malloc(sz
);
1043 CAPIerr(CAPI_F_WIDE_TO_ASC
, ERR_R_MALLOC_FAILURE
);
1046 if (!WideCharToMultiByte(CP_ACP
, 0, wstr
, len_0
, str
, sz
, NULL
, NULL
)) {
1048 CAPIerr(CAPI_F_WIDE_TO_ASC
, CAPI_R_WIN32_ERROR
);
1054 static int capi_get_provname(CAPI_CTX
* ctx
, LPSTR
* pname
, DWORD
* ptype
,
1059 CAPI_trace(ctx
, "capi_get_provname, index=%d\n", idx
);
1060 if (!CryptEnumProvidersA(idx
, NULL
, 0, ptype
, NULL
, &len
)) {
1061 err
= GetLastError();
1062 if (err
== ERROR_NO_MORE_ITEMS
)
1064 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1068 name
= OPENSSL_malloc(len
);
1069 if (!CryptEnumProvidersA(idx
, NULL
, 0, ptype
, name
, &len
)) {
1070 err
= GetLastError();
1071 if (err
== ERROR_NO_MORE_ITEMS
)
1073 CAPIerr(CAPI_F_CAPI_GET_PROVNAME
, CAPI_R_CRYPTENUMPROVIDERS_ERROR
);
1078 CAPI_trace(ctx
, "capi_get_provname, returned name=%s, type=%d\n", name
,
1084 static int capi_list_providers(CAPI_CTX
* ctx
, BIO
*out
)
1088 LPSTR provname
= NULL
;
1089 CAPI_trace(ctx
, "capi_list_providers\n");
1090 BIO_printf(out
, "Available CSPs:\n");
1091 for (idx
= 0;; idx
++) {
1092 ret
= capi_get_provname(ctx
, &provname
, &ptype
, idx
);
1097 BIO_printf(out
, "%d. %s, type %d\n", idx
, provname
, ptype
);
1098 OPENSSL_free(provname
);
1103 static int capi_list_containers(CAPI_CTX
* ctx
, BIO
*out
)
1107 DWORD err
, idx
, flags
, buflen
= 0, clen
;
1109 CAPI_trace(ctx
, "Listing containers CSP=%s, type = %d\n", ctx
->cspname
,
1111 if (!CryptAcquireContextA
1112 (&hprov
, NULL
, ctx
->cspname
, ctx
->csptype
, CRYPT_VERIFYCONTEXT
)) {
1113 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
,
1114 CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1115 capi_addlasterror();
1118 if (!CryptGetProvParam
1119 (hprov
, PP_ENUMCONTAINERS
, NULL
, &buflen
, CRYPT_FIRST
)) {
1120 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1121 capi_addlasterror();
1122 CryptReleaseContext(hprov
, 0);
1125 CAPI_trace(ctx
, "Got max container len %d\n", buflen
);
1128 cname
= OPENSSL_malloc(buflen
);
1130 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, ERR_R_MALLOC_FAILURE
);
1134 for (idx
= 0;; idx
++) {
1139 flags
= CRYPT_FIRST
;
1142 if (!CryptGetProvParam(hprov
, PP_ENUMCONTAINERS
, cname
, &clen
, flags
)) {
1143 err
= GetLastError();
1144 if (err
== ERROR_NO_MORE_ITEMS
)
1146 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS
, CAPI_R_ENUMCONTAINERS_ERROR
);
1150 CAPI_trace(ctx
, "Container name %s, len=%d, index=%d, flags=%d\n",
1151 cname
, clen
, idx
, flags
);
1152 if (!cname
[0] && (clen
== buflen
)) {
1153 CAPI_trace(ctx
, "Enumerate bug: using workaround\n");
1156 BIO_printf(out
, "%d. %s\n", idx
, cname
);
1164 OPENSSL_free(cname
);
1165 CryptReleaseContext(hprov
, 0);
1170 CRYPT_KEY_PROV_INFO
*capi_get_prov_info(CAPI_CTX
* ctx
, PCCERT_CONTEXT cert
)
1173 CRYPT_KEY_PROV_INFO
*pinfo
;
1175 if (!CertGetCertificateContextProperty
1176 (cert
, CERT_KEY_PROV_INFO_PROP_ID
, NULL
, &len
))
1178 pinfo
= OPENSSL_malloc(len
);
1180 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
, ERR_R_MALLOC_FAILURE
);
1183 if (!CertGetCertificateContextProperty
1184 (cert
, CERT_KEY_PROV_INFO_PROP_ID
, pinfo
, &len
)) {
1185 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO
,
1186 CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO
);
1187 capi_addlasterror();
1188 OPENSSL_free(pinfo
);
1194 static void capi_dump_prov_info(CAPI_CTX
* ctx
, BIO
*out
,
1195 CRYPT_KEY_PROV_INFO
* pinfo
)
1197 char *provname
= NULL
, *contname
= NULL
;
1199 BIO_printf(out
, " No Private Key\n");
1202 provname
= wide_to_asc(pinfo
->pwszProvName
);
1203 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1204 if (!provname
|| !contname
)
1207 BIO_printf(out
, " Private Key Info:\n");
1208 BIO_printf(out
, " Provider Name: %s, Provider Type %d\n", provname
,
1210 BIO_printf(out
, " Container Name: %s, Key Type %d\n", contname
,
1214 OPENSSL_free(provname
);
1216 OPENSSL_free(contname
);
1219 char *capi_cert_get_fname(CAPI_CTX
* ctx
, PCCERT_CONTEXT cert
)
1224 CAPI_trace(ctx
, "capi_cert_get_fname\n");
1225 if (!CertGetCertificateContextProperty
1226 (cert
, CERT_FRIENDLY_NAME_PROP_ID
, NULL
, &dlen
))
1228 wfname
= OPENSSL_malloc(dlen
);
1229 if (CertGetCertificateContextProperty
1230 (cert
, CERT_FRIENDLY_NAME_PROP_ID
, wfname
, &dlen
)) {
1231 char *fname
= wide_to_asc(wfname
);
1232 OPENSSL_free(wfname
);
1235 CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME
, CAPI_R_ERROR_GETTING_FRIENDLY_NAME
);
1236 capi_addlasterror();
1238 OPENSSL_free(wfname
);
1242 void capi_dump_cert(CAPI_CTX
* ctx
, BIO
*out
, PCCERT_CONTEXT cert
)
1246 unsigned long flags
= ctx
->dump_flags
;
1247 if (flags
& CAPI_DMP_FNAME
) {
1249 fname
= capi_cert_get_fname(ctx
, cert
);
1251 BIO_printf(out
, " Friendly Name \"%s\"\n", fname
);
1252 OPENSSL_free(fname
);
1254 BIO_printf(out
, " <No Friendly Name>\n");
1257 p
= cert
->pbCertEncoded
;
1258 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1260 BIO_printf(out
, " <Can't parse certificate>\n");
1261 if (flags
& CAPI_DMP_SUMMARY
) {
1262 BIO_printf(out
, " Subject: ");
1263 X509_NAME_print_ex(out
, X509_get_subject_name(x
), 0, XN_FLAG_ONELINE
);
1264 BIO_printf(out
, "\n Issuer: ");
1265 X509_NAME_print_ex(out
, X509_get_issuer_name(x
), 0, XN_FLAG_ONELINE
);
1266 BIO_printf(out
, "\n");
1268 if (flags
& CAPI_DMP_FULL
)
1269 X509_print_ex(out
, x
, XN_FLAG_ONELINE
, 0);
1271 if (flags
& CAPI_DMP_PKEYINFO
) {
1272 CRYPT_KEY_PROV_INFO
*pinfo
;
1273 pinfo
= capi_get_prov_info(ctx
, cert
);
1274 capi_dump_prov_info(ctx
, out
, pinfo
);
1276 OPENSSL_free(pinfo
);
1279 if (flags
& CAPI_DMP_PEM
)
1280 PEM_write_bio_X509(out
, x
);
1284 HCERTSTORE
capi_open_store(CAPI_CTX
* ctx
, char *storename
)
1289 storename
= ctx
->storename
;
1292 CAPI_trace(ctx
, "Opening certificate store %s\n", storename
);
1294 hstore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_A
, 0, 0,
1295 ctx
->store_flags
, storename
);
1297 CAPIerr(CAPI_F_CAPI_OPEN_STORE
, CAPI_R_ERROR_OPENING_STORE
);
1298 capi_addlasterror();
1303 int capi_list_certs(CAPI_CTX
* ctx
, BIO
*out
, char *id
)
1309 PCCERT_CONTEXT cert
= NULL
;
1311 storename
= ctx
->storename
;
1314 CAPI_trace(ctx
, "Listing certs for store %s\n", storename
);
1316 hstore
= capi_open_store(ctx
, storename
);
1320 cert
= capi_find_cert(ctx
, id
, hstore
);
1325 capi_dump_cert(ctx
, out
, cert
);
1326 CertFreeCertificateContext(cert
);
1328 for (idx
= 0;; idx
++) {
1329 LPWSTR fname
= NULL
;
1330 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1333 BIO_printf(out
, "Certificate %d\n", idx
);
1334 capi_dump_cert(ctx
, out
, cert
);
1338 CertCloseStore(hstore
, 0);
1342 static PCCERT_CONTEXT
capi_find_cert(CAPI_CTX
* ctx
, const char *id
,
1345 PCCERT_CONTEXT cert
= NULL
;
1348 switch (ctx
->lookup_method
) {
1349 case CAPI_LU_SUBSTR
:
1350 return CertFindCertificateInStore(hstore
,
1351 X509_ASN_ENCODING
, 0,
1352 CERT_FIND_SUBJECT_STR_A
, id
, NULL
);
1355 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1358 fname
= capi_cert_get_fname(ctx
, cert
);
1360 if (strcmp(fname
, id
))
1364 OPENSSL_free(fname
);
1374 static CAPI_KEY
*capi_get_key(CAPI_CTX
* ctx
, const char *contname
,
1375 char *provname
, DWORD ptype
, DWORD keyspec
)
1379 key
= OPENSSL_malloc(sizeof(CAPI_KEY
));
1380 CAPI_trace(ctx
, "capi_get_key, contname=%s, provname=%s, type=%d\n",
1381 contname
, provname
, ptype
);
1382 if (ctx
->store_flags
& CERT_SYSTEM_STORE_LOCAL_MACHINE
)
1383 dwFlags
= CRYPT_MACHINE_KEYSET
;
1384 if (!CryptAcquireContextA
1385 (&key
->hprov
, contname
, provname
, ptype
, dwFlags
)) {
1386 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1387 capi_addlasterror();
1390 if (!CryptGetUserKey(key
->hprov
, keyspec
, &key
->key
)) {
1391 CAPIerr(CAPI_F_CAPI_GET_KEY
, CAPI_R_GETUSERKEY_ERROR
);
1392 capi_addlasterror();
1393 CryptReleaseContext(key
->hprov
, 0);
1396 key
->keyspec
= keyspec
;
1405 static CAPI_KEY
*capi_get_cert_key(CAPI_CTX
* ctx
, PCCERT_CONTEXT cert
)
1407 CAPI_KEY
*key
= NULL
;
1408 CRYPT_KEY_PROV_INFO
*pinfo
= NULL
;
1409 char *provname
= NULL
, *contname
= NULL
;
1410 pinfo
= capi_get_prov_info(ctx
, cert
);
1413 provname
= wide_to_asc(pinfo
->pwszProvName
);
1414 contname
= wide_to_asc(pinfo
->pwszContainerName
);
1415 if (!provname
|| !contname
)
1417 key
= capi_get_key(ctx
, contname
, provname
,
1418 pinfo
->dwProvType
, pinfo
->dwKeySpec
);
1422 OPENSSL_free(pinfo
);
1424 OPENSSL_free(provname
);
1426 OPENSSL_free(contname
);
1430 CAPI_KEY
*capi_find_key(CAPI_CTX
* ctx
, const char *id
)
1432 PCCERT_CONTEXT cert
;
1434 CAPI_KEY
*key
= NULL
;
1435 switch (ctx
->lookup_method
) {
1436 case CAPI_LU_SUBSTR
:
1438 hstore
= capi_open_store(ctx
, NULL
);
1441 cert
= capi_find_cert(ctx
, id
, hstore
);
1443 key
= capi_get_cert_key(ctx
, cert
);
1444 CertFreeCertificateContext(cert
);
1446 CertCloseStore(hstore
, 0);
1449 case CAPI_LU_CONTNAME
:
1450 key
= capi_get_key(ctx
, id
, ctx
->cspname
, ctx
->csptype
, ctx
->keytype
);
1457 void capi_free_key(CAPI_KEY
* key
)
1461 CryptDestroyKey(key
->key
);
1462 CryptReleaseContext(key
->hprov
, 0);
1464 CertFreeCertificateContext(key
->pcert
);
1468 /* Initialize a CAPI_CTX structure */
1470 static CAPI_CTX
*capi_ctx_new()
1473 ctx
= OPENSSL_malloc(sizeof(CAPI_CTX
));
1475 CAPIerr(CAPI_F_CAPI_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
1478 ctx
->cspname
= NULL
;
1479 ctx
->csptype
= PROV_RSA_FULL
;
1480 ctx
->dump_flags
= CAPI_DMP_SUMMARY
| CAPI_DMP_FNAME
;
1481 ctx
->keytype
= AT_KEYEXCHANGE
;
1482 ctx
->storename
= NULL
;
1483 ctx
->ssl_client_store
= NULL
;
1484 ctx
->store_flags
= CERT_STORE_OPEN_EXISTING_FLAG
|
1485 CERT_STORE_READONLY_FLAG
| CERT_SYSTEM_STORE_CURRENT_USER
;
1486 ctx
->lookup_method
= CAPI_LU_SUBSTR
;
1487 ctx
->debug_level
= 0;
1488 ctx
->debug_file
= NULL
;
1489 ctx
->client_cert_select
= cert_select_simple
;
1493 static void capi_ctx_free(CAPI_CTX
* ctx
)
1495 CAPI_trace(ctx
, "Calling capi_ctx_free with %lx\n", ctx
);
1499 OPENSSL_free(ctx
->cspname
);
1500 if (ctx
->debug_file
)
1501 OPENSSL_free(ctx
->debug_file
);
1503 OPENSSL_free(ctx
->storename
);
1504 if (ctx
->ssl_client_store
)
1505 OPENSSL_free(ctx
->ssl_client_store
);
1509 static int capi_ctx_set_provname(CAPI_CTX
* ctx
, LPSTR pname
, DWORD type
,
1512 CAPI_trace(ctx
, "capi_ctx_set_provname, name=%s, type=%d\n", pname
, type
);
1515 if (!CryptAcquireContextA(&hprov
, NULL
, pname
, type
,
1516 CRYPT_VERIFYCONTEXT
)) {
1517 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME
,
1518 CAPI_R_CRYPTACQUIRECONTEXT_ERROR
);
1519 capi_addlasterror();
1522 CryptReleaseContext(hprov
, 0);
1525 OPENSSL_free(ctx
->cspname
);
1526 ctx
->cspname
= BUF_strdup(pname
);
1527 ctx
->csptype
= type
;
1531 static int capi_ctx_set_provname_idx(CAPI_CTX
* ctx
, int idx
)
1536 if (capi_get_provname(ctx
, &pname
, &type
, idx
) != 1)
1538 res
= capi_ctx_set_provname(ctx
, pname
, type
, 0);
1539 OPENSSL_free(pname
);
1543 static int cert_issuer_match(STACK_OF(X509_NAME
) *ca_dn
, X509
*x
)
1547 /* Special case: empty list: match anything */
1548 if (sk_X509_NAME_num(ca_dn
) <= 0)
1550 for (i
= 0; i
< sk_X509_NAME_num(ca_dn
); i
++) {
1551 nm
= sk_X509_NAME_value(ca_dn
, i
);
1552 if (!X509_NAME_cmp(nm
, X509_get_issuer_name(x
)))
1558 static int capi_load_ssl_client_cert(ENGINE
*e
, SSL
*ssl
,
1559 STACK_OF(X509_NAME
) *ca_dn
, X509
**pcert
,
1560 EVP_PKEY
**pkey
, STACK_OF(X509
) **pother
,
1561 UI_METHOD
*ui_method
,
1562 void *callback_data
)
1564 STACK_OF(X509
) *certs
= NULL
;
1568 int i
, client_cert_idx
;
1570 PCCERT_CONTEXT cert
= NULL
, excert
= NULL
;
1573 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1578 storename
= ctx
->ssl_client_store
;
1582 hstore
= capi_open_store(ctx
, storename
);
1585 /* Enumerate all certificates collect any matches */
1587 cert
= CertEnumCertificatesInStore(hstore
, cert
);
1590 p
= cert
->pbCertEncoded
;
1591 x
= d2i_X509(NULL
, &p
, cert
->cbCertEncoded
);
1593 CAPI_trace(ctx
, "Can't Parse Certificate %d\n", i
);
1596 if (cert_issuer_match(ca_dn
, x
)
1597 && X509_check_purpose(x
, X509_PURPOSE_SSL_CLIENT
, 0)) {
1598 key
= capi_get_cert_key(ctx
, cert
);
1604 * Match found: attach extra data to it so we can retrieve the
1607 excert
= CertDuplicateCertificateContext(cert
);
1608 key
->pcert
= excert
;
1609 X509_set_ex_data(x
, cert_capi_idx
, key
);
1612 certs
= sk_X509_new_null();
1614 sk_X509_push(certs
, x
);
1621 CertFreeCertificateContext(cert
);
1623 CertCloseStore(hstore
, 0);
1628 /* Select the appropriate certificate */
1630 client_cert_idx
= ctx
->client_cert_select(e
, ssl
, certs
);
1632 /* Set the selected certificate and free the rest */
1634 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1635 x
= sk_X509_value(certs
, i
);
1636 if (i
== client_cert_idx
)
1639 key
= X509_get_ex_data(x
, cert_capi_idx
);
1645 sk_X509_free(certs
);
1650 /* Setup key for selected certificate */
1652 key
= X509_get_ex_data(*pcert
, cert_capi_idx
);
1653 *pkey
= capi_get_pkey(e
, key
);
1654 X509_set_ex_data(*pcert
, cert_capi_idx
, NULL
);
1660 /* Simple client cert selection function: always select first */
1662 static int cert_select_simple(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1667 # ifdef OPENSSL_CAPIENG_DIALOG
1670 * More complex cert selection function, using standard function
1671 * CryptUIDlgSelectCertificateFromStore() to produce a dialog box.
1675 * Definitions which are in cryptuiapi.h but this is not present in older
1676 * versions of headers.
1679 # ifndef CRYPTUI_SELECT_LOCATION_COLUMN
1680 # define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010
1681 # define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004
1684 # define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
1685 # define dlg_prompt L"Select a certificate to use for authentication"
1686 # define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
1687 |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
1689 static int cert_select_dialog(ENGINE
*e
, SSL
*ssl
, STACK_OF(X509
) *certs
)
1693 PCCERT_CONTEXT cert
;
1698 if (sk_X509_num(certs
) == 1)
1700 ctx
= ENGINE_get_ex_data(e
, capi_idx
);
1701 /* Create an in memory store of certificates */
1702 dstore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1703 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1705 CAPIerr(CAPI_F_CERT_SELECT_DIALOG
, CAPI_R_ERROR_CREATING_STORE
);
1706 capi_addlasterror();
1709 /* Add all certificates to store */
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
);
1714 if (!CertAddCertificateContextToStore(dstore
, key
->pcert
,
1715 CERT_STORE_ADD_NEW
, NULL
)) {
1716 CAPIerr(CAPI_F_CERT_SELECT_DIALOG
, CAPI_R_ERROR_ADDING_CERT
);
1717 capi_addlasterror();
1722 hwnd
= GetForegroundWindow();
1724 hwnd
= GetActiveWindow();
1725 if (!hwnd
&& ctx
->getconswindow
)
1726 hwnd
= ctx
->getconswindow();
1727 /* Call dialog to select one */
1728 cert
= ctx
->certselectdlg(dstore
, hwnd
, dlg_title
, dlg_prompt
,
1729 dlg_columns
, 0, NULL
);
1731 /* Find matching cert from list */
1733 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1734 x
= sk_X509_value(certs
, i
);
1735 key
= X509_get_ex_data(x
, cert_capi_idx
);
1736 if (CertCompareCertificate
1737 (X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
, cert
->pCertInfo
,
1738 key
->pcert
->pCertInfo
)) {
1747 CertCloseStore(dstore
, 0);
1753 #else /* !__COMPILE_CAPIENG */
1754 # include <openssl/engine.h>
1755 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1757 int bind_engine(ENGINE
*e
, const char *id
, const dynamic_fns
*fns
);
1759 int bind_engine(ENGINE
*e
, const char *id
, const dynamic_fns
*fns
)
1764 IMPLEMENT_DYNAMIC_CHECK_FN()
1766 void ENGINE_load_capi(void)