blob: ff61fcde010f455ec9d3b18ae7e0d5ccec071d16 [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
40#define CHECK_DLERROR( function, argument ) \
41 do \
42 { \
43 char *CHECK_DLERROR_error = dlerror ( ); \
44 if( CHECK_DLERROR_error != NULL ) \
45 { \
46 fprintf( stderr, "Dynamic loading error for %s(%s): %s\n", \
47 function, argument, CHECK_DLERROR_error ); \
48 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
49 } \
50 } \
51 while( 0 )
52
53int main( void )
54{
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)
60 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++ )
67 /* nothing to do, we're just counting */;
Gilles Peskine7fb54c52021-11-10 21:04:24 +010068 mbedtls_printf( "dlopen(%s): %u ciphersuites\n",
69 TLS_SO_FILENAME, n );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010070 dlclose( tls_so );
71 CHECK_DLERROR( "dlclose", TLS_SO_FILENAME );
72#endif /* MBEDTLS_SSL_TLS_C */
73
74#if defined(MBEDTLS_X509_CRT_PARSE_C)
75 void *x509_so = dlopen( X509_SO_FILENAME, RTLD_NOW );
76 CHECK_DLERROR( "dlopen", X509_SO_FILENAME );
77 const mbedtls_x509_crt_profile *profile =
78 dlsym( x509_so, "mbedtls_x509_crt_profile_default" );
79 CHECK_DLERROR( "dlsym", "mbedtls_x509_crt_profile_default" );
Gilles Peskine7fb54c52021-11-10 21:04:24 +010080 mbedtls_printf( "dlopen(%s): Allowed md mask: %08x\n",
81 X509_SO_FILENAME, (unsigned) profile->allowed_mds );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010082 dlclose( x509_so );
83 CHECK_DLERROR( "dlclose", X509_SO_FILENAME );
84#endif /* MBEDTLS_X509_CRT_PARSE_C */
85
86#if defined(MBEDTLS_MD_C)
87 void *crypto_so = dlopen( CRYPTO_SO_FILENAME, RTLD_NOW );
88 CHECK_DLERROR( "dlopen", CRYPTO_SO_FILENAME );
89 const int *( *md_list )( void ) =
90 dlsym( crypto_so, "mbedtls_md_list" );
91 CHECK_DLERROR( "dlsym", "mbedtls_md_list" );
92 const int *mds = md_list( );
93 for( n = 0; mds[n] != 0; n++ )
94 /* nothing to do, we're just counting */;
Gilles Peskine7fb54c52021-11-10 21:04:24 +010095 mbedtls_printf( "dlopen(%s): %u hashes\n",
96 CRYPTO_SO_FILENAME, n );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010097 dlclose( crypto_so );
98 CHECK_DLERROR( "dlclose", CRYPTO_SO_FILENAME );
99#endif /* MBEDTLS_MD_C */
100
101 return( 0 );
102}
103