openssl.c 3.8 KB

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