blob: 1ee24aefeb8bb7c69e56f48e60353766fbb6e274 [file] [log] [blame]
Gilles Peskinea7c247e2021-11-04 12:45:19 +01001/*
2 * Test dynamic loading of libmbed*
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinea7c247e2021-11-04 12:45:19 +01006 */
7
8#include "mbedtls/build_info.h"
9
10#include "mbedtls/platform.h"
Gilles Peskinea7c247e2021-11-04 12:45:19 +010011
12#if defined(MBEDTLS_X509_CRT_PARSE_C)
13#include "mbedtls/x509_crt.h"
14#endif
15
Gilles Peskine834d2292021-11-12 14:30:22 +010016#if defined(__APPLE__)
17#define SO_SUFFIX ".dylib"
18#else
19#define SO_SUFFIX ".so"
20#endif
21
22#define CRYPTO_SO_FILENAME "libmbedcrypto" SO_SUFFIX
23#define X509_SO_FILENAME "libmbedx509" SO_SUFFIX
24#define TLS_SO_FILENAME "libmbedtls" SO_SUFFIX
Gilles Peskinea7c247e2021-11-04 12:45:19 +010025
26#include <dlfcn.h>
27
Gilles Peskine449bd832023-01-11 14:50:10 +010028#define CHECK_DLERROR(function, argument) \
Gilles Peskinea7c247e2021-11-04 12:45:19 +010029 do \
30 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010031 char *CHECK_DLERROR_error = dlerror(); \
32 if (CHECK_DLERROR_error != NULL) \
Gilles Peskinea7c247e2021-11-04 12:45:19 +010033 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010034 fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \
35 function, argument, CHECK_DLERROR_error); \
36 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Gilles Peskinea7c247e2021-11-04 12:45:19 +010037 } \
38 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010039 while (0)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010040
Gilles Peskine449bd832023-01-11 14:50:10 +010041int main(void)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010042{
Gilles Peskineb6a02992021-11-10 19:11:32 +010043#if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010044 unsigned n;
Gilles Peskineb6a02992021-11-10 19:11:32 +010045#endif
Gilles Peskinea7c247e2021-11-04 12:45:19 +010046
47#if defined(MBEDTLS_SSL_TLS_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010048 void *tls_so = dlopen(TLS_SO_FILENAME, RTLD_NOW);
49 CHECK_DLERROR("dlopen", TLS_SO_FILENAME);
Gilles Peskine451b9ad2025-01-08 17:26:01 +010050#pragma GCC diagnostic push
51 /* dlsym() returns an object pointer which is meant to be used as a
52 * function pointer. This has undefined behavior in standard C, so
53 * "gcc -std=c99 -pedantic" complains about it, but it is perfectly
54 * fine on platforms that have dlsym(). */
55#pragma GCC diagnostic ignored "-Wpedantic"
Gilles Peskine449bd832023-01-11 14:50:10 +010056 const int *(*ssl_list_ciphersuites)(void) =
57 dlsym(tls_so, "mbedtls_ssl_list_ciphersuites");
Gilles Peskine451b9ad2025-01-08 17:26:01 +010058#pragma GCC diagnostic pop
Gilles Peskine449bd832023-01-11 14:50:10 +010059 CHECK_DLERROR("dlsym", "mbedtls_ssl_list_ciphersuites");
60 const int *ciphersuites = ssl_list_ciphersuites();
61 for (n = 0; ciphersuites[n] != 0; n++) {/* nothing to do, we're just counting */
62 ;
63 }
64 mbedtls_printf("dlopen(%s): %u ciphersuites\n",
65 TLS_SO_FILENAME, n);
66 dlclose(tls_so);
67 CHECK_DLERROR("dlclose", TLS_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +010068#endif /* MBEDTLS_SSL_TLS_C */
69
70#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010071 void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW);
72 CHECK_DLERROR("dlopen", X509_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +010073 const mbedtls_x509_crt_profile *profile =
Gilles Peskine449bd832023-01-11 14:50:10 +010074 dlsym(x509_so, "mbedtls_x509_crt_profile_default");
75 CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default");
76 mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n",
77 X509_SO_FILENAME, (unsigned) profile->allowed_mds);
78 dlclose(x509_so);
79 CHECK_DLERROR("dlclose", X509_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +010080#endif /* MBEDTLS_X509_CRT_PARSE_C */
81
82#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010083 void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW);
84 CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME);
Gilles Peskine451b9ad2025-01-08 17:26:01 +010085#pragma GCC diagnostic push
86 /* dlsym() returns an object pointer which is meant to be used as a
87 * function pointer. This has undefined behavior in standard C, so
88 * "gcc -std=c99 -pedantic" complains about it, but it is perfectly
89 * fine on platforms that have dlsym(). */
90#pragma GCC diagnostic ignored "-Wpedantic"
Gilles Peskine449bd832023-01-11 14:50:10 +010091 const int *(*md_list)(void) =
92 dlsym(crypto_so, "mbedtls_md_list");
Gilles Peskine451b9ad2025-01-08 17:26:01 +010093#pragma GCC diagnostic pop
Gilles Peskine449bd832023-01-11 14:50:10 +010094 CHECK_DLERROR("dlsym", "mbedtls_md_list");
95 const int *mds = md_list();
96 for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */
97 ;
98 }
99 mbedtls_printf("dlopen(%s): %u hashes\n",
100 CRYPTO_SO_FILENAME, n);
101 dlclose(crypto_so);
102 CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +0100103#endif /* MBEDTLS_MD_C */
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return 0;
Gilles Peskinea7c247e2021-11-04 12:45:19 +0100106}