loader_file.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "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 "crypto/asn1.h"
  26. #include "crypto/ctype.h"
  27. #include "internal/o_dir.h"
  28. #include "internal/cryptlib.h"
  29. #include "crypto/store.h"
  30. #include "store_local.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. * initiated by a repeatable try_decode function. 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. #ifndef OPENSSL_NO_ENGINE
  383. ENGINE *curengine = ENGINE_get_first();
  384. while (curengine != NULL) {
  385. ENGINE_PKEY_ASN1_METHS_PTR asn1meths =
  386. ENGINE_get_pkey_asn1_meths(curengine);
  387. if (asn1meths != NULL) {
  388. const int *nids = NULL;
  389. int nids_n = asn1meths(curengine, NULL, &nids, 0);
  390. for (i = 0; i < nids_n; i++) {
  391. EVP_PKEY_ASN1_METHOD *ameth2 = NULL;
  392. EVP_PKEY *tmp_pkey = NULL;
  393. const unsigned char *tmp_blob = blob;
  394. if (!asn1meths(curengine, &ameth2, NULL, nids[i]))
  395. continue;
  396. if (ameth2 == NULL
  397. || ameth2->pkey_flags & ASN1_PKEY_ALIAS)
  398. continue;
  399. tmp_pkey = d2i_PrivateKey(ameth2->pkey_id, NULL,
  400. &tmp_blob, len);
  401. if (tmp_pkey != NULL) {
  402. if (pkey != NULL)
  403. EVP_PKEY_free(tmp_pkey);
  404. else
  405. pkey = tmp_pkey;
  406. (*matchcount)++;
  407. }
  408. }
  409. }
  410. curengine = ENGINE_get_next(curengine);
  411. }
  412. #endif
  413. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  414. EVP_PKEY *tmp_pkey = NULL;
  415. const unsigned char *tmp_blob = blob;
  416. ameth = EVP_PKEY_asn1_get0(i);
  417. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  418. continue;
  419. tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len);
  420. if (tmp_pkey != NULL) {
  421. if (pkey != NULL)
  422. EVP_PKEY_free(tmp_pkey);
  423. else
  424. pkey = tmp_pkey;
  425. (*matchcount)++;
  426. }
  427. }
  428. if (*matchcount > 1) {
  429. EVP_PKEY_free(pkey);
  430. pkey = NULL;
  431. }
  432. }
  433. if (pkey == NULL)
  434. /* No match */
  435. return NULL;
  436. store_info = OSSL_STORE_INFO_new_PKEY(pkey);
  437. if (store_info == NULL)
  438. EVP_PKEY_free(pkey);
  439. return store_info;
  440. }
  441. static FILE_HANDLER PrivateKey_handler = {
  442. "PrivateKey",
  443. try_decode_PrivateKey
  444. };
  445. /*
  446. * Public key decoder. Only supports SubjectPublicKeyInfo formatted keys.
  447. */
  448. static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
  449. const char *pem_header,
  450. const unsigned char *blob,
  451. size_t len, void **pctx,
  452. int *matchcount,
  453. const UI_METHOD *ui_method,
  454. void *ui_data)
  455. {
  456. OSSL_STORE_INFO *store_info = NULL;
  457. EVP_PKEY *pkey = NULL;
  458. if (pem_name != NULL) {
  459. if (strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
  460. /* No match */
  461. return NULL;
  462. *matchcount = 1;
  463. }
  464. if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL) {
  465. *matchcount = 1;
  466. store_info = OSSL_STORE_INFO_new_PKEY(pkey);
  467. }
  468. return store_info;
  469. }
  470. static FILE_HANDLER PUBKEY_handler = {
  471. "PUBKEY",
  472. try_decode_PUBKEY
  473. };
  474. /*
  475. * Key parameter decoder.
  476. */
  477. static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
  478. const char *pem_header,
  479. const unsigned char *blob,
  480. size_t len, void **pctx,
  481. int *matchcount,
  482. const UI_METHOD *ui_method,
  483. void *ui_data)
  484. {
  485. OSSL_STORE_INFO *store_info = NULL;
  486. int slen = 0;
  487. EVP_PKEY *pkey = NULL;
  488. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  489. int ok = 0;
  490. if (pem_name != NULL) {
  491. if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0)
  492. return NULL;
  493. *matchcount = 1;
  494. }
  495. if (slen > 0) {
  496. if ((pkey = EVP_PKEY_new()) == NULL) {
  497. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
  498. return NULL;
  499. }
  500. if (EVP_PKEY_set_type_str(pkey, pem_name, slen)
  501. && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
  502. && ameth->param_decode != NULL
  503. && ameth->param_decode(pkey, &blob, len))
  504. ok = 1;
  505. } else {
  506. int i;
  507. EVP_PKEY *tmp_pkey = NULL;
  508. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  509. const unsigned char *tmp_blob = blob;
  510. if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) {
  511. OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
  512. break;
  513. }
  514. ameth = EVP_PKEY_asn1_get0(i);
  515. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  516. continue;
  517. if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id)
  518. && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL
  519. && ameth->param_decode != NULL
  520. && ameth->param_decode(tmp_pkey, &tmp_blob, len)) {
  521. if (pkey != NULL)
  522. EVP_PKEY_free(tmp_pkey);
  523. else
  524. pkey = tmp_pkey;
  525. tmp_pkey = NULL;
  526. (*matchcount)++;
  527. }
  528. }
  529. EVP_PKEY_free(tmp_pkey);
  530. if (*matchcount == 1) {
  531. ok = 1;
  532. }
  533. }
  534. if (ok)
  535. store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
  536. if (store_info == NULL)
  537. EVP_PKEY_free(pkey);
  538. return store_info;
  539. }
  540. static FILE_HANDLER params_handler = {
  541. "params",
  542. try_decode_params
  543. };
  544. /*
  545. * X.509 certificate decoder.
  546. */
  547. static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
  548. const char *pem_header,
  549. const unsigned char *blob,
  550. size_t len, void **pctx,
  551. int *matchcount,
  552. const UI_METHOD *ui_method,
  553. void *ui_data)
  554. {
  555. OSSL_STORE_INFO *store_info = NULL;
  556. X509 *cert = NULL;
  557. /*
  558. * In most cases, we can try to interpret the serialized data as a trusted
  559. * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
  560. * (just X509), but if the PEM name specifically declares it as a trusted
  561. * cert, then no fallback should be engaged. |ignore_trusted| tells if
  562. * the fallback can be used (1) or not (0).
  563. */
  564. int ignore_trusted = 1;
  565. if (pem_name != NULL) {
  566. if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
  567. ignore_trusted = 0;
  568. else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
  569. && strcmp(pem_name, PEM_STRING_X509) != 0)
  570. /* No match */
  571. return NULL;
  572. *matchcount = 1;
  573. }
  574. if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
  575. || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL)) {
  576. *matchcount = 1;
  577. store_info = OSSL_STORE_INFO_new_CERT(cert);
  578. }
  579. if (store_info == NULL)
  580. X509_free(cert);
  581. return store_info;
  582. }
  583. static FILE_HANDLER X509Certificate_handler = {
  584. "X509Certificate",
  585. try_decode_X509Certificate
  586. };
  587. /*
  588. * X.509 CRL decoder.
  589. */
  590. static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
  591. const char *pem_header,
  592. const unsigned char *blob,
  593. size_t len, void **pctx,
  594. int *matchcount,
  595. const UI_METHOD *ui_method,
  596. void *ui_data)
  597. {
  598. OSSL_STORE_INFO *store_info = NULL;
  599. X509_CRL *crl = NULL;
  600. if (pem_name != NULL) {
  601. if (strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
  602. /* No match */
  603. return NULL;
  604. *matchcount = 1;
  605. }
  606. if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL) {
  607. *matchcount = 1;
  608. store_info = OSSL_STORE_INFO_new_CRL(crl);
  609. }
  610. if (store_info == NULL)
  611. X509_CRL_free(crl);
  612. return store_info;
  613. }
  614. static FILE_HANDLER X509CRL_handler = {
  615. "X509CRL",
  616. try_decode_X509CRL
  617. };
  618. /*
  619. * To finish it all off, we collect all the handlers.
  620. */
  621. static const FILE_HANDLER *file_handlers[] = {
  622. &PKCS12_handler,
  623. &PKCS8Encrypted_handler,
  624. &X509Certificate_handler,
  625. &X509CRL_handler,
  626. &params_handler,
  627. &PUBKEY_handler,
  628. &PrivateKey_handler,
  629. };
  630. /*-
  631. * The loader itself
  632. * -----------------
  633. */
  634. struct ossl_store_loader_ctx_st {
  635. enum {
  636. is_raw = 0,
  637. is_pem,
  638. is_dir
  639. } type;
  640. int errcnt;
  641. #define FILE_FLAG_SECMEM (1<<0)
  642. unsigned int flags;
  643. union {
  644. struct { /* Used with is_raw and is_pem */
  645. BIO *file;
  646. /*
  647. * The following are used when the handler is marked as
  648. * repeatable
  649. */
  650. const FILE_HANDLER *last_handler;
  651. void *last_handler_ctx;
  652. } file;
  653. struct { /* Used with is_dir */
  654. OPENSSL_DIR_CTX *ctx;
  655. int end_reached;
  656. char *uri;
  657. /*
  658. * When a search expression is given, these are filled in.
  659. * |search_name| contains the file basename to look for.
  660. * The string is exactly 8 characters long.
  661. */
  662. char search_name[9];
  663. /*
  664. * The directory reading utility we have combines opening with
  665. * reading the first name. To make sure we can detect the end
  666. * at the right time, we read early and cache the name.
  667. */
  668. const char *last_entry;
  669. int last_errno;
  670. } dir;
  671. } _;
  672. /* Expected object type. May be unspecified */
  673. int expected_type;
  674. };
  675. static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
  676. {
  677. if (ctx->type == is_dir) {
  678. OPENSSL_free(ctx->_.dir.uri);
  679. } else {
  680. if (ctx->_.file.last_handler != NULL) {
  681. ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
  682. ctx->_.file.last_handler_ctx = NULL;
  683. ctx->_.file.last_handler = NULL;
  684. }
  685. }
  686. OPENSSL_free(ctx);
  687. }
  688. static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
  689. const char *uri,
  690. const UI_METHOD *ui_method,
  691. void *ui_data)
  692. {
  693. OSSL_STORE_LOADER_CTX *ctx = NULL;
  694. struct stat st;
  695. struct {
  696. const char *path;
  697. unsigned int check_absolute:1;
  698. } path_data[2];
  699. size_t path_data_n = 0, i;
  700. const char *path;
  701. /*
  702. * First step, just take the URI as is.
  703. */
  704. path_data[path_data_n].check_absolute = 0;
  705. path_data[path_data_n++].path = uri;
  706. /*
  707. * Second step, if the URI appears to start with the 'file' scheme,
  708. * extract the path and make that the second path to check.
  709. * There's a special case if the URI also contains an authority, then
  710. * the full URI shouldn't be used as a path anywhere.
  711. */
  712. if (strncasecmp(uri, "file:", 5) == 0) {
  713. const char *p = &uri[5];
  714. if (strncmp(&uri[5], "//", 2) == 0) {
  715. path_data_n--; /* Invalidate using the full URI */
  716. if (strncasecmp(&uri[7], "localhost/", 10) == 0) {
  717. p = &uri[16];
  718. } else if (uri[7] == '/') {
  719. p = &uri[7];
  720. } else {
  721. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
  722. OSSL_STORE_R_URI_AUTHORITY_UNSUPPORTED);
  723. return NULL;
  724. }
  725. }
  726. path_data[path_data_n].check_absolute = 1;
  727. #ifdef _WIN32
  728. /* Windows file: URIs with a drive letter start with a / */
  729. if (p[0] == '/' && p[2] == ':' && p[3] == '/') {
  730. char c = ossl_tolower(p[1]);
  731. if (c >= 'a' && c <= 'z') {
  732. p++;
  733. /* We know it's absolute, so no need to check */
  734. path_data[path_data_n].check_absolute = 0;
  735. }
  736. }
  737. #endif
  738. path_data[path_data_n++].path = p;
  739. }
  740. for (i = 0, path = NULL; path == NULL && i < path_data_n; i++) {
  741. /*
  742. * If the scheme "file" was an explicit part of the URI, the path must
  743. * be absolute. So says RFC 8089
  744. */
  745. if (path_data[i].check_absolute && path_data[i].path[0] != '/') {
  746. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
  747. OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
  748. ERR_add_error_data(1, path_data[i].path);
  749. return NULL;
  750. }
  751. if (stat(path_data[i].path, &st) < 0) {
  752. SYSerr(SYS_F_STAT, errno);
  753. ERR_add_error_data(1, path_data[i].path);
  754. } else {
  755. path = path_data[i].path;
  756. }
  757. }
  758. if (path == NULL) {
  759. return NULL;
  760. }
  761. /* Successfully found a working path, clear possible collected errors */
  762. ERR_clear_error();
  763. ctx = OPENSSL_zalloc(sizeof(*ctx));
  764. if (ctx == NULL) {
  765. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
  766. return NULL;
  767. }
  768. if (S_ISDIR(st.st_mode)) {
  769. /*
  770. * Try to copy everything, even if we know that some of them must be
  771. * NULL for the moment. This prevents errors in the future, when more
  772. * components may be used.
  773. */
  774. ctx->_.dir.uri = OPENSSL_strdup(uri);
  775. ctx->type = is_dir;
  776. if (ctx->_.dir.uri == NULL)
  777. goto err;
  778. ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx, path);
  779. ctx->_.dir.last_errno = errno;
  780. if (ctx->_.dir.last_entry == NULL) {
  781. if (ctx->_.dir.last_errno != 0) {
  782. char errbuf[256];
  783. OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_SYS_LIB);
  784. errno = ctx->_.dir.last_errno;
  785. if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
  786. ERR_add_error_data(1, errbuf);
  787. goto err;
  788. }
  789. ctx->_.dir.end_reached = 1;
  790. }
  791. } else {
  792. BIO *buff = NULL;
  793. char peekbuf[4096] = { 0, };
  794. if ((buff = BIO_new(BIO_f_buffer())) == NULL
  795. || (ctx->_.file.file = BIO_new_file(path, "rb")) == NULL) {
  796. BIO_free_all(buff);
  797. goto err;
  798. }
  799. ctx->_.file.file = BIO_push(buff, ctx->_.file.file);
  800. if (BIO_buffer_peek(ctx->_.file.file, peekbuf, sizeof(peekbuf) - 1) > 0) {
  801. peekbuf[sizeof(peekbuf) - 1] = '\0';
  802. if (strstr(peekbuf, "-----BEGIN ") != NULL)
  803. ctx->type = is_pem;
  804. }
  805. }
  806. return ctx;
  807. err:
  808. OSSL_STORE_LOADER_CTX_free(ctx);
  809. return NULL;
  810. }
  811. static int file_ctrl(OSSL_STORE_LOADER_CTX *ctx, int cmd, va_list args)
  812. {
  813. int ret = 1;
  814. switch (cmd) {
  815. case OSSL_STORE_C_USE_SECMEM:
  816. {
  817. int on = *(va_arg(args, int *));
  818. switch (on) {
  819. case 0:
  820. ctx->flags &= ~FILE_FLAG_SECMEM;
  821. break;
  822. case 1:
  823. ctx->flags |= FILE_FLAG_SECMEM;
  824. break;
  825. default:
  826. OSSL_STOREerr(OSSL_STORE_F_FILE_CTRL,
  827. ERR_R_PASSED_INVALID_ARGUMENT);
  828. ret = 0;
  829. break;
  830. }
  831. }
  832. break;
  833. default:
  834. break;
  835. }
  836. return ret;
  837. }
  838. static int file_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
  839. {
  840. ctx->expected_type = expected;
  841. return 1;
  842. }
  843. static int file_find(OSSL_STORE_LOADER_CTX *ctx, OSSL_STORE_SEARCH *search)
  844. {
  845. /*
  846. * If ctx == NULL, the library is looking to know if this loader supports
  847. * the given search type.
  848. */
  849. if (OSSL_STORE_SEARCH_get_type(search) == OSSL_STORE_SEARCH_BY_NAME) {
  850. unsigned long hash = 0;
  851. if (ctx == NULL)
  852. return 1;
  853. if (ctx->type != is_dir) {
  854. OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
  855. OSSL_STORE_R_SEARCH_ONLY_SUPPORTED_FOR_DIRECTORIES);
  856. return 0;
  857. }
  858. hash = X509_NAME_hash(OSSL_STORE_SEARCH_get0_name(search));
  859. BIO_snprintf(ctx->_.dir.search_name, sizeof(ctx->_.dir.search_name),
  860. "%08lx", hash);
  861. return 1;
  862. }
  863. if (ctx != NULL)
  864. OSSL_STOREerr(OSSL_STORE_F_FILE_FIND,
  865. OSSL_STORE_R_UNSUPPORTED_SEARCH_TYPE);
  866. return 0;
  867. }
  868. /* Internal function to decode an already opened PEM file */
  869. OSSL_STORE_LOADER_CTX *ossl_store_file_attach_pem_bio_int(BIO *bp)
  870. {
  871. OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  872. if (ctx == NULL) {
  873. OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_FILE_ATTACH_PEM_BIO_INT,
  874. ERR_R_MALLOC_FAILURE);
  875. return NULL;
  876. }
  877. ctx->_.file.file = bp;
  878. ctx->type = is_pem;
  879. return ctx;
  880. }
  881. static OSSL_STORE_INFO *file_load_try_decode(OSSL_STORE_LOADER_CTX *ctx,
  882. const char *pem_name,
  883. const char *pem_header,
  884. unsigned char *data, size_t len,
  885. const UI_METHOD *ui_method,
  886. void *ui_data, int *matchcount)
  887. {
  888. OSSL_STORE_INFO *result = NULL;
  889. BUF_MEM *new_mem = NULL;
  890. char *new_pem_name = NULL;
  891. int t = 0;
  892. again:
  893. {
  894. size_t i = 0;
  895. void *handler_ctx = NULL;
  896. const FILE_HANDLER **matching_handlers =
  897. OPENSSL_zalloc(sizeof(*matching_handlers)
  898. * OSSL_NELEM(file_handlers));
  899. if (matching_handlers == NULL) {
  900. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD_TRY_DECODE,
  901. ERR_R_MALLOC_FAILURE);
  902. goto err;
  903. }
  904. *matchcount = 0;
  905. for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
  906. const FILE_HANDLER *handler = file_handlers[i];
  907. int try_matchcount = 0;
  908. void *tmp_handler_ctx = NULL;
  909. OSSL_STORE_INFO *tmp_result =
  910. handler->try_decode(pem_name, pem_header, data, len,
  911. &tmp_handler_ctx, &try_matchcount,
  912. ui_method, ui_data);
  913. if (try_matchcount > 0) {
  914. matching_handlers[*matchcount] = handler;
  915. if (handler_ctx)
  916. handler->destroy_ctx(&handler_ctx);
  917. handler_ctx = tmp_handler_ctx;
  918. if ((*matchcount += try_matchcount) > 1) {
  919. /* more than one match => ambiguous, kill any result */
  920. OSSL_STORE_INFO_free(result);
  921. OSSL_STORE_INFO_free(tmp_result);
  922. if (handler->destroy_ctx != NULL)
  923. handler->destroy_ctx(&handler_ctx);
  924. handler_ctx = NULL;
  925. tmp_result = NULL;
  926. result = NULL;
  927. }
  928. if (result == NULL)
  929. result = tmp_result;
  930. }
  931. }
  932. if (*matchcount == 1 && matching_handlers[0]->repeatable) {
  933. ctx->_.file.last_handler = matching_handlers[0];
  934. ctx->_.file.last_handler_ctx = handler_ctx;
  935. }
  936. OPENSSL_free(matching_handlers);
  937. }
  938. err:
  939. OPENSSL_free(new_pem_name);
  940. BUF_MEM_free(new_mem);
  941. if (result != NULL
  942. && (t = OSSL_STORE_INFO_get_type(result)) == OSSL_STORE_INFO_EMBEDDED) {
  943. pem_name = new_pem_name =
  944. ossl_store_info_get0_EMBEDDED_pem_name(result);
  945. new_mem = ossl_store_info_get0_EMBEDDED_buffer(result);
  946. data = (unsigned char *)new_mem->data;
  947. len = new_mem->length;
  948. OPENSSL_free(result);
  949. result = NULL;
  950. goto again;
  951. }
  952. if (result != NULL)
  953. ERR_clear_error();
  954. return result;
  955. }
  956. static OSSL_STORE_INFO *file_load_try_repeat(OSSL_STORE_LOADER_CTX *ctx,
  957. const UI_METHOD *ui_method,
  958. void *ui_data)
  959. {
  960. OSSL_STORE_INFO *result = NULL;
  961. int try_matchcount = 0;
  962. if (ctx->_.file.last_handler != NULL) {
  963. result =
  964. ctx->_.file.last_handler->try_decode(NULL, NULL, NULL, 0,
  965. &ctx->_.file.last_handler_ctx,
  966. &try_matchcount,
  967. ui_method, ui_data);
  968. if (result == NULL) {
  969. ctx->_.file.last_handler->destroy_ctx(&ctx->_.file.last_handler_ctx);
  970. ctx->_.file.last_handler_ctx = NULL;
  971. ctx->_.file.last_handler = NULL;
  972. }
  973. }
  974. return result;
  975. }
  976. static void pem_free_flag(void *pem_data, int secure, size_t num)
  977. {
  978. if (secure)
  979. OPENSSL_secure_clear_free(pem_data, num);
  980. else
  981. OPENSSL_free(pem_data);
  982. }
  983. static int file_read_pem(BIO *bp, char **pem_name, char **pem_header,
  984. unsigned char **data, long *len,
  985. const UI_METHOD *ui_method,
  986. void *ui_data, int secure)
  987. {
  988. int i = secure
  989. ? PEM_read_bio_ex(bp, pem_name, pem_header, data, len,
  990. PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE)
  991. : PEM_read_bio(bp, pem_name, pem_header, data, len);
  992. if (i <= 0)
  993. return 0;
  994. /*
  995. * 10 is the number of characters in "Proc-Type:", which
  996. * PEM_get_EVP_CIPHER_INFO() requires to be present.
  997. * If the PEM header has less characters than that, it's
  998. * not worth spending cycles on it.
  999. */
  1000. if (strlen(*pem_header) > 10) {
  1001. EVP_CIPHER_INFO cipher;
  1002. struct pem_pass_data pass_data;
  1003. if (!PEM_get_EVP_CIPHER_INFO(*pem_header, &cipher)
  1004. || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method, ui_data)
  1005. || !PEM_do_header(&cipher, *data, len, file_get_pem_pass,
  1006. &pass_data)) {
  1007. return 0;
  1008. }
  1009. }
  1010. return 1;
  1011. }
  1012. static int file_read_asn1(BIO *bp, unsigned char **data, long *len)
  1013. {
  1014. BUF_MEM *mem = NULL;
  1015. if (asn1_d2i_read_bio(bp, &mem) < 0)
  1016. return 0;
  1017. *data = (unsigned char *)mem->data;
  1018. *len = (long)mem->length;
  1019. OPENSSL_free(mem);
  1020. return 1;
  1021. }
  1022. static int ends_with_dirsep(const char *uri)
  1023. {
  1024. if (*uri != '\0')
  1025. uri += strlen(uri) - 1;
  1026. #if defined __VMS
  1027. if (*uri == ']' || *uri == '>' || *uri == ':')
  1028. return 1;
  1029. #elif defined _WIN32
  1030. if (*uri == '\\')
  1031. return 1;
  1032. #endif
  1033. return *uri == '/';
  1034. }
  1035. static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name,
  1036. char **data)
  1037. {
  1038. assert(name != NULL);
  1039. assert(data != NULL);
  1040. {
  1041. const char *pathsep = ends_with_dirsep(ctx->_.dir.uri) ? "" : "/";
  1042. long calculated_length = strlen(ctx->_.dir.uri) + strlen(pathsep)
  1043. + strlen(name) + 1 /* \0 */;
  1044. *data = OPENSSL_zalloc(calculated_length);
  1045. if (*data == NULL) {
  1046. OSSL_STOREerr(OSSL_STORE_F_FILE_NAME_TO_URI, ERR_R_MALLOC_FAILURE);
  1047. return 0;
  1048. }
  1049. OPENSSL_strlcat(*data, ctx->_.dir.uri, calculated_length);
  1050. OPENSSL_strlcat(*data, pathsep, calculated_length);
  1051. OPENSSL_strlcat(*data, name, calculated_length);
  1052. }
  1053. return 1;
  1054. }
  1055. static int file_name_check(OSSL_STORE_LOADER_CTX *ctx, const char *name)
  1056. {
  1057. const char *p = NULL;
  1058. /* If there are no search criteria, all names are accepted */
  1059. if (ctx->_.dir.search_name[0] == '\0')
  1060. return 1;
  1061. /* If the expected type isn't supported, no name is accepted */
  1062. if (ctx->expected_type != 0
  1063. && ctx->expected_type != OSSL_STORE_INFO_CERT
  1064. && ctx->expected_type != OSSL_STORE_INFO_CRL)
  1065. return 0;
  1066. /*
  1067. * First, check the basename
  1068. */
  1069. if (strncasecmp(name, ctx->_.dir.search_name,
  1070. sizeof(ctx->_.dir.search_name) - 1) != 0
  1071. || name[sizeof(ctx->_.dir.search_name) - 1] != '.')
  1072. return 0;
  1073. p = &name[sizeof(ctx->_.dir.search_name)];
  1074. /*
  1075. * Then, if the expected type is a CRL, check that the extension starts
  1076. * with 'r'
  1077. */
  1078. if (*p == 'r') {
  1079. p++;
  1080. if (ctx->expected_type != 0
  1081. && ctx->expected_type != OSSL_STORE_INFO_CRL)
  1082. return 0;
  1083. } else if (ctx->expected_type == OSSL_STORE_INFO_CRL) {
  1084. return 0;
  1085. }
  1086. /*
  1087. * Last, check that the rest of the extension is a decimal number, at
  1088. * least one digit long.
  1089. */
  1090. if (!ossl_isdigit(*p))
  1091. return 0;
  1092. while (ossl_isdigit(*p))
  1093. p++;
  1094. # ifdef __VMS
  1095. /*
  1096. * One extra step here, check for a possible generation number.
  1097. */
  1098. if (*p == ';')
  1099. for (p++; *p != '\0'; p++)
  1100. if (!ossl_isdigit(*p))
  1101. break;
  1102. # endif
  1103. /*
  1104. * If we've reached the end of the string at this point, we've successfully
  1105. * found a fitting file name.
  1106. */
  1107. return *p == '\0';
  1108. }
  1109. static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
  1110. static int file_error(OSSL_STORE_LOADER_CTX *ctx);
  1111. static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
  1112. const UI_METHOD *ui_method, void *ui_data)
  1113. {
  1114. OSSL_STORE_INFO *result = NULL;
  1115. ctx->errcnt = 0;
  1116. ERR_clear_error();
  1117. if (ctx->type == is_dir) {
  1118. do {
  1119. char *newname = NULL;
  1120. if (ctx->_.dir.last_entry == NULL) {
  1121. if (!ctx->_.dir.end_reached) {
  1122. char errbuf[256];
  1123. assert(ctx->_.dir.last_errno != 0);
  1124. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_SYS_LIB);
  1125. errno = ctx->_.dir.last_errno;
  1126. ctx->errcnt++;
  1127. if (openssl_strerror_r(errno, errbuf, sizeof(errbuf)))
  1128. ERR_add_error_data(1, errbuf);
  1129. }
  1130. return NULL;
  1131. }
  1132. if (ctx->_.dir.last_entry[0] != '.'
  1133. && file_name_check(ctx, ctx->_.dir.last_entry)
  1134. && !file_name_to_uri(ctx, ctx->_.dir.last_entry, &newname))
  1135. return NULL;
  1136. /*
  1137. * On the first call (with a NULL context), OPENSSL_DIR_read()
  1138. * cares about the second argument. On the following calls, it
  1139. * only cares that it isn't NULL. Therefore, we can safely give
  1140. * it our URI here.
  1141. */
  1142. ctx->_.dir.last_entry = OPENSSL_DIR_read(&ctx->_.dir.ctx,
  1143. ctx->_.dir.uri);
  1144. ctx->_.dir.last_errno = errno;
  1145. if (ctx->_.dir.last_entry == NULL && ctx->_.dir.last_errno == 0)
  1146. ctx->_.dir.end_reached = 1;
  1147. if (newname != NULL
  1148. && (result = OSSL_STORE_INFO_new_NAME(newname)) == NULL) {
  1149. OPENSSL_free(newname);
  1150. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_OSSL_STORE_LIB);
  1151. return NULL;
  1152. }
  1153. } while (result == NULL && !file_eof(ctx));
  1154. } else {
  1155. int matchcount = -1;
  1156. again:
  1157. result = file_load_try_repeat(ctx, ui_method, ui_data);
  1158. if (result != NULL)
  1159. return result;
  1160. if (file_eof(ctx))
  1161. return NULL;
  1162. do {
  1163. char *pem_name = NULL; /* PEM record name */
  1164. char *pem_header = NULL; /* PEM record header */
  1165. unsigned char *data = NULL; /* DER encoded data */
  1166. long len = 0; /* DER encoded data length */
  1167. matchcount = -1;
  1168. if (ctx->type == is_pem) {
  1169. if (!file_read_pem(ctx->_.file.file, &pem_name, &pem_header,
  1170. &data, &len, ui_method, ui_data,
  1171. (ctx->flags & FILE_FLAG_SECMEM) != 0)) {
  1172. ctx->errcnt++;
  1173. goto endloop;
  1174. }
  1175. } else {
  1176. if (!file_read_asn1(ctx->_.file.file, &data, &len)) {
  1177. ctx->errcnt++;
  1178. goto endloop;
  1179. }
  1180. }
  1181. result = file_load_try_decode(ctx, pem_name, pem_header, data, len,
  1182. ui_method, ui_data, &matchcount);
  1183. if (result != NULL)
  1184. goto endloop;
  1185. /*
  1186. * If a PEM name matches more than one handler, the handlers are
  1187. * badly coded.
  1188. */
  1189. if (!ossl_assert(pem_name == NULL || matchcount <= 1)) {
  1190. ctx->errcnt++;
  1191. goto endloop;
  1192. }
  1193. if (matchcount > 1) {
  1194. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
  1195. OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
  1196. } else if (matchcount == 1) {
  1197. /*
  1198. * If there are other errors on the stack, they already show
  1199. * what the problem is.
  1200. */
  1201. if (ERR_peek_error() == 0) {
  1202. OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
  1203. OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
  1204. if (pem_name != NULL)
  1205. ERR_add_error_data(3, "PEM type is '", pem_name, "'");
  1206. }
  1207. }
  1208. if (matchcount > 0)
  1209. ctx->errcnt++;
  1210. endloop:
  1211. pem_free_flag(pem_name, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
  1212. pem_free_flag(pem_header, (ctx->flags & FILE_FLAG_SECMEM) != 0, 0);
  1213. pem_free_flag(data, (ctx->flags & FILE_FLAG_SECMEM) != 0, len);
  1214. } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
  1215. /* We bail out on ambiguity */
  1216. if (matchcount > 1)
  1217. return NULL;
  1218. if (result != NULL
  1219. && ctx->expected_type != 0
  1220. && ctx->expected_type != OSSL_STORE_INFO_get_type(result)) {
  1221. OSSL_STORE_INFO_free(result);
  1222. goto again;
  1223. }
  1224. }
  1225. return result;
  1226. }
  1227. static int file_error(OSSL_STORE_LOADER_CTX *ctx)
  1228. {
  1229. return ctx->errcnt > 0;
  1230. }
  1231. static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
  1232. {
  1233. if (ctx->type == is_dir)
  1234. return ctx->_.dir.end_reached;
  1235. if (ctx->_.file.last_handler != NULL
  1236. && !ctx->_.file.last_handler->eof(ctx->_.file.last_handler_ctx))
  1237. return 0;
  1238. return BIO_eof(ctx->_.file.file);
  1239. }
  1240. static int file_close(OSSL_STORE_LOADER_CTX *ctx)
  1241. {
  1242. if (ctx->type == is_dir) {
  1243. OPENSSL_DIR_end(&ctx->_.dir.ctx);
  1244. } else {
  1245. BIO_free_all(ctx->_.file.file);
  1246. }
  1247. OSSL_STORE_LOADER_CTX_free(ctx);
  1248. return 1;
  1249. }
  1250. int ossl_store_file_detach_pem_bio_int(OSSL_STORE_LOADER_CTX *ctx)
  1251. {
  1252. OSSL_STORE_LOADER_CTX_free(ctx);
  1253. return 1;
  1254. }
  1255. static OSSL_STORE_LOADER file_loader =
  1256. {
  1257. "file",
  1258. NULL,
  1259. file_open,
  1260. file_ctrl,
  1261. file_expect,
  1262. file_find,
  1263. file_load,
  1264. file_eof,
  1265. file_error,
  1266. file_close
  1267. };
  1268. static void store_file_loader_deinit(void)
  1269. {
  1270. ossl_store_unregister_loader_int(file_loader.scheme);
  1271. }
  1272. int ossl_store_file_loader_init(void)
  1273. {
  1274. int ret = ossl_store_register_loader_int(&file_loader);
  1275. OPENSSL_atexit(store_file_loader_deinit);
  1276. return ret;
  1277. }