e_loader_attic.c 55 KB

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