e_capi.c 45 KB

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