1
0

mbedtls.c 5.5 KB

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