]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/conf/conf_lib.c
Rename some BUF_xxx to OPENSSL_xxx
[thirdparty/openssl.git] / crypto / conf / conf_lib.c
CommitLineData
d86b6915 1/* conf_lib.c */
0f113f3e
MC
2/*
3 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
4 * 2000.
d86b6915
RL
5 */
6/* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
d86b6915
RL
15 *
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
19 * distribution.
20 *
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/)"
25 *
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.
30 *
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.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
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 * ====================================================================
53 *
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).
57 *
58 */
59
60#include <stdio.h>
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>
66
0f113f3e 67static CONF_METHOD *default_CONF_method = NULL;
d86b6915 68
b7a26e6d
DSH
69/* Init a 'CONF' structure from an old LHASH */
70
3c1d6bbc 71void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
0f113f3e
MC
72{
73 if (default_CONF_method == NULL)
74 default_CONF_method = NCONF_default();
b7a26e6d 75
0f113f3e
MC
76 default_CONF_method->init(conf);
77 conf->data = hash;
78}
b7a26e6d 79
0f113f3e
MC
80/*
81 * The following section contains the "CONF classic" functions, rewritten in
82 * terms of the new CONF interface.
83 */
d86b6915
RL
84
85int CONF_set_default_method(CONF_METHOD *meth)
0f113f3e
MC
86{
87 default_CONF_method = meth;
88 return 1;
89}
d86b6915 90
3c1d6bbc 91LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
0f113f3e
MC
92 long *eline)
93{
94 LHASH_OF(CONF_VALUE) *ltmp;
95 BIO *in = NULL;
d86b6915 96
bc36ee62 97#ifdef OPENSSL_SYS_VMS
0f113f3e 98 in = BIO_new_file(file, "r");
d86b6915 99#else
0f113f3e 100 in = BIO_new_file(file, "rb");
d86b6915 101#endif
0f113f3e
MC
102 if (in == NULL) {
103 CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
104 return NULL;
105 }
d86b6915 106
0f113f3e
MC
107 ltmp = CONF_load_bio(conf, in, eline);
108 BIO_free(in);
d86b6915 109
0f113f3e
MC
110 return ltmp;
111}
d86b6915 112
4b618848 113#ifndef OPENSSL_NO_STDIO
3c1d6bbc 114LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
0f113f3e
MC
115 long *eline)
116{
117 BIO *btmp;
118 LHASH_OF(CONF_VALUE) *ltmp;
75ebbd9a 119 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
0f113f3e
MC
120 CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
121 return NULL;
122 }
123 ltmp = CONF_load_bio(conf, btmp, eline);
124 BIO_free(btmp);
125 return ltmp;
126}
d86b6915
RL
127#endif
128
3c1d6bbc 129LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
0f113f3e
MC
130 long *eline)
131{
132 CONF ctmp;
133 int ret;
d86b6915 134
0f113f3e 135 CONF_set_nconf(&ctmp, conf);
d86b6915 136
0f113f3e
MC
137 ret = NCONF_load_bio(&ctmp, bp, eline);
138 if (ret)
139 return ctmp.data;
140 return NULL;
141}
d86b6915 142
3c1d6bbc 143STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
0f113f3e
MC
144 const char *section)
145{
146 if (conf == NULL) {
147 return NULL;
148 } else {
149 CONF ctmp;
150 CONF_set_nconf(&ctmp, conf);
151 return NCONF_get_section(&ctmp, section);
152 }
153}
154
155char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
156 const char *name)
157{
158 if (conf == NULL) {
159 return NCONF_get_string(NULL, group, name);
160 } else {
161 CONF ctmp;
162 CONF_set_nconf(&ctmp, conf);
163 return NCONF_get_string(&ctmp, group, name);
164 }
165}
166
167long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
168 const char *name)
169{
170 int status;
171 long result = 0;
172
173 if (conf == NULL) {
174 status = NCONF_get_number_e(NULL, group, name, &result);
175 } else {
176 CONF ctmp;
177 CONF_set_nconf(&ctmp, conf);
178 status = NCONF_get_number_e(&ctmp, group, name, &result);
179 }
180
181 if (status == 0) {
182 /* This function does not believe in errors... */
183 ERR_clear_error();
184 }
185 return result;
186}
d86b6915 187
3c1d6bbc 188void CONF_free(LHASH_OF(CONF_VALUE) *conf)
0f113f3e
MC
189{
190 CONF ctmp;
191 CONF_set_nconf(&ctmp, conf);
192 NCONF_free_data(&ctmp);
193}
d86b6915 194
4b618848 195#ifndef OPENSSL_NO_STDIO
3c1d6bbc 196int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
0f113f3e
MC
197{
198 BIO *btmp;
199 int ret;
200
75ebbd9a 201 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
0f113f3e
MC
202 CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
203 return 0;
204 }
205 ret = CONF_dump_bio(conf, btmp);
206 BIO_free(btmp);
207 return ret;
208}
d86b6915
RL
209#endif
210
3c1d6bbc 211int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
0f113f3e
MC
212{
213 CONF ctmp;
214 CONF_set_nconf(&ctmp, conf);
215 return NCONF_dump_bio(&ctmp, out);
216}
217
218/*
219 * The following section contains the "New CONF" functions. They are
220 * completely centralised around a new CONF structure that may contain
221 * basically anything, but at least a method pointer and a table of data.
222 * These functions are also written in terms of the bridge functions used by
223 * the "CONF classic" functions, for consistency.
224 */
d86b6915
RL
225
226CONF *NCONF_new(CONF_METHOD *meth)
0f113f3e
MC
227{
228 CONF *ret;
d86b6915 229
0f113f3e
MC
230 if (meth == NULL)
231 meth = NCONF_default();
d86b6915 232
0f113f3e
MC
233 ret = meth->create(meth);
234 if (ret == NULL) {
235 CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
236 return (NULL);
237 }
d86b6915 238
0f113f3e
MC
239 return ret;
240}
d86b6915
RL
241
242void NCONF_free(CONF *conf)
0f113f3e
MC
243{
244 if (conf == NULL)
245 return;
246 conf->meth->destroy(conf);
247}
d86b6915
RL
248
249void NCONF_free_data(CONF *conf)
0f113f3e
MC
250{
251 if (conf == NULL)
252 return;
253 conf->meth->destroy_data(conf);
254}
d86b6915
RL
255
256int NCONF_load(CONF *conf, const char *file, long *eline)
0f113f3e
MC
257{
258 if (conf == NULL) {
259 CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
260 return 0;
261 }
d86b6915 262
0f113f3e
MC
263 return conf->meth->load(conf, file, eline);
264}
d86b6915 265
4b618848 266#ifndef OPENSSL_NO_STDIO
0f113f3e
MC
267int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
268{
269 BIO *btmp;
270 int ret;
75ebbd9a 271 if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
0f113f3e
MC
272 CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
273 return 0;
274 }
275 ret = NCONF_load_bio(conf, btmp, eline);
276 BIO_free(btmp);
277 return ret;
278}
d86b6915
RL
279#endif
280
0f113f3e
MC
281int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
282{
283 if (conf == NULL) {
284 CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
285 return 0;
286 }
287
288 return conf->meth->load_bio(conf, bp, eline);
289}
290
291STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
292{
293 if (conf == NULL) {
294 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
295 return NULL;
296 }
297
298 if (section == NULL) {
299 CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
300 return NULL;
301 }
302
303 return _CONF_get_section_values(conf, section);
304}
305
306char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
307{
308 char *s = _CONF_get_string(conf, group, name);
309
310 /*
311 * Since we may get a value from an environment variable even if conf is
312 * NULL, let's check the value first
313 */
314 if (s)
315 return s;
316
317 if (conf == NULL) {
318 CONFerr(CONF_F_NCONF_GET_STRING,
319 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
320 return NULL;
321 }
322 CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
323 ERR_add_error_data(4, "group=", group, " name=", name);
324 return NULL;
325}
326
327int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
328 long *result)
329{
330 char *str;
331
332 if (result == NULL) {
333 CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
334 return 0;
335 }
336
337 str = NCONF_get_string(conf, group, name);
338
339 if (str == NULL)
340 return 0;
341
342 for (*result = 0; conf->meth->is_number(conf, *str);) {
343 *result = (*result) * 10 + conf->meth->to_int(conf, *str);
344 str++;
345 }
346
347 return 1;
348}
d86b6915 349
4b618848 350#ifndef OPENSSL_NO_STDIO
9dd5ae65 351int NCONF_dump_fp(const CONF *conf, FILE *out)
0f113f3e
MC
352{
353 BIO *btmp;
354 int ret;
75ebbd9a 355 if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
0f113f3e
MC
356 CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
357 return 0;
358 }
359 ret = NCONF_dump_bio(conf, btmp);
360 BIO_free(btmp);
361 return ret;
362}
d86b6915
RL
363#endif
364
9dd5ae65 365int NCONF_dump_bio(const CONF *conf, BIO *out)
0f113f3e
MC
366{
367 if (conf == NULL) {
368 CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
369 return 0;
370 }
d86b6915 371
0f113f3e
MC
372 return conf->meth->dump(conf, out);
373}