]> git.proxmox.com Git - efi-boot-shim.git/blob - Cryptlib/OpenSSL/crypto/conf/conf_lib.c
Merge upstream version 0.7
[efi-boot-shim.git] / Cryptlib / OpenSSL / crypto / conf / conf_lib.c
1 /* conf_lib.c */
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/conf_api.h>
64 #include <openssl/lhash.h>
65
66 const char CONF_version[]="CONF" OPENSSL_VERSION_PTEXT;
67
68 static CONF_METHOD *default_CONF_method=NULL;
69
70 /* Init a 'CONF' structure from an old LHASH */
71
72 void CONF_set_nconf(CONF *conf, LHASH *hash)
73 {
74 if (default_CONF_method == NULL)
75 default_CONF_method = NCONF_default();
76
77 default_CONF_method->init(conf);
78 conf->data = hash;
79 }
80
81 /* The following section contains the "CONF classic" functions,
82 rewritten in terms of the new CONF interface. */
83
84 int CONF_set_default_method(CONF_METHOD *meth)
85 {
86 default_CONF_method = meth;
87 return 1;
88 }
89
90 LHASH *CONF_load(LHASH *conf, const char *file, long *eline)
91 {
92 LHASH *ltmp;
93 BIO *in=NULL;
94
95 #ifndef OPENSSL_NO_STDIO
96 #ifdef OPENSSL_SYS_VMS
97 in=BIO_new_file(file, "r");
98 #else
99 in=BIO_new_file(file, "rb");
100 #endif
101 #endif
102 if (in == NULL)
103 {
104 CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
105 return NULL;
106 }
107
108 ltmp = CONF_load_bio(conf, in, eline);
109 BIO_free(in);
110
111 return ltmp;
112 }
113
114 #ifndef OPENSSL_NO_FP_API
115 LHASH *CONF_load_fp(LHASH *conf, FILE *fp,long *eline)
116 {
117 BIO *btmp;
118 LHASH *ltmp;
119 if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
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 }
127 #endif
128
129 LHASH *CONF_load_bio(LHASH *conf, BIO *bp,long *eline)
130 {
131 CONF ctmp;
132 int ret;
133
134 CONF_set_nconf(&ctmp, conf);
135
136 ret = NCONF_load_bio(&ctmp, bp, eline);
137 if (ret)
138 return ctmp.data;
139 return NULL;
140 }
141
142 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH *conf,const char *section)
143 {
144 if (conf == NULL)
145 {
146 return NULL;
147 }
148 else
149 {
150 CONF ctmp;
151 CONF_set_nconf(&ctmp, conf);
152 return NCONF_get_section(&ctmp, section);
153 }
154 }
155
156 char *CONF_get_string(LHASH *conf,const char *group,const char *name)
157 {
158 if (conf == NULL)
159 {
160 return NCONF_get_string(NULL, group, name);
161 }
162 else
163 {
164 CONF ctmp;
165 CONF_set_nconf(&ctmp, conf);
166 return NCONF_get_string(&ctmp, group, name);
167 }
168 }
169
170 long CONF_get_number(LHASH *conf,const char *group,const char *name)
171 {
172 int status;
173 long result = 0;
174
175 if (conf == NULL)
176 {
177 status = NCONF_get_number_e(NULL, group, name, &result);
178 }
179 else
180 {
181 CONF ctmp;
182 CONF_set_nconf(&ctmp, conf);
183 status = NCONF_get_number_e(&ctmp, group, name, &result);
184 }
185
186 if (status == 0)
187 {
188 /* This function does not believe in errors... */
189 ERR_clear_error();
190 }
191 return result;
192 }
193
194 void CONF_free(LHASH *conf)
195 {
196 CONF ctmp;
197 CONF_set_nconf(&ctmp, conf);
198 NCONF_free_data(&ctmp);
199 }
200
201 #ifndef OPENSSL_NO_FP_API
202 int CONF_dump_fp(LHASH *conf, FILE *out)
203 {
204 BIO *btmp;
205 int ret;
206
207 if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
208 CONFerr(CONF_F_CONF_DUMP_FP,ERR_R_BUF_LIB);
209 return 0;
210 }
211 ret = CONF_dump_bio(conf, btmp);
212 BIO_free(btmp);
213 return ret;
214 }
215 #endif
216
217 int CONF_dump_bio(LHASH *conf, BIO *out)
218 {
219 CONF ctmp;
220 CONF_set_nconf(&ctmp, conf);
221 return NCONF_dump_bio(&ctmp, out);
222 }
223
224 /* The following section contains the "New CONF" functions. They are
225 completely centralised around a new CONF structure that may contain
226 basically anything, but at least a method pointer and a table of data.
227 These functions are also written in terms of the bridge functions used
228 by the "CONF classic" functions, for consistency. */
229
230 CONF *NCONF_new(CONF_METHOD *meth)
231 {
232 CONF *ret;
233
234 if (meth == NULL)
235 meth = NCONF_default();
236
237 ret = meth->create(meth);
238 if (ret == NULL)
239 {
240 CONFerr(CONF_F_NCONF_NEW,ERR_R_MALLOC_FAILURE);
241 return(NULL);
242 }
243
244 return ret;
245 }
246
247 void NCONF_free(CONF *conf)
248 {
249 if (conf == NULL)
250 return;
251 conf->meth->destroy(conf);
252 }
253
254 void NCONF_free_data(CONF *conf)
255 {
256 if (conf == NULL)
257 return;
258 conf->meth->destroy_data(conf);
259 }
260
261 int NCONF_load(CONF *conf, const char *file, long *eline)
262 {
263 if (conf == NULL)
264 {
265 CONFerr(CONF_F_NCONF_LOAD,CONF_R_NO_CONF);
266 return 0;
267 }
268
269 return conf->meth->load(conf, file, eline);
270 }
271
272 #ifndef OPENSSL_NO_FP_API
273 int NCONF_load_fp(CONF *conf, FILE *fp,long *eline)
274 {
275 BIO *btmp;
276 int ret;
277 if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE)))
278 {
279 CONFerr(CONF_F_NCONF_LOAD_FP,ERR_R_BUF_LIB);
280 return 0;
281 }
282 ret = NCONF_load_bio(conf, btmp, eline);
283 BIO_free(btmp);
284 return ret;
285 }
286 #endif
287
288 int NCONF_load_bio(CONF *conf, BIO *bp,long *eline)
289 {
290 if (conf == NULL)
291 {
292 CONFerr(CONF_F_NCONF_LOAD_BIO,CONF_R_NO_CONF);
293 return 0;
294 }
295
296 return conf->meth->load_bio(conf, bp, eline);
297 }
298
299 STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,const char *section)
300 {
301 if (conf == NULL)
302 {
303 CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_CONF);
304 return NULL;
305 }
306
307 if (section == NULL)
308 {
309 CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_SECTION);
310 return NULL;
311 }
312
313 return _CONF_get_section_values(conf, section);
314 }
315
316 char *NCONF_get_string(const CONF *conf,const char *group,const char *name)
317 {
318 char *s = _CONF_get_string(conf, group, name);
319
320 /* Since we may get a value from an environment variable even
321 if conf is NULL, let's check the value first */
322 if (s) return s;
323
324 if (conf == NULL)
325 {
326 CONFerr(CONF_F_NCONF_GET_STRING,
327 CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
328 return NULL;
329 }
330 CONFerr(CONF_F_NCONF_GET_STRING,
331 CONF_R_NO_VALUE);
332 ERR_add_error_data(4,"group=",group," name=",name);
333 return NULL;
334 }
335
336 int NCONF_get_number_e(const CONF *conf,const char *group,const char *name,
337 long *result)
338 {
339 char *str;
340
341 if (result == NULL)
342 {
343 CONFerr(CONF_F_NCONF_GET_NUMBER_E,ERR_R_PASSED_NULL_PARAMETER);
344 return 0;
345 }
346
347 str = NCONF_get_string(conf,group,name);
348
349 if (str == NULL)
350 return 0;
351
352 for (*result = 0;conf->meth->is_number(conf, *str);)
353 {
354 *result = (*result)*10 + conf->meth->to_int(conf, *str);
355 str++;
356 }
357
358 return 1;
359 }
360
361 #ifndef OPENSSL_NO_FP_API
362 int NCONF_dump_fp(const CONF *conf, FILE *out)
363 {
364 BIO *btmp;
365 int ret;
366 if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
367 CONFerr(CONF_F_NCONF_DUMP_FP,ERR_R_BUF_LIB);
368 return 0;
369 }
370 ret = NCONF_dump_bio(conf, btmp);
371 BIO_free(btmp);
372 return ret;
373 }
374 #endif
375
376 int NCONF_dump_bio(const CONF *conf, BIO *out)
377 {
378 if (conf == NULL)
379 {
380 CONFerr(CONF_F_NCONF_DUMP_BIO,CONF_R_NO_CONF);
381 return 0;
382 }
383
384 return conf->meth->dump(conf, out);
385 }
386
387
388 /* This function should be avoided */
389 #if 0
390 long NCONF_get_number(CONF *conf,char *group,char *name)
391 {
392 int status;
393 long ret=0;
394
395 status = NCONF_get_number_e(conf, group, name, &ret);
396 if (status == 0)
397 {
398 /* This function does not believe in errors... */
399 ERR_get_error();
400 }
401 return ret;
402 }
403 #endif