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