openssl.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * openssl.c -- handles:
  22. * libcrypto / libssl handling
  23. *
  24. */
  25. #include "common.h"
  26. #include "main.h"
  27. #include "dl.h"
  28. #include "shell.h"
  29. #include <bdlib/src/String.h>
  30. #include <bdlib/src/Array.h>
  31. #include "libssl.h"
  32. #include "libcrypto.h"
  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. #include "dhparam.cc"
  40. static DH* tmp_dh_callback(SSL* ssl, int is_export, int keylength) {
  41. DH *ret = NULL;
  42. switch (keylength) {
  43. case 4096:
  44. ret = get_dh4096();
  45. break;
  46. case 2048:
  47. ret = get_dh2048();
  48. break;
  49. case 1024:
  50. ret = get_dh1024();
  51. break;
  52. case 512:
  53. ret = get_dh512();
  54. break;
  55. default:
  56. putlog(LOG_DEBUG, "*", "Missing DH key length: %d", keylength);
  57. break;
  58. }
  59. return ret;
  60. }
  61. int init_openssl() {
  62. if (load_libcrypto() || load_libssl()) {
  63. werr(ERR_LIBS);
  64. }
  65. #ifdef EGG_SSL_EXT
  66. /* good place to init ssl stuff */
  67. SSL_load_error_strings();
  68. OpenSSL_add_ssl_algorithms();
  69. #if (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER > 0x20020002L) || \
  70. (!defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L)
  71. ssl_ctx = SSL_CTX_new(TLS_client_method());
  72. #else
  73. ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  74. #endif
  75. if (!ssl_ctx) {
  76. sdprintf("SSL_CTX_new() failed");
  77. return 1;
  78. }
  79. // Disable insecure SSLv2
  80. SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE);
  81. SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER|SSL_MODE_ENABLE_PARTIAL_WRITE);
  82. SSL_CTX_set_tmp_dh_callback(ssl_ctx, tmp_dh_callback);
  83. const char* ciphers = "HIGH:!MEDIUM:!LOW:!EXP:!SSLv2:!ADH:!aNULL:!eNULL:!NULL:@STRENGTH";
  84. if (!SSL_CTX_set_cipher_list(ssl_ctx, ciphers)) {
  85. sdprintf("Unable to load ciphers");
  86. return 1;
  87. }
  88. if (seed_PRNG()) {
  89. sdprintf("Wasn't able to properly seed the PRNG!");
  90. SSL_CTX_free(ssl_ctx);
  91. ssl_ctx = NULL;
  92. return 1;
  93. }
  94. #endif
  95. DH1080_init();
  96. return 0;
  97. }
  98. int uninit_openssl () {
  99. DH1080_uninit();
  100. #ifdef EGG_SSL_EXT
  101. /* cleanup mess when quiting */
  102. if (ssl_ctx) {
  103. SSL_CTX_free(ssl_ctx);
  104. ssl_ctx = NULL;
  105. }
  106. if (tls_rand_file)
  107. RAND_write_file(tls_rand_file);
  108. #endif
  109. /* Macro from src/libcrypto.cc */
  110. #if !((defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x30500000L) || \
  111. (!defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L))
  112. ERR_free_strings();
  113. EVP_cleanup();
  114. CRYPTO_cleanup_all_ex_data();
  115. #endif
  116. unload_libssl();
  117. unload_libcrypto();
  118. return 0;
  119. }
  120. #ifdef EGG_SSL_EXT
  121. static int seed_PRNG(void)
  122. {
  123. char stackdata[1024];
  124. static char rand_file[300];
  125. FILE *fh;
  126. if (RAND_status())
  127. return 0; /* PRNG already good seeded */
  128. /* if the device '/dev/urandom' is present, OpenSSL uses it by default.
  129. * check if it's present, else we have to make random data ourselfs.
  130. */
  131. if ((fh = fopen("/dev/urandom", "r"))) {
  132. fclose(fh);
  133. // Try /dev/random if urandom is unavailable
  134. if ((fh = fopen("/dev/random", "r"))) {
  135. fclose(fh);
  136. return 0;
  137. }
  138. }
  139. if (RAND_file_name(rand_file, sizeof(rand_file)))
  140. tls_rand_file = rand_file;
  141. else
  142. return 1;
  143. if (!RAND_load_file(rand_file, 1024)) {
  144. /* no .rnd file found, create new seed */
  145. RAND_seed(&now, sizeof(time_t));
  146. RAND_seed(&conf.bot->pid, sizeof(pid_t));
  147. RAND_seed(stackdata, sizeof(stackdata));
  148. }
  149. if (!RAND_status())
  150. return 2; /* PRNG still badly seeded */
  151. return 0;
  152. }
  153. #endif
  154. /* vim: set sts=2 sw=2 ts=8 et: */