libssl.cc 3.9 KB

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