libssl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.c"
  32. void *libssl_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, SSL_get_error);
  37. DLSYM_GLOBAL(handle, SSL_connect);
  38. DLSYM_GLOBAL(handle, SSL_CTX_free);
  39. DLSYM_GLOBAL(handle, SSL_CTX_new);
  40. DLSYM_GLOBAL(handle, SSL_free);
  41. DLSYM_GLOBAL(handle, SSL_library_init);
  42. DLSYM_GLOBAL(handle, SSL_load_error_strings);
  43. DLSYM_GLOBAL(handle, SSL_new);
  44. DLSYM_GLOBAL(handle, SSL_pending);
  45. DLSYM_GLOBAL(handle, SSL_read);
  46. DLSYM_GLOBAL(handle, SSL_set_fd);
  47. DLSYM_GLOBAL(handle, SSL_shutdown);
  48. DLSYM_GLOBAL(handle, SSLv23_client_method);
  49. DLSYM_GLOBAL(handle, SSL_write);
  50. DLSYM_GLOBAL(handle, SSL_CTX_ctrl);
  51. DLSYM_GLOBAL(handle, SSL_CTX_set_cipher_list);
  52. DLSYM_GLOBAL(handle, SSL_CTX_set_tmp_dh_callback);
  53. return 0;
  54. }
  55. int load_libssl() {
  56. if (ssl_ctx) {
  57. return 0;
  58. }
  59. sdprintf("Loading libssl");
  60. bd::Array<bd::String> libs_list(bd::String("libssl.so." SHLIB_VERSION_NUMBER " libssl.so libssl.so.0.9.8 libssl.so.7 libssl.so.6").split(' '));
  61. for (size_t i = 0; i < libs_list.length(); ++i) {
  62. dlerror(); // Clear Errors
  63. libssl_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY);
  64. if (libssl_handle) {
  65. sdprintf("Found libssl: %s", bd::String(libs_list[i]).c_str());
  66. break;
  67. }
  68. }
  69. if (!libssl_handle) {
  70. fprintf(stderr, STR("Unable to find libssl\n"));
  71. return(1);
  72. }
  73. if (load_symbols(libssl_handle)) {
  74. fprintf(stderr, STR("Missing symbols for libssl (likely too old)\n"));
  75. return(1);
  76. }
  77. return 0;
  78. }
  79. int unload_libssl() {
  80. if (libssl_handle) {
  81. // Cleanup symbol table
  82. for (size_t i = 0; i < my_symbols.length(); ++i) {
  83. dl_symbol_table.remove(my_symbols[i]);
  84. static_cast<bd::String>(my_symbols[i]).clear();
  85. }
  86. my_symbols.clear();
  87. dlclose(libssl_handle);
  88. libssl_handle = NULL;
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. /* vim: set sts=2 sw=2 ts=8 et: */