2
0

conf_def.c 23 KB

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