loader_file.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /*
  2. * Copyright 2016-2018 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. #include "e_os.h"
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <ctype.h>
  13. #include <assert.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
  16. #include <openssl/err.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
  20. #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
  21. #include <openssl/safestack.h>
  22. #include <openssl/store.h>
  23. #include <openssl/ui.h>
  24. #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
  25. #include "internal/asn1_int.h"
  26. #include "internal/ctype.h"
  27. #include "internal/o_dir.h"
  28. #include "internal/cryptlib.h"
  29. #include "internal/store_int.h"
  30. #include "store_locl.h"
  31. #ifdef _WIN32
  32. # define stat _stat
  33. #endif
  34. #ifndef S_ISDIR
  35. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  36. #endif
  37. /*-
  38. * Password prompting
  39. * ------------------
  40. */
  41. static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
  42. size_t maxsize, const char *prompt_info, void *data)
  43. {
  44. UI *ui = UI_new();
  45. char *prompt = NULL;
  46. if (ui == NULL) {
  47. OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
  48. return NULL;
  49. }
  50. if (ui_method != NULL)
  51. UI_set_method(ui, ui_method);
  52. UI_add_user_data(ui, data);
  53. if ((prompt = UI_construct_prompt(ui, "pass phrase",
  54. prompt_info)) == NULL) {
  55. OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
  56. pass = NULL;
  57. } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
  58. pass, 0, maxsize - 1)) {
  59. OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
  60. pass = NULL;
  61. } else {
  62. switch (UI_process(ui)) {
  63. case -2:
  64. OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
  65. OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
  66. pass = NULL;
  67. break;
  68. case -1:
  69. OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
  70. pass = NULL;
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. OPENSSL_free(prompt);
  77. UI_free(ui);
  78. return pass;
  79. }
  80. struct pem_pass_data {
  81. const UI_METHOD *ui_method;
  82. void *data;
  83. const char *prompt_info;
  84. };
  85. static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
  86. const char *prompt_info,
  87. const UI_METHOD *ui_method, void *ui_data)
  88. {
  89. if (pass_data == NULL)
  90. return 0;
  91. pass_data->ui_method = ui_method;
  92. pass_data->data = ui_data;
  93. pass_data->prompt_info = prompt_info;
  94. return 1;
  95. }
  96. /* This is used anywhere a pem_password_cb is needed */
  97. static int file_get_pem_pass(char *buf, int num, int w, void *data)
  98. {
  99. struct pem_pass_data *pass_data = data;
  100. char *pass = file_get_pass(pass_data->ui_method, buf, num,
  101. pass_data->prompt_info, pass_data->data);
  102. return pass == NULL ? 0 : strlen(pass);
  103. }
  104. /*-
  105. * The file scheme decoders
  106. * ------------------------
  107. *
  108. * Each possible data type has its own decoder, which either operates
  109. * through a given PEM name, or attempts to decode to see if the blob
  110. * it's given is decodable for its data type. The assumption is that
  111. * only the correct data type will match the content.
  112. */
  113. /*-
  114. * The try_decode function is called to check if the blob of data can
  115. * be used by this handler, and if it can, decodes it into a supported
  116. * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
  117. * Input:
  118. * pem_name: If this blob comes from a PEM file, this holds
  119. * the PEM name. If it comes from another type of
  120. * file, this is NULL.
  121. * pem_header: If this blob comes from a PEM file, this holds
  122. * the PEM headers. If it comes from another type of
  123. * file, this is NULL.
  124. * blob: The blob of data to match with what this handler
  125. * can use.
  126. * len: The length of the blob.
  127. * handler_ctx: For a handler marked repeatable, this pointer can
  128. * be used to create a context for the handler. IT IS
  129. * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
  130. * THIS CONTEXT APPROPRIATELY, i.e. create on first call
  131. * and destroy when about to return NULL.
  132. * matchcount: A pointer to an int to count matches for this data.
  133. * Usually becomes 0 (no match) or 1 (match!), but may
  134. * be higher in the (unlikely) event that the data matches
  135. * more than one possibility. The int will always be
  136. * zero when the function is called.
  137. * ui_method: Application UI method for getting a password, pin
  138. * or any other interactive data.
  139. * ui_data: Application data to be passed to ui_method when
  140. * it's called.
  141. * Output:
  142. * a OSSL_STORE_INFO
  143. */
  144. typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
  145. const char *pem_header,
  146. const unsigned char *blob,
  147. size_t len, void **handler_ctx,
  148. int *matchcount,
  149. const UI_METHOD *ui_method,
  150. void *ui_data);
  151. /*
  152. * The eof function should return 1 if there's no more data to be found
  153. * with the handler_ctx, otherwise 0. This is only used when the handler is
  154. * marked repeatable.
  155. */
  156. typedef int (*file_eof_fn)(void *handler_ctx);
  157. /*
  158. * The destroy_ctx function is used to destroy the handler_ctx that was
  159. * intiated by a repeatable try_decode fuction. This is only used when
  160. * the handler is marked repeatable.
  161. */
  162. typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
  163. typedef struct file_handler_st {
  164. const char *name;
  165. file_try_decode_fn try_decode;
  166. file_eof_fn eof;
  167. file_destroy_ctx_fn destroy_ctx;
  168. /* flags */
  169. int repeatable;
  170. } FILE_HANDLER;
  171. /*
  172. * PKCS#12 decoder. It operates by decoding all of the blob content,
  173. * extracting all the interesting data from it and storing them internally,
  174. * then serving them one piece at a time.
  175. */
  176. static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
  177. const char *pem_header,
  178. const unsigned char *blob,
  179. size_t len, void **pctx,
  180. int *matchcount,
  181. const UI_METHOD *ui_method,
  182. void *ui_data)
  183. {
  184. OSSL_STORE_INFO *store_info = NULL;
  185. STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
  186. if (ctx == NULL) {
  187. /* Initial parsing */
  188. PKCS12 *p12;
  189. int ok = 0;
  190. if (pem_name != NULL)
  191. /* No match, there is no PEM PKCS12 tag */
  192. return NULL;
  193. if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
  194. char *pass = NULL;
  195. char tpass[PEM_BUFSIZE];
  196. EVP_PKEY *pkey = NULL;
  197. X509 *cert = NULL;
  198. STACK_OF(X509) *chain = NULL;
  199. *matchcount = 1;
  200. if (PKCS12_verify_mac(p12, "", 0)
  201. || PKCS12_verify_mac(p12, NULL, 0)) {
  202. pass = "";
  203. } else {
  204. if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
  205. "PKCS12 import password",
  206. ui_data)) == NULL) {
  207. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
  208. OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
  209. goto p12_end;
  210. }
  211. if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
  212. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
  213. OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
  214. goto p12_end;
  215. }
  216. }
  217. if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
  218. OSSL_STORE_INFO *osi_pkey = NULL;
  219. OSSL_STORE_INFO *osi_cert = NULL;
  220. OSSL_STORE_INFO *osi_ca = NULL;
  221. if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL
  222. && (osi_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
  223. && sk_OSSL_STORE_INFO_push(ctx, osi_pkey) != 0
  224. && (osi_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
  225. && sk_OSSL_STORE_INFO_push(ctx, osi_cert) != 0) {
  226. ok = 1;
  227. osi_pkey = NULL;
  228. osi_cert = NULL;
  229. while(sk_X509_num(chain) > 0) {
  230. X509 *ca = sk_X509_value(chain, 0);
  231. if ((osi_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL
  232. || sk_OSSL_STORE_INFO_push(ctx, osi_ca) == 0) {
  233. ok = 0;
  234. break;
  235. }
  236. osi_ca = NULL;
  237. (void)sk_X509_shift(chain);
  238. }
  239. }
  240. if (!ok) {
  241. OSSL_STORE_INFO_free(osi_ca);
  242. OSSL_STORE_INFO_free(osi_cert);
  243. OSSL_STORE_INFO_free(osi_pkey);
  244. sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
  245. EVP_PKEY_free(pkey);
  246. X509_free(cert);
  247. sk_X509_pop_free(chain, X509_free);
  248. ctx = NULL;
  249. }
  250. *pctx = ctx;
  251. }
  252. }
  253. p12_end:
  254. PKCS12_free(p12);
  255. if (!ok)
  256. return NULL;
  257. }
  258. if (ctx != NULL) {
  259. *matchcount = 1;
  260. store_info = sk_OSSL_STORE_INFO_shift(ctx);
  261. }
  262. return store_info;
  263. }
  264. static int eof_PKCS12(void *ctx_)
  265. {
  266. STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
  267. return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
  268. }
  269. static void destroy_ctx_PKCS12(void **pctx)
  270. {
  271. STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
  272. sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
  273. *pctx = NULL;
  274. }
  275. static FILE_HANDLER PKCS12_handler = {
  276. "PKCS12",
  277. try_decode_PKCS12,
  278. eof_PKCS12,
  279. destroy_ctx_PKCS12,
  280. 1 /* repeatable */
  281. };
  282. /*
  283. * Encrypted PKCS#8 decoder. It operates by just decrypting the given blob
  284. * into a new blob, which is returned as an EMBEDDED STORE_INFO. The whole
  285. * decoding process will then start over with the new blob.
  286. */
  287. static OSSL_STORE_INFO *try_decode_PKCS8Encrypted(const char *pem_name,
  288. const char *pem_header,
  289. const unsigned char *blob,
  290. size_t len, void **pctx,
  291. int *matchcount,
  292. const UI_METHOD *ui_method,
  293. void *ui_data)
  294. {
  295. X509_SIG *p8 = NULL;
  296. char kbuf[PEM_BUFSIZE];
  297. char *pass = NULL;
  298. const X509_ALGOR *dalg = NULL;
  299. const ASN1_OCTET_STRING *doct = NULL;
  300. OSSL_STORE_INFO *store_info = NULL;
  301. BUF_MEM *mem = NULL;
  302. unsigned char *new_data = NULL;
  303. int new_data_len;
  304. if (pem_name != NULL) {
  305. if (strcmp(pem_name, PEM_STRING_PKCS8) != 0)
  306. return NULL;
  307. *matchcount = 1;
  308. }
  309. if ((p8 = d2i_X509_SIG(NULL, &blob, len)) == NULL)
  310. return NULL;
  311. *matchcount = 1;
  312. if ((mem = BUF_MEM_new()) == NULL) {
  313. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
  314. ERR_R_MALLOC_FAILURE);
  315. goto nop8;
  316. }
  317. if ((pass = file_get_pass(ui_method, kbuf, PEM_BUFSIZE,
  318. "PKCS8 decrypt password", ui_data)) == NULL) {
  319. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
  320. OSSL_STORE_R_BAD_PASSWORD_READ);
  321. goto nop8;
  322. }
  323. X509_SIG_get0(p8, &dalg, &doct);
  324. if (!PKCS12_pbe_crypt(dalg, pass, strlen(pass), doct->data, doct->length,
  325. &new_data, &new_data_len, 0))
  326. goto nop8;
  327. mem->data = (char *)new_data;
  328. mem->max = mem->length = (size_t)new_data_len;
  329. X509_SIG_free(p8);
  330. store_info = ossl_store_info_new_EMBEDDED(PEM_STRING_PKCS8INF, mem);
  331. if (store_info == NULL) {
  332. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS8ENCRYPTED,
  333. ERR_R_MALLOC_FAILURE);
  334. goto nop8;
  335. }
  336. return store_info;
  337. nop8:
  338. X509_SIG_free(p8);
  339. BUF_MEM_free(mem);
  340. return NULL;
  341. }
  342. static FILE_HANDLER PKCS8Encrypted_handler = {
  343. "PKCS8Encrypted",
  344. try_decode_PKCS8Encrypted
  345. };
  346. /*
  347. * Private key decoder. Decodes all sorts of private keys, both PKCS#8
  348. * encoded ones and old style PEM ones (with the key type is encoded into
  349. * the PEM name).
  350. */
  351. int pem_check_suffix(const char *pem_str, const char *suffix);
  352. static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
  353. const char *pem_header,
  354. const unsigned char *blob,
  355. size_t len, void **pctx,
  356. int *matchcount,
  357. const UI_METHOD *ui_method,
  358. void *ui_data)
  359. {
  360. OSSL_STORE_INFO *store_info = NULL;
  361. EVP_PKEY *pkey = NULL;
  362. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  363. if (pem_name != NULL) {
  364. if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) {
  365. PKCS8_PRIV_KEY_INFO *p8inf =
  366. d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len);
  367. *matchcount = 1;
  368. if (p8inf != NULL)
  369. pkey = EVP_PKCS82PKEY(p8inf);
  370. PKCS8_PRIV_KEY_INFO_free(p8inf);
  371. } else {
  372. int slen;
  373. if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
  374. && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name,
  375. slen)) != NULL) {
  376. *matchcount = 1;
  377. pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
  378. }
  379. }
  380. } else {
  381. int i;
  382. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  383. EVP_PKEY *tmp_pkey = NULL;
  384. const unsigned char *tmp_blob = blob;
  385. ameth = EVP_PKEY_asn1_get0(i);
  386. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  387. continue;
  388. tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len);
  389. if (tmp_pkey != NULL) {
  390. if (pkey != NULL)
  391. EVP_PKEY_free(tmp_pkey);
  392. else
  393. pkey = tmp_pkey;
  394. (*matchcount)++;
  395. }
  396. }
  397. if (*matchcount > 1) {
  398. EVP_PKEY_free(pkey);
  399. pkey = NULL;
  400. }
  401. }
  402. if (pkey == NULL)
  403. /* No match */
  404. return NULL;
  405. store_info = OSSL_STORE_INFO_new_PKEY(pkey);
  406. if (store_info == NULL)
  407. EVP_PKEY_free(pkey);
  408. return store_info;
  409. }
  410. static FILE_HANDLER PrivateKey_handler = {
  411. "PrivateKey",
  412. try_decode_PrivateKey
  413. };
  414. /*
  415. * Public key decoder. Only supports SubjectPublicKeyInfo formated keys.
  416. */
  417. static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
  418. const char *pem_header,
  419. const unsigned char *blob,
  420. size_t len, void **pctx,
  421. int *matchcount,
  422. const UI_METHOD *ui_method,
  423. void *ui_data)
  424. {
  425. OSSL_STORE_INFO *store_info = NULL;
  426. EVP_PKEY *pkey = NULL;
  427. if (pem_name != NULL) {
  428. if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
  429. /* No match */
  430. return NULL;
  431. *matchcount = 1;
  432. }
  433. if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
  434. *matchcount = 1;
  435. store_info = OSSL_STORE_INFO_new_PKEY(pkey);
  436. }
  437. return store_info;
  438. }
  439. static FILE_HANDLER PUBKEY_handler = {
  440. "PUBKEY",
  441. try_decode_PUBKEY
  442. };
  443. /*
  444. * Key parameter decoder.
  445. */
  446. static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
  447. const char *pem_header,
  448. const unsigned char *blob,
  449. size_t len, void **pctx,
  450. int *matchcount,
  451. const UI_METHOD *ui_method,
  452. void *ui_data)
  453. {
  454. OSSL_STORE_INFO *store_info = NULL;
  455. int slen = 0;
  456. EVP_PKEY *pkey = NULL;
  457. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  458. int ok = 0;
  459. if (pem_name != NULL) {
  460. if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
  461. return NULL;
  462. *matchcount = 1;
  463. }
  464. if (slen > 0) {
  465. if ((pkey = EVP_PKEY_new()) == NULL) {
  466. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
  467. return NULL;
  468. }
  469. if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
  470. && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
  471. && ameth->param_decode != NULL
  472. && ameth->param_decode(pkey, &blob, len))
  473. ok = 1;
  474. } else {
  475. int i;
  476. EVP_PKEY *tmp_pkey = NULL;
  477. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  478. const unsigned char *tmp_blob = blob;
  479. if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
  480. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
  481. break;
  482. }
  483. ameth = EVP_PKEY_asn1_get0(i);
  484. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  485. continue;
  486. if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
  487. && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
  488. && ameth->param_decode != NULL
  489. && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
  490. if (pkey != NULL)
  491. EVP_PKEY_free(tmp_pkey);
  492. else
  493. pkey = tmp_pkey;
  494. tmp_pkey = NULL;
  495. (*matchcount)++;
  496. }
  497. }
  498. EVP_PKEY_free(tmp_pkey);
  499. if (*matchcount == 1) {
  500. ok = 1;
  501. }
  502. }
  503. if (ok)
  504. store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
  505. if (store_info == NULL)
  506. EVP_PKEY_free(pkey);
  507. return store_info;
  508. }
  509. static FILE_HANDLER params_handler = {
  510. "params",
  511. try_decode_params
  512. };
  513. /*
  514. * X.509 certificate decoder.
  515. */
  516. static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
  517. const char *pem_header,
  518. const unsigned char *blob,
  519. size_t len, void **pctx,
  520. int *matchcount,
  521. const UI_METHOD *ui_method,
  522. void *ui_data)
  523. {
  524. OSSL_STORE_INFO *store_info = NULL;
  525. X509 *cert = NULL;
  526. /*
  527. * In most cases, we can try to interpret the serialized data as a trusted
  528. * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
  529. * (just X509), but if the PEM name specifically declares it as a trusted
  530. * cert, then no fallback should be engaged. |ignore_trusted| tells if
  531. * the fallback can be used (1) or not (0).
  532. */
  533. int ignore_trusted = 1;
  534. if (pem_name != NULL) {
  535. if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
  536. ignore_trusted = 0;
  537. else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
  538. && strcmp(pem_name, PEM_STRING_X509) != 0)
  539. /* No match */
  540. return NULL;
  541. *matchcount = 1;
  542. }
  543. if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
  544. || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
  545. *matchcount = 1;
  546. store_info = OSSL_STORE_INFO_new_CERT(cert);
  547. }
  548. if (store_info == NULL)
  549. X509_free(cert);
  550. return store_info;
  551. }
  552. static FILE_HANDLER X509Certificate_handler = {
  553. "X509Certificate",
  554. try_decode_X509Certificate
  555. };
  556. /*
  557. * X.509 CRL decoder.
  558. */
  559. static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
  560. const char *pem_header,
  561. const unsigned char *blob,
  562. size_t len, void **pctx,
  563. int *matchcount,
  564. const UI_METHOD *ui_method,
  565. void *ui_data)
  566. {
  567. OSSL_STORE_INFO *store_info = NULL;
  568. X509_CRL *crl = NULL;
  569. if (pem_name != NULL) {
  570. if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
  571. /* No match */
  572. return NULL;
  573. *matchcount = 1;
  574. }
  575. if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
  576. *matchcount = 1;
  577. store_info = OSSL_STORE_INFO_new_CRL(crl);
  578. }
  579. if (store_info == NULL)
  580. X509_CRL_free(crl);
  581. return store_info;
  582. }
  583. static FILE_HANDLER X509CRL_handler = {
  584. "X509CRL",
  585. try_decode_X509CRL
  586. };
  587. /*
  588. * To finish it all off, we collect all the handlers.
  589. */
  590. static const FILE_HANDLER *file_handlers[] = {
  591. &PKCS12_handler,
  592. &PKCS8Encrypted_handler,
  593. &X509Certificate_handler,
  594. &X509CRL_handler,
  595. &params_handler,
  596. &PUBKEY_handler,
  597. &PrivateKey_handler,
  598. };
  599. /*-
  600. * The loader itself
  601. * -----------------
  602. */
  603. struct ossl_store_loader_ctx_st {
  604. enum {
  605. is_raw = 0,
  606. is_pem,
  607. is_dir
  608. } type;
  609. int errcnt;
  610. #define FILE_FLAG_SECMEM (1<<0)
  611. unsigned int flags;
  612. union {
  613. struct { /* Used with is_raw and is_pem */
  614. BIO *file;
  615. /*
  616. * The following are used when the handler is marked as
  617. * repeatable
  618. */
  619. const FILE_HANDLER *last_handler;
  620. void *last_handler_ctx;
  621. } file;
  622. struct { /* Used with is_dir */
  623. OPENSSL_DIR_CTX *ctx;
  624. int end_reached;
  625. char *uri;
  626. /*
  627. * When a search expression is given, these are filled in.
  628. * |search_name| contains the file basename to look for.
  629. * The string is exactly 8 characters long.
  630. */
  631. char search_name[9];
  632. /*
  633. * The directory reading utility we have combines opening with
  634. * reading the first name. To make sure we can detect the end
  635. * at the right time, we read early and cache the name.
  636. */
  637. const char *last_entry;
  638. int last_errno;
  639. } dir;
  640. } _;
  641. /* Expected object type. May be unspecified */
  642. int expected_type;
  643. };
  644. static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
  645. {
  646. if (ctx->type == is_dir) {
  647. OPENSSL_free(ctx->_.dir.uri);
  648. } else {
  649. if (ctx->_.file.last_handler != NULL) {
  650. ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
  651. ctx->_.file.last_handler_ctx = NULL;
  652. ctx->_.file.last_handler = NULL;
  653. }
  654. }
  655. OPENSSL_free(ctx);
  656. }
  657. static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
  658. const char *uri,
  659. const UI_METHOD *ui_method,
  660. void *ui_data)
  661. {
  662. OSSL_STORE_LOADER_CTX *ctx = NULL;
  663. struct stat st;
  664. struct {
  665. const char *path;
  666. unsigned int check_absolute:1;
  667. } path_data[2];
  668. size_t path_data_n = 0, i;
  669. const char *path;
  670. /*
  671. * First step, just take the URI as is.
  672. */
  673. path_data[path_data_n].check_absolute = 0;
  674. path_data[path_data_n++].path = uri;
  675. /*
  676. * Second step, if the URI appears to start with the 'file' scheme,
  677. * extract the path and make that the second path to check.
  678. * There's a special case if the URI also contains an authority, then
  679. * the full URI shouldn't be used as a path anywhere.
  680. */
  681. if (strncasecmp(uri, "file:", 5) == 0) {
  682. const char *p = &uri[5];
  683. if (strncmp(&uri[5], "//", 2) == 0) {
  684. path_data_n--; /* Invalidate using the full URI */
  685. if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
  686. p = &uri[16];
  687. } else if (uri[7] == '/') {
  688. p = &uri[7];
  689. } else {
  690. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
  691. OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
  692. return NULL;
  693. }
  694. }
  695. path_data[path_data_n].check_absolute = 1;
  696. #ifdef _WIN32
  697. /* Windows file: URIs with a drive letter start with a / */
  698. if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
  699. char c = ossl_tolower(p[1]);
  700. if (c >= 'a' && c <= 'z') {
  701. p++;
  702. /* We know it's absolute, so no need to check */
  703. path_data[path_data_n].check_absolute = 0;
  704. }
  705. }
  706. #endif
  707. path_data[path_data_n++].path = p;
  708. }
  709. for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
  710. /*
  711. * If the scheme "file" was an explicit part of the URI, the path must
  712. * be absolute. So says RFC 8089
  713. */
  714. if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
  715. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
  716. OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
  717. ERR_add_error_data(1, path_data[i].path);
  718. return NULL;
  719. }
  720. if (stat(path_data[i].path, &st) < 0) {
  721. SYSerr(SYS_F_STAT, errno);
  722. ERR_add_error_data(1, path_data[i].path);
  723. } else {
  724. path = path_data[i].path;
  725. }
  726. }
  727. if (path == NULL) {
  728. return NULL;
  729. }
  730. /* Successfully found a working path, clear possible collected errors */
  731. ERR_clear_error();
  732. ctx = OPENSSL_zalloc(sizeof(*ctx));
  733. if (ctx == NULL) {
  734. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
  735. return NULL;
  736. }
  737. if (S_ISDIR(st.st_mode)) {
  738. /*
  739. * Try to copy everything, even if we know that some of them must be
  740. * NULL for the moment. This prevents errors in the future, when more
  741. * components may be used.
  742. */
  743. ctx->_.dir.uri = OPENSSL_strdup(uri);
  744. ctx->type = is_dir;
  745. if (ctx->_.dir.uri == NULL)
  746. goto err;
  747. ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
  748. ctx->_.dir.last_errno = errno;
  749. if (ctx->_.dir.last_entry == NULL) {
  750. if (ctx->_.dir.last_errno != 0) {
  751. char errbuf[256];
  752. errno = ctx->_.dir.last_errno;
  753. openssl_strerror_r(errno, errbuf, sizeof(errbuf));
  754. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
  755. ERR_add_error_data(1, errbuf);
  756. goto err;
  757. }
  758. ctx->_.dir.end_reached = 1;
  759. }
  760. } else {
  761. BIO *buff = NULL;
  762. char peekbuf[4096] = { 0, };
  763. if ((buff = BIO_new(BIO_f_buffer())) == NULL
  764. || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
  765. BIO_free_all(buff);
  766. goto err;
  767. }
  768. ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
  769. if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
  770. peekbuf[sizeof(peekbuf) - 1] = '\0';
  771. if (strstr(peekbuf, "-----BEGIN ") != NULL)
  772. ctx->type = is_pem;
  773. }
  774. }
  775. return ctx;
  776. err:
  777. OSSL_STORE_LOADER_CTX_free(ctx);
  778. return NULL;
  779. }
  780. static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
  781. {
  782. int ret = 1;
  783. switch (cmd) {
  784. case OSSL_STORE_C_USE_SECMEM:
  785. {
  786. int on = *(va_arg(args, int *));
  787. switch (on) {
  788. case 0:
  789. ctx->flags &= ~FILE_FLAG_SECMEM;
  790. break;
  791. case 1:
  792. ctx->flags |= FILE_FLAG_SECMEM;
  793. break;
  794. default:
  795. OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
  796. ERR_R_PASSED_INVALID_ARGUMENT);
  797. ret = 0;
  798. break;
  799. }
  800. }
  801. break;
  802. default:
  803. break;
  804. }
  805. return ret;
  806. }
  807. static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
  808. {
  809. ctx->expected_type = expected;
  810. return 1;
  811. }
  812. static int file_find(OSSL_STORE_LOADER_CTX *ctx, OSSL_STORE_SEARCH *search)
  813. {
  814. /*
  815. * If ctx == NULL, the library is looking to know if this loader supports
  816. * the given search type.
  817. */
  818. if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
  819. unsigned long hash = 0;
  820. if (ctx == NULL)
  821. return 1;
  822. if (ctx->type != is_dir) {
  823. OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
  824. OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
  825. return 0;
  826. }
  827. hash = X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search));
  828. BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
  829. "%08lx", hash);
  830. return 1;
  831. }
  832. if (ctx != NULL)
  833. OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
  834. OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE);
  835. return 0;
  836. }
  837. /* Internal function to decode an already opened PEM file */
  838. OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
  839. {
  840. OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  841. if (ctx == NULL) {
  842. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
  843. ERR_R_MALLOC_FAILURE);
  844. return NULL;
  845. }
  846. ctx->_.file.file = bp;
  847. ctx->type = is_pem;
  848. return ctx;
  849. }
  850. static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
  851. const char *pem_name,
  852. const char *pem_header,
  853. unsigned char *data, size_t len,
  854. const UI_METHOD *ui_method,
  855. void *ui_data, int *matchcount)
  856. {
  857. OSSL_STORE_INFO *result = NULL;
  858. BUF_MEM *new_mem = NULL;
  859. char *new_pem_name = NULL;
  860. int t = 0;
  861. again:
  862. {
  863. size_t i = 0;
  864. void *handler_ctx = NULL;
  865. const FILE_HANDLER **matching_handlers =
  866. OPENSSL_zalloc(sizeof(*matching_handlers)
  867. * OSSL_NELEM(file_handlers));
  868. if (matching_handlers == NULL) {
  869. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
  870. ERR_R_MALLOC_FAILURE);
  871. goto err;
  872. }
  873. *matchcount = 0;
  874. for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
  875. const FILE_HANDLER *handler = file_handlers[i];
  876. int try_matchcount = 0;
  877. void *tmp_handler_ctx = NULL;
  878. OSSL_STORE_INFO *tmp_result =
  879. handler->try_decode(pem_name, pem_header, data, len,
  880. &tmp_handler_ctx, &try_matchcount,
  881. ui_method, ui_data);
  882. if (try_matchcount > 0) {
  883. matching_handlers[*matchcount] = handler;
  884. if (handler_ctx)
  885. handler->destroy_ctx(&handler_ctx);
  886. handler_ctx = tmp_handler_ctx;
  887. if ((*matchcount += try_matchcount) > 1) {
  888. /* more than one match => ambiguous, kill any result */
  889. OSSL_STORE_INFO_free(result);
  890. OSSL_STORE_INFO_free(tmp_result);
  891. if (handler->destroy_ctx != NULL)
  892. handler->destroy_ctx(&handler_ctx);
  893. handler_ctx = NULL;
  894. tmp_result = NULL;
  895. result = NULL;
  896. }
  897. if (result == NULL)
  898. result = tmp_result;
  899. }
  900. }
  901. if (*matchcount == 1 && matching_handlers[0]->repeatable) {
  902. ctx->_.file.last_handler = matching_handlers[0];
  903. ctx->_.file.last_handler_ctx = handler_ctx;
  904. }
  905. OPENSSL_free(matching_handlers);
  906. }
  907. err:
  908. OPENSSL_free(new_pem_name);
  909. BUF_MEM_free(new_mem);
  910. if (result != NULL
  911. && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
  912. pem_name = new_pem_name =
  913. ossl_store_info_get0_EMBEDDED_pem_name(result);
  914. new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
  915. data = (unsigned char *)new_mem->data;
  916. len = new_mem->length;
  917. OPENSSL_free(result);
  918. result = NULL;
  919. goto again;
  920. }
  921. if (result != NULL)
  922. ERR_clear_error();
  923. return result;
  924. }
  925. static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
  926. const UI_METHOD *ui_method,
  927. void *ui_data)
  928. {
  929. OSSL_STORE_INFO *result = NULL;
  930. int try_matchcount = 0;
  931. if (ctx->_.file.last_handler != NULL) {
  932. result =
  933. ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
  934. &ctx->_.file.last_handler_ctx,
  935. &try_matchcount,
  936. ui_method, ui_data);
  937. if (result == NULL) {
  938. ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
  939. ctx->_.file.last_handler_ctx = NULL;
  940. ctx->_.file.last_handler = NULL;
  941. }
  942. }
  943. return result;
  944. }
  945. static void pem_free_flag(void *pem_data, int secure, size_t num)
  946. {
  947. if (secure)
  948. OPENSSL_secure_clear_free(pem_data, num);
  949. else
  950. OPENSSL_free(pem_data);
  951. }
  952. static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
  953. unsigned char **data, long *len,
  954. const UI_METHOD *ui_method,
  955. void *ui_data, int secure)
  956. {
  957. int i = secure
  958. ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
  959. PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
  960. : PEM_read_bio(bp, pem_name, pem_header, data, len);
  961. if (i <= 0)
  962. return 0;
  963. /*
  964. * 10 is the number of characters in "Proc-Type:", which
  965. * PEM_get_EVP_CIPHER_INFO() requires to be present.
  966. * If the PEM header has less characters than that, it's
  967. * not worth spending cycles on it.
  968. */
  969. if (strlen(*pem_header) > 10) {
  970. EVP_CIPHER_INFO cipher;
  971. struct pem_pass_data pass_data;
  972. if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
  973. || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
  974. || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
  975. &pass_data)) {
  976. return 0;
  977. }
  978. }
  979. return 1;
  980. }
  981. static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
  982. {
  983. BUF_MEM *mem = NULL;
  984. if (asn1_d2i_read_bio(bp, &mem) < 0)
  985. return 0;
  986. *data = (unsigned char *)mem->data;
  987. *len = (long)mem->length;
  988. OPENSSL_free(mem);
  989. return 1;
  990. }
  991. static int ends_with_dirsep(const char *uri)
  992. {
  993. if (*uri != '\0')
  994. uri += strlen(uri) - 1;
  995. #if defined __VMS
  996. if (*uri == ']' || *uri == '>' || *uri == ':')
  997. return 1;
  998. #elif defined _WIN32
  999. if (*uri == '\\')
  1000. return 1;
  1001. #endif
  1002. return *uri == '/';
  1003. }
  1004. static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
  1005. char **data)
  1006. {
  1007. assert(name != NULL);
  1008. assert(data != NULL);
  1009. {
  1010. const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
  1011. long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
  1012. + strlen(name) + 1 /* \0 */;
  1013. *data = OPENSSL_zalloc(calculated_length);
  1014. if (*data == NULL) {
  1015. OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
  1016. return 0;
  1017. }
  1018. OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
  1019. OPENSSL_strlcat(*data, pathsep, calculated_length);
  1020. OPENSSL_strlcat(*data, name, calculated_length);
  1021. }
  1022. return 1;
  1023. }
  1024. static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
  1025. {
  1026. const char *p = NULL;
  1027. /* If there are no search criteria, all names are accepted */
  1028. if (ctx->_.dir.search_name[0] == '\0')
  1029. return 1;
  1030. /* If the expected type isn't supported, no name is accepted */
  1031. if (ctx->expected_type != 0
  1032. && ctx->expected_type != OSSL_STORE_INFO_CERT
  1033. && ctx->expected_type != OSSL_STORE_INFO_CRL)
  1034. return 0;
  1035. /*
  1036. * First, check the basename
  1037. */
  1038. if (strncasecmp(name, ctx->_.dir.search_name,
  1039. sizeof(ctx->_.dir.search_name) - 1) != 0
  1040. || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
  1041. return 0;
  1042. p = &name[sizeof(ctx->_.dir.search_name)];
  1043. /*
  1044. * Then, if the expected type is a CRL, check that the extension starts
  1045. * with 'r'
  1046. */
  1047. if (*p == 'r') {
  1048. p++;
  1049. if (ctx->expected_type != 0
  1050. && ctx->expected_type != OSSL_STORE_INFO_CRL)
  1051. return 0;
  1052. } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
  1053. return 0;
  1054. }
  1055. /*
  1056. * Last, check that the rest of the extension is a decimal number, at
  1057. * least one digit long.
  1058. */
  1059. if (!ossl_isdigit(*p))
  1060. return 0;
  1061. while (ossl_isdigit(*p))
  1062. p++;
  1063. # ifdef __VMS
  1064. /*
  1065. * One extra step here, check for a possible generation number.
  1066. */
  1067. if (*p == ';')
  1068. for (p++; *p != '\0'; p++)
  1069. if (!ossl_isdigit(*p))
  1070. break;
  1071. # endif
  1072. /*
  1073. * If we've reached the end of the string at this point, we've successfully
  1074. * found a fitting file name.
  1075. */
  1076. return *p == '\0';
  1077. }
  1078. static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
  1079. static int file_error(OSSL_STORE_LOADER_CTX *ctx);
  1080. static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
  1081. const UI_METHOD *ui_method, void *ui_data)
  1082. {
  1083. OSSL_STORE_INFO *result = NULL;
  1084. ctx->errcnt = 0;
  1085. ERR_clear_error();
  1086. if (ctx->type == is_dir) {
  1087. do {
  1088. char *newname = NULL;
  1089. if (ctx->_.dir.last_entry == NULL) {
  1090. if (!ctx->_.dir.end_reached) {
  1091. char errbuf[256];
  1092. assert(ctx->_.dir.last_errno != 0);
  1093. errno = ctx->_.dir.last_errno;
  1094. ctx->errcnt++;
  1095. openssl_strerror_r(errno, errbuf, sizeof(errbuf));
  1096. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
  1097. ERR_add_error_data(1, errbuf);
  1098. }
  1099. return NULL;
  1100. }
  1101. if (ctx->_.dir.last_entry[0] != '.'
  1102. && file_name_check(ctx, ctx->_.dir.last_entry)
  1103. && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
  1104. return NULL;
  1105. /*
  1106. * On the first call (with a NULL context), OPENSSL_DIR_read()
  1107. * cares about the second argument. On the following calls, it
  1108. * only cares that it isn't NULL. Therefore, we can safely give
  1109. * it our URI here.
  1110. */
  1111. ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
  1112. ctx->_.dir.uri);
  1113. ctx->_.dir.last_errno = errno;
  1114. if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
  1115. ctx->_.dir.end_reached = 1;
  1116. if (newname != NULL
  1117. && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
  1118. OPENSSL_free(newname);
  1119. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
  1120. return NULL;
  1121. }
  1122. } while (result == NULL && !file_eof(ctx));
  1123. } else {
  1124. int matchcount = -1;
  1125. again:
  1126. result = file_load_try_repeat(ctx, ui_method, ui_data);
  1127. if (result != NULL)
  1128. return result;
  1129. if (file_eof(ctx))
  1130. return NULL;
  1131. do {
  1132. char *pem_name = NULL; /* PEM record name */
  1133. char *pem_header = NULL; /* PEM record header */
  1134. unsigned char *data = NULL; /* DER encoded data */
  1135. long len = 0; /* DER encoded data length */
  1136. matchcount = -1;
  1137. if (ctx->type == is_pem) {
  1138. if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
  1139. &data, &len, ui_method, ui_data,
  1140. (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
  1141. ctx->errcnt++;
  1142. goto endloop;
  1143. }
  1144. } else {
  1145. if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
  1146. ctx->errcnt++;
  1147. goto endloop;
  1148. }
  1149. }
  1150. result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
  1151. ui_method, ui_data, &matchcount);
  1152. if (result != NULL)
  1153. goto endloop;
  1154. /*
  1155. * If a PEM name matches more than one handler, the handlers are
  1156. * badly coded.
  1157. */
  1158. if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
  1159. ctx->errcnt++;
  1160. goto endloop;
  1161. }
  1162. if (matchcount > 1) {
  1163. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
  1164. OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
  1165. } else if (matchcount == 1) {
  1166. /*
  1167. * If there are other errors on the stack, they already show
  1168. * what the problem is.
  1169. */
  1170. if (ERR_peek_error() == 0) {
  1171. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
  1172. OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
  1173. if (pem_name != NULL)
  1174. ERR_add_error_data(3, "PEM type is '", pem_name, "'");
  1175. }
  1176. }
  1177. if (matchcount > 0)
  1178. ctx->errcnt++;
  1179. endloop:
  1180. pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
  1181. pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
  1182. pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
  1183. } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
  1184. /* We bail out on ambiguity */
  1185. if (matchcount > 1)
  1186. return NULL;
  1187. if (result != NULL
  1188. && ctx->expected_type != 0
  1189. && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
  1190. OSSL_STORE_INFO_free(result);
  1191. goto again;
  1192. }
  1193. }
  1194. return result;
  1195. }
  1196. static int file_error(OSSL_STORE_LOADER_CTX *ctx)
  1197. {
  1198. return ctx->errcnt > 0;
  1199. }
  1200. static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
  1201. {
  1202. if (ctx->type == is_dir)
  1203. return ctx->_.dir.end_reached;
  1204. if (ctx->_.file.last_handler != NULL
  1205. && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
  1206. return 0;
  1207. return BIO_eof(ctx->_.file.file);
  1208. }
  1209. static int file_close(OSSL_STORE_LOADER_CTX *ctx)
  1210. {
  1211. if (ctx->type == is_dir) {
  1212. OPENSSL_DIR_end(&ctx->_.dir.ctx);
  1213. } else {
  1214. BIO_free_all(ctx->_.file.file);
  1215. }
  1216. OSSL_STORE_LOADER_CTX_free(ctx);
  1217. return 1;
  1218. }
  1219. int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
  1220. {
  1221. OSSL_STORE_LOADER_CTX_free(ctx);
  1222. return 1;
  1223. }
  1224. static OSSL_STORE_LOADER file_loader =
  1225. {
  1226. "file",
  1227. NULL,
  1228. file_open,
  1229. file_ctrl,
  1230. file_expect,
  1231. file_find,
  1232. file_load,
  1233. file_eof,
  1234. file_error,
  1235. file_close
  1236. };
  1237. static void store_file_loader_deinit(void)
  1238. {
  1239. ossl_store_unregister_loader_int(file_loader.scheme);
  1240. }
  1241. int ossl_store_file_loader_init(void)
  1242. {
  1243. int ret = ossl_store_register_loader_int(&file_loader);
  1244. OPENSSL_atexit(store_file_loader_deinit);
  1245. return ret;
  1246. }