conf_def.c 23 KB

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