loader_file.c 44 KB

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