shlibloadtest.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/opensslv.h>
  13. #include <openssl/ssl.h>
  14. #include <openssl/ossl_typ.h>
  15. #include "internal/dso_conf.h"
  16. #include "testutil.h"
  17. typedef void DSO;
  18. typedef const SSL_METHOD * (*TLS_method_t)(void);
  19. typedef SSL_CTX * (*SSL_CTX_new_t)(const SSL_METHOD *meth);
  20. typedef void (*SSL_CTX_free_t)(SSL_CTX *);
  21. typedef unsigned long (*ERR_get_error_t)(void);
  22. typedef unsigned long (*OpenSSL_version_num_t)(void);
  23. typedef DSO * (*DSO_dsobyaddr_t)(void (*addr)(void), int flags);
  24. typedef int (*DSO_free_t)(DSO *dso);
  25. typedef enum test_types_en {
  26. CRYPTO_FIRST,
  27. SSL_FIRST,
  28. JUST_CRYPTO,
  29. DSO_REFTEST
  30. } TEST_TYPE;
  31. static TEST_TYPE test_type;
  32. static const char *path_crypto;
  33. static const char *path_ssl;
  34. #ifdef DSO_DLFCN
  35. # include <dlfcn.h>
  36. # define SHLIB_INIT NULL
  37. typedef void *SHLIB;
  38. typedef void *SHLIB_SYM;
  39. static int shlib_load(const char *filename, SHLIB *lib)
  40. {
  41. int dl_flags = (RTLD_GLOBAL|RTLD_LAZY);
  42. #ifdef _AIX
  43. if (filename[strlen(filename) - 1] == ')')
  44. dl_flags |= RTLD_MEMBER;
  45. #endif
  46. *lib = dlopen(filename, dl_flags);
  47. return *lib == NULL ? 0 : 1;
  48. }
  49. static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
  50. {
  51. *sym = dlsym(lib, symname);
  52. return *sym != NULL;
  53. }
  54. static int shlib_close(SHLIB lib)
  55. {
  56. return dlclose(lib) != 0 ? 0 : 1;
  57. }
  58. #endif
  59. #ifdef DSO_WIN32
  60. # include <windows.h>
  61. # define SHLIB_INIT 0
  62. typedef HINSTANCE SHLIB;
  63. typedef void *SHLIB_SYM;
  64. static int shlib_load(const char *filename, SHLIB *lib)
  65. {
  66. *lib = LoadLibraryA(filename);
  67. return *lib == NULL ? 0 : 1;
  68. }
  69. static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
  70. {
  71. *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
  72. return *sym != NULL;
  73. }
  74. static int shlib_close(SHLIB lib)
  75. {
  76. return FreeLibrary(lib) == 0 ? 0 : 1;
  77. }
  78. #endif
  79. #if defined(DSO_DLFCN) || defined(DSO_WIN32)
  80. static int test_lib(void)
  81. {
  82. SHLIB ssllib = SHLIB_INIT;
  83. SHLIB cryptolib = SHLIB_INIT;
  84. SSL_CTX *ctx;
  85. union {
  86. void (*func)(void);
  87. SHLIB_SYM sym;
  88. } symbols[3];
  89. TLS_method_t myTLS_method;
  90. SSL_CTX_new_t mySSL_CTX_new;
  91. SSL_CTX_free_t mySSL_CTX_free;
  92. ERR_get_error_t myERR_get_error;
  93. OpenSSL_version_num_t myOpenSSL_version_num;
  94. int result = 0;
  95. switch (test_type) {
  96. case JUST_CRYPTO:
  97. if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
  98. goto end;
  99. break;
  100. case CRYPTO_FIRST:
  101. if (!TEST_true(shlib_load(path_crypto, &cryptolib))
  102. || !TEST_true(shlib_load(path_ssl, &ssllib)))
  103. goto end;
  104. break;
  105. case SSL_FIRST:
  106. if (!TEST_true(shlib_load(path_ssl, &ssllib))
  107. || !TEST_true(shlib_load(path_crypto, &cryptolib)))
  108. goto end;
  109. break;
  110. case DSO_REFTEST:
  111. if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
  112. goto end;
  113. break;
  114. }
  115. if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
  116. if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
  117. || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
  118. || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
  119. goto end;
  120. myTLS_method = (TLS_method_t)symbols[0].func;
  121. mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
  122. mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
  123. if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
  124. goto end;
  125. mySSL_CTX_free(ctx);
  126. }
  127. if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
  128. || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
  129. &symbols[1].sym)))
  130. goto end;
  131. myERR_get_error = (ERR_get_error_t)symbols[0].func;
  132. if (!TEST_int_eq(myERR_get_error(), 0))
  133. goto end;
  134. /*
  135. * The bits that COMPATIBILITY_MASK lets through MUST be the same in
  136. * the library and in the application.
  137. * The bits that are masked away MUST be a larger or equal number in
  138. * the library compared to the application.
  139. */
  140. # define COMPATIBILITY_MASK 0xfff00000L
  141. myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
  142. if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK,
  143. OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK))
  144. goto end;
  145. if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK,
  146. OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK))
  147. goto end;
  148. if (test_type == DSO_REFTEST) {
  149. # ifdef DSO_DLFCN
  150. DSO_dsobyaddr_t myDSO_dsobyaddr;
  151. DSO_free_t myDSO_free;
  152. /*
  153. * This is resembling the code used in ossl_init_base() and
  154. * OPENSSL_atexit() to block unloading the library after dlclose().
  155. * We are not testing this on Windows, because it is done there in a
  156. * completely different way. Especially as a call to DSO_dsobyaddr()
  157. * will always return an error, because DSO_pathbyaddr() is not
  158. * implemented there.
  159. */
  160. if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
  161. || !TEST_true(shlib_sym(cryptolib, "DSO_free",
  162. &symbols[1].sym)))
  163. goto end;
  164. myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
  165. myDSO_free = (DSO_free_t)symbols[1].func;
  166. {
  167. DSO *hndl;
  168. /* use known symbol from crypto module */
  169. if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
  170. goto end;
  171. myDSO_free(hndl);
  172. }
  173. # endif /* DSO_DLFCN */
  174. }
  175. switch (test_type) {
  176. case JUST_CRYPTO:
  177. if (!TEST_true(shlib_close(cryptolib)))
  178. goto end;
  179. break;
  180. case CRYPTO_FIRST:
  181. if (!TEST_true(shlib_close(cryptolib))
  182. || !TEST_true(shlib_close(ssllib)))
  183. goto end;
  184. break;
  185. case SSL_FIRST:
  186. if (!TEST_true(shlib_close(ssllib))
  187. || !TEST_true(shlib_close(cryptolib)))
  188. goto end;
  189. break;
  190. case DSO_REFTEST:
  191. if (!TEST_true(shlib_close(cryptolib)))
  192. goto end;
  193. break;
  194. }
  195. result = 1;
  196. end:
  197. return result;
  198. }
  199. #endif
  200. int setup_tests(void)
  201. {
  202. const char *p = test_get_argument(0);
  203. if (strcmp(p, "-crypto_first") == 0) {
  204. test_type = CRYPTO_FIRST;
  205. } else if (strcmp(p, "-ssl_first") == 0) {
  206. test_type = SSL_FIRST;
  207. } else if (strcmp(p, "-just_crypto") == 0) {
  208. test_type = JUST_CRYPTO;
  209. } else if (strcmp(p, "-dso_ref") == 0) {
  210. test_type = JUST_CRYPTO;
  211. } else {
  212. TEST_error("Unrecognised argument");
  213. return 0;
  214. }
  215. if (!TEST_ptr(path_crypto = test_get_argument(1))
  216. || !TEST_ptr(path_ssl = test_get_argument(2)))
  217. return 0;
  218. #if defined(DSO_DLFCN) || defined(DSO_WIN32)
  219. ADD_TEST(test_lib);
  220. #endif
  221. return 1;
  222. }