hssl.c 3.9 KB

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