mbedtls.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_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_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. return ctx;
  86. error:
  87. free(ctx);
  88. return NULL;
  89. }
  90. void hssl_ctx_free(hssl_ctx_t ssl_ctx) {
  91. if (!ssl_ctx) return;
  92. struct mbedtls_ctx *mctx = (struct mbedtls_ctx *)ssl_ctx;
  93. mbedtls_x509_crt_free(&mctx->cert);
  94. mbedtls_pk_free(&mctx->pkey);
  95. mbedtls_ssl_config_free(&mctx->conf);
  96. #if defined(MBEDTLS_SSL_CACHE_C)
  97. mbedtls_ssl_cache_free(&mctx->cache);
  98. #endif
  99. mbedtls_ctr_drbg_free(&mctx->ctr_drbg);
  100. mbedtls_entropy_free(&mctx->entropy);
  101. free(mctx);
  102. }
  103. static int __mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len) {
  104. int fd = (intptr_t)ctx;
  105. int n = write(fd, buf, len);
  106. if (n >= 0) return n;
  107. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_WRITE : -1);
  108. }
  109. static int __mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len) {
  110. int fd = (intptr_t)ctx;
  111. int n = read(fd, buf, len);
  112. if (n >= 0) return n;
  113. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_READ : -1);
  114. }
  115. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  116. struct mbedtls_ctx* mctx = (struct mbedtls_ctx*)ssl_ctx;
  117. mbedtls_ssl_context* ssl = (mbedtls_ssl_context*)malloc(sizeof(mbedtls_ssl_context));
  118. if (ssl == NULL) return NULL;
  119. mbedtls_ssl_init(ssl);
  120. mbedtls_ssl_setup(ssl, &mctx->conf);
  121. mbedtls_ssl_set_bio(ssl, (void*)(intptr_t)fd, __mbedtls_net_send, __mbedtls_net_recv, NULL);
  122. return ssl;
  123. }
  124. void hssl_free(hssl_t ssl) {
  125. if (ssl) {
  126. mbedtls_ssl_free(ssl);
  127. ssl = NULL;
  128. }
  129. }
  130. static int hssl_handshake(hssl_t ssl) {
  131. int ret = mbedtls_ssl_handshake(ssl);
  132. if (ret != 0) {
  133. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  134. return HSSL_WANT_READ;
  135. }
  136. else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  137. return HSSL_WANT_WRITE;
  138. }
  139. }
  140. return ret;
  141. }
  142. int hssl_accept(hssl_t ssl) {
  143. return hssl_handshake(ssl);
  144. }
  145. int hssl_connect(hssl_t ssl) {
  146. return hssl_handshake(ssl);
  147. }
  148. int hssl_read(hssl_t ssl, void* buf, int len) {
  149. return mbedtls_ssl_read(ssl, buf, len);
  150. }
  151. int hssl_write(hssl_t ssl, const void* buf, int len) {
  152. return mbedtls_ssl_write(ssl, buf, len);
  153. }
  154. int hssl_close(hssl_t ssl) {
  155. return 0;
  156. }
  157. int hssl_set_sni_hostname(hssl_t ssl, const char* hostname) {
  158. #ifdef MBEDTLS_X509_CRT_PARSE_C
  159. mbedtls_ssl_set_hostname(ssl, hostname);
  160. #endif
  161. return 0;
  162. }
  163. #endif // WITH_MBEDTLS