blob: 2dcda3bb265992f473db4cdfb25924e055a7aebf [file] [log] [blame]
Gilles Peskinea7c247e2021-11-04 12:45:19 +01001/*
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
20#include "mbedtls/build_info.h"
21
22#include "mbedtls/platform.h"
Gilles Peskinea7c247e2021-11-04 12:45:19 +010023
24#if defined(MBEDTLS_X509_CRT_PARSE_C)
25#include "mbedtls/x509_crt.h"
26#endif
27
Gilles Peskine834d2292021-11-12 14:30:22 +010028#if defined(__APPLE__)
29#define SO_SUFFIX ".dylib"
30#else
31#define SO_SUFFIX ".so"
32#endif
33
34#define CRYPTO_SO_FILENAME "libmbedcrypto" SO_SUFFIX
35#define X509_SO_FILENAME "libmbedx509" SO_SUFFIX
36#define TLS_SO_FILENAME "libmbedtls" SO_SUFFIX
Gilles Peskinea7c247e2021-11-04 12:45:19 +010037
38#include <dlfcn.h>
39
Gilles Peskine449bd832023-01-11 14:50:10 +010040#define CHECK_DLERROR(function, argument) \
Gilles Peskinea7c247e2021-11-04 12:45:19 +010041 do \
42 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010043 char *CHECK_DLERROR_error = dlerror(); \
44 if (CHECK_DLERROR_error != NULL) \
Gilles Peskinea7c247e2021-11-04 12:45:19 +010045 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010046 fprintf(stderr, "Dynamic loading error for %s(%s): %s\n", \
47 function, argument, CHECK_DLERROR_error); \
48 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Gilles Peskinea7c247e2021-11-04 12:45:19 +010049 } \
50 } \
Gilles Peskine449bd832023-01-11 14:50:10 +010051 while (0)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010052
Gilles Peskine449bd832023-01-11 14:50:10 +010053int main(void)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010054{
Gilles Peskineb6a02992021-11-10 19:11:32 +010055#if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010056 unsigned n;
Gilles Peskineb6a02992021-11-10 19:11:32 +010057#endif
Gilles Peskinea7c247e2021-11-04 12:45:19 +010058
59#if defined(MBEDTLS_SSL_TLS_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010060 void *tls_so = dlopen(TLS_SO_FILENAME, RTLD_NOW);
61 CHECK_DLERROR("dlopen", TLS_SO_FILENAME);
62 const int *(*ssl_list_ciphersuites)(void) =
63 dlsym(tls_so, "mbedtls_ssl_list_ciphersuites");
64 CHECK_DLERROR("dlsym", "mbedtls_ssl_list_ciphersuites");
65 const int *ciphersuites = ssl_list_ciphersuites();
66 for (n = 0; ciphersuites[n] != 0; n++) {/* nothing to do, we're just counting */
67 ;
68 }
69 mbedtls_printf("dlopen(%s): %u ciphersuites\n",
70 TLS_SO_FILENAME, n);
71 dlclose(tls_so);
72 CHECK_DLERROR("dlclose", TLS_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +010073#endif /* MBEDTLS_SSL_TLS_C */
74
75#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010076 void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW);
77 CHECK_DLERROR("dlopen", X509_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +010078 const mbedtls_x509_crt_profile *profile =
Gilles Peskine449bd832023-01-11 14:50:10 +010079 dlsym(x509_so, "mbedtls_x509_crt_profile_default");
80 CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default");
81 mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n",
82 X509_SO_FILENAME, (unsigned) profile->allowed_mds);
83 dlclose(x509_so);
84 CHECK_DLERROR("dlclose", X509_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +010085#endif /* MBEDTLS_X509_CRT_PARSE_C */
86
87#if defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010088 void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW);
89 CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME);
90 const int *(*md_list)(void) =
91 dlsym(crypto_so, "mbedtls_md_list");
92 CHECK_DLERROR("dlsym", "mbedtls_md_list");
93 const int *mds = md_list();
94 for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */
95 ;
96 }
97 mbedtls_printf("dlopen(%s): %u hashes\n",
98 CRYPTO_SO_FILENAME, n);
99 dlclose(crypto_so);
100 CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME);
Gilles Peskinea7c247e2021-11-04 12:45:19 +0100101#endif /* MBEDTLS_MD_C */
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return 0;
Gilles Peskinea7c247e2021-11-04 12:45:19 +0100104}