mod_mbedtls.inl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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,
  27. SSL_CTX *ssl_ctx,
  28. int *sock,
  29. struct mg_context *phys_ctx);
  30. int mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len);
  31. int mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len);
  32. static void mbed_debug(void *context,
  33. int level,
  34. const char *file,
  35. int line,
  36. const char *str);
  37. static int mbed_ssl_handshake(mbedtls_ssl_context *ssl);
  38. int
  39. mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
  40. {
  41. mbedtls_ssl_config *conf;
  42. int rc;
  43. if (ctx == NULL || crt == NULL) {
  44. return -1;
  45. }
  46. DEBUG_TRACE("%s", "Initializing MbedTLS SSL");
  47. mbedtls_entropy_init(&ctx->entropy);
  48. conf = &ctx->conf;
  49. mbedtls_ssl_config_init(conf);
  50. /* Set mbedTLS debug level by defining MG_CONFIG_MBEDTLS_DEBUG:
  51. * 0 No debug = mbedTLS DEFAULT
  52. * 1 Error (default if "DEBUG" is set for CivetWeb)
  53. * 2 State change
  54. * 3 Informational
  55. * 4 Verbose
  56. */
  57. #if defined(DEBUG) || defined(MG_CONFIG_MBEDTLS_DEBUG)
  58. #if defined(MG_CONFIG_MBEDTLS_DEBUG)
  59. mbedtls_debug_set_threshold(MG_CONFIG_MBEDTLS_DEBUG);
  60. #else
  61. mbedtls_debug_set_threshold(1);
  62. #endif
  63. mbedtls_ssl_conf_dbg(conf, mbed_debug, (void *)ctx);
  64. #endif
  65. /* Initialize TLS key and cert */
  66. mbedtls_pk_init(&ctx->pkey);
  67. mbedtls_ctr_drbg_init(&ctx->ctr);
  68. mbedtls_x509_crt_init(&ctx->cert);
  69. rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
  70. mbedtls_entropy_func,
  71. &ctx->entropy,
  72. (unsigned char *)"CivetWeb",
  73. strlen("CivetWeb"));
  74. if (rc != 0) {
  75. DEBUG_TRACE("TLS random seed failed (%i)", rc);
  76. return -1;
  77. }
  78. rc = mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL);
  79. if (rc != 0) {
  80. DEBUG_TRACE("TLS parse key file failed (%i)", rc);
  81. return -1;
  82. }
  83. rc = mbedtls_x509_crt_parse_file(&ctx->cert, crt);
  84. if (rc != 0) {
  85. DEBUG_TRACE("TLS parse crt file failed (%i)", rc);
  86. return -1;
  87. }
  88. rc = mbedtls_ssl_config_defaults(conf,
  89. MBEDTLS_SSL_IS_SERVER,
  90. MBEDTLS_SSL_TRANSPORT_STREAM,
  91. MBEDTLS_SSL_PRESET_DEFAULT);
  92. if (rc != 0) {
  93. DEBUG_TRACE("TLS set defaults failed (%i)", rc);
  94. return -1;
  95. }
  96. mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, &ctx->ctr);
  97. /* Set auth mode if peer cert should be verified */
  98. mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
  99. mbedtls_ssl_conf_ca_chain(conf, NULL, NULL);
  100. /* Configure server cert and key */
  101. rc = mbedtls_ssl_conf_own_cert(conf, &ctx->cert, &ctx->pkey);
  102. if (rc != 0) {
  103. DEBUG_TRACE("TLS cannot set certificate and private key (%i)", rc);
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. void
  109. mbed_sslctx_uninit(SSL_CTX *ctx)
  110. {
  111. mbedtls_ctr_drbg_free(&ctx->ctr);
  112. mbedtls_pk_free(&ctx->pkey);
  113. mbedtls_x509_crt_free(&ctx->cert);
  114. mbedtls_entropy_free(&ctx->entropy);
  115. mbedtls_ssl_config_free(&ctx->conf);
  116. }
  117. int
  118. mbed_ssl_accept(mbedtls_ssl_context **ssl,
  119. SSL_CTX *ssl_ctx,
  120. int *sock,
  121. struct mg_context *phys_ctx)
  122. {
  123. int rc;
  124. (void)phys_ctx; /* unused, if server statistics is not turned on */
  125. DEBUG_TRACE("TLS accept processing %p", ssl);
  126. *ssl = (mbedtls_ssl_context *)mg_calloc_ctx(1,
  127. sizeof(mbedtls_ssl_context),
  128. phys_ctx);
  129. if (*ssl == NULL) {
  130. DEBUG_TRACE("TLS accept: malloc ssl failed (%i)",
  131. (int)sizeof(mbedtls_ssl_context));
  132. return -1;
  133. }
  134. mbedtls_ssl_init(*ssl);
  135. mbedtls_ssl_setup(*ssl, &ssl_ctx->conf);
  136. mbedtls_ssl_set_bio(*ssl, sock, mbedtls_net_send, mbedtls_net_recv, NULL);
  137. rc = mbed_ssl_handshake(*ssl);
  138. if (rc != 0) {
  139. DEBUG_TRACE("TLS handshake failed (%i)", rc);
  140. mbedtls_ssl_free(*ssl);
  141. mg_free(*ssl);
  142. *ssl = NULL;
  143. return -1;
  144. }
  145. DEBUG_TRACE("TLS connection %p accepted, state: %d", ssl, (*ssl)->state);
  146. return 0;
  147. }
  148. void
  149. mbed_ssl_close(mbedtls_ssl_context *ssl)
  150. {
  151. DEBUG_TRACE("TLS connection %p closed", ssl);
  152. mbedtls_ssl_close_notify(ssl);
  153. mbedtls_ssl_free(ssl);
  154. mg_free(ssl); /* mg_free for mg_calloc in mbed_ssl_accept */
  155. }
  156. static int
  157. mbed_ssl_handshake(mbedtls_ssl_context *ssl)
  158. {
  159. int rc;
  160. while ((rc = mbedtls_ssl_handshake(ssl)) != 0) {
  161. if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE
  162. && rc != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  163. break;
  164. }
  165. }
  166. DEBUG_TRACE("TLS handshake rc: %d, state: %d", rc, ssl->state);
  167. return rc;
  168. }
  169. int
  170. mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len)
  171. {
  172. int rc = mbedtls_ssl_read(ssl, buf, len);
  173. /* DEBUG_TRACE("mbedtls_ssl_read: %d", rc); */
  174. return rc;
  175. }
  176. int
  177. mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len)
  178. {
  179. int rc = mbedtls_ssl_write(ssl, buf, len);
  180. /* DEBUG_TRACE("mbedtls_ssl_write: %d", rc); */
  181. return rc;
  182. }
  183. static void
  184. mbed_debug(void *user_param,
  185. int level,
  186. const char *file,
  187. int line,
  188. const char *str)
  189. {
  190. (void)level; /* Ignored. Limit is set using mbedtls_debug_set_threshold */
  191. (void)user_param; /* Ignored. User parameter (context) is set using
  192. mbedtls_ssl_conf_dbg */
  193. DEBUG_TRACE("mbedTLS DEBUG: file: [%s] line: [%d] str: [%s]",
  194. file,
  195. line,
  196. str);
  197. }
  198. #endif /* USE_MBEDTLS */