libcrypto.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.c"
  32. void *libcrypto_handle = NULL;
  33. static bd::Array<bd::String> my_symbols;
  34. static int load_symbols(void *handle) {
  35. const char *dlsym_error = NULL;
  36. DLSYM_GLOBAL(handle, AES_cbc_encrypt);
  37. DLSYM_GLOBAL(handle, AES_decrypt);
  38. DLSYM_GLOBAL(handle, AES_encrypt);
  39. DLSYM_GLOBAL(handle, AES_set_decrypt_key);
  40. DLSYM_GLOBAL(handle, AES_set_encrypt_key);
  41. DLSYM_GLOBAL(handle, BF_decrypt);
  42. DLSYM_GLOBAL(handle, BF_encrypt);
  43. DLSYM_GLOBAL(handle, BF_set_key);
  44. DLSYM_GLOBAL(handle, ERR_error_string);
  45. DLSYM_GLOBAL(handle, ERR_free_strings);
  46. DLSYM_GLOBAL(handle, ERR_get_error);
  47. DLSYM_GLOBAL(handle, OPENSSL_cleanse);
  48. DLSYM_GLOBAL(handle, RAND_file_name);
  49. DLSYM_GLOBAL(handle, RAND_load_file);
  50. DLSYM_GLOBAL(handle, RAND_seed);
  51. DLSYM_GLOBAL(handle, RAND_status);
  52. DLSYM_GLOBAL(handle, RAND_write_file);
  53. DLSYM_GLOBAL(handle, MD5_Final);
  54. DLSYM_GLOBAL(handle, MD5_Init);
  55. DLSYM_GLOBAL(handle, MD5_Update);
  56. DLSYM_GLOBAL(handle, SHA1_Final);
  57. DLSYM_GLOBAL(handle, SHA1_Init);
  58. DLSYM_GLOBAL(handle, SHA1_Update);
  59. DLSYM_GLOBAL(handle, SHA256_Final);
  60. DLSYM_GLOBAL(handle, SHA256_Init);
  61. DLSYM_GLOBAL(handle, SHA256_Update);
  62. DLSYM_GLOBAL(handle, BN_bin2bn);
  63. DLSYM_GLOBAL(handle, BN_bn2bin);
  64. DLSYM_GLOBAL(handle, BN_clear_free);
  65. DLSYM_GLOBAL(handle, BN_dec2bn);
  66. DLSYM_GLOBAL(handle, BN_dup);
  67. DLSYM_GLOBAL(handle, BN_hex2bn);
  68. DLSYM_GLOBAL(handle, BN_num_bits);
  69. DLSYM_GLOBAL(handle, DH_compute_key);
  70. DLSYM_GLOBAL(handle, DH_free);
  71. DLSYM_GLOBAL(handle, DH_generate_key);
  72. DLSYM_GLOBAL(handle, DH_new);
  73. DLSYM_GLOBAL(handle, DH_size);
  74. DLSYM_GLOBAL(handle, EVP_cleanup);
  75. DLSYM_GLOBAL(handle, CRYPTO_cleanup_all_ex_data);
  76. return 0;
  77. }
  78. int load_libcrypto() {
  79. if (libcrypto_handle) {
  80. return 0;
  81. }
  82. sdprintf("Loading libcrypto");
  83. bd::Array<bd::String> libs_list(bd::String("libcrypto.so." SHLIB_VERSION_NUMBER " libcrypto.so libcrypto.so.0.9.8 libcrypto.so.7 libcrypto.so.6").split(' '));
  84. for (size_t i = 0; i < libs_list.length(); ++i) {
  85. dlerror(); // Clear Errors
  86. libcrypto_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY|RTLD_GLOBAL);
  87. if (libcrypto_handle) {
  88. sdprintf("Found libcrypto: %s", bd::String(libs_list[i]).c_str());
  89. break;
  90. }
  91. }
  92. if (!libcrypto_handle) {
  93. fprintf(stderr, STR("Unable to find libcrypto\n"));
  94. return(1);
  95. }
  96. if (load_symbols(libcrypto_handle)) {
  97. fprintf(stderr, STR("Missing symbols for libcrypto (likely too old)\n"));
  98. return(1);
  99. }
  100. return 0;
  101. }
  102. int unload_libcrypto() {
  103. if (libcrypto_handle) {
  104. // Cleanup symbol table
  105. for (size_t i = 0; i < my_symbols.length(); ++i) {
  106. dl_symbol_table.remove(my_symbols[i]);
  107. static_cast<bd::String>(my_symbols[i]).clear();
  108. }
  109. my_symbols.clear();
  110. dlclose(libcrypto_handle);
  111. libcrypto_handle = NULL;
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. /* vim: set sts=2 sw=2 ts=8 et: */