conf_def.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Copyright 1995-2020 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. long eline = 0;
  164. char btmp[DECIMAL_SIZE(eline) + 1];
  165. CONF_VALUE *v = NULL, *tv;
  166. CONF_VALUE *sv = NULL;
  167. char *section = NULL, *buf;
  168. char *start, *psection, *pname;
  169. void *h = (void *)(conf->data);
  170. STACK_OF(BIO) *biosk = NULL;
  171. #ifndef OPENSSL_NO_POSIX_IO
  172. char *dirpath = NULL;
  173. OPENSSL_DIR_CTX *dirctx = NULL;
  174. #endif
  175. if ((buff = BUF_MEM_new()) == NULL) {
  176. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  177. goto err;
  178. }
  179. section = OPENSSL_strdup("default");
  180. if (section == NULL) {
  181. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  182. goto err;
  183. }
  184. if (_CONF_new_data(conf) == 0) {
  185. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  186. goto err;
  187. }
  188. sv = _CONF_new_section(conf, section);
  189. if (sv == NULL) {
  190. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  191. goto err;
  192. }
  193. bufnum = 0;
  194. again = 0;
  195. for (;;) {
  196. if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
  197. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  198. goto err;
  199. }
  200. p = &(buff->data[bufnum]);
  201. *p = '\0';
  202. read_retry:
  203. BIO_gets(in, p, CONFBUFSIZE - 1);
  204. p[CONFBUFSIZE - 1] = '\0';
  205. ii = i = strlen(p);
  206. if (i == 0 && !again) {
  207. /* the currently processed BIO is at EOF */
  208. BIO *parent;
  209. #ifndef OPENSSL_NO_POSIX_IO
  210. /* continue processing with the next file from directory */
  211. if (dirctx != NULL) {
  212. BIO *next;
  213. if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
  214. BIO_vfree(in);
  215. in = next;
  216. goto read_retry;
  217. } else {
  218. OPENSSL_free(dirpath);
  219. dirpath = NULL;
  220. }
  221. }
  222. #endif
  223. /* no more files in directory, continue with processing parent */
  224. if ((parent = sk_BIO_pop(biosk)) == NULL) {
  225. /* everything processed get out of the loop */
  226. break;
  227. } else {
  228. BIO_vfree(in);
  229. in = parent;
  230. goto read_retry;
  231. }
  232. }
  233. again = 0;
  234. while (i > 0) {
  235. if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
  236. break;
  237. else
  238. i--;
  239. }
  240. /*
  241. * we removed some trailing stuff so there is a new line on the end.
  242. */
  243. if (ii && i == ii)
  244. again = 1; /* long line */
  245. else {
  246. p[i] = '\0';
  247. eline++; /* another input line */
  248. }
  249. /* we now have a line with trailing \r\n removed */
  250. /* i is the number of bytes */
  251. bufnum += i;
  252. v = NULL;
  253. /* check for line continuation */
  254. if (bufnum >= 1) {
  255. /*
  256. * If we have bytes and the last char '\\' and second last char
  257. * is not '\\'
  258. */
  259. p = &(buff->data[bufnum - 1]);
  260. if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
  261. bufnum--;
  262. again = 1;
  263. }
  264. }
  265. if (again)
  266. continue;
  267. bufnum = 0;
  268. buf = buff->data;
  269. clear_comments(conf, buf);
  270. s = eat_ws(conf, buf);
  271. if (IS_EOF(conf, *s))
  272. continue; /* blank line */
  273. if (*s == '[') {
  274. char *ss;
  275. s++;
  276. start = eat_ws(conf, s);
  277. ss = start;
  278. again:
  279. end = eat_alpha_numeric(conf, ss);
  280. p = eat_ws(conf, end);
  281. if (*p != ']') {
  282. if (*p != '\0' && ss != p) {
  283. ss = p;
  284. goto again;
  285. }
  286. CONFerr(CONF_F_DEF_LOAD_BIO,
  287. CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
  288. goto err;
  289. }
  290. *end = '\0';
  291. if (!str_copy(conf, NULL, &section, start))
  292. goto err;
  293. if ((sv = _CONF_get_section(conf, section)) == NULL)
  294. sv = _CONF_new_section(conf, section);
  295. if (sv == NULL) {
  296. CONFerr(CONF_F_DEF_LOAD_BIO,
  297. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  298. goto err;
  299. }
  300. continue;
  301. } else {
  302. pname = s;
  303. end = eat_alpha_numeric(conf, s);
  304. if ((end[0] == ':') && (end[1] == ':')) {
  305. *end = '\0';
  306. end += 2;
  307. psection = pname;
  308. pname = end;
  309. end = eat_alpha_numeric(conf, end);
  310. } else {
  311. psection = section;
  312. }
  313. p = eat_ws(conf, end);
  314. if (strncmp(pname, ".include", 8) == 0
  315. && (p != pname + 8 || *p == '=')) {
  316. char *include = NULL;
  317. BIO *next;
  318. if (*p == '=') {
  319. p++;
  320. p = eat_ws(conf, p);
  321. }
  322. trim_ws(conf, p);
  323. if (!str_copy(conf, psection, &include, p))
  324. goto err;
  325. /* get the BIO of the included file */
  326. #ifndef OPENSSL_NO_POSIX_IO
  327. next = process_include(include, &dirctx, &dirpath);
  328. if (include != dirpath) {
  329. /* dirpath will contain include in case of a directory */
  330. OPENSSL_free(include);
  331. }
  332. #else
  333. next = BIO_new_file(include, "r");
  334. OPENSSL_free(include);
  335. #endif
  336. if (next != NULL) {
  337. /* push the currently processing BIO onto stack */
  338. if (biosk == NULL) {
  339. if ((biosk = sk_BIO_new_null()) == NULL) {
  340. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  341. BIO_free(next);
  342. goto err;
  343. }
  344. }
  345. if (!sk_BIO_push(biosk, in)) {
  346. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  347. BIO_free(next);
  348. goto err;
  349. }
  350. /* continue with reading from the included BIO */
  351. in = next;
  352. }
  353. continue;
  354. } else if (*p != '=') {
  355. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
  356. goto err;
  357. }
  358. *end = '\0';
  359. p++;
  360. start = eat_ws(conf, p);
  361. trim_ws(conf, start);
  362. if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
  363. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  364. goto err;
  365. }
  366. v->name = OPENSSL_strdup(pname);
  367. v->value = NULL;
  368. if (v->name == NULL) {
  369. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  370. goto err;
  371. }
  372. if (!str_copy(conf, psection, &(v->value), start))
  373. goto err;
  374. if (strcmp(psection, section) != 0) {
  375. if ((tv = _CONF_get_section(conf, psection))
  376. == NULL)
  377. tv = _CONF_new_section(conf, psection);
  378. if (tv == NULL) {
  379. CONFerr(CONF_F_DEF_LOAD_BIO,
  380. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  381. goto err;
  382. }
  383. } else
  384. tv = sv;
  385. if (_CONF_add_string(conf, tv, v) == 0) {
  386. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  387. goto err;
  388. }
  389. v = NULL;
  390. }
  391. }
  392. BUF_MEM_free(buff);
  393. OPENSSL_free(section);
  394. /*
  395. * No need to pop, since we only get here if the stack is empty.
  396. * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
  397. */
  398. sk_BIO_free(biosk);
  399. return 1;
  400. err:
  401. BUF_MEM_free(buff);
  402. OPENSSL_free(section);
  403. /*
  404. * Since |in| is the first element of the stack and should NOT be freed
  405. * here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one
  406. * BIO at a time, making sure that the last one popped isn't.
  407. */
  408. while (sk_BIO_num(biosk) > 0) {
  409. BIO *popped = sk_BIO_pop(biosk);
  410. BIO_vfree(in);
  411. in = popped;
  412. }
  413. sk_BIO_free(biosk);
  414. #ifndef OPENSSL_NO_POSIX_IO
  415. OPENSSL_free(dirpath);
  416. if (dirctx != NULL)
  417. OPENSSL_DIR_end(&dirctx);
  418. #endif
  419. if (line != NULL)
  420. *line = eline;
  421. BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
  422. ERR_add_error_data(2, "line ", btmp);
  423. if (h != conf->data) {
  424. CONF_free(conf->data);
  425. conf->data = NULL;
  426. }
  427. if (v != NULL) {
  428. OPENSSL_free(v->name);
  429. OPENSSL_free(v->value);
  430. OPENSSL_free(v);
  431. }
  432. return 0;
  433. }
  434. static void clear_comments(CONF *conf, char *p)
  435. {
  436. for (;;) {
  437. if (IS_FCOMMENT(conf, *p)) {
  438. *p = '\0';
  439. return;
  440. }
  441. if (!IS_WS(conf, *p)) {
  442. break;
  443. }
  444. p++;
  445. }
  446. for (;;) {
  447. if (IS_COMMENT(conf, *p)) {
  448. *p = '\0';
  449. return;
  450. }
  451. if (IS_DQUOTE(conf, *p)) {
  452. p = scan_dquote(conf, p);
  453. continue;
  454. }
  455. if (IS_QUOTE(conf, *p)) {
  456. p = scan_quote(conf, p);
  457. continue;
  458. }
  459. if (IS_ESC(conf, *p)) {
  460. p = scan_esc(conf, p);
  461. continue;
  462. }
  463. if (IS_EOF(conf, *p))
  464. return;
  465. else
  466. p++;
  467. }
  468. }
  469. static int str_copy(CONF *conf, char *section, char **pto, char *from)
  470. {
  471. int q, r, rr = 0, to = 0, len = 0;
  472. char *s, *e, *rp, *p, *rrp, *np, *cp, v;
  473. BUF_MEM *buf;
  474. if ((buf = BUF_MEM_new()) == NULL)
  475. return 0;
  476. len = strlen(from) + 1;
  477. if (!BUF_MEM_grow(buf, len))
  478. goto err;
  479. for (;;) {
  480. if (IS_QUOTE(conf, *from)) {
  481. q = *from;
  482. from++;
  483. while (!IS_EOF(conf, *from) && (*from != q)) {
  484. if (IS_ESC(conf, *from)) {
  485. from++;
  486. if (IS_EOF(conf, *from))
  487. break;
  488. }
  489. buf->data[to++] = *(from++);
  490. }
  491. if (*from == q)
  492. from++;
  493. } else if (IS_DQUOTE(conf, *from)) {
  494. q = *from;
  495. from++;
  496. while (!IS_EOF(conf, *from)) {
  497. if (*from == q) {
  498. if (*(from + 1) == q) {
  499. from++;
  500. } else {
  501. break;
  502. }
  503. }
  504. buf->data[to++] = *(from++);
  505. }
  506. if (*from == q)
  507. from++;
  508. } else if (IS_ESC(conf, *from)) {
  509. from++;
  510. v = *(from++);
  511. if (IS_EOF(conf, v))
  512. break;
  513. else if (v == 'r')
  514. v = '\r';
  515. else if (v == 'n')
  516. v = '\n';
  517. else if (v == 'b')
  518. v = '\b';
  519. else if (v == 't')
  520. v = '\t';
  521. buf->data[to++] = v;
  522. } else if (IS_EOF(conf, *from))
  523. break;
  524. else if (*from == '$') {
  525. size_t newsize;
  526. /* try to expand it */
  527. rrp = NULL;
  528. s = &(from[1]);
  529. if (*s == '{')
  530. q = '}';
  531. else if (*s == '(')
  532. q = ')';
  533. else
  534. q = 0;
  535. if (q)
  536. s++;
  537. cp = section;
  538. e = np = s;
  539. while (IS_ALNUM(conf, *e))
  540. e++;
  541. if ((e[0] == ':') && (e[1] == ':')) {
  542. cp = np;
  543. rrp = e;
  544. rr = *e;
  545. *rrp = '\0';
  546. e += 2;
  547. np = e;
  548. while (IS_ALNUM(conf, *e))
  549. e++;
  550. }
  551. r = *e;
  552. *e = '\0';
  553. rp = e;
  554. if (q) {
  555. if (r != q) {
  556. CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
  557. goto err;
  558. }
  559. e++;
  560. }
  561. /*-
  562. * So at this point we have
  563. * np which is the start of the name string which is
  564. * '\0' terminated.
  565. * cp which is the start of the section string which is
  566. * '\0' terminated.
  567. * e is the 'next point after'.
  568. * r and rr are the chars replaced by the '\0'
  569. * rp and rrp is where 'r' and 'rr' came from.
  570. */
  571. p = _CONF_get_string(conf, cp, np);
  572. if (rrp != NULL)
  573. *rrp = rr;
  574. *rp = r;
  575. if (p == NULL) {
  576. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
  577. goto err;
  578. }
  579. newsize = strlen(p) + buf->length - (e - from);
  580. if (newsize > MAX_CONF_VALUE_LENGTH) {
  581. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
  582. goto err;
  583. }
  584. if (!BUF_MEM_grow_clean(buf, newsize)) {
  585. CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
  586. goto err;
  587. }
  588. while (*p)
  589. buf->data[to++] = *(p++);
  590. /*
  591. * Since we change the pointer 'from', we also have to change the
  592. * perceived length of the string it points at. /RL
  593. */
  594. len -= e - from;
  595. from = e;
  596. /*
  597. * In case there were no braces or parenthesis around the
  598. * variable reference, we have to put back the character that was
  599. * replaced with a '\0'. /RL
  600. */
  601. *rp = r;
  602. } else
  603. buf->data[to++] = *(from++);
  604. }
  605. buf->data[to] = '\0';
  606. OPENSSL_free(*pto);
  607. *pto = buf->data;
  608. OPENSSL_free(buf);
  609. return 1;
  610. err:
  611. BUF_MEM_free(buf);
  612. return 0;
  613. }
  614. #ifndef OPENSSL_NO_POSIX_IO
  615. /*
  616. * Check whether included path is a directory.
  617. * Returns next BIO to process and in case of a directory
  618. * also an opened directory context and the include path.
  619. */
  620. static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
  621. char **dirpath)
  622. {
  623. struct stat st = { 0 };
  624. BIO *next;
  625. if (stat(include, &st) < 0) {
  626. SYSerr(SYS_F_STAT, errno);
  627. ERR_add_error_data(1, include);
  628. /* missing include file is not fatal error */
  629. return NULL;
  630. }
  631. if (S_ISDIR(st.st_mode)) {
  632. if (*dirctx != NULL) {
  633. CONFerr(CONF_F_PROCESS_INCLUDE,
  634. CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
  635. ERR_add_error_data(1, include);
  636. return NULL;
  637. }
  638. /* a directory, load its contents */
  639. if ((next = get_next_file(include, dirctx)) != NULL)
  640. *dirpath = include;
  641. return next;
  642. }
  643. next = BIO_new_file(include, "r");
  644. return next;
  645. }
  646. /*
  647. * Get next file from the directory path.
  648. * Returns BIO of the next file to read and updates dirctx.
  649. */
  650. static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
  651. {
  652. const char *filename;
  653. size_t pathlen;
  654. pathlen = strlen(path);
  655. while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
  656. size_t namelen;
  657. namelen = strlen(filename);
  658. if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
  659. || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
  660. size_t newlen;
  661. char *newpath;
  662. BIO *bio;
  663. newlen = pathlen + namelen + 2;
  664. newpath = OPENSSL_zalloc(newlen);
  665. if (newpath == NULL) {
  666. CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
  667. break;
  668. }
  669. #ifdef OPENSSL_SYS_VMS
  670. /*
  671. * If the given path isn't clear VMS syntax,
  672. * we treat it as on Unix.
  673. */
  674. if (path[pathlen - 1] == ']'
  675. || path[pathlen - 1] == '>'
  676. || path[pathlen - 1] == ':') {
  677. /* Clear VMS directory syntax, just copy as is */
  678. OPENSSL_strlcpy(newpath, path, newlen);
  679. }
  680. #endif
  681. if (newpath[0] == '\0') {
  682. OPENSSL_strlcpy(newpath, path, newlen);
  683. OPENSSL_strlcat(newpath, "/", newlen);
  684. }
  685. OPENSSL_strlcat(newpath, filename, newlen);
  686. bio = BIO_new_file(newpath, "r");
  687. OPENSSL_free(newpath);
  688. /* Errors when opening files are non-fatal. */
  689. if (bio != NULL)
  690. return bio;
  691. }
  692. }
  693. OPENSSL_DIR_end(dirctx);
  694. *dirctx = NULL;
  695. return NULL;
  696. }
  697. #endif
  698. static int is_keytype(const CONF *conf, char c, unsigned short type)
  699. {
  700. const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
  701. unsigned char key = (unsigned char)c;
  702. #ifdef CHARSET_EBCDIC
  703. # if CHAR_BIT > 8
  704. if (key > 255) {
  705. /* key is out of range for os_toascii table */
  706. return 0;
  707. }
  708. # endif
  709. /* convert key from ebcdic to ascii */
  710. key = os_toascii[key];
  711. #endif
  712. if (key > 127) {
  713. /* key is not a seven bit ascii character */
  714. return 0;
  715. }
  716. return (keytypes[key] & type) ? 1 : 0;
  717. }
  718. static char *eat_ws(CONF *conf, char *p)
  719. {
  720. while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
  721. p++;
  722. return p;
  723. }
  724. static void trim_ws(CONF *conf, char *start)
  725. {
  726. char *p = start;
  727. while (!IS_EOF(conf, *p))
  728. p++;
  729. p--;
  730. while ((p >= start) && IS_WS(conf, *p))
  731. p--;
  732. p++;
  733. *p = '\0';
  734. }
  735. static char *eat_alpha_numeric(CONF *conf, char *p)
  736. {
  737. for (;;) {
  738. if (IS_ESC(conf, *p)) {
  739. p = scan_esc(conf, p);
  740. continue;
  741. }
  742. if (!IS_ALNUM_PUNCT(conf, *p))
  743. return p;
  744. p++;
  745. }
  746. }
  747. static char *scan_quote(CONF *conf, char *p)
  748. {
  749. int q = *p;
  750. p++;
  751. while (!(IS_EOF(conf, *p)) && (*p != q)) {
  752. if (IS_ESC(conf, *p)) {
  753. p++;
  754. if (IS_EOF(conf, *p))
  755. return p;
  756. }
  757. p++;
  758. }
  759. if (*p == q)
  760. p++;
  761. return p;
  762. }
  763. static char *scan_dquote(CONF *conf, char *p)
  764. {
  765. int q = *p;
  766. p++;
  767. while (!(IS_EOF(conf, *p))) {
  768. if (*p == q) {
  769. if (*(p + 1) == q) {
  770. p++;
  771. } else {
  772. break;
  773. }
  774. }
  775. p++;
  776. }
  777. if (*p == q)
  778. p++;
  779. return p;
  780. }
  781. static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
  782. {
  783. if (a->name)
  784. BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
  785. else
  786. BIO_printf(out, "[[%s]]\n", a->section);
  787. }
  788. IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
  789. static int def_dump(const CONF *conf, BIO *out)
  790. {
  791. lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
  792. return 1;
  793. }
  794. static int def_is_number(const CONF *conf, char c)
  795. {
  796. return IS_NUMBER(conf, c);
  797. }
  798. static int def_to_int(const CONF *conf, char c)
  799. {
  800. return c - '0';
  801. }