2
0

e_capi.c 55 KB

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