mod_mbedtls.inl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #ifdef MBEDTLS_PSA_CRYPTO_C
  76. /* Initialize PSA crypto (mandatory with TLS 1.3)
  77. * This must be done before calling any other PSA Crypto
  78. * functions or they will fail with PSA_ERROR_BAD_STATE
  79. */
  80. const psa_status_t status = psa_crypto_init();
  81. if (status != PSA_SUCCESS) {
  82. DEBUG_TRACE("Failed to initialize PSA crypto, returned %d\n", (int) status);
  83. return -1;
  84. }
  85. #endif
  86. rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
  87. mbedtls_entropy_func,
  88. &ctx->entropy,
  89. (unsigned char *)"CivetWeb",
  90. strlen("CivetWeb"));
  91. if (rc != 0) {
  92. DEBUG_TRACE("TLS random seed failed (%i)", rc);
  93. return -1;
  94. }
  95. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  96. // mbedtls_pk_parse_keyfile() has changed in mbedTLS 3.0. You now need
  97. // to pass a properly seeded, cryptographically secure RNG when calling
  98. // these functions. It is used for blinding, a countermeasure against
  99. // side-channel attacks.
  100. // https://github.com/Mbed-TLS/mbedtls/blob/development/docs/3.0-migration-guide.md#some-functions-gained-an-rng-parameter
  101. rc = mbedtls_pk_parse_keyfile(
  102. &ctx->pkey, crt, NULL, mbedtls_ctr_drbg_random, &ctx->ctr);
  103. #else
  104. rc = mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL);
  105. #endif
  106. if (rc != 0) {
  107. DEBUG_TRACE("TLS parse key file failed (%i)", rc);
  108. return -1;
  109. }
  110. rc = mbedtls_x509_crt_parse_file(&ctx->cert, crt);
  111. if (rc != 0) {
  112. DEBUG_TRACE("TLS parse crt file failed (%i)", rc);
  113. return -1;
  114. }
  115. rc = mbedtls_ssl_config_defaults(conf,
  116. MBEDTLS_SSL_IS_SERVER,
  117. MBEDTLS_SSL_TRANSPORT_STREAM,
  118. MBEDTLS_SSL_PRESET_DEFAULT);
  119. if (rc != 0) {
  120. DEBUG_TRACE("TLS set defaults failed (%i)", rc);
  121. return -1;
  122. }
  123. mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, &ctx->ctr);
  124. /* Set auth mode if peer cert should be verified */
  125. mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
  126. mbedtls_ssl_conf_ca_chain(conf, NULL, NULL);
  127. /* Configure server cert and key */
  128. rc = mbedtls_ssl_conf_own_cert(conf, &ctx->cert, &ctx->pkey);
  129. if (rc != 0) {
  130. DEBUG_TRACE("TLS cannot set certificate and private key (%i)", rc);
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. void
  136. mbed_sslctx_uninit(SSL_CTX *ctx)
  137. {
  138. mbedtls_ctr_drbg_free(&ctx->ctr);
  139. mbedtls_pk_free(&ctx->pkey);
  140. mbedtls_x509_crt_free(&ctx->cert);
  141. mbedtls_entropy_free(&ctx->entropy);
  142. mbedtls_ssl_config_free(&ctx->conf);
  143. }
  144. int
  145. mbed_ssl_accept(mbedtls_ssl_context **ssl,
  146. SSL_CTX *ssl_ctx,
  147. int *sock,
  148. struct mg_context *phys_ctx)
  149. {
  150. int rc;
  151. (void)phys_ctx; /* unused, if server statistics is not turned on */
  152. DEBUG_TRACE("TLS accept processing %p", ssl);
  153. *ssl = (mbedtls_ssl_context *)mg_calloc_ctx(1,
  154. sizeof(mbedtls_ssl_context),
  155. phys_ctx);
  156. if (*ssl == NULL) {
  157. DEBUG_TRACE("TLS accept: malloc ssl failed (%i)",
  158. (int)sizeof(mbedtls_ssl_context));
  159. return -1;
  160. }
  161. mbedtls_ssl_init(*ssl);
  162. mbedtls_ssl_setup(*ssl, &ssl_ctx->conf);
  163. mbedtls_ssl_set_bio(*ssl, sock, mbedtls_net_send, mbedtls_net_recv, NULL);
  164. rc = mbed_ssl_handshake(*ssl);
  165. if (rc != 0) {
  166. DEBUG_TRACE("TLS handshake failed (%i)", rc);
  167. mbedtls_ssl_free(*ssl);
  168. mg_free(*ssl);
  169. *ssl = NULL;
  170. return -1;
  171. }
  172. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  173. DEBUG_TRACE("TLS connection %p accepted, state: %d",
  174. ssl,
  175. (*ssl)->MBEDTLS_PRIVATE(state));
  176. #else
  177. DEBUG_TRACE("TLS connection %p accepted, state: %d", ssl, (*ssl)->state);
  178. #endif
  179. return 0;
  180. }
  181. void
  182. mbed_ssl_close(mbedtls_ssl_context *ssl)
  183. {
  184. DEBUG_TRACE("TLS connection %p closed", ssl);
  185. mbedtls_ssl_close_notify(ssl);
  186. mbedtls_ssl_free(ssl);
  187. mg_free(ssl); /* mg_free for mg_calloc in mbed_ssl_accept */
  188. }
  189. static int
  190. mbed_ssl_handshake(mbedtls_ssl_context *ssl)
  191. {
  192. int rc;
  193. while ((rc = mbedtls_ssl_handshake(ssl)) != 0) {
  194. if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE
  195. && rc != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  196. break;
  197. }
  198. }
  199. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  200. DEBUG_TRACE("TLS handshake rc: %d, state: %d",
  201. rc,
  202. ssl->MBEDTLS_PRIVATE(state));
  203. #else
  204. DEBUG_TRACE("TLS handshake rc: %d, state: %d", rc, ssl->state);
  205. #endif
  206. return rc;
  207. }
  208. int
  209. mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len)
  210. {
  211. int rc = mbedtls_ssl_read(ssl, buf, len);
  212. /* DEBUG_TRACE("mbedtls_ssl_read: %d", rc); */
  213. return rc;
  214. }
  215. int
  216. mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len)
  217. {
  218. int rc = mbedtls_ssl_write(ssl, buf, len);
  219. /* DEBUG_TRACE("mbedtls_ssl_write: %d", rc); */
  220. return rc;
  221. }
  222. static void
  223. mbed_debug(void *user_param,
  224. int level,
  225. const char *file,
  226. int line,
  227. const char *str)
  228. {
  229. (void)level; /* Ignored. Limit is set using mbedtls_debug_set_threshold */
  230. (void)user_param; /* Ignored. User parameter (context) is set using
  231. mbedtls_ssl_conf_dbg */
  232. DEBUG_TRACE("mbedTLS DEBUG: file: [%s] line: [%d] str: [%s]",
  233. file,
  234. line,
  235. str);
  236. }
  237. #endif /* USE_MBEDTLS */