shlibloadtest.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. *lib = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
  42. return *lib == NULL ? 0 : 1;
  43. }
  44. static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
  45. {
  46. *sym = dlsym(lib, symname);
  47. return *sym != NULL;
  48. }
  49. static int shlib_close(SHLIB lib)
  50. {
  51. return dlclose(lib) != 0 ? 0 : 1;
  52. }
  53. #endif
  54. #ifdef DSO_WIN32
  55. # include <windows.h>
  56. # define SHLIB_INIT 0
  57. typedef HINSTANCE SHLIB;
  58. typedef void *SHLIB_SYM;
  59. static int shlib_load(const char *filename, SHLIB *lib)
  60. {
  61. *lib = LoadLibraryA(filename);
  62. return *lib == NULL ? 0 : 1;
  63. }
  64. static int shlib_sym(SHLIB lib, const char *symname, SHLIB_SYM *sym)
  65. {
  66. *sym = (SHLIB_SYM)GetProcAddress(lib, symname);
  67. return *sym != NULL;
  68. }
  69. static int shlib_close(SHLIB lib)
  70. {
  71. return FreeLibrary(lib) == 0 ? 0 : 1;
  72. }
  73. #endif
  74. #if defined(DSO_DLFCN) || defined(DSO_WIN32)
  75. static int test_lib(void)
  76. {
  77. SHLIB ssllib = SHLIB_INIT;
  78. SHLIB cryptolib = SHLIB_INIT;
  79. SSL_CTX *ctx;
  80. union {
  81. void (*func)(void);
  82. SHLIB_SYM sym;
  83. } symbols[3];
  84. TLS_method_t myTLS_method;
  85. SSL_CTX_new_t mySSL_CTX_new;
  86. SSL_CTX_free_t mySSL_CTX_free;
  87. ERR_get_error_t myERR_get_error;
  88. OpenSSL_version_num_t myOpenSSL_version_num;
  89. int result = 0;
  90. switch (test_type) {
  91. case JUST_CRYPTO:
  92. if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
  93. goto end;
  94. break;
  95. case CRYPTO_FIRST:
  96. if (!TEST_true(shlib_load(path_crypto, &cryptolib))
  97. || !TEST_true(shlib_load(path_ssl, &ssllib)))
  98. goto end;
  99. break;
  100. case SSL_FIRST:
  101. if (!TEST_true(shlib_load(path_ssl, &ssllib))
  102. || !TEST_true(shlib_load(path_crypto, &cryptolib)))
  103. goto end;
  104. break;
  105. case DSO_REFTEST:
  106. if (!TEST_true(shlib_load(path_crypto, &cryptolib)))
  107. goto end;
  108. break;
  109. }
  110. if (test_type != JUST_CRYPTO && test_type != DSO_REFTEST) {
  111. if (!TEST_true(shlib_sym(ssllib, "TLS_method", &symbols[0].sym))
  112. || !TEST_true(shlib_sym(ssllib, "SSL_CTX_new", &symbols[1].sym))
  113. || !TEST_true(shlib_sym(ssllib, "SSL_CTX_free", &symbols[2].sym)))
  114. goto end;
  115. myTLS_method = (TLS_method_t)symbols[0].func;
  116. mySSL_CTX_new = (SSL_CTX_new_t)symbols[1].func;
  117. mySSL_CTX_free = (SSL_CTX_free_t)symbols[2].func;
  118. if (!TEST_ptr(ctx = mySSL_CTX_new(myTLS_method())))
  119. goto end;
  120. mySSL_CTX_free(ctx);
  121. }
  122. if (!TEST_true(shlib_sym(cryptolib, "ERR_get_error", &symbols[0].sym))
  123. || !TEST_true(shlib_sym(cryptolib, "OpenSSL_version_num",
  124. &symbols[1].sym)))
  125. goto end;
  126. myERR_get_error = (ERR_get_error_t)symbols[0].func;
  127. if (!TEST_int_eq(myERR_get_error(), 0))
  128. goto end;
  129. /*
  130. * The bits that COMPATIBILITY_MASK lets through MUST be the same in
  131. * the library and in the application.
  132. * The bits that are masked away MUST be a larger or equal number in
  133. * the library compared to the application.
  134. */
  135. # define COMPATIBILITY_MASK 0xfff00000L
  136. myOpenSSL_version_num = (OpenSSL_version_num_t)symbols[1].func;
  137. if (!TEST_int_eq(myOpenSSL_version_num() & COMPATIBILITY_MASK,
  138. OPENSSL_VERSION_NUMBER & COMPATIBILITY_MASK))
  139. goto end;
  140. if (!TEST_int_ge(myOpenSSL_version_num() & ~COMPATIBILITY_MASK,
  141. OPENSSL_VERSION_NUMBER & ~COMPATIBILITY_MASK))
  142. goto end;
  143. if (test_type == DSO_REFTEST) {
  144. # ifdef DSO_DLFCN
  145. DSO_dsobyaddr_t myDSO_dsobyaddr;
  146. DSO_free_t myDSO_free;
  147. /*
  148. * This is resembling the code used in ossl_init_base() and
  149. * OPENSSL_atexit() to block unloading the library after dlclose().
  150. * We are not testing this on Windows, because it is done there in a
  151. * completely different way. Especially as a call to DSO_dsobyaddr()
  152. * will always return an error, because DSO_pathbyaddr() is not
  153. * implemented there.
  154. */
  155. if (!TEST_true(shlib_sym(cryptolib, "DSO_dsobyaddr", &symbols[0].sym))
  156. || !TEST_true(shlib_sym(cryptolib, "DSO_free",
  157. &symbols[1].sym)))
  158. goto end;
  159. myDSO_dsobyaddr = (DSO_dsobyaddr_t)symbols[0].func;
  160. myDSO_free = (DSO_free_t)symbols[1].func;
  161. {
  162. DSO *hndl;
  163. /* use known symbol from crypto module */
  164. if (!TEST_ptr(hndl = myDSO_dsobyaddr((void (*)(void))ERR_get_error, 0)))
  165. goto end;
  166. myDSO_free(hndl);
  167. }
  168. # endif /* DSO_DLFCN */
  169. }
  170. switch (test_type) {
  171. case JUST_CRYPTO:
  172. if (!TEST_true(shlib_close(cryptolib)))
  173. goto end;
  174. break;
  175. case CRYPTO_FIRST:
  176. if (!TEST_true(shlib_close(cryptolib))
  177. || !TEST_true(shlib_close(ssllib)))
  178. goto end;
  179. break;
  180. case SSL_FIRST:
  181. if (!TEST_true(shlib_close(ssllib))
  182. || !TEST_true(shlib_close(cryptolib)))
  183. goto end;
  184. break;
  185. case DSO_REFTEST:
  186. if (!TEST_true(shlib_close(cryptolib)))
  187. goto end;
  188. break;
  189. }
  190. result = 1;
  191. end:
  192. return result;
  193. }
  194. #endif
  195. int setup_tests(void)
  196. {
  197. const char *p = test_get_argument(0);
  198. if (strcmp(p, "-crypto_first") == 0) {
  199. test_type = CRYPTO_FIRST;
  200. } else if (strcmp(p, "-ssl_first") == 0) {
  201. test_type = SSL_FIRST;
  202. } else if (strcmp(p, "-just_crypto") == 0) {
  203. test_type = JUST_CRYPTO;
  204. } else if (strcmp(p, "-dso_ref") == 0) {
  205. test_type = JUST_CRYPTO;
  206. } else {
  207. TEST_error("Unrecognised argument");
  208. return 0;
  209. }
  210. if (!TEST_ptr(path_crypto = test_get_argument(1))
  211. || !TEST_ptr(path_ssl = test_get_argument(2)))
  212. return 0;
  213. #if defined(DSO_DLFCN) || defined(DSO_WIN32)
  214. ADD_TEST(test_lib);
  215. #endif
  216. return 1;
  217. }