conf_def.c 28 KB

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