mod_mbedtls.inl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #if defined(USE_MBEDTLS) // USE_MBEDTLS used with NO_SSL
  2. #include "mbedtls/ctr_drbg.h"
  3. #include "mbedtls/debug.h"
  4. #include "mbedtls/entropy.h"
  5. #include "mbedtls/error.h"
  6. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  7. // The file include/mbedtls/net.h was removed in v3.0.0 because its only
  8. // function was to include mbedtls/net_sockets.h which now should be included
  9. // directly.
  10. #include "mbedtls/net_sockets.h"
  11. #else
  12. #include "mbedtls/net.h"
  13. #endif
  14. #include "mbedtls/pk.h"
  15. #include "mbedtls/platform.h"
  16. #include "mbedtls/ssl.h"
  17. #include "mbedtls/x509.h"
  18. #include "mbedtls/x509_crt.h"
  19. #include <string.h>
  20. typedef mbedtls_ssl_context SSL;
  21. typedef struct {
  22. mbedtls_ssl_config conf; /* SSL configuration */
  23. mbedtls_x509_crt cert; /* Certificate */
  24. mbedtls_ctr_drbg_context ctr; /* Counter random generator state */
  25. mbedtls_entropy_context entropy; /* Entropy context */
  26. mbedtls_pk_context pkey; /* Private key */
  27. } SSL_CTX;
  28. /* public api */
  29. int mbed_sslctx_init(SSL_CTX *ctx, const char *crt);
  30. void mbed_sslctx_uninit(SSL_CTX *ctx);
  31. void mbed_ssl_close(mbedtls_ssl_context *ssl);
  32. int mbed_ssl_accept(mbedtls_ssl_context **ssl,
  33. SSL_CTX *ssl_ctx,
  34. int *sock,
  35. struct mg_context *phys_ctx);
  36. int mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len);
  37. int mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len);
  38. static void mbed_debug(void *context,
  39. int level,
  40. const char *file,
  41. int line,
  42. const char *str);
  43. static int mbed_ssl_handshake(mbedtls_ssl_context *ssl);
  44. int
  45. mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
  46. {
  47. mbedtls_ssl_config *conf;
  48. int rc;
  49. if (ctx == NULL || crt == NULL) {
  50. return -1;
  51. }
  52. DEBUG_TRACE("%s", "Initializing MbedTLS SSL");
  53. mbedtls_entropy_init(&ctx->entropy);
  54. conf = &ctx->conf;
  55. mbedtls_ssl_config_init(conf);
  56. /* Set mbedTLS debug level by defining MG_CONFIG_MBEDTLS_DEBUG:
  57. * 0 No debug = mbedTLS DEFAULT
  58. * 1 Error (default if "DEBUG" is set for CivetWeb)
  59. * 2 State change
  60. * 3 Informational
  61. * 4 Verbose
  62. */
  63. #if defined(DEBUG) || defined(MG_CONFIG_MBEDTLS_DEBUG)
  64. #if defined(MG_CONFIG_MBEDTLS_DEBUG)
  65. mbedtls_debug_set_threshold(MG_CONFIG_MBEDTLS_DEBUG);
  66. #else
  67. mbedtls_debug_set_threshold(1);
  68. #endif
  69. mbedtls_ssl_conf_dbg(conf, mbed_debug, (void *)ctx);
  70. #endif
  71. /* Initialize TLS key and cert */
  72. mbedtls_pk_init(&ctx->pkey);
  73. mbedtls_ctr_drbg_init(&ctx->ctr);
  74. mbedtls_x509_crt_init(&ctx->cert);
  75. rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
  76. mbedtls_entropy_func,
  77. &ctx->entropy,
  78. (unsigned char *)"CivetWeb",
  79. strlen("CivetWeb"));
  80. if (rc != 0) {
  81. DEBUG_TRACE("TLS random seed failed (%i)", rc);
  82. return -1;
  83. }
  84. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  85. // mbedtls_pk_parse_keyfile() has changed in mbedTLS 3.0. You now need
  86. // to pass a properly seeded, cryptographically secure RNG when calling
  87. // these functions. It is used for blinding, a countermeasure against
  88. // side-channel attacks.
  89. // https://github.com/Mbed-TLS/mbedtls/blob/development/docs/3.0-migration-guide.md#some-functions-gained-an-rng-parameter
  90. rc = mbedtls_pk_parse_keyfile(
  91. &ctx->pkey, crt, NULL, mbedtls_ctr_drbg_random, &ctx->ctr);
  92. #else
  93. rc = mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL);
  94. #endif
  95. if (rc != 0) {
  96. DEBUG_TRACE("TLS parse key file failed (%i)", rc);
  97. return -1;
  98. }
  99. rc = mbedtls_x509_crt_parse_file(&ctx->cert, crt);
  100. if (rc != 0) {
  101. DEBUG_TRACE("TLS parse crt file failed (%i)", rc);
  102. return -1;
  103. }
  104. rc = mbedtls_ssl_config_defaults(conf,
  105. MBEDTLS_SSL_IS_SERVER,
  106. MBEDTLS_SSL_TRANSPORT_STREAM,
  107. MBEDTLS_SSL_PRESET_DEFAULT);
  108. if (rc != 0) {
  109. DEBUG_TRACE("TLS set defaults failed (%i)", rc);
  110. return -1;
  111. }
  112. mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, &ctx->ctr);
  113. /* Set auth mode if peer cert should be verified */
  114. mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
  115. mbedtls_ssl_conf_ca_chain(conf, NULL, NULL);
  116. /* Configure server cert and key */
  117. rc = mbedtls_ssl_conf_own_cert(conf, &ctx->cert, &ctx->pkey);
  118. if (rc != 0) {
  119. DEBUG_TRACE("TLS cannot set certificate and private key (%i)", rc);
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. void
  125. mbed_sslctx_uninit(SSL_CTX *ctx)
  126. {
  127. mbedtls_ctr_drbg_free(&ctx->ctr);
  128. mbedtls_pk_free(&ctx->pkey);
  129. mbedtls_x509_crt_free(&ctx->cert);
  130. mbedtls_entropy_free(&ctx->entropy);
  131. mbedtls_ssl_config_free(&ctx->conf);
  132. }
  133. int
  134. mbed_ssl_accept(mbedtls_ssl_context **ssl,
  135. SSL_CTX *ssl_ctx,
  136. int *sock,
  137. struct mg_context *phys_ctx)
  138. {
  139. int rc;
  140. (void)phys_ctx; /* unused, if server statistics is not turned on */
  141. DEBUG_TRACE("TLS accept processing %p", ssl);
  142. *ssl = (mbedtls_ssl_context *)mg_calloc_ctx(1,
  143. sizeof(mbedtls_ssl_context),
  144. phys_ctx);
  145. if (*ssl == NULL) {
  146. DEBUG_TRACE("TLS accept: malloc ssl failed (%i)",
  147. (int)sizeof(mbedtls_ssl_context));
  148. return -1;
  149. }
  150. mbedtls_ssl_init(*ssl);
  151. mbedtls_ssl_setup(*ssl, &ssl_ctx->conf);
  152. mbedtls_ssl_set_bio(*ssl, sock, mbedtls_net_send, mbedtls_net_recv, NULL);
  153. rc = mbed_ssl_handshake(*ssl);
  154. if (rc != 0) {
  155. DEBUG_TRACE("TLS handshake failed (%i)", rc);
  156. mbedtls_ssl_free(*ssl);
  157. mg_free(*ssl);
  158. *ssl = NULL;
  159. return -1;
  160. }
  161. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  162. DEBUG_TRACE("TLS connection %p accepted, state: %d",
  163. ssl,
  164. (*ssl)->MBEDTLS_PRIVATE(state));
  165. #else
  166. DEBUG_TRACE("TLS connection %p accepted, state: %d", ssl, (*ssl)->state);
  167. #endif
  168. return 0;
  169. }
  170. void
  171. mbed_ssl_close(mbedtls_ssl_context *ssl)
  172. {
  173. DEBUG_TRACE("TLS connection %p closed", ssl);
  174. mbedtls_ssl_close_notify(ssl);
  175. mbedtls_ssl_free(ssl);
  176. mg_free(ssl); /* mg_free for mg_calloc in mbed_ssl_accept */
  177. }
  178. static int
  179. mbed_ssl_handshake(mbedtls_ssl_context *ssl)
  180. {
  181. int rc;
  182. while ((rc = mbedtls_ssl_handshake(ssl)) != 0) {
  183. if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE
  184. && rc != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  185. break;
  186. }
  187. }
  188. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  189. DEBUG_TRACE("TLS handshake rc: %d, state: %d",
  190. rc,
  191. ssl->MBEDTLS_PRIVATE(state));
  192. #else
  193. DEBUG_TRACE("TLS handshake rc: %d, state: %d", rc, ssl->state);
  194. #endif
  195. return rc;
  196. }
  197. int
  198. mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len)
  199. {
  200. int rc = mbedtls_ssl_read(ssl, buf, len);
  201. /* DEBUG_TRACE("mbedtls_ssl_read: %d", rc); */
  202. return rc;
  203. }
  204. int
  205. mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len)
  206. {
  207. int rc = mbedtls_ssl_write(ssl, buf, len);
  208. /* DEBUG_TRACE("mbedtls_ssl_write: %d", rc); */
  209. return rc;
  210. }
  211. static void
  212. mbed_debug(void *user_param,
  213. int level,
  214. const char *file,
  215. int line,
  216. const char *str)
  217. {
  218. (void)level; /* Ignored. Limit is set using mbedtls_debug_set_threshold */
  219. (void)user_param; /* Ignored. User parameter (context) is set using
  220. mbedtls_ssl_conf_dbg */
  221. DEBUG_TRACE("mbedTLS DEBUG: file: [%s] line: [%d] str: [%s]",
  222. file,
  223. line,
  224. str);
  225. }
  226. #endif /* USE_MBEDTLS */