conf_def.c 23 KB

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