conf_def.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Part of the code in here was originally in conf.c, which is now removed */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/o_dir.h"
  14. #include <openssl/lhash.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/conf_api.h>
  17. #include "conf_def.h"
  18. #include <openssl/buffer.h>
  19. #include <openssl/err.h>
  20. #ifndef OPENSSL_NO_POSIX_IO
  21. # include <sys/stat.h>
  22. # ifdef _WIN32
  23. # define stat _stat
  24. # define strcasecmp _stricmp
  25. # endif
  26. #endif
  27. #ifndef S_ISDIR
  28. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  29. #endif
  30. /*
  31. * The maximum length we can grow a value to after variable expansion. 64k
  32. * should be more than enough for all reasonable uses.
  33. */
  34. #define MAX_CONF_VALUE_LENGTH 65536
  35. static int is_keytype(const CONF *conf, char c, unsigned short type);
  36. static char *eat_ws(CONF *conf, char *p);
  37. static void trim_ws(CONF *conf, char *start);
  38. static char *eat_alpha_numeric(CONF *conf, char *p);
  39. static void clear_comments(CONF *conf, char *p);
  40. static int str_copy(CONF *conf, char *section, char **to, char *from);
  41. static char *scan_quote(CONF *conf, char *p);
  42. static char *scan_dquote(CONF *conf, char *p);
  43. #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
  44. #ifndef OPENSSL_NO_POSIX_IO
  45. static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
  46. char **dirpath);
  47. static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
  48. #endif
  49. static CONF *def_create(CONF_METHOD *meth);
  50. static int def_init_default(CONF *conf);
  51. static int def_init_WIN32(CONF *conf);
  52. static int def_destroy(CONF *conf);
  53. static int def_destroy_data(CONF *conf);
  54. static int def_load(CONF *conf, const char *name, long *eline);
  55. static int def_load_bio(CONF *conf, BIO *bp, long *eline);
  56. static int def_dump(const CONF *conf, BIO *bp);
  57. static int def_is_number(const CONF *conf, char c);
  58. static int def_to_int(const CONF *conf, char c);
  59. static CONF_METHOD default_method = {
  60. "OpenSSL default",
  61. def_create,
  62. def_init_default,
  63. def_destroy,
  64. def_destroy_data,
  65. def_load_bio,
  66. def_dump,
  67. def_is_number,
  68. def_to_int,
  69. def_load
  70. };
  71. static CONF_METHOD WIN32_method = {
  72. "WIN32",
  73. def_create,
  74. def_init_WIN32,
  75. def_destroy,
  76. def_destroy_data,
  77. def_load_bio,
  78. def_dump,
  79. def_is_number,
  80. def_to_int,
  81. def_load
  82. };
  83. CONF_METHOD *NCONF_default(void)
  84. {
  85. return &default_method;
  86. }
  87. CONF_METHOD *NCONF_WIN32(void)
  88. {
  89. return &WIN32_method;
  90. }
  91. static CONF *def_create(CONF_METHOD *meth)
  92. {
  93. CONF *ret;
  94. ret = OPENSSL_malloc(sizeof(*ret));
  95. if (ret != NULL)
  96. if (meth->init(ret) == 0) {
  97. OPENSSL_free(ret);
  98. ret = NULL;
  99. }
  100. return ret;
  101. }
  102. static int def_init_default(CONF *conf)
  103. {
  104. if (conf == NULL)
  105. return 0;
  106. conf->meth = &default_method;
  107. conf->meth_data = (void *)CONF_type_default;
  108. conf->data = NULL;
  109. return 1;
  110. }
  111. static int def_init_WIN32(CONF *conf)
  112. {
  113. if (conf == NULL)
  114. return 0;
  115. conf->meth = &WIN32_method;
  116. conf->meth_data = (void *)CONF_type_win32;
  117. conf->data = NULL;
  118. return 1;
  119. }
  120. static int def_destroy(CONF *conf)
  121. {
  122. if (def_destroy_data(conf)) {
  123. OPENSSL_free(conf);
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. static int def_destroy_data(CONF *conf)
  129. {
  130. if (conf == NULL)
  131. return 0;
  132. _CONF_free_data(conf);
  133. return 1;
  134. }
  135. static int def_load(CONF *conf, const char *name, long *line)
  136. {
  137. int ret;
  138. BIO *in = NULL;
  139. #ifdef OPENSSL_SYS_VMS
  140. in = BIO_new_file(name, "r");
  141. #else
  142. in = BIO_new_file(name, "rb");
  143. #endif
  144. if (in == NULL) {
  145. if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
  146. CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
  147. else
  148. CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
  149. return 0;
  150. }
  151. ret = def_load_bio(conf, in, line);
  152. BIO_free(in);
  153. return ret;
  154. }
  155. static int def_load_bio(CONF *conf, BIO *in, long *line)
  156. {
  157. /* The macro BUFSIZE conflicts with a system macro in VxWorks */
  158. #define CONFBUFSIZE 512
  159. int bufnum = 0, i, ii;
  160. BUF_MEM *buff = NULL;
  161. char *s, *p, *end;
  162. int again;
  163. int first_call = 1;
  164. long eline = 0;
  165. char btmp[DECIMAL_SIZE(eline) + 1];
  166. CONF_VALUE *v = NULL, *tv;
  167. CONF_VALUE *sv = NULL;
  168. char *section = NULL, *buf;
  169. char *start, *psection, *pname;
  170. void *h = (void *)(conf->data);
  171. STACK_OF(BIO) *biosk = NULL;
  172. #ifndef OPENSSL_NO_POSIX_IO
  173. char *dirpath = NULL;
  174. OPENSSL_DIR_CTX *dirctx = NULL;
  175. #endif
  176. if ((buff = BUF_MEM_new()) == NULL) {
  177. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  178. goto err;
  179. }
  180. section = OPENSSL_strdup("default");
  181. if (section == NULL) {
  182. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  183. goto err;
  184. }
  185. if (_CONF_new_data(conf) == 0) {
  186. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  187. goto err;
  188. }
  189. sv = _CONF_new_section(conf, section);
  190. if (sv == NULL) {
  191. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  192. goto err;
  193. }
  194. bufnum = 0;
  195. again = 0;
  196. for (;;) {
  197. if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
  198. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  199. goto err;
  200. }
  201. p = &(buff->data[bufnum]);
  202. *p = '\0';
  203. read_retry:
  204. BIO_gets(in, p, CONFBUFSIZE - 1);
  205. p[CONFBUFSIZE - 1] = '\0';
  206. ii = i = strlen(p);
  207. if (first_call) {
  208. /* Other BOMs imply unsupported multibyte encoding,
  209. * so don't strip them and let the error raise */
  210. const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
  211. if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) {
  212. memmove(p, p + 3, i - 3);
  213. p[i - 3] = 0;
  214. i -= 3;
  215. ii -= 3;
  216. }
  217. first_call = 0;
  218. }
  219. if (i == 0 && !again) {
  220. /* the currently processed BIO is at EOF */
  221. BIO *parent;
  222. #ifndef OPENSSL_NO_POSIX_IO
  223. /* continue processing with the next file from directory */
  224. if (dirctx != NULL) {
  225. BIO *next;
  226. if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
  227. BIO_vfree(in);
  228. in = next;
  229. goto read_retry;
  230. } else {
  231. OPENSSL_free(dirpath);
  232. dirpath = NULL;
  233. }
  234. }
  235. #endif
  236. /* no more files in directory, continue with processing parent */
  237. if ((parent = sk_BIO_pop(biosk)) == NULL) {
  238. /* everything processed get out of the loop */
  239. break;
  240. } else {
  241. BIO_vfree(in);
  242. in = parent;
  243. goto read_retry;
  244. }
  245. }
  246. again = 0;
  247. while (i > 0) {
  248. if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
  249. break;
  250. else
  251. i--;
  252. }
  253. /*
  254. * we removed some trailing stuff so there is a new line on the end.
  255. */
  256. if (ii && i == ii)
  257. again = 1; /* long line */
  258. else {
  259. p[i] = '\0';
  260. eline++; /* another input line */
  261. }
  262. /* we now have a line with trailing \r\n removed */
  263. /* i is the number of bytes */
  264. bufnum += i;
  265. v = NULL;
  266. /* check for line continuation */
  267. if (bufnum >= 1) {
  268. /*
  269. * If we have bytes and the last char '\\' and second last char
  270. * is not '\\'
  271. */
  272. p = &(buff->data[bufnum - 1]);
  273. if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
  274. bufnum--;
  275. again = 1;
  276. }
  277. }
  278. if (again)
  279. continue;
  280. bufnum = 0;
  281. buf = buff->data;
  282. clear_comments(conf, buf);
  283. s = eat_ws(conf, buf);
  284. if (IS_EOF(conf, *s))
  285. continue; /* blank line */
  286. if (*s == '[') {
  287. char *ss;
  288. s++;
  289. start = eat_ws(conf, s);
  290. ss = start;
  291. again:
  292. end = eat_alpha_numeric(conf, ss);
  293. p = eat_ws(conf, end);
  294. if (*p != ']') {
  295. if (*p != '\0' && ss != p) {
  296. ss = p;
  297. goto again;
  298. }
  299. CONFerr(CONF_F_DEF_LOAD_BIO,
  300. CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
  301. goto err;
  302. }
  303. *end = '\0';
  304. if (!str_copy(conf, NULL, &section, start))
  305. goto err;
  306. if ((sv = _CONF_get_section(conf, section)) == NULL)
  307. sv = _CONF_new_section(conf, section);
  308. if (sv == NULL) {
  309. CONFerr(CONF_F_DEF_LOAD_BIO,
  310. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  311. goto err;
  312. }
  313. continue;
  314. } else {
  315. pname = s;
  316. end = eat_alpha_numeric(conf, s);
  317. if ((end[0] == ':') && (end[1] == ':')) {
  318. *end = '\0';
  319. end += 2;
  320. psection = pname;
  321. pname = end;
  322. end = eat_alpha_numeric(conf, end);
  323. } else {
  324. psection = section;
  325. }
  326. p = eat_ws(conf, end);
  327. if (strncmp(pname, ".include", 8) == 0
  328. && (p != pname + 8 || *p == '=')) {
  329. char *include = NULL;
  330. BIO *next;
  331. if (*p == '=') {
  332. p++;
  333. p = eat_ws(conf, p);
  334. }
  335. trim_ws(conf, p);
  336. if (!str_copy(conf, psection, &include, p))
  337. goto err;
  338. /* get the BIO of the included file */
  339. #ifndef OPENSSL_NO_POSIX_IO
  340. next = process_include(include, &dirctx, &dirpath);
  341. if (include != dirpath) {
  342. /* dirpath will contain include in case of a directory */
  343. OPENSSL_free(include);
  344. }
  345. #else
  346. next = BIO_new_file(include, "r");
  347. OPENSSL_free(include);
  348. #endif
  349. if (next != NULL) {
  350. /* push the currently processing BIO onto stack */
  351. if (biosk == NULL) {
  352. if ((biosk = sk_BIO_new_null()) == NULL) {
  353. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  354. BIO_free(next);
  355. goto err;
  356. }
  357. }
  358. if (!sk_BIO_push(biosk, in)) {
  359. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  360. BIO_free(next);
  361. goto err;
  362. }
  363. /* continue with reading from the included BIO */
  364. in = next;
  365. }
  366. continue;
  367. } else if (*p != '=') {
  368. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
  369. goto err;
  370. }
  371. *end = '\0';
  372. p++;
  373. start = eat_ws(conf, p);
  374. trim_ws(conf, start);
  375. if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
  376. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  377. goto err;
  378. }
  379. v->name = OPENSSL_strdup(pname);
  380. v->value = NULL;
  381. if (v->name == NULL) {
  382. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  383. goto err;
  384. }
  385. if (!str_copy(conf, psection, &(v->value), start))
  386. goto err;
  387. if (strcmp(psection, section) != 0) {
  388. if ((tv = _CONF_get_section(conf, psection))
  389. == NULL)
  390. tv = _CONF_new_section(conf, psection);
  391. if (tv == NULL) {
  392. CONFerr(CONF_F_DEF_LOAD_BIO,
  393. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  394. goto err;
  395. }
  396. } else
  397. tv = sv;
  398. if (_CONF_add_string(conf, tv, v) == 0) {
  399. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  400. goto err;
  401. }
  402. v = NULL;
  403. }
  404. }
  405. BUF_MEM_free(buff);
  406. OPENSSL_free(section);
  407. /*
  408. * No need to pop, since we only get here if the stack is empty.
  409. * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
  410. */
  411. sk_BIO_free(biosk);
  412. return 1;
  413. err:
  414. BUF_MEM_free(buff);
  415. OPENSSL_free(section);
  416. /*
  417. * Since |in| is the first element of the stack and should NOT be freed
  418. * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
  419. * BIO at a time, making sure that the last one popped isn't.
  420. */
  421. while (sk_BIO_num(biosk) > 0) {
  422. BIO *popped = sk_BIO_pop(biosk);
  423. BIO_vfree(in);
  424. in = popped;
  425. }
  426. sk_BIO_free(biosk);
  427. #ifndef OPENSSL_NO_POSIX_IO
  428. OPENSSL_free(dirpath);
  429. if (dirctx != NULL)
  430. OPENSSL_DIR_end(&dirctx);
  431. #endif
  432. if (line != NULL)
  433. *line = eline;
  434. BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
  435. ERR_add_error_data(2, "line ", btmp);
  436. if (h != conf->data) {
  437. CONF_free(conf->data);
  438. conf->data = NULL;
  439. }
  440. if (v != NULL) {
  441. OPENSSL_free(v->name);
  442. OPENSSL_free(v->value);
  443. OPENSSL_free(v);
  444. }
  445. return 0;
  446. }
  447. static void clear_comments(CONF *conf, char *p)
  448. {
  449. for (;;) {
  450. if (IS_FCOMMENT(conf, *p)) {
  451. *p = '\0';
  452. return;
  453. }
  454. if (!IS_WS(conf, *p)) {
  455. break;
  456. }
  457. p++;
  458. }
  459. for (;;) {
  460. if (IS_COMMENT(conf, *p)) {
  461. *p = '\0';
  462. return;
  463. }
  464. if (IS_DQUOTE(conf, *p)) {
  465. p = scan_dquote(conf, p);
  466. continue;
  467. }
  468. if (IS_QUOTE(conf, *p)) {
  469. p = scan_quote(conf, p);
  470. continue;
  471. }
  472. if (IS_ESC(conf, *p)) {
  473. p = scan_esc(conf, p);
  474. continue;
  475. }
  476. if (IS_EOF(conf, *p))
  477. return;
  478. else
  479. p++;
  480. }
  481. }
  482. static int str_copy(CONF *conf, char *section, char **pto, char *from)
  483. {
  484. int q, r, rr = 0, to = 0, len = 0;
  485. char *s, *e, *rp, *p, *rrp, *np, *cp, v;
  486. BUF_MEM *buf;
  487. if ((buf = BUF_MEM_new()) == NULL)
  488. return 0;
  489. len = strlen(from) + 1;
  490. if (!BUF_MEM_grow(buf, len))
  491. goto err;
  492. for (;;) {
  493. if (IS_QUOTE(conf, *from)) {
  494. q = *from;
  495. from++;
  496. while (!IS_EOF(conf, *from) && (*from != q)) {
  497. if (IS_ESC(conf, *from)) {
  498. from++;
  499. if (IS_EOF(conf, *from))
  500. break;
  501. }
  502. buf->data[to++] = *(from++);
  503. }
  504. if (*from == q)
  505. from++;
  506. } else if (IS_DQUOTE(conf, *from)) {
  507. q = *from;
  508. from++;
  509. while (!IS_EOF(conf, *from)) {
  510. if (*from == q) {
  511. if (*(from + 1) == q) {
  512. from++;
  513. } else {
  514. break;
  515. }
  516. }
  517. buf->data[to++] = *(from++);
  518. }
  519. if (*from == q)
  520. from++;
  521. } else if (IS_ESC(conf, *from)) {
  522. from++;
  523. v = *(from++);
  524. if (IS_EOF(conf, v))
  525. break;
  526. else if (v == 'r')
  527. v = '\r';
  528. else if (v == 'n')
  529. v = '\n';
  530. else if (v == 'b')
  531. v = '\b';
  532. else if (v == 't')
  533. v = '\t';
  534. buf->data[to++] = v;
  535. } else if (IS_EOF(conf, *from))
  536. break;
  537. else if (*from == '$') {
  538. size_t newsize;
  539. /* try to expand it */
  540. rrp = NULL;
  541. s = &(from[1]);
  542. if (*s == '{')
  543. q = '}';
  544. else if (*s == '(')
  545. q = ')';
  546. else
  547. q = 0;
  548. if (q)
  549. s++;
  550. cp = section;
  551. e = np = s;
  552. while (IS_ALNUM(conf, *e))
  553. e++;
  554. if ((e[0] == ':') && (e[1] == ':')) {
  555. cp = np;
  556. rrp = e;
  557. rr = *e;
  558. *rrp = '\0';
  559. e += 2;
  560. np = e;
  561. while (IS_ALNUM(conf, *e))
  562. e++;
  563. }
  564. r = *e;
  565. *e = '\0';
  566. rp = e;
  567. if (q) {
  568. if (r != q) {
  569. CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
  570. goto err;
  571. }
  572. e++;
  573. }
  574. /*-
  575. * So at this point we have
  576. * np which is the start of the name string which is
  577. * '\0' terminated.
  578. * cp which is the start of the section string which is
  579. * '\0' terminated.
  580. * e is the 'next point after'.
  581. * r and rr are the chars replaced by the '\0'
  582. * rp and rrp is where 'r' and 'rr' came from.
  583. */
  584. p = _CONF_get_string(conf, cp, np);
  585. if (rrp != NULL)
  586. *rrp = rr;
  587. *rp = r;
  588. if (p == NULL) {
  589. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
  590. goto err;
  591. }
  592. newsize = strlen(p) + buf->length - (e - from);
  593. if (newsize > MAX_CONF_VALUE_LENGTH) {
  594. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
  595. goto err;
  596. }
  597. if (!BUF_MEM_grow_clean(buf, newsize)) {
  598. CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
  599. goto err;
  600. }
  601. while (*p)
  602. buf->data[to++] = *(p++);
  603. /*
  604. * Since we change the pointer 'from', we also have to change the
  605. * perceived length of the string it points at. /RL
  606. */
  607. len -= e - from;
  608. from = e;
  609. /*
  610. * In case there were no braces or parenthesis around the
  611. * variable reference, we have to put back the character that was
  612. * replaced with a '\0'. /RL
  613. */
  614. *rp = r;
  615. } else
  616. buf->data[to++] = *(from++);
  617. }
  618. buf->data[to] = '\0';
  619. OPENSSL_free(*pto);
  620. *pto = buf->data;
  621. OPENSSL_free(buf);
  622. return 1;
  623. err:
  624. BUF_MEM_free(buf);
  625. return 0;
  626. }
  627. #ifndef OPENSSL_NO_POSIX_IO
  628. /*
  629. * Check whether included path is a directory.
  630. * Returns next BIO to process and in case of a directory
  631. * also an opened directory context and the include path.
  632. */
  633. static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
  634. char **dirpath)
  635. {
  636. struct stat st = { 0 };
  637. BIO *next;
  638. if (stat(include, &st) < 0) {
  639. SYSerr(SYS_F_STAT, errno);
  640. ERR_add_error_data(1, include);
  641. /* missing include file is not fatal error */
  642. return NULL;
  643. }
  644. if (S_ISDIR(st.st_mode)) {
  645. if (*dirctx != NULL) {
  646. CONFerr(CONF_F_PROCESS_INCLUDE,
  647. CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
  648. ERR_add_error_data(1, include);
  649. return NULL;
  650. }
  651. /* a directory, load its contents */
  652. if ((next = get_next_file(include, dirctx)) != NULL)
  653. *dirpath = include;
  654. return next;
  655. }
  656. next = BIO_new_file(include, "r");
  657. return next;
  658. }
  659. /*
  660. * Get next file from the directory path.
  661. * Returns BIO of the next file to read and updates dirctx.
  662. */
  663. static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
  664. {
  665. const char *filename;
  666. size_t pathlen;
  667. pathlen = strlen(path);
  668. while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
  669. size_t namelen;
  670. namelen = strlen(filename);
  671. if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
  672. || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
  673. size_t newlen;
  674. char *newpath;
  675. BIO *bio;
  676. newlen = pathlen + namelen + 2;
  677. newpath = OPENSSL_zalloc(newlen);
  678. if (newpath == NULL) {
  679. CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
  680. break;
  681. }
  682. #ifdef OPENSSL_SYS_VMS
  683. /*
  684. * If the given path isn't clear VMS syntax,
  685. * we treat it as on Unix.
  686. */
  687. if (path[pathlen - 1] == ']'
  688. || path[pathlen - 1] == '>'
  689. || path[pathlen - 1] == ':') {
  690. /* Clear VMS directory syntax, just copy as is */
  691. OPENSSL_strlcpy(newpath, path, newlen);
  692. }
  693. #endif
  694. if (newpath[0] == '\0') {
  695. OPENSSL_strlcpy(newpath, path, newlen);
  696. OPENSSL_strlcat(newpath, "/", newlen);
  697. }
  698. OPENSSL_strlcat(newpath, filename, newlen);
  699. bio = BIO_new_file(newpath, "r");
  700. OPENSSL_free(newpath);
  701. /* Errors when opening files are non-fatal. */
  702. if (bio != NULL)
  703. return bio;
  704. }
  705. }
  706. OPENSSL_DIR_end(dirctx);
  707. *dirctx = NULL;
  708. return NULL;
  709. }
  710. #endif
  711. static int is_keytype(const CONF *conf, char c, unsigned short type)
  712. {
  713. const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
  714. unsigned char key = (unsigned char)c;
  715. #ifdef CHARSET_EBCDIC
  716. # if CHAR_BIT > 8
  717. if (key > 255) {
  718. /* key is out of range for os_toascii table */
  719. return 0;
  720. }
  721. # endif
  722. /* convert key from ebcdic to ascii */
  723. key = os_toascii[key];
  724. #endif
  725. if (key > 127) {
  726. /* key is not a seven bit ascii character */
  727. return 0;
  728. }
  729. return (keytypes[key] & type) ? 1 : 0;
  730. }
  731. static char *eat_ws(CONF *conf, char *p)
  732. {
  733. while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
  734. p++;
  735. return p;
  736. }
  737. static void trim_ws(CONF *conf, char *start)
  738. {
  739. char *p = start;
  740. while (!IS_EOF(conf, *p))
  741. p++;
  742. p--;
  743. while ((p >= start) && IS_WS(conf, *p))
  744. p--;
  745. p++;
  746. *p = '\0';
  747. }
  748. static char *eat_alpha_numeric(CONF *conf, char *p)
  749. {
  750. for (;;) {
  751. if (IS_ESC(conf, *p)) {
  752. p = scan_esc(conf, p);
  753. continue;
  754. }
  755. if (!IS_ALNUM_PUNCT(conf, *p))
  756. return p;
  757. p++;
  758. }
  759. }
  760. static char *scan_quote(CONF *conf, char *p)
  761. {
  762. int q = *p;
  763. p++;
  764. while (!(IS_EOF(conf, *p)) && (*p != q)) {
  765. if (IS_ESC(conf, *p)) {
  766. p++;
  767. if (IS_EOF(conf, *p))
  768. return p;
  769. }
  770. p++;
  771. }
  772. if (*p == q)
  773. p++;
  774. return p;
  775. }
  776. static char *scan_dquote(CONF *conf, char *p)
  777. {
  778. int q = *p;
  779. p++;
  780. while (!(IS_EOF(conf, *p))) {
  781. if (*p == q) {
  782. if (*(p + 1) == q) {
  783. p++;
  784. } else {
  785. break;
  786. }
  787. }
  788. p++;
  789. }
  790. if (*p == q)
  791. p++;
  792. return p;
  793. }
  794. static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
  795. {
  796. if (a->name)
  797. BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
  798. else
  799. BIO_printf(out, "[[%s]]\n", a->section);
  800. }
  801. IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
  802. static int def_dump(const CONF *conf, BIO *out)
  803. {
  804. lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
  805. return 1;
  806. }
  807. static int def_is_number(const CONF *conf, char c)
  808. {
  809. return IS_NUMBER(conf, c);
  810. }
  811. static int def_to_int(const CONF *conf, char c)
  812. {
  813. return c - '0';
  814. }