xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/conf/conf.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/conf.h>
58 
59 #include <string.h>
60 #include <ctype.h>
61 
62 #include <openssl/bio.h>
63 #include <openssl/buf.h>
64 #include <openssl/err.h>
65 #include <openssl/lhash.h>
66 #include <openssl/mem.h>
67 
68 #include "conf_def.h"
69 #include "internal.h"
70 #include "../internal.h"
71 
72 
73 struct conf_section_st {
74   char *name;
75   // values contains non-owning pointers to the values in the section.
76   STACK_OF(CONF_VALUE) *values;
77 };
78 
79 static const char kDefaultSectionName[] = "default";
80 
conf_section_hash(const CONF_SECTION * s)81 static uint32_t conf_section_hash(const CONF_SECTION *s) {
82   return OPENSSL_strhash(s->name);
83 }
84 
conf_section_cmp(const CONF_SECTION * a,const CONF_SECTION * b)85 static int conf_section_cmp(const CONF_SECTION *a, const CONF_SECTION *b) {
86   return strcmp(a->name, b->name);
87 }
88 
conf_value_hash(const CONF_VALUE * v)89 static uint32_t conf_value_hash(const CONF_VALUE *v) {
90   const uint32_t section_hash = OPENSSL_strhash(v->section);
91   const uint32_t name_hash = OPENSSL_strhash(v->name);
92   return (section_hash << 2) ^ name_hash;
93 }
94 
conf_value_cmp(const CONF_VALUE * a,const CONF_VALUE * b)95 static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
96   int cmp = strcmp(a->section, b->section);
97   if (cmp != 0) {
98     return cmp;
99   }
100 
101   return strcmp(a->name, b->name);
102 }
103 
NCONF_new(void * method)104 CONF *NCONF_new(void *method) {
105   if (method != NULL) {
106     return NULL;
107   }
108 
109   CONF *conf = OPENSSL_malloc(sizeof(CONF));
110   if (conf == NULL) {
111     return NULL;
112   }
113 
114   conf->sections = lh_CONF_SECTION_new(conf_section_hash, conf_section_cmp);
115   conf->values = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
116   if (conf->sections == NULL || conf->values == NULL) {
117     NCONF_free(conf);
118     return NULL;
119   }
120 
121   return conf;
122 }
123 
CONF_VALUE_new(void)124 CONF_VALUE *CONF_VALUE_new(void) { return OPENSSL_zalloc(sizeof(CONF_VALUE)); }
125 
value_free(CONF_VALUE * value)126 static void value_free(CONF_VALUE *value) {
127   if (value == NULL) {
128     return;
129   }
130   OPENSSL_free(value->section);
131   OPENSSL_free(value->name);
132   OPENSSL_free(value->value);
133   OPENSSL_free(value);
134 }
135 
section_free(CONF_SECTION * section)136 static void section_free(CONF_SECTION *section) {
137   if (section == NULL) {
138     return;
139   }
140   OPENSSL_free(section->name);
141   sk_CONF_VALUE_free(section->values);
142   OPENSSL_free(section);
143 }
144 
value_free_arg(CONF_VALUE * value,void * arg)145 static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
146 
section_free_arg(CONF_SECTION * section,void * arg)147 static void section_free_arg(CONF_SECTION *section, void *arg) {
148   section_free(section);
149 }
150 
NCONF_free(CONF * conf)151 void NCONF_free(CONF *conf) {
152   if (conf == NULL) {
153     return;
154   }
155 
156   lh_CONF_SECTION_doall_arg(conf->sections, section_free_arg, NULL);
157   lh_CONF_SECTION_free(conf->sections);
158   lh_CONF_VALUE_doall_arg(conf->values, value_free_arg, NULL);
159   lh_CONF_VALUE_free(conf->values);
160   OPENSSL_free(conf);
161 }
162 
NCONF_new_section(const CONF * conf,const char * section)163 static CONF_SECTION *NCONF_new_section(const CONF *conf, const char *section) {
164   CONF_SECTION *s = OPENSSL_malloc(sizeof(CONF_SECTION));
165   if (!s) {
166     return NULL;
167   }
168   s->name = OPENSSL_strdup(section);
169   s->values = sk_CONF_VALUE_new_null();
170   if (s->name == NULL || s->values == NULL) {
171     goto err;
172   }
173 
174   CONF_SECTION *old_section;
175   if (!lh_CONF_SECTION_insert(conf->sections, &old_section, s)) {
176     goto err;
177   }
178   section_free(old_section);
179   return s;
180 
181 err:
182   section_free(s);
183   return NULL;
184 }
185 
str_copy(CONF * conf,char * section,char ** pto,char * from)186 static int str_copy(CONF *conf, char *section, char **pto, char *from) {
187   int q, to = 0, len = 0;
188   char v;
189   BUF_MEM *buf;
190 
191   buf = BUF_MEM_new();
192   if (buf == NULL) {
193     return 0;
194   }
195 
196   len = strlen(from) + 1;
197   if (!BUF_MEM_grow(buf, len)) {
198     goto err;
199   }
200 
201   for (;;) {
202     if (IS_QUOTE(conf, *from)) {
203       q = *from;
204       from++;
205       while (!IS_EOF(conf, *from) && (*from != q)) {
206         if (IS_ESC(conf, *from)) {
207           from++;
208           if (IS_EOF(conf, *from)) {
209             break;
210           }
211         }
212         buf->data[to++] = *(from++);
213       }
214       if (*from == q) {
215         from++;
216       }
217     } else if (IS_ESC(conf, *from)) {
218       from++;
219       v = *(from++);
220       if (IS_EOF(conf, v)) {
221         break;
222       } else if (v == 'r') {
223         v = '\r';
224       } else if (v == 'n') {
225         v = '\n';
226       } else if (v == 'b') {
227         v = '\b';
228       } else if (v == 't') {
229         v = '\t';
230       }
231       buf->data[to++] = v;
232     } else if (IS_EOF(conf, *from)) {
233       break;
234     } else if (*from == '$') {
235       // Historically, $foo would expand to a previously-parsed value. This
236       // feature has been removed as it was unused and is a DoS vector.
237       OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_NOT_SUPPORTED);
238       goto err;
239     } else {
240       buf->data[to++] = *(from++);
241     }
242   }
243 
244   buf->data[to] = '\0';
245   OPENSSL_free(*pto);
246   *pto = buf->data;
247   OPENSSL_free(buf);
248   return 1;
249 
250 err:
251   BUF_MEM_free(buf);
252   return 0;
253 }
254 
get_section(const CONF * conf,const char * section)255 static CONF_SECTION *get_section(const CONF *conf, const char *section) {
256   CONF_SECTION template;
257   OPENSSL_memset(&template, 0, sizeof(template));
258   template.name = (char *) section;
259   return lh_CONF_SECTION_retrieve(conf->sections, &template);
260 }
261 
STACK_OF(CONF_VALUE)262 const STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,
263                                               const char *section) {
264   const CONF_SECTION *section_obj = get_section(conf, section);
265   if (section_obj == NULL) {
266     return NULL;
267   }
268   return section_obj->values;
269 }
270 
NCONF_get_string(const CONF * conf,const char * section,const char * name)271 const char *NCONF_get_string(const CONF *conf, const char *section,
272                              const char *name) {
273   CONF_VALUE template, *value;
274 
275   if (section == NULL) {
276     section = kDefaultSectionName;
277   }
278 
279   OPENSSL_memset(&template, 0, sizeof(template));
280   template.section = (char *)section;
281   template.name = (char *)name;
282   value = lh_CONF_VALUE_retrieve(conf->values, &template);
283   if (value == NULL) {
284     return NULL;
285   }
286   return value->value;
287 }
288 
add_string(const CONF * conf,CONF_SECTION * section,CONF_VALUE * value)289 static int add_string(const CONF *conf, CONF_SECTION *section,
290                       CONF_VALUE *value) {
291   value->section = OPENSSL_strdup(section->name);
292   if (value->section == NULL) {
293     return 0;
294   }
295 
296   if (!sk_CONF_VALUE_push(section->values, value)) {
297     return 0;
298   }
299 
300   CONF_VALUE *old_value;
301   if (!lh_CONF_VALUE_insert(conf->values, &old_value, value)) {
302     // Remove |value| from |section->values|, so we do not leave a dangling
303     // pointer.
304     sk_CONF_VALUE_pop(section->values);
305     return 0;
306   }
307   if (old_value != NULL) {
308     (void)sk_CONF_VALUE_delete_ptr(section->values, old_value);
309     value_free(old_value);
310   }
311 
312   return 1;
313 }
314 
eat_ws(CONF * conf,char * p)315 static char *eat_ws(CONF *conf, char *p) {
316   while (IS_WS(conf, *p) && !IS_EOF(conf, *p)) {
317     p++;
318   }
319   return p;
320 }
321 
322 #define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
323 
eat_alpha_numeric(CONF * conf,char * p)324 static char *eat_alpha_numeric(CONF *conf, char *p) {
325   for (;;) {
326     if (IS_ESC(conf, *p)) {
327       p = scan_esc(conf, p);
328       continue;
329     }
330     if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) {
331       return p;
332     }
333     p++;
334   }
335 }
336 
scan_quote(CONF * conf,char * p)337 static char *scan_quote(CONF *conf, char *p) {
338   int q = *p;
339 
340   p++;
341   while (!IS_EOF(conf, *p) && *p != q) {
342     if (IS_ESC(conf, *p)) {
343       p++;
344       if (IS_EOF(conf, *p)) {
345         return p;
346       }
347     }
348     p++;
349   }
350   if (*p == q) {
351     p++;
352   }
353   return p;
354 }
355 
clear_comments(CONF * conf,char * p)356 static void clear_comments(CONF *conf, char *p) {
357   for (;;) {
358     if (!IS_WS(conf, *p)) {
359       break;
360     }
361     p++;
362   }
363 
364   for (;;) {
365     if (IS_COMMENT(conf, *p)) {
366       *p = '\0';
367       return;
368     }
369     if (IS_QUOTE(conf, *p)) {
370       p = scan_quote(conf, p);
371       continue;
372     }
373     if (IS_ESC(conf, *p)) {
374       p = scan_esc(conf, p);
375       continue;
376     }
377     if (IS_EOF(conf, *p)) {
378       return;
379     } else {
380       p++;
381     }
382   }
383 }
384 
NCONF_load_bio(CONF * conf,BIO * in,long * out_error_line)385 int NCONF_load_bio(CONF *conf, BIO *in, long *out_error_line) {
386   static const size_t CONFBUFSIZE = 512;
387   int bufnum = 0, i, ii;
388   BUF_MEM *buff = NULL;
389   char *s, *p, *end;
390   int again;
391   long eline = 0;
392   char btmp[DECIMAL_SIZE(eline) + 1];
393   CONF_VALUE *v = NULL;
394   CONF_SECTION *sv = NULL;
395   char *section = NULL, *buf;
396   char *start, *psection, *pname;
397 
398   if ((buff = BUF_MEM_new()) == NULL) {
399     OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
400     goto err;
401   }
402 
403   section = OPENSSL_strdup(kDefaultSectionName);
404   if (section == NULL) {
405     goto err;
406   }
407 
408   sv = NCONF_new_section(conf, section);
409   if (sv == NULL) {
410     OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
411     goto err;
412   }
413 
414   bufnum = 0;
415   again = 0;
416   for (;;) {
417     if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
418       OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB);
419       goto err;
420     }
421     p = &(buff->data[bufnum]);
422     *p = '\0';
423     BIO_gets(in, p, CONFBUFSIZE - 1);
424     p[CONFBUFSIZE - 1] = '\0';
425     ii = i = strlen(p);
426     if (i == 0 && !again) {
427       break;
428     }
429     again = 0;
430     while (i > 0) {
431       if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) {
432         break;
433       } else {
434         i--;
435       }
436     }
437     // we removed some trailing stuff so there is a new
438     // line on the end.
439     if (ii && i == ii) {
440       again = 1;  // long line
441     } else {
442       p[i] = '\0';
443       eline++;  // another input line
444     }
445 
446     // we now have a line with trailing \r\n removed
447 
448     // i is the number of bytes
449     bufnum += i;
450 
451     v = NULL;
452     // check for line continuation
453     if (bufnum >= 1) {
454       // If we have bytes and the last char '\\' and
455       // second last char is not '\\'
456       p = &(buff->data[bufnum - 1]);
457       if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
458         bufnum--;
459         again = 1;
460       }
461     }
462     if (again) {
463       continue;
464     }
465     bufnum = 0;
466     buf = buff->data;
467 
468     clear_comments(conf, buf);
469     s = eat_ws(conf, buf);
470     if (IS_EOF(conf, *s)) {
471       continue;  // blank line
472     }
473     if (*s == '[') {
474       char *ss;
475 
476       s++;
477       start = eat_ws(conf, s);
478       ss = start;
479     again:
480       end = eat_alpha_numeric(conf, ss);
481       p = eat_ws(conf, end);
482       if (*p != ']') {
483         if (*p != '\0' && ss != p) {
484           ss = p;
485           goto again;
486         }
487         OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
488         goto err;
489       }
490       *end = '\0';
491       if (!str_copy(conf, NULL, &section, start)) {
492         goto err;
493       }
494       if ((sv = get_section(conf, section)) == NULL) {
495         sv = NCONF_new_section(conf, section);
496       }
497       if (sv == NULL) {
498         OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
499         goto err;
500       }
501       continue;
502     } else {
503       pname = s;
504       psection = NULL;
505       end = eat_alpha_numeric(conf, s);
506       if ((end[0] == ':') && (end[1] == ':')) {
507         *end = '\0';
508         end += 2;
509         psection = pname;
510         pname = end;
511         end = eat_alpha_numeric(conf, end);
512       }
513       p = eat_ws(conf, end);
514       if (*p != '=') {
515         OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_EQUAL_SIGN);
516         goto err;
517       }
518       *end = '\0';
519       p++;
520       start = eat_ws(conf, p);
521       while (!IS_EOF(conf, *p)) {
522         p++;
523       }
524       p--;
525       while ((p != start) && (IS_WS(conf, *p))) {
526         p--;
527       }
528       p++;
529       *p = '\0';
530 
531       if (!(v = CONF_VALUE_new())) {
532         goto err;
533       }
534       if (psection == NULL) {
535         psection = section;
536       }
537       v->name = OPENSSL_strdup(pname);
538       if (v->name == NULL) {
539         goto err;
540       }
541       if (!str_copy(conf, psection, &(v->value), start)) {
542         goto err;
543       }
544 
545       CONF_SECTION *tv;
546       if (strcmp(psection, section) != 0) {
547         if ((tv = get_section(conf, psection)) == NULL) {
548           tv = NCONF_new_section(conf, psection);
549         }
550         if (tv == NULL) {
551           OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
552           goto err;
553         }
554       } else {
555         tv = sv;
556       }
557       if (add_string(conf, tv, v) == 0) {
558         goto err;
559       }
560       v = NULL;
561     }
562   }
563   BUF_MEM_free(buff);
564   OPENSSL_free(section);
565   return 1;
566 
567 err:
568   BUF_MEM_free(buff);
569   OPENSSL_free(section);
570   if (out_error_line != NULL) {
571     *out_error_line = eline;
572   }
573   snprintf(btmp, sizeof btmp, "%ld", eline);
574   ERR_add_error_data(2, "line ", btmp);
575   value_free(v);
576   return 0;
577 }
578 
NCONF_load(CONF * conf,const char * filename,long * out_error_line)579 int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
580   BIO *in = BIO_new_file(filename, "rb");
581   int ret;
582 
583   if (in == NULL) {
584     OPENSSL_PUT_ERROR(CONF, ERR_R_SYS_LIB);
585     return 0;
586   }
587 
588   ret = NCONF_load_bio(conf, in, out_error_line);
589   BIO_free(in);
590 
591   return ret;
592 }
593 
CONF_parse_list(const char * list,char sep,int remove_whitespace,int (* list_cb)(const char * elem,size_t len,void * usr),void * arg)594 int CONF_parse_list(const char *list, char sep, int remove_whitespace,
595                     int (*list_cb)(const char *elem, size_t len, void *usr),
596                     void *arg) {
597   int ret;
598   const char *lstart, *tmpend, *p;
599 
600   if (list == NULL) {
601     OPENSSL_PUT_ERROR(CONF, CONF_R_LIST_CANNOT_BE_NULL);
602     return 0;
603   }
604 
605   lstart = list;
606   for (;;) {
607     if (remove_whitespace) {
608       while (*lstart && OPENSSL_isspace((unsigned char)*lstart)) {
609         lstart++;
610       }
611     }
612     p = strchr(lstart, sep);
613     if (p == lstart || !*lstart) {
614       ret = list_cb(NULL, 0, arg);
615     } else {
616       if (p) {
617         tmpend = p - 1;
618       } else {
619         tmpend = lstart + strlen(lstart) - 1;
620       }
621       if (remove_whitespace) {
622         while (OPENSSL_isspace((unsigned char)*tmpend)) {
623           tmpend--;
624         }
625       }
626       ret = list_cb(lstart, tmpend - lstart + 1, arg);
627     }
628     if (ret <= 0) {
629       return ret;
630     }
631     if (p == NULL) {
632       return 1;
633     }
634     lstart = p + 1;
635   }
636 }
637 
CONF_modules_load_file(const char * filename,const char * appname,unsigned long flags)638 int CONF_modules_load_file(const char *filename, const char *appname,
639                            unsigned long flags) {
640   return 1;
641 }
642 
CONF_modules_free(void)643 void CONF_modules_free(void) {}
644 
OPENSSL_config(const char * config_name)645 void OPENSSL_config(const char *config_name) {}
646 
OPENSSL_no_config(void)647 void OPENSSL_no_config(void) {}
648