conf_def.c 29 KB

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