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