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