2
0

conf_lib.c 9.5 KB

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