mbedtls.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "hssl.h"
  2. #ifdef WITH_MBEDTLS
  3. #include "mbedtls/entropy.h"
  4. #include "mbedtls/ctr_drbg.h"
  5. #include "mbedtls/certs.h"
  6. #include "mbedtls/x509.h"
  7. #include "mbedtls/ssl.h"
  8. #include "mbedtls/net.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_init(hssl_ctx_init_param_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_pk_parse_keyfile(&ctx->pkey, param->key_file, NULL) != 0) {
  55. fprintf(stderr, "ssl key_file error!\n");
  56. goto error;
  57. }
  58. check = true;
  59. }
  60. if (param->verify_peer) {
  61. mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  62. }
  63. if (param->endpoint == HSSL_SERVER) {
  64. endpoint = MBEDTLS_SSL_IS_SERVER;
  65. }
  66. }
  67. mbedtls_ctr_drbg_seed(&ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy, NULL, 0);
  68. if (mbedtls_ssl_config_defaults(&ctx->conf, endpoint,
  69. MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
  70. fprintf(stderr, "ssl config error!\n");
  71. goto error;
  72. }
  73. mbedtls_ssl_conf_authmode(&ctx->conf, mode);
  74. mbedtls_ssl_conf_rng(&ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg);
  75. #if defined(MBEDTLS_SSL_CACHE_C)
  76. mbedtls_ssl_conf_session_cache(&ctx->conf, &ctx->cache, mbedtls_ssl_cache_get, mbedtls_ssl_cache_set);
  77. #endif
  78. if (check) {
  79. mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
  80. if (mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->pkey) != 0) {
  81. fprintf(stderr, "ssl key_file check failed!\n");
  82. goto error;
  83. }
  84. }
  85. g_ssl_ctx = ctx;
  86. return ctx;
  87. error:
  88. free(ctx);
  89. return NULL;
  90. }
  91. void hssl_ctx_cleanup(hssl_ctx_t ssl_ctx) {
  92. if (!ssl_ctx) return;
  93. if (g_ssl_ctx == ssl_ctx) {
  94. g_ssl_ctx = NULL;
  95. }
  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. if (n >= 0) return n;
  111. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_WRITE : -1);
  112. }
  113. static int __mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len) {
  114. int fd = (intptr_t)ctx;
  115. int n = read(fd, buf, len);
  116. if (n >= 0) return n;
  117. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_READ : -1);
  118. }
  119. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  120. struct mbedtls_ctx* mctx = (struct mbedtls_ctx*)ssl_ctx;
  121. mbedtls_ssl_context* ssl = (mbedtls_ssl_context*)malloc(sizeof(mbedtls_ssl_context));
  122. if (ssl == NULL) return NULL;
  123. mbedtls_ssl_init(ssl);
  124. mbedtls_ssl_setup(ssl, &mctx->conf);
  125. mbedtls_ssl_set_bio(ssl, (void*)(intptr_t)fd, __mbedtls_net_send, __mbedtls_net_recv, NULL);
  126. return ssl;
  127. }
  128. void hssl_free(hssl_t ssl) {
  129. if (ssl) {
  130. mbedtls_ssl_free(ssl);
  131. ssl = NULL;
  132. }
  133. }
  134. static int hssl_handshake(hssl_t ssl) {
  135. int ret = mbedtls_ssl_handshake(ssl);
  136. if (ret != 0) {
  137. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  138. return HSSL_WANT_READ;
  139. }
  140. else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  141. return HSSL_WANT_WRITE;
  142. }
  143. }
  144. return ret;
  145. }
  146. int hssl_accept(hssl_t ssl) {
  147. return hssl_handshake(ssl);
  148. }
  149. int hssl_connect(hssl_t ssl) {
  150. return hssl_handshake(ssl);
  151. }
  152. int hssl_read(hssl_t ssl, void* buf, int len) {
  153. return mbedtls_ssl_read(ssl, buf, len);
  154. }
  155. int hssl_write(hssl_t ssl, const void* buf, int len) {
  156. return mbedtls_ssl_write(ssl, buf, len);
  157. }
  158. int hssl_close(hssl_t ssl) {
  159. return 0;
  160. }
  161. int hssl_set_sni_hostname(hssl_t ssl, const char* hostname) {
  162. #ifdef MBEDTLS_X509_CRT_PARSE_C
  163. mbedtls_ssl_set_hostname(ssl, hostname);
  164. #endif
  165. return 0;
  166. }
  167. #endif // WITH_MBEDTLS