e_capi.c 50 KB

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