1
0

openssl.c 3.6 KB

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