dlopen.c 3.7 KB

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