conf_def.c 26 KB

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