e_capi.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /* engines/e_capi.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  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
  19. * distribution.
  20. *
  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/)"
  25. *
  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. * [email protected].
  30. *
  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.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  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. * ====================================================================
  53. */
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <openssl/crypto.h>
  57. #include <openssl/buffer.h>
  58. #include <openssl/bn.h>
  59. #ifdef OPENSSL_SYS_WIN32
  60. # ifndef OPENSSL_NO_CAPIENG
  61. # include <openssl/rsa.h>
  62. # include <windows.h>
  63. # ifndef _WIN32_WINNT
  64. # define _WIN32_WINNT 0x0400
  65. # endif
  66. # include <wincrypt.h>
  67. /*
  68. * This module uses several "new" interfaces, among which is
  69. * CertGetCertificateContextProperty. CERT_KEY_PROV_INFO_PROP_ID is
  70. * one of possible values you can pass to function in question. By
  71. * checking if it's defined we can see if wincrypt.h and accompanying
  72. * crypt32.lib are in shape. The native MingW32 headers up to and
  73. * including __W32API_VERSION 3.14 lack of struct DSSPUBKEY and the
  74. * defines CERT_STORE_PROV_SYSTEM_A and CERT_STORE_READONLY_FLAG,
  75. * so we check for these too and avoid compiling.
  76. * Yes, it's rather "weak" test and if compilation fails,
  77. * then re-configure with -DOPENSSL_NO_CAPIENG.
  78. */
  79. # if defined(CERT_KEY_PROV_INFO_PROP_ID) && \
  80. defined(CERT_STORE_PROV_SYSTEM_A) && \
  81. defined(CERT_STORE_READONLY_FLAG)
  82. # define __COMPILE_CAPIENG
  83. # endif /* CERT_KEY_PROV_INFO_PROP_ID */
  84. # endif /* OPENSSL_NO_CAPIENG */
  85. #endif /* OPENSSL_SYS_WIN32 */
  86. #ifdef __COMPILE_CAPIENG
  87. # undef X509_EXTENSIONS
  88. # undef X509_CERT_PAIR
  89. /* Definitions which may be missing from earlier version of headers */
  90. # ifndef CERT_STORE_OPEN_EXISTING_FLAG
  91. # define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
  92. # endif
  93. # ifndef CERT_STORE_CREATE_NEW_FLAG
  94. # define CERT_STORE_CREATE_NEW_FLAG 0x00002000
  95. # endif
  96. # ifndef CERT_SYSTEM_STORE_CURRENT_USER
  97. # define CERT_SYSTEM_STORE_CURRENT_USER 0x00010000
  98. # endif
  99. # include <openssl/engine.h>
  100. # include <openssl/pem.h>
  101. # include <openssl/x509v3.h>
  102. # include "e_capi_err.h"
  103. # include "e_capi_err.c"
  104. static const char *engine_capi_id = "capi";
  105. static const char *engine_capi_name = "CryptoAPI ENGINE";
  106. typedef struct CAPI_CTX_st CAPI_CTX;
  107. typedef struct CAPI_KEY_st CAPI_KEY;
  108. static void capi_addlasterror(void);
  109. static void capi_adderror(DWORD err);
  110. static void CAPI_trace(CAPI_CTX * ctx, char *format, ...);
  111. static int capi_list_providers(CAPI_CTX * ctx, BIO *out);
  112. static int capi_list_containers(CAPI_CTX * ctx, BIO *out);
  113. int capi_list_certs(CAPI_CTX * ctx, BIO *out, char *storename);
  114. void capi_free_key(CAPI_KEY * key);
  115. static PCCERT_CONTEXT capi_find_cert(CAPI_CTX * ctx, const char *id,
  116. HCERTSTORE hstore);
  117. CAPI_KEY *capi_find_key(CAPI_CTX * ctx, const char *id);
  118. static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
  119. UI_METHOD *ui_method, void *callback_data);
  120. static int capi_rsa_sign(int dtype, const unsigned char *m,
  121. unsigned int m_len, unsigned char *sigret,
  122. unsigned int *siglen, const RSA *rsa);
  123. static int capi_rsa_priv_enc(int flen, const unsigned char *from,
  124. unsigned char *to, RSA *rsa, int padding);
  125. static int capi_rsa_priv_dec(int flen, const unsigned char *from,
  126. unsigned char *to, RSA *rsa, int padding);
  127. static int capi_rsa_free(RSA *rsa);
  128. static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
  129. DSA *dsa);
  130. static int capi_dsa_free(DSA *dsa);
  131. static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl,
  132. STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
  133. EVP_PKEY **pkey, STACK_OF(X509) **pother,
  134. UI_METHOD *ui_method,
  135. void *callback_data);
  136. static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs);
  137. # ifdef OPENSSL_CAPIENG_DIALOG
  138. static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs);
  139. # endif
  140. typedef PCCERT_CONTEXT(WINAPI *CERTDLG) (HCERTSTORE, HWND, LPCWSTR,
  141. LPCWSTR, DWORD, DWORD, void *);
  142. typedef HWND(WINAPI *GETCONSWIN) (void);
  143. /*
  144. * This structure contains CAPI ENGINE specific data: it contains various
  145. * global options and affects how other functions behave.
  146. */
  147. # define CAPI_DBG_TRACE 2
  148. # define CAPI_DBG_ERROR 1
  149. struct CAPI_CTX_st {
  150. int debug_level;
  151. char *debug_file;
  152. /* Parameters to use for container lookup */
  153. DWORD keytype;
  154. LPSTR cspname;
  155. DWORD csptype;
  156. /* Certificate store name to use */
  157. LPSTR storename;
  158. LPSTR ssl_client_store;
  159. /* System store flags */
  160. DWORD store_flags;
  161. /* Lookup string meanings in load_private_key */
  162. /* Substring of subject: uses "storename" */
  163. # define CAPI_LU_SUBSTR 1
  164. /* Friendly name: uses storename */
  165. # define CAPI_LU_FNAME 2
  166. /* Container name: uses cspname, keytype */
  167. # define CAPI_LU_CONTNAME 3
  168. int lookup_method;
  169. /* Info to dump with dumpcerts option */
  170. /* Issuer and serial name strings */
  171. # define CAPI_DMP_SUMMARY 0x1
  172. /* Friendly name */
  173. # define CAPI_DMP_FNAME 0x2
  174. /* Full X509_print dump */
  175. # define CAPI_DMP_FULL 0x4
  176. /* Dump PEM format certificate */
  177. # define CAPI_DMP_PEM 0x8
  178. /* Dump pseudo key (if possible) */
  179. # define CAPI_DMP_PSKEY 0x10
  180. /* Dump key info (if possible) */
  181. # define CAPI_DMP_PKEYINFO 0x20
  182. DWORD dump_flags;
  183. int (*client_cert_select) (ENGINE *e, SSL *ssl, STACK_OF(X509) *certs);
  184. CERTDLG certselectdlg;
  185. GETCONSWIN getconswindow;
  186. };
  187. static CAPI_CTX *capi_ctx_new();
  188. static void capi_ctx_free(CAPI_CTX * ctx);
  189. static int capi_ctx_set_provname(CAPI_CTX * ctx, LPSTR pname, DWORD type,
  190. int check);
  191. static int capi_ctx_set_provname_idx(CAPI_CTX * ctx, int idx);
  192. # define CAPI_CMD_LIST_CERTS ENGINE_CMD_BASE
  193. # define CAPI_CMD_LOOKUP_CERT (ENGINE_CMD_BASE + 1)
  194. # define CAPI_CMD_DEBUG_LEVEL (ENGINE_CMD_BASE + 2)
  195. # define CAPI_CMD_DEBUG_FILE (ENGINE_CMD_BASE + 3)
  196. # define CAPI_CMD_KEYTYPE (ENGINE_CMD_BASE + 4)
  197. # define CAPI_CMD_LIST_CSPS (ENGINE_CMD_BASE + 5)
  198. # define CAPI_CMD_SET_CSP_IDX (ENGINE_CMD_BASE + 6)
  199. # define CAPI_CMD_SET_CSP_NAME (ENGINE_CMD_BASE + 7)
  200. # define CAPI_CMD_SET_CSP_TYPE (ENGINE_CMD_BASE + 8)
  201. # define CAPI_CMD_LIST_CONTAINERS (ENGINE_CMD_BASE + 9)
  202. # define CAPI_CMD_LIST_OPTIONS (ENGINE_CMD_BASE + 10)
  203. # define CAPI_CMD_LOOKUP_METHOD (ENGINE_CMD_BASE + 11)
  204. # define CAPI_CMD_STORE_NAME (ENGINE_CMD_BASE + 12)
  205. # define CAPI_CMD_STORE_FLAGS (ENGINE_CMD_BASE + 13)
  206. static const ENGINE_CMD_DEFN capi_cmd_defns[] = {
  207. {CAPI_CMD_LIST_CERTS,
  208. "list_certs",
  209. "List all certificates in store",
  210. ENGINE_CMD_FLAG_NO_INPUT},
  211. {CAPI_CMD_LOOKUP_CERT,
  212. "lookup_cert",
  213. "Lookup and output certificates",
  214. ENGINE_CMD_FLAG_STRING},
  215. {CAPI_CMD_DEBUG_LEVEL,
  216. "debug_level",
  217. "debug level (1=errors, 2=trace)",
  218. ENGINE_CMD_FLAG_NUMERIC},
  219. {CAPI_CMD_DEBUG_FILE,
  220. "debug_file",
  221. "debugging filename)",
  222. ENGINE_CMD_FLAG_STRING},
  223. {CAPI_CMD_KEYTYPE,
  224. "key_type",
  225. "Key type: 1=AT_KEYEXCHANGE (default), 2=AT_SIGNATURE",
  226. ENGINE_CMD_FLAG_NUMERIC},
  227. {CAPI_CMD_LIST_CSPS,
  228. "list_csps",
  229. "List all CSPs",
  230. ENGINE_CMD_FLAG_NO_INPUT},
  231. {CAPI_CMD_SET_CSP_IDX,
  232. "csp_idx",
  233. "Set CSP by index",
  234. ENGINE_CMD_FLAG_NUMERIC},
  235. {CAPI_CMD_SET_CSP_NAME,
  236. "csp_name",
  237. "Set CSP name, (default CSP used if not specified)",
  238. ENGINE_CMD_FLAG_STRING},
  239. {CAPI_CMD_SET_CSP_TYPE,
  240. "csp_type",
  241. "Set CSP type, (default RSA_PROV_FULL)",
  242. ENGINE_CMD_FLAG_NUMERIC},
  243. {CAPI_CMD_LIST_CONTAINERS,
  244. "list_containers",
  245. "list container names",
  246. ENGINE_CMD_FLAG_NO_INPUT},
  247. {CAPI_CMD_LIST_OPTIONS,
  248. "list_options",
  249. "Set list options (1=summary,2=friendly name, 4=full printout, 8=PEM output, 16=XXX, "
  250. "32=private key info)",
  251. ENGINE_CMD_FLAG_NUMERIC},
  252. {CAPI_CMD_LOOKUP_METHOD,
  253. "lookup_method",
  254. "Set key lookup method (1=substring, 2=friendlyname, 3=container name)",
  255. ENGINE_CMD_FLAG_NUMERIC},
  256. {CAPI_CMD_STORE_NAME,
  257. "store_name",
  258. "certificate store name, default \"MY\"",
  259. ENGINE_CMD_FLAG_STRING},
  260. {CAPI_CMD_STORE_FLAGS,
  261. "store_flags",
  262. "Certificate store flags: 1 = system store",
  263. ENGINE_CMD_FLAG_NUMERIC},
  264. {0, NULL, NULL, 0}
  265. };
  266. static int capi_idx = -1;
  267. static int rsa_capi_idx = -1;
  268. static int dsa_capi_idx = -1;
  269. static int cert_capi_idx = -1;
  270. static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  271. {
  272. int ret = 1;
  273. CAPI_CTX *ctx;
  274. BIO *out;
  275. if (capi_idx == -1) {
  276. CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_ENGINE_NOT_INITIALIZED);
  277. return 0;
  278. }
  279. ctx = ENGINE_get_ex_data(e, capi_idx);
  280. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  281. switch (cmd) {
  282. case CAPI_CMD_LIST_CSPS:
  283. ret = capi_list_providers(ctx, out);
  284. break;
  285. case CAPI_CMD_LIST_CERTS:
  286. ret = capi_list_certs(ctx, out, NULL);
  287. break;
  288. case CAPI_CMD_LOOKUP_CERT:
  289. ret = capi_list_certs(ctx, out, p);
  290. break;
  291. case CAPI_CMD_LIST_CONTAINERS:
  292. ret = capi_list_containers(ctx, out);
  293. break;
  294. case CAPI_CMD_STORE_NAME:
  295. if (ctx->storename)
  296. OPENSSL_free(ctx->storename);
  297. ctx->storename = BUF_strdup(p);
  298. CAPI_trace(ctx, "Setting store name to %s\n", p);
  299. break;
  300. case CAPI_CMD_STORE_FLAGS:
  301. if (i & 1) {
  302. ctx->store_flags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
  303. ctx->store_flags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
  304. } else {
  305. ctx->store_flags |= CERT_SYSTEM_STORE_CURRENT_USER;
  306. ctx->store_flags &= ~CERT_SYSTEM_STORE_LOCAL_MACHINE;
  307. }
  308. CAPI_trace(ctx, "Setting flags to %d\n", i);
  309. break;
  310. case CAPI_CMD_DEBUG_LEVEL:
  311. ctx->debug_level = (int)i;
  312. CAPI_trace(ctx, "Setting debug level to %d\n", ctx->debug_level);
  313. break;
  314. case CAPI_CMD_DEBUG_FILE:
  315. ctx->debug_file = BUF_strdup(p);
  316. CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file);
  317. break;
  318. case CAPI_CMD_KEYTYPE:
  319. ctx->keytype = i;
  320. CAPI_trace(ctx, "Setting key type to %d\n", ctx->keytype);
  321. break;
  322. case CAPI_CMD_SET_CSP_IDX:
  323. ret = capi_ctx_set_provname_idx(ctx, i);
  324. break;
  325. case CAPI_CMD_LIST_OPTIONS:
  326. ctx->dump_flags = i;
  327. break;
  328. case CAPI_CMD_LOOKUP_METHOD:
  329. if (i < 1 || i > 3) {
  330. CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_INVALID_LOOKUP_METHOD);
  331. return 0;
  332. }
  333. ctx->lookup_method = i;
  334. break;
  335. case CAPI_CMD_SET_CSP_NAME:
  336. ret = capi_ctx_set_provname(ctx, p, ctx->csptype, 1);
  337. break;
  338. case CAPI_CMD_SET_CSP_TYPE:
  339. ctx->csptype = i;
  340. break;
  341. default:
  342. CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_UNKNOWN_COMMAND);
  343. ret = 0;
  344. }
  345. BIO_free(out);
  346. return ret;
  347. }
  348. static RSA_METHOD capi_rsa_method = {
  349. "CryptoAPI RSA method",
  350. 0, /* pub_enc */
  351. 0, /* pub_dec */
  352. capi_rsa_priv_enc, /* priv_enc */
  353. capi_rsa_priv_dec, /* priv_dec */
  354. 0, /* rsa_mod_exp */
  355. 0, /* bn_mod_exp */
  356. 0, /* init */
  357. capi_rsa_free, /* finish */
  358. RSA_FLAG_SIGN_VER, /* flags */
  359. NULL, /* app_data */
  360. capi_rsa_sign, /* rsa_sign */
  361. 0 /* rsa_verify */
  362. };
  363. static DSA_METHOD capi_dsa_method = {
  364. "CryptoAPI DSA method",
  365. capi_dsa_do_sign, /* dsa_do_sign */
  366. 0, /* dsa_sign_setup */
  367. 0, /* dsa_do_verify */
  368. 0, /* dsa_mod_exp */
  369. 0, /* bn_mod_exp */
  370. 0, /* init */
  371. capi_dsa_free, /* finish */
  372. 0, /* flags */
  373. NULL, /* app_data */
  374. 0, /* dsa_paramgen */
  375. 0 /* dsa_keygen */
  376. };
  377. static int capi_init(ENGINE *e)
  378. {
  379. CAPI_CTX *ctx;
  380. const RSA_METHOD *ossl_rsa_meth;
  381. const DSA_METHOD *ossl_dsa_meth;
  382. if (capi_idx < 0) {
  383. capi_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 0);
  384. if (capi_idx < 0)
  385. goto memerr;
  386. cert_capi_idx = X509_get_ex_new_index(0, NULL, NULL, NULL, 0);
  387. /* Setup RSA_METHOD */
  388. rsa_capi_idx = RSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
  389. ossl_rsa_meth = RSA_PKCS1_SSLeay();
  390. capi_rsa_method.rsa_pub_enc = ossl_rsa_meth->rsa_pub_enc;
  391. capi_rsa_method.rsa_pub_dec = ossl_rsa_meth->rsa_pub_dec;
  392. capi_rsa_method.rsa_mod_exp = ossl_rsa_meth->rsa_mod_exp;
  393. capi_rsa_method.bn_mod_exp = ossl_rsa_meth->bn_mod_exp;
  394. /* Setup DSA Method */
  395. dsa_capi_idx = DSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
  396. ossl_dsa_meth = DSA_OpenSSL();
  397. capi_dsa_method.dsa_do_verify = ossl_dsa_meth->dsa_do_verify;
  398. capi_dsa_method.dsa_mod_exp = ossl_dsa_meth->dsa_mod_exp;
  399. capi_dsa_method.bn_mod_exp = ossl_dsa_meth->bn_mod_exp;
  400. }
  401. ctx = capi_ctx_new();
  402. if (!ctx)
  403. goto memerr;
  404. ENGINE_set_ex_data(e, capi_idx, ctx);
  405. # ifdef OPENSSL_CAPIENG_DIALOG
  406. {
  407. HMODULE cryptui = LoadLibrary(TEXT("CRYPTUI.DLL"));
  408. HMODULE kernel = GetModuleHandle(TEXT("KERNEL32.DLL"));
  409. if (cryptui)
  410. ctx->certselectdlg =
  411. (CERTDLG) GetProcAddress(cryptui,
  412. "CryptUIDlgSelectCertificateFromStore");
  413. if (kernel)
  414. ctx->getconswindow =
  415. (GETCONSWIN) GetProcAddress(kernel, "GetConsoleWindow");
  416. if (cryptui && !OPENSSL_isservice())
  417. ctx->client_cert_select = cert_select_dialog;
  418. }
  419. # endif
  420. return 1;
  421. memerr:
  422. CAPIerr(CAPI_F_CAPI_INIT, ERR_R_MALLOC_FAILURE);
  423. return 0;
  424. return 1;
  425. }
  426. static int capi_destroy(ENGINE *e)
  427. {
  428. ERR_unload_CAPI_strings();
  429. return 1;
  430. }
  431. static int capi_finish(ENGINE *e)
  432. {
  433. CAPI_CTX *ctx;
  434. ctx = ENGINE_get_ex_data(e, capi_idx);
  435. capi_ctx_free(ctx);
  436. ENGINE_set_ex_data(e, capi_idx, NULL);
  437. return 1;
  438. }
  439. /*
  440. * CryptoAPI key application data. This contains a handle to the private key
  441. * container (for sign operations) and a handle to the key (for decrypt
  442. * operations).
  443. */
  444. struct CAPI_KEY_st {
  445. /* Associated certificate context (if any) */
  446. PCCERT_CONTEXT pcert;
  447. HCRYPTPROV hprov;
  448. HCRYPTKEY key;
  449. DWORD keyspec;
  450. };
  451. static int bind_capi(ENGINE *e)
  452. {
  453. if (!ENGINE_set_id(e, engine_capi_id)
  454. || !ENGINE_set_name(e, engine_capi_name)
  455. || !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL)
  456. || !ENGINE_set_init_function(e, capi_init)
  457. || !ENGINE_set_finish_function(e, capi_finish)
  458. || !ENGINE_set_destroy_function(e, capi_destroy)
  459. || !ENGINE_set_RSA(e, &capi_rsa_method)
  460. || !ENGINE_set_DSA(e, &capi_dsa_method)
  461. || !ENGINE_set_load_privkey_function(e, capi_load_privkey)
  462. || !ENGINE_set_load_ssl_client_cert_function(e,
  463. capi_load_ssl_client_cert)
  464. || !ENGINE_set_cmd_defns(e, capi_cmd_defns)
  465. || !ENGINE_set_ctrl_function(e, capi_ctrl))
  466. return 0;
  467. ERR_load_CAPI_strings();
  468. return 1;
  469. }
  470. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  471. static int bind_helper(ENGINE *e, const char *id)
  472. {
  473. if (id && (strcmp(id, engine_capi_id) != 0))
  474. return 0;
  475. if (!bind_capi(e))
  476. return 0;
  477. return 1;
  478. }
  479. IMPLEMENT_DYNAMIC_CHECK_FN()
  480. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  481. # else
  482. static ENGINE *engine_capi(void)
  483. {
  484. ENGINE *ret = ENGINE_new();
  485. if (!ret)
  486. return NULL;
  487. if (!bind_capi(ret)) {
  488. ENGINE_free(ret);
  489. return NULL;
  490. }
  491. return ret;
  492. }
  493. void ENGINE_load_capi(void)
  494. {
  495. /* Copied from eng_[openssl|dyn].c */
  496. ENGINE *toadd = engine_capi();
  497. if (!toadd)
  498. return;
  499. ENGINE_add(toadd);
  500. ENGINE_free(toadd);
  501. ERR_clear_error();
  502. }
  503. # endif
  504. static int lend_tobn(BIGNUM *bn, unsigned char *bin, int binlen)
  505. {
  506. int i;
  507. /*
  508. * Reverse buffer in place: since this is a keyblob structure that will
  509. * be freed up after conversion anyway it doesn't matter if we change
  510. * it.
  511. */
  512. for (i = 0; i < binlen / 2; i++) {
  513. unsigned char c;
  514. c = bin[i];
  515. bin[i] = bin[binlen - i - 1];
  516. bin[binlen - i - 1] = c;
  517. }
  518. if (!BN_bin2bn(bin, binlen, bn))
  519. return 0;
  520. return 1;
  521. }
  522. /* Given a CAPI_KEY get an EVP_PKEY structure */
  523. static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key)
  524. {
  525. unsigned char *pubkey = NULL;
  526. DWORD len;
  527. BLOBHEADER *bh;
  528. RSA *rkey = NULL;
  529. DSA *dkey = NULL;
  530. EVP_PKEY *ret = NULL;
  531. if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, NULL, &len)) {
  532. CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_LENGTH_ERROR);
  533. capi_addlasterror();
  534. return NULL;
  535. }
  536. pubkey = OPENSSL_malloc(len);
  537. if (!pubkey)
  538. goto memerr;
  539. if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len)) {
  540. CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_ERROR);
  541. capi_addlasterror();
  542. goto err;
  543. }
  544. bh = (BLOBHEADER *) pubkey;
  545. if (bh->bType != PUBLICKEYBLOB) {
  546. CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_INVALID_PUBLIC_KEY_BLOB);
  547. goto err;
  548. }
  549. if (bh->aiKeyAlg == CALG_RSA_SIGN || bh->aiKeyAlg == CALG_RSA_KEYX) {
  550. RSAPUBKEY *rp;
  551. DWORD rsa_modlen;
  552. unsigned char *rsa_modulus;
  553. rp = (RSAPUBKEY *) (bh + 1);
  554. if (rp->magic != 0x31415352) {
  555. char magstr[10];
  556. BIO_snprintf(magstr, 10, "%lx", rp->magic);
  557. CAPIerr(CAPI_F_CAPI_GET_PKEY,
  558. CAPI_R_INVALID_RSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER);
  559. ERR_add_error_data(2, "magic=0x", magstr);
  560. goto err;
  561. }
  562. rsa_modulus = (unsigned char *)(rp + 1);
  563. rkey = RSA_new_method(eng);
  564. if (!rkey)
  565. goto memerr;
  566. rkey->e = BN_new();
  567. rkey->n = BN_new();
  568. if (!rkey->e || !rkey->n)
  569. goto memerr;
  570. if (!BN_set_word(rkey->e, rp->pubexp))
  571. goto memerr;
  572. rsa_modlen = rp->bitlen / 8;
  573. if (!lend_tobn(rkey->n, rsa_modulus, rsa_modlen))
  574. goto memerr;
  575. RSA_set_ex_data(rkey, rsa_capi_idx, key);
  576. if (!(ret = EVP_PKEY_new()))
  577. goto memerr;
  578. EVP_PKEY_assign_RSA(ret, rkey);
  579. rkey = NULL;
  580. } else if (bh->aiKeyAlg == CALG_DSS_SIGN) {
  581. DSSPUBKEY *dp;
  582. DWORD dsa_plen;
  583. unsigned char *btmp;
  584. dp = (DSSPUBKEY *) (bh + 1);
  585. if (dp->magic != 0x31535344) {
  586. char magstr[10];
  587. BIO_snprintf(magstr, 10, "%lx", dp->magic);
  588. CAPIerr(CAPI_F_CAPI_GET_PKEY,
  589. CAPI_R_INVALID_DSA_PUBLIC_KEY_BLOB_MAGIC_NUMBER);
  590. ERR_add_error_data(2, "magic=0x", magstr);
  591. goto err;
  592. }
  593. dsa_plen = dp->bitlen / 8;
  594. btmp = (unsigned char *)(dp + 1);
  595. dkey = DSA_new_method(eng);
  596. if (!dkey)
  597. goto memerr;
  598. dkey->p = BN_new();
  599. dkey->q = BN_new();
  600. dkey->g = BN_new();
  601. dkey->pub_key = BN_new();
  602. if (!dkey->p || !dkey->q || !dkey->g || !dkey->pub_key)
  603. goto memerr;
  604. if (!lend_tobn(dkey->p, btmp, dsa_plen))
  605. goto memerr;
  606. btmp += dsa_plen;
  607. if (!lend_tobn(dkey->q, btmp, 20))
  608. goto memerr;
  609. btmp += 20;
  610. if (!lend_tobn(dkey->g, btmp, dsa_plen))
  611. goto memerr;
  612. btmp += dsa_plen;
  613. if (!lend_tobn(dkey->pub_key, btmp, dsa_plen))
  614. goto memerr;
  615. btmp += dsa_plen;
  616. DSA_set_ex_data(dkey, dsa_capi_idx, key);
  617. if (!(ret = EVP_PKEY_new()))
  618. goto memerr;
  619. EVP_PKEY_assign_DSA(ret, dkey);
  620. dkey = NULL;
  621. } else {
  622. char algstr[10];
  623. BIO_snprintf(algstr, 10, "%lx", bh->aiKeyAlg);
  624. CAPIerr(CAPI_F_CAPI_GET_PKEY,
  625. CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM);
  626. ERR_add_error_data(2, "aiKeyAlg=0x", algstr);
  627. goto err;
  628. }
  629. err:
  630. if (pubkey)
  631. OPENSSL_free(pubkey);
  632. if (!ret) {
  633. if (rkey)
  634. RSA_free(rkey);
  635. if (dkey)
  636. DSA_free(dkey);
  637. }
  638. return ret;
  639. memerr:
  640. CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_MALLOC_FAILURE);
  641. goto err;
  642. }
  643. static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
  644. UI_METHOD *ui_method, void *callback_data)
  645. {
  646. CAPI_CTX *ctx;
  647. CAPI_KEY *key;
  648. EVP_PKEY *ret;
  649. ctx = ENGINE_get_ex_data(eng, capi_idx);
  650. if (!ctx) {
  651. CAPIerr(CAPI_F_CAPI_LOAD_PRIVKEY, CAPI_R_CANT_FIND_CAPI_CONTEXT);
  652. return NULL;
  653. }
  654. key = capi_find_key(ctx, key_id);
  655. if (!key)
  656. return NULL;
  657. ret = capi_get_pkey(eng, key);
  658. if (!ret)
  659. capi_free_key(key);
  660. return ret;
  661. }
  662. /* CryptoAPI RSA operations */
  663. int capi_rsa_priv_enc(int flen, const unsigned char *from,
  664. unsigned char *to, RSA *rsa, int padding)
  665. {
  666. CAPIerr(CAPI_F_CAPI_RSA_PRIV_ENC, CAPI_R_FUNCTION_NOT_SUPPORTED);
  667. return -1;
  668. }
  669. int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len,
  670. unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
  671. {
  672. ALG_ID alg;
  673. HCRYPTHASH hash;
  674. DWORD slen;
  675. unsigned int i;
  676. int ret = -1;
  677. CAPI_KEY *capi_key;
  678. CAPI_CTX *ctx;
  679. ctx = ENGINE_get_ex_data(rsa->engine, capi_idx);
  680. CAPI_trace(ctx, "Called CAPI_rsa_sign()\n");
  681. capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
  682. if (!capi_key) {
  683. CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_GET_KEY);
  684. return -1;
  685. }
  686. /* Convert the signature type to a CryptoAPI algorithm ID */
  687. switch (dtype) {
  688. case NID_sha1:
  689. alg = CALG_SHA1;
  690. break;
  691. case NID_md5:
  692. alg = CALG_MD5;
  693. break;
  694. case NID_md5_sha1:
  695. alg = CALG_SSL3_SHAMD5;
  696. break;
  697. default:
  698. {
  699. char algstr[10];
  700. BIO_snprintf(algstr, 10, "%lx", dtype);
  701. CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_UNSUPPORTED_ALGORITHM_NID);
  702. ERR_add_error_data(2, "NID=0x", algstr);
  703. return -1;
  704. }
  705. }
  706. /* Create the hash object */
  707. if (!CryptCreateHash(capi_key->hprov, alg, 0, 0, &hash)) {
  708. CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT);
  709. capi_addlasterror();
  710. return -1;
  711. }
  712. /* Set the hash value to the value passed */
  713. if (!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)m, 0)) {
  714. CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_CANT_SET_HASH_VALUE);
  715. capi_addlasterror();
  716. goto err;
  717. }
  718. /* Finally sign it */
  719. slen = RSA_size(rsa);
  720. if (!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, sigret, &slen)) {
  721. CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_ERROR_SIGNING_HASH);
  722. capi_addlasterror();
  723. goto err;
  724. } else {
  725. ret = 1;
  726. /* Inplace byte reversal of signature */
  727. for (i = 0; i < slen / 2; i++) {
  728. unsigned char c;
  729. c = sigret[i];
  730. sigret[i] = sigret[slen - i - 1];
  731. sigret[slen - i - 1] = c;
  732. }
  733. *siglen = slen;
  734. }
  735. /* Now cleanup */
  736. err:
  737. CryptDestroyHash(hash);
  738. return ret;
  739. }
  740. int capi_rsa_priv_dec(int flen, const unsigned char *from,
  741. unsigned char *to, RSA *rsa, int padding)
  742. {
  743. int i;
  744. unsigned char *tmpbuf;
  745. CAPI_KEY *capi_key;
  746. CAPI_CTX *ctx;
  747. ctx = ENGINE_get_ex_data(rsa->engine, capi_idx);
  748. CAPI_trace(ctx, "Called capi_rsa_priv_dec()\n");
  749. capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
  750. if (!capi_key) {
  751. CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_CANT_GET_KEY);
  752. return -1;
  753. }
  754. if (padding != RSA_PKCS1_PADDING) {
  755. char errstr[10];
  756. BIO_snprintf(errstr, 10, "%d", padding);
  757. CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_UNSUPPORTED_PADDING);
  758. ERR_add_error_data(2, "padding=", errstr);
  759. return -1;
  760. }
  761. /* Create temp reverse order version of input */
  762. if (!(tmpbuf = OPENSSL_malloc(flen))) {
  763. CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, ERR_R_MALLOC_FAILURE);
  764. return -1;
  765. }
  766. for (i = 0; i < flen; i++)
  767. tmpbuf[flen - i - 1] = from[i];
  768. /* Finally decrypt it */
  769. if (!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &flen)) {
  770. CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_DECRYPT_ERROR);
  771. capi_addlasterror();
  772. OPENSSL_free(tmpbuf);
  773. return -1;
  774. } else
  775. memcpy(to, tmpbuf, flen);
  776. OPENSSL_free(tmpbuf);
  777. return flen;
  778. }
  779. static int capi_rsa_free(RSA *rsa)
  780. {
  781. CAPI_KEY *capi_key;
  782. capi_key = RSA_get_ex_data(rsa, rsa_capi_idx);
  783. capi_free_key(capi_key);
  784. RSA_set_ex_data(rsa, rsa_capi_idx, 0);
  785. return 1;
  786. }
  787. /* CryptoAPI DSA operations */
  788. static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
  789. DSA *dsa)
  790. {
  791. HCRYPTHASH hash;
  792. DWORD slen;
  793. DSA_SIG *ret = NULL;
  794. CAPI_KEY *capi_key;
  795. CAPI_CTX *ctx;
  796. unsigned char csigbuf[40];
  797. ctx = ENGINE_get_ex_data(dsa->engine, capi_idx);
  798. CAPI_trace(ctx, "Called CAPI_dsa_do_sign()\n");
  799. capi_key = DSA_get_ex_data(dsa, dsa_capi_idx);
  800. if (!capi_key) {
  801. CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_GET_KEY);
  802. return NULL;
  803. }
  804. if (dlen != 20) {
  805. CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_INVALID_DIGEST_LENGTH);
  806. return NULL;
  807. }
  808. /* Create the hash object */
  809. if (!CryptCreateHash(capi_key->hprov, CALG_SHA1, 0, 0, &hash)) {
  810. CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_CREATE_HASH_OBJECT);
  811. capi_addlasterror();
  812. return NULL;
  813. }
  814. /* Set the hash value to the value passed */
  815. if (!CryptSetHashParam(hash, HP_HASHVAL, (unsigned char *)digest, 0)) {
  816. CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_CANT_SET_HASH_VALUE);
  817. capi_addlasterror();
  818. goto err;
  819. }
  820. /* Finally sign it */
  821. slen = sizeof(csigbuf);
  822. if (!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen)) {
  823. CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_ERROR_SIGNING_HASH);
  824. capi_addlasterror();
  825. goto err;
  826. } else {
  827. ret = DSA_SIG_new();
  828. if (!ret)
  829. goto err;
  830. ret->r = BN_new();
  831. ret->s = BN_new();
  832. if (!ret->r || !ret->s)
  833. goto err;
  834. if (!lend_tobn(ret->r, csigbuf, 20)
  835. || !lend_tobn(ret->s, csigbuf + 20, 20)) {
  836. DSA_SIG_free(ret);
  837. ret = NULL;
  838. goto err;
  839. }
  840. }
  841. /* Now cleanup */
  842. err:
  843. OPENSSL_cleanse(csigbuf, 40);
  844. CryptDestroyHash(hash);
  845. return ret;
  846. }
  847. static int capi_dsa_free(DSA *dsa)
  848. {
  849. CAPI_KEY *capi_key;
  850. capi_key = DSA_get_ex_data(dsa, dsa_capi_idx);
  851. capi_free_key(capi_key);
  852. DSA_set_ex_data(dsa, dsa_capi_idx, 0);
  853. return 1;
  854. }
  855. static void capi_vtrace(CAPI_CTX * ctx, int level, char *format,
  856. va_list argptr)
  857. {
  858. BIO *out;
  859. if (!ctx || (ctx->debug_level < level) || (!ctx->debug_file))
  860. return;
  861. out = BIO_new_file(ctx->debug_file, "a+");
  862. BIO_vprintf(out, format, argptr);
  863. BIO_free(out);
  864. }
  865. static void CAPI_trace(CAPI_CTX * ctx, char *format, ...)
  866. {
  867. va_list args;
  868. va_start(args, format);
  869. capi_vtrace(ctx, CAPI_DBG_TRACE, format, args);
  870. va_end(args);
  871. }
  872. static void capi_addlasterror(void)
  873. {
  874. capi_adderror(GetLastError());
  875. }
  876. static void capi_adderror(DWORD err)
  877. {
  878. char errstr[10];
  879. BIO_snprintf(errstr, 10, "%lX", err);
  880. ERR_add_error_data(2, "Error code= 0x", errstr);
  881. }
  882. static char *wide_to_asc(LPWSTR wstr)
  883. {
  884. char *str;
  885. int len_0, sz;
  886. if (!wstr)
  887. return NULL;
  888. len_0 = (int)wcslen(wstr) + 1; /* WideCharToMultiByte expects int */
  889. sz = WideCharToMultiByte(CP_ACP, 0, wstr, len_0, NULL, 0, NULL, NULL);
  890. if (!sz) {
  891. CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR);
  892. return NULL;
  893. }
  894. str = OPENSSL_malloc(sz);
  895. if (!str) {
  896. CAPIerr(CAPI_F_WIDE_TO_ASC, ERR_R_MALLOC_FAILURE);
  897. return NULL;
  898. }
  899. if (!WideCharToMultiByte(CP_ACP, 0, wstr, len_0, str, sz, NULL, NULL)) {
  900. OPENSSL_free(str);
  901. CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR);
  902. return NULL;
  903. }
  904. return str;
  905. }
  906. static int capi_get_provname(CAPI_CTX * ctx, LPSTR * pname, DWORD * ptype,
  907. DWORD idx)
  908. {
  909. LPSTR name;
  910. DWORD len, err;
  911. CAPI_trace(ctx, "capi_get_provname, index=%d\n", idx);
  912. if (!CryptEnumProvidersA(idx, NULL, 0, ptype, NULL, &len)) {
  913. err = GetLastError();
  914. if (err == ERROR_NO_MORE_ITEMS)
  915. return 2;
  916. CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR);
  917. capi_adderror(err);
  918. return 0;
  919. }
  920. name = OPENSSL_malloc(len);
  921. if (!CryptEnumProvidersA(idx, NULL, 0, ptype, name, &len)) {
  922. err = GetLastError();
  923. if (err == ERROR_NO_MORE_ITEMS)
  924. return 2;
  925. CAPIerr(CAPI_F_CAPI_GET_PROVNAME, CAPI_R_CRYPTENUMPROVIDERS_ERROR);
  926. capi_adderror(err);
  927. return 0;
  928. }
  929. *pname = name;
  930. CAPI_trace(ctx, "capi_get_provname, returned name=%s, type=%d\n", name,
  931. *ptype);
  932. return 1;
  933. }
  934. static int capi_list_providers(CAPI_CTX * ctx, BIO *out)
  935. {
  936. DWORD idx, ptype;
  937. int ret;
  938. LPSTR provname = NULL;
  939. CAPI_trace(ctx, "capi_list_providers\n");
  940. BIO_printf(out, "Available CSPs:\n");
  941. for (idx = 0;; idx++) {
  942. ret = capi_get_provname(ctx, &provname, &ptype, idx);
  943. if (ret == 2)
  944. break;
  945. if (ret == 0)
  946. break;
  947. BIO_printf(out, "%d. %s, type %d\n", idx, provname, ptype);
  948. OPENSSL_free(provname);
  949. }
  950. return 1;
  951. }
  952. static int capi_list_containers(CAPI_CTX * ctx, BIO *out)
  953. {
  954. int ret = 1;
  955. HCRYPTPROV hprov;
  956. DWORD err, idx, flags, buflen = 0, clen;
  957. LPSTR cname;
  958. CAPI_trace(ctx, "Listing containers CSP=%s, type = %d\n", ctx->cspname,
  959. ctx->csptype);
  960. if (!CryptAcquireContextA
  961. (&hprov, NULL, ctx->cspname, ctx->csptype, CRYPT_VERIFYCONTEXT)) {
  962. CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS,
  963. CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
  964. capi_addlasterror();
  965. return 0;
  966. }
  967. if (!CryptGetProvParam
  968. (hprov, PP_ENUMCONTAINERS, NULL, &buflen, CRYPT_FIRST)) {
  969. CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR);
  970. capi_addlasterror();
  971. CryptReleaseContext(hprov, 0);
  972. return 0;
  973. }
  974. CAPI_trace(ctx, "Got max container len %d\n", buflen);
  975. if (buflen == 0)
  976. buflen = 1024;
  977. cname = OPENSSL_malloc(buflen);
  978. if (!cname) {
  979. CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE);
  980. goto err;
  981. }
  982. for (idx = 0;; idx++) {
  983. clen = buflen;
  984. cname[0] = 0;
  985. if (idx == 0)
  986. flags = CRYPT_FIRST;
  987. else
  988. flags = 0;
  989. if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, cname, &clen, flags)) {
  990. err = GetLastError();
  991. if (err == ERROR_NO_MORE_ITEMS)
  992. goto done;
  993. CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_ENUMCONTAINERS_ERROR);
  994. capi_adderror(err);
  995. goto err;
  996. }
  997. CAPI_trace(ctx, "Container name %s, len=%d, index=%d, flags=%d\n",
  998. cname, clen, idx, flags);
  999. if (!cname[0] && (clen == buflen)) {
  1000. CAPI_trace(ctx, "Enumerate bug: using workaround\n");
  1001. goto done;
  1002. }
  1003. BIO_printf(out, "%d. %s\n", idx, cname);
  1004. }
  1005. err:
  1006. ret = 0;
  1007. done:
  1008. if (cname)
  1009. OPENSSL_free(cname);
  1010. CryptReleaseContext(hprov, 0);
  1011. return ret;
  1012. }
  1013. CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX * ctx, PCCERT_CONTEXT cert)
  1014. {
  1015. DWORD len;
  1016. CRYPT_KEY_PROV_INFO *pinfo;
  1017. if (!CertGetCertificateContextProperty
  1018. (cert, CERT_KEY_PROV_INFO_PROP_ID, NULL, &len))
  1019. return NULL;
  1020. pinfo = OPENSSL_malloc(len);
  1021. if (!pinfo) {
  1022. CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, ERR_R_MALLOC_FAILURE);
  1023. return NULL;
  1024. }
  1025. if (!CertGetCertificateContextProperty
  1026. (cert, CERT_KEY_PROV_INFO_PROP_ID, pinfo, &len)) {
  1027. CAPIerr(CAPI_F_CAPI_GET_PROV_INFO,
  1028. CAPI_R_ERROR_GETTING_KEY_PROVIDER_INFO);
  1029. capi_addlasterror();
  1030. OPENSSL_free(pinfo);
  1031. return NULL;
  1032. }
  1033. return pinfo;
  1034. }
  1035. static void capi_dump_prov_info(CAPI_CTX * ctx, BIO *out,
  1036. CRYPT_KEY_PROV_INFO * pinfo)
  1037. {
  1038. char *provname = NULL, *contname = NULL;
  1039. if (!pinfo) {
  1040. BIO_printf(out, " No Private Key\n");
  1041. return;
  1042. }
  1043. provname = wide_to_asc(pinfo->pwszProvName);
  1044. contname = wide_to_asc(pinfo->pwszContainerName);
  1045. if (!provname || !contname)
  1046. goto err;
  1047. BIO_printf(out, " Private Key Info:\n");
  1048. BIO_printf(out, " Provider Name: %s, Provider Type %d\n", provname,
  1049. pinfo->dwProvType);
  1050. BIO_printf(out, " Container Name: %s, Key Type %d\n", contname,
  1051. pinfo->dwKeySpec);
  1052. err:
  1053. if (provname)
  1054. OPENSSL_free(provname);
  1055. if (contname)
  1056. OPENSSL_free(contname);
  1057. }
  1058. char *capi_cert_get_fname(CAPI_CTX * ctx, PCCERT_CONTEXT cert)
  1059. {
  1060. LPWSTR wfname;
  1061. DWORD dlen;
  1062. CAPI_trace(ctx, "capi_cert_get_fname\n");
  1063. if (!CertGetCertificateContextProperty
  1064. (cert, CERT_FRIENDLY_NAME_PROP_ID, NULL, &dlen))
  1065. return NULL;
  1066. wfname = OPENSSL_malloc(dlen);
  1067. if (CertGetCertificateContextProperty
  1068. (cert, CERT_FRIENDLY_NAME_PROP_ID, wfname, &dlen)) {
  1069. char *fname = wide_to_asc(wfname);
  1070. OPENSSL_free(wfname);
  1071. return fname;
  1072. }
  1073. CAPIerr(CAPI_F_CAPI_CERT_GET_FNAME, CAPI_R_ERROR_GETTING_FRIENDLY_NAME);
  1074. capi_addlasterror();
  1075. OPENSSL_free(wfname);
  1076. return NULL;
  1077. }
  1078. void capi_dump_cert(CAPI_CTX * ctx, BIO *out, PCCERT_CONTEXT cert)
  1079. {
  1080. X509 *x;
  1081. unsigned char *p;
  1082. unsigned long flags = ctx->dump_flags;
  1083. if (flags & CAPI_DMP_FNAME) {
  1084. char *fname;
  1085. fname = capi_cert_get_fname(ctx, cert);
  1086. if (fname) {
  1087. BIO_printf(out, " Friendly Name \"%s\"\n", fname);
  1088. OPENSSL_free(fname);
  1089. } else
  1090. BIO_printf(out, " <No Friendly Name>\n");
  1091. }
  1092. p = cert->pbCertEncoded;
  1093. x = d2i_X509(NULL, &p, cert->cbCertEncoded);
  1094. if (!x)
  1095. BIO_printf(out, " <Can't parse certificate>\n");
  1096. if (flags & CAPI_DMP_SUMMARY) {
  1097. BIO_printf(out, " Subject: ");
  1098. X509_NAME_print_ex(out, X509_get_subject_name(x), 0, XN_FLAG_ONELINE);
  1099. BIO_printf(out, "\n Issuer: ");
  1100. X509_NAME_print_ex(out, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE);
  1101. BIO_printf(out, "\n");
  1102. }
  1103. if (flags & CAPI_DMP_FULL)
  1104. X509_print_ex(out, x, XN_FLAG_ONELINE, 0);
  1105. if (flags & CAPI_DMP_PKEYINFO) {
  1106. CRYPT_KEY_PROV_INFO *pinfo;
  1107. pinfo = capi_get_prov_info(ctx, cert);
  1108. capi_dump_prov_info(ctx, out, pinfo);
  1109. if (pinfo)
  1110. OPENSSL_free(pinfo);
  1111. }
  1112. if (flags & CAPI_DMP_PEM)
  1113. PEM_write_bio_X509(out, x);
  1114. X509_free(x);
  1115. }
  1116. HCERTSTORE capi_open_store(CAPI_CTX * ctx, char *storename)
  1117. {
  1118. HCERTSTORE hstore;
  1119. if (!storename)
  1120. storename = ctx->storename;
  1121. if (!storename)
  1122. storename = "MY";
  1123. CAPI_trace(ctx, "Opening certificate store %s\n", storename);
  1124. hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, 0,
  1125. ctx->store_flags, storename);
  1126. if (!hstore) {
  1127. CAPIerr(CAPI_F_CAPI_OPEN_STORE, CAPI_R_ERROR_OPENING_STORE);
  1128. capi_addlasterror();
  1129. }
  1130. return hstore;
  1131. }
  1132. int capi_list_certs(CAPI_CTX * ctx, BIO *out, char *id)
  1133. {
  1134. char *storename;
  1135. int idx;
  1136. int ret = 1;
  1137. HCERTSTORE hstore;
  1138. PCCERT_CONTEXT cert = NULL;
  1139. storename = ctx->storename;
  1140. if (!storename)
  1141. storename = "MY";
  1142. CAPI_trace(ctx, "Listing certs for store %s\n", storename);
  1143. hstore = capi_open_store(ctx, storename);
  1144. if (!hstore)
  1145. return 0;
  1146. if (id) {
  1147. cert = capi_find_cert(ctx, id, hstore);
  1148. if (!cert) {
  1149. ret = 0;
  1150. goto err;
  1151. }
  1152. capi_dump_cert(ctx, out, cert);
  1153. CertFreeCertificateContext(cert);
  1154. } else {
  1155. for (idx = 0;; idx++) {
  1156. LPWSTR fname = NULL;
  1157. cert = CertEnumCertificatesInStore(hstore, cert);
  1158. if (!cert)
  1159. break;
  1160. BIO_printf(out, "Certificate %d\n", idx);
  1161. capi_dump_cert(ctx, out, cert);
  1162. }
  1163. }
  1164. err:
  1165. CertCloseStore(hstore, 0);
  1166. return ret;
  1167. }
  1168. static PCCERT_CONTEXT capi_find_cert(CAPI_CTX * ctx, const char *id,
  1169. HCERTSTORE hstore)
  1170. {
  1171. PCCERT_CONTEXT cert = NULL;
  1172. char *fname = NULL;
  1173. int match;
  1174. switch (ctx->lookup_method) {
  1175. case CAPI_LU_SUBSTR:
  1176. return CertFindCertificateInStore(hstore,
  1177. X509_ASN_ENCODING, 0,
  1178. CERT_FIND_SUBJECT_STR_A, id, NULL);
  1179. case CAPI_LU_FNAME:
  1180. for (;;) {
  1181. cert = CertEnumCertificatesInStore(hstore, cert);
  1182. if (!cert)
  1183. return NULL;
  1184. fname = capi_cert_get_fname(ctx, cert);
  1185. if (fname) {
  1186. if (strcmp(fname, id))
  1187. match = 0;
  1188. else
  1189. match = 1;
  1190. OPENSSL_free(fname);
  1191. if (match)
  1192. return cert;
  1193. }
  1194. }
  1195. default:
  1196. return NULL;
  1197. }
  1198. }
  1199. static CAPI_KEY *capi_get_key(CAPI_CTX * ctx, const char *contname,
  1200. char *provname, DWORD ptype, DWORD keyspec)
  1201. {
  1202. CAPI_KEY *key;
  1203. DWORD dwFlags = 0;
  1204. key = OPENSSL_malloc(sizeof(CAPI_KEY));
  1205. CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n",
  1206. contname, provname, ptype);
  1207. if (ctx->store_flags & CERT_SYSTEM_STORE_LOCAL_MACHINE)
  1208. dwFlags = CRYPT_MACHINE_KEYSET;
  1209. if (!CryptAcquireContextA
  1210. (&key->hprov, contname, provname, ptype, dwFlags)) {
  1211. CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
  1212. capi_addlasterror();
  1213. goto err;
  1214. }
  1215. if (!CryptGetUserKey(key->hprov, keyspec, &key->key)) {
  1216. CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_GETUSERKEY_ERROR);
  1217. capi_addlasterror();
  1218. CryptReleaseContext(key->hprov, 0);
  1219. goto err;
  1220. }
  1221. key->keyspec = keyspec;
  1222. key->pcert = NULL;
  1223. return key;
  1224. err:
  1225. OPENSSL_free(key);
  1226. return NULL;
  1227. }
  1228. static CAPI_KEY *capi_get_cert_key(CAPI_CTX * ctx, PCCERT_CONTEXT cert)
  1229. {
  1230. CAPI_KEY *key = NULL;
  1231. CRYPT_KEY_PROV_INFO *pinfo = NULL;
  1232. char *provname = NULL, *contname = NULL;
  1233. pinfo = capi_get_prov_info(ctx, cert);
  1234. if (!pinfo)
  1235. goto err;
  1236. provname = wide_to_asc(pinfo->pwszProvName);
  1237. contname = wide_to_asc(pinfo->pwszContainerName);
  1238. if (!provname || !contname)
  1239. goto err;
  1240. key = capi_get_key(ctx, contname, provname,
  1241. pinfo->dwProvType, pinfo->dwKeySpec);
  1242. err:
  1243. if (pinfo)
  1244. OPENSSL_free(pinfo);
  1245. if (provname)
  1246. OPENSSL_free(provname);
  1247. if (contname)
  1248. OPENSSL_free(contname);
  1249. return key;
  1250. }
  1251. CAPI_KEY *capi_find_key(CAPI_CTX * ctx, const char *id)
  1252. {
  1253. PCCERT_CONTEXT cert;
  1254. HCERTSTORE hstore;
  1255. CAPI_KEY *key = NULL;
  1256. switch (ctx->lookup_method) {
  1257. case CAPI_LU_SUBSTR:
  1258. case CAPI_LU_FNAME:
  1259. hstore = capi_open_store(ctx, NULL);
  1260. if (!hstore)
  1261. return NULL;
  1262. cert = capi_find_cert(ctx, id, hstore);
  1263. if (cert) {
  1264. key = capi_get_cert_key(ctx, cert);
  1265. CertFreeCertificateContext(cert);
  1266. }
  1267. CertCloseStore(hstore, 0);
  1268. break;
  1269. case CAPI_LU_CONTNAME:
  1270. key = capi_get_key(ctx, id, ctx->cspname, ctx->csptype, ctx->keytype);
  1271. break;
  1272. }
  1273. return key;
  1274. }
  1275. void capi_free_key(CAPI_KEY * key)
  1276. {
  1277. if (!key)
  1278. return;
  1279. CryptDestroyKey(key->key);
  1280. CryptReleaseContext(key->hprov, 0);
  1281. if (key->pcert)
  1282. CertFreeCertificateContext(key->pcert);
  1283. OPENSSL_free(key);
  1284. }
  1285. /* Initialize a CAPI_CTX structure */
  1286. static CAPI_CTX *capi_ctx_new()
  1287. {
  1288. CAPI_CTX *ctx;
  1289. ctx = OPENSSL_malloc(sizeof(CAPI_CTX));
  1290. if (!ctx) {
  1291. CAPIerr(CAPI_F_CAPI_CTX_NEW, ERR_R_MALLOC_FAILURE);
  1292. return NULL;
  1293. }
  1294. ctx->cspname = NULL;
  1295. ctx->csptype = PROV_RSA_FULL;
  1296. ctx->dump_flags = CAPI_DMP_SUMMARY | CAPI_DMP_FNAME;
  1297. ctx->keytype = AT_KEYEXCHANGE;
  1298. ctx->storename = NULL;
  1299. ctx->ssl_client_store = NULL;
  1300. ctx->store_flags = CERT_STORE_OPEN_EXISTING_FLAG |
  1301. CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_CURRENT_USER;
  1302. ctx->lookup_method = CAPI_LU_SUBSTR;
  1303. ctx->debug_level = 0;
  1304. ctx->debug_file = NULL;
  1305. ctx->client_cert_select = cert_select_simple;
  1306. return ctx;
  1307. }
  1308. static void capi_ctx_free(CAPI_CTX * ctx)
  1309. {
  1310. CAPI_trace(ctx, "Calling capi_ctx_free with %lx\n", ctx);
  1311. if (!ctx)
  1312. return;
  1313. if (ctx->cspname)
  1314. OPENSSL_free(ctx->cspname);
  1315. if (ctx->debug_file)
  1316. OPENSSL_free(ctx->debug_file);
  1317. if (ctx->storename)
  1318. OPENSSL_free(ctx->storename);
  1319. if (ctx->ssl_client_store)
  1320. OPENSSL_free(ctx->ssl_client_store);
  1321. OPENSSL_free(ctx);
  1322. }
  1323. static int capi_ctx_set_provname(CAPI_CTX * ctx, LPSTR pname, DWORD type,
  1324. int check)
  1325. {
  1326. CAPI_trace(ctx, "capi_ctx_set_provname, name=%s, type=%d\n", pname, type);
  1327. if (check) {
  1328. HCRYPTPROV hprov;
  1329. if (!CryptAcquireContextA(&hprov, NULL, pname, type,
  1330. CRYPT_VERIFYCONTEXT)) {
  1331. CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME,
  1332. CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
  1333. capi_addlasterror();
  1334. return 0;
  1335. }
  1336. CryptReleaseContext(hprov, 0);
  1337. }
  1338. if (ctx->cspname)
  1339. OPENSSL_free(ctx->cspname);
  1340. ctx->cspname = BUF_strdup(pname);
  1341. ctx->csptype = type;
  1342. return 1;
  1343. }
  1344. static int capi_ctx_set_provname_idx(CAPI_CTX * ctx, int idx)
  1345. {
  1346. LPSTR pname;
  1347. DWORD type;
  1348. int res;
  1349. if (capi_get_provname(ctx, &pname, &type, idx) != 1)
  1350. return 0;
  1351. res = capi_ctx_set_provname(ctx, pname, type, 0);
  1352. OPENSSL_free(pname);
  1353. return res;
  1354. }
  1355. static int cert_issuer_match(STACK_OF(X509_NAME) *ca_dn, X509 *x)
  1356. {
  1357. int i;
  1358. X509_NAME *nm;
  1359. /* Special case: empty list: match anything */
  1360. if (sk_X509_NAME_num(ca_dn) <= 0)
  1361. return 1;
  1362. for (i = 0; i < sk_X509_NAME_num(ca_dn); i++) {
  1363. nm = sk_X509_NAME_value(ca_dn, i);
  1364. if (!X509_NAME_cmp(nm, X509_get_issuer_name(x)))
  1365. return 1;
  1366. }
  1367. return 0;
  1368. }
  1369. static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl,
  1370. STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
  1371. EVP_PKEY **pkey, STACK_OF(X509) **pother,
  1372. UI_METHOD *ui_method,
  1373. void *callback_data)
  1374. {
  1375. STACK_OF(X509) *certs = NULL;
  1376. X509 *x;
  1377. char *storename;
  1378. const char *p;
  1379. int i, client_cert_idx;
  1380. HCERTSTORE hstore;
  1381. PCCERT_CONTEXT cert = NULL, excert = NULL;
  1382. CAPI_CTX *ctx;
  1383. CAPI_KEY *key;
  1384. ctx = ENGINE_get_ex_data(e, capi_idx);
  1385. *pcert = NULL;
  1386. *pkey = NULL;
  1387. storename = ctx->ssl_client_store;
  1388. if (!storename)
  1389. storename = "MY";
  1390. hstore = capi_open_store(ctx, storename);
  1391. if (!hstore)
  1392. return 0;
  1393. /* Enumerate all certificates collect any matches */
  1394. for (i = 0;; i++) {
  1395. cert = CertEnumCertificatesInStore(hstore, cert);
  1396. if (!cert)
  1397. break;
  1398. p = cert->pbCertEncoded;
  1399. x = d2i_X509(NULL, &p, cert->cbCertEncoded);
  1400. if (!x) {
  1401. CAPI_trace(ctx, "Can't Parse Certificate %d\n", i);
  1402. continue;
  1403. }
  1404. if (cert_issuer_match(ca_dn, x)
  1405. && X509_check_purpose(x, X509_PURPOSE_SSL_CLIENT, 0)) {
  1406. key = capi_get_cert_key(ctx, cert);
  1407. if (!key) {
  1408. X509_free(x);
  1409. continue;
  1410. }
  1411. /*
  1412. * Match found: attach extra data to it so we can retrieve the
  1413. * key later.
  1414. */
  1415. excert = CertDuplicateCertificateContext(cert);
  1416. key->pcert = excert;
  1417. X509_set_ex_data(x, cert_capi_idx, key);
  1418. if (!certs)
  1419. certs = sk_X509_new_null();
  1420. sk_X509_push(certs, x);
  1421. } else
  1422. X509_free(x);
  1423. }
  1424. if (cert)
  1425. CertFreeCertificateContext(cert);
  1426. if (hstore)
  1427. CertCloseStore(hstore, 0);
  1428. if (!certs)
  1429. return 0;
  1430. /* Select the appropriate certificate */
  1431. client_cert_idx = ctx->client_cert_select(e, ssl, certs);
  1432. /* Set the selected certificate and free the rest */
  1433. for (i = 0; i < sk_X509_num(certs); i++) {
  1434. x = sk_X509_value(certs, i);
  1435. if (i == client_cert_idx)
  1436. *pcert = x;
  1437. else {
  1438. key = X509_get_ex_data(x, cert_capi_idx);
  1439. capi_free_key(key);
  1440. X509_free(x);
  1441. }
  1442. }
  1443. sk_X509_free(certs);
  1444. if (!*pcert)
  1445. return 0;
  1446. /* Setup key for selected certificate */
  1447. key = X509_get_ex_data(*pcert, cert_capi_idx);
  1448. *pkey = capi_get_pkey(e, key);
  1449. X509_set_ex_data(*pcert, cert_capi_idx, NULL);
  1450. return 1;
  1451. }
  1452. /* Simple client cert selection function: always select first */
  1453. static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs)
  1454. {
  1455. return 0;
  1456. }
  1457. # ifdef OPENSSL_CAPIENG_DIALOG
  1458. /*
  1459. * More complex cert selection function, using standard function
  1460. * CryptUIDlgSelectCertificateFromStore() to produce a dialog box.
  1461. */
  1462. /*
  1463. * Definitions which are in cryptuiapi.h but this is not present in older
  1464. * versions of headers.
  1465. */
  1466. # ifndef CRYPTUI_SELECT_LOCATION_COLUMN
  1467. # define CRYPTUI_SELECT_LOCATION_COLUMN 0x000000010
  1468. # define CRYPTUI_SELECT_INTENDEDUSE_COLUMN 0x000000004
  1469. # endif
  1470. # define dlg_title L"OpenSSL Application SSL Client Certificate Selection"
  1471. # define dlg_prompt L"Select a certificate to use for authentication"
  1472. # define dlg_columns CRYPTUI_SELECT_LOCATION_COLUMN \
  1473. |CRYPTUI_SELECT_INTENDEDUSE_COLUMN
  1474. static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs)
  1475. {
  1476. X509 *x;
  1477. HCERTSTORE dstore;
  1478. PCCERT_CONTEXT cert;
  1479. CAPI_CTX *ctx;
  1480. CAPI_KEY *key;
  1481. HWND hwnd;
  1482. int i, idx = -1;
  1483. if (sk_X509_num(certs) == 1)
  1484. return 0;
  1485. ctx = ENGINE_get_ex_data(e, capi_idx);
  1486. /* Create an in memory store of certificates */
  1487. dstore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
  1488. CERT_STORE_CREATE_NEW_FLAG, NULL);
  1489. if (!dstore) {
  1490. CAPIerr(CAPI_F_CERT_SELECT_DIALOG, CAPI_R_ERROR_CREATING_STORE);
  1491. capi_addlasterror();
  1492. goto err;
  1493. }
  1494. /* Add all certificates to store */
  1495. for (i = 0; i < sk_X509_num(certs); i++) {
  1496. x = sk_X509_value(certs, i);
  1497. key = X509_get_ex_data(x, cert_capi_idx);
  1498. if (!CertAddCertificateContextToStore(dstore, key->pcert,
  1499. CERT_STORE_ADD_NEW, NULL)) {
  1500. CAPIerr(CAPI_F_CERT_SELECT_DIALOG, CAPI_R_ERROR_ADDING_CERT);
  1501. capi_addlasterror();
  1502. goto err;
  1503. }
  1504. }
  1505. hwnd = GetForegroundWindow();
  1506. if (!hwnd)
  1507. hwnd = GetActiveWindow();
  1508. if (!hwnd && ctx->getconswindow)
  1509. hwnd = ctx->getconswindow();
  1510. /* Call dialog to select one */
  1511. cert = ctx->certselectdlg(dstore, hwnd, dlg_title, dlg_prompt,
  1512. dlg_columns, 0, NULL);
  1513. /* Find matching cert from list */
  1514. if (cert) {
  1515. for (i = 0; i < sk_X509_num(certs); i++) {
  1516. x = sk_X509_value(certs, i);
  1517. key = X509_get_ex_data(x, cert_capi_idx);
  1518. if (CertCompareCertificate
  1519. (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, cert->pCertInfo,
  1520. key->pcert->pCertInfo)) {
  1521. idx = i;
  1522. break;
  1523. }
  1524. }
  1525. }
  1526. err:
  1527. if (dstore)
  1528. CertCloseStore(dstore, 0);
  1529. return idx;
  1530. }
  1531. # endif
  1532. #else /* !__COMPILE_CAPIENG */
  1533. # include <openssl/engine.h>
  1534. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  1535. OPENSSL_EXPORT
  1536. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
  1537. OPENSSL_EXPORT
  1538. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
  1539. {
  1540. return 0;
  1541. }
  1542. IMPLEMENT_DYNAMIC_CHECK_FN()
  1543. # else
  1544. void ENGINE_load_capi(void)
  1545. {
  1546. }
  1547. # endif
  1548. #endif