conf_def.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* crypto/conf/conf.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* Part of the code in here was originally in conf.c, which is now removed */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "internal/cryptlib.h"
  62. #include <openssl/stack.h>
  63. #include <openssl/lhash.h>
  64. #include <openssl/conf.h>
  65. #include <openssl/conf_api.h>
  66. #include "conf_def.h"
  67. #include <openssl/buffer.h>
  68. #include <openssl/err.h>
  69. static char *eat_ws(CONF *conf, char *p);
  70. static char *eat_alpha_numeric(CONF *conf, char *p);
  71. static void clear_comments(CONF *conf, char *p);
  72. static int str_copy(CONF *conf, char *section, char **to, char *from);
  73. static char *scan_quote(CONF *conf, char *p);
  74. static char *scan_dquote(CONF *conf, char *p);
  75. #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
  76. static CONF *def_create(CONF_METHOD *meth);
  77. static int def_init_default(CONF *conf);
  78. static int def_init_WIN32(CONF *conf);
  79. static int def_destroy(CONF *conf);
  80. static int def_destroy_data(CONF *conf);
  81. static int def_load(CONF *conf, const char *name, long *eline);
  82. static int def_load_bio(CONF *conf, BIO *bp, long *eline);
  83. static int def_dump(const CONF *conf, BIO *bp);
  84. static int def_is_number(const CONF *conf, char c);
  85. static int def_to_int(const CONF *conf, char c);
  86. static CONF_METHOD default_method = {
  87. "OpenSSL default",
  88. def_create,
  89. def_init_default,
  90. def_destroy,
  91. def_destroy_data,
  92. def_load_bio,
  93. def_dump,
  94. def_is_number,
  95. def_to_int,
  96. def_load
  97. };
  98. static CONF_METHOD WIN32_method = {
  99. "WIN32",
  100. def_create,
  101. def_init_WIN32,
  102. def_destroy,
  103. def_destroy_data,
  104. def_load_bio,
  105. def_dump,
  106. def_is_number,
  107. def_to_int,
  108. def_load
  109. };
  110. CONF_METHOD *NCONF_default()
  111. {
  112. return &default_method;
  113. }
  114. CONF_METHOD *NCONF_WIN32()
  115. {
  116. return &WIN32_method;
  117. }
  118. static CONF *def_create(CONF_METHOD *meth)
  119. {
  120. CONF *ret;
  121. ret = OPENSSL_malloc(sizeof(*ret));
  122. if (ret != NULL)
  123. if (meth->init(ret) == 0) {
  124. OPENSSL_free(ret);
  125. ret = NULL;
  126. }
  127. return ret;
  128. }
  129. static int def_init_default(CONF *conf)
  130. {
  131. if (conf == NULL)
  132. return 0;
  133. conf->meth = &default_method;
  134. conf->meth_data = (void *)CONF_type_default;
  135. conf->data = NULL;
  136. return 1;
  137. }
  138. static int def_init_WIN32(CONF *conf)
  139. {
  140. if (conf == NULL)
  141. return 0;
  142. conf->meth = &WIN32_method;
  143. conf->meth_data = (void *)CONF_type_win32;
  144. conf->data = NULL;
  145. return 1;
  146. }
  147. static int def_destroy(CONF *conf)
  148. {
  149. if (def_destroy_data(conf)) {
  150. OPENSSL_free(conf);
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. static int def_destroy_data(CONF *conf)
  156. {
  157. if (conf == NULL)
  158. return 0;
  159. _CONF_free_data(conf);
  160. return 1;
  161. }
  162. static int def_load(CONF *conf, const char *name, long *line)
  163. {
  164. int ret;
  165. BIO *in = NULL;
  166. #ifdef OPENSSL_SYS_VMS
  167. in = BIO_new_file(name, "r");
  168. #else
  169. in = BIO_new_file(name, "rb");
  170. #endif
  171. if (in == NULL) {
  172. if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
  173. CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
  174. else
  175. CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
  176. return 0;
  177. }
  178. ret = def_load_bio(conf, in, line);
  179. BIO_free(in);
  180. return ret;
  181. }
  182. static int def_load_bio(CONF *conf, BIO *in, long *line)
  183. {
  184. /* The macro BUFSIZE conflicts with a system macro in VxWorks */
  185. #define CONFBUFSIZE 512
  186. int bufnum = 0, i, ii;
  187. BUF_MEM *buff = NULL;
  188. char *s, *p, *end;
  189. int again;
  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. if ((buff = BUF_MEM_new()) == NULL) {
  198. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  199. goto err;
  200. }
  201. section = OPENSSL_strdup("default");
  202. if (section == NULL) {
  203. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  204. goto err;
  205. }
  206. if (_CONF_new_data(conf) == 0) {
  207. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  208. goto err;
  209. }
  210. sv = _CONF_new_section(conf, section);
  211. if (sv == NULL) {
  212. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  213. goto err;
  214. }
  215. bufnum = 0;
  216. again = 0;
  217. for (;;) {
  218. if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
  219. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
  220. goto err;
  221. }
  222. p = &(buff->data[bufnum]);
  223. *p = '\0';
  224. BIO_gets(in, p, CONFBUFSIZE - 1);
  225. p[CONFBUFSIZE - 1] = '\0';
  226. ii = i = strlen(p);
  227. if (i == 0 && !again)
  228. break;
  229. again = 0;
  230. while (i > 0) {
  231. if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
  232. break;
  233. else
  234. i--;
  235. }
  236. /*
  237. * we removed some trailing stuff so there is a new line on the end.
  238. */
  239. if (ii && i == ii)
  240. again = 1; /* long line */
  241. else {
  242. p[i] = '\0';
  243. eline++; /* another input line */
  244. }
  245. /* we now have a line with trailing \r\n removed */
  246. /* i is the number of bytes */
  247. bufnum += i;
  248. v = NULL;
  249. /* check for line continuation */
  250. if (bufnum >= 1) {
  251. /*
  252. * If we have bytes and the last char '\\' and second last char
  253. * is not '\\'
  254. */
  255. p = &(buff->data[bufnum - 1]);
  256. if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
  257. bufnum--;
  258. again = 1;
  259. }
  260. }
  261. if (again)
  262. continue;
  263. bufnum = 0;
  264. buf = buff->data;
  265. clear_comments(conf, buf);
  266. s = eat_ws(conf, buf);
  267. if (IS_EOF(conf, *s))
  268. continue; /* blank line */
  269. if (*s == '[') {
  270. char *ss;
  271. s++;
  272. start = eat_ws(conf, s);
  273. ss = start;
  274. again:
  275. end = eat_alpha_numeric(conf, ss);
  276. p = eat_ws(conf, end);
  277. if (*p != ']') {
  278. if (*p != '\0' && ss != p) {
  279. ss = p;
  280. goto again;
  281. }
  282. CONFerr(CONF_F_DEF_LOAD_BIO,
  283. CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
  284. goto err;
  285. }
  286. *end = '\0';
  287. if (!str_copy(conf, NULL, &section, start))
  288. goto err;
  289. if ((sv = _CONF_get_section(conf, section)) == NULL)
  290. sv = _CONF_new_section(conf, section);
  291. if (sv == NULL) {
  292. CONFerr(CONF_F_DEF_LOAD_BIO,
  293. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  294. goto err;
  295. }
  296. continue;
  297. } else {
  298. pname = s;
  299. psection = NULL;
  300. end = eat_alpha_numeric(conf, s);
  301. if ((end[0] == ':') && (end[1] == ':')) {
  302. *end = '\0';
  303. end += 2;
  304. psection = pname;
  305. pname = end;
  306. end = eat_alpha_numeric(conf, end);
  307. }
  308. p = eat_ws(conf, end);
  309. if (*p != '=') {
  310. CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
  311. goto err;
  312. }
  313. *end = '\0';
  314. p++;
  315. start = eat_ws(conf, p);
  316. while (!IS_EOF(conf, *p))
  317. p++;
  318. p--;
  319. while ((p != start) && (IS_WS(conf, *p)))
  320. p--;
  321. p++;
  322. *p = '\0';
  323. if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
  324. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  325. goto err;
  326. }
  327. if (psection == NULL)
  328. psection = section;
  329. v->name = OPENSSL_malloc(strlen(pname) + 1);
  330. v->value = NULL;
  331. if (v->name == NULL) {
  332. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  333. goto err;
  334. }
  335. OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
  336. if (!str_copy(conf, psection, &(v->value), start))
  337. goto err;
  338. if (strcmp(psection, section) != 0) {
  339. if ((tv = _CONF_get_section(conf, psection))
  340. == NULL)
  341. tv = _CONF_new_section(conf, psection);
  342. if (tv == NULL) {
  343. CONFerr(CONF_F_DEF_LOAD_BIO,
  344. CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  345. goto err;
  346. }
  347. } else
  348. tv = sv;
  349. if (_CONF_add_string(conf, tv, v) == 0) {
  350. CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
  351. goto err;
  352. }
  353. v = NULL;
  354. }
  355. }
  356. BUF_MEM_free(buff);
  357. OPENSSL_free(section);
  358. return (1);
  359. err:
  360. BUF_MEM_free(buff);
  361. OPENSSL_free(section);
  362. if (line != NULL)
  363. *line = eline;
  364. BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
  365. ERR_add_error_data(2, "line ", btmp);
  366. if (h != conf->data) {
  367. CONF_free(conf->data);
  368. conf->data = NULL;
  369. }
  370. if (v != NULL) {
  371. OPENSSL_free(v->name);
  372. OPENSSL_free(v->value);
  373. OPENSSL_free(v);
  374. }
  375. return (0);
  376. }
  377. static void clear_comments(CONF *conf, char *p)
  378. {
  379. for (;;) {
  380. if (IS_FCOMMENT(conf, *p)) {
  381. *p = '\0';
  382. return;
  383. }
  384. if (!IS_WS(conf, *p)) {
  385. break;
  386. }
  387. p++;
  388. }
  389. for (;;) {
  390. if (IS_COMMENT(conf, *p)) {
  391. *p = '\0';
  392. return;
  393. }
  394. if (IS_DQUOTE(conf, *p)) {
  395. p = scan_dquote(conf, p);
  396. continue;
  397. }
  398. if (IS_QUOTE(conf, *p)) {
  399. p = scan_quote(conf, p);
  400. continue;
  401. }
  402. if (IS_ESC(conf, *p)) {
  403. p = scan_esc(conf, p);
  404. continue;
  405. }
  406. if (IS_EOF(conf, *p))
  407. return;
  408. else
  409. p++;
  410. }
  411. }
  412. static int str_copy(CONF *conf, char *section, char **pto, char *from)
  413. {
  414. int q, r, rr = 0, to = 0, len = 0;
  415. char *s, *e, *rp, *p, *rrp, *np, *cp, v;
  416. BUF_MEM *buf;
  417. if ((buf = BUF_MEM_new()) == NULL)
  418. return (0);
  419. len = strlen(from) + 1;
  420. if (!BUF_MEM_grow(buf, len))
  421. goto err;
  422. for (;;) {
  423. if (IS_QUOTE(conf, *from)) {
  424. q = *from;
  425. from++;
  426. while (!IS_EOF(conf, *from) && (*from != q)) {
  427. if (IS_ESC(conf, *from)) {
  428. from++;
  429. if (IS_EOF(conf, *from))
  430. break;
  431. }
  432. buf->data[to++] = *(from++);
  433. }
  434. if (*from == q)
  435. from++;
  436. } else if (IS_DQUOTE(conf, *from)) {
  437. q = *from;
  438. from++;
  439. while (!IS_EOF(conf, *from)) {
  440. if (*from == q) {
  441. if (*(from + 1) == q) {
  442. from++;
  443. } else {
  444. break;
  445. }
  446. }
  447. buf->data[to++] = *(from++);
  448. }
  449. if (*from == q)
  450. from++;
  451. } else if (IS_ESC(conf, *from)) {
  452. from++;
  453. v = *(from++);
  454. if (IS_EOF(conf, v))
  455. break;
  456. else if (v == 'r')
  457. v = '\r';
  458. else if (v == 'n')
  459. v = '\n';
  460. else if (v == 'b')
  461. v = '\b';
  462. else if (v == 't')
  463. v = '\t';
  464. buf->data[to++] = v;
  465. } else if (IS_EOF(conf, *from))
  466. break;
  467. else if (*from == '$') {
  468. /* try to expand it */
  469. rrp = NULL;
  470. s = &(from[1]);
  471. if (*s == '{')
  472. q = '}';
  473. else if (*s == '(')
  474. q = ')';
  475. else
  476. q = 0;
  477. if (q)
  478. s++;
  479. cp = section;
  480. e = np = s;
  481. while (IS_ALPHA_NUMERIC(conf, *e))
  482. e++;
  483. if ((e[0] == ':') && (e[1] == ':')) {
  484. cp = np;
  485. rrp = e;
  486. rr = *e;
  487. *rrp = '\0';
  488. e += 2;
  489. np = e;
  490. while (IS_ALPHA_NUMERIC(conf, *e))
  491. e++;
  492. }
  493. r = *e;
  494. *e = '\0';
  495. rp = e;
  496. if (q) {
  497. if (r != q) {
  498. CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
  499. goto err;
  500. }
  501. e++;
  502. }
  503. /*-
  504. * So at this point we have
  505. * np which is the start of the name string which is
  506. * '\0' terminated.
  507. * cp which is the start of the section string which is
  508. * '\0' terminated.
  509. * e is the 'next point after'.
  510. * r and rr are the chars replaced by the '\0'
  511. * rp and rrp is where 'r' and 'rr' came from.
  512. */
  513. p = _CONF_get_string(conf, cp, np);
  514. if (rrp != NULL)
  515. *rrp = rr;
  516. *rp = r;
  517. if (p == NULL) {
  518. CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
  519. goto err;
  520. }
  521. if (!BUF_MEM_grow_clean(buf,
  522. (strlen(p) + buf->length - (e - from)))) {
  523. CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
  524. goto err;
  525. }
  526. while (*p)
  527. buf->data[to++] = *(p++);
  528. /*
  529. * Since we change the pointer 'from', we also have to change the
  530. * perceived length of the string it points at. /RL
  531. */
  532. len -= e - from;
  533. from = e;
  534. /*
  535. * In case there were no braces or parenthesis around the
  536. * variable reference, we have to put back the character that was
  537. * replaced with a '\0'. /RL
  538. */
  539. *rp = r;
  540. } else
  541. buf->data[to++] = *(from++);
  542. }
  543. buf->data[to] = '\0';
  544. OPENSSL_free(*pto);
  545. *pto = buf->data;
  546. OPENSSL_free(buf);
  547. return (1);
  548. err:
  549. BUF_MEM_free(buf);
  550. return (0);
  551. }
  552. static char *eat_ws(CONF *conf, char *p)
  553. {
  554. while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
  555. p++;
  556. return (p);
  557. }
  558. static char *eat_alpha_numeric(CONF *conf, char *p)
  559. {
  560. for (;;) {
  561. if (IS_ESC(conf, *p)) {
  562. p = scan_esc(conf, p);
  563. continue;
  564. }
  565. if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
  566. return (p);
  567. p++;
  568. }
  569. }
  570. static char *scan_quote(CONF *conf, char *p)
  571. {
  572. int q = *p;
  573. p++;
  574. while (!(IS_EOF(conf, *p)) && (*p != q)) {
  575. if (IS_ESC(conf, *p)) {
  576. p++;
  577. if (IS_EOF(conf, *p))
  578. return (p);
  579. }
  580. p++;
  581. }
  582. if (*p == q)
  583. p++;
  584. return (p);
  585. }
  586. static char *scan_dquote(CONF *conf, char *p)
  587. {
  588. int q = *p;
  589. p++;
  590. while (!(IS_EOF(conf, *p))) {
  591. if (*p == q) {
  592. if (*(p + 1) == q) {
  593. p++;
  594. } else {
  595. break;
  596. }
  597. }
  598. p++;
  599. }
  600. if (*p == q)
  601. p++;
  602. return (p);
  603. }
  604. static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
  605. {
  606. if (a->name)
  607. BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
  608. else
  609. BIO_printf(out, "[[%s]]\n", a->section);
  610. }
  611. IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
  612. static int def_dump(const CONF *conf, BIO *out)
  613. {
  614. lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
  615. return 1;
  616. }
  617. static int def_is_number(const CONF *conf, char c)
  618. {
  619. return IS_NUMBER(conf, c);
  620. }
  621. static int def_to_int(const CONF *conf, char c)
  622. {
  623. return c - '0';
  624. }