3 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/crypto.h>
62 #include <openssl/err.h>
63 #include <openssl/conf.h>
64 #include <openssl/conf_api.h>
65 #include <openssl/lhash.h>
67 const char CONF_version[] = "CONF" OPENSSL_VERSION_PTEXT;
69 static CONF_METHOD *default_CONF_method = NULL;
71 /* Init a 'CONF' structure from an old LHASH */
73 void CONF_set_nconf(CONF *conf, LHASH *hash)
75 if (default_CONF_method == NULL)
76 default_CONF_method = NCONF_default();
78 default_CONF_method->init(conf);
83 * The following section contains the "CONF classic" functions, rewritten in
84 * terms of the new CONF interface.
87 int CONF_set_default_method(CONF_METHOD *meth)
89 default_CONF_method = meth;
93 LHASH *CONF_load(LHASH *conf, const char *file, long *eline)
98 #ifdef OPENSSL_SYS_VMS
99 in = BIO_new_file(file, "r");
101 in = BIO_new_file(file, "rb");
104 CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
108 ltmp = CONF_load_bio(conf, in, eline);
114 #ifndef OPENSSL_NO_FP_API
115 LHASH *CONF_load_fp(LHASH *conf, FILE *fp, long *eline)
119 if (!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
120 CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
123 ltmp = CONF_load_bio(conf, btmp, eline);
129 LHASH *CONF_load_bio(LHASH *conf, BIO *bp, long *eline)
134 CONF_set_nconf(&ctmp, conf);
136 ret = NCONF_load_bio(&ctmp, bp, eline);
142 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH *conf, const char *section)
148 CONF_set_nconf(&ctmp, conf);
149 return NCONF_get_section(&ctmp, section);
153 char *CONF_get_string(LHASH *conf, const char *group, const char *name)
156 return NCONF_get_string(NULL, group, name);
159 CONF_set_nconf(&ctmp, conf);
160 return NCONF_get_string(&ctmp, group, name);
164 long CONF_get_number(LHASH *conf, const char *group, const char *name)
170 status = NCONF_get_number_e(NULL, group, name, &result);
173 CONF_set_nconf(&ctmp, conf);
174 status = NCONF_get_number_e(&ctmp, group, name, &result);
178 /* This function does not believe in errors... */
184 void CONF_free(LHASH *conf)
187 CONF_set_nconf(&ctmp, conf);
188 NCONF_free_data(&ctmp);
191 #ifndef OPENSSL_NO_FP_API
192 int CONF_dump_fp(LHASH *conf, FILE *out)
197 if (!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
198 CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
201 ret = CONF_dump_bio(conf, btmp);
207 int CONF_dump_bio(LHASH *conf, BIO *out)
210 CONF_set_nconf(&ctmp, conf);
211 return NCONF_dump_bio(&ctmp, out);
215 * The following section contains the "New CONF" functions. They are
216 * completely centralised around a new CONF structure that may contain
217 * basically anything, but at least a method pointer and a table of data.
218 * These functions are also written in terms of the bridge functions used by
219 * the "CONF classic" functions, for consistency.
222 CONF *NCONF_new(CONF_METHOD *meth)
227 meth = NCONF_default();
229 ret = meth->create(meth);
231 CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
238 void NCONF_free(CONF *conf)
242 conf->meth->destroy(conf);
245 void NCONF_free_data(CONF *conf)
249 conf->meth->destroy_data(conf);
252 int NCONF_load(CONF *conf, const char *file, long *eline)
255 CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
259 return conf->meth->load(conf, file, eline);
262 #ifndef OPENSSL_NO_FP_API
263 int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
267 if (!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
268 CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
271 ret = NCONF_load_bio(conf, btmp, eline);
277 int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
280 CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
284 return conf->meth->load_bio(conf, bp, eline);
287 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
290 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
294 if (section == NULL) {
295 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
299 return _CONF_get_section_values(conf, section);
302 char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
304 char *s = _CONF_get_string(conf, group, name);
307 * Since we may get a value from an environment variable even if conf is
308 * NULL, let's check the value first
314 CONFerr(CONF_F_NCONF_GET_STRING,
315 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
318 CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
319 ERR_add_error_data(4, "group=", group, " name=", name);
323 int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
328 if (result == NULL) {
329 CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
333 str = NCONF_get_string(conf, group, name);
338 for (*result = 0; conf->meth->is_number(conf, *str);) {
339 *result = (*result) * 10 + conf->meth->to_int(conf, *str);
346 #ifndef OPENSSL_NO_FP_API
347 int NCONF_dump_fp(const CONF *conf, FILE *out)
351 if (!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
352 CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
355 ret = NCONF_dump_bio(conf, btmp);
361 int NCONF_dump_bio(const CONF *conf, BIO *out)
364 CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
368 return conf->meth->dump(conf, out);
371 /* This function should be avoided */
373 long NCONF_get_number(CONF *conf, char *group, char *name)
378 status = NCONF_get_number_e(conf, group, name, &ret);
380 /* This function does not believe in errors... */