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>
19 static CONF_METHOD *default_CONF_method = NULL;
21 /* Init a 'CONF' structure from an old LHASH */
23 void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
25 if (default_CONF_method == NULL)
26 default_CONF_method = NCONF_default();
28 default_CONF_method->init(conf);
33 * The following section contains the "CONF classic" functions, rewritten in
34 * terms of the new CONF interface.
37 int CONF_set_default_method(CONF_METHOD *meth)
39 default_CONF_method = meth;
43 LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
46 LHASH_OF(CONF_VALUE) *ltmp;
49 #ifdef OPENSSL_SYS_VMS
50 in = BIO_new_file(file, "r");
52 in = BIO_new_file(file, "rb");
55 CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
59 ltmp = CONF_load_bio(conf, in, eline);
65 #ifndef OPENSSL_NO_STDIO
66 LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
70 LHASH_OF(CONF_VALUE) *ltmp;
71 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
72 CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
75 ltmp = CONF_load_bio(conf, btmp, eline);
81 LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
87 CONF_set_nconf(&ctmp, conf);
89 ret = NCONF_load_bio(&ctmp, bp, eline);
95 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
102 CONF_set_nconf(&ctmp, conf);
103 return NCONF_get_section(&ctmp, section);
107 char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
111 return NCONF_get_string(NULL, group, name);
114 CONF_set_nconf(&ctmp, conf);
115 return NCONF_get_string(&ctmp, group, name);
119 long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
126 status = NCONF_get_number_e(NULL, group, name, &result);
129 CONF_set_nconf(&ctmp, conf);
130 status = NCONF_get_number_e(&ctmp, group, name, &result);
134 /* This function does not believe in errors... */
140 void CONF_free(LHASH_OF(CONF_VALUE) *conf)
143 CONF_set_nconf(&ctmp, conf);
144 NCONF_free_data(&ctmp);
147 #ifndef OPENSSL_NO_STDIO
148 int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
153 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
154 CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
157 ret = CONF_dump_bio(conf, btmp);
163 int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
166 CONF_set_nconf(&ctmp, conf);
167 return NCONF_dump_bio(&ctmp, out);
171 * The following section contains the "New CONF" functions. They are
172 * completely centralised around a new CONF structure that may contain
173 * basically anything, but at least a method pointer and a table of data.
174 * These functions are also written in terms of the bridge functions used by
175 * the "CONF classic" functions, for consistency.
178 CONF *NCONF_new(CONF_METHOD *meth)
183 meth = NCONF_default();
185 ret = meth->create(meth);
187 CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
194 void NCONF_free(CONF *conf)
198 conf->meth->destroy(conf);
201 void NCONF_free_data(CONF *conf)
205 conf->meth->destroy_data(conf);
208 int NCONF_load(CONF *conf, const char *file, long *eline)
211 CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
215 return conf->meth->load(conf, file, eline);
218 #ifndef OPENSSL_NO_STDIO
219 int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
223 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
224 CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
227 ret = NCONF_load_bio(conf, btmp, eline);
233 int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
236 CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
240 return conf->meth->load_bio(conf, bp, eline);
243 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
246 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
250 if (section == NULL) {
251 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
255 return _CONF_get_section_values(conf, section);
258 char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
260 char *s = _CONF_get_string(conf, group, name);
263 * Since we may get a value from an environment variable even if conf is
264 * NULL, let's check the value first
270 CONFerr(CONF_F_NCONF_GET_STRING,
271 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
274 CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
275 ERR_add_error_data(4, "group=", group, " name=", name);
279 int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
284 if (result == NULL) {
285 CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
289 str = NCONF_get_string(conf, group, name);
294 for (*result = 0; conf->meth->is_number(conf, *str);) {
295 *result = (*result) * 10 + conf->meth->to_int(conf, *str);
302 #ifndef OPENSSL_NO_STDIO
303 int NCONF_dump_fp(const CONF *conf, FILE *out)
307 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
308 CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
311 ret = NCONF_dump_bio(conf, btmp);
317 int NCONF_dump_bio(const CONF *conf, BIO *out)
320 CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
324 return conf->meth->dump(conf, out);
328 * These routines call the C malloc/free, to avoid intermixing with
329 * OpenSSL function pointers before the library is initialized.
331 OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
333 OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
336 memset(ret, 0, sizeof(*ret));
341 #ifndef OPENSSL_NO_STDIO
342 void OPENSSL_INIT_set_config_filename(OPENSSL_INIT_SETTINGS *settings,
343 const char *config_file)
345 free(settings->config_name);
346 settings->config_name = config_file == NULL ? NULL : strdup(config_file);
350 void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
352 free(settings->config_name);