aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-cal/source/unix/openssl_platform_init.c
blob: e98168b4ac915ae5d6da22ebfd2952655d3d1508 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/allocator.h>
#include <aws/common/mutex.h>
#include <aws/common/thread.h>

#include <dlfcn.h>

#include <aws/cal/private/opensslcrypto_common.h>

#if defined(AWS_LIBCRYPTO_LOG_RESOLVE)
#    define FLOGF(...)                                                                                                 \
        do {                                                                                                           \
            fprintf(stderr, "AWS libcrypto resolve: ");                                                                \
            fprintf(stderr, __VA_ARGS__);                                                                              \
            fprintf(stderr, "\n");                                                                                     \
        } while (0)
#else
#    define FLOGF(...)
#endif

static struct openssl_hmac_ctx_table hmac_ctx_table;
static struct openssl_evp_md_ctx_table evp_md_ctx_table;

struct openssl_hmac_ctx_table *g_aws_openssl_hmac_ctx_table = NULL;
struct openssl_evp_md_ctx_table *g_aws_openssl_evp_md_ctx_table = NULL;

/* weak refs to libcrypto functions to force them to at least try to link
 * and avoid dead-stripping
 */
/* 1.1 */
extern HMAC_CTX *HMAC_CTX_new(void) __attribute__((weak)) __attribute__((used));
extern void HMAC_CTX_free(HMAC_CTX *) __attribute__((weak)) __attribute__((used));
extern int HMAC_CTX_reset(HMAC_CTX *) __attribute__((weak)) __attribute__((used));

/* 1.0.2 */
extern void HMAC_CTX_init(HMAC_CTX *) __attribute__((weak)) __attribute__((used));
extern void HMAC_CTX_cleanup(HMAC_CTX *) __attribute__((weak)) __attribute__((used));

/* common */
extern int HMAC_Update(HMAC_CTX *, const unsigned char *, size_t) __attribute__((weak)) __attribute__((used));
extern int HMAC_Final(HMAC_CTX *, unsigned char *, unsigned int *) __attribute__((weak)) __attribute__((used));
extern int HMAC_Init_ex(HMAC_CTX *, const void *, int, const EVP_MD *, ENGINE *) __attribute__((weak))
__attribute__((used));

/* libcrypto 1.1 stub for init */
static void s_hmac_ctx_init_noop(HMAC_CTX *ctx) {
    (void)ctx;
}

/* libcrypto 1.1 stub for clean_up */
static void s_hmac_ctx_clean_up_noop(HMAC_CTX *ctx) {
    (void)ctx;
}

/* libcrypto 1.0 shim for new */
static HMAC_CTX *s_hmac_ctx_new(void) {
    AWS_PRECONDITION(
        g_aws_openssl_hmac_ctx_table->init_fn != s_hmac_ctx_init_noop &&
        "libcrypto 1.0 init called on libcrypto 1.1 vtable");
    HMAC_CTX *ctx = aws_mem_calloc(aws_default_allocator(), 1, 300);
    AWS_FATAL_ASSERT(ctx && "Unable to allocate to HMAC_CTX");
    g_aws_openssl_hmac_ctx_table->init_fn(ctx);
    return ctx;
}

/* libcrypto 1.0 shim for free */
static void s_hmac_ctx_free(HMAC_CTX *ctx) {
    AWS_PRECONDITION(ctx);
    AWS_PRECONDITION(
        g_aws_openssl_hmac_ctx_table->clean_up_fn != s_hmac_ctx_clean_up_noop &&
        "libcrypto 1.0 clean_up called on libcrypto 1.1 vtable");
    g_aws_openssl_hmac_ctx_table->clean_up_fn(ctx);
    aws_mem_release(aws_default_allocator(), ctx);
}

/* libcrypto 1.0 shim for reset, matches HMAC_CTX_reset semantics */
static int s_hmac_ctx_reset(HMAC_CTX *ctx) {
    AWS_PRECONDITION(ctx);
    AWS_PRECONDITION(
        g_aws_openssl_hmac_ctx_table->init_fn != s_hmac_ctx_init_noop &&
        g_aws_openssl_hmac_ctx_table->clean_up_fn != s_hmac_ctx_clean_up_noop &&
        "libcrypto 1.0 reset called on libcrypto 1.1 vtable");
    g_aws_openssl_hmac_ctx_table->clean_up_fn(ctx);
    g_aws_openssl_hmac_ctx_table->init_fn(ctx);
    return 1;
}

static struct aws_mutex *s_libcrypto_locks = NULL;
static struct aws_allocator *s_libcrypto_allocator = NULL;

static void s_locking_fn(int mode, int n, const char *unused0, int unused1) {
    (void)unused0;
    (void)unused1;

    if (mode & CRYPTO_LOCK) {
        aws_mutex_lock(&s_libcrypto_locks[n]);
    } else {
        aws_mutex_unlock(&s_libcrypto_locks[n]);
    }
}

static unsigned long s_id_fn(void) {
    return (unsigned long)aws_thread_current_thread_id();
}

enum aws_libcrypto_version {
    AWS_LIBCRYPTO_NONE = 0,
    AWS_LIBCRYPTO_1_0_2,
    AWS_LIBCRYPTO_1_1_1,
    AWS_LIBCRYPTO_LC,
} s_libcrypto_version = AWS_LIBCRYPTO_NONE;

static int s_resolve_libcrypto_hmac(enum aws_libcrypto_version version, void *module) {
    hmac_ctx_init init_fn = 0;
    hmac_ctx_clean_up clean_up_fn = 0;
    hmac_ctx_new new_fn = HMAC_CTX_new;
    hmac_ctx_free free_fn = HMAC_CTX_free;
    hmac_ctx_reset reset_fn = HMAC_CTX_reset;
    hmac_ctx_update update_fn = HMAC_Update;
    hmac_ctx_final final_fn = HMAC_Final;
    hmac_ctx_init_ex init_ex_fn = HMAC_Init_ex;

    /* were symbols bound by static linking? */
    bool has_102_symbols = init_fn && clean_up_fn && update_fn && final_fn && init_ex_fn;
    bool has_111_symbols = new_fn && free_fn && update_fn && final_fn && init_ex_fn && reset_fn;

    if (version == AWS_LIBCRYPTO_NONE) {
        if (has_102_symbols) {
            version = AWS_LIBCRYPTO_1_0_2;
            FLOGF("auto-resolving libcrypto 1.0.2");
        } else if (has_111_symbols) {
            version = AWS_LIBCRYPTO_1_1_1;
            FLOGF("auto-resolving libcrypto 1.1.1");
        } else {
            /* not pre-linked, need to ask for a specific version */
            return AWS_LIBCRYPTO_NONE;
        }
    }

    if (has_102_symbols) {
        FLOGF("found static libcrypto 1.0.2 HMAC");
    }
    if (has_111_symbols) {
        FLOGF("found static libcrypto 1.1.1 HMAC");
    }

    /* If symbols aren't already found, try to find the requested version */
    /* when built as a shared lib, and multiple versions of openssl are possibly
     * available (e.g. brazil), select 1.0.2 by default for consistency */
    if (!has_102_symbols && version == AWS_LIBCRYPTO_1_0_2) {
        *(void **)(&init_fn) = dlsym(module, "HMAC_CTX_init");
        *(void **)(&clean_up_fn) = dlsym(module, "HMAC_CTX_cleanup");
        *(void **)(&update_fn) = dlsym(module, "HMAC_Update");
        *(void **)(&final_fn) = dlsym(module, "HMAC_Final");
        *(void **)(&init_ex_fn) = dlsym(module, "HMAC_Init_ex");
        if (init_fn) {
            FLOGF("found dynamic libcrypto 1.0.2 HMAC symbols");
        }
    }

    if (!has_111_symbols && version == AWS_LIBCRYPTO_1_1_1) {
        *(void **)(&new_fn) = dlsym(module, "HMAC_CTX_new");
        *(void **)(&reset_fn) = dlsym(module, "HMAC_CTX_reset");
        *(void **)(&free_fn) = dlsym(module, "HMAC_CTX_free");
        *(void **)(&update_fn) = dlsym(module, "HMAC_Update");
        *(void **)(&final_fn) = dlsym(module, "HMAC_Final");
        *(void **)(&init_ex_fn) = dlsym(module, "HMAC_Init_ex");
        if (new_fn) {
            FLOGF("found dynamic libcrypto 1.1.1 HMAC symbols");
        }
    }

    /* Fill out the vtable for the requested version */
    if (version == AWS_LIBCRYPTO_1_0_2 && init_fn) {
        hmac_ctx_table.new_fn = s_hmac_ctx_new;
        hmac_ctx_table.reset_fn = s_hmac_ctx_reset;
        hmac_ctx_table.free_fn = s_hmac_ctx_free;
        hmac_ctx_table.init_fn = init_fn;
        hmac_ctx_table.clean_up_fn = clean_up_fn;
    } else if (version == AWS_LIBCRYPTO_1_1_1 && new_fn) {
        hmac_ctx_table.new_fn = new_fn;
        hmac_ctx_table.reset_fn = reset_fn;
        hmac_ctx_table.free_fn = free_fn;
        hmac_ctx_table.init_fn = s_hmac_ctx_init_noop;
        hmac_ctx_table.clean_up_fn = s_hmac_ctx_clean_up_noop;
    }
    hmac_ctx_table.update_fn = update_fn;
    hmac_ctx_table.final_fn = final_fn;
    hmac_ctx_table.init_ex_fn = init_ex_fn;
    g_aws_openssl_hmac_ctx_table = &hmac_ctx_table;

    return version;
}

/* EVP_MD_CTX API */
/* 1.0.2 NOTE: these are macros in 1.1.x, so we have to undef them to weak link */
#if defined(EVP_MD_CTX_create)
#    pragma push_macro("EVP_MD_CTX_create")
#    undef EVP_MD_CTX_create
#endif
extern EVP_MD_CTX *EVP_MD_CTX_create(void) __attribute__((weak, used));
static evp_md_ctx_new s_EVP_MD_CTX_create = 0;
#if defined(EVP_MD_CTX_create)
#    pragma pop_macro("EVP_MD_CTX_create")
#endif

#if defined(EVP_MD_CTX_destroy)
#    pragma push_macro("EVP_MD_CTX_destroy")
#    undef EVP_MD_CTX_destroy
#endif
extern void EVP_MD_CTX_destroy(EVP_MD_CTX *) __attribute__((weak, used));
static evp_md_ctx_free s_EVP_MD_CTX_destroy = 0;
#if defined(EVP_MD_CTX_destroy)
#    pragma pop_macro("EVP_MD_CTX_destroy")
#endif

/* 1.1 */
extern EVP_MD_CTX *EVP_MD_CTX_new(void) __attribute__((weak, used));
extern void EVP_MD_CTX_free(EVP_MD_CTX *) __attribute__((weak, used));

/* common */
extern int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *) __attribute__((weak, used));
extern int EVP_DigestUpdate(EVP_MD_CTX *, const void *, size_t) __attribute__((weak, used));
extern int EVP_DigestFinal_ex(EVP_MD_CTX *, unsigned char *, unsigned int *) __attribute__((weak, used));

static int s_resolve_libcrypto_md(enum aws_libcrypto_version version, void *module) {
    /* OpenSSL changed the EVP api in 1.1 to use new/free verbs */
    evp_md_ctx_new md_new_fn = EVP_MD_CTX_new;
    evp_md_ctx_new md_create_fn = s_EVP_MD_CTX_create;
    evp_md_ctx_free md_free_fn = EVP_MD_CTX_free;
    evp_md_ctx_free md_destroy_fn = s_EVP_MD_CTX_destroy;
    evp_md_ctx_digest_init_ex md_init_ex_fn = EVP_DigestInit_ex;
    evp_md_ctx_digest_update md_update_fn = EVP_DigestUpdate;
    evp_md_ctx_digest_final_ex md_final_ex_fn = EVP_DigestFinal_ex;

    bool has_102_symbols = md_create_fn && md_destroy_fn && md_init_ex_fn && md_update_fn && md_final_ex_fn;
    bool has_111_symbols = md_new_fn && md_free_fn && md_init_ex_fn && md_update_fn && md_final_ex_fn;

    if (has_102_symbols) {
        FLOGF("found static libcrypto 1.0.2 EVP_MD");
    }
    if (has_111_symbols) {
        FLOGF("found static libcrypto 1.1.1 EVP_MD");
    }

    if (!has_102_symbols && version == AWS_LIBCRYPTO_1_0_2) {
        *(void **)(&md_create_fn) = dlsym(module, "EVP_MD_CTX_create");
        *(void **)(&md_destroy_fn) = dlsym(module, "EVP_MD_CTX_destroy");
        *(void **)(&md_init_ex_fn) = dlsym(module, "EVP_DigestInit_ex");
        *(void **)(&md_update_fn) = dlsym(module, "EVP_DigestUpdate");
        *(void **)(&md_final_ex_fn) = dlsym(module, "EVP_DigestFinal_ex");
        if (md_create_fn) {
            FLOGF("found dynamic libcrypto 1.0.2 EVP_MD symbols");
        }
    }

    if (!has_111_symbols && version == AWS_LIBCRYPTO_1_1_1) {
        *(void **)(&md_new_fn) = dlsym(module, "EVP_MD_CTX_new");
        *(void **)(&md_free_fn) = dlsym(module, "EVP_MD_CTX_free");
        *(void **)(&md_init_ex_fn) = dlsym(module, "EVP_DigestInit_ex");
        *(void **)(&md_update_fn) = dlsym(module, "EVP_DigestUpdate");
        *(void **)(&md_final_ex_fn) = dlsym(module, "EVP_DigestFinal_ex");
        if (md_new_fn) {
            FLOGF("found dynamic libcrypto 1.1.1 EVP_MD symbols");
        }
    }

    /* Add the found symbols to the vtable */
    if (version == AWS_LIBCRYPTO_1_0_2 && md_create_fn) {
        evp_md_ctx_table.new_fn = md_create_fn;
        evp_md_ctx_table.free_fn = md_destroy_fn;
        evp_md_ctx_table.init_ex_fn = md_init_ex_fn;
        evp_md_ctx_table.update_fn = md_update_fn;
        evp_md_ctx_table.final_ex_fn = md_final_ex_fn;
        g_aws_openssl_evp_md_ctx_table = &evp_md_ctx_table;
        return version;
    } else if (version == AWS_LIBCRYPTO_1_1_1 && md_new_fn) {
        evp_md_ctx_table.new_fn = md_new_fn;
        evp_md_ctx_table.free_fn = md_free_fn;
        evp_md_ctx_table.init_ex_fn = md_init_ex_fn;
        evp_md_ctx_table.update_fn = md_update_fn;
        evp_md_ctx_table.final_ex_fn = md_final_ex_fn;
        g_aws_openssl_evp_md_ctx_table = &evp_md_ctx_table;
        return version;
    }

    return AWS_LIBCRYPTO_NONE;
}

static int s_resolve_libcrypto_symbols(enum aws_libcrypto_version version, void *module) {
    int found_version = s_resolve_libcrypto_hmac(version, module);
    if (!found_version) {
        return AWS_LIBCRYPTO_NONE;
    }
    if (!s_resolve_libcrypto_md(found_version, module)) {
        return AWS_LIBCRYPTO_NONE;
    }
    return found_version;
}

static int s_resolve_libcrypto_version(enum aws_libcrypto_version version) {
    const char *libcrypto_102 = "libcrypto.so.1.0.0";
    const char *libcrypto_111 = "libcrypto.so.1.1";
    switch (version) {
        case AWS_LIBCRYPTO_NONE: {
            FLOGF("searching process and loaded modules");
            void *process = dlopen(NULL, RTLD_NOW);
            AWS_FATAL_ASSERT(process && "Unable to load symbols from process space");
            int result = s_resolve_libcrypto_symbols(version, process);
            dlclose(process);
            return result;
        }
        case AWS_LIBCRYPTO_1_0_2: {
            FLOGF("loading libcrypto 1.0.2");
            void *module = dlopen(libcrypto_102, RTLD_NOW);
            if (module) {
                FLOGF("resolving against libcrypto 1.0.2");
                int result = s_resolve_libcrypto_symbols(version, module);
                dlclose(module);
                return result;
            }
            FLOGF("libcrypto 1.0.2 not found");
            break;
        }
        case AWS_LIBCRYPTO_1_1_1: {
            FLOGF("loading libcrypto 1.1.1");
            void *module = dlopen(libcrypto_111, RTLD_NOW);
            if (module) {
                FLOGF("resolving against libcrypto 1.1.1");
                int result = s_resolve_libcrypto_symbols(version, module);
                dlclose(module);
                return result;
            }
            FLOGF("libcrypto 1.1.1 not found");
            break;
        }
        default:
            AWS_FATAL_ASSERT("Attempted to use an unsupported version of libcrypto");
    }
    return AWS_LIBCRYPTO_NONE;
}

static int s_resolve_libcrypto(void) {
    if (s_libcrypto_version != AWS_LIBCRYPTO_NONE) {
        return s_libcrypto_version;
    }

    /* Try to auto-resolve against what's linked in/process space */
    FLOGF("Attempting auto-resolve against static linkage");
    s_libcrypto_version = s_resolve_libcrypto_version(AWS_LIBCRYPTO_NONE);
    /* try 1.0.2 */
    if (s_libcrypto_version == AWS_LIBCRYPTO_NONE) {
        FLOGF("Attempting resolve against libcrypto 1.0.2");
        s_libcrypto_version = s_resolve_libcrypto_version(AWS_LIBCRYPTO_1_0_2);
    }
    /* try 1.1.1 */
    if (s_libcrypto_version == AWS_LIBCRYPTO_NONE) {
        FLOGF("Attempting resolve against libcrypto 1.1.1");
        s_libcrypto_version = s_resolve_libcrypto_version(AWS_LIBCRYPTO_1_1_1);
    }

    return s_libcrypto_version;
}

/* Ignore warnings about how CRYPTO_get_locking_callback() always returns NULL on 1.1.1 */
#if !defined(__GNUC__) || (__GNUC__ >= 4 && __GNUC_MINOR__ > 1)
#    pragma GCC diagnostic push
#    pragma GCC diagnostic ignored "-Waddress"
#endif

void aws_cal_platform_init(struct aws_allocator *allocator) {
    s_libcrypto_allocator = allocator;

    int version = s_resolve_libcrypto();
    AWS_FATAL_ASSERT(version != AWS_LIBCRYPTO_NONE && "libcrypto could not be resolved");

    /* Ensure that libcrypto 1.0.2 has working locking mechanisms. This code is macro'ed
     * by libcrypto to be a no-op on 1.1.1 */
    if (!CRYPTO_get_locking_callback()) {
        /* on 1.1.1 this is a no-op */
        CRYPTO_set_locking_callback(s_locking_fn);
        if (CRYPTO_get_locking_callback() == s_locking_fn) {
            s_libcrypto_locks = aws_mem_acquire(allocator, sizeof(struct aws_mutex) * CRYPTO_num_locks());
            AWS_FATAL_ASSERT(s_libcrypto_locks);
            size_t lock_count = (size_t)CRYPTO_num_locks();
            for (size_t i = 0; i < lock_count; ++i) {
                aws_mutex_init(&s_libcrypto_locks[i]);
            }
        }
    }

    if (!CRYPTO_get_id_callback()) {
        CRYPTO_set_id_callback(s_id_fn);
    }
}

void aws_cal_platform_clean_up(void) {
    if (CRYPTO_get_locking_callback() == s_locking_fn) {
        CRYPTO_set_locking_callback(NULL);
        size_t lock_count = (size_t)CRYPTO_num_locks();
        for (size_t i = 0; i < lock_count; ++i) {
            aws_mutex_clean_up(&s_libcrypto_locks[i]);
        }
        aws_mem_release(s_libcrypto_allocator, s_libcrypto_locks);
    }

    if (CRYPTO_get_id_callback() == s_id_fn) {
        CRYPTO_set_id_callback(NULL);
    }
}
#if !defined(__GNUC__) || (__GNUC__ >= 4 && __GNUC_MINOR__ > 1)
#    pragma GCC diagnostic pop
#endif