loader_file.c 46 KB

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