libssl.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * libssl.c -- handles:
  22. * libssl 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 "libssl.h"
  31. #include ".defs/libssl_defs.cc"
  32. void *libssl_handle = NULL;
  33. static bd::Array<bd::String> my_symbols;
  34. static int load_symbols(void *handle) {
  35. DLSYM_GLOBAL(handle, SSL_get_error);
  36. DLSYM_GLOBAL(handle, SSL_connect);
  37. DLSYM_GLOBAL(handle, SSL_CTX_free);
  38. DLSYM_GLOBAL(handle, SSL_CTX_new);
  39. DLSYM_GLOBAL(handle, SSL_free);
  40. DLSYM_GLOBAL(handle, SSL_new);
  41. DLSYM_GLOBAL(handle, SSL_pending);
  42. DLSYM_GLOBAL(handle, SSL_read);
  43. DLSYM_GLOBAL(handle, SSL_set_fd);
  44. DLSYM_GLOBAL(handle, SSL_shutdown);
  45. DLSYM_GLOBAL(handle, SSL_write);
  46. DLSYM_GLOBAL(handle, SSL_CTX_ctrl);
  47. DLSYM_GLOBAL(handle, SSL_CTX_set_cipher_list);
  48. DLSYM_GLOBAL(handle, SSL_CTX_set_tmp_dh_callback);
  49. #if defined(OPENSSL_API_COMPAT) && OPENSSL_API_COMPAT < 0x10100000L
  50. /* For SSL_library_init and SSL_load_error_strings. */
  51. DLSYM_GLOBAL(handle, OPENSSL_init_ssl);
  52. #else
  53. DLSYM_GLOBAL_FWDCOMPAT(handle, SSL_library_init);
  54. DLSYM_GLOBAL_FWDCOMPAT(handle, SSL_load_error_strings);
  55. /* Some forward-compat is handled in src/compat/openssl.cc. */
  56. #endif
  57. #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  58. /* For SSLv23_client_method */
  59. DLSYM_GLOBAL(handle, TLS_client_method);
  60. /* For SSLv23_method */
  61. DLSYM_GLOBAL(handle, TLS_method);
  62. DLSYM_GLOBAL(handle, SSL_CTX_set_options);
  63. #else
  64. DLSYM_GLOBAL_FWDCOMPAT(handle, SSLv23_client_method);
  65. /* Some forward-compat is handled in src/compat/openssl.cc. */
  66. #endif
  67. return 0;
  68. }
  69. int load_libssl() {
  70. if (ssl_ctx) {
  71. return 0;
  72. }
  73. sdprintf("Loading libssl");
  74. const auto& libs_list(bd::String("libssl.so." SHLIB_VERSION_NUMBER " libssl.so libssl.so.1.1 libssl.so.1.0.0 libssl.so.0.9.8 libssl.so.10 libssl.so.9 libssl.so.8 libssl.so.7 libssl.so.6").split(' '));
  75. for (const auto& lib : libs_list) {
  76. dlerror(); // Clear Errors
  77. libssl_handle = dlopen(lib.c_str(), RTLD_LAZY);
  78. if (libssl_handle) {
  79. sdprintf("Found libssl: %s", lib.c_str());
  80. break;
  81. }
  82. }
  83. if (!libssl_handle) {
  84. fprintf(stderr, STR("Unable to find libssl\n"));
  85. return(1);
  86. }
  87. if (load_symbols(libssl_handle)) {
  88. fprintf(stderr, STR("Missing symbols for libssl (likely too old)\n"));
  89. return(1);
  90. }
  91. return 0;
  92. }
  93. int unload_libssl() {
  94. if (libssl_handle) {
  95. // Cleanup symbol table
  96. for (const auto& symbol : my_symbols) {
  97. dl_symbol_table.remove(symbol);
  98. }
  99. my_symbols.clear();
  100. dlclose(libssl_handle);
  101. libssl_handle = NULL;
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. /* vim: set sts=2 sw=2 ts=8 et: */