blob: ee3a9d255e84f399a5d94294377ae45fc7af2189 [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
37#define CRYPTO_SO_FILENAME "libmbedcrypto.so"
38#define X509_SO_FILENAME "libmbedx509.so"
39#define TLS_SO_FILENAME "libmbedtls.so"
40
41#include <dlfcn.h>
42
43#define CHECK_DLERROR( function, argument ) \
44 do \
45 { \
46 char *CHECK_DLERROR_error = dlerror ( ); \
47 if( CHECK_DLERROR_error != NULL ) \
48 { \
49 fprintf( stderr, "Dynamic loading error for %s(%s): %s\n", \
50 function, argument, CHECK_DLERROR_error ); \
51 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
52 } \
53 } \
54 while( 0 )
55
56int main( void )
57{
Gilles Peskineb6a02992021-11-10 19:11:32 +010058#if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
Gilles Peskinea7c247e2021-11-04 12:45:19 +010059 unsigned n;
Gilles Peskineb6a02992021-11-10 19:11:32 +010060#endif
Gilles Peskinea7c247e2021-11-04 12:45:19 +010061
62#if defined(MBEDTLS_SSL_TLS_C)
63 void *tls_so = dlopen( TLS_SO_FILENAME, RTLD_NOW );
64 CHECK_DLERROR( "dlopen", TLS_SO_FILENAME );
65 const int *( *ssl_list_ciphersuites )( void ) =
66 dlsym( tls_so, "mbedtls_ssl_list_ciphersuites" );
67 CHECK_DLERROR( "dlsym", "mbedtls_ssl_list_ciphersuites" );
68 const int *ciphersuites = ssl_list_ciphersuites( );
69 for( n = 0; ciphersuites[n] != 0; n++ )
70 /* nothing to do, we're just counting */;
Gilles Peskine7fb54c52021-11-10 21:04:24 +010071 mbedtls_printf( "dlopen(%s): %u ciphersuites\n",
72 TLS_SO_FILENAME, n );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010073 dlclose( tls_so );
74 CHECK_DLERROR( "dlclose", TLS_SO_FILENAME );
75#endif /* MBEDTLS_SSL_TLS_C */
76
77#if defined(MBEDTLS_X509_CRT_PARSE_C)
78 void *x509_so = dlopen( X509_SO_FILENAME, RTLD_NOW );
79 CHECK_DLERROR( "dlopen", X509_SO_FILENAME );
80 const mbedtls_x509_crt_profile *profile =
81 dlsym( x509_so, "mbedtls_x509_crt_profile_default" );
82 CHECK_DLERROR( "dlsym", "mbedtls_x509_crt_profile_default" );
Gilles Peskine7fb54c52021-11-10 21:04:24 +010083 mbedtls_printf( "dlopen(%s): Allowed md mask: %08x\n",
84 X509_SO_FILENAME, (unsigned) profile->allowed_mds );
Gilles Peskinea7c247e2021-11-04 12:45:19 +010085 dlclose( x509_so );
86 CHECK_DLERROR( "dlclose", X509_SO_FILENAME );
87#endif /* MBEDTLS_X509_CRT_PARSE_C */
88
89#if defined(MBEDTLS_MD_C)
90 void *crypto_so = dlopen( CRYPTO_SO_FILENAME, RTLD_NOW );
91 CHECK_DLERROR( "dlopen", CRYPTO_SO_FILENAME );
92 const int *( *md_list )( void ) =
93 dlsym( crypto_so, "mbedtls_md_list" );
94 CHECK_DLERROR( "dlsym", "mbedtls_md_list" );
95 const int *mds = md_list( );
96 for( n = 0; mds[n] != 0; n++ )
97 /* nothing to do, we're just counting */;
Gilles Peskine7fb54c52021-11-10 21:04:24 +010098 mbedtls_printf( "dlopen(%s): %u hashes\n",
99 CRYPTO_SO_FILENAME, n );
Gilles Peskinea7c247e2021-11-04 12:45:19 +0100100 dlclose( crypto_so );
101 CHECK_DLERROR( "dlclose", CRYPTO_SO_FILENAME );
102#endif /* MBEDTLS_MD_C */
103
104 return( 0 );
105}
106