1 /*
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
24 #if defined(MBEDTLS_X509_CRT_PARSE_C)
25 #include "mbedtls/x509_crt.h"
26 #endif
27
28 #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
37
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
main(void)53 int main( void )
54 {
55 #if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C)
56 unsigned n;
57 #endif
58
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 */;
68 mbedtls_printf( "dlopen(%s): %u ciphersuites\n",
69 TLS_SO_FILENAME, n );
70 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" );
80 mbedtls_printf( "dlopen(%s): Allowed md mask: %08x\n",
81 X509_SO_FILENAME, (unsigned) profile->allowed_mds );
82 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 */;
95 mbedtls_printf( "dlopen(%s): %u hashes\n",
96 CRYPTO_SO_FILENAME, n );
97 dlclose( crypto_so );
98 CHECK_DLERROR( "dlclose", CRYPTO_SO_FILENAME );
99 #endif /* MBEDTLS_MD_C */
100
101 return( 0 );
102 }
103
104