libcrypto.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * libcrypto.c -- handles:
  22. * libcrypto handling
  23. *
  24. */
  25. #include "buildinfo.h"
  26. #include "common.h"
  27. #include "main.h"
  28. #include "dl.h"
  29. #include <bdlib/src/String.h>
  30. #include <bdlib/src/Array.h>
  31. #include "libcrypto.h"
  32. #include ".defs/libcrypto_defs.cc"
  33. #ifndef OPENSSL_SHLIB_VERSION
  34. #define OPENSSL_SHLIB_VERSION_STR SHLIB_VERSION_NUMBER
  35. #else
  36. #define OPENSSL_SHLIB_VERSION_STR __XSTRING(OPENSSL_SHLIB_VERSION)
  37. #endif
  38. void *libcrypto_handle = NULL;
  39. static bd::Array<bd::String> my_symbols;
  40. static int load_symbols(void *handle) {
  41. DLSYM_GLOBAL(handle, AES_cbc_encrypt);
  42. DLSYM_GLOBAL(handle, AES_decrypt);
  43. DLSYM_GLOBAL(handle, AES_encrypt);
  44. DLSYM_GLOBAL(handle, AES_set_decrypt_key);
  45. DLSYM_GLOBAL(handle, AES_set_encrypt_key);
  46. DLSYM_GLOBAL(handle, BF_decrypt);
  47. DLSYM_GLOBAL(handle, BF_encrypt);
  48. DLSYM_GLOBAL(handle, BF_set_key);
  49. DLSYM_GLOBAL(handle, ERR_error_string);
  50. DLSYM_GLOBAL(handle, ERR_get_error);
  51. DLSYM_GLOBAL(handle, OPENSSL_cleanse);
  52. DLSYM_GLOBAL(handle, RAND_file_name);
  53. DLSYM_GLOBAL(handle, RAND_load_file);
  54. DLSYM_GLOBAL(handle, RAND_seed);
  55. DLSYM_GLOBAL(handle, RAND_status);
  56. DLSYM_GLOBAL(handle, RAND_write_file);
  57. DLSYM_GLOBAL(handle, MD5_Final);
  58. DLSYM_GLOBAL(handle, MD5_Init);
  59. DLSYM_GLOBAL(handle, MD5_Update);
  60. DLSYM_GLOBAL(handle, SHA1_Final);
  61. DLSYM_GLOBAL(handle, SHA1_Init);
  62. DLSYM_GLOBAL(handle, SHA1_Update);
  63. DLSYM_GLOBAL(handle, SHA256_Final);
  64. DLSYM_GLOBAL(handle, SHA256_Init);
  65. DLSYM_GLOBAL(handle, SHA256_Update);
  66. DLSYM_GLOBAL(handle, BN_bin2bn);
  67. DLSYM_GLOBAL(handle, BN_bn2bin);
  68. DLSYM_GLOBAL(handle, BN_clear_free);
  69. DLSYM_GLOBAL(handle, BN_dec2bn);
  70. DLSYM_GLOBAL(handle, BN_dup);
  71. DLSYM_GLOBAL(handle, BN_hex2bn);
  72. DLSYM_GLOBAL(handle, BN_num_bits);
  73. DLSYM_GLOBAL(handle, DH_compute_key);
  74. DLSYM_GLOBAL(handle, DH_free);
  75. DLSYM_GLOBAL(handle, DH_generate_key);
  76. DLSYM_GLOBAL(handle, DH_new);
  77. DLSYM_GLOBAL(handle, DH_size);
  78. #if (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x30500000L) || \
  79. (!defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L)
  80. /* For dh_util.cc */
  81. DLSYM_GLOBAL(handle, DH_get0_key);
  82. DLSYM_GLOBAL(handle, DH_set0_key);
  83. DLSYM_GLOBAL(handle, DH_set0_pqg);
  84. DLSYM_GLOBAL(handle, BN_free);
  85. #else
  86. DLSYM_GLOBAL_FWDCOMPAT(handle, ERR_free_strings);
  87. DLSYM_GLOBAL_FWDCOMPAT(handle, EVP_cleanup);
  88. DLSYM_GLOBAL_FWDCOMPAT(handle, CRYPTO_cleanup_all_ex_data);
  89. #endif
  90. return 0;
  91. }
  92. int load_libcrypto() {
  93. if (libcrypto_handle) {
  94. return 0;
  95. }
  96. sdprintf("Loading libcrypto");
  97. bd::Array<bd::String> libs_list(bd::String(
  98. #if !defined(LIBRESSL_VERSION_NUMBER)
  99. SSL_LIBDIR "/libcrypto.so." OPENSSL_SHLIB_VERSION_STR " "
  100. "libcrypto.so." OPENSSL_SHLIB_VERSION_STR " "
  101. #endif
  102. SSL_LIBDIR "/libcrypto.so "
  103. "libcrypto.so "
  104. "libcrypto.so.12 "
  105. "libcrypto.so.30 "
  106. "libcrypto.so.111 "
  107. "libcrypto.so.1.1 "
  108. "libcrypto.so.11 "
  109. "libcrypto.so.1.0.0 "
  110. "libcrypto.so.10 "
  111. "libcrypto.so.9 "
  112. "libcrypto.so.8 "
  113. "libcrypto.so.7 "
  114. "libcrypto.so.6 "
  115. "libcrypto.so.0.9.8").split(' '));
  116. for (size_t i = 0; i < libs_list.length(); ++i) {
  117. dlerror(); // Clear Errors
  118. libcrypto_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY|RTLD_GLOBAL);
  119. if (libcrypto_handle) {
  120. sdprintf("Found libcrypto: %s", bd::String(libs_list[i]).c_str());
  121. break;
  122. }
  123. }
  124. if (!libcrypto_handle) {
  125. fprintf(stderr, STR("Unable to find libcrypto\n"));
  126. return(1);
  127. }
  128. if (load_symbols(libcrypto_handle)) {
  129. fprintf(stderr, STR("\nMissing symbols for libcrypto (likely too old)\n"));
  130. return(1);
  131. }
  132. return 0;
  133. }
  134. int unload_libcrypto() {
  135. if (libcrypto_handle) {
  136. // Cleanup symbol table
  137. for (size_t i = 0; i < my_symbols.length(); ++i) {
  138. dl_symbol_table.remove(my_symbols[i]);
  139. static_cast<bd::String>(my_symbols[i]).clear();
  140. }
  141. my_symbols.clear();
  142. dlclose(libcrypto_handle);
  143. libcrypto_handle = NULL;
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. /* vim: set sts=2 sw=2 ts=8 et: */