Debian Codegraph @

crt/aws-c-cal/source/unix/openssl_platform_init.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/cal/cal.h>
7 #include <aws/common/allocator.h>
8 #include <aws/common/logging.h>
9 #include <aws/common/mutex.h>
10 #include <aws/common/thread.h>
11
12 #include <dlfcn.h>
13
14 #include <aws/cal/private/opensslcrypto_common.h>
15
16 /*
17 * OpenSSL 3 has a large amount of interface changes and many of the functions used
18 * throughout aws-c-cal have become deprecated.
19 * Lets disable deprecation warnings, so that we can atleast run CI, until we
20 * can move over to new functions.
21 */
22 #define OPENSSL_SUPPRESS_DEPRECATED
23 #include <openssl/crypto.h>
24
25 #if defined(OPENSSL_IS_AWSLC)
26 # include <openssl/service_indicator.h>
27 #endif
28
29 static struct openssl_hmac_ctx_table hmac_ctx_table;
30 static struct openssl_evp_md_ctx_table evp_md_ctx_table;
31
32 struct openssl_hmac_ctx_table *g_aws_openssl_hmac_ctx_table = NULL;
33 struct openssl_evp_md_ctx_table *g_aws_openssl_evp_md_ctx_table = NULL;
34
35 static struct aws_allocator *s_libcrypto_allocator = NULL;
36
37 /* weak refs to libcrypto functions to force them to at least try to link
38 * and avoid dead-stripping
39 */
40 #if defined(OPENSSL_IS_AWSLC) || defined(OPENSSL_IS_BORINGSSL)
41 /* TODO:the weak refs is not GUARANTEED to avoid linker to strip the symbol.
42 Build on musl with openssl 1.1.1w, those those was referenced, but still stripped from libcrypto during linking.
43 Logs was:
44 ```
45 / # gcc -static -Wl,--trace-symbol=HMAC_CTX_new,-v -o test test.c -I $HOME/opt/aws/include -L $HOME/opt/aws/lib
46 -laws-c-cal -laws-c-common /usr/lib/libcrypto.a -fno-lto collect2 version 11.2.1 20220219
47 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ld --hash-style=gnu -m
48 elf_x86_64 --as-needed -static -z now -o test /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/crt1.o
49 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/crti.o
50 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/crtbeginT.o -L/root/opt/aws/lib
51 -L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1
52 -L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib/../lib
53 -L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib
54 -L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib
55 -L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../.. --trace-symbol=HMAC_CTX_new -v /tmp/ccBcKMkh.o -laws-c-cal
56 -laws-c-common /usr/lib/libcrypto.a -lssp_nonshared --start-group -lgcc -lgcc_eh -lc --end-group
57 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/crtend.o
58 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/crtn.o
59
60 GNU ld (GNU Binutils) 2.38
61 /usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/ld:
62 /root/opt/aws/lib/libaws-c-cal.a(openssl_platform_init.c.o): reference to HMAC_CTX_new
63 ```
64 We don't really understand what strips the symbols, but aws-lc is a workaround.
65 And, --require-defined=HMAC_CTX_new is another workaround to force the linker to keep the symbol.
66 */
67 extern HMAC_CTX *HMAC_CTX_new(void) __attribute__((weak, used));
68 extern void HMAC_CTX_free(HMAC_CTX *) __attribute__((weak, used));
69 extern void HMAC_CTX_init(HMAC_CTX *) __attribute__((weak, used));
70 extern void HMAC_CTX_cleanup(HMAC_CTX *) __attribute__((weak, used));
71 extern int HMAC_Update(HMAC_CTX *, const unsigned char *, size_t) __attribute__((weak, used));
72 extern int HMAC_Final(HMAC_CTX *, unsigned char *, unsigned int *) __attribute__((weak, used));
73 extern int HMAC_Init_ex(HMAC_CTX *, const void *, size_t, const EVP_MD *, ENGINE *) __attribute__((weak, used));
74
75 static int s_hmac_init_ex_bssl(HMAC_CTX *ctx, const void *key, size_t key_len, const EVP_MD *md, ENGINE *impl) {
76 AWS_PRECONDITION(ctx);
77
78 int (*init_ex_pt)(HMAC_CTX *, const void *, size_t, const EVP_MD *, ENGINE *) = (int (*)(
79 HMAC_CTX *, const void *, size_t, const EVP_MD *, ENGINE *))g_aws_openssl_hmac_ctx_table->impl.init_ex_fn;
80
81 return init_ex_pt(ctx, key, key_len, md, impl);
82 }
83
84 #else
85 /* 1.1 */
86 extern HMAC_CTX *HMAC_CTX_new(void) __attribute__((weak, used));
87 extern void HMAC_CTX_free(HMAC_CTX *) __attribute__((weak, used));
88
89 /* 1.0.2 */
90 extern void HMAC_CTX_init(HMAC_CTX *) __attribute__((weak, used));
91 extern void HMAC_CTX_cleanup(HMAC_CTX *) __attribute__((weak, used));
92
93 /* common */
94 extern int HMAC_Update(HMAC_CTX *, const unsigned char *, size_t) __attribute__((weak, used));
95 extern int HMAC_Final(HMAC_CTX *, unsigned char *, unsigned int *) __attribute__((weak, used));
96 extern int HMAC_Init_ex(HMAC_CTX *, const void *, int, const EVP_MD *, ENGINE *) __attribute__((weak, used));
97
98 static int s_hmac_init_ex_openssl(HMAC_CTX *ctx, const void *key, size_t key_len, const EVP_MD *md, ENGINE *impl) {
99 AWS_PRECONDITION(ctx);
100 if (key_len > INT_MAX) {
101 return 0;
102 }
103
104 /*Note: unlike aws-lc and boringssl, openssl 1.1.1 and 1.0.2 take int as key
105 len arg. */
106 int (*init_ex_ptr)(HMAC_CTX *, const void *, int, const EVP_MD *, ENGINE *) =
107 (int (*)(HMAC_CTX *, const void *, int, const EVP_MD *, ENGINE *))g_aws_openssl_hmac_ctx_table->impl.init_ex_fn;
108
109 return init_ex_ptr(ctx, key, (int)key_len, md, impl);
110 }
111
112 #endif /* !OPENSSL_IS_AWSLC && !OPENSSL_IS_BORINGSSL*/
113
114 #if !defined(OPENSSL_IS_AWSLC)
115 /* libcrypto 1.1 stub for init */
116 static void s_hmac_ctx_init_noop(HMAC_CTX *ctx) {
117 (void)ctx;
118 }
119
120 /* libcrypto 1.1 stub for clean_up */
121 static void s_hmac_ctx_clean_up_noop(HMAC_CTX *ctx) {
122 (void)ctx;
123 }
124 #endif
125
126 #if defined(OPENSSL_IS_OPENSSL)
127 /* libcrypto 1.0 shim for new */
128 static HMAC_CTX *s_hmac_ctx_new(void) {
129 AWS_PRECONDITION(
130 g_aws_openssl_hmac_ctx_table->init_fn != s_hmac_ctx_init_noop &&
131 "libcrypto 1.0 init called on libcrypto 1.1 vtable");
132 HMAC_CTX *ctx = aws_mem_calloc(s_libcrypto_allocator, 1, 300);
133 AWS_FATAL_ASSERT(ctx && "Unable to allocate to HMAC_CTX");
134 g_aws_openssl_hmac_ctx_table->init_fn(ctx);
135 return ctx;
136 }
137
138 /* libcrypto 1.0 shim for free */
139 static void s_hmac_ctx_free(HMAC_CTX *ctx) {
140 AWS_PRECONDITION(ctx);
141 AWS_PRECONDITION(
142 g_aws_openssl_hmac_ctx_table->clean_up_fn != s_hmac_ctx_clean_up_noop &&
143 "libcrypto 1.0 clean_up called on libcrypto 1.1 vtable");
144 g_aws_openssl_hmac_ctx_table->clean_up_fn(ctx);
145 aws_mem_release(s_libcrypto_allocator, ctx);
146 }
147
148 #endif /* !OPENSSL_IS_AWSLC */
149
150 enum aws_libcrypto_version {
151 AWS_LIBCRYPTO_NONE = 0,
152 AWS_LIBCRYPTO_1_0_2,
153 AWS_LIBCRYPTO_1_1_1,
154 AWS_LIBCRYPTO_LC,
155 AWS_LIBCRYPTO_BORINGSSL
156 };
157
158 bool s_resolve_hmac_102(void *module) {
159 #if defined(OPENSSL_IS_OPENSSL)
160 hmac_ctx_init init_fn = (hmac_ctx_init)HMAC_CTX_init;
161 hmac_ctx_clean_up clean_up_fn = (hmac_ctx_clean_up)HMAC_CTX_cleanup;
162 hmac_update update_fn = (hmac_update)HMAC_Update;
163 hmac_final final_fn = (hmac_final)HMAC_Final;
164 hmac_init_ex init_ex_fn = (hmac_init_ex)HMAC_Init_ex;
165
166 /* were symbols bound by static linking? */
167 bool has_102_symbols = init_fn && clean_up_fn && update_fn && final_fn && init_ex_fn;
168 if (has_102_symbols) {
169 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref libcrypto 1.0.2 HMAC symbols");
170 } else {
171 if (!module) {
172 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded libcrypto 1.0.2 HMAC symbols found");
173 return false;
174 }
175 /* If symbols aren't already found, try to find the requested version */
176 *(void **)(&init_fn) = dlsym(module, "HMAC_CTX_init");
177 *(void **)(&clean_up_fn) = dlsym(module, "HMAC_CTX_cleanup");
178 *(void **)(&update_fn) = dlsym(module, "HMAC_Update");
179 *(void **)(&final_fn) = dlsym(module, "HMAC_Final");
180 *(void **)(&init_ex_fn) = dlsym(module, "HMAC_Init_ex");
181 if (init_fn) {
182 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic libcrypto 1.0.2 HMAC symbols");
183 }
184 }
185
186 if (init_fn) {
187 hmac_ctx_table.new_fn = (hmac_ctx_new)s_hmac_ctx_new;
188 hmac_ctx_table.free_fn = s_hmac_ctx_free;
189 hmac_ctx_table.init_fn = init_fn;
190 hmac_ctx_table.clean_up_fn = clean_up_fn;
191 hmac_ctx_table.update_fn = update_fn;
192 hmac_ctx_table.final_fn = final_fn;
193 hmac_ctx_table.init_ex_fn = init_ex_fn;
194 g_aws_openssl_hmac_ctx_table = &hmac_ctx_table;
195 return true;
196 }
197 #endif
198 return false;
199 }
200
201 bool s_resolve_hmac_111(void *module) {
202 #if defined(OPENSSL_IS_OPENSSL)
203 hmac_ctx_new new_fn = (hmac_ctx_new)HMAC_CTX_new;
204 hmac_ctx_free free_fn = (hmac_ctx_free)HMAC_CTX_free;
205 hmac_update update_fn = (hmac_update)HMAC_Update;
206 hmac_final final_fn = (hmac_final)HMAC_Final;
207 hmac_init_ex init_ex_fn = (hmac_init_ex)HMAC_Init_ex;
208
209 /* were symbols bound by static linking? */
210 bool has_111_symbols = new_fn && free_fn && update_fn && final_fn && init_ex_fn;
211
212 if (has_111_symbols) {
213 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref libcrypto 1.1.1 HMAC symbols");
214 } else {
215 if (!module) {
216 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded libcrypto 1.1.1 HMAC symbols found");
217 return false;
218 }
219 *(void **)(&new_fn) = dlsym(module, "HMAC_CTX_new");
220 *(void **)(&free_fn) = dlsym(module, "HMAC_CTX_free");
221 *(void **)(&update_fn) = dlsym(module, "HMAC_Update");
222 *(void **)(&final_fn) = dlsym(module, "HMAC_Final");
223 *(void **)(&init_ex_fn) = dlsym(module, "HMAC_Init_ex");
224 if (new_fn) {
225 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic libcrypto 1.1.1 HMAC symbols");
226 }
227 }
228
229 if (new_fn) {
230 hmac_ctx_table.new_fn = new_fn;
231 hmac_ctx_table.free_fn = free_fn;
232 hmac_ctx_table.init_fn = s_hmac_ctx_init_noop;
233 hmac_ctx_table.clean_up_fn = s_hmac_ctx_clean_up_noop;
234 hmac_ctx_table.update_fn = update_fn;
235 hmac_ctx_table.final_fn = final_fn;
236 hmac_ctx_table.init_ex_fn = s_hmac_init_ex_openssl;
237 hmac_ctx_table.impl.init_ex_fn = (crypto_generic_fn_ptr)init_ex_fn;
238 g_aws_openssl_hmac_ctx_table = &hmac_ctx_table;
239 return true;
240 }
241 #endif
242 return false;
243 }
244
245 bool s_resolve_hmac_lc(void *module) {
246 #if defined(OPENSSL_IS_AWSLC)
247 hmac_ctx_init init_fn = (hmac_ctx_init)HMAC_CTX_init;
248 hmac_ctx_clean_up clean_up_fn = (hmac_ctx_clean_up)HMAC_CTX_cleanup;
249 hmac_ctx_new new_fn = (hmac_ctx_new)HMAC_CTX_new;
250 hmac_ctx_free free_fn = (hmac_ctx_free)HMAC_CTX_free;
251 hmac_update update_fn = (hmac_update)HMAC_Update;
252 hmac_final final_fn = (hmac_final)HMAC_Final;
253 hmac_init_ex init_ex_fn = (hmac_init_ex)HMAC_Init_ex;
254
255 /* were symbols bound by static linking? */
256 bool has_awslc_symbols = new_fn && free_fn && update_fn && final_fn && init_fn && init_ex_fn;
257
258 /* If symbols aren't already found, try to find the requested version */
259 /* when built as a shared lib, and multiple versions of libcrypto are possibly
260 * available (e.g. brazil), select AWS-LC by default for consistency */
261 if (has_awslc_symbols) {
262 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref aws-lc HMAC symbols");
263 } else {
264 if (!module) {
265 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded aws-lc HMAC symbols found");
266 return false;
267 }
268 *(void **)(&new_fn) = dlsym(module, "HMAC_CTX_new");
269 *(void **)(&free_fn) = dlsym(module, "HMAC_CTX_free");
270 *(void **)(&update_fn) = dlsym(module, "HMAC_Update");
271 *(void **)(&final_fn) = dlsym(module, "HMAC_Final");
272 *(void **)(&init_ex_fn) = dlsym(module, "HMAC_Init_ex");
273 if (new_fn) {
274 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic aws-lc HMAC symbols");
275 }
276 }
277
278 if (new_fn) {
279 /* Fill out the vtable for the requested version */
280 hmac_ctx_table.new_fn = new_fn;
281 hmac_ctx_table.free_fn = free_fn;
282 hmac_ctx_table.init_fn = init_fn;
283 hmac_ctx_table.clean_up_fn = clean_up_fn;
284 hmac_ctx_table.update_fn = update_fn;
285 hmac_ctx_table.final_fn = final_fn;
286 hmac_ctx_table.init_ex_fn = s_hmac_init_ex_bssl;
287 hmac_ctx_table.impl.init_ex_fn = (crypto_generic_fn_ptr)init_ex_fn;
288 g_aws_openssl_hmac_ctx_table = &hmac_ctx_table;
289 return true;
290 }
291 #endif
292 return false;
293 }
294
295 bool s_resolve_hmac_boringssl(void *module) {
296 #if defined(OPENSSL_IS_BORINGSSL)
297 hmac_ctx_new new_fn = (hmac_ctx_new)HMAC_CTX_new;
298 hmac_ctx_free free_fn = (hmac_ctx_free)HMAC_CTX_free;
299 hmac_update update_fn = (hmac_update)HMAC_Update;
300 hmac_final final_fn = (hmac_final)HMAC_Final;
301 hmac_init_ex init_ex_fn = (hmac_init_ex)HMAC_Init_ex;
302
303 /* were symbols bound by static linking? */
304 bool has_bssl_symbols = new_fn && free_fn && update_fn && final_fn && init_ex_fn;
305
306 if (has_bssl_symbols) {
307 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref boringssl HMAC symbols");
308 } else {
309 if (!module) {
310 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded boringssl HMAC symbols found");
311 return false;
312 }
313 *(void **)(&new_fn) = dlsym(module, "HMAC_CTX_new");
314 *(void **)(&free_fn) = dlsym(module, "HMAC_CTX_free");
315 *(void **)(&update_fn) = dlsym(module, "HMAC_Update");
316 *(void **)(&final_fn) = dlsym(module, "HMAC_Final");
317 *(void **)(&init_ex_fn) = dlsym(module, "HMAC_Init_ex");
318 if (new_fn) {
319 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic boringssl HMAC symbols");
320 }
321 }
322
323 if (new_fn) {
324 hmac_ctx_table.new_fn = new_fn;
325 hmac_ctx_table.free_fn = free_fn;
326 hmac_ctx_table.init_fn = s_hmac_ctx_init_noop;
327 hmac_ctx_table.clean_up_fn = s_hmac_ctx_clean_up_noop;
328 hmac_ctx_table.update_fn = update_fn;
329 hmac_ctx_table.final_fn = final_fn;
330 hmac_ctx_table.init_ex_fn = s_hmac_init_ex_bssl;
331 hmac_ctx_table.impl.init_ex_fn = (crypto_generic_fn_ptr)init_ex_fn;
332 g_aws_openssl_hmac_ctx_table = &hmac_ctx_table;
333 return true;
334 }
335 #endif
336 return false;
337 }
338
339 static enum aws_libcrypto_version s_resolve_libcrypto_hmac(enum aws_libcrypto_version version, void *module) {
340 switch (version) {
341 case AWS_LIBCRYPTO_LC:
342 return s_resolve_hmac_lc(module) ? version : AWS_LIBCRYPTO_NONE;
343 case AWS_LIBCRYPTO_1_1_1:
344 return s_resolve_hmac_111(module) ? version : AWS_LIBCRYPTO_NONE;
345 case AWS_LIBCRYPTO_1_0_2:
346 return s_resolve_hmac_102(module) ? version : AWS_LIBCRYPTO_NONE;
347 case AWS_LIBCRYPTO_BORINGSSL:
348 return s_resolve_hmac_boringssl(module) ? version : AWS_LIBCRYPTO_NONE;
349 case AWS_LIBCRYPTO_NONE:
350 AWS_FATAL_ASSERT(!"Attempted to resolve invalid libcrypto HMAC API version AWS_LIBCRYPTO_NONE");
351 }
352
353 return AWS_LIBCRYPTO_NONE;
354 }
355
356 #if !defined(OPENSSL_IS_AWSLC)
357 /* EVP_MD_CTX API */
358 /* 1.0.2 NOTE: these are macros in 1.1.x, so we have to undef them to weak link */
359 # if defined(EVP_MD_CTX_create)
360 # pragma push_macro("EVP_MD_CTX_create")
361 # undef EVP_MD_CTX_create
362 # endif
363 extern EVP_MD_CTX *EVP_MD_CTX_create(void) __attribute__((weak, used));
364 static evp_md_ctx_new s_EVP_MD_CTX_create = EVP_MD_CTX_create;
365 # if defined(EVP_MD_CTX_create)
366 # pragma pop_macro("EVP_MD_CTX_create")
367 # endif
368
369 # if defined(EVP_MD_CTX_destroy)
370 # pragma push_macro("EVP_MD_CTX_destroy")
371 # undef EVP_MD_CTX_destroy
372 # endif
373 extern void EVP_MD_CTX_destroy(EVP_MD_CTX *) __attribute__((weak, used));
374 static evp_md_ctx_free s_EVP_MD_CTX_destroy = EVP_MD_CTX_destroy;
375 # if defined(EVP_MD_CTX_destroy)
376 # pragma pop_macro("EVP_MD_CTX_destroy")
377 # endif
378 #endif /* !OPENSSL_IS_AWSLC */
379
380 extern EVP_MD_CTX *EVP_MD_CTX_new(void) __attribute__((weak, used));
381 extern void EVP_MD_CTX_free(EVP_MD_CTX *) __attribute__((weak, used));
382 extern int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *) __attribute__((weak, used));
383 extern int EVP_DigestUpdate(EVP_MD_CTX *, const void *, size_t) __attribute__((weak, used));
384 extern int EVP_DigestFinal_ex(EVP_MD_CTX *, unsigned char *, unsigned int *) __attribute__((weak, used));
385
386 bool s_resolve_md_102(void *module) {
387 #if !defined(OPENSSL_IS_AWSLC)
388 evp_md_ctx_new md_create_fn = s_EVP_MD_CTX_create;
389 evp_md_ctx_free md_destroy_fn = s_EVP_MD_CTX_destroy;
390 evp_md_ctx_digest_init_ex md_init_ex_fn = EVP_DigestInit_ex;
391 evp_md_ctx_digest_update md_update_fn = EVP_DigestUpdate;
392 evp_md_ctx_digest_final_ex md_final_ex_fn = EVP_DigestFinal_ex;
393
394 bool has_102_symbols = md_create_fn && md_destroy_fn && md_init_ex_fn && md_update_fn && md_final_ex_fn;
395
396 if (has_102_symbols) {
397 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref libcrypto 1.0.2 EVP_MD symbols");
398 } else {
399 if (!module) {
400 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded libcrypto 1.0.2 EVP_MD symbols found");
401 return false;
402 }
403 *(void **)(&md_create_fn) = dlsym(module, "EVP_MD_CTX_create");
404 *(void **)(&md_destroy_fn) = dlsym(module, "EVP_MD_CTX_destroy");
405 *(void **)(&md_init_ex_fn) = dlsym(module, "EVP_DigestInit_ex");
406 *(void **)(&md_update_fn) = dlsym(module, "EVP_DigestUpdate");
407 *(void **)(&md_final_ex_fn) = dlsym(module, "EVP_DigestFinal_ex");
408 if (md_create_fn) {
409 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic libcrypto 1.0.2 EVP_MD symbols");
410 }
411 }
412
413 if (md_create_fn) {
414 evp_md_ctx_table.new_fn = md_create_fn;
415 evp_md_ctx_table.free_fn = md_destroy_fn;
416 evp_md_ctx_table.init_ex_fn = md_init_ex_fn;
417 evp_md_ctx_table.update_fn = md_update_fn;
418 evp_md_ctx_table.final_ex_fn = md_final_ex_fn;
419 g_aws_openssl_evp_md_ctx_table = &evp_md_ctx_table;
420 return true;
421 }
422 #endif
423 return false;
424 }
425
426 bool s_resolve_md_111(void *module) {
427 #if !defined(OPENSSL_IS_AWSLC)
428 evp_md_ctx_new md_new_fn = EVP_MD_CTX_new;
429 evp_md_ctx_free md_free_fn = EVP_MD_CTX_free;
430 evp_md_ctx_digest_init_ex md_init_ex_fn = EVP_DigestInit_ex;
431 evp_md_ctx_digest_update md_update_fn = EVP_DigestUpdate;
432 evp_md_ctx_digest_final_ex md_final_ex_fn = EVP_DigestFinal_ex;
433
434 bool has_111_symbols = md_new_fn && md_free_fn && md_init_ex_fn && md_update_fn && md_final_ex_fn;
435 if (has_111_symbols) {
436 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref libcrypto 1.1.1 EVP_MD symbols");
437 } else {
438 if (!module) {
439 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded libcrypto 1.1.1 EVP_MD symbols found");
440 return false;
441 }
442 *(void **)(&md_new_fn) = dlsym(module, "EVP_MD_CTX_new");
443 *(void **)(&md_free_fn) = dlsym(module, "EVP_MD_CTX_free");
444 *(void **)(&md_init_ex_fn) = dlsym(module, "EVP_DigestInit_ex");
445 *(void **)(&md_update_fn) = dlsym(module, "EVP_DigestUpdate");
446 *(void **)(&md_final_ex_fn) = dlsym(module, "EVP_DigestFinal_ex");
447 if (md_new_fn) {
448 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic libcrypto 1.1.1 EVP_MD symbols");
449 }
450 }
451
452 if (md_new_fn) {
453 evp_md_ctx_table.new_fn = md_new_fn;
454 evp_md_ctx_table.free_fn = md_free_fn;
455 evp_md_ctx_table.init_ex_fn = md_init_ex_fn;
456 evp_md_ctx_table.update_fn = md_update_fn;
457 evp_md_ctx_table.final_ex_fn = md_final_ex_fn;
458 g_aws_openssl_evp_md_ctx_table = &evp_md_ctx_table;
459 return true;
460 }
461 #endif
462 return false;
463 }
464
465 bool s_resolve_md_lc(void *module) {
466 #if defined(OPENSSL_IS_AWSLC)
467 evp_md_ctx_new md_new_fn = EVP_MD_CTX_new;
468 evp_md_ctx_new md_create_fn = EVP_MD_CTX_new;
469 evp_md_ctx_free md_free_fn = EVP_MD_CTX_free;
470 evp_md_ctx_free md_destroy_fn = EVP_MD_CTX_destroy;
471 evp_md_ctx_digest_init_ex md_init_ex_fn = EVP_DigestInit_ex;
472 evp_md_ctx_digest_update md_update_fn = EVP_DigestUpdate;
473 evp_md_ctx_digest_final_ex md_final_ex_fn = EVP_DigestFinal_ex;
474
475 bool has_awslc_symbols =
476 md_new_fn && md_create_fn && md_free_fn && md_destroy_fn && md_init_ex_fn && md_update_fn && md_final_ex_fn;
477
478 if (has_awslc_symbols) {
479 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found weak ref aws-lc libcrypto 1.1.1 EVP_MD symbols");
480 } else {
481 if (!module) {
482 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "No loaded aws-lc libcrypto 1.1.1 EVP_MD symbols found");
483 return false;
484 }
485 *(void **)(&md_new_fn) = dlsym(module, "EVP_MD_CTX_new");
486 *(void **)(&md_free_fn) = dlsym(module, "EVP_MD_CTX_free");
487 *(void **)(&md_init_ex_fn) = dlsym(module, "EVP_DigestInit_ex");
488 *(void **)(&md_update_fn) = dlsym(module, "EVP_DigestUpdate");
489 *(void **)(&md_final_ex_fn) = dlsym(module, "EVP_DigestFinal_ex");
490 if (md_new_fn) {
491 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "found dynamic aws-lc libcrypto 1.1.1 EVP_MD symbols");
492 }
493 }
494
495 if (md_new_fn) {
496 /* Add the found symbols to the vtable */
497 evp_md_ctx_table.new_fn = md_new_fn;
498 evp_md_ctx_table.free_fn = md_free_fn;
499 evp_md_ctx_table.init_ex_fn = md_init_ex_fn;
500 evp_md_ctx_table.update_fn = md_update_fn;
501 evp_md_ctx_table.final_ex_fn = md_final_ex_fn;
502 g_aws_openssl_evp_md_ctx_table = &evp_md_ctx_table;
503 return true;
504 }
505 #endif
506 return false;
507 }
508
509 bool s_resolve_md_boringssl(void *module) {
510 #if !defined(OPENSSL_IS_AWSLC)
511 return s_resolve_md_111(module);
512 #else
513 return false;
514 #endif
515 }
516
517 static enum aws_libcrypto_version s_resolve_libcrypto_md(enum aws_libcrypto_version version, void *module) {
518 switch (version) {
519 case AWS_LIBCRYPTO_LC:
520 return s_resolve_md_lc(module) ? version : AWS_LIBCRYPTO_NONE;
521 case AWS_LIBCRYPTO_1_1_1:
522 return s_resolve_md_111(module) ? version : AWS_LIBCRYPTO_NONE;
523 case AWS_LIBCRYPTO_1_0_2:
524 return s_resolve_md_102(module) ? version : AWS_LIBCRYPTO_NONE;
525 case AWS_LIBCRYPTO_BORINGSSL:
526 return s_resolve_md_boringssl(module) ? version : AWS_LIBCRYPTO_NONE;
527 case AWS_LIBCRYPTO_NONE:
528 AWS_FATAL_ASSERT(!"Attempted to resolve invalid libcrypto MD API version AWS_LIBCRYPTO_NONE");
529 }
530
531 return AWS_LIBCRYPTO_NONE;
532 }
533
534 static enum aws_libcrypto_version s_resolve_libcrypto_symbols(enum aws_libcrypto_version version, void *module) {
535 enum aws_libcrypto_version found_version = s_resolve_libcrypto_hmac(version, module);
536 if (found_version == AWS_LIBCRYPTO_NONE) {
537 return AWS_LIBCRYPTO_NONE;
538 }
539 found_version = s_resolve_libcrypto_md(found_version, module);
540 if (found_version == AWS_LIBCRYPTO_NONE) {
541 return AWS_LIBCRYPTO_NONE;
542 }
543 return found_version;
544 }
545
546 static enum aws_libcrypto_version s_libcrypto_version_at_compile_time(void) {
547 #ifdef OPENSSL_IS_OPENSSL
548 /*
549 * Currently, this only checks for 1.0.2 vs 1.1. As a future optimization, we can also add a branch for OpenSSL 3.0.
550 * OpenSSL 3.0 is compatible with OpenSSL 1.1, so it works currently.
551 */
552 if (OPENSSL_VERSION_NUMBER < 0x10100000L) {
553 return AWS_LIBCRYPTO_1_0_2;
554 } else {
555 return AWS_LIBCRYPTO_1_1_1;
556 }
557 #endif
558 /*
559 * Follow the default path instead of prioritizing the compiled version. This works, since we enforce that
560 * the compiled version and runtime version must be the same for BoringSSL and AWS-LC in
561 * `s_validate_libcrypto_linkage()`.
562 */
563 return AWS_LIBCRYPTO_NONE;
564 }
565
566 /* Given libcrypto version, return the filename of the .so */
567 static char *s_libcrypto_sharedlib_filename(enum aws_libcrypto_version version) {
568 switch (version) {
569 case AWS_LIBCRYPTO_1_0_2:
570 return "libcrypto.so.1.0.0";
571 case AWS_LIBCRYPTO_1_1_1:
572 return "libcrypto.so.1.1";
573 default:
574 return "libcrypto.so";
575 }
576 }
577
578 static bool s_load_libcrypto_sharedlib(enum aws_libcrypto_version version) {
579 const char *libcrypto_version = s_libcrypto_sharedlib_filename(version);
580
581 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "loading %s", libcrypto_version);
582 void *module = dlopen(libcrypto_version, RTLD_NOW);
583 if (module) {
584 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "resolving against %s", libcrypto_version);
585 enum aws_libcrypto_version result = s_resolve_libcrypto_symbols(version, module);
586 if (result == version) {
587 return true;
588 }
589 dlclose(module);
590 } else {
591 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "%s not found", libcrypto_version);
592 }
593
594 return false;
595 }
596
597 static enum aws_libcrypto_version s_resolve_libcrypto_sharedlib(void) {
598 /* First try to load the same version as the compiled libcrypto version */
599 const enum aws_libcrypto_version compiled_version = s_libcrypto_version_at_compile_time();
600 if (compiled_version != AWS_LIBCRYPTO_NONE) {
601 if (s_load_libcrypto_sharedlib(compiled_version)) {
602 return compiled_version;
603 }
604 }
605
606 /* If compiled_version is AWS_LIBCRYPTO_1_1_1, we have already tried to load it and failed. So, skip it here. */
607 if (compiled_version != AWS_LIBCRYPTO_1_1_1) {
608 if (s_load_libcrypto_sharedlib(AWS_LIBCRYPTO_1_1_1)) {
609 return AWS_LIBCRYPTO_1_1_1;
610 }
611 }
612
613 /* If compiled_version is AWS_LIBCRYPTO_1_0_2, we have already tried to load it and failed. So, skip it here. */
614 if (compiled_version != AWS_LIBCRYPTO_1_0_2) {
615 if (s_load_libcrypto_sharedlib(AWS_LIBCRYPTO_1_0_2)) {
616 return AWS_LIBCRYPTO_1_0_2;
617 }
618 }
619
620 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "loading libcrypto.so");
621 void *module = dlopen("libcrypto.so", RTLD_NOW);
622 if (module) {
623 unsigned long (*openssl_version_num)(void) = NULL;
624 *(void **)(&openssl_version_num) = dlsym(module, "OpenSSL_version_num");
625 if (openssl_version_num) {
626 unsigned long version = openssl_version_num();
627 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "libcrypto.so reported version is 0x%lx", version);
628 enum aws_libcrypto_version result = AWS_LIBCRYPTO_NONE;
629 if (version >= 0x10101000L) {
630 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "probing libcrypto.so for aws-lc symbols");
631 result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_LC, module);
632 if (result == AWS_LIBCRYPTO_NONE) {
633 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "probing libcrypto.so for 1.1.1 symbols");
634 result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_1_1_1, module);
635 }
636 } else if (version >= 0x10002000L) {
637 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "probing libcrypto.so for 1.0.2 symbols");
638 result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_1_0_2, module);
639 } else {
640 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "libcrypto.so reported version is unsupported");
641 }
642 if (result != AWS_LIBCRYPTO_NONE) {
643 return result;
644 }
645 } else {
646 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "Unable to determine version of libcrypto.so");
647 }
648 dlclose(module);
649 } else {
650 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "libcrypto.so not found");
651 }
652
653 return AWS_LIBCRYPTO_NONE;
654 }
655
656 static enum aws_libcrypto_version s_resolve_libcrypto(void) {
657 /* Try to auto-resolve against what's linked in/process space */
658 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "searching process and loaded modules");
659 enum aws_libcrypto_version result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_LC, NULL);
660 if (result == AWS_LIBCRYPTO_NONE) {
661 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "did not find aws-lc symbols linked");
662 result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_BORINGSSL, NULL);
663 }
664 if (result == AWS_LIBCRYPTO_NONE) {
665 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "did not find boringssl symbols linked");
666 result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_1_1_1, NULL);
667 }
668 if (result == AWS_LIBCRYPTO_NONE) {
669 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "did not find libcrypto 1.1.1 symbols linked");
670 result = s_resolve_libcrypto_symbols(AWS_LIBCRYPTO_1_0_2, NULL);
671 }
672
673 if (result == AWS_LIBCRYPTO_NONE) {
674 AWS_LOGF_DEBUG(AWS_LS_CAL_LIBCRYPTO_RESOLVE, "did not find libcrypto 1.0.2 symbols linked");
675 AWS_LOGF_DEBUG(
676 AWS_LS_CAL_LIBCRYPTO_RESOLVE,
677 "libcrypto symbols were not linked, searching for shared libraries and loading it");
678 result = s_resolve_libcrypto_sharedlib();
679 }
680
681 aws_validate_libcrypto_linkage();
682
683 return result;
684 }
685
686 /* Ignore warnings about how CRYPTO_get_locking_callback() always returns NULL on 1.1.1 */
687 #if !defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ * 10 > 410)
688 # pragma GCC diagnostic push
689 # pragma GCC diagnostic ignored "-Waddress"
690 #endif
691
692 /* Openssl 1.0.x requires special handling for its locking callbacks or else it's not thread safe */
693 #if !defined(OPENSSL_IS_AWSLC) && !defined(OPENSSL_IS_BORINGSSL)
694 static struct aws_mutex *s_libcrypto_locks = NULL;
695
696 static void s_locking_fn(int mode, int n, const char *unused0, int unused1) {
697 (void)unused0;
698 (void)unused1;
699
700 if (mode & CRYPTO_LOCK) {
701 aws_mutex_lock(&s_libcrypto_locks[n]);
702 } else {
703 aws_mutex_unlock(&s_libcrypto_locks[n]);
704 }
705 }
706
707 static unsigned long s_id_fn(void) {
708 return (unsigned long)aws_thread_current_thread_id();
709 }
710 #endif
711
712 void aws_cal_platform_init(struct aws_allocator *allocator) {
713 int version = s_resolve_libcrypto();
714 AWS_FATAL_ASSERT(version != AWS_LIBCRYPTO_NONE && "libcrypto could not be resolved");
715 AWS_FATAL_ASSERT(g_aws_openssl_evp_md_ctx_table);
716 AWS_FATAL_ASSERT(g_aws_openssl_hmac_ctx_table);
717
718 s_libcrypto_allocator = allocator;
719
720 #if !defined(OPENSSL_IS_AWSLC) && !defined(OPENSSL_IS_BORINGSSL)
721 /* Ensure that libcrypto 1.0.2 has working locking mechanisms. This code is macro'ed
722 * by libcrypto to be a no-op on 1.1.1 */
723 if (!CRYPTO_get_locking_callback()) {
724 /* on 1.1.1 this is a no-op */
725 CRYPTO_set_locking_callback(s_locking_fn);
726 if (CRYPTO_get_locking_callback() == s_locking_fn) {
727 s_libcrypto_locks = aws_mem_acquire(allocator, sizeof(struct aws_mutex) * CRYPTO_num_locks());
728 AWS_FATAL_ASSERT(s_libcrypto_locks);
729 size_t lock_count = (size_t)CRYPTO_num_locks();
730 for (size_t i = 0; i < lock_count; ++i) {
731 aws_mutex_init(&s_libcrypto_locks[i]);
732 }
733 }
734 }
735
736 if (!CRYPTO_get_id_callback()) {
737 CRYPTO_set_id_callback(s_id_fn);
738 }
739 #endif
740 }
741
742 /*
743 * Shutdown any resources before unloading CRT (ex. dlclose).
744 * This is currently aws-lc specific.
745 * Ex. why we need it:
746 * aws-lc uses thread local data extensively and registers thread atexit
747 * callback to clean it up.
748 * there are cases where crt gets dlopen'ed and then dlclose'ed within a larger program
749 * (ex. nodejs workers).
750 * with glibc, dlclose actually removes symbols from global space (musl does not).
751 * once crt is unloaded, thread atexit will no longer point at a valid aws-lc
752 * symbol and will happily crash when thread is closed.
753 * AWSLC_thread_local_shutdown was added by aws-lc to let teams remove thread
754 * local data manually before lib is unloaded.
755 * We can't call AWSLC_thread_local_shutdown in cal cleanup because it renders
756 * aws-lc unusable and there is no way to reinitilize aws-lc to a working state,
757 * i.e. everything that depends on aws-lc stops working after shutdown (ex. curl).
758 * So instead rely on GCC/Clang destructor extension to shutdown right before
759 * crt gets unloaded. Does not work on msvc, but thats a bridge we can cross at
760 * a later date (since we dont support aws-lc on win right now)
761 * TODO: do already init'ed check on lc similar to what we do for s2n, so we
762 * only shutdown when we initialized aws-lc. currently not possible because
763 * there is no way to check that aws-lc has been initialized.
764 */
765 void __attribute__((destructor)) s_cal_crypto_shutdown(void) {
766 #if defined(OPENSSL_IS_AWSLC)
767 AWSLC_thread_local_shutdown();
768 #endif
769 }
770
771 void aws_cal_platform_clean_up(void) {
772 #if !defined(OPENSSL_IS_AWSLC) && !defined(OPENSSL_IS_BORINGSSL)
773 if (CRYPTO_get_locking_callback() == s_locking_fn) {
774 CRYPTO_set_locking_callback(NULL);
775 size_t lock_count = (size_t)CRYPTO_num_locks();
776 for (size_t i = 0; i < lock_count; ++i) {
777 aws_mutex_clean_up(&s_libcrypto_locks[i]);
778 }
779 aws_mem_release(s_libcrypto_allocator, s_libcrypto_locks);
780 }
781
782 if (CRYPTO_get_id_callback() == s_id_fn) {
783 CRYPTO_set_id_callback(NULL);
784 }
785 #endif
786
787 #if defined(OPENSSL_IS_AWSLC)
788 AWSLC_thread_local_clear();
789 #endif
790
791 s_libcrypto_allocator = NULL;
792 }
793
794 void aws_cal_platform_thread_clean_up(void) {
795 #if defined(OPENSSL_IS_AWSLC)
796 AWSLC_thread_local_clear();
797 #endif
798 }
799
800 #if !defined(__GNUC__) || (__GNUC__ >= 4 && __GNUC_MINOR__ > 1)
801 # pragma GCC diagnostic pop
802 #endif