xref: /openbsd-src/lib/libcrypto/conf/conf_lib.c (revision 904647e4cf107ebb262581dd88a8415cc907f2b5)
1*904647e4Stb /* $OpenBSD: conf_lib.c,v 1.24 2024/08/31 09:50:52 tb Exp $ */
2c109e398Sbeck /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3c109e398Sbeck  * project 2000.
4c109e398Sbeck  */
5c109e398Sbeck /* ====================================================================
6c109e398Sbeck  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7c109e398Sbeck  *
8c109e398Sbeck  * Redistribution and use in source and binary forms, with or without
9c109e398Sbeck  * modification, are permitted provided that the following conditions
10c109e398Sbeck  * are met:
11c109e398Sbeck  *
12c109e398Sbeck  * 1. Redistributions of source code must retain the above copyright
13c109e398Sbeck  *    notice, this list of conditions and the following disclaimer.
14c109e398Sbeck  *
15c109e398Sbeck  * 2. Redistributions in binary form must reproduce the above copyright
16c109e398Sbeck  *    notice, this list of conditions and the following disclaimer in
17c109e398Sbeck  *    the documentation and/or other materials provided with the
18c109e398Sbeck  *    distribution.
19c109e398Sbeck  *
20c109e398Sbeck  * 3. All advertising materials mentioning features or use of this
21c109e398Sbeck  *    software must display the following acknowledgment:
22c109e398Sbeck  *    "This product includes software developed by the OpenSSL Project
23c109e398Sbeck  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24c109e398Sbeck  *
25c109e398Sbeck  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26c109e398Sbeck  *    endorse or promote products derived from this software without
27c109e398Sbeck  *    prior written permission. For written permission, please contact
28c109e398Sbeck  *    licensing@OpenSSL.org.
29c109e398Sbeck  *
30c109e398Sbeck  * 5. Products derived from this software may not be called "OpenSSL"
31c109e398Sbeck  *    nor may "OpenSSL" appear in their names without prior written
32c109e398Sbeck  *    permission of the OpenSSL Project.
33c109e398Sbeck  *
34c109e398Sbeck  * 6. Redistributions of any form whatsoever must retain the following
35c109e398Sbeck  *    acknowledgment:
36c109e398Sbeck  *    "This product includes software developed by the OpenSSL Project
37c109e398Sbeck  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38c109e398Sbeck  *
39c109e398Sbeck  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40c109e398Sbeck  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41c109e398Sbeck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42c109e398Sbeck  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43c109e398Sbeck  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44c109e398Sbeck  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45c109e398Sbeck  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46c109e398Sbeck  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47c109e398Sbeck  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48c109e398Sbeck  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49c109e398Sbeck  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50c109e398Sbeck  * OF THE POSSIBILITY OF SUCH DAMAGE.
51c109e398Sbeck  * ====================================================================
52c109e398Sbeck  *
53c109e398Sbeck  * This product includes cryptographic software written by Eric Young
54c109e398Sbeck  * (eay@cryptsoft.com).  This product includes software written by Tim
55c109e398Sbeck  * Hudson (tjh@cryptsoft.com).
56c109e398Sbeck  *
57c109e398Sbeck  */
58c109e398Sbeck 
59c109e398Sbeck #include <stdio.h>
60c109e398Sbeck #include <openssl/crypto.h>
61c109e398Sbeck #include <openssl/err.h>
62c109e398Sbeck #include <openssl/conf.h>
63c109e398Sbeck #include <openssl/lhash.h>
64c109e398Sbeck 
65a681313fStb #include "conf_local.h"
66a681313fStb 
679cc23e66Stb static const CONF_METHOD *default_CONF_method = NULL;
68c109e398Sbeck 
69da347917Sbeck /* Init a 'CONF' structure from an old LHASH */
70da347917Sbeck 
7144486fcbSjsing void
7244486fcbSjsing CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
73da347917Sbeck {
74da347917Sbeck 	if (default_CONF_method == NULL)
75da347917Sbeck 		default_CONF_method = NCONF_default();
76da347917Sbeck 	default_CONF_method->init(conf);
77da347917Sbeck 	conf->data = hash;
78da347917Sbeck }
79da347917Sbeck 
8044486fcbSjsing CONF *
819cc23e66Stb NCONF_new(const CONF_METHOD *meth)
82c109e398Sbeck {
83c109e398Sbeck 	CONF *ret;
84c109e398Sbeck 
85c109e398Sbeck 	if (meth == NULL)
86c109e398Sbeck 		meth = NCONF_default();
87c109e398Sbeck 
88c109e398Sbeck 	ret = meth->create(meth);
8944486fcbSjsing 	if (ret == NULL) {
905067ae9fSbeck 		CONFerror(ERR_R_MALLOC_FAILURE);
91c109e398Sbeck 		return (NULL);
92c109e398Sbeck 	}
93c109e398Sbeck 
94c109e398Sbeck 	return ret;
95c109e398Sbeck }
96bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_new);
97c109e398Sbeck 
9844486fcbSjsing void
9944486fcbSjsing NCONF_free(CONF *conf)
100c109e398Sbeck {
101c109e398Sbeck 	if (conf == NULL)
102c109e398Sbeck 		return;
103c109e398Sbeck 	conf->meth->destroy(conf);
104c109e398Sbeck }
105bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_free);
106c109e398Sbeck 
10744486fcbSjsing int
10844486fcbSjsing NCONF_load(CONF *conf, const char *file, long *eline)
109c109e398Sbeck {
11044486fcbSjsing 	if (conf == NULL) {
1115067ae9fSbeck 		CONFerror(CONF_R_NO_CONF);
112c109e398Sbeck 		return 0;
113c109e398Sbeck 	}
114c109e398Sbeck 
115da347917Sbeck 	return conf->meth->load(conf, file, eline);
116c109e398Sbeck }
117bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_load);
118c109e398Sbeck 
11944486fcbSjsing int
12044486fcbSjsing NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
121c109e398Sbeck {
12244486fcbSjsing 	if (conf == NULL) {
1235067ae9fSbeck 		CONFerror(CONF_R_NO_CONF);
124c109e398Sbeck 		return 0;
125c109e398Sbeck 	}
126c109e398Sbeck 
127da347917Sbeck 	return conf->meth->load_bio(conf, bp, eline);
128c109e398Sbeck }
129bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_load_bio);
130c109e398Sbeck 
13144486fcbSjsing STACK_OF(CONF_VALUE) *
13244486fcbSjsing NCONF_get_section(const CONF *conf, const char *section)
133c109e398Sbeck {
13444486fcbSjsing 	if (conf == NULL) {
1355067ae9fSbeck 		CONFerror(CONF_R_NO_CONF);
136c109e398Sbeck 		return NULL;
137c109e398Sbeck 	}
138c109e398Sbeck 
13944486fcbSjsing 	if (section == NULL) {
1405067ae9fSbeck 		CONFerror(CONF_R_NO_SECTION);
141ce6fc090Sbeck 		return NULL;
142ce6fc090Sbeck 	}
143ce6fc090Sbeck 
144c109e398Sbeck 	return _CONF_get_section_values(conf, section);
145c109e398Sbeck }
146bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_get_section);
147c109e398Sbeck 
14844486fcbSjsing char *
14944486fcbSjsing NCONF_get_string(const CONF *conf, const char *group, const char *name)
150c109e398Sbeck {
151ce6fc090Sbeck 	char *s = _CONF_get_string(conf, group, name);
152ce6fc090Sbeck 
153ce6fc090Sbeck         /* Since we may get a value from an environment variable even
154ce6fc090Sbeck            if conf is NULL, let's check the value first */
15544486fcbSjsing 	if (s)
15644486fcbSjsing 		return s;
157ce6fc090Sbeck 
15844486fcbSjsing 	if (conf == NULL) {
1595067ae9fSbeck 		CONFerror(CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
160c109e398Sbeck 		return NULL;
161c109e398Sbeck 	}
1625067ae9fSbeck 	CONFerror(CONF_R_NO_VALUE);
163d86c414fSderaadt 	ERR_asprintf_error_data("group=%s name=%s",
164d86c414fSderaadt 	    group ? group : "", name);
165ce6fc090Sbeck 	return NULL;
166c109e398Sbeck }
167bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_get_string);
168c109e398Sbeck 
16944486fcbSjsing int
17044486fcbSjsing NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
171da347917Sbeck     long *result)
172c109e398Sbeck {
173da347917Sbeck 	char *str;
174da347917Sbeck 
17544486fcbSjsing 	if (result == NULL) {
1765067ae9fSbeck 		CONFerror(ERR_R_PASSED_NULL_PARAMETER);
177c109e398Sbeck 		return 0;
178c109e398Sbeck 	}
179c109e398Sbeck 
180da347917Sbeck 	str = NCONF_get_string(conf, group, name);
181da347917Sbeck 
182da347917Sbeck 	if (str == NULL)
183da347917Sbeck 		return 0;
184da347917Sbeck 
18544486fcbSjsing 	for (*result = 0; conf->meth->is_number(conf, *str); ) {
186da347917Sbeck 		*result = (*result) * 10 + conf->meth->to_int(conf, *str);
187da347917Sbeck 		str++;
188c109e398Sbeck 	}
189c109e398Sbeck 
190da347917Sbeck 	return 1;
191da347917Sbeck }
192bbdd77aaSbeck LCRYPTO_ALIAS(NCONF_get_number_e);
193