2
0

e_capi.c 51 KB

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