mod_mbedtls.inl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #if defined(USE_MBEDTLS) // USE_MBEDTLS used with NO_SSL
  2. #include "mbedtls/certs.h"
  3. #include "mbedtls/ctr_drbg.h"
  4. #include "mbedtls/debug.h"
  5. #include "mbedtls/entropy.h"
  6. #include "mbedtls/error.h"
  7. #include "mbedtls/net.h"
  8. #include "mbedtls/pk.h"
  9. #include "mbedtls/platform.h"
  10. #include "mbedtls/ssl.h"
  11. #include "mbedtls/x509.h"
  12. #include "mbedtls/x509_crt.h"
  13. #include <string.h>
  14. typedef mbedtls_ssl_context SSL;
  15. typedef struct {
  16. mbedtls_ssl_config conf; /* SSL configuration */
  17. mbedtls_x509_crt cert; /* Certificate */
  18. mbedtls_ctr_drbg_context ctr; /* Counter random generator state */
  19. mbedtls_entropy_context entropy; /* Entropy context */
  20. mbedtls_pk_context pkey; /* Private key */
  21. } SSL_CTX;
  22. /* public api */
  23. int mbed_sslctx_init(SSL_CTX *ctx, const char *crt);
  24. void mbed_sslctx_uninit(SSL_CTX *ctx);
  25. void mbed_ssl_close(mbedtls_ssl_context *ssl);
  26. int mbed_ssl_accept(mbedtls_ssl_context **ssl, SSL_CTX *ssl_ctx, int *sock);
  27. int mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len);
  28. int mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len);
  29. static void mbed_debug(void *context,
  30. int level,
  31. const char *file,
  32. int line,
  33. const char *str);
  34. static int mbed_ssl_handshake(mbedtls_ssl_context *ssl);
  35. int
  36. mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
  37. {
  38. mbedtls_ssl_config *conf;
  39. int rc;
  40. if (ctx == NULL || crt == NULL) {
  41. return -1;
  42. }
  43. DEBUG_TRACE("Initializing MbedTLS SSL");
  44. mbedtls_entropy_init(&ctx->entropy);
  45. conf = &ctx->conf;
  46. mbedtls_ssl_config_init(conf);
  47. /* Set mbedTLS debug level by defining MG_CONFIG_MBEDTLS_DEBUG:
  48. * 0 No debug = mbedTLS DEFAULT
  49. * 1 Error (default if "DEBUG" is set for CivetWeb)
  50. * 2 State change
  51. * 3 Informational
  52. * 4 Verbose
  53. */
  54. #if defined(DEBUG) or defined(MG_CONFIG_MBEDTLS_DEBUG)
  55. #if defined(MG_CONFIG_MBEDTLS_DEBUG)
  56. mbedtls_debug_set_threshold(MG_CONFIG_MBEDTLS_DEBUG);
  57. #else
  58. mbedtls_debug_set_threshold(1);
  59. #endif
  60. mbedtls_ssl_conf_dbg(conf, mbed_debug, (void *)ctx);
  61. #endif
  62. /* Initialize TLS key and cert */
  63. mbedtls_pk_init(&ctx->pkey);
  64. mbedtls_ctr_drbg_init(&ctx->ctr);
  65. mbedtls_x509_crt_init(&ctx->cert);
  66. if ((rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
  67. mbedtls_entropy_func,
  68. &ctx->entropy,
  69. (unsigned char *)"CivetWeb",
  70. strlen("CivetWeb")))
  71. != 0) {
  72. DEBUG_TRACE("TLS random seed failed");
  73. return -1;
  74. }
  75. if (mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL) != 0) {
  76. DEBUG_TRACE("TLS parse key file failed");
  77. return -1;
  78. }
  79. if (mbedtls_x509_crt_parse_file(&ctx->cert, crt) != 0) {
  80. DEBUG_TRACE("TLS parse crt file failed");
  81. return -1;
  82. }
  83. if ((rc = mbedtls_ssl_config_defaults(conf,
  84. MBEDTLS_SSL_IS_SERVER,
  85. MBEDTLS_SSL_TRANSPORT_STREAM,
  86. MBEDTLS_SSL_PRESET_DEFAULT))
  87. != 0) {
  88. DEBUG_TRACE("TLS set defaults failed");
  89. return -1;
  90. }
  91. mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, &ctx->ctr);
  92. /* Set auth mode if peer cert should be verified */
  93. mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
  94. mbedtls_ssl_conf_ca_chain(conf, NULL, NULL);
  95. /* Configure server cert and key */
  96. if ((rc = mbedtls_ssl_conf_own_cert(conf, &ctx->cert, &ctx->pkey)) != 0) {
  97. DEBUG_TRACE("TLS cannot set certificate and private key");
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. void
  103. mbed_sslctx_uninit(SSL_CTX *ctx)
  104. {
  105. mbedtls_ctr_drbg_free(&ctx->ctr);
  106. mbedtls_pk_free(&ctx->pkey);
  107. mbedtls_x509_crt_free(&ctx->cert);
  108. mbedtls_entropy_free(&ctx->entropy);
  109. mbedtls_ssl_config_free(&ctx->conf);
  110. }
  111. int
  112. mbed_ssl_accept(mbedtls_ssl_context **ssl, SSL_CTX *ssl_ctx, int *sock)
  113. {
  114. *ssl = calloc(1, sizeof(**ssl));
  115. if (*ssl == NULL) {
  116. DEBUG_TRACE("TLS accept: malloc ssl failed (%i)", sizeof(**ssl));
  117. return -1;
  118. }
  119. mbedtls_ssl_init(*ssl);
  120. mbedtls_ssl_setup(*ssl, &ssl_ctx->conf);
  121. mbedtls_ssl_set_bio(*ssl, sock, mbedtls_net_send, mbedtls_net_recv, NULL);
  122. if (mbed_ssl_handshake(*ssl) != 0) {
  123. DEBUG_TRACE("TLS handshake failed");
  124. return -1;
  125. }
  126. DEBUG_TRACE("TLS connection accepted, state: %d", (*ssl)->state);
  127. return 0;
  128. }
  129. void
  130. mbed_ssl_close(mbedtls_ssl_context *ssl)
  131. {
  132. DEBUG_TRACE("TLS close");
  133. mbedtls_ssl_close_notify(ssl);
  134. mbedtls_ssl_free(ssl);
  135. ssl = NULL;
  136. }
  137. static int
  138. mbed_ssl_handshake(mbedtls_ssl_context *ssl)
  139. {
  140. int rc;
  141. while ((rc = mbedtls_ssl_handshake(ssl)) != 0) {
  142. if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE
  143. && rc != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  144. break;
  145. }
  146. }
  147. DEBUG_TRACE("TLS handshake rc: %d, state: %d", rc, ssl->state);
  148. return rc;
  149. }
  150. int
  151. mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len)
  152. {
  153. int rc = mbedtls_ssl_read(ssl, buf, len);
  154. /* DEBUG_TRACE("mbedtls_ssl_read: %d", rc); */
  155. return rc;
  156. }
  157. int
  158. mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len)
  159. {
  160. int rc = mbedtls_ssl_write(ssl, buf, len);
  161. /* DEBUG_TRACE("mbedtls_ssl_write: %d", rc); */
  162. return rc;
  163. }
  164. static void
  165. mbed_debug(void *user_param,
  166. int level,
  167. const char *file,
  168. int line,
  169. const char *str)
  170. {
  171. (void)level; /* Ignored. Limit is set using mbedtls_debug_set_threshold */
  172. (void)user_param; /* Ignored. User parameter (context) is set using
  173. mbedtls_ssl_conf_dbg */
  174. DEBUG_TRACE("mbedTLS DEBUG: file: [%s] line: [%d] str: [%s]",
  175. file,
  176. line,
  177. str);
  178. }
  179. #endif /* USE_MBEDTLS */