conf_def.c 16 KB

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