Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Test dynamic loading of libmbed* |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
Gilles Peskine | 6fa5c1d | 2021-11-25 18:12:44 +0100 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Gilles Peskine | 6fa5c1d | 2021-11-25 18:12:44 +0100 | [diff] [blame] | 22 | #else |
| 23 | #include MBEDTLS_CONFIG_FILE |
| 24 | #endif |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 25 | |
| 26 | #include "mbedtls/platform.h" |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 27 | |
| 28 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 29 | #include "mbedtls/x509_crt.h" |
| 30 | #endif |
| 31 | |
Gilles Peskine | 8e8e965 | 2021-11-12 14:30:22 +0100 | [diff] [blame] | 32 | #if defined(__APPLE__) |
| 33 | #define SO_SUFFIX ".dylib" |
| 34 | #else |
| 35 | #define SO_SUFFIX ".so" |
| 36 | #endif |
| 37 | |
| 38 | #define CRYPTO_SO_FILENAME "libmbedcrypto" SO_SUFFIX |
| 39 | #define X509_SO_FILENAME "libmbedx509" SO_SUFFIX |
| 40 | #define TLS_SO_FILENAME "libmbedtls" SO_SUFFIX |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 41 | |
| 42 | #include <dlfcn.h> |
| 43 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | #define CHECK_DLERROR(function, argument) \ |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 45 | do \ |
| 46 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | char *CHECK_DLERROR_error = dlerror(); \ |
| 48 | if (CHECK_DLERROR_error != NULL) \ |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 49 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \ |
| 51 | function, argument, CHECK_DLERROR_error); \ |
| 52 | mbedtls_exit(MBEDTLS_EXIT_FAILURE); \ |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 53 | } \ |
| 54 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 55 | while (0) |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 56 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | int main(void) |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 58 | { |
Gilles Peskine | 3dbb3e7 | 2021-11-10 19:11:32 +0100 | [diff] [blame] | 59 | #if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C) |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 60 | unsigned n; |
Gilles Peskine | 3dbb3e7 | 2021-11-10 19:11:32 +0100 | [diff] [blame] | 61 | #endif |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 62 | |
| 63 | #if defined(MBEDTLS_SSL_TLS_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | void *tls_so = dlopen(TLS_SO_FILENAME, RTLD_NOW); |
| 65 | CHECK_DLERROR("dlopen", TLS_SO_FILENAME); |
| 66 | const int *(*ssl_list_ciphersuites)(void) = |
| 67 | dlsym(tls_so, "mbedtls_ssl_list_ciphersuites"); |
| 68 | CHECK_DLERROR("dlsym", "mbedtls_ssl_list_ciphersuites"); |
| 69 | const int *ciphersuites = ssl_list_ciphersuites(); |
| 70 | for (n = 0; ciphersuites[n] != 0; n++) {/* nothing to do, we're just counting */ |
| 71 | ; |
| 72 | } |
| 73 | mbedtls_printf("dlopen(%s): %u ciphersuites\n", |
| 74 | TLS_SO_FILENAME, n); |
| 75 | dlclose(tls_so); |
| 76 | CHECK_DLERROR("dlclose", TLS_SO_FILENAME); |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 77 | #endif /* MBEDTLS_SSL_TLS_C */ |
| 78 | |
| 79 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW); |
| 81 | CHECK_DLERROR("dlopen", X509_SO_FILENAME); |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 82 | const mbedtls_x509_crt_profile *profile = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | dlsym(x509_so, "mbedtls_x509_crt_profile_default"); |
| 84 | CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default"); |
| 85 | mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n", |
| 86 | X509_SO_FILENAME, (unsigned) profile->allowed_mds); |
| 87 | dlclose(x509_so); |
| 88 | CHECK_DLERROR("dlclose", X509_SO_FILENAME); |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 89 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 90 | |
| 91 | #if defined(MBEDTLS_MD_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW); |
| 93 | CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME); |
| 94 | const int *(*md_list)(void) = |
| 95 | dlsym(crypto_so, "mbedtls_md_list"); |
| 96 | CHECK_DLERROR("dlsym", "mbedtls_md_list"); |
| 97 | const int *mds = md_list(); |
| 98 | for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */ |
| 99 | ; |
| 100 | } |
| 101 | mbedtls_printf("dlopen(%s): %u hashes\n", |
| 102 | CRYPTO_SO_FILENAME, n); |
| 103 | dlclose(crypto_so); |
| 104 | CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME); |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 105 | #endif /* MBEDTLS_MD_C */ |
| 106 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | return 0; |
Gilles Peskine | e943353 | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 108 | } |