dlopen.c 4.1 KB

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