libssl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 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. void *libssl_handle = NULL;
  32. static bd::Array<bd::String> my_symbols;
  33. #ifdef EGG_SSL_EXT
  34. SSL_CTX *ssl_ctx = NULL;
  35. char *tls_rand_file = NULL;
  36. #endif
  37. int ssl_use = 0; /* kyotou */
  38. static int seed_PRNG(void);
  39. static int load_symbols(void *handle) {
  40. const char *dlsym_error = NULL;
  41. DLSYM_GLOBAL(handle, SSL_get_error);
  42. DLSYM_GLOBAL(handle, SSL_connect);
  43. DLSYM_GLOBAL(handle, SSL_CTX_free);
  44. DLSYM_GLOBAL(handle, SSL_CTX_new);
  45. DLSYM_GLOBAL(handle, SSL_free);
  46. DLSYM_GLOBAL(handle, SSL_library_init);
  47. DLSYM_GLOBAL(handle, SSL_load_error_strings);
  48. DLSYM_GLOBAL(handle, SSL_new);
  49. DLSYM_GLOBAL(handle, SSL_pending);
  50. DLSYM_GLOBAL(handle, SSL_read);
  51. DLSYM_GLOBAL(handle, SSL_set_fd);
  52. DLSYM_GLOBAL(handle, SSL_shutdown);
  53. DLSYM_GLOBAL(handle, SSLv23_client_method);
  54. DLSYM_GLOBAL(handle, SSL_write);
  55. return 0;
  56. }
  57. int load_ssl() {
  58. if (ssl_ctx) {
  59. return 0;
  60. }
  61. sdprintf("Loading libssl");
  62. bd::Array<bd::String> libs_list(bd::String("libssl.so libssl.so.0.9.8 libssl.so.7 libssl.so.6").split(' '));
  63. for (size_t i = 0; i < libs_list.length(); ++i) {
  64. dlerror(); // Clear Errors
  65. libssl_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY);
  66. if (libssl_handle) {
  67. sdprintf("Found libssl: %s", bd::String(libs_list[i]).c_str());
  68. break;
  69. }
  70. }
  71. if (!libssl_handle) {
  72. sdprintf("Unable to find libssl");
  73. return(1);
  74. }
  75. load_symbols(libssl_handle);
  76. #ifdef EGG_SSL_EXT
  77. /* good place to init ssl stuff */
  78. SSL_load_error_strings();
  79. OpenSSL_add_ssl_algorithms();
  80. ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  81. if (!ssl_ctx) {
  82. sdprintf("SSL_CTX_new() failed");
  83. return 1;
  84. }
  85. if (seed_PRNG()) {
  86. sdprintf("Wasn't able to properly seed the PRNG!");
  87. return 1;
  88. }
  89. #endif
  90. return 0;
  91. }
  92. int unload_ssl() {
  93. if (libssl_handle) {
  94. #ifdef EGG_SSL_EXT
  95. /* cleanup mess when quiting */
  96. if (ssl_ctx) {
  97. SSL_CTX_free(ssl_ctx);
  98. ssl_ctx = NULL;
  99. }
  100. if (tls_rand_file)
  101. RAND_write_file(tls_rand_file);
  102. #endif
  103. // Cleanup symbol table
  104. for (size_t i = 0; i < my_symbols.length(); ++i) {
  105. dl_symbol_table.remove(my_symbols[i]);
  106. static_cast<bd::String>(my_symbols[i]).clear();
  107. }
  108. my_symbols.clear();
  109. dlclose(libssl_handle);
  110. libssl_handle = NULL;
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. #ifdef EGG_SSL_EXT
  116. static int seed_PRNG(void)
  117. {
  118. char stackdata[1024];
  119. static char rand_file[300];
  120. FILE *fh;
  121. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  122. if (RAND_status())
  123. return 0; /* PRNG already good seeded */
  124. #endif
  125. /* if the device '/dev/urandom' is present, OpenSSL uses it by default.
  126. * check if it's present, else we have to make random data ourselfs.
  127. */
  128. if ((fh = fopen("/dev/urandom", "r"))) {
  129. fclose(fh);
  130. // Try /dev/random if urandom is unavailable
  131. if ((fh = fopen("/dev/random", "r"))) {
  132. fclose(fh);
  133. return 0;
  134. }
  135. }
  136. if (RAND_file_name(rand_file, sizeof(rand_file)))
  137. tls_rand_file = rand_file;
  138. else
  139. return 1;
  140. if (!RAND_load_file(rand_file, 1024)) {
  141. /* no .rnd file found, create new seed */
  142. unsigned int c;
  143. c = time(NULL);
  144. RAND_seed(&c, sizeof(c));
  145. c = getpid();
  146. RAND_seed(&c, sizeof(c));
  147. RAND_seed(stackdata, sizeof(stackdata));
  148. }
  149. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  150. if (!RAND_status())
  151. return 2; /* PRNG still badly seeded */
  152. #endif
  153. return 0;
  154. }
  155. #endif