2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 # define _WIN32_WINNT 0x0400
15 # include <wincrypt.h>
22 # define alloca _alloca
25 # include <openssl/crypto.h>
27 # ifndef OPENSSL_NO_CAPIENG
29 # include <openssl/buffer.h>
30 # include <openssl/bn.h>
31 # include <openssl/rsa.h>
32 # include <openssl/dsa.h>
35 * This module uses several "new" interfaces, among which is
36 * CertGetCertificateContextProperty. CERT_KEY_PROV_INFO_PROP_ID is
37 * one of possible values you can pass to function in question. By
38 * checking if it's defined we can see if wincrypt.h and accompanying
39 * crypt32.lib are in shape. The native MingW32 headers up to and
40 * including __W32API_VERSION 3.14 lack of struct DSSPUBKEY and the
41 * defines CERT_STORE_PROV_SYSTEM_A and CERT_STORE_READONLY_FLAG,
42 * so we check for these too and avoid compiling.
43 * Yes, it's rather "weak" test and if compilation fails,
44 * then re-configure with -DOPENSSL_NO_CAPIENG.
46 # if defined(CERT_KEY_PROV_INFO_PROP_ID) && \
47 defined(CERT_STORE_PROV_SYSTEM_A) && \
48 defined(CERT_STORE_READONLY_FLAG)
49 # define __COMPILE_CAPIENG
50 # endif /* CERT_KEY_PROV_INFO_PROP_ID */
51 # endif /* OPENSSL_NO_CAPIENG */
54 #ifdef __COMPILE_CAPIENG
56 # undef X509_EXTENSIONS
58 /* Definitions which may be missing from earlier version of headers */
59 # ifndef CERT_STORE_OPEN_EXISTING_FLAG
60 # define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
63 # ifndef CERT_STORE_CREATE_NEW_FLAG
64 # define CERT_STORE_CREATE_NEW_FLAG 0x00002000
67 # ifndef CERT_SYSTEM_STORE_CURRENT_USER
68 # define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000
71 # ifndef ALG_SID_SHA_256
72 # define ALG_SID_SHA_256 12
74 # ifndef ALG_SID_SHA_384
75 # define ALG_SID_SHA_384 13
77 # ifndef ALG_SID_SHA_512
78 # define ALG_SID_SHA_512 14
82 # define CALG_SHA_256 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256)
85 # define CALG_SHA_384 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_384)
88 # define CALG_SHA_512 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_512)
92 # define PROV_RSA_AES 24
95 # include <openssl/engine.h>
96 # include <openssl/pem.h>
97 # include <openssl/x509v3.h>
99 # include "e_capi_err.h"
100 # include "e_capi_err.c"
102 static const char *engine_capi_id = "capi";
103 static const char *engine_capi_name = "CryptoAPI ENGINE";
105 typedef struct CAPI_CTX_st CAPI_CTX;
106 typedef struct CAPI_KEY_st CAPI_KEY;
108 static void capi_addlasterror(void);
109 static void capi_adderror(DWORD err);
111 static void CAPI_trace(CAPI_CTX *ctx, char *format, ...);
113 static int capi_list_providers(CAPI_CTX *ctx, BIO *out);
114 static int capi_list_containers(CAPI_CTX *ctx, BIO *out);
115 int capi_list_certs(CAPI_CTX *ctx, BIO *out, char *storename);
116 void capi_free_key(CAPI_KEY *key);
118 static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id,
121 CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id);
123 static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
124 UI_METHOD *ui_method, void *callback_data);
125 static int capi_rsa_sign(int dtype, const unsigned char *m,
126 unsigned int m_len, unsigned char *sigret,
127 unsigned int *siglen, const RSA *rsa);
128 static int capi_rsa_priv_enc(int flen, const unsigned char *from,
129 unsigned char *to, RSA *rsa, int padding);
130 static int capi_rsa_priv_dec(int flen, const unsigned char *from,
131 unsigned char *to, RSA *rsa, int padding);
132 static int capi_rsa_free(RSA *rsa);
134 # ifndef OPENSSL_NO_DSA
135 static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
137 static int capi_dsa_free(DSA *dsa);
140 static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl,
141 STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
142 EVP_PKEY **pkey, STACK_OF(X509) **pother,
143 UI_METHOD *ui_method,
144 void *callback_data);
146 static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs);
147 # ifdef OPENSSL_CAPIENG_DIALOG
148 static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs);
151 void engine_load_capi_int(void);
153 typedef PCCERT_CONTEXT(WINAPI *CERTDLG)(HCERTSTORE, HWND, LPCWSTR,
154 LPCWSTR, DWORD, DWORD, void *);
155 typedef HWND(WINAPI *GETCONSWIN)(void);
158 * This structure contains CAPI ENGINE specific data: it contains various
159 * global options and affects how other functions behave.
162 # define CAPI_DBG_TRACE 2
163 # define CAPI_DBG_ERROR 1
168 /* Parameters to use for container lookup */
172 /* Certificate store name to use */
174 LPSTR ssl_client_store;
175 /* System store flags */
177 /* Lookup string meanings in load_private_key */
178 # define CAPI_LU_SUBSTR 1 /* Substring of subject: uses "storename" */
179 # define CAPI_LU_FNAME 2 /* Friendly name: uses storename */
180 # define CAPI_LU_CONTNAME 3 /* Container name: uses cspname, keytype */
182 /* Info to dump with dumpcerts option */
183 # define CAPI_DMP_SUMMARY 0x1 /* Issuer and serial name strings */
184 # define CAPI_DMP_FNAME 0x2 /* Friendly name */
185 # define CAPI_DMP_FULL 0x4 /* Full X509_print dump */
186 # define CAPI_DMP_PEM 0x8 /* Dump PEM format certificate */
187 # define CAPI_DMP_PSKEY 0x10 /* Dump pseudo key (if possible) */
188 # define CAPI_DMP_PKEYINFO 0x20 /* Dump key info (if possible) */
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(void);
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))
289 if (capi_idx == -1) {
290 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_ENGINE_NOT_INITIALIZED);
293 ctx = ENGINE_get_ex_data(e, capi_idx);
294 out = BIO_new_fp(stdout, BIO_NOCLOSE);
296 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_FILE_OPEN_ERROR);
300 case CAPI_CMD_LIST_CSPS:
301 ret = capi_list_providers(ctx, out);
304 case CAPI_CMD_LIST_CERTS:
305 ret = capi_list_certs(ctx, out, NULL);
308 case CAPI_CMD_LOOKUP_CERT:
309 ret = capi_list_certs(ctx, out, p);
312 case CAPI_CMD_LIST_CONTAINERS:
313 ret = capi_list_containers(ctx, out);
316 case CAPI_CMD_STORE_NAME:
317 tmpstr = OPENSSL_strdup(p);
318 if (tmpstr != NULL) {
319 OPENSSL_free(ctx->storename);
320 ctx->storename = tmpstr;
321 CAPI_trace(ctx, "Setting store name to %s\n", p);
323 CAPIerr(CAPI_F_CAPI_CTRL, ERR_R_MALLOC_FAILURE);
328 case CAPI_CMD_STORE_FLAGS:
330 ctx->store_flags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
331 ctx->store_flags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
333 ctx->store_flags |= CERT_SYSTEM_STORE_CURRENT_USER;
334 ctx->store_flags &= ~CERT_SYSTEM_STORE_LOCAL_MACHINE;
336 CAPI_trace(ctx, "Setting flags to %d\n", i);
339 case CAPI_CMD_DEBUG_LEVEL:
340 ctx->debug_level = (int)i;
341 CAPI_trace(ctx, "Setting debug level to %d\n", ctx->debug_level);
344 case CAPI_CMD_DEBUG_FILE:
345 tmpstr = OPENSSL_strdup(p);
346 if (tmpstr != NULL) {
347 ctx->debug_file = tmpstr;
348 CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file);
350 CAPIerr(CAPI_F_CAPI_CTRL, ERR_R_MALLOC_FAILURE);
355 case CAPI_CMD_KEYTYPE:
357 CAPI_trace(ctx, "Setting key type to %d\n", ctx->keytype);
360 case CAPI_CMD_SET_CSP_IDX:
361 ret = capi_ctx_set_provname_idx(ctx, i);
364 case CAPI_CMD_LIST_OPTIONS:
368 case CAPI_CMD_LOOKUP_METHOD:
369 if (i < 1 || i > 3) {
370 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_INVALID_LOOKUP_METHOD);
374 ctx->lookup_method = i;
377 case CAPI_CMD_SET_CSP_NAME:
378 ret = capi_ctx_set_provname(ctx, p, ctx->csptype, 1);
381 case CAPI_CMD_SET_CSP_TYPE:
386 CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_UNKNOWN_COMMAND);
395 static RSA_METHOD *capi_rsa_method = NULL;
396 # ifndef OPENSSL_NO_DSA
397 static DSA_METHOD *capi_dsa_method = NULL;
400 static int use_aes_csp = 0;
401 static const WCHAR rsa_aes_cspname[] =
402 L"Microsoft Enhanced RSA and AES Cryptographic Provider";
403 static const WCHAR rsa_enh_cspname[] =
404 L"Microsoft Enhanced Cryptographic Provider v1.0";
406 static int capi_init(ENGINE *e)
409 const RSA_METHOD *ossl_rsa_meth;
410 # ifndef OPENSSL_NO_DSA
411 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_OpenSSL();
425 if ( !RSA_meth_set_pub_enc(capi_rsa_method,
426 RSA_meth_get_pub_enc(ossl_rsa_meth))
427 || !RSA_meth_set_pub_dec(capi_rsa_method,
428 RSA_meth_get_pub_dec(ossl_rsa_meth))
429 || !RSA_meth_set_priv_enc(capi_rsa_method, capi_rsa_priv_enc)
430 || !RSA_meth_set_priv_dec(capi_rsa_method, capi_rsa_priv_dec)
431 || !RSA_meth_set_mod_exp(capi_rsa_method,
432 RSA_meth_get_mod_exp(ossl_rsa_meth))
433 || !RSA_meth_set_bn_mod_exp(capi_rsa_method,
434 RSA_meth_get_bn_mod_exp(ossl_rsa_meth))
435 || !RSA_meth_set_finish(capi_rsa_method, capi_rsa_free)
436 || !RSA_meth_set_sign(capi_rsa_method, capi_rsa_sign)) {
440 # ifndef OPENSSL_NO_DSA
441 /* Setup DSA Method */
442 dsa_capi_idx = DSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
443 ossl_dsa_meth = DSA_OpenSSL();
444 if ( !DSA_meth_set_sign(capi_dsa_method, capi_dsa_do_sign)
445 || !DSA_meth_set_verify(capi_dsa_method,
446 DSA_meth_get_verify(ossl_dsa_meth))
447 || !DSA_meth_set_finish(capi_dsa_method, capi_dsa_free)
448 || !DSA_meth_set_mod_exp(capi_dsa_method,
449 DSA_meth_get_mod_exp(ossl_dsa_meth))
450 || !DSA_meth_set_bn_mod_exp(capi_dsa_method,
451 DSA_meth_get_bn_mod_exp(ossl_dsa_meth))) {
457 ctx = capi_ctx_new();
461 ENGINE_set_ex_data(e, capi_idx, ctx);
463 # ifdef OPENSSL_CAPIENG_DIALOG
465 HMODULE cryptui = LoadLibrary(TEXT("CRYPTUI.DLL"));
466 HMODULE kernel = GetModuleHandle(TEXT("KERNEL32.DLL"));
469 (CERTDLG) GetProcAddress(cryptui,
470 "CryptUIDlgSelectCertificateFromStore");
473 (GETCONSWIN) GetProcAddress(kernel, "GetConsoleWindow");
474 if (cryptui && !OPENSSL_isservice())
475 ctx->client_cert_select = cert_select_dialog;
479 /* See if there is RSA+AES CSP */
480 if (CryptAcquireContextW(&hprov, NULL, rsa_aes_cspname, PROV_RSA_AES,
481 CRYPT_VERIFYCONTEXT)) {
483 CryptReleaseContext(hprov, 0);
489 CAPIerr(CAPI_F_CAPI_INIT, ERR_R_MALLOC_FAILURE);
495 static int capi_destroy(ENGINE *e)
497 RSA_meth_free(capi_rsa_method);
498 capi_rsa_method = NULL;
499 # ifndef OPENSSL_NO_DSA
500 DSA_meth_free(capi_dsa_method);
501 capi_dsa_method = NULL;
503 ERR_unload_CAPI_strings();
507 static int capi_finish(ENGINE *e)
510 ctx = ENGINE_get_ex_data(e, capi_idx);
512 ENGINE_set_ex_data(e, capi_idx, NULL);
517 * CryptoAPI key application data. This contains a handle to the private key
518 * container (for sign operations) and a handle to the key (for decrypt
523 /* Associated certificate context (if any) */
524 PCCERT_CONTEXT pcert;
530 static int bind_capi(ENGINE *e)
532 capi_rsa_method = RSA_meth_new("CryptoAPI RSA method", 0);
533 if (capi_rsa_method == NULL)
535 # ifndef OPENSSL_NO_DSA
536 capi_dsa_method = DSA_meth_new("CryptoAPI DSA method", 0);
537 if (capi_dsa_method == NULL)
540 if (!ENGINE_set_id(e, engine_capi_id)
541 || !ENGINE_set_name(e, engine_capi_name)
542 || !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL)
543 || !ENGINE_set_init_function(e, capi_init)
544 || !ENGINE_set_finish_function(e, capi_finish)
545 || !ENGINE_set_destroy_function(e, capi_destroy)
546 || !ENGINE_set_RSA(e, capi_rsa_method)
547 # ifndef OPENSSL_NO_DSA
548 || !ENGINE_set_DSA(e, capi_dsa_method)
550 || !ENGINE_set_load_privkey_function(e, capi_load_privkey)
551 || !ENGINE_set_load_ssl_client_cert_function(e,
552 capi_load_ssl_client_cert)
553 || !ENGINE_set_cmd_defns(e, capi_cmd_defns)
554 || !ENGINE_set_ctrl_function(e, capi_ctrl))
556 ERR_load_CAPI_strings();
560 RSA_meth_free(capi_rsa_method);
561 capi_rsa_method = NULL;
562 # ifndef OPENSSL_NO_DSA
563 DSA_meth_free(capi_dsa_method);
564 capi_dsa_method = NULL;
569 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
570 static int bind_helper(ENGINE *e, const char *id)
572 if (id && (strcmp(id, engine_capi_id) != 0))
579 IMPLEMENT_DYNAMIC_CHECK_FN()
580 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
582 static ENGINE *engine_capi(void)
584 ENGINE *ret = ENGINE_new();
587 if (!bind_capi(ret)) {
594 void engine_load_capi_int(void)
596 /* Copied from eng_[openssl|dyn].c */
597 ENGINE *toadd = engine_capi();
606 static int lend_tobn(BIGNUM *bn, unsigned char *bin, int binlen)
610 * Reverse buffer in place: since this is a keyblob structure that will
611 * be freed up after conversion anyway it doesn't matter if we change
614 for (i = 0; i < binlen / 2; i++) {
617 bin[i] = bin[binlen - i - 1];
618 bin[binlen - i - 1] = c;
621 if (!BN_bin2bn(bin, binlen, bn))
626 /* Given a CAPI_KEY get an EVP_PKEY structure */
628 static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY *key)
630 unsigned char *pubkey = NULL;
635 EVP_PKEY *ret = NULL;
636 if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, NULL, &len)) {
637 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR);
642 pubkey = OPENSSL_malloc(len);
647 if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len)) {
648 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_ERROR);
653 bh = (BLOBHEADER *) pubkey;
654 if (bh->bType != PUBLICKEYBLOB) {
655 CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_PUBLIC_KEY_BLOB);
658 if (bh->aiKeyAlg == CALG_RSA_SIGN || bh->aiKeyAlg == CALG_RSA_KEYX) {
661 BIGNUM *e = NULL, *n = NULL;
662 unsigned char *rsa_modulus;
663 rp = (RSAPUBKEY *) (bh + 1);
664 if (rp->magic != 0x31415352) {
666 BIO_snprintf(magstr, 10, "%lx", rp->magic);
667 CAPIerr(CAPI_F_CAPI_GET_PKEY,
668 CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER);
669 ERR_add_error_data(2, "magic=0x", magstr);
672 rsa_modulus = (unsigned char *)(rp + 1);
673 rkey = RSA_new_method(eng);
680 if (e == NULL || n == NULL) {
686 RSA_set0_key(rkey, n, e, NULL);
688 if (!BN_set_word(e, rp->pubexp))
691 rsa_modlen = rp->bitlen / 8;
692 if (!lend_tobn(n, rsa_modulus, rsa_modlen))
695 RSA_set_ex_data(rkey, rsa_capi_idx, key);
697 if ((ret = EVP_PKEY_new()) == NULL)
700 EVP_PKEY_assign_RSA(ret, rkey);
703 # ifndef OPENSSL_NO_DSA
704 } else if (bh->aiKeyAlg == CALG_DSS_SIGN) {
708 BIGNUM *p, *q, *g, *pub_key;
709 dp = (DSSPUBKEY *) (bh + 1);
710 if (dp->magic != 0x31535344) {
712 BIO_snprintf(magstr, 10, "%lx", dp->magic);
713 CAPIerr(CAPI_F_CAPI_GET_PKEY,
714 CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER);
715 ERR_add_error_data(2, "magic=0x", magstr);
718 dsa_plen = dp->bitlen / 8;
719 btmp = (unsigned char *)(dp + 1);
720 dkey = DSA_new_method(eng);
727 if (p == NULL || q == NULL || g == NULL || pub_key == NULL) {
734 DSA_set0_pqg(dkey, p, q, g);
735 DSA_set0_key(dkey, pub_key, NULL);
736 if (!lend_tobn(p, btmp, dsa_plen))
739 if (!lend_tobn(q, btmp, 20))
742 if (!lend_tobn(g, btmp, dsa_plen))
745 if (!lend_tobn(pub_key, btmp, dsa_plen))
749 DSA_set_ex_data(dkey, dsa_capi_idx, key);
751 if ((ret = EVP_PKEY_new()) == NULL)
754 EVP_PKEY_assign_DSA(ret, dkey);
759 BIO_snprintf(algstr, 10, "%ux", bh->aiKeyAlg);
760 CAPIerr(CAPI_F_CAPI_GET_PKEY,
761 CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM);
762 ERR_add_error_data(2, "aiKeyAlg=0x", algstr);
767 OPENSSL_free(pubkey);
770 # ifndef OPENSSL_NO_DSA
778 CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_MALLOC_FAILURE);
783 static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
784 UI_METHOD *ui_method, void *callback_data)
789 ctx = ENGINE_get_ex_data(eng, capi_idx);
792 CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY, CAPI_R_CANT_FIND_CAPI_CONTEXT);
796 key = capi_find_key(ctx, key_id);
801 ret = capi_get_pkey(eng, key);
809 /* CryptoAPI RSA operations */
811 int capi_rsa_priv_enc(int flen, const unsigned char *from,
812 unsigned char *to, RSA *rsa, int padding)
814 CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC, CAPI_R_FUNCTION_NOT_SUPPORTED);
818 int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len,
819 unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
829 ctx = ENGINE_get_ex_data(RSA_get0_engine(rsa), capi_idx);
831 CAPI_trace(ctx, "Called CAPI_rsa_sign()\n");
833 capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
835 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_GET_KEY);
838 /* Convert the signature type to a CryptoAPI algorithm ID */
861 alg = CALG_SSL3_SHAMD5;
866 BIO_snprintf(algstr, 10, "%x", dtype);
867 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_UNSUPPORTED_ALGORITHM_NID);
868 ERR_add_error_data(2, "NID=0x", algstr);
873 /* Create the hash object */
874 if (!CryptCreateHash(capi_key->hprov, alg, 0, 0, &hash)) {
875 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT);
879 /* Set the hash value to the value passed */
881 if (!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)m, 0)) {
882 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_SET_HASH_VALUE);
887 /* Finally sign it */
888 slen = RSA_size(rsa);
889 if (!CryptSignHash(hash, capi_key->keyspec, NULL, 0, sigret, &slen)) {
890 CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_ERROR_SIGNING_HASH);
895 /* Inplace byte reversal of signature */
896 for (i = 0; i < slen / 2; i++) {
899 sigret[i] = sigret[slen - i - 1];
900 sigret[slen - i - 1] = c;
908 CryptDestroyHash(hash);
913 int capi_rsa_priv_dec(int flen, const unsigned char *from,
914 unsigned char *to, RSA *rsa, int padding)
917 unsigned char *tmpbuf;
925 ctx = ENGINE_get_ex_data(RSA_get0_engine(rsa), capi_idx);
927 CAPI_trace(ctx, "Called capi_rsa_priv_dec()\n");
929 capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
931 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_CANT_GET_KEY);
935 if (padding != RSA_PKCS1_PADDING) {
937 BIO_snprintf(errstr, 10, "%d", padding);
938 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_UNSUPPORTED_PADDING);
939 ERR_add_error_data(2, "padding=", errstr);
943 /* Create temp reverse order version of input */
944 if ((tmpbuf = OPENSSL_malloc(flen)) == NULL) {
945 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, ERR_R_MALLOC_FAILURE);
948 for (i = 0; i < flen; i++)
949 tmpbuf[flen - i - 1] = from[i];
951 /* Finally decrypt it */
953 if (!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &dlen)) {
954 CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_DECRYPT_ERROR);
956 OPENSSL_free(tmpbuf);
959 memcpy(to, tmpbuf, (flen = (int)dlen));
961 OPENSSL_free(tmpbuf);
966 static int capi_rsa_free(RSA *rsa)
969 capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
970 capi_free_key(capi_key);
971 RSA_set_ex_data(rsa, rsa_capi_idx, 0);
975 # ifndef OPENSSL_NO_DSA
976 /* CryptoAPI DSA operations */
978 static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
986 unsigned char csigbuf[40];
988 ctx = ENGINE_get_ex_data(DSA_get0_engine(dsa), capi_idx);
990 CAPI_trace(ctx, "Called CAPI_dsa_do_sign()\n");
992 capi_key = DSA_get_ex_data(dsa, dsa_capi_idx);
995 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_GET_KEY);
1000 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_INVALID_DIGEST_LENGTH);
1004 /* Create the hash object */
1005 if (!CryptCreateHash(capi_key->hprov, CALG_SHA1, 0, 0, &hash)) {
1006 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT);
1007 capi_addlasterror();
1011 /* Set the hash value to the value passed */
1012 if (!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)digest, 0)) {
1013 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_SET_HASH_VALUE);
1014 capi_addlasterror();
1018 /* Finally sign it */
1019 slen = sizeof(csigbuf);
1020 if (!CryptSignHash(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen)) {
1021 CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_ERROR_SIGNING_HASH);
1022 capi_addlasterror();
1025 BIGNUM *r = BN_new(), *s = BN_new();
1027 if (r == NULL || s == NULL
1028 || !lend_tobn(r, csigbuf, 20)
1029 || !lend_tobn(s, csigbuf + 20, 20)
1030 || (ret = DSA_SIG_new()) == NULL) {
1031 BN_free(r); /* BN_free checks for BIGNUM * being NULL */
1035 DSA_SIG_set0(ret, r, s);
1041 OPENSSL_cleanse(csigbuf, 40);
1042 CryptDestroyHash(hash);
1046 static int capi_dsa_free(DSA *dsa)
1049 capi_key = DSA_get_ex_data(dsa, dsa_capi_idx);
1050 capi_free_key(capi_key);
1051 DSA_set_ex_data(dsa, dsa_capi_idx, 0);
1056 static void capi_vtrace(CAPI_CTX *ctx, int level, char *format,
1061 if (!ctx || (ctx->debug_level < level) || (!ctx->debug_file))
1063 out = BIO_new_file(ctx->debug_file, "a+");
1065 CAPIerr(CAPI_F_CAPI_VTRACE, CAPI_R_FILE_OPEN_ERROR);
1068 BIO_vprintf(out, format, argptr);
1072 static void CAPI_trace(CAPI_CTX *ctx, char *format, ...)
1075 va_start(args, format);
1076 capi_vtrace(ctx, CAPI_DBG_TRACE, format, args);
1080 static void capi_addlasterror(void)
1082 capi_adderror(GetLastError());
1085 static void capi_adderror(DWORD err)
1088 BIO_snprintf(errstr, 10, "%lX", err);
1089 ERR_add_error_data(2, "Error code= 0x", errstr);
1092 static char *wide_to_asc(LPCWSTR wstr)
1099 len_0 = (int)wcslen(wstr) + 1; /* WideCharToMultiByte expects int */
1100 sz = WideCharToMultiByte(CP_ACP, 0, wstr, len_0, NULL, 0, NULL, NULL);
1102 CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR);
1105 str = OPENSSL_malloc(sz);
1107 CAPIerr(CAPI_F_WIDE_TO_ASC, ERR_R_MALLOC_FAILURE);
1110 if (!WideCharToMultiByte(CP_ACP, 0, wstr, len_0, str, sz, NULL, NULL)) {
1112 CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR);
1118 static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype,
1123 CAPI_trace(ctx, "capi_get_provname, index=%d\n", idx);
1124 if (!CryptEnumProviders(idx, NULL, 0, ptype, NULL, &len)) {
1125 err = GetLastError();
1126 if (err == ERROR_NO_MORE_ITEMS)
1128 CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR);
1132 name = OPENSSL_malloc(len);
1134 CAPIerr(CAPI_F_CAPI_GET_PROVNAME, ERR_R_MALLOC_FAILURE);
1137 if (!CryptEnumProviders(idx, NULL, 0, ptype, name, &len)) {
1138 err = GetLastError();
1140 if (err == ERROR_NO_MORE_ITEMS)
1142 CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR);
1146 if (sizeof(TCHAR) != sizeof(char)) {
1147 *pname = wide_to_asc((WCHAR *)name);
1152 *pname = (char *)name;
1154 CAPI_trace(ctx, "capi_get_provname, returned name=%s, type=%d\n", *pname,
1160 static int capi_list_providers(CAPI_CTX *ctx, BIO *out)
1164 LPSTR provname = NULL;
1165 CAPI_trace(ctx, "capi_list_providers\n");
1166 BIO_printf(out, "Available CSPs:\n");
1167 for (idx = 0;; idx++) {
1168 ret = capi_get_provname(ctx, &provname, &ptype, idx);
1173 BIO_printf(out, "%lu. %s, type %lu\n", idx, provname, ptype);
1174 OPENSSL_free(provname);
1179 static int capi_list_containers(CAPI_CTX *ctx, BIO *out)
1183 DWORD err, idx, flags, buflen = 0, clen;
1185 LPWSTR cspname = NULL;
1187 CAPI_trace(ctx, "Listing containers CSP=%s, type = %d\n", ctx->cspname,
1189 if (ctx->cspname != NULL) {
1190 if ((clen = MultiByteToWideChar(CP_ACP, 0, ctx->cspname, -1,
1192 cspname = alloca(clen * sizeof(WCHAR));
1193 MultiByteToWideChar(CP_ACP, 0, ctx->cspname, -1, (WCHAR *)cspname,
1196 if (cspname == NULL) {
1197 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE);
1198 capi_addlasterror();
1202 if (!CryptAcquireContextW(&hprov, NULL, cspname, ctx->csptype,
1203 CRYPT_VERIFYCONTEXT)) {
1204 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS,
1205 CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
1206 capi_addlasterror();
1209 if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, NULL, &buflen,
1211 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR);
1212 capi_addlasterror();
1213 CryptReleaseContext(hprov, 0);
1216 CAPI_trace(ctx, "Got max container len %d\n", buflen);
1219 cname = OPENSSL_malloc(buflen);
1220 if (cname == NULL) {
1221 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE);
1225 for (idx = 0;; idx++) {
1230 flags = CRYPT_FIRST;
1233 if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, (BYTE *)cname,
1235 err = GetLastError();
1236 if (err == ERROR_NO_MORE_ITEMS)
1238 CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR);
1242 CAPI_trace(ctx, "Container name %s, len=%d, index=%d, flags=%d\n",
1243 cname, clen, idx, flags);
1244 if (!cname[0] && (clen == buflen)) {
1245 CAPI_trace(ctx, "Enumerate bug: using workaround\n");
1248 BIO_printf(out, "%lu. %s\n", idx, cname);
1255 OPENSSL_free(cname);
1256 CryptReleaseContext(hprov, 0);
1261 static CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX *ctx,
1262 PCCERT_CONTEXT cert)
1265 CRYPT_KEY_PROV_INFO *pinfo;
1267 if (!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID,
1270 pinfo = OPENSSL_malloc(len);
1271 if (pinfo == NULL) {
1272 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, ERR_R_MALLOC_FAILURE);
1275 if (!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID,
1277 CAPIerr(CAPI_F_CAPI_GET_PROV_INFO,
1278 CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO);
1279 capi_addlasterror();
1280 OPENSSL_free(pinfo);
1286 static void capi_dump_prov_info(CAPI_CTX *ctx, BIO *out,
1287 CRYPT_KEY_PROV_INFO *pinfo)
1289 char *provname = NULL, *contname = NULL;
1291 BIO_printf(out, " No Private Key\n");
1294 provname = wide_to_asc(pinfo->pwszProvName);
1295 contname = wide_to_asc(pinfo->pwszContainerName);
1296 if (!provname || !contname)
1299 BIO_printf(out, " Private Key Info:\n");
1300 BIO_printf(out, " Provider Name: %s, Provider Type %lu\n", provname,
1302 BIO_printf(out, " Container Name: %s, Key Type %lu\n", contname,
1305 OPENSSL_free(provname);
1306 OPENSSL_free(contname);
1309 static char *capi_cert_get_fname(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1314 CAPI_trace(ctx, "capi_cert_get_fname\n");
1315 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID,
1318 wfname = OPENSSL_malloc(dlen);
1321 if (CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID,
1323 char *fname = wide_to_asc(wfname);
1324 OPENSSL_free(wfname);
1327 CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME, CAPI_R_ERROR_GETTING_FRIENDLY_NAME);
1328 capi_addlasterror();
1330 OPENSSL_free(wfname);
1334 static void capi_dump_cert(CAPI_CTX *ctx, BIO *out, PCCERT_CONTEXT cert)
1337 const unsigned char *p;
1338 unsigned long flags = ctx->dump_flags;
1339 if (flags & CAPI_DMP_FNAME) {
1341 fname = capi_cert_get_fname(ctx, cert);
1343 BIO_printf(out, " Friendly Name \"%s\"\n", fname);
1344 OPENSSL_free(fname);
1346 BIO_printf(out, " <No Friendly Name>\n");
1350 p = cert->pbCertEncoded;
1351 x = d2i_X509(NULL, &p, cert->cbCertEncoded);
1353 BIO_printf(out, " <Can't parse certificate>\n");
1354 if (flags & CAPI_DMP_SUMMARY) {
1355 BIO_printf(out, " Subject: ");
1356 X509_NAME_print_ex(out, X509_get_subject_name(x), 0, XN_FLAG_ONELINE);
1357 BIO_printf(out, "\n Issuer: ");
1358 X509_NAME_print_ex(out, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE);
1359 BIO_printf(out, "\n");
1361 if (flags & CAPI_DMP_FULL)
1362 X509_print_ex(out, x, XN_FLAG_ONELINE, 0);
1364 if (flags & CAPI_DMP_PKEYINFO) {
1365 CRYPT_KEY_PROV_INFO *pinfo;
1366 pinfo = capi_get_prov_info(ctx, cert);
1367 capi_dump_prov_info(ctx, out, pinfo);
1368 OPENSSL_free(pinfo);
1371 if (flags & CAPI_DMP_PEM)
1372 PEM_write_bio_X509(out, x);
1376 static HCERTSTORE capi_open_store(CAPI_CTX *ctx, char *storename)
1381 storename = ctx->storename;
1384 CAPI_trace(ctx, "Opening certificate store %s\n", storename);
1386 hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, 0,
1387 ctx->store_flags, storename);
1389 CAPIerr(CAPI_F_CAPI_OPEN_STORE, CAPI_R_ERROR_OPENING_STORE);
1390 capi_addlasterror();
1395 int capi_list_certs(CAPI_CTX *ctx, BIO *out, char *id)
1401 PCCERT_CONTEXT cert = NULL;
1403 storename = ctx->storename;
1406 CAPI_trace(ctx, "Listing certs for store %s\n", storename);
1408 hstore = capi_open_store(ctx, storename);
1412 cert = capi_find_cert(ctx, id, hstore);
1417 capi_dump_cert(ctx, out, cert);
1418 CertFreeCertificateContext(cert);
1420 for (idx = 0;; idx++) {
1421 cert = CertEnumCertificatesInStore(hstore, cert);
1424 BIO_printf(out, "Certificate %d\n", idx);
1425 capi_dump_cert(ctx, out, cert);
1429 CertCloseStore(hstore, 0);
1433 static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id,
1436 PCCERT_CONTEXT cert = NULL;
1439 switch (ctx->lookup_method) {
1440 case CAPI_LU_SUBSTR:
1441 return CertFindCertificateInStore(hstore, X509_ASN_ENCODING, 0,
1442 CERT_FIND_SUBJECT_STR_A, id, NULL);
1445 cert = CertEnumCertificatesInStore(hstore, cert);
1448 fname = capi_cert_get_fname(ctx, cert);
1450 if (strcmp(fname, id))
1454 OPENSSL_free(fname);
1464 static CAPI_KEY *capi_get_key(CAPI_CTX *ctx, const WCHAR *contname,
1465 const WCHAR *provname, DWORD ptype,
1469 CAPI_KEY *key = OPENSSL_malloc(sizeof(*key));
1473 /* If PROV_RSA_AES supported use it instead */
1474 if (ptype == PROV_RSA_FULL && use_aes_csp &&
1475 wcscmp(provname, rsa_enh_cspname) == 0) {
1476 provname = rsa_aes_cspname;
1477 ptype = PROV_RSA_AES;
1479 if (ctx && ctx->debug_level >= CAPI_DBG_TRACE && ctx->debug_file) {
1480 /* above 'if' is [complementary] copy from CAPI_trace and serves
1481 * as optimization to minimize [below] malloc-ations */
1482 char *_contname = wide_to_asc(contname);
1483 char *_provname = wide_to_asc(provname);
1485 CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n",
1486 _contname, _provname, ptype);
1487 OPENSSL_free(_provname);
1488 OPENSSL_free(_contname);
1490 if (ctx->store_flags & CERT_SYSTEM_STORE_LOCAL_MACHINE)
1491 dwFlags = CRYPT_MACHINE_KEYSET;
1492 if (!CryptAcquireContextW(&key->hprov, contname, provname, ptype,
1494 CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
1495 capi_addlasterror();
1498 if (!CryptGetUserKey(key->hprov, keyspec, &key->key)) {
1499 CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_GETUSERKEY_ERROR);
1500 capi_addlasterror();
1501 CryptReleaseContext(key->hprov, 0);
1504 key->keyspec = keyspec;
1513 static CAPI_KEY *capi_get_cert_key(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
1515 CAPI_KEY *key = NULL;
1516 CRYPT_KEY_PROV_INFO *pinfo = NULL;
1518 pinfo = capi_get_prov_info(ctx, cert);
1521 key = capi_get_key(ctx, pinfo->pwszContainerName, pinfo->pwszProvName,
1522 pinfo->dwProvType, pinfo->dwKeySpec);
1524 OPENSSL_free(pinfo);
1528 CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id)
1530 PCCERT_CONTEXT cert;
1532 CAPI_KEY *key = NULL;
1534 switch (ctx->lookup_method) {
1535 case CAPI_LU_SUBSTR:
1537 hstore = capi_open_store(ctx, NULL);
1540 cert = capi_find_cert(ctx, id, hstore);
1542 key = capi_get_cert_key(ctx, cert);
1543 CertFreeCertificateContext(cert);
1545 CertCloseStore(hstore, 0);
1548 case CAPI_LU_CONTNAME:
1550 WCHAR *contname, *provname;
1553 if ((len = MultiByteToWideChar(CP_ACP, 0, id, -1, NULL, 0)) &&
1554 (contname = alloca(len * sizeof(WCHAR)),
1555 MultiByteToWideChar(CP_ACP, 0, id, -1, contname, len)) &&
1556 (len = MultiByteToWideChar(CP_ACP, 0, ctx->cspname, -1,
1558 (provname = alloca(len * sizeof(WCHAR)),
1559 MultiByteToWideChar(CP_ACP, 0, ctx->cspname, -1,
1561 key = capi_get_key(ctx, contname, provname,
1562 ctx->csptype, ctx->keytype);
1570 void capi_free_key(CAPI_KEY *key)
1574 CryptDestroyKey(key->key);
1575 CryptReleaseContext(key->hprov, 0);
1577 CertFreeCertificateContext(key->pcert);
1581 /* Initialize a CAPI_CTX structure */
1583 static CAPI_CTX *capi_ctx_new(void)
1585 CAPI_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
1588 CAPIerr(CAPI_F_CAPI_CTX_NEW, ERR_R_MALLOC_FAILURE);
1591 ctx->csptype = PROV_RSA_FULL;
1592 ctx->dump_flags = CAPI_DMP_SUMMARY | CAPI_DMP_FNAME;
1593 ctx->keytype = AT_KEYEXCHANGE;
1594 ctx->store_flags = CERT_STORE_OPEN_EXISTING_FLAG |
1595 CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_CURRENT_USER;
1596 ctx->lookup_method = CAPI_LU_SUBSTR;
1597 ctx->client_cert_select = cert_select_simple;
1601 static void capi_ctx_free(CAPI_CTX *ctx)
1603 CAPI_trace(ctx, "Calling capi_ctx_free with %lx\n", ctx);
1606 OPENSSL_free(ctx->cspname);
1607 OPENSSL_free(ctx->debug_file);
1608 OPENSSL_free(ctx->storename);
1609 OPENSSL_free(ctx->ssl_client_store);
1613 static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD type,
1618 CAPI_trace(ctx, "capi_ctx_set_provname, name=%s, type=%d\n", pname, type);
1624 if ((len = MultiByteToWideChar(CP_ACP, 0, pname, -1, NULL, 0))) {
1625 name = alloca(len * sizeof(WCHAR));
1626 MultiByteToWideChar(CP_ACP, 0, pname, -1, (WCHAR *)name, len);
1628 if (name == NULL || !CryptAcquireContextW(&hprov, NULL, name, type,
1629 CRYPT_VERIFYCONTEXT)) {
1630 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME,
1631 CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
1632 capi_addlasterror();
1635 CryptReleaseContext(hprov, 0);
1637 tmpcspname = OPENSSL_strdup(pname);
1638 if (tmpcspname == NULL) {
1639 CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, ERR_R_MALLOC_FAILURE);
1642 OPENSSL_free(ctx->cspname);
1643 ctx->cspname = tmpcspname;
1644 ctx->csptype = type;
1648 static int capi_ctx_set_provname_idx(CAPI_CTX *ctx, int idx)
1653 if (capi_get_provname(ctx, &pname, &type, idx) != 1)
1655 res = capi_ctx_set_provname(ctx, pname, type, 0);
1656 OPENSSL_free(pname);
1660 static int cert_issuer_match(STACK_OF(X509_NAME) *ca_dn, X509 *x)
1664 /* Special case: empty list: match anything */
1665 if (sk_X509_NAME_num(ca_dn) <= 0)
1667 for (i = 0; i < sk_X509_NAME_num(ca_dn); i++) {
1668 nm = sk_X509_NAME_value(ca_dn, i);
1669 if (!X509_NAME_cmp(nm, X509_get_issuer_name(x)))
1675 static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl,
1676 STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
1677 EVP_PKEY **pkey, STACK_OF(X509) **pother,
1678 UI_METHOD *ui_method,
1679 void *callback_data)
1681 STACK_OF(X509) *certs = NULL;
1684 const unsigned char *p;
1685 int i, client_cert_idx;
1687 PCCERT_CONTEXT cert = NULL, excert = NULL;
1690 ctx = ENGINE_get_ex_data(e, capi_idx);
1695 storename = ctx->ssl_client_store;
1699 hstore = capi_open_store(ctx, storename);
1702 /* Enumerate all certificates collect any matches */
1704 cert = CertEnumCertificatesInStore(hstore, cert);
1707 p = cert->pbCertEncoded;
1708 x = d2i_X509(NULL, &p, cert->cbCertEncoded);
1710 CAPI_trace(ctx, "Can't Parse Certificate %d\n", i);
1713 if (cert_issuer_match(ca_dn, x)
1714 && X509_check_purpose(x, X509_PURPOSE_SSL_CLIENT, 0)) {
1715 key = capi_get_cert_key(ctx, cert);
1721 * Match found: attach extra data to it so we can retrieve the
1724 excert = CertDuplicateCertificateContext(cert);
1725 key->pcert = excert;
1726 X509_set_ex_data(x, cert_capi_idx, key);
1729 certs = sk_X509_new_null();
1731 sk_X509_push(certs, x);
1738 CertFreeCertificateContext(cert);
1740 CertCloseStore(hstore, 0);
1745 /* Select the appropriate certificate */
1747 client_cert_idx = ctx->client_cert_select(e, ssl, certs);
1749 /* Set the selected certificate and free the rest */
1751 for (i = 0; i < sk_X509_num(certs); i++) {
1752 x = sk_X509_value(certs, i);
1753 if (i == client_cert_idx)
1756 key = X509_get_ex_data(x, cert_capi_idx);
1762 sk_X509_free(certs);
1767 /* Setup key for selected certificate */
1769 key = X509_get_ex_data(*pcert, cert_capi_idx);
1770 *pkey = capi_get_pkey(e, key);
1771 X509_set_ex_data(*pcert, cert_capi_idx, NULL);
1777 /* Simple client cert selection function: always select first */
1779 static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs)
1784 # ifdef OPENSSL_CAPIENG_DIALOG
1787 * More complex cert selection function, using standard function
1788 * CryptUIDlgSelectCertificateFromStore() to produce a dialog box.
1792 * Definitions which are in cryptuiapi.h but this is not present in older
1793 * versions of headers.
1796 # ifndef CRYPTUI_SELECT_LOCATION_COLUMN
1797 # define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010
1798 # define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004
1801 # define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
1802 # define dlg_prompt L"Select a certificate to use for authentication"
1803 # define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
1804 |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
1806 static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs)
1810 PCCERT_CONTEXT cert;
1815 if (sk_X509_num(certs) == 1)
1817 ctx = ENGINE_get_ex_data(e, capi_idx);
1818 /* Create an in memory store of certificates */
1819 dstore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1820 CERT_STORE_CREATE_NEW_FLAG, NULL);
1822 CAPIerr(CAPI_F_CERT_SELECT_DIALOG, CAPI_R_ERROR_CREATING_STORE);
1823 capi_addlasterror();
1826 /* Add all certificates to store */
1827 for (i = 0; i < sk_X509_num(certs); i++) {
1828 x = sk_X509_value(certs, i);
1829 key = X509_get_ex_data(x, cert_capi_idx);
1831 if (!CertAddCertificateContextToStore(dstore, key->pcert,
1832 CERT_STORE_ADD_NEW, NULL)) {
1833 CAPIerr(CAPI_F_CERT_SELECT_DIALOG, CAPI_R_ERROR_ADDING_CERT);
1834 capi_addlasterror();
1839 hwnd = GetForegroundWindow();
1841 hwnd = GetActiveWindow();
1842 if (!hwnd && ctx->getconswindow)
1843 hwnd = ctx->getconswindow();
1844 /* Call dialog to select one */
1845 cert = ctx->certselectdlg(dstore, hwnd, dlg_title, dlg_prompt,
1846 dlg_columns, 0, NULL);
1848 /* Find matching cert from list */
1850 for (i = 0; i < sk_X509_num(certs); i++) {
1851 x = sk_X509_value(certs, i);
1852 key = X509_get_ex_data(x, cert_capi_idx);
1853 if (CertCompareCertificate
1854 (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, cert->pCertInfo,
1855 key->pcert->pCertInfo)) {
1864 CertCloseStore(dstore, 0);
1870 #else /* !__COMPILE_CAPIENG */
1871 # include <openssl/engine.h>
1872 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1874 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
1876 int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
1881 IMPLEMENT_DYNAMIC_CHECK_FN()
1883 void engine_load_capi_int(void);
1884 void engine_load_capi_int(void)