openssl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_new(hssl_ctx_opt_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. #ifdef SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
  69. SSL_CTX_set_mode(ctx, SSL_CTX_get_mode(ctx) | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  70. #endif
  71. SSL_CTX_set_verify(ctx, mode, NULL);
  72. return ctx;
  73. error:
  74. SSL_CTX_free(ctx);
  75. return NULL;
  76. }
  77. void hssl_ctx_free(hssl_ctx_t ssl_ctx) {
  78. if (!ssl_ctx) return;
  79. SSL_CTX_free((SSL_CTX*)ssl_ctx);
  80. }
  81. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  82. SSL* ssl = SSL_new((SSL_CTX*)ssl_ctx);
  83. if (ssl == NULL) return NULL;
  84. SSL_set_fd(ssl, fd);
  85. return ssl;
  86. }
  87. void hssl_free(hssl_t ssl) {
  88. if (ssl) {
  89. SSL_free((SSL*)ssl);
  90. ssl = NULL;
  91. }
  92. }
  93. int hssl_accept(hssl_t ssl) {
  94. int ret = SSL_accept((SSL*)ssl);
  95. if (ret == 1) return 0;
  96. int err = SSL_get_error((SSL*)ssl, ret);
  97. if (err == SSL_ERROR_WANT_READ) {
  98. return HSSL_WANT_READ;
  99. }
  100. else if (err == SSL_ERROR_WANT_WRITE) {
  101. return HSSL_WANT_WRITE;
  102. }
  103. return err;
  104. }
  105. int hssl_connect(hssl_t ssl) {
  106. int ret = SSL_connect((SSL*)ssl);
  107. if (ret == 1) return 0;
  108. int err = SSL_get_error((SSL*)ssl, ret);
  109. if (err == SSL_ERROR_WANT_READ) {
  110. return HSSL_WANT_READ;
  111. }
  112. else if (err == SSL_ERROR_WANT_WRITE) {
  113. return HSSL_WANT_WRITE;
  114. }
  115. return err;
  116. }
  117. int hssl_read(hssl_t ssl, void* buf, int len) {
  118. return SSL_read((SSL*)ssl, buf, len);
  119. }
  120. int hssl_write(hssl_t ssl, const void* buf, int len) {
  121. return SSL_write((SSL*)ssl, buf, len);
  122. }
  123. int hssl_close(hssl_t ssl) {
  124. SSL_shutdown((SSL*)ssl);
  125. return 0;
  126. }
  127. int hssl_set_sni_hostname(hssl_t ssl, const char* hostname) {
  128. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
  129. SSL_set_tlsext_host_name((SSL*)ssl, hostname);
  130. #endif
  131. return 0;
  132. }
  133. #endif // WITH_OPENSSL