]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/conf/conf_api.c
Fix safestack issues in conf.h
[thirdparty/openssl.git] / crypto / conf / conf_api.c
CommitLineData
62867571 1/*
454afd98 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d86b6915 3 *
2044d382 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d86b6915
RL
8 */
9
10/* Part of the code in here was originally in conf.c, which is now removed */
11
07016a8a 12#include "e_os.h"
5c39a55d 13#include "internal/cryptlib.h"
2c1f5ce4 14#include <stdlib.h>
0baed24c 15#include <string.h>
d86b6915
RL
16#include <openssl/conf.h>
17#include <openssl/conf_api.h>
18
2a056de8 19static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf);
5ce278a7 20static void value_free_stack_doall(CONF_VALUE *a);
97b17195 21
9dd5ae65 22CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
0f113f3e 23{
2b9bafe6 24 CONF_VALUE vv;
d86b6915 25
2b9bafe6 26 if (conf == NULL || section == NULL)
26a7d938 27 return NULL;
0f113f3e
MC
28 vv.name = NULL;
29 vv.section = (char *)section;
2b9bafe6 30 return lh_CONF_VALUE_retrieve(conf->data, &vv);
0f113f3e 31}
d86b6915 32
9dd5ae65 33STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
0f113f3e
MC
34 const char *section)
35{
36 CONF_VALUE *v;
d86b6915 37
0f113f3e 38 v = _CONF_get_section(conf, section);
2b9bafe6 39 if (v == NULL)
26a7d938 40 return NULL;
2b9bafe6 41 return ((STACK_OF(CONF_VALUE) *)v->value);
0f113f3e 42}
d86b6915
RL
43
44int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
0f113f3e
MC
45{
46 CONF_VALUE *v = NULL;
47 STACK_OF(CONF_VALUE) *ts;
48
49 ts = (STACK_OF(CONF_VALUE) *)section->value;
50
51 value->section = section->section;
2b9bafe6 52 if (!sk_CONF_VALUE_push(ts, value))
0f113f3e 53 return 0;
0f113f3e
MC
54
55 v = lh_CONF_VALUE_insert(conf->data, value);
56 if (v != NULL) {
57 (void)sk_CONF_VALUE_delete_ptr(ts, v);
58 OPENSSL_free(v->name);
59 OPENSSL_free(v->value);
60 OPENSSL_free(v);
61 }
62 return 1;
63}
64
65char *_CONF_get_string(const CONF *conf, const char *section,
66 const char *name)
67{
68 CONF_VALUE *v, vv;
69 char *p;
70
71 if (name == NULL)
26a7d938 72 return NULL;
2b9bafe6
RS
73 if (conf == NULL)
74 return ossl_safe_getenv(name);
75 if (section != NULL) {
0f113f3e 76 vv.name = (char *)name;
2b9bafe6 77 vv.section = (char *)section;
0f113f3e
MC
78 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
79 if (v != NULL)
26a7d938 80 return v->value;
2b9bafe6
RS
81 if (strcmp(section, "ENV") == 0) {
82 p = ossl_safe_getenv(name);
83 if (p != NULL)
84 return p;
85 }
86 }
87 vv.section = "default";
88 vv.name = (char *)name;
89 v = lh_CONF_VALUE_retrieve(conf->data, &vv);
90 if (v == NULL)
91 return NULL;
92 return v->value;
0f113f3e
MC
93}
94
3c1d6bbc 95static unsigned long conf_value_hash(const CONF_VALUE *v)
0f113f3e 96{
739a1eb1 97 return (OPENSSL_LH_strhash(v->section) << 2) ^ OPENSSL_LH_strhash(v->name);
0f113f3e
MC
98}
99
3c1d6bbc 100static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b)
0f113f3e
MC
101{
102 int i;
103
104 if (a->section != b->section) {
105 i = strcmp(a->section, b->section);
2b9bafe6 106 if (i != 0)
26a7d938 107 return i;
0f113f3e
MC
108 }
109
2b9bafe6
RS
110 if (a->name != NULL && b->name != NULL)
111 return strcmp(a->name, b->name);
112 if (a->name == b->name)
26a7d938 113 return 0;
2b9bafe6 114 return (a->name == NULL) ? -1 : 1;
0f113f3e
MC
115}
116
d86b6915 117int _CONF_new_data(CONF *conf)
0f113f3e 118{
2b9bafe6 119 if (conf == NULL)
0f113f3e 120 return 0;
62d0577e
DSH
121 if (conf->data == NULL) {
122 conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
123 if (conf->data == NULL)
0f113f3e 124 return 0;
62d0577e 125 }
0f113f3e
MC
126 return 1;
127}
d86b6915 128
2a056de8
DSH
129typedef LHASH_OF(CONF_VALUE) LH_CONF_VALUE;
130
131IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, LH_CONF_VALUE);
132
d86b6915 133void _CONF_free_data(CONF *conf)
0f113f3e
MC
134{
135 if (conf == NULL || conf->data == NULL)
136 return;
137
e6b5c341
DSH
138 /* evil thing to make sure the 'OPENSSL_free()' works as expected */
139 lh_CONF_VALUE_set_down_load(conf->data, 0);
2a056de8 140 lh_CONF_VALUE_doall_LH_CONF_VALUE(conf->data, value_free_hash, conf->data);
0f113f3e
MC
141
142 /*
143 * We now have only 'section' entries in the hash table. Due to problems
144 * with
145 */
146
63c75cd6 147 lh_CONF_VALUE_doall(conf->data, value_free_stack_doall);
0f113f3e
MC
148 lh_CONF_VALUE_free(conf->data);
149}
d86b6915 150
2a056de8 151static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf)
0f113f3e
MC
152{
153 if (a->name != NULL)
154 (void)lh_CONF_VALUE_delete(conf, a);
155}
d86b6915 156
5ce278a7 157static void value_free_stack_doall(CONF_VALUE *a)
0f113f3e
MC
158{
159 CONF_VALUE *vv;
160 STACK_OF(CONF_VALUE) *sk;
161 int i;
162
163 if (a->name != NULL)
164 return;
165
166 sk = (STACK_OF(CONF_VALUE) *)a->value;
167 for (i = sk_CONF_VALUE_num(sk) - 1; i >= 0; i--) {
168 vv = sk_CONF_VALUE_value(sk, i);
169 OPENSSL_free(vv->value);
170 OPENSSL_free(vv->name);
171 OPENSSL_free(vv);
172 }
efa7dd64 173 sk_CONF_VALUE_free(sk);
0f113f3e
MC
174 OPENSSL_free(a->section);
175 OPENSSL_free(a);
176}
d86b6915 177
9dd5ae65 178CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
0f113f3e
MC
179{
180 STACK_OF(CONF_VALUE) *sk = NULL;
efa7dd64 181 int i;
0f113f3e
MC
182 CONF_VALUE *v = NULL, *vv;
183
184 if ((sk = sk_CONF_VALUE_new_null()) == NULL)
185 goto err;
b4faea50 186 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
0f113f3e
MC
187 goto err;
188 i = strlen(section) + 1;
189 if ((v->section = OPENSSL_malloc(i)) == NULL)
190 goto err;
191
192 memcpy(v->section, section, i);
193 v->name = NULL;
194 v->value = (char *)sk;
195
196 vv = lh_CONF_VALUE_insert(conf->data, v);
aebd0e5c 197 if (vv != NULL || lh_CONF_VALUE_error(conf->data) > 0)
64d9844a 198 goto err;
efa7dd64
RS
199 return v;
200
0f113f3e 201 err:
efa7dd64 202 sk_CONF_VALUE_free(sk);
aebd0e5c
PK
203 if (v != NULL)
204 OPENSSL_free(v->section);
efa7dd64
RS
205 OPENSSL_free(v);
206 return NULL;
0f113f3e 207}