Gilles Peskine | a7c247e | 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 | |
| 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 | |
| 56 | int main( void ) |
| 57 | { |
Gilles Peskine | b6a0299 | 2021-11-10 19:11:32 +0100 | [diff] [blame^] | 58 | #if defined(MBEDTLS_MD_C) || defined(MBEDTLS_SSL_TLS_C) |
Gilles Peskine | a7c247e | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 59 | unsigned n; |
Gilles Peskine | b6a0299 | 2021-11-10 19:11:32 +0100 | [diff] [blame^] | 60 | #endif |
Gilles Peskine | a7c247e | 2021-11-04 12:45:19 +0100 | [diff] [blame] | 61 | |
| 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 */; |
| 71 | mbedtls_printf( "%u ciphersuites\n", n ); |
| 72 | dlclose( tls_so ); |
| 73 | CHECK_DLERROR( "dlclose", TLS_SO_FILENAME ); |
| 74 | #endif /* MBEDTLS_SSL_TLS_C */ |
| 75 | |
| 76 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 77 | void *x509_so = dlopen( X509_SO_FILENAME, RTLD_NOW ); |
| 78 | CHECK_DLERROR( "dlopen", X509_SO_FILENAME ); |
| 79 | const mbedtls_x509_crt_profile *profile = |
| 80 | dlsym( x509_so, "mbedtls_x509_crt_profile_default" ); |
| 81 | CHECK_DLERROR( "dlsym", "mbedtls_x509_crt_profile_default" ); |
| 82 | mbedtls_printf( "Allowed md mask: %08x\n", |
| 83 | (unsigned) profile->allowed_mds ); |
| 84 | dlclose( x509_so ); |
| 85 | CHECK_DLERROR( "dlclose", X509_SO_FILENAME ); |
| 86 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 87 | |
| 88 | #if defined(MBEDTLS_MD_C) |
| 89 | void *crypto_so = dlopen( CRYPTO_SO_FILENAME, RTLD_NOW ); |
| 90 | CHECK_DLERROR( "dlopen", CRYPTO_SO_FILENAME ); |
| 91 | const int *( *md_list )( void ) = |
| 92 | dlsym( crypto_so, "mbedtls_md_list" ); |
| 93 | CHECK_DLERROR( "dlsym", "mbedtls_md_list" ); |
| 94 | const int *mds = md_list( ); |
| 95 | for( n = 0; mds[n] != 0; n++ ) |
| 96 | /* nothing to do, we're just counting */; |
| 97 | mbedtls_printf( "%u hashes\n", n ); |
| 98 | dlclose( crypto_so ); |
| 99 | CHECK_DLERROR( "dlclose", CRYPTO_SO_FILENAME ); |
| 100 | #endif /* MBEDTLS_MD_C */ |
| 101 | |
| 102 | return( 0 ); |
| 103 | } |
| 104 | |