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 53int 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++) {/* nothing to do, we're just counting */ 67 ; 68 } 69 mbedtls_printf("dlopen(%s): %u ciphersuites\n", 70 TLS_SO_FILENAME, n); 71 dlclose(tls_so); 72 CHECK_DLERROR("dlclose", TLS_SO_FILENAME); 73#endif /* MBEDTLS_SSL_TLS_C */ 74 75#if defined(MBEDTLS_X509_CRT_PARSE_C) 76 void *x509_so = dlopen(X509_SO_FILENAME, RTLD_NOW); 77 CHECK_DLERROR("dlopen", X509_SO_FILENAME); 78 const mbedtls_x509_crt_profile *profile = 79 dlsym(x509_so, "mbedtls_x509_crt_profile_default"); 80 CHECK_DLERROR("dlsym", "mbedtls_x509_crt_profile_default"); 81 mbedtls_printf("dlopen(%s): Allowed md mask: %08x\n", 82 X509_SO_FILENAME, (unsigned) profile->allowed_mds); 83 dlclose(x509_so); 84 CHECK_DLERROR("dlclose", X509_SO_FILENAME); 85#endif /* MBEDTLS_X509_CRT_PARSE_C */ 86 87#if defined(MBEDTLS_MD_C) 88 void *crypto_so = dlopen(CRYPTO_SO_FILENAME, RTLD_NOW); 89 CHECK_DLERROR("dlopen", CRYPTO_SO_FILENAME); 90 const int *(*md_list)(void) = 91 dlsym(crypto_so, "mbedtls_md_list"); 92 CHECK_DLERROR("dlsym", "mbedtls_md_list"); 93 const int *mds = md_list(); 94 for (n = 0; mds[n] != 0; n++) {/* nothing to do, we're just counting */ 95 ; 96 } 97 mbedtls_printf("dlopen(%s): %u hashes\n", 98 CRYPTO_SO_FILENAME, n); 99 dlclose(crypto_so); 100 CHECK_DLERROR("dlclose", CRYPTO_SO_FILENAME); 101#endif /* MBEDTLS_MD_C */ 102 103 return 0; 104} 105