libcrypto.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  78. /* For dh_util.cc */
  79. DLSYM_GLOBAL(handle, DH_get0_key);
  80. DLSYM_GLOBAL(handle, DH_set0_key);
  81. DLSYM_GLOBAL(handle, DH_set0_pqg);
  82. DLSYM_GLOBAL(handle, BN_free);
  83. #else
  84. DLSYM_GLOBAL_FWDCOMPAT(handle, ERR_free_strings);
  85. DLSYM_GLOBAL_FWDCOMPAT(handle, EVP_cleanup);
  86. DLSYM_GLOBAL_FWDCOMPAT(handle, CRYPTO_cleanup_all_ex_data);
  87. #endif
  88. return 0;
  89. }
  90. int load_libcrypto() {
  91. if (libcrypto_handle) {
  92. return 0;
  93. }
  94. sdprintf("Loading libcrypto");
  95. const auto& libs_list(bd::String("libcrypto.so." OPENSSL_SHLIB_VERSION_STR " libcrypto.so libcrypto.so.1.1 libcrypto.so.1.0.0 libcrypto.so.0.9.8 libcrypto.so.10 libcrypto.so.9 libcrypto.so.8 libcrypto.so.7 libcrypto.so.6").split(' '));
  96. for (const auto& lib : libs_list) {
  97. dlerror(); // Clear Errors
  98. libcrypto_handle = dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
  99. if (libcrypto_handle) {
  100. sdprintf("Found libcrypto: %s", lib.c_str());
  101. break;
  102. }
  103. }
  104. if (!libcrypto_handle) {
  105. fprintf(stderr, STR("Unable to find libcrypto\n"));
  106. return(1);
  107. }
  108. if (load_symbols(libcrypto_handle)) {
  109. fprintf(stderr, STR("Missing symbols for libcrypto (likely too old)\n"));
  110. return(1);
  111. }
  112. return 0;
  113. }
  114. int unload_libcrypto() {
  115. if (libcrypto_handle) {
  116. // Cleanup symbol table
  117. for (const auto& symbol : my_symbols) {
  118. dl_symbol_table.remove(symbol);
  119. }
  120. my_symbols.clear();
  121. dlclose(libcrypto_handle);
  122. libcrypto_handle = NULL;
  123. return 0;
  124. }
  125. return 1;
  126. }
  127. /* vim: set sts=2 sw=2 ts=8 et: */