conf_def.c 27 KB

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