openssl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "hssl.h"
  2. #ifdef WITH_OPENSSL
  3. #include "openssl/ssl.h"
  4. #include "openssl/err.h"
  5. #ifdef _MSC_VER
  6. //#pragma comment(lib, "libssl.a")
  7. //#pragma comment(lib, "libcrypto.a")
  8. #endif
  9. const char* hssl_backend() {
  10. return "openssl";
  11. }
  12. hssl_ctx_t hssl_ctx_init(hssl_ctx_init_param_t* param) {
  13. static int s_initialized = 0;
  14. if (s_initialized == 0) {
  15. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  16. SSL_library_init();
  17. SSL_load_error_strings();
  18. #else
  19. OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, NULL);
  20. #endif
  21. s_initialized = 1;
  22. }
  23. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  24. SSL_CTX* ctx = SSL_CTX_new(SSLv23_method());
  25. #else
  26. SSL_CTX* ctx = SSL_CTX_new(TLS_method());
  27. #endif
  28. if (ctx == NULL) return NULL;
  29. int mode = SSL_VERIFY_NONE;
  30. const char* ca_file = NULL;
  31. const char* ca_path = NULL;
  32. if (param) {
  33. if (param->ca_file && *param->ca_file) {
  34. ca_file = param->ca_file;
  35. }
  36. if (param->ca_path && *param->ca_path) {
  37. ca_path = param->ca_path;
  38. }
  39. if (ca_file || ca_path) {
  40. if (!SSL_CTX_load_verify_locations(ctx, ca_file, ca_path)) {
  41. fprintf(stderr, "ssl ca_file/ca_path failed!\n");
  42. goto error;
  43. }
  44. }
  45. if (param->crt_file && *param->crt_file) {
  46. if (!SSL_CTX_use_certificate_file(ctx, param->crt_file, SSL_FILETYPE_PEM)) {
  47. fprintf(stderr, "ssl crt_file error!\n");
  48. goto error;
  49. }
  50. }
  51. if (param->key_file && *param->key_file) {
  52. if (!SSL_CTX_use_PrivateKey_file(ctx, param->key_file, SSL_FILETYPE_PEM)) {
  53. fprintf(stderr, "ssl key_file error!\n");
  54. goto error;
  55. }
  56. if (!SSL_CTX_check_private_key(ctx)) {
  57. fprintf(stderr, "ssl key_file check failed!\n");
  58. goto error;
  59. }
  60. }
  61. if (param->verify_peer) {
  62. mode = SSL_VERIFY_PEER;
  63. }
  64. }
  65. if (mode == SSL_VERIFY_PEER && !ca_file && !ca_path) {
  66. SSL_CTX_set_default_verify_paths(ctx);
  67. }
  68. SSL_CTX_set_verify(ctx, mode, NULL);
  69. g_ssl_ctx = ctx;
  70. return ctx;
  71. error:
  72. SSL_CTX_free(ctx);
  73. return NULL;
  74. }
  75. void hssl_ctx_cleanup(hssl_ctx_t ssl_ctx) {
  76. if (!ssl_ctx) return;
  77. if (g_ssl_ctx == ssl_ctx) {
  78. g_ssl_ctx = NULL;
  79. }
  80. SSL_CTX_free((SSL_CTX*)ssl_ctx);
  81. }
  82. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  83. SSL* ssl = SSL_new((SSL_CTX*)ssl_ctx);
  84. if (ssl == NULL) return NULL;
  85. SSL_set_fd(ssl, fd);
  86. return ssl;
  87. }
  88. void hssl_free(hssl_t ssl) {
  89. if (ssl) {
  90. SSL_free((SSL*)ssl);
  91. ssl = NULL;
  92. }
  93. }
  94. int hssl_accept(hssl_t ssl) {
  95. int ret = SSL_accept((SSL*)ssl);
  96. if (ret == 1) return 0;
  97. int err = SSL_get_error((SSL*)ssl, ret);
  98. if (err == SSL_ERROR_WANT_READ) {
  99. return HSSL_WANT_READ;
  100. }
  101. else if (err == SSL_ERROR_WANT_WRITE) {
  102. return HSSL_WANT_WRITE;
  103. }
  104. return err;
  105. }
  106. int hssl_connect(hssl_t ssl) {
  107. int ret = SSL_connect((SSL*)ssl);
  108. if (ret == 1) return 0;
  109. int err = SSL_get_error((SSL*)ssl, ret);
  110. if (err == SSL_ERROR_WANT_READ) {
  111. return HSSL_WANT_READ;
  112. }
  113. else if (err == SSL_ERROR_WANT_WRITE) {
  114. return HSSL_WANT_WRITE;
  115. }
  116. return err;
  117. }
  118. int hssl_read(hssl_t ssl, void* buf, int len) {
  119. return SSL_read((SSL*)ssl, buf, len);
  120. }
  121. int hssl_write(hssl_t ssl, const void* buf, int len) {
  122. return SSL_write((SSL*)ssl, buf, len);
  123. }
  124. int hssl_close(hssl_t ssl) {
  125. SSL_shutdown((SSL*)ssl);
  126. return 0;
  127. }
  128. int hssl_set_sni_hostname(hssl_t ssl, const char* hostname) {
  129. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
  130. SSL_set_tlsext_host_name((SSL*)ssl, hostname);
  131. #endif
  132. return 0;
  133. }
  134. #endif // WITH_OPENSSL