2
0

e_loader_attic.c 55 KB

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