e_capi.c 55 KB

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