Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/pal/tlsio_options.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdlib.h>
5
6 #include "azure_c_shared_utility/gballoc.h"
7 #include "azure_c_shared_utility/tlsio_options.h"
8 #include "azure_c_shared_utility/shared_util_options.h"
9 #include "azure_c_shared_utility/xlogging.h"
10 #include "azure_c_shared_utility/crt_abstractions.h"
11
12
13 // Initialize the TLSIO_OPTIONS struct
14 void tlsio_options_initialize(TLSIO_OPTIONS* options, int supported_options)
15 {
16 // Using static function rules, so 'options' is not checked for NULL
17 //
18 // The supported_options value does not need validation because undefined bits are
19 // ignored, while any valid missing bits result in an "option not supported" error
20 // that will show up in unit testing.
21 options->supported_options = supported_options;
22 options->trusted_certs = NULL;
23 options->x509_type = TLSIO_OPTIONS_x509_TYPE_UNSPECIFIED;
24 options->x509_cert = NULL;
25 options->x509_key = NULL;
26 }
27
28 static int set_and_validate_x509_type(TLSIO_OPTIONS* options, TLSIO_OPTIONS_x509_TYPE x509_type)
29 {
30 int result;
31 if ((options->supported_options & x509_type) == 0)
32 {
33 // This case also rejects the nonsensical TLSIO_OPTIONS_x509_TYPE_UNSPECIFIED
34 LogError("Unsupported x509 type: %d", x509_type);
35 result = MU_FAILURE;
36 }
37 else if (options->x509_type == TLSIO_OPTIONS_x509_TYPE_UNSPECIFIED)
38 {
39 // Initial type setting
40 options->x509_type = x509_type;
41 result = 0;
42 }
43 else if (options->x509_type != x509_type)
44 {
45 LogError("Supplied x509 type conflicts with previously set x509");
46 result = MU_FAILURE;
47 }
48 else
49 {
50 // The types match okay
51 result = 0;
52 }
53
54 return result;
55 }
56
57 void tlsio_options_release_resources(TLSIO_OPTIONS* options)
58 {
59 if (options != NULL)
60 {
61 free((void*)options->trusted_certs);
62 free((void*)options->x509_cert);
63 free((void*)options->x509_key);
64 options->trusted_certs = NULL;
65 options->x509_cert = NULL;
66 options->x509_key = NULL;
67 }
68 else
69 {
70 LogError("NULL options");
71 }
72 }
73
74 static bool is_supported_string_option(const char* name)
75 {
76 return
77 (strcmp(name, OPTION_TRUSTED_CERT) == 0) ||
78 (strcmp(name, OPTION_OPENSSL_CIPHER_SUITE) == 0) ||
79 (strcmp(name, OPTION_OPENSSL_ENGINE) == 0) ||
80 (strcmp(name, OPTION_OPENSSL_PRIVATE_KEY_TYPE) == 0) ||
81 (strcmp(name, SU_OPTION_X509_CERT) == 0) ||
82 (strcmp(name, SU_OPTION_X509_PRIVATE_KEY) == 0) ||
83 (strcmp(name, OPTION_X509_ECC_CERT) == 0) ||
84 (strcmp(name, OPTION_X509_ECC_KEY) == 0);
85 }
86
87 TLSIO_OPTIONS_RESULT tlsio_options_destroy_option(const char* name, const void* value)
88 {
89 TLSIO_OPTIONS_RESULT result;
90 if (name == NULL || value == NULL)
91 {
92 LogError("NULL parameter: name: %p, value: %p", name, value);
93 result = TLSIO_OPTIONS_RESULT_ERROR;
94 }
95 else if (is_supported_string_option(name))
96 {
97 free((void*)value);
98 result = TLSIO_OPTIONS_RESULT_SUCCESS;
99 }
100 else
101 {
102 result = TLSIO_OPTIONS_RESULT_NOT_HANDLED;
103 }
104 return result;
105 }
106
107 TLSIO_OPTIONS_RESULT tlsio_options_clone_option(const char* name, const void* value, void** out_value)
108 {
109 TLSIO_OPTIONS_RESULT result;
110
111 if (name == NULL || value == NULL || out_value == NULL)
112 {
113 LogError("NULL parameter: name: %p, value: %p, out_value: %p",
114 name, value, out_value);
115 result = TLSIO_OPTIONS_RESULT_ERROR;
116 }
117 else if (is_supported_string_option(name))
118 {
119 *out_value = NULL;
120 if (mallocAndStrcpy_s((char**)out_value, value) != 0)
121 {
122 LogError("unable to mallocAndStrcpy_s option value");
123 result = TLSIO_OPTIONS_RESULT_ERROR;
124 }
125 else
126 {
127 result = TLSIO_OPTIONS_RESULT_SUCCESS;
128 }
129 }
130 else
131 {
132 result = TLSIO_OPTIONS_RESULT_NOT_HANDLED;
133 }
134 return result;
135 }
136
137 TLSIO_OPTIONS_RESULT tlsio_options_set(TLSIO_OPTIONS* options,
138 const char* optionName, const void* value)
139 {
140 TLSIO_OPTIONS_RESULT result;
141 char* copied_value = NULL;
142
143 if (options == NULL || optionName == NULL || value == NULL)
144 {
145 LogError("NULL parameter: options: %p, optionName: %p, value: %p",
146 options, optionName, value);
147 result = TLSIO_OPTIONS_RESULT_ERROR;
148 }
149 else if (!is_supported_string_option(optionName))
150 {
151 result = TLSIO_OPTIONS_RESULT_NOT_HANDLED;
152 }
153 else if(mallocAndStrcpy_s(&copied_value, value) != 0)
154 {
155 LogError("unable to mallocAndStrcpy_s option value");
156 result = TLSIO_OPTIONS_RESULT_ERROR;
157 }
158 else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
159 {
160 if ((options->supported_options & TLSIO_OPTION_BIT_TRUSTED_CERTS) == 0)
161 {
162 LogError("Trusted certs option not supported");
163 result = TLSIO_OPTIONS_RESULT_ERROR;
164 }
165 else if (options->trusted_certs != NULL)
166 {
167 LogError("unable to set trusted cert option more than once");
168 result = TLSIO_OPTIONS_RESULT_ERROR;
169 }
170 else
171 {
172 options->trusted_certs = copied_value;
173 result = TLSIO_OPTIONS_RESULT_SUCCESS;
174 }
175 }
176 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
177 {
178 TLSIO_OPTIONS_x509_TYPE this_type = (strcmp(SU_OPTION_X509_CERT, optionName) == 0) ? TLSIO_OPTIONS_x509_TYPE_RSA : TLSIO_OPTIONS_x509_TYPE_ECC;
179 if (options->x509_cert != NULL)
180 {
181 LogError("unable to set x509 cert more than once");
182 result = TLSIO_OPTIONS_RESULT_ERROR;
183 }
184 else if (set_and_validate_x509_type(options, this_type) != 0)
185 {
186 // Error logged by helper
187 result = TLSIO_OPTIONS_RESULT_ERROR;
188 }
189 else
190 {
191 options->x509_cert = copied_value;
192 result = TLSIO_OPTIONS_RESULT_SUCCESS;
193 }
194 }
195 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
196 {
197 TLSIO_OPTIONS_x509_TYPE this_type = (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0) ? TLSIO_OPTIONS_x509_TYPE_RSA : TLSIO_OPTIONS_x509_TYPE_ECC;
198 if (options->x509_key != NULL)
199 {
200 LogError("unable to set x509 key more than once");
201 result = TLSIO_OPTIONS_RESULT_ERROR;
202 }
203 else if (set_and_validate_x509_type(options, this_type) != 0)
204 {
205 // Error logged by helper
206 result = TLSIO_OPTIONS_RESULT_ERROR;
207 }
208 else
209 {
210 options->x509_key = copied_value;
211 result = TLSIO_OPTIONS_RESULT_SUCCESS;
212 }
213 }
214 else
215 {
216 // This is logically impossible due to earlier tests, so just quiet the compiler
217 result = TLSIO_OPTIONS_RESULT_ERROR;
218 }
219
220 if (result != TLSIO_OPTIONS_RESULT_SUCCESS)
221 {
222 free(copied_value);
223 }
224
225 return result;
226 }
227
228 // A helper that works if the tlsio does not use any extra options
229 static void* local_clone_option(const char* name, const void* value)
230 {
231 void* result = NULL;
232 if (tlsio_options_clone_option(name, value, &result) != TLSIO_OPTIONS_RESULT_SUCCESS)
233 {
234 LogError("Unexpected local_clone_option failure");
235 }
236 return result;
237 }
238
239 // A helper that works if the tlsio does not use any extra options
240 void local_destroy_option(const char* name, const void* value)
241 {
242 if (tlsio_options_destroy_option(name, value) != TLSIO_OPTIONS_RESULT_SUCCESS)
243 {
244 LogError("Unexpected local_destroy_option failure");
245 }
246 }
247
248 OPTIONHANDLER_HANDLE tlsio_options_retrieve_options(TLSIO_OPTIONS* options, pfSetOption setOption)
249 {
250 return tlsio_options_retrieve_options_ex(options, local_clone_option, local_destroy_option, setOption);
251 }
252
253
254 OPTIONHANDLER_HANDLE tlsio_options_retrieve_options_ex(TLSIO_OPTIONS* options,
255 pfCloneOption cloneOption, pfDestroyOption destroyOption, pfSetOption setOption)
256 {
257 OPTIONHANDLER_HANDLE result;
258 if (options == NULL || cloneOption == NULL || destroyOption == NULL || setOption == NULL)
259 {
260 LogError("Null parameter in options: %p, cloneOption: %p, destroyOption: %p, setOption: %p",
261 options, cloneOption, destroyOption, setOption);
262 result = NULL;
263 }
264 else
265 {
266 result = OptionHandler_Create(cloneOption, destroyOption, setOption);
267 if (result == NULL)
268 {
269 LogError("OptionHandler_Create failed");
270 /*return as is*/
271 }
272 else if (
273 (options->trusted_certs != NULL) &&
274 (OptionHandler_AddOption(result, OPTION_TRUSTED_CERT, options->trusted_certs) != OPTIONHANDLER_OK)
275 )
276 {
277 LogError("unable to save TrustedCerts option");
278 OptionHandler_Destroy(result);
279 result = NULL;
280 }
281 else if (options->x509_type != TLSIO_OPTIONS_x509_TYPE_UNSPECIFIED)
282 {
283 const char* x509_cert_option;
284 const char* x509_key_option;
285 if (options->x509_type == TLSIO_OPTIONS_x509_TYPE_ECC)
286 {
287 x509_cert_option = OPTION_X509_ECC_CERT;
288 x509_key_option = OPTION_X509_ECC_KEY;
289 }
290 else
291 {
292 x509_cert_option = SU_OPTION_X509_CERT;
293 x509_key_option = SU_OPTION_X509_PRIVATE_KEY;
294 }
295 if (
296 (options->x509_cert != NULL) &&
297 (OptionHandler_AddOption(result, x509_cert_option, options->x509_cert) != OPTIONHANDLER_OK)
298 )
299 {
300 LogError("unable to save x509 cert option");
301 OptionHandler_Destroy(result);
302 result = NULL;
303 }
304 else if (
305 (options->x509_key != NULL) &&
306 (OptionHandler_AddOption(result, x509_key_option, options->x509_key) != OPTIONHANDLER_OK)
307 )
308 {
309 LogError("unable to save x509 key option");
310 OptionHandler_Destroy(result);
311 result = NULL;
312 }
313 }
314 }
315
316 return result;
317 }
318