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