openssl.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ssl_ctx = SSL_CTX_new(SSLv23_client_method());
  70. if (!ssl_ctx) {
  71. sdprintf("SSL_CTX_new() failed");
  72. return 1;
  73. }
  74. // Disable insecure SSLv2
  75. SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2|SSL_OP_SINGLE_DH_USE);
  76. SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER|SSL_MODE_ENABLE_PARTIAL_WRITE);
  77. SSL_CTX_set_tmp_dh_callback(ssl_ctx, tmp_dh_callback);
  78. const char* ciphers = "HIGH:!MEDIUM:!LOW:!EXP:!SSLv2:!ADH:!aNULL:!eNULL:!NULL:@STRENGTH";
  79. if (!SSL_CTX_set_cipher_list(ssl_ctx, ciphers)) {
  80. sdprintf("Unable to load ciphers");
  81. return 1;
  82. }
  83. if (seed_PRNG()) {
  84. sdprintf("Wasn't able to properly seed the PRNG!");
  85. SSL_CTX_free(ssl_ctx);
  86. ssl_ctx = NULL;
  87. return 1;
  88. }
  89. #endif
  90. DH1080_init();
  91. return 0;
  92. }
  93. int uninit_openssl () {
  94. DH1080_uninit();
  95. #ifdef EGG_SSL_EXT
  96. /* cleanup mess when quiting */
  97. if (ssl_ctx) {
  98. SSL_CTX_free(ssl_ctx);
  99. ssl_ctx = NULL;
  100. }
  101. if (tls_rand_file)
  102. RAND_write_file(tls_rand_file);
  103. #endif
  104. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  105. ERR_free_strings();
  106. EVP_cleanup();
  107. CRYPTO_cleanup_all_ex_data();
  108. #endif
  109. unload_libssl();
  110. unload_libcrypto();
  111. return 0;
  112. }
  113. #ifdef EGG_SSL_EXT
  114. static int seed_PRNG(void)
  115. {
  116. char stackdata[1024];
  117. static char rand_file[300];
  118. FILE *fh;
  119. if (RAND_status())
  120. return 0; /* PRNG already good seeded */
  121. /* if the device '/dev/urandom' is present, OpenSSL uses it by default.
  122. * check if it's present, else we have to make random data ourselfs.
  123. */
  124. if ((fh = fopen("/dev/urandom", "r"))) {
  125. fclose(fh);
  126. // Try /dev/random if urandom is unavailable
  127. if ((fh = fopen("/dev/random", "r"))) {
  128. fclose(fh);
  129. return 0;
  130. }
  131. }
  132. if (RAND_file_name(rand_file, sizeof(rand_file)))
  133. tls_rand_file = rand_file;
  134. else
  135. return 1;
  136. if (!RAND_load_file(rand_file, 1024)) {
  137. /* no .rnd file found, create new seed */
  138. RAND_seed(&now, sizeof(time_t));
  139. RAND_seed(&conf.bot->pid, sizeof(pid_t));
  140. RAND_seed(stackdata, sizeof(stackdata));
  141. }
  142. if (!RAND_status())
  143. return 2; /* PRNG still badly seeded */
  144. return 0;
  145. }
  146. #endif
  147. /* vim: set sts=2 sw=2 ts=8 et: */