dlopen.c 4.0 KB

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