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