blob: 3b88df4cf3e955a9c6b1648508972e00b127d243 [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"
23#if !defined(MBEDTLS_PLATFORM_C)
24#include <stdio.h>
25#include <stdlib.h>
26#define mbedtls_fprintf fprintf
27#define mbedtls_printf printf
28#define mbedtls_exit exit
29#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
30#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif
32
33#if defined(MBEDTLS_X509_CRT_PARSE_C)
34#include "mbedtls/x509_crt.h"
35#endif
36
Gilles Peskine834d2292021-11-12 14:30:22 +010037#if defined(__APPLE__)
38#define SO_SUFFIX ".dylib"
39#else
40#define SO_SUFFIX ".so"
41#endif
42
43#define CRYPTO_SO_FILENAME "libmbedcrypto" SO_SUFFIX
44#define X509_SO_FILENAME "libmbedx509" SO_SUFFIX
45#define TLS_SO_FILENAME "libmbedtls" SO_SUFFIX
Gilles Peskinea7c247e2021-11-04 12:45:19 +010046
47#include <dlfcn.h>
48
49#define CHECK_DLERROR( function, argument ) \
50 do \
51 { \
52 char *CHECK_DLERROR_error = dlerror ( ); \
53 if( CHECK_DLERROR_error != NULL ) \
54 { \
55 fprintf( stderr, "Dynamic loading error for %s(%s): %s\n", \
56 function, argument, CHECK_DLERROR_error ); \
57 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
58 } \
59 } \
60 while( 0 )
61
62int main( void )
63{
Gilles Peskineb6a02992021-11-10 19:11:32 +010064#if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010065 unsigned n;
Gilles Peskineb6a02992021-11-10 19:11:32 +010066#endif
Gilles Peskinea7c247e2021-11-04 12:45:19 +010067
68#if defined(MBEDTLS_SSL_TLS_C)
69 void *tls_so = dlopen( TLS_SO_FILENAME, RTLD_NOW );
70 CHECK_DLERROR( "dlopen", TLS_SO_FILENAME );
71 const int *( *ssl_list_ciphersuites )( void ) =
72 dlsym( tls_so, "mbedtls_ssl_list_ciphersuites" );
73 CHECK_DLERROR( "dlsym", "mbedtls_ssl_list_ciphersuites" );
74 const int *ciphersuites = ssl_list_ciphersuites( );
75 for( n = 0; ciphersuites[n] != 0; n++ )
76 /* nothing to do, we're just counting */;
Gilles Peskine7fb54c52021-11-10 21:04:24 +010077 mbedtls_printf( "dlopen(%s): %u ciphersuites\n",
78 TLS_SO_FILENAME, n );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010079 dlclose( tls_so );
80 CHECK_DLERROR( "dlclose", TLS_SO_FILENAME );
81#endif /* MBEDTLS_SSL_TLS_C */
82
83#if defined(MBEDTLS_X509_CRT_PARSE_C)
84 void *x509_so = dlopen( X509_SO_FILENAME, RTLD_NOW );
85 CHECK_DLERROR( "dlopen", X509_SO_FILENAME );
86 const mbedtls_x509_crt_profile *profile =
87 dlsym( x509_so, "mbedtls_x509_crt_profile_default" );
88 CHECK_DLERROR( "dlsym", "mbedtls_x509_crt_profile_default" );
Gilles Peskine7fb54c52021-11-10 21:04:24 +010089 mbedtls_printf( "dlopen(%s): Allowed md mask: %08x\n",
90 X509_SO_FILENAME, (unsigned) profile->allowed_mds );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010091 dlclose( x509_so );
92 CHECK_DLERROR( "dlclose", X509_SO_FILENAME );
93#endif /* MBEDTLS_X509_CRT_PARSE_C */
94
95#if defined(MBEDTLS_MD_C)
96 void *crypto_so = dlopen( CRYPTO_SO_FILENAME, RTLD_NOW );
97 CHECK_DLERROR( "dlopen", CRYPTO_SO_FILENAME );
98 const int *( *md_list )( void ) =
99 dlsym( crypto_so, "mbedtls_md_list" );
100 CHECK_DLERROR( "dlsym", "mbedtls_md_list" );
101 const int *mds = md_list( );
102 for( n = 0; mds[n] != 0; n++ )
103 /* nothing to do, we're just counting */;
Gilles Peskine7fb54c52021-11-10 21:04:24 +0100104 mbedtls_printf( "dlopen(%s): %u hashes\n",
105 CRYPTO_SO_FILENAME, n );
Gilles Peskinea7c247e2021-11-04 12:45:19 +0100106 dlclose( crypto_so );
107 CHECK_DLERROR( "dlclose", CRYPTO_SO_FILENAME );
108#endif /* MBEDTLS_MD_C */
109
110 return( 0 );
111}
112