libcrypto.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "common.h"
  26. #include "main.h"
  27. #include "dl.h"
  28. #include <bdlib/src/String.h>
  29. #include <bdlib/src/Array.h>
  30. #include "libcrypto.h"
  31. #include ".defs/libcrypto_defs.cc"
  32. #ifndef OPENSSL_SHLIB_VERSION
  33. #define OPENSSL_SHLIB_VERSION_STR SHLIB_VERSION_NUMBER
  34. #else
  35. #define OPENSSL_SHLIB_VERSION_STR STRINGIFY(OPENSSL_SHLIB_VERSION)
  36. #endif
  37. void *libcrypto_handle = NULL;
  38. static bd::Array<bd::String> my_symbols;
  39. static int load_symbols(void *handle) {
  40. DLSYM_GLOBAL(handle, AES_cbc_encrypt);
  41. DLSYM_GLOBAL(handle, AES_decrypt);
  42. DLSYM_GLOBAL(handle, AES_encrypt);
  43. DLSYM_GLOBAL(handle, AES_set_decrypt_key);
  44. DLSYM_GLOBAL(handle, AES_set_encrypt_key);
  45. DLSYM_GLOBAL(handle, BF_decrypt);
  46. DLSYM_GLOBAL(handle, BF_encrypt);
  47. DLSYM_GLOBAL(handle, BF_set_key);
  48. DLSYM_GLOBAL(handle, ERR_error_string);
  49. DLSYM_GLOBAL(handle, ERR_get_error);
  50. DLSYM_GLOBAL(handle, OPENSSL_cleanse);
  51. DLSYM_GLOBAL(handle, RAND_file_name);
  52. DLSYM_GLOBAL(handle, RAND_load_file);
  53. DLSYM_GLOBAL(handle, RAND_seed);
  54. DLSYM_GLOBAL(handle, RAND_status);
  55. DLSYM_GLOBAL(handle, RAND_write_file);
  56. DLSYM_GLOBAL(handle, MD5_Final);
  57. DLSYM_GLOBAL(handle, MD5_Init);
  58. DLSYM_GLOBAL(handle, MD5_Update);
  59. DLSYM_GLOBAL(handle, SHA1_Final);
  60. DLSYM_GLOBAL(handle, SHA1_Init);
  61. DLSYM_GLOBAL(handle, SHA1_Update);
  62. DLSYM_GLOBAL(handle, SHA256_Final);
  63. DLSYM_GLOBAL(handle, SHA256_Init);
  64. DLSYM_GLOBAL(handle, SHA256_Update);
  65. DLSYM_GLOBAL(handle, BN_bin2bn);
  66. DLSYM_GLOBAL(handle, BN_bn2bin);
  67. DLSYM_GLOBAL(handle, BN_clear_free);
  68. DLSYM_GLOBAL(handle, BN_dec2bn);
  69. DLSYM_GLOBAL(handle, BN_dup);
  70. DLSYM_GLOBAL(handle, BN_hex2bn);
  71. DLSYM_GLOBAL(handle, BN_num_bits);
  72. DLSYM_GLOBAL(handle, DH_compute_key);
  73. DLSYM_GLOBAL(handle, DH_free);
  74. DLSYM_GLOBAL(handle, DH_generate_key);
  75. DLSYM_GLOBAL(handle, DH_new);
  76. DLSYM_GLOBAL(handle, DH_size);
  77. #if (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x30500000L) || \
  78. (!defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L)
  79. /* For dh_util.cc */
  80. DLSYM_GLOBAL(handle, DH_get0_key);
  81. DLSYM_GLOBAL(handle, DH_set0_key);
  82. DLSYM_GLOBAL(handle, DH_set0_pqg);
  83. DLSYM_GLOBAL(handle, BN_free);
  84. #else
  85. DLSYM_GLOBAL_FWDCOMPAT(handle, ERR_free_strings);
  86. DLSYM_GLOBAL_FWDCOMPAT(handle, EVP_cleanup);
  87. DLSYM_GLOBAL_FWDCOMPAT(handle, CRYPTO_cleanup_all_ex_data);
  88. #endif
  89. return 0;
  90. }
  91. int load_libcrypto() {
  92. if (libcrypto_handle) {
  93. return 0;
  94. }
  95. sdprintf("Loading libcrypto");
  96. bd::Array<bd::String> libs_list(bd::String("libcrypto.so." OPENSSL_SHLIB_VERSION_STR " "
  97. "libcrypto.so "
  98. "libcrypto.so.111 "
  99. "libcrypto.so.1.1 "
  100. "libcrypto.so.11 "
  101. "libcrypto.so.1.0.0 "
  102. "libcrypto.so.10 "
  103. "libcrypto.so.9 "
  104. "libcrypto.so.8 "
  105. "libcrypto.so.7 "
  106. "libcrypto.so.6 "
  107. "libcrypto.so.0.9.8").split(' '));
  108. for (size_t i = 0; i < libs_list.length(); ++i) {
  109. dlerror(); // Clear Errors
  110. libcrypto_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY|RTLD_GLOBAL);
  111. if (libcrypto_handle) {
  112. sdprintf("Found libcrypto: %s", bd::String(libs_list[i]).c_str());
  113. break;
  114. }
  115. }
  116. if (!libcrypto_handle) {
  117. fprintf(stderr, STR("Unable to find libcrypto\n"));
  118. return(1);
  119. }
  120. if (load_symbols(libcrypto_handle)) {
  121. fprintf(stderr, STR("\nMissing symbols for libcrypto (likely too old)\n"));
  122. return(1);
  123. }
  124. return 0;
  125. }
  126. int unload_libcrypto() {
  127. if (libcrypto_handle) {
  128. // Cleanup symbol table
  129. for (size_t i = 0; i < my_symbols.length(); ++i) {
  130. dl_symbol_table.remove(my_symbols[i]);
  131. static_cast<bd::String>(my_symbols[i]).clear();
  132. }
  133. my_symbols.clear();
  134. dlclose(libcrypto_handle);
  135. libcrypto_handle = NULL;
  136. return 0;
  137. }
  138. return 1;
  139. }
  140. /* vim: set sts=2 sw=2 ts=8 et: */