openssl.c 3.9 KB

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