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 #include "mbedtls/build_info.h"
9
10 #include "mbedtls/platform.h"
11
12 #if defined(MBEDTLS_X509_CRT_PARSE_C)
13 #include "mbedtls/x509_crt.h"
14 #endif
15
16 #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
25
26 #include <dlfcn.h>
27
28 #define CHECK_DLERROR(function, argument) \
29 do \
30 { \
31 char *CHECK_DLERROR_error = dlerror(); \
32 if (CHECK_DLERROR_error != NULL) \
33 { \
34 fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \
35 function, argument, CHECK_DLERROR_error); \
36 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
37 } \
38 } \
39 while (0)
40
main(void)41 int main(void)
42 {
43 #if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
44 unsigned n;
45 #endif
46
47 #if defined(MBEDTLS_SSL_TLS_C)
48 void *tls_so = dlopen(TLS_SO_FILENAME, RTLD_NOW);
49 CHECK_DLERROR("dlopen", TLS_SO_FILENAME);
50 const int *(*ssl_list_ciphersuites)(void) =
51 dlsym(tls_so, "mbedtls_ssl_list_ciphersuites");
52 CHECK_DLERROR("dlsym", "mbedtls_ssl_list_ciphersuites");
53 const int *ciphersuites = ssl_list_ciphersuites();
54 for (n = 0; ciphersuites[n] != 0; n++) {/* nothing to do, we're just counting */
55 ;
56 }
57 mbedtls_printf("dlopen(%s): %u ciphersuites\n",
58 TLS_SO_FILENAME, n);
59 dlclose(tls_so);
60 CHECK_DLERROR("dlclose", TLS_SO_FILENAME);
61 #endif /* MBEDTLS_SSL_TLS_C */
62
63 #if defined(MBEDTLS_X509_CRT_PARSE_C)
64 void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW);
65 CHECK_DLERROR("dlopen", X509_SO_FILENAME);
66 const mbedtls_x509_crt_profile *profile =
67 dlsym(x509_so, "mbedtls_x509_crt_profile_default");
68 CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default");
69 mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n",
70 X509_SO_FILENAME, (unsigned) profile->allowed_mds);
71 dlclose(x509_so);
72 CHECK_DLERROR("dlclose", X509_SO_FILENAME);
73 #endif /* MBEDTLS_X509_CRT_PARSE_C */
74
75 #if defined(MBEDTLS_MD_C)
76 void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW);
77 CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME);
78 const int *(*md_list)(void) =
79 dlsym(crypto_so, "mbedtls_md_list");
80 CHECK_DLERROR("dlsym", "mbedtls_md_list");
81 const int *mds = md_list();
82 for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */
83 ;
84 }
85 mbedtls_printf("dlopen(%s): %u hashes\n",
86 CRYPTO_SO_FILENAME, n);
87 dlclose(crypto_so);
88 CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME);
89 #endif /* MBEDTLS_MD_C */
90
91 return 0;
92 }
93