2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Part of the code in here was originally in conf.c, which is now removed */
13 #include "internal/cryptlib.h"
16 #include <openssl/conf.h>
17 #include <openssl/conf_api.h>
18 #include "conf_local.h"
20 static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf);
21 static void value_free_stack_doall(CONF_VALUE *a);
23 CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
27 if (conf == NULL || section == NULL)
30 vv.section = (char *)section;
31 return conf->data != NULL ? lh_CONF_VALUE_retrieve(conf->data, &vv) : NULL;
34 STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
39 v = _CONF_get_section(conf, section);
42 return ((STACK_OF(CONF_VALUE) *)v->value);
45 int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
48 STACK_OF(CONF_VALUE) *ts;
50 ts = (STACK_OF(CONF_VALUE) *)section->value;
52 value->section = section->section;
53 if (!sk_CONF_VALUE_push(ts, value))
56 v = lh_CONF_VALUE_insert(conf->data, value);
58 (void)sk_CONF_VALUE_delete_ptr(ts, v);
59 OPENSSL_free(v->name);
60 OPENSSL_free(v->value);
66 char *_CONF_get_string(const CONF *conf, const char *section,
75 return ossl_safe_getenv(name);
76 if (conf->data == NULL)
78 if (section != NULL) {
79 vv.name = (char *)name;
80 vv.section = (char *)section;
81 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
84 if (strcmp(section, "ENV") == 0) {
85 p = ossl_safe_getenv(name);
90 vv.section = "default";
91 vv.name = (char *)name;
92 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
98 static unsigned long conf_value_hash(const CONF_VALUE *v)
100 return (OPENSSL_LH_strhash(v->section) << 2) ^ OPENSSL_LH_strhash(v->name);
103 static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b)
107 if (a->section != b->section) {
108 i = strcmp(a->section, b->section);
113 if (a->name != NULL && b->name != NULL)
114 return strcmp(a->name, b->name);
115 if (a->name == b->name)
117 return (a->name == NULL) ? -1 : 1;
120 int _CONF_new_data(CONF *conf)
124 if (conf->data == NULL) {
125 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
126 if (conf->data == NULL)
132 typedef LHASH_OF(CONF_VALUE) LH_CONF_VALUE;
134 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, LH_CONF_VALUE);
136 void _CONF_free_data(CONF *conf)
141 OPENSSL_free(conf->includedir);
142 if (conf->data == NULL)
145 /* evil thing to make sure the 'OPENSSL_free()' works as expected */
146 lh_CONF_VALUE_set_down_load(conf->data, 0);
147 lh_CONF_VALUE_doall_LH_CONF_VALUE(conf->data, value_free_hash, conf->data);
150 * We now have only 'section' entries in the hash table. Due to problems
154 lh_CONF_VALUE_doall(conf->data, value_free_stack_doall);
155 lh_CONF_VALUE_free(conf->data);
158 static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf)
161 (void)lh_CONF_VALUE_delete(conf, a);
164 static void value_free_stack_doall(CONF_VALUE *a)
167 STACK_OF(CONF_VALUE) *sk;
173 sk = (STACK_OF(CONF_VALUE) *)a->value;
174 for (i = sk_CONF_VALUE_num(sk) - 1; i >= 0; i--) {
175 vv = sk_CONF_VALUE_value(sk, i);
176 OPENSSL_free(vv->value);
177 OPENSSL_free(vv->name);
180 sk_CONF_VALUE_free(sk);
181 OPENSSL_free(a->section);
185 CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
187 STACK_OF(CONF_VALUE) *sk = NULL;
189 CONF_VALUE *v = NULL, *vv;
191 if ((sk = sk_CONF_VALUE_new_null()) == NULL)
193 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
195 i = strlen(section) + 1;
196 if ((v->section = OPENSSL_malloc(i)) == NULL)
199 memcpy(v->section, section, i);
201 v->value = (char *)sk;
203 vv = lh_CONF_VALUE_insert(conf->data, v);
204 if (vv != NULL || lh_CONF_VALUE_error(conf->data) > 0)
209 sk_CONF_VALUE_free(sk);
211 OPENSSL_free(v->section);