e_loader_attic.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  1. /*
  2. * Copyright 2016-2022 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. /* THIS ENGINE IS FOR TESTING PURPOSES ONLY. */
  10. /* This file has quite some overlap with providers/implementations/storemgmt/file_store.c */
  11. /* We need to use some engine deprecated APIs */
  12. #define OPENSSL_SUPPRESS_DEPRECATED
  13. #include "internal/e_os.h" /* for stat */
  14. #include <string.h>
  15. #include <sys/stat.h>
  16. #include <ctype.h>
  17. #include <assert.h>
  18. #include <openssl/bio.h>
  19. #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
  20. #include <openssl/err.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/pem.h>
  23. #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
  24. #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
  25. #include <openssl/safestack.h>
  26. #include <openssl/store.h>
  27. #include <openssl/ui.h>
  28. #include <openssl/engine.h>
  29. #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
  30. #include "internal/asn1.h" /* For asn1_d2i_read_bio */
  31. #include "internal/o_dir.h"
  32. #include "internal/cryptlib.h"
  33. #include "crypto/ctype.h" /* For ossl_isdigit */
  34. #include "crypto/pem.h" /* For PVK and "blob" PEM headers */
  35. #include "e_loader_attic_err.c"
  36. DEFINE_STACK_OF(OSSL_STORE_INFO)
  37. #ifndef S_ISDIR
  38. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  39. #endif
  40. /*-
  41. * Password prompting
  42. * ------------------
  43. */
  44. static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
  45. size_t maxsize, const char *desc, const char *info,
  46. void *data)
  47. {
  48. UI *ui = UI_new();
  49. char *prompt = NULL;
  50. if (ui == NULL) {
  51. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  52. return NULL;
  53. }
  54. if (ui_method != NULL)
  55. UI_set_method(ui, ui_method);
  56. UI_add_user_data(ui, data);
  57. if ((prompt = UI_construct_prompt(ui, desc, info)) == NULL) {
  58. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  59. pass = NULL;
  60. } else if (UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
  61. pass, 0, maxsize - 1) <= 0) {
  62. ATTICerr(0, ERR_R_UI_LIB);
  63. pass = NULL;
  64. } else {
  65. switch (UI_process(ui)) {
  66. case -2:
  67. ATTICerr(0, ATTIC_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
  68. pass = NULL;
  69. break;
  70. case -1:
  71. ATTICerr(0, ERR_R_UI_LIB);
  72. pass = NULL;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. OPENSSL_free(prompt);
  79. UI_free(ui);
  80. return pass;
  81. }
  82. struct pem_pass_data {
  83. const UI_METHOD *ui_method;
  84. void *data;
  85. const char *prompt_desc;
  86. const char *prompt_info;
  87. };
  88. static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
  89. const char *desc, const char *info,
  90. const UI_METHOD *ui_method, void *ui_data)
  91. {
  92. if (pass_data == NULL)
  93. return 0;
  94. pass_data->ui_method = ui_method;
  95. pass_data->data = ui_data;
  96. pass_data->prompt_desc = desc;
  97. pass_data->prompt_info = info;
  98. return 1;
  99. }
  100. /* This is used anywhere a pem_password_cb is needed */
  101. static int file_get_pem_pass(char *buf, int num, int w, void *data)
  102. {
  103. struct pem_pass_data *pass_data = data;
  104. char *pass = file_get_pass(pass_data->ui_method, buf, num,
  105. pass_data->prompt_desc, pass_data->prompt_info,
  106. pass_data->data);
  107. return pass == NULL ? 0 : strlen(pass);
  108. }
  109. /*
  110. * Check if |str| ends with |suffix| preceded by a space, and if it does,
  111. * return the index of that space. If there is no such suffix in |str|,
  112. * return -1.
  113. * For |str| == "FOO BAR" and |suffix| == "BAR", the returned value is 3.
  114. */
  115. static int check_suffix(const char *str, const char *suffix)
  116. {
  117. int str_len = strlen(str);
  118. int suffix_len = strlen(suffix) + 1;
  119. const char *p = NULL;
  120. if (suffix_len >= str_len)
  121. return -1;
  122. p = str + str_len - suffix_len;
  123. if (*p != ' '
  124. || strcmp(p + 1, suffix) != 0)
  125. return -1;
  126. return p - str;
  127. }
  128. /*
  129. * EMBEDDED is a special type of OSSL_STORE_INFO, specially for the file
  130. * handlers, so we define it internally. This uses the possibility to
  131. * create an OSSL_STORE_INFO with a generic data pointer and arbitrary
  132. * type number.
  133. *
  134. * This is used by a FILE_HANDLER's try_decode function to signal that it
  135. * has decoded the incoming blob into a new blob, and that the attempted
  136. * decoding should be immediately restarted with the new blob, using the
  137. * new PEM name.
  138. */
  139. /* Negative numbers are never used for public OSSL_STORE_INFO types */
  140. #define STORE_INFO_EMBEDDED -1
  141. /* This is the embedded data */
  142. struct embedded_st {
  143. BUF_MEM *blob;
  144. char *pem_name;
  145. };
  146. /* Helper functions */
  147. static struct embedded_st *get0_EMBEDDED(OSSL_STORE_INFO *info)
  148. {
  149. return OSSL_STORE_INFO_get0_data(STORE_INFO_EMBEDDED, info);
  150. }
  151. static void store_info_free(OSSL_STORE_INFO *info)
  152. {
  153. struct embedded_st *data;
  154. if (info != NULL && (data = get0_EMBEDDED(info)) != NULL) {
  155. BUF_MEM_free(data->blob);
  156. OPENSSL_free(data->pem_name);
  157. OPENSSL_free(data);
  158. }
  159. OSSL_STORE_INFO_free(info);
  160. }
  161. static OSSL_STORE_INFO *new_EMBEDDED(const char *new_pem_name,
  162. BUF_MEM *embedded)
  163. {
  164. OSSL_STORE_INFO *info = NULL;
  165. struct embedded_st *data = NULL;
  166. if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL
  167. || (info = OSSL_STORE_INFO_new(STORE_INFO_EMBEDDED, data)) == NULL) {
  168. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  169. OPENSSL_free(data);
  170. return NULL;
  171. }
  172. data->blob = embedded;
  173. data->pem_name =
  174. new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
  175. if (new_pem_name != NULL && data->pem_name == NULL) {
  176. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  177. store_info_free(info);
  178. info = NULL;
  179. }
  180. return info;
  181. }
  182. /*-
  183. * The file scheme decoders
  184. * ------------------------
  185. *
  186. * Each possible data type has its own decoder, which either operates
  187. * through a given PEM name, or attempts to decode to see if the blob
  188. * it's given is decodable for its data type. The assumption is that
  189. * only the correct data type will match the content.
  190. */
  191. /*-
  192. * The try_decode function is called to check if the blob of data can
  193. * be used by this handler, and if it can, decodes it into a supported
  194. * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
  195. * Input:
  196. * pem_name: If this blob comes from a PEM file, this holds
  197. * the PEM name. If it comes from another type of
  198. * file, this is NULL.
  199. * pem_header: If this blob comes from a PEM file, this holds
  200. * the PEM headers. If it comes from another type of
  201. * file, this is NULL.
  202. * blob: The blob of data to match with what this handler
  203. * can use.
  204. * len: The length of the blob.
  205. * handler_ctx: For a handler marked repeatable, this pointer can
  206. * be used to create a context for the handler. IT IS
  207. * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
  208. * THIS CONTEXT APPROPRIATELY, i.e. create on first call
  209. * and destroy when about to return NULL.
  210. * matchcount: A pointer to an int to count matches for this data.
  211. * Usually becomes 0 (no match) or 1 (match!), but may
  212. * be higher in the (unlikely) event that the data matches
  213. * more than one possibility. The int will always be
  214. * zero when the function is called.
  215. * ui_method: Application UI method for getting a password, pin
  216. * or any other interactive data.
  217. * ui_data: Application data to be passed to ui_method when
  218. * it's called.
  219. * libctx: The library context to be used if applicable
  220. * propq: The property query string for any algorithm fetches
  221. * Output:
  222. * a OSSL_STORE_INFO
  223. */
  224. typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
  225. const char *pem_header,
  226. const unsigned char *blob,
  227. size_t len, void **handler_ctx,
  228. int *matchcount,
  229. const UI_METHOD *ui_method,
  230. void *ui_data, const char *uri,
  231. OSSL_LIB_CTX *libctx,
  232. const char *propq);
  233. /*
  234. * The eof function should return 1 if there's no more data to be found
  235. * with the handler_ctx, otherwise 0. This is only used when the handler is
  236. * marked repeatable.
  237. */
  238. typedef int (*file_eof_fn)(void *handler_ctx);
  239. /*
  240. * The destroy_ctx function is used to destroy the handler_ctx that was
  241. * initiated by a repeatable try_decode function. This is only used when
  242. * the handler is marked repeatable.
  243. */
  244. typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
  245. typedef struct file_handler_st {
  246. const char *name;
  247. file_try_decode_fn try_decode;
  248. file_eof_fn eof;
  249. file_destroy_ctx_fn destroy_ctx;
  250. /* flags */
  251. int repeatable;
  252. } FILE_HANDLER;
  253. /*
  254. * PKCS#12 decoder. It operates by decoding all of the blob content,
  255. * extracting all the interesting data from it and storing them internally,
  256. * then serving them one piece at a time.
  257. */
  258. static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
  259. const char *pem_header,
  260. const unsigned char *blob,
  261. size_t len, void **pctx,
  262. int *matchcount,
  263. const UI_METHOD *ui_method,
  264. void *ui_data, const char *uri,
  265. OSSL_LIB_CTX *libctx,
  266. const char *propq)
  267. {
  268. OSSL_STORE_INFO *store_info = NULL;
  269. STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
  270. if (ctx == NULL) {
  271. /* Initial parsing */
  272. PKCS12 *p12;
  273. if (pem_name != NULL)
  274. /* No match, there is no PEM PKCS12 tag */
  275. return NULL;
  276. if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
  277. char *pass = NULL;
  278. char tpass[PEM_BUFSIZE];
  279. EVP_PKEY *pkey = NULL;
  280. X509 *cert = NULL;
  281. STACK_OF(X509) *chain = NULL;
  282. *matchcount = 1;
  283. if (!PKCS12_mac_present(p12)
  284. || PKCS12_verify_mac(p12, "", 0)
  285. || PKCS12_verify_mac(p12, NULL, 0)) {
  286. pass = "";
  287. } else {
  288. if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
  289. "PKCS12 import", uri,
  290. ui_data)) == NULL) {
  291. ATTICerr(0, ATTIC_R_PASSPHRASE_CALLBACK_ERROR);
  292. goto p12_end;
  293. }
  294. if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
  295. ATTICerr(0, ATTIC_R_ERROR_VERIFYING_PKCS12_MAC);
  296. goto p12_end;
  297. }
  298. }
  299. if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
  300. OSSL_STORE_INFO *osi_pkey = NULL;
  301. OSSL_STORE_INFO *osi_cert = NULL;
  302. OSSL_STORE_INFO *osi_ca = NULL;
  303. int ok = 1;
  304. if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL) {
  305. if (pkey != NULL) {
  306. if ((osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
  307. /* clearing pkey here avoids case distinctions */
  308. && (pkey = NULL) == NULL
  309. && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0)
  310. osi_pkey = NULL;
  311. else
  312. ok = 0;
  313. }
  314. if (ok && cert != NULL) {
  315. if ((osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
  316. /* clearing cert here avoids case distinctions */
  317. && (cert = NULL) == NULL
  318. && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0)
  319. osi_cert = NULL;
  320. else
  321. ok = 0;
  322. }
  323. while (ok && sk_X509_num(chain) > 0) {
  324. X509 *ca = sk_X509_value(chain, 0);
  325. if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) != NULL
  326. && sk_X509_shift(chain) != NULL
  327. && sk_OSSL_STORE_INFO_push(ctx, osi_ca) != 0)
  328. osi_ca = NULL;
  329. else
  330. ok = 0;
  331. }
  332. }
  333. EVP_PKEY_free(pkey);
  334. X509_free(cert);
  335. OSSL_STACK_OF_X509_free(chain);
  336. store_info_free(osi_pkey);
  337. store_info_free(osi_cert);
  338. store_info_free(osi_ca);
  339. if (!ok) {
  340. sk_OSSL_STORE_INFO_pop_free(ctx, store_info_free);
  341. ctx = NULL;
  342. }
  343. *pctx = ctx;
  344. }
  345. }
  346. p12_end:
  347. PKCS12_free(p12);
  348. if (ctx == NULL)
  349. return NULL;
  350. }
  351. *matchcount = 1;
  352. store_info = sk_OSSL_STORE_INFO_shift(ctx);
  353. return store_info;
  354. }
  355. static int eof_PKCS12(void *ctx_)
  356. {
  357. STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
  358. return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
  359. }
  360. static void destroy_ctx_PKCS12(void **pctx)
  361. {
  362. STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
  363. sk_OSSL_STORE_INFO_pop_free(ctx, store_info_free);
  364. *pctx = NULL;
  365. }
  366. static FILE_HANDLER PKCS12_handler = {
  367. "PKCS12",
  368. try_decode_PKCS12,
  369. eof_PKCS12,
  370. destroy_ctx_PKCS12,
  371. 1 /* repeatable */
  372. };
  373. /*
  374. * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
  375. * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
  376. * decoding process will then start over with the new blob.
  377. */
  378. static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
  379. const char *pem_header,
  380. const unsigned char *blob,
  381. size_t len, void **pctx,
  382. int *matchcount,
  383. const UI_METHOD *ui_method,
  384. void *ui_data,
  385. const char *uri,
  386. OSSL_LIB_CTX *libctx,
  387. const char *propq)
  388. {
  389. X509_SIG *p8 = NULL;
  390. char kbuf[PEM_BUFSIZE];
  391. char *pass = NULL;
  392. const X509_ALGOR *dalg = NULL;
  393. const ASN1_OCTET_STRING *doct = NULL;
  394. OSSL_STORE_INFO *store_info = NULL;
  395. BUF_MEM *mem = NULL;
  396. unsigned char *new_data = NULL;
  397. int new_data_len;
  398. if (pem_name != NULL) {
  399. if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
  400. return NULL;
  401. *matchcount = 1;
  402. }
  403. if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
  404. return NULL;
  405. *matchcount = 1;
  406. if ((mem = BUF_MEM_new()) == NULL) {
  407. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  408. goto nop8;
  409. }
  410. if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
  411. "PKCS8 decrypt pass phrase", uri,
  412. ui_data)) == NULL) {
  413. ATTICerr(0, ATTIC_R_BAD_PASSWORD_READ);
  414. goto nop8;
  415. }
  416. X509_SIG_get0(p8, &dalg, &doct);
  417. if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
  418. &new_data, &new_data_len, 0))
  419. goto nop8;
  420. mem->data = (char *)new_data;
  421. mem->max = mem->length = (size_t)new_data_len;
  422. X509_SIG_free(p8);
  423. p8 = NULL;
  424. store_info = new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
  425. if (store_info == NULL) {
  426. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  427. goto nop8;
  428. }
  429. return store_info;
  430. nop8:
  431. X509_SIG_free(p8);
  432. BUF_MEM_free(mem);
  433. return NULL;
  434. }
  435. static FILE_HANDLER PKCS8Encrypted_handler = {
  436. "PKCS8Encrypted",
  437. try_decode_PKCS8Encrypted
  438. };
  439. /*
  440. * Private key decoder. Decodes all sorts of private keys, both PKCS#8
  441. * encoded ones and old style PEM ones (with the key type is encoded into
  442. * the PEM name).
  443. */
  444. static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
  445. const char *pem_header,
  446. const unsigned char *blob,
  447. size_t len, void **pctx,
  448. int *matchcount,
  449. const UI_METHOD *ui_method,
  450. void *ui_data, const char *uri,
  451. OSSL_LIB_CTX *libctx,
  452. const char *propq)
  453. {
  454. OSSL_STORE_INFO *store_info = NULL;
  455. EVP_PKEY *pkey = NULL;
  456. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  457. if (pem_name != NULL) {
  458. if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
  459. PKCS8_PRIV_KEY_INFO *p8inf =
  460. d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
  461. *matchcount = 1;
  462. if (p8inf != NULL)
  463. pkey = EVP_PKCS82PKEY_ex(p8inf, libctx, propq);
  464. PKCS8_PRIV_KEY_INFO_free(p8inf);
  465. } else {
  466. int slen;
  467. int pkey_id;
  468. if ((slen = check_suffix(pem_name, "PRIVATE KEY")) > 0
  469. && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
  470. slen)) != NULL
  471. && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
  472. ameth)) {
  473. *matchcount = 1;
  474. pkey = d2i_PrivateKey_ex(pkey_id, NULL, &blob, len,
  475. libctx, propq);
  476. }
  477. }
  478. } else {
  479. int i;
  480. #ifndef OPENSSL_NO_ENGINE
  481. ENGINE *curengine = ENGINE_get_first();
  482. while (curengine != NULL) {
  483. ENGINE_PKEY_ASN1_METHS_PTR asn1meths =
  484. ENGINE_get_pkey_asn1_meths(curengine);
  485. if (asn1meths != NULL) {
  486. const int *nids = NULL;
  487. int nids_n = asn1meths(curengine, NULL, &nids, 0);
  488. for (i = 0; i < nids_n; i++) {
  489. EVP_PKEY_ASN1_METHOD *ameth2 = NULL;
  490. EVP_PKEY *tmp_pkey = NULL;
  491. const unsigned char *tmp_blob = blob;
  492. int pkey_id, pkey_flags;
  493. if (!asn1meths(curengine, &ameth2, NULL, nids[i])
  494. || !EVP_PKEY_asn1_get0_info(&pkey_id, NULL,
  495. &pkey_flags, NULL, NULL,
  496. ameth2)
  497. || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
  498. continue;
  499. ERR_set_mark(); /* prevent flooding error queue */
  500. tmp_pkey = d2i_PrivateKey_ex(pkey_id, NULL,
  501. &tmp_blob, len,
  502. libctx, propq);
  503. if (tmp_pkey != NULL) {
  504. if (pkey != NULL)
  505. EVP_PKEY_free(tmp_pkey);
  506. else
  507. pkey = tmp_pkey;
  508. (*matchcount)++;
  509. }
  510. ERR_pop_to_mark();
  511. }
  512. }
  513. curengine = ENGINE_get_next(curengine);
  514. }
  515. #endif
  516. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  517. EVP_PKEY *tmp_pkey = NULL;
  518. const unsigned char *tmp_blob = blob;
  519. int pkey_id, pkey_flags;
  520. ameth = EVP_PKEY_asn1_get0(i);
  521. if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, &pkey_flags, NULL,
  522. NULL, ameth)
  523. || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
  524. continue;
  525. ERR_set_mark(); /* prevent flooding error queue */
  526. tmp_pkey = d2i_PrivateKey_ex(pkey_id, NULL, &tmp_blob, len,
  527. libctx, propq);
  528. if (tmp_pkey != NULL) {
  529. if (pkey != NULL)
  530. EVP_PKEY_free(tmp_pkey);
  531. else
  532. pkey = tmp_pkey;
  533. (*matchcount)++;
  534. }
  535. ERR_pop_to_mark();
  536. }
  537. if (*matchcount > 1) {
  538. EVP_PKEY_free(pkey);
  539. pkey = NULL;
  540. }
  541. }
  542. if (pkey == NULL)
  543. /* No match */
  544. return NULL;
  545. store_info = OSSL_STORE_INFO_new_PKEY(pkey);
  546. if (store_info == NULL)
  547. EVP_PKEY_free(pkey);
  548. return store_info;
  549. }
  550. static FILE_HANDLER PrivateKey_handler = {
  551. "PrivateKey",
  552. try_decode_PrivateKey
  553. };
  554. /*
  555. * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
  556. */
  557. static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
  558. const char *pem_header,
  559. const unsigned char *blob,
  560. size_t len, void **pctx,
  561. int *matchcount,
  562. const UI_METHOD *ui_method,
  563. void *ui_data, const char *uri,
  564. OSSL_LIB_CTX *libctx,
  565. const char *propq)
  566. {
  567. OSSL_STORE_INFO *store_info = NULL;
  568. EVP_PKEY *pkey = NULL;
  569. if (pem_name != NULL) {
  570. if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
  571. /* No match */
  572. return NULL;
  573. *matchcount = 1;
  574. }
  575. if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
  576. *matchcount = 1;
  577. store_info = OSSL_STORE_INFO_new_PUBKEY(pkey);
  578. }
  579. return store_info;
  580. }
  581. static FILE_HANDLER PUBKEY_handler = {
  582. "PUBKEY",
  583. try_decode_PUBKEY
  584. };
  585. /*
  586. * Key parameter decoder.
  587. */
  588. static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
  589. const char *pem_header,
  590. const unsigned char *blob,
  591. size_t len, void **pctx,
  592. int *matchcount,
  593. const UI_METHOD *ui_method,
  594. void *ui_data, const char *uri,
  595. OSSL_LIB_CTX *libctx,
  596. const char *propq)
  597. {
  598. OSSL_STORE_INFO *store_info = NULL;
  599. EVP_PKEY *pkey = NULL;
  600. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  601. if (pem_name != NULL) {
  602. int slen;
  603. int pkey_id;
  604. if ((slen = check_suffix(pem_name, "PARAMETERS")) > 0
  605. && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL
  606. && EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL,
  607. ameth)) {
  608. *matchcount = 1;
  609. pkey = d2i_KeyParams(pkey_id, NULL, &blob, len);
  610. }
  611. } else {
  612. int i;
  613. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  614. EVP_PKEY *tmp_pkey = NULL;
  615. const unsigned char *tmp_blob = blob;
  616. int pkey_id, pkey_flags;
  617. ameth = EVP_PKEY_asn1_get0(i);
  618. if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, &pkey_flags, NULL,
  619. NULL, ameth)
  620. || (pkey_flags & ASN1_PKEY_ALIAS) != 0)
  621. continue;
  622. ERR_set_mark(); /* prevent flooding error queue */
  623. tmp_pkey = d2i_KeyParams(pkey_id, NULL, &tmp_blob, len);
  624. if (tmp_pkey != NULL) {
  625. if (pkey != NULL)
  626. EVP_PKEY_free(tmp_pkey);
  627. else
  628. pkey = tmp_pkey;
  629. (*matchcount)++;
  630. }
  631. ERR_pop_to_mark();
  632. }
  633. if (*matchcount > 1) {
  634. EVP_PKEY_free(pkey);
  635. pkey = NULL;
  636. }
  637. }
  638. if (pkey == NULL)
  639. /* No match */
  640. return NULL;
  641. store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
  642. if (store_info == NULL)
  643. EVP_PKEY_free(pkey);
  644. return store_info;
  645. }
  646. static FILE_HANDLER params_handler = {
  647. "params",
  648. try_decode_params
  649. };
  650. /*
  651. * X.509 certificate decoder.
  652. */
  653. static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
  654. const char *pem_header,
  655. const unsigned char *blob,
  656. size_t len, void **pctx,
  657. int *matchcount,
  658. const UI_METHOD *ui_method,
  659. void *ui_data,
  660. const char *uri,
  661. OSSL_LIB_CTX *libctx,
  662. const char *propq)
  663. {
  664. OSSL_STORE_INFO *store_info = NULL;
  665. X509 *cert = NULL;
  666. /*
  667. * In most cases, we can try to interpret the serialized data as a trusted
  668. * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
  669. * (just X509), but if the PEM name specifically declares it as a trusted
  670. * cert, then no fallback should be engaged. |ignore_trusted| tells if
  671. * the fallback can be used (1) or not (0).
  672. */
  673. int ignore_trusted = 1;
  674. if (pem_name != NULL) {
  675. if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
  676. ignore_trusted = 0;
  677. else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
  678. && strcmp(pem_name, PEM_STRING_X509) != 0)
  679. /* No match */
  680. return NULL;
  681. *matchcount = 1;
  682. }
  683. cert = X509_new_ex(libctx, propq);
  684. if (cert == NULL)
  685. return NULL;
  686. if ((d2i_X509_AUX(&cert, &blob, len)) != NULL
  687. || (ignore_trusted && (d2i_X509(&cert, &blob, len)) != NULL)) {
  688. *matchcount = 1;
  689. store_info = OSSL_STORE_INFO_new_CERT(cert);
  690. }
  691. if (store_info == NULL)
  692. X509_free(cert);
  693. return store_info;
  694. }
  695. static FILE_HANDLER X509Certificate_handler = {
  696. "X509Certificate",
  697. try_decode_X509Certificate
  698. };
  699. /*
  700. * X.509 CRL decoder.
  701. */
  702. static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
  703. const char *pem_header,
  704. const unsigned char *blob,
  705. size_t len, void **pctx,
  706. int *matchcount,
  707. const UI_METHOD *ui_method,
  708. void *ui_data, const char *uri,
  709. OSSL_LIB_CTX *libctx,
  710. const char *propq)
  711. {
  712. OSSL_STORE_INFO *store_info = NULL;
  713. X509_CRL *crl = NULL;
  714. if (pem_name != NULL) {
  715. if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
  716. /* No match */
  717. return NULL;
  718. *matchcount = 1;
  719. }
  720. if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
  721. *matchcount = 1;
  722. store_info = OSSL_STORE_INFO_new_CRL(crl);
  723. }
  724. if (store_info == NULL)
  725. X509_CRL_free(crl);
  726. return store_info;
  727. }
  728. static FILE_HANDLER X509CRL_handler = {
  729. "X509CRL",
  730. try_decode_X509CRL
  731. };
  732. /*
  733. * To finish it all off, we collect all the handlers.
  734. */
  735. static const FILE_HANDLER *file_handlers[] = {
  736. &PKCS12_handler,
  737. &PKCS8Encrypted_handler,
  738. &X509Certificate_handler,
  739. &X509CRL_handler,
  740. &params_handler,
  741. &PUBKEY_handler,
  742. &PrivateKey_handler,
  743. };
  744. /*-
  745. * The loader itself
  746. * -----------------
  747. */
  748. struct ossl_store_loader_ctx_st {
  749. char *uri; /* The URI we currently try to load */
  750. enum {
  751. is_raw = 0,
  752. is_pem,
  753. is_dir
  754. } type;
  755. int errcnt;
  756. #define FILE_FLAG_SECMEM (1<<0)
  757. #define FILE_FLAG_ATTACHED (1<<1)
  758. unsigned int flags;
  759. union {
  760. struct { /* Used with is_raw and is_pem */
  761. BIO *file;
  762. /*
  763. * The following are used when the handler is marked as
  764. * repeatable
  765. */
  766. const FILE_HANDLER *last_handler;
  767. void *last_handler_ctx;
  768. } file;
  769. struct { /* Used with is_dir */
  770. OPENSSL_DIR_CTX *ctx;
  771. int end_reached;
  772. /*
  773. * When a search expression is given, these are filled in.
  774. * |search_name| contains the file basename to look for.
  775. * The string is exactly 8 characters long.
  776. */
  777. char search_name[9];
  778. /*
  779. * The directory reading utility we have combines opening with
  780. * reading the first name. To make sure we can detect the end
  781. * at the right time, we read early and cache the name.
  782. */
  783. const char *last_entry;
  784. int last_errno;
  785. } dir;
  786. } _;
  787. /* Expected object type. May be unspecified */
  788. int expected_type;
  789. OSSL_LIB_CTX *libctx;
  790. char *propq;
  791. };
  792. static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
  793. {
  794. if (ctx == NULL)
  795. return;
  796. OPENSSL_free(ctx->propq);
  797. OPENSSL_free(ctx->uri);
  798. if (ctx->type != is_dir) {
  799. if (ctx->_.file.last_handler != NULL) {
  800. ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
  801. ctx->_.file.last_handler_ctx = NULL;
  802. ctx->_.file.last_handler = NULL;
  803. }
  804. }
  805. OPENSSL_free(ctx);
  806. }
  807. static int file_find_type(OSSL_STORE_LOADER_CTX *ctx)
  808. {
  809. BIO *buff = NULL;
  810. char peekbuf[4096] = { 0, };
  811. if ((buff = BIO_new(BIO_f_buffer())) == NULL)
  812. return 0;
  813. ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
  814. if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
  815. peekbuf[sizeof(peekbuf) - 1] = '\0';
  816. if (strstr(peekbuf, "-----BEGIN ") != NULL)
  817. ctx->type = is_pem;
  818. }
  819. return 1;
  820. }
  821. static OSSL_STORE_LOADER_CTX *file_open_ex
  822. (const OSSL_STORE_LOADER *loader, const char *uri,
  823. OSSL_LIB_CTX *libctx, const char *propq,
  824. const UI_METHOD *ui_method, void *ui_data)
  825. {
  826. OSSL_STORE_LOADER_CTX *ctx = NULL;
  827. struct stat st;
  828. struct {
  829. const char *path;
  830. unsigned int check_absolute:1;
  831. } path_data[2];
  832. size_t path_data_n = 0, i;
  833. const char *path, *p = uri, *q;
  834. /*
  835. * First step, just take the URI as is.
  836. */
  837. path_data[path_data_n].check_absolute = 0;
  838. path_data[path_data_n++].path = uri;
  839. /*
  840. * Second step, if the URI appears to start with the "file" scheme,
  841. * extract the path and make that the second path to check.
  842. * There's a special case if the URI also contains an authority, then
  843. * the full URI shouldn't be used as a path anywhere.
  844. */
  845. if (CHECK_AND_SKIP_CASE_PREFIX(p, "file:")) {
  846. q = p;
  847. if (CHECK_AND_SKIP_PREFIX(q, "//")) {
  848. path_data_n--; /* Invalidate using the full URI */
  849. if (CHECK_AND_SKIP_CASE_PREFIX(q, "localhost/")
  850. || CHECK_AND_SKIP_PREFIX(q, "/")) {
  851. p = q - 1;
  852. } else {
  853. ATTICerr(0, ATTIC_R_URI_AUTHORITY_UNSUPPORTED);
  854. return NULL;
  855. }
  856. }
  857. path_data[path_data_n].check_absolute = 1;
  858. #ifdef _WIN32
  859. /* Windows "file:" URIs with a drive letter start with a '/' */
  860. if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
  861. char c = tolower(p[1]);
  862. if (c >= 'a' && c <= 'z') {
  863. p++;
  864. /* We know it's absolute, so no need to check */
  865. path_data[path_data_n].check_absolute = 0;
  866. }
  867. }
  868. #endif
  869. path_data[path_data_n++].path = p;
  870. }
  871. for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
  872. /*
  873. * If the scheme "file" was an explicit part of the URI, the path must
  874. * be absolute. So says RFC 8089
  875. */
  876. if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
  877. ATTICerr(0, ATTIC_R_PATH_MUST_BE_ABSOLUTE);
  878. ERR_add_error_data(1, path_data[i].path);
  879. return NULL;
  880. }
  881. if (stat(path_data[i].path, &st) < 0) {
  882. ERR_raise_data(ERR_LIB_SYS, errno,
  883. "calling stat(%s)",
  884. path_data[i].path);
  885. } else {
  886. path = path_data[i].path;
  887. }
  888. }
  889. if (path == NULL) {
  890. return NULL;
  891. }
  892. /* Successfully found a working path */
  893. ctx = OPENSSL_zalloc(sizeof(*ctx));
  894. if (ctx == NULL) {
  895. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  896. return NULL;
  897. }
  898. ctx->uri = OPENSSL_strdup(uri);
  899. if (ctx->uri == NULL) {
  900. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  901. goto err;
  902. }
  903. if (S_ISDIR(st.st_mode)) {
  904. ctx->type = is_dir;
  905. ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
  906. ctx->_.dir.last_errno = errno;
  907. if (ctx->_.dir.last_entry == NULL) {
  908. if (ctx->_.dir.last_errno != 0) {
  909. ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
  910. goto err;
  911. }
  912. ctx->_.dir.end_reached = 1;
  913. }
  914. } else if ((ctx->_.file.file = BIO_new_file(path, "rb")) == NULL
  915. || !file_find_type(ctx)) {
  916. BIO_free_all(ctx->_.file.file);
  917. goto err;
  918. }
  919. if (propq != NULL) {
  920. ctx->propq = OPENSSL_strdup(propq);
  921. if (ctx->propq == NULL) {
  922. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  923. goto err;
  924. }
  925. }
  926. ctx->libctx = libctx;
  927. return ctx;
  928. err:
  929. OSSL_STORE_LOADER_CTX_free(ctx);
  930. return NULL;
  931. }
  932. static OSSL_STORE_LOADER_CTX *file_open
  933. (const OSSL_STORE_LOADER *loader, const char *uri,
  934. const UI_METHOD *ui_method, void *ui_data)
  935. {
  936. return file_open_ex(loader, uri, NULL, NULL, ui_method, ui_data);
  937. }
  938. static OSSL_STORE_LOADER_CTX *file_attach
  939. (const OSSL_STORE_LOADER *loader, BIO *bp,
  940. OSSL_LIB_CTX *libctx, const char *propq,
  941. const UI_METHOD *ui_method, void *ui_data)
  942. {
  943. OSSL_STORE_LOADER_CTX *ctx = NULL;
  944. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
  945. || (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)) {
  946. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  947. OSSL_STORE_LOADER_CTX_free(ctx);
  948. return NULL;
  949. }
  950. ctx->libctx = libctx;
  951. ctx->flags |= FILE_FLAG_ATTACHED;
  952. ctx->_.file.file = bp;
  953. if (!file_find_type(ctx)) {
  954. /* Safety measure */
  955. ctx->_.file.file = NULL;
  956. goto err;
  957. }
  958. return ctx;
  959. err:
  960. OSSL_STORE_LOADER_CTX_free(ctx);
  961. return NULL;
  962. }
  963. static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
  964. {
  965. int ret = 1;
  966. switch (cmd) {
  967. case OSSL_STORE_C_USE_SECMEM:
  968. {
  969. int on = *(va_arg(args, int *));
  970. switch (on) {
  971. case 0:
  972. ctx->flags &= ~FILE_FLAG_SECMEM;
  973. break;
  974. case 1:
  975. ctx->flags |= FILE_FLAG_SECMEM;
  976. break;
  977. default:
  978. ATTICerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
  979. ret = 0;
  980. break;
  981. }
  982. }
  983. break;
  984. default:
  985. break;
  986. }
  987. return ret;
  988. }
  989. static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
  990. {
  991. ctx->expected_type = expected;
  992. return 1;
  993. }
  994. static int file_find(OSSL_STORE_LOADER_CTX *ctx,
  995. const OSSL_STORE_SEARCH *search)
  996. {
  997. /*
  998. * If ctx == NULL, the library is looking to know if this loader supports
  999. * the given search type.
  1000. */
  1001. if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
  1002. unsigned long hash = 0;
  1003. if (ctx == NULL)
  1004. return 1;
  1005. if (ctx->type != is_dir) {
  1006. ATTICerr(0, ATTIC_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
  1007. return 0;
  1008. }
  1009. hash = X509_NAME_hash_ex(OSSL_STORE_SEARCH_get0_name(search),
  1010. NULL, NULL, NULL);
  1011. BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
  1012. "%08lx", hash);
  1013. return 1;
  1014. }
  1015. if (ctx != NULL)
  1016. ATTICerr(0, ATTIC_R_UNSUPPORTED_SEARCH_TYPE);
  1017. return 0;
  1018. }
  1019. static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
  1020. const char *pem_name,
  1021. const char *pem_header,
  1022. unsigned char *data, size_t len,
  1023. const UI_METHOD *ui_method,
  1024. void *ui_data, int *matchcount)
  1025. {
  1026. OSSL_STORE_INFO *result = NULL;
  1027. BUF_MEM *new_mem = NULL;
  1028. char *new_pem_name = NULL;
  1029. int t = 0;
  1030. again:
  1031. {
  1032. size_t i = 0;
  1033. void *handler_ctx = NULL;
  1034. const FILE_HANDLER **matching_handlers =
  1035. OPENSSL_zalloc(sizeof(*matching_handlers)
  1036. * OSSL_NELEM(file_handlers));
  1037. if (matching_handlers == NULL) {
  1038. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  1039. goto err;
  1040. }
  1041. *matchcount = 0;
  1042. for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
  1043. const FILE_HANDLER *handler = file_handlers[i];
  1044. int try_matchcount = 0;
  1045. void *tmp_handler_ctx = NULL;
  1046. OSSL_STORE_INFO *tmp_result;
  1047. unsigned long err;
  1048. ERR_set_mark();
  1049. tmp_result =
  1050. handler->try_decode(pem_name, pem_header, data, len,
  1051. &tmp_handler_ctx, &try_matchcount,
  1052. ui_method, ui_data, ctx->uri,
  1053. ctx->libctx, ctx->propq);
  1054. /* avoid flooding error queue with low-level ASN.1 parse errors */
  1055. err = ERR_peek_last_error();
  1056. if (ERR_GET_LIB(err) == ERR_LIB_ASN1
  1057. && ERR_GET_REASON(err) == ERR_R_NESTED_ASN1_ERROR)
  1058. ERR_pop_to_mark();
  1059. else
  1060. ERR_clear_last_mark();
  1061. if (try_matchcount > 0) {
  1062. matching_handlers[*matchcount] = handler;
  1063. if (handler_ctx)
  1064. handler->destroy_ctx(&handler_ctx);
  1065. handler_ctx = tmp_handler_ctx;
  1066. if ((*matchcount += try_matchcount) > 1) {
  1067. /* more than one match => ambiguous, kill any result */
  1068. store_info_free(result);
  1069. store_info_free(tmp_result);
  1070. if (handler->destroy_ctx != NULL)
  1071. handler->destroy_ctx(&handler_ctx);
  1072. handler_ctx = NULL;
  1073. tmp_result = NULL;
  1074. result = NULL;
  1075. }
  1076. if (result == NULL)
  1077. result = tmp_result;
  1078. if (result == NULL) /* e.g., PKCS#12 file decryption error */
  1079. break;
  1080. }
  1081. }
  1082. if (result != NULL
  1083. && *matchcount == 1 && matching_handlers[0]->repeatable) {
  1084. ctx->_.file.last_handler = matching_handlers[0];
  1085. ctx->_.file.last_handler_ctx = handler_ctx;
  1086. }
  1087. OPENSSL_free(matching_handlers);
  1088. }
  1089. err:
  1090. OPENSSL_free(new_pem_name);
  1091. BUF_MEM_free(new_mem);
  1092. if (result != NULL
  1093. && (t = OSSL_STORE_INFO_get_type(result)) == STORE_INFO_EMBEDDED) {
  1094. struct embedded_st *embedded = get0_EMBEDDED(result);
  1095. /* "steal" the embedded data */
  1096. pem_name = new_pem_name = embedded->pem_name;
  1097. new_mem = embedded->blob;
  1098. data = (unsigned char *)new_mem->data;
  1099. len = new_mem->length;
  1100. embedded->pem_name = NULL;
  1101. embedded->blob = NULL;
  1102. store_info_free(result);
  1103. result = NULL;
  1104. goto again;
  1105. }
  1106. return result;
  1107. }
  1108. static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
  1109. const UI_METHOD *ui_method,
  1110. void *ui_data)
  1111. {
  1112. OSSL_STORE_INFO *result = NULL;
  1113. int try_matchcount = 0;
  1114. if (ctx->_.file.last_handler != NULL) {
  1115. result =
  1116. ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
  1117. &ctx->_.file.last_handler_ctx,
  1118. &try_matchcount,
  1119. ui_method, ui_data, ctx->uri,
  1120. ctx->libctx, ctx->propq);
  1121. if (result == NULL) {
  1122. ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
  1123. ctx->_.file.last_handler_ctx = NULL;
  1124. ctx->_.file.last_handler = NULL;
  1125. }
  1126. }
  1127. return result;
  1128. }
  1129. static void pem_free_flag(void *pem_data, int secure, size_t num)
  1130. {
  1131. if (secure)
  1132. OPENSSL_secure_clear_free(pem_data, num);
  1133. else
  1134. OPENSSL_free(pem_data);
  1135. }
  1136. static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
  1137. unsigned char **data, long *len,
  1138. const UI_METHOD *ui_method, void *ui_data,
  1139. const char *uri, int secure)
  1140. {
  1141. int i = secure
  1142. ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
  1143. PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
  1144. : PEM_read_bio(bp, pem_name, pem_header, data, len);
  1145. if (i <= 0)
  1146. return 0;
  1147. /*
  1148. * 10 is the number of characters in "Proc-Type:", which
  1149. * PEM_get_EVP_CIPHER_INFO() requires to be present.
  1150. * If the PEM header has less characters than that, it's
  1151. * not worth spending cycles on it.
  1152. */
  1153. if (strlen(*pem_header) > 10) {
  1154. EVP_CIPHER_INFO cipher;
  1155. struct pem_pass_data pass_data;
  1156. if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
  1157. || !file_fill_pem_pass_data(&pass_data, "PEM pass phrase", uri,
  1158. ui_method, ui_data)
  1159. || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
  1160. &pass_data)) {
  1161. return 0;
  1162. }
  1163. }
  1164. return 1;
  1165. }
  1166. static OSSL_STORE_INFO *file_try_read_msblob(BIO *bp, int *matchcount)
  1167. {
  1168. OSSL_STORE_INFO *result = NULL;
  1169. int ispub = -1;
  1170. {
  1171. unsigned int magic = 0, bitlen = 0;
  1172. int isdss = 0;
  1173. unsigned char peekbuf[16] = { 0, };
  1174. const unsigned char *p = peekbuf;
  1175. if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
  1176. return 0;
  1177. if (ossl_do_blob_header(&p, sizeof(peekbuf), &magic, &bitlen,
  1178. &isdss, &ispub) <= 0)
  1179. return 0;
  1180. }
  1181. (*matchcount)++;
  1182. {
  1183. EVP_PKEY *tmp = ispub
  1184. ? b2i_PublicKey_bio(bp)
  1185. : b2i_PrivateKey_bio(bp);
  1186. if (tmp == NULL
  1187. || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
  1188. EVP_PKEY_free(tmp);
  1189. return 0;
  1190. }
  1191. }
  1192. return result;
  1193. }
  1194. static OSSL_STORE_INFO *file_try_read_PVK(BIO *bp, const UI_METHOD *ui_method,
  1195. void *ui_data, const char *uri,
  1196. int *matchcount)
  1197. {
  1198. OSSL_STORE_INFO *result = NULL;
  1199. {
  1200. unsigned int saltlen = 0, keylen = 0;
  1201. unsigned char peekbuf[24] = { 0, };
  1202. const unsigned char *p = peekbuf;
  1203. if (BIO_buffer_peek(bp, peekbuf, sizeof(peekbuf)) <= 0)
  1204. return 0;
  1205. if (!ossl_do_PVK_header(&p, sizeof(peekbuf), 0, &saltlen, &keylen))
  1206. return 0;
  1207. }
  1208. (*matchcount)++;
  1209. {
  1210. EVP_PKEY *tmp = NULL;
  1211. struct pem_pass_data pass_data;
  1212. if (!file_fill_pem_pass_data(&pass_data, "PVK pass phrase", uri,
  1213. ui_method, ui_data)
  1214. || (tmp = b2i_PVK_bio(bp, file_get_pem_pass, &pass_data)) == NULL
  1215. || (result = OSSL_STORE_INFO_new_PKEY(tmp)) == NULL) {
  1216. EVP_PKEY_free(tmp);
  1217. return 0;
  1218. }
  1219. }
  1220. return result;
  1221. }
  1222. static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
  1223. {
  1224. BUF_MEM *mem = NULL;
  1225. if (asn1_d2i_read_bio(bp, &mem) < 0)
  1226. return 0;
  1227. *data = (unsigned char *)mem->data;
  1228. *len = (long)mem->length;
  1229. OPENSSL_free(mem);
  1230. return 1;
  1231. }
  1232. static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
  1233. char **data)
  1234. {
  1235. assert(name != NULL);
  1236. assert(data != NULL);
  1237. {
  1238. const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/";
  1239. long calculated_length = strlen(ctx->uri) + strlen(pathsep)
  1240. + strlen(name) + 1 /* \0 */;
  1241. *data = OPENSSL_zalloc(calculated_length);
  1242. if (*data == NULL) {
  1243. ATTICerr(0, ERR_R_MALLOC_FAILURE);
  1244. return 0;
  1245. }
  1246. OPENSSL_strlcat(*data, ctx->uri, calculated_length);
  1247. OPENSSL_strlcat(*data, pathsep, calculated_length);
  1248. OPENSSL_strlcat(*data, name, calculated_length);
  1249. }
  1250. return 1;
  1251. }
  1252. static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
  1253. {
  1254. const char *p = NULL;
  1255. size_t len = strlen(ctx->_.dir.search_name);
  1256. /* If there are no search criteria, all names are accepted */
  1257. if (ctx->_.dir.search_name[0] == '\0')
  1258. return 1;
  1259. /* If the expected type isn't supported, no name is accepted */
  1260. if (ctx->expected_type != 0
  1261. && ctx->expected_type != OSSL_STORE_INFO_CERT
  1262. && ctx->expected_type != OSSL_STORE_INFO_CRL)
  1263. return 0;
  1264. /*
  1265. * First, check the basename
  1266. */
  1267. if (OPENSSL_strncasecmp(name, ctx->_.dir.search_name, len) != 0
  1268. || name[len] != '.')
  1269. return 0;
  1270. p = &name[len + 1];
  1271. /*
  1272. * Then, if the expected type is a CRL, check that the extension starts
  1273. * with 'r'
  1274. */
  1275. if (*p == 'r') {
  1276. p++;
  1277. if (ctx->expected_type != 0
  1278. && ctx->expected_type != OSSL_STORE_INFO_CRL)
  1279. return 0;
  1280. } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
  1281. return 0;
  1282. }
  1283. /*
  1284. * Last, check that the rest of the extension is a decimal number, at
  1285. * least one digit long.
  1286. */
  1287. if (!isdigit(*p))
  1288. return 0;
  1289. while (isdigit(*p))
  1290. p++;
  1291. #ifdef __VMS
  1292. /*
  1293. * One extra step here, check for a possible generation number.
  1294. */
  1295. if (*p == ';')
  1296. for (p++; *p != '\0'; p++)
  1297. if (!ossl_isdigit(*p))
  1298. break;
  1299. #endif
  1300. /*
  1301. * If we've reached the end of the string at this point, we've successfully
  1302. * found a fitting file name.
  1303. */
  1304. return *p == '\0';
  1305. }
  1306. static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
  1307. static int file_error(OSSL_STORE_LOADER_CTX *ctx);
  1308. static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
  1309. const UI_METHOD *ui_method,
  1310. void *ui_data)
  1311. {
  1312. OSSL_STORE_INFO *result = NULL;
  1313. ctx->errcnt = 0;
  1314. if (ctx->type == is_dir) {
  1315. do {
  1316. char *newname = NULL;
  1317. if (ctx->_.dir.last_entry == NULL) {
  1318. if (!ctx->_.dir.end_reached) {
  1319. assert(ctx->_.dir.last_errno != 0);
  1320. ERR_raise(ERR_LIB_SYS, ctx->_.dir.last_errno);
  1321. ctx->errcnt++;
  1322. }
  1323. return NULL;
  1324. }
  1325. if (ctx->_.dir.last_entry[0] != '.'
  1326. && file_name_check(ctx, ctx->_.dir.last_entry)
  1327. && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
  1328. return NULL;
  1329. /*
  1330. * On the first call (with a NULL context), OPENSSL_DIR_read()
  1331. * cares about the second argument. On the following calls, it
  1332. * only cares that it isn't NULL. Therefore, we can safely give
  1333. * it our URI here.
  1334. */
  1335. ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, ctx->uri);
  1336. ctx->_.dir.last_errno = errno;
  1337. if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
  1338. ctx->_.dir.end_reached = 1;
  1339. if (newname != NULL
  1340. && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
  1341. OPENSSL_free(newname);
  1342. ATTICerr(0, ERR_R_OSSL_STORE_LIB);
  1343. return NULL;
  1344. }
  1345. } while (result == NULL && !file_eof(ctx));
  1346. } else {
  1347. int matchcount = -1;
  1348. again:
  1349. result = file_load_try_repeat(ctx, ui_method, ui_data);
  1350. if (result != NULL)
  1351. return result;
  1352. if (file_eof(ctx))
  1353. return NULL;
  1354. do {
  1355. char *pem_name = NULL; /* PEM record name */
  1356. char *pem_header = NULL; /* PEM record header */
  1357. unsigned char *data = NULL; /* DER encoded data */
  1358. long len = 0; /* DER encoded data length */
  1359. matchcount = -1;
  1360. if (ctx->type == is_pem) {
  1361. if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
  1362. &data, &len, ui_method, ui_data, ctx->uri,
  1363. (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
  1364. ctx->errcnt++;
  1365. goto endloop;
  1366. }
  1367. } else {
  1368. if ((result = file_try_read_msblob(ctx->_.file.file,
  1369. &matchcount)) != NULL
  1370. || (result = file_try_read_PVK(ctx->_.file.file,
  1371. ui_method, ui_data, ctx->uri,
  1372. &matchcount)) != NULL)
  1373. goto endloop;
  1374. if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
  1375. ctx->errcnt++;
  1376. goto endloop;
  1377. }
  1378. }
  1379. result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
  1380. ui_method, ui_data, &matchcount);
  1381. if (result != NULL)
  1382. goto endloop;
  1383. /*
  1384. * If a PEM name matches more than one handler, the handlers are
  1385. * badly coded.
  1386. */
  1387. if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
  1388. ctx->errcnt++;
  1389. goto endloop;
  1390. }
  1391. if (matchcount > 1) {
  1392. ATTICerr(0, ATTIC_R_AMBIGUOUS_CONTENT_TYPE);
  1393. } else if (matchcount == 1) {
  1394. /*
  1395. * If there are other errors on the stack, they already show
  1396. * what the problem is.
  1397. */
  1398. if (ERR_peek_error() == 0) {
  1399. ATTICerr(0, ATTIC_R_UNSUPPORTED_CONTENT_TYPE);
  1400. if (pem_name != NULL)
  1401. ERR_add_error_data(3, "PEM type is '", pem_name, "'");
  1402. }
  1403. }
  1404. if (matchcount > 0)
  1405. ctx->errcnt++;
  1406. endloop:
  1407. pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
  1408. pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
  1409. pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
  1410. } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
  1411. /* We bail out on ambiguity */
  1412. if (matchcount > 1) {
  1413. store_info_free(result);
  1414. return NULL;
  1415. }
  1416. if (result != NULL
  1417. && ctx->expected_type != 0
  1418. && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
  1419. store_info_free(result);
  1420. goto again;
  1421. }
  1422. }
  1423. return result;
  1424. }
  1425. static int file_error(OSSL_STORE_LOADER_CTX *ctx)
  1426. {
  1427. return ctx->errcnt > 0;
  1428. }
  1429. static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
  1430. {
  1431. if (ctx->type == is_dir)
  1432. return ctx->_.dir.end_reached;
  1433. if (ctx->_.file.last_handler != NULL
  1434. && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
  1435. return 0;
  1436. return BIO_eof(ctx->_.file.file);
  1437. }
  1438. static int file_close(OSSL_STORE_LOADER_CTX *ctx)
  1439. {
  1440. if ((ctx->flags & FILE_FLAG_ATTACHED) == 0) {
  1441. if (ctx->type == is_dir)
  1442. OPENSSL_DIR_end(&ctx->_.dir.ctx);
  1443. else
  1444. BIO_free_all(ctx->_.file.file);
  1445. } else {
  1446. /*
  1447. * Because file_attach() called file_find_type(), we know that a
  1448. * BIO_f_buffer() has been pushed on top of the regular BIO.
  1449. */
  1450. BIO *buff = ctx->_.file.file;
  1451. /* Detach buff */
  1452. (void)BIO_pop(ctx->_.file.file);
  1453. /* Safety measure */
  1454. ctx->_.file.file = NULL;
  1455. BIO_free(buff);
  1456. }
  1457. OSSL_STORE_LOADER_CTX_free(ctx);
  1458. return 1;
  1459. }
  1460. /*-
  1461. * ENGINE management
  1462. */
  1463. static const char *loader_attic_id = "loader_attic";
  1464. static const char *loader_attic_name = "'file:' loader";
  1465. static OSSL_STORE_LOADER *loader_attic = NULL;
  1466. static int loader_attic_init(ENGINE *e)
  1467. {
  1468. return 1;
  1469. }
  1470. static int loader_attic_finish(ENGINE *e)
  1471. {
  1472. return 1;
  1473. }
  1474. static int loader_attic_destroy(ENGINE *e)
  1475. {
  1476. OSSL_STORE_LOADER *loader = OSSL_STORE_unregister_loader("file");
  1477. if (loader == NULL)
  1478. return 0;
  1479. ERR_unload_ATTIC_strings();
  1480. OSSL_STORE_LOADER_free(loader);
  1481. return 1;
  1482. }
  1483. static int bind_loader_attic(ENGINE *e)
  1484. {
  1485. /* Ensure the ATTIC error handling is set up on best effort basis */
  1486. ERR_load_ATTIC_strings();
  1487. if (/* Create the OSSL_STORE_LOADER */
  1488. (loader_attic = OSSL_STORE_LOADER_new(e, "file")) == NULL
  1489. || !OSSL_STORE_LOADER_set_open_ex(loader_attic, file_open_ex)
  1490. || !OSSL_STORE_LOADER_set_open(loader_attic, file_open)
  1491. || !OSSL_STORE_LOADER_set_attach(loader_attic, file_attach)
  1492. || !OSSL_STORE_LOADER_set_ctrl(loader_attic, file_ctrl)
  1493. || !OSSL_STORE_LOADER_set_expect(loader_attic, file_expect)
  1494. || !OSSL_STORE_LOADER_set_find(loader_attic, file_find)
  1495. || !OSSL_STORE_LOADER_set_load(loader_attic, file_load)
  1496. || !OSSL_STORE_LOADER_set_eof(loader_attic, file_eof)
  1497. || !OSSL_STORE_LOADER_set_error(loader_attic, file_error)
  1498. || !OSSL_STORE_LOADER_set_close(loader_attic, file_close)
  1499. /* Init the engine itself */
  1500. || !ENGINE_set_id(e, loader_attic_id)
  1501. || !ENGINE_set_name(e, loader_attic_name)
  1502. || !ENGINE_set_destroy_function(e, loader_attic_destroy)
  1503. || !ENGINE_set_init_function(e, loader_attic_init)
  1504. || !ENGINE_set_finish_function(e, loader_attic_finish)
  1505. /* Finally, register the method with libcrypto */
  1506. || !OSSL_STORE_register_loader(loader_attic)) {
  1507. OSSL_STORE_LOADER_free(loader_attic);
  1508. loader_attic = NULL;
  1509. ATTICerr(0, ATTIC_R_INIT_FAILED);
  1510. return 0;
  1511. }
  1512. return 1;
  1513. }
  1514. #ifdef OPENSSL_NO_DYNAMIC_ENGINE
  1515. # error "Only allowed as dynamically shared object"
  1516. #endif
  1517. static int bind_helper(ENGINE *e, const char *id)
  1518. {
  1519. if (id && (strcmp(id, loader_attic_id) != 0))
  1520. return 0;
  1521. if (!bind_loader_attic(e))
  1522. return 0;
  1523. return 1;
  1524. }
  1525. IMPLEMENT_DYNAMIC_CHECK_FN()
  1526. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)