1 /*
2 * Test dynamic loading of libmbed*
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if defined(MBEDTLS_X509_CRT_PARSE_C)
17 #include "mbedtls/x509_crt.h"
18 #endif
19
20 #if defined(__APPLE__)
21 #define SO_SUFFIX ".dylib"
22 #else
23 #define SO_SUFFIX ".so"
24 #endif
25
26 #define CRYPTO_SO_FILENAME "libmbedcrypto" SO_SUFFIX
27 #define X509_SO_FILENAME "libmbedx509" SO_SUFFIX
28 #define TLS_SO_FILENAME "libmbedtls" SO_SUFFIX
29
30 #include <dlfcn.h>
31
32 #define CHECK_DLERROR(function, argument) \
33 do \
34 { \
35 char *CHECK_DLERROR_error = dlerror(); \
36 if (CHECK_DLERROR_error != NULL) \
37 { \
38 fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \
39 function, argument, CHECK_DLERROR_error); \
40 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
41 } \
42 } \
43 while (0)
44
main(void)45 int main(void)
46 {
47 #if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
48 unsigned n;
49 #endif
50
51 #if defined(MBEDTLS_SSL_TLS_C)
52 void *tls_so = dlopen(TLS_SO_FILENAME, RTLD_NOW);
53 CHECK_DLERROR("dlopen", TLS_SO_FILENAME);
54 const int *(*ssl_list_ciphersuites)(void) =
55 dlsym(tls_so, "mbedtls_ssl_list_ciphersuites");
56 CHECK_DLERROR("dlsym", "mbedtls_ssl_list_ciphersuites");
57 const int *ciphersuites = ssl_list_ciphersuites();
58 for (n = 0; ciphersuites[n] != 0; n++) {/* nothing to do, we're just counting */
59 ;
60 }
61 mbedtls_printf("dlopen(%s): %u ciphersuites\n",
62 TLS_SO_FILENAME, n);
63 dlclose(tls_so);
64 CHECK_DLERROR("dlclose", TLS_SO_FILENAME);
65 #endif /* MBEDTLS_SSL_TLS_C */
66
67 #if defined(MBEDTLS_X509_CRT_PARSE_C)
68 void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW);
69 CHECK_DLERROR("dlopen", X509_SO_FILENAME);
70 const mbedtls_x509_crt_profile *profile =
71 dlsym(x509_so, "mbedtls_x509_crt_profile_default");
72 CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default");
73 mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n",
74 X509_SO_FILENAME, (unsigned) profile->allowed_mds);
75 dlclose(x509_so);
76 CHECK_DLERROR("dlclose", X509_SO_FILENAME);
77 #endif /* MBEDTLS_X509_CRT_PARSE_C */
78
79 #if defined(MBEDTLS_MD_C)
80 void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW);
81 CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME);
82 const int *(*md_list)(void) =
83 dlsym(crypto_so, "mbedtls_md_list");
84 CHECK_DLERROR("dlsym", "mbedtls_md_list");
85 const int *mds = md_list();
86 for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */
87 ;
88 }
89 mbedtls_printf("dlopen(%s): %u hashes\n",
90 CRYPTO_SO_FILENAME, n);
91 dlclose(crypto_so);
92 CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME);
93 #endif /* MBEDTLS_MD_C */
94
95 return 0;
96 }
97