conf_def.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Part of the code in here was originally in conf.c, which is now removed */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/stack.h>
  14. #include <openssl/lhash.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/conf_api.h>
  17. #include "conf_def.h"
  18. #include <openssl/buffer.h>
  19. #include <openssl/err.h>
  20. /*
  21. * The maximum length we can grow a value to after variable expansion. 64k
  22. * should be more than enough for all reasonable uses.
  23. */
  24. #define MAX_CONF_VALUE_LENGTH 65536
  25. static char *eat_ws(CONF *conf, char *p);
  26. static char *eat_alpha_numeric(CONF *conf, char *p);
  27. static void clear_comments(CONF *conf, char *p);
  28. static int str_copy(CONF *conf, char *section, char **to, char *from);
  29. static char *scan_quote(CONF *conf, char *p);
  30. static char *scan_dquote(CONF *conf, char *p);
  31. #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
  32. static CONF *def_create(CONF_METHOD *meth);
  33. static int def_init_default(CONF *conf);
  34. static int def_init_WIN32(CONF *conf);
  35. static int def_destroy(CONF *conf);
  36. static int def_destroy_data(CONF *conf);
  37. static int def_load(CONF *conf, const char *name, long *eline);
  38. static int def_load_bio(CONF *conf, BIO *bp, long *eline);
  39. static int def_dump(const CONF *conf, BIO *bp);
  40. static int def_is_number(const CONF *conf, char c);
  41. static int def_to_int(const CONF *conf, char c);
  42. static CONF_METHOD default_method = {
  43. "OpenSSL default",
  44. def_create,
  45. def_init_default,
  46. def_destroy,
  47. def_destroy_data,
  48. def_load_bio,
  49. def_dump,
  50. def_is_number,
  51. def_to_int,
  52. def_load
  53. };
  54. static CONF_METHOD WIN32_method = {
  55. "WIN32",
  56. def_create,
  57. def_init_WIN32,
  58. def_destroy,
  59. def_destroy_data,
  60. def_load_bio,
  61. def_dump,
  62. def_is_number,
  63. def_to_int,
  64. def_load
  65. };
  66. CONF_METHOD *NCONF_default()
  67. {
  68. return &default_method;
  69. }
  70. CONF_METHOD *NCONF_WIN32()
  71. {
  72. return &WIN32_method;
  73. }
  74. static CONF *def_create(CONF_METHOD *meth)
  75. {
  76. CONF *ret;
  77. ret = OPENSSL_malloc(sizeof(*ret));
  78. if (ret != NULL)
  79. if (meth->init(ret) == 0) {
  80. OPENSSL_free(ret);
  81. ret = NULL;
  82. }
  83. return ret;
  84. }
  85. static int def_init_default(CONF *conf)
  86. {
  87. if (conf == NULL)
  88. return 0;
  89. conf->meth = &default_method;
  90. conf->meth_data = (void *)CONF_type_default;
  91. conf->data = NULL;
  92. return 1;
  93. }
  94. static int def_init_WIN32(CONF *conf)
  95. {
  96. if (conf == NULL)
  97. return 0;
  98. conf->meth = &WIN32_method;
  99. conf->meth_data = (void *)CONF_type_win32;
  100. conf->data = NULL;
  101. return 1;
  102. }
  103. static int def_destroy(CONF *conf)
  104. {
  105. if (def_destroy_data(conf)) {
  106. OPENSSL_free(conf);
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. static int def_destroy_data(CONF *conf)
  112. {
  113. if (conf == NULL)
  114. return 0;
  115. _CONF_free_data(conf);
  116. return 1;
  117. }
  118. static int def_load(CONF *conf, const char *name, long *line)
  119. {
  120. int ret;
  121. BIO *in = NULL;
  122. #ifdef OPENSSL_SYS_VMS
  123. in = BIO_new_file(name, "r");
  124. #else
  125. in = BIO_new_file(name, "rb");
  126. #endif
  127. if (in == NULL) {
  128. if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
  129. CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
  130. else
  131. CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
  132. return 0;
  133. }
  134. ret = def_load_bio(conf, in, line);
  135. BIO_free(in);
  136. return ret;
  137. }
  138. static int def_load_bio(CONF *conf, BIO *in, long *line)
  139. {
  140. /* The macro BUFSIZE conflicts with a system macro in VxWorks */
  141. #define CONFBUFSIZE 512
  142. int bufnum = 0, i, ii;
  143. BUF_MEM *buff = NULL;
  144. char *s, *p, *end;
  145. int again;
  146. long eline = 0;
  147. char btmp[DECIMAL_SIZE(eline) + 1];
  148. CONF_VALUE *v = NULL, *tv;
  149. CONF_VALUE *sv = NULL;
  150. char *section = NULL, *buf;
  151. char *start, *psection, *pname;
  152. void *h = (void *)(conf->data);
  153. if ((buff = BUF_MEM_new()) == NULL) {
  154. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  155. goto err;
  156. }
  157. section = OPENSSL_strdup("default");
  158. if (section == NULL) {
  159. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  160. goto err;
  161. }
  162. if (_CONF_new_data(conf) == 0) {
  163. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  164. goto err;
  165. }
  166. sv = _CONF_new_section(conf, section);
  167. if (sv == NULL) {
  168. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  169. goto err;
  170. }
  171. bufnum = 0;
  172. again = 0;
  173. for (;;) {
  174. if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
  175. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  176. goto err;
  177. }
  178. p = &(buff->data[bufnum]);
  179. *p = '\0';
  180. BIO_gets(in, p, CONFBUFSIZE - 1);
  181. p[CONFBUFSIZE - 1] = '\0';
  182. ii = i = strlen(p);
  183. if (i == 0 && !again)
  184. break;
  185. again = 0;
  186. while (i > 0) {
  187. if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
  188. break;
  189. else
  190. i--;
  191. }
  192. /*
  193. * we removed some trailing stuff so there is a new line on the end.
  194. */
  195. if (ii && i == ii)
  196. again = 1; /* long line */
  197. else {
  198. p[i] = '\0';
  199. eline++; /* another input line */
  200. }
  201. /* we now have a line with trailing \r\n removed */
  202. /* i is the number of bytes */
  203. bufnum += i;
  204. v = NULL;
  205. /* check for line continuation */
  206. if (bufnum >= 1) {
  207. /*
  208. * If we have bytes and the last char '\\' and second last char
  209. * is not '\\'
  210. */
  211. p = &(buff->data[bufnum - 1]);
  212. if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
  213. bufnum--;
  214. again = 1;
  215. }
  216. }
  217. if (again)
  218. continue;
  219. bufnum = 0;
  220. buf = buff->data;
  221. clear_comments(conf, buf);
  222. s = eat_ws(conf, buf);
  223. if (IS_EOF(conf, *s))
  224. continue; /* blank line */
  225. if (*s == '[') {
  226. char *ss;
  227. s++;
  228. start = eat_ws(conf, s);
  229. ss = start;
  230. again:
  231. end = eat_alpha_numeric(conf, ss);
  232. p = eat_ws(conf, end);
  233. if (*p != ']') {
  234. if (*p != '\0' && ss != p) {
  235. ss = p;
  236. goto again;
  237. }
  238. CONFerr(CONF_F_DEF_LOAD_BIO,
  239. CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
  240. goto err;
  241. }
  242. *end = '\0';
  243. if (!str_copy(conf, NULL, &section, start))
  244. goto err;
  245. if ((sv = _CONF_get_section(conf, section)) == NULL)
  246. sv = _CONF_new_section(conf, section);
  247. if (sv == NULL) {
  248. CONFerr(CONF_F_DEF_LOAD_BIO,
  249. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  250. goto err;
  251. }
  252. continue;
  253. } else {
  254. pname = s;
  255. psection = NULL;
  256. end = eat_alpha_numeric(conf, s);
  257. if ((end[0] == ':') && (end[1] == ':')) {
  258. *end = '\0';
  259. end += 2;
  260. psection = pname;
  261. pname = end;
  262. end = eat_alpha_numeric(conf, end);
  263. }
  264. p = eat_ws(conf, end);
  265. if (*p != '=') {
  266. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
  267. goto err;
  268. }
  269. *end = '\0';
  270. p++;
  271. start = eat_ws(conf, p);
  272. while (!IS_EOF(conf, *p))
  273. p++;
  274. p--;
  275. while ((p != start) && (IS_WS(conf, *p)))
  276. p--;
  277. p++;
  278. *p = '\0';
  279. if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
  280. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  281. goto err;
  282. }
  283. if (psection == NULL)
  284. psection = section;
  285. v->name = OPENSSL_malloc(strlen(pname) + 1);
  286. v->value = NULL;
  287. if (v->name == NULL) {
  288. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  289. goto err;
  290. }
  291. OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
  292. if (!str_copy(conf, psection, &(v->value), start))
  293. goto err;
  294. if (strcmp(psection, section) != 0) {
  295. if ((tv = _CONF_get_section(conf, psection))
  296. == NULL)
  297. tv = _CONF_new_section(conf, psection);
  298. if (tv == NULL) {
  299. CONFerr(CONF_F_DEF_LOAD_BIO,
  300. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  301. goto err;
  302. }
  303. } else
  304. tv = sv;
  305. if (_CONF_add_string(conf, tv, v) == 0) {
  306. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  307. goto err;
  308. }
  309. v = NULL;
  310. }
  311. }
  312. BUF_MEM_free(buff);
  313. OPENSSL_free(section);
  314. return (1);
  315. err:
  316. BUF_MEM_free(buff);
  317. OPENSSL_free(section);
  318. if (line != NULL)
  319. *line = eline;
  320. BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
  321. ERR_add_error_data(2, "line ", btmp);
  322. if (h != conf->data) {
  323. CONF_free(conf->data);
  324. conf->data = NULL;
  325. }
  326. if (v != NULL) {
  327. OPENSSL_free(v->name);
  328. OPENSSL_free(v->value);
  329. OPENSSL_free(v);
  330. }
  331. return (0);
  332. }
  333. static void clear_comments(CONF *conf, char *p)
  334. {
  335. for (;;) {
  336. if (IS_FCOMMENT(conf, *p)) {
  337. *p = '\0';
  338. return;
  339. }
  340. if (!IS_WS(conf, *p)) {
  341. break;
  342. }
  343. p++;
  344. }
  345. for (;;) {
  346. if (IS_COMMENT(conf, *p)) {
  347. *p = '\0';
  348. return;
  349. }
  350. if (IS_DQUOTE(conf, *p)) {
  351. p = scan_dquote(conf, p);
  352. continue;
  353. }
  354. if (IS_QUOTE(conf, *p)) {
  355. p = scan_quote(conf, p);
  356. continue;
  357. }
  358. if (IS_ESC(conf, *p)) {
  359. p = scan_esc(conf, p);
  360. continue;
  361. }
  362. if (IS_EOF(conf, *p))
  363. return;
  364. else
  365. p++;
  366. }
  367. }
  368. static int str_copy(CONF *conf, char *section, char **pto, char *from)
  369. {
  370. int q, r, rr = 0, to = 0, len = 0;
  371. char *s, *e, *rp, *p, *rrp, *np, *cp, v;
  372. BUF_MEM *buf;
  373. if ((buf = BUF_MEM_new()) == NULL)
  374. return (0);
  375. len = strlen(from) + 1;
  376. if (!BUF_MEM_grow(buf, len))
  377. goto err;
  378. for (;;) {
  379. if (IS_QUOTE(conf, *from)) {
  380. q = *from;
  381. from++;
  382. while (!IS_EOF(conf, *from) && (*from != q)) {
  383. if (IS_ESC(conf, *from)) {
  384. from++;
  385. if (IS_EOF(conf, *from))
  386. break;
  387. }
  388. buf->data[to++] = *(from++);
  389. }
  390. if (*from == q)
  391. from++;
  392. } else if (IS_DQUOTE(conf, *from)) {
  393. q = *from;
  394. from++;
  395. while (!IS_EOF(conf, *from)) {
  396. if (*from == q) {
  397. if (*(from + 1) == q) {
  398. from++;
  399. } else {
  400. break;
  401. }
  402. }
  403. buf->data[to++] = *(from++);
  404. }
  405. if (*from == q)
  406. from++;
  407. } else if (IS_ESC(conf, *from)) {
  408. from++;
  409. v = *(from++);
  410. if (IS_EOF(conf, v))
  411. break;
  412. else if (v == 'r')
  413. v = '\r';
  414. else if (v == 'n')
  415. v = '\n';
  416. else if (v == 'b')
  417. v = '\b';
  418. else if (v == 't')
  419. v = '\t';
  420. buf->data[to++] = v;
  421. } else if (IS_EOF(conf, *from))
  422. break;
  423. else if (*from == '$') {
  424. size_t newsize;
  425. /* try to expand it */
  426. rrp = NULL;
  427. s = &(from[1]);
  428. if (*s == '{')
  429. q = '}';
  430. else if (*s == '(')
  431. q = ')';
  432. else
  433. q = 0;
  434. if (q)
  435. s++;
  436. cp = section;
  437. e = np = s;
  438. while (IS_ALPHA_NUMERIC(conf, *e))
  439. e++;
  440. if ((e[0] == ':') && (e[1] == ':')) {
  441. cp = np;
  442. rrp = e;
  443. rr = *e;
  444. *rrp = '\0';
  445. e += 2;
  446. np = e;
  447. while (IS_ALPHA_NUMERIC(conf, *e))
  448. e++;
  449. }
  450. r = *e;
  451. *e = '\0';
  452. rp = e;
  453. if (q) {
  454. if (r != q) {
  455. CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
  456. goto err;
  457. }
  458. e++;
  459. }
  460. /*-
  461. * So at this point we have
  462. * np which is the start of the name string which is
  463. * '\0' terminated.
  464. * cp which is the start of the section string which is
  465. * '\0' terminated.
  466. * e is the 'next point after'.
  467. * r and rr are the chars replaced by the '\0'
  468. * rp and rrp is where 'r' and 'rr' came from.
  469. */
  470. p = _CONF_get_string(conf, cp, np);
  471. if (rrp != NULL)
  472. *rrp = rr;
  473. *rp = r;
  474. if (p == NULL) {
  475. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
  476. goto err;
  477. }
  478. newsize = strlen(p) + buf->length - (e - from);
  479. if (newsize > MAX_CONF_VALUE_LENGTH) {
  480. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
  481. goto err;
  482. }
  483. if (!BUF_MEM_grow_clean(buf, newsize)) {
  484. CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
  485. goto err;
  486. }
  487. while (*p)
  488. buf->data[to++] = *(p++);
  489. /*
  490. * Since we change the pointer 'from', we also have to change the
  491. * perceived length of the string it points at. /RL
  492. */
  493. len -= e - from;
  494. from = e;
  495. /*
  496. * In case there were no braces or parenthesis around the
  497. * variable reference, we have to put back the character that was
  498. * replaced with a '\0'. /RL
  499. */
  500. *rp = r;
  501. } else
  502. buf->data[to++] = *(from++);
  503. }
  504. buf->data[to] = '\0';
  505. OPENSSL_free(*pto);
  506. *pto = buf->data;
  507. OPENSSL_free(buf);
  508. return (1);
  509. err:
  510. BUF_MEM_free(buf);
  511. return (0);
  512. }
  513. static char *eat_ws(CONF *conf, char *p)
  514. {
  515. while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
  516. p++;
  517. return (p);
  518. }
  519. static char *eat_alpha_numeric(CONF *conf, char *p)
  520. {
  521. for (;;) {
  522. if (IS_ESC(conf, *p)) {
  523. p = scan_esc(conf, p);
  524. continue;
  525. }
  526. if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
  527. return (p);
  528. p++;
  529. }
  530. }
  531. static char *scan_quote(CONF *conf, char *p)
  532. {
  533. int q = *p;
  534. p++;
  535. while (!(IS_EOF(conf, *p)) && (*p != q)) {
  536. if (IS_ESC(conf, *p)) {
  537. p++;
  538. if (IS_EOF(conf, *p))
  539. return (p);
  540. }
  541. p++;
  542. }
  543. if (*p == q)
  544. p++;
  545. return (p);
  546. }
  547. static char *scan_dquote(CONF *conf, char *p)
  548. {
  549. int q = *p;
  550. p++;
  551. while (!(IS_EOF(conf, *p))) {
  552. if (*p == q) {
  553. if (*(p + 1) == q) {
  554. p++;
  555. } else {
  556. break;
  557. }
  558. }
  559. p++;
  560. }
  561. if (*p == q)
  562. p++;
  563. return (p);
  564. }
  565. static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
  566. {
  567. if (a->name)
  568. BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
  569. else
  570. BIO_printf(out, "[[%s]]\n", a->section);
  571. }
  572. IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
  573. static int def_dump(const CONF *conf, BIO *out)
  574. {
  575. lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
  576. return 1;
  577. }
  578. static int def_is_number(const CONF *conf, char c)
  579. {
  580. return IS_NUMBER(conf, c);
  581. }
  582. static int def_to_int(const CONF *conf, char c)
  583. {
  584. return c - '0';
  585. }