2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <internal/conf.h>
13 #include <openssl/crypto.h>
14 #include <openssl/err.h>
15 #include <openssl/conf.h>
16 #include <openssl/conf_api.h>
17 #include <openssl/lhash.h>
20 static CONF_METHOD *default_CONF_method = NULL;
22 /* Init a 'CONF' structure from an old LHASH */
24 void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
26 if (default_CONF_method == NULL)
27 default_CONF_method = NCONF_default();
29 default_CONF_method->init(conf);
34 * The following section contains the "CONF classic" functions, rewritten in
35 * terms of the new CONF interface.
38 int CONF_set_default_method(CONF_METHOD *meth)
40 default_CONF_method = meth;
44 LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
47 LHASH_OF(CONF_VALUE) *ltmp;
50 #ifdef OPENSSL_SYS_VMS
51 in = BIO_new_file(file, "r");
53 in = BIO_new_file(file, "rb");
56 CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
60 ltmp = CONF_load_bio(conf, in, eline);
66 #ifndef OPENSSL_NO_STDIO
67 LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
71 LHASH_OF(CONF_VALUE) *ltmp;
72 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
73 CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
76 ltmp = CONF_load_bio(conf, btmp, eline);
82 LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
88 CONF_set_nconf(&ctmp, conf);
90 ret = NCONF_load_bio(&ctmp, bp, eline);
96 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
103 CONF_set_nconf(&ctmp, conf);
104 return NCONF_get_section(&ctmp, section);
108 char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
112 return NCONF_get_string(NULL, group, name);
115 CONF_set_nconf(&ctmp, conf);
116 return NCONF_get_string(&ctmp, group, name);
120 long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
127 status = NCONF_get_number_e(NULL, group, name, &result);
130 CONF_set_nconf(&ctmp, conf);
131 status = NCONF_get_number_e(&ctmp, group, name, &result);
135 /* This function does not believe in errors... */
141 void CONF_free(LHASH_OF(CONF_VALUE) *conf)
144 CONF_set_nconf(&ctmp, conf);
145 NCONF_free_data(&ctmp);
148 #ifndef OPENSSL_NO_STDIO
149 int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
154 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
155 CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
158 ret = CONF_dump_bio(conf, btmp);
164 int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
167 CONF_set_nconf(&ctmp, conf);
168 return NCONF_dump_bio(&ctmp, out);
172 * The following section contains the "New CONF" functions. They are
173 * completely centralised around a new CONF structure that may contain
174 * basically anything, but at least a method pointer and a table of data.
175 * These functions are also written in terms of the bridge functions used by
176 * the "CONF classic" functions, for consistency.
179 CONF *NCONF_new(CONF_METHOD *meth)
184 meth = NCONF_default();
186 ret = meth->create(meth);
188 CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
195 void NCONF_free(CONF *conf)
199 conf->meth->destroy(conf);
202 void NCONF_free_data(CONF *conf)
206 conf->meth->destroy_data(conf);
209 int NCONF_load(CONF *conf, const char *file, long *eline)
212 CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
216 return conf->meth->load(conf, file, eline);
219 #ifndef OPENSSL_NO_STDIO
220 int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
224 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
225 CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
228 ret = NCONF_load_bio(conf, btmp, eline);
234 int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
237 CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
241 return conf->meth->load_bio(conf, bp, eline);
244 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
247 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
251 if (section == NULL) {
252 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
256 return _CONF_get_section_values(conf, section);
259 char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
261 char *s = _CONF_get_string(conf, group, name);
264 * Since we may get a value from an environment variable even if conf is
265 * NULL, let's check the value first
271 CONFerr(CONF_F_NCONF_GET_STRING,
272 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
275 CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
276 ERR_add_error_data(4, "group=", group, " name=", name);
280 int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
285 if (result == NULL) {
286 CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
290 str = NCONF_get_string(conf, group, name);
295 for (*result = 0; conf->meth->is_number(conf, *str);) {
296 *result = (*result) * 10 + conf->meth->to_int(conf, *str);
303 #ifndef OPENSSL_NO_STDIO
304 int NCONF_dump_fp(const CONF *conf, FILE *out)
308 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
309 CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
312 ret = NCONF_dump_bio(conf, btmp);
318 int NCONF_dump_bio(const CONF *conf, BIO *out)
321 CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
325 return conf->meth->dump(conf, out);
329 * These routines call the C malloc/free, to avoid intermixing with
330 * OpenSSL function pointers before the library is initialized.
332 OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
334 OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
337 memset(ret, 0, sizeof(*ret));
342 #ifndef OPENSSL_NO_STDIO
343 int OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
344 const char *config_file)
346 char *new_config_file = NULL;
348 if (config_file != NULL) {
349 new_config_file = strdup(config_file);
350 if (new_config_file == NULL)
354 free(settings->config_name);
355 settings->config_name = new_config_file;
361 void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
363 free(settings->config_name);