2
0

conf_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright 2000-2020 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. #include "e_os.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "internal/conf.h"
  13. #include "crypto/ctype.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/err.h>
  16. #include <openssl/conf.h>
  17. #include <openssl/conf_api.h>
  18. #include "conf_local.h"
  19. #include <openssl/lhash.h>
  20. static CONF_METHOD *default_CONF_method = NULL;
  21. /* Init a 'CONF' structure from an old LHASH */
  22. void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
  23. {
  24. if (default_CONF_method == NULL)
  25. default_CONF_method = NCONF_default();
  26. default_CONF_method->init(conf);
  27. conf->data = hash;
  28. }
  29. /*
  30. * The following section contains the "CONF classic" functions, rewritten in
  31. * terms of the new CONF interface.
  32. */
  33. int CONF_set_default_method(CONF_METHOD *meth)
  34. {
  35. default_CONF_method = meth;
  36. return 1;
  37. }
  38. LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
  39. long *eline)
  40. {
  41. LHASH_OF(CONF_VALUE) *ltmp;
  42. BIO *in = NULL;
  43. #ifdef OPENSSL_SYS_VMS
  44. in = BIO_new_file(file, "r");
  45. #else
  46. in = BIO_new_file(file, "rb");
  47. #endif
  48. if (in == NULL) {
  49. ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB);
  50. return NULL;
  51. }
  52. ltmp = CONF_load_bio(conf, in, eline);
  53. BIO_free(in);
  54. return ltmp;
  55. }
  56. #ifndef OPENSSL_NO_STDIO
  57. LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
  58. long *eline)
  59. {
  60. BIO *btmp;
  61. LHASH_OF(CONF_VALUE) *ltmp;
  62. if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  63. ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
  64. return NULL;
  65. }
  66. ltmp = CONF_load_bio(conf, btmp, eline);
  67. BIO_free(btmp);
  68. return ltmp;
  69. }
  70. #endif
  71. LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
  72. long *eline)
  73. {
  74. CONF ctmp;
  75. int ret;
  76. CONF_set_nconf(&ctmp, conf);
  77. ret = NCONF_load_bio(&ctmp, bp, eline);
  78. if (ret)
  79. return ctmp.data;
  80. return NULL;
  81. }
  82. STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
  83. const char *section)
  84. {
  85. if (conf == NULL) {
  86. return NULL;
  87. } else {
  88. CONF ctmp;
  89. CONF_set_nconf(&ctmp, conf);
  90. return NCONF_get_section(&ctmp, section);
  91. }
  92. }
  93. char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
  94. const char *name)
  95. {
  96. if (conf == NULL) {
  97. return NCONF_get_string(NULL, group, name);
  98. } else {
  99. CONF ctmp;
  100. CONF_set_nconf(&ctmp, conf);
  101. return NCONF_get_string(&ctmp, group, name);
  102. }
  103. }
  104. long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
  105. const char *name)
  106. {
  107. int status;
  108. long result = 0;
  109. ERR_set_mark();
  110. if (conf == NULL) {
  111. status = NCONF_get_number_e(NULL, group, name, &result);
  112. } else {
  113. CONF ctmp;
  114. CONF_set_nconf(&ctmp, conf);
  115. status = NCONF_get_number_e(&ctmp, group, name, &result);
  116. }
  117. ERR_pop_to_mark();
  118. return status == 0 ? 0L : result;
  119. }
  120. void CONF_free(LHASH_OF(CONF_VALUE) *conf)
  121. {
  122. CONF ctmp;
  123. CONF_set_nconf(&ctmp, conf);
  124. NCONF_free_data(&ctmp);
  125. }
  126. #ifndef OPENSSL_NO_STDIO
  127. int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
  128. {
  129. BIO *btmp;
  130. int ret;
  131. if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  132. ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
  133. return 0;
  134. }
  135. ret = CONF_dump_bio(conf, btmp);
  136. BIO_free(btmp);
  137. return ret;
  138. }
  139. #endif
  140. int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
  141. {
  142. CONF ctmp;
  143. CONF_set_nconf(&ctmp, conf);
  144. return NCONF_dump_bio(&ctmp, out);
  145. }
  146. /*
  147. * The following section contains the "New CONF" functions. They are
  148. * completely centralised around a new CONF structure that may contain
  149. * basically anything, but at least a method pointer and a table of data.
  150. * These functions are also written in terms of the bridge functions used by
  151. * the "CONF classic" functions, for consistency.
  152. */
  153. CONF *NCONF_new_ex(OSSL_LIB_CTX *libctx, CONF_METHOD *meth)
  154. {
  155. CONF *ret;
  156. if (meth == NULL)
  157. meth = NCONF_default();
  158. ret = meth->create(meth);
  159. if (ret == NULL) {
  160. ERR_raise(ERR_LIB_CONF, ERR_R_MALLOC_FAILURE);
  161. return NULL;
  162. }
  163. ret->libctx = libctx;
  164. return ret;
  165. }
  166. CONF *NCONF_new(CONF_METHOD *meth)
  167. {
  168. return NCONF_new_ex(NULL, meth);
  169. }
  170. void NCONF_free(CONF *conf)
  171. {
  172. if (conf == NULL)
  173. return;
  174. conf->meth->destroy(conf);
  175. }
  176. void NCONF_free_data(CONF *conf)
  177. {
  178. if (conf == NULL)
  179. return;
  180. conf->meth->destroy_data(conf);
  181. }
  182. OSSL_LIB_CTX *NCONF_get0_libctx(const CONF *conf)
  183. {
  184. return conf->libctx;
  185. }
  186. typedef STACK_OF(OPENSSL_CSTRING) SECTION_NAMES;
  187. IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, SECTION_NAMES);
  188. static void collect_section_name(const CONF_VALUE *v, SECTION_NAMES *names)
  189. {
  190. /* A section is a CONF_VALUE with name == NULL */
  191. if (v->name == NULL)
  192. sk_OPENSSL_CSTRING_push(names, v->section);
  193. }
  194. static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
  195. {
  196. return strcmp(*a, *b);
  197. }
  198. STACK_OF(OPENSSL_CSTRING) *NCONF_get_section_names(const CONF *cnf)
  199. {
  200. SECTION_NAMES *names;
  201. if ((names = sk_OPENSSL_CSTRING_new(section_name_cmp)) == NULL)
  202. return NULL;
  203. lh_CONF_VALUE_doall_SECTION_NAMES(cnf->data, collect_section_name, names);
  204. sk_OPENSSL_CSTRING_sort(names);
  205. return names;
  206. }
  207. int NCONF_load(CONF *conf, const char *file, long *eline)
  208. {
  209. if (conf == NULL) {
  210. ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
  211. return 0;
  212. }
  213. return conf->meth->load(conf, file, eline);
  214. }
  215. #ifndef OPENSSL_NO_STDIO
  216. int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
  217. {
  218. BIO *btmp;
  219. int ret;
  220. if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  221. ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
  222. return 0;
  223. }
  224. ret = NCONF_load_bio(conf, btmp, eline);
  225. BIO_free(btmp);
  226. return ret;
  227. }
  228. #endif
  229. int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
  230. {
  231. if (conf == NULL) {
  232. ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
  233. return 0;
  234. }
  235. return conf->meth->load_bio(conf, bp, eline);
  236. }
  237. STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
  238. {
  239. if (conf == NULL) {
  240. ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
  241. return NULL;
  242. }
  243. if (section == NULL) {
  244. ERR_raise(ERR_LIB_CONF, CONF_R_NO_SECTION);
  245. return NULL;
  246. }
  247. return _CONF_get_section_values(conf, section);
  248. }
  249. char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
  250. {
  251. char *s = _CONF_get_string(conf, group, name);
  252. /*
  253. * Since we may get a value from an environment variable even if conf is
  254. * NULL, let's check the value first
  255. */
  256. if (s)
  257. return s;
  258. if (conf == NULL) {
  259. ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
  260. return NULL;
  261. }
  262. ERR_raise_data(ERR_LIB_CONF, CONF_R_NO_VALUE,
  263. "group=%s name=%s", group, name);
  264. return NULL;
  265. }
  266. static int default_is_number(const CONF *conf, char c)
  267. {
  268. return ossl_isdigit(c);
  269. }
  270. static int default_to_int(const CONF *conf, char c)
  271. {
  272. return (int)(c - '0');
  273. }
  274. int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
  275. long *result)
  276. {
  277. char *str;
  278. long res;
  279. int (*is_number)(const CONF *, char) = &default_is_number;
  280. int (*to_int)(const CONF *, char) = &default_to_int;
  281. if (result == NULL) {
  282. ERR_raise(ERR_LIB_CONF, ERR_R_PASSED_NULL_PARAMETER);
  283. return 0;
  284. }
  285. str = NCONF_get_string(conf, group, name);
  286. if (str == NULL)
  287. return 0;
  288. if (conf != NULL) {
  289. if (conf->meth->is_number != NULL)
  290. is_number = conf->meth->is_number;
  291. if (conf->meth->to_int != NULL)
  292. to_int = conf->meth->to_int;
  293. }
  294. for (res = 0; is_number(conf, *str); str++) {
  295. const int d = to_int(conf, *str);
  296. if (res > (LONG_MAX - d) / 10L) {
  297. ERR_raise(ERR_LIB_CONF, CONF_R_NUMBER_TOO_LARGE);
  298. return 0;
  299. }
  300. res = res * 10 + d;
  301. }
  302. *result = res;
  303. return 1;
  304. }
  305. long _CONF_get_number(const CONF *conf, const char *section,
  306. const char *name)
  307. {
  308. int status;
  309. long result = 0;
  310. ERR_set_mark();
  311. status = NCONF_get_number_e(conf, section, name, &result);
  312. ERR_pop_to_mark();
  313. return status == 0 ? 0L : result;
  314. }
  315. #ifndef OPENSSL_NO_STDIO
  316. int NCONF_dump_fp(const CONF *conf, FILE *out)
  317. {
  318. BIO *btmp;
  319. int ret;
  320. if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  321. ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB);
  322. return 0;
  323. }
  324. ret = NCONF_dump_bio(conf, btmp);
  325. BIO_free(btmp);
  326. return ret;
  327. }
  328. #endif
  329. int NCONF_dump_bio(const CONF *conf, BIO *out)
  330. {
  331. if (conf == NULL) {
  332. ERR_raise(ERR_LIB_CONF, CONF_R_NO_CONF);
  333. return 0;
  334. }
  335. return conf->meth->dump(conf, out);
  336. }
  337. /*
  338. * These routines call the C malloc/free, to avoid intermixing with
  339. * OpenSSL function pointers before the library is initialized.
  340. */
  341. OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
  342. {
  343. OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
  344. if (ret == NULL)
  345. return NULL;
  346. memset(ret, 0, sizeof(*ret));
  347. ret->flags = DEFAULT_CONF_MFLAGS;
  348. return ret;
  349. }
  350. #ifndef OPENSSL_NO_STDIO
  351. int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
  352. const char *filename)
  353. {
  354. char *newfilename = NULL;
  355. if (filename != NULL) {
  356. newfilename = strdup(filename);
  357. if (newfilename == NULL)
  358. return 0;
  359. }
  360. free(settings->filename);
  361. settings->filename = newfilename;
  362. return 1;
  363. }
  364. void OPENSSL_INIT_set_config_file_flags(OPENSSL_INIT_SETTINGS *settings,
  365. unsigned long flags)
  366. {
  367. settings->flags = flags;
  368. }
  369. int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
  370. const char *appname)
  371. {
  372. char *newappname = NULL;
  373. if (appname != NULL) {
  374. newappname = strdup(appname);
  375. if (newappname == NULL)
  376. return 0;
  377. }
  378. free(settings->appname);
  379. settings->appname = newappname;
  380. return 1;
  381. }
  382. #endif
  383. void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
  384. {
  385. free(settings->filename);
  386. free(settings->appname);
  387. free(settings);
  388. }