2
0

e_loader_attic.c 55 KB

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