mbedtls.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "hssl.h"
  2. #ifdef WITH_MBEDTLS
  3. #include "mbedtls/version.h"
  4. #include "mbedtls/entropy.h"
  5. #include "mbedtls/ctr_drbg.h"
  6. #include "mbedtls/x509.h"
  7. #include "mbedtls/ssl.h"
  8. #include "mbedtls/net_sockets.h"
  9. #include "mbedtls/error.h"
  10. #include "mbedtls/debug.h"
  11. #if defined(MBEDTLS_SSL_CACHE_C)
  12. #include "mbedtls/ssl_cache.h"
  13. #endif
  14. #ifdef _MSC_VER
  15. //#pragma comment(lib, "libmbedtls.a")
  16. //#pragma comment(lib, "libmbedx509.a")
  17. //#pragma comment(lib, "libmbedcrypto.a")
  18. #endif
  19. const char* hssl_backend() {
  20. return "mbedtls";
  21. }
  22. struct mbedtls_ctx {
  23. mbedtls_entropy_context entropy;
  24. mbedtls_ctr_drbg_context ctr_drbg;
  25. mbedtls_ssl_config conf;
  26. mbedtls_x509_crt cert;
  27. mbedtls_pk_context pkey;
  28. #if defined(MBEDTLS_SSL_CACHE_C)
  29. mbedtls_ssl_cache_context cache;
  30. #endif
  31. };
  32. hssl_ctx_t hssl_ctx_new(hssl_ctx_opt_t* param) {
  33. struct mbedtls_ctx* ctx = (struct mbedtls_ctx*)malloc(sizeof(struct mbedtls_ctx));
  34. if (ctx == NULL) return NULL;
  35. mbedtls_ssl_config_init(&ctx->conf);
  36. #if defined(MBEDTLS_SSL_CACHE_C)
  37. mbedtls_ssl_cache_init(&ctx->cache);
  38. #endif
  39. mbedtls_x509_crt_init(&ctx->cert);
  40. mbedtls_pk_init(&ctx->pkey);
  41. mbedtls_entropy_init(&ctx->entropy);
  42. mbedtls_ctr_drbg_init(&ctx->ctr_drbg);
  43. int mode = MBEDTLS_SSL_VERIFY_NONE;
  44. int endpoint = MBEDTLS_SSL_IS_CLIENT;
  45. bool check = false;
  46. if (param) {
  47. if (param->crt_file && *param->crt_file) {
  48. if (mbedtls_x509_crt_parse_file(&ctx->cert, param->crt_file) != 0) {
  49. fprintf(stderr, "ssl crt_file error!\n");
  50. goto error;
  51. }
  52. }
  53. if (param->key_file && *param->key_file) {
  54. #if MBEDTLS_VERSION_MAJOR >= 3
  55. if (mbedtls_pk_parse_keyfile(&ctx->pkey, param->key_file, NULL, NULL, NULL) != 0) {
  56. #else
  57. if (mbedtls_pk_parse_keyfile(&ctx->pkey, param->key_file, NULL) != 0) {
  58. #endif
  59. fprintf(stderr, "ssl key_file error!\n");
  60. goto error;
  61. }
  62. check = true;
  63. }
  64. if (param->verify_peer) {
  65. mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  66. }
  67. if (param->endpoint == HSSL_SERVER) {
  68. endpoint = MBEDTLS_SSL_IS_SERVER;
  69. }
  70. }
  71. mbedtls_ctr_drbg_seed(&ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy, NULL, 0);
  72. if (mbedtls_ssl_config_defaults(&ctx->conf, endpoint,
  73. MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
  74. fprintf(stderr, "ssl config error!\n");
  75. goto error;
  76. }
  77. mbedtls_ssl_conf_authmode(&ctx->conf, mode);
  78. mbedtls_ssl_conf_rng(&ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg);
  79. #if defined(MBEDTLS_SSL_CACHE_C)
  80. mbedtls_ssl_conf_session_cache(&ctx->conf, &ctx->cache, mbedtls_ssl_cache_get, mbedtls_ssl_cache_set);
  81. #endif
  82. if (check) {
  83. mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
  84. if (mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->pkey) != 0) {
  85. fprintf(stderr, "ssl key_file check failed!\n");
  86. goto error;
  87. }
  88. }
  89. return ctx;
  90. error:
  91. free(ctx);
  92. return NULL;
  93. }
  94. void hssl_ctx_free(hssl_ctx_t ssl_ctx) {
  95. if (!ssl_ctx) return;
  96. struct mbedtls_ctx *mctx = (struct mbedtls_ctx *)ssl_ctx;
  97. mbedtls_x509_crt_free(&mctx->cert);
  98. mbedtls_pk_free(&mctx->pkey);
  99. mbedtls_ssl_config_free(&mctx->conf);
  100. #if defined(MBEDTLS_SSL_CACHE_C)
  101. mbedtls_ssl_cache_free(&mctx->cache);
  102. #endif
  103. mbedtls_ctr_drbg_free(&mctx->ctr_drbg);
  104. mbedtls_entropy_free(&mctx->entropy);
  105. free(mctx);
  106. }
  107. static int __mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len) {
  108. int fd = (intptr_t)ctx;
  109. // int n = write(fd, buf, len);
  110. int n = send(fd, (char*)(buf), (int)(len), 0);
  111. if (n >= 0) return n;
  112. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_WRITE : -1);
  113. }
  114. static int __mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len) {
  115. int fd = (intptr_t)ctx;
  116. // int n = read(fd, buf, len);
  117. int n = recv(fd, (char*)(buf), (int)(len), 0);
  118. if (n >= 0) return n;
  119. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_READ : -1);
  120. }
  121. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  122. struct mbedtls_ctx* mctx = (struct mbedtls_ctx*)ssl_ctx;
  123. mbedtls_ssl_context* ssl = (mbedtls_ssl_context*)malloc(sizeof(mbedtls_ssl_context));
  124. if (ssl == NULL) return NULL;
  125. mbedtls_ssl_init(ssl);
  126. mbedtls_ssl_setup(ssl, &mctx->conf);
  127. mbedtls_ssl_set_bio(ssl, (void*)(intptr_t)fd, __mbedtls_net_send, __mbedtls_net_recv, NULL);
  128. return ssl;
  129. }
  130. void hssl_free(hssl_t ssl) {
  131. if (ssl) {
  132. mbedtls_ssl_free(ssl);
  133. ssl = NULL;
  134. }
  135. }
  136. static int hssl_handshake(hssl_t ssl) {
  137. int ret = mbedtls_ssl_handshake(ssl);
  138. if (ret != 0) {
  139. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  140. return HSSL_WANT_READ;
  141. }
  142. else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  143. return HSSL_WANT_WRITE;
  144. }
  145. }
  146. return ret;
  147. }
  148. int hssl_accept(hssl_t ssl) {
  149. return hssl_handshake(ssl);
  150. }
  151. int hssl_connect(hssl_t ssl) {
  152. return hssl_handshake(ssl);
  153. }
  154. int hssl_read(hssl_t ssl, void* buf, int len) {
  155. return mbedtls_ssl_read(ssl, buf, len);
  156. }
  157. int hssl_write(hssl_t ssl, const void* buf, int len) {
  158. return mbedtls_ssl_write(ssl, buf, len);
  159. }
  160. int hssl_close(hssl_t ssl) {
  161. return 0;
  162. }
  163. int hssl_set_sni_hostname(hssl_t ssl, const char* hostname) {
  164. #ifdef MBEDTLS_X509_CRT_PARSE_C
  165. mbedtls_ssl_set_hostname(ssl, hostname);
  166. #endif
  167. return 0;
  168. }
  169. #endif // WITH_MBEDTLS