mod_mbedtls.inl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #if defined(USE_MBEDTLS) // USE_MBEDTLS used with NO_SSL
  2. #include <string.h>
  3. #include "mbedtls/certs.h"
  4. #include "mbedtls/ssl.h"
  5. #include "mbedtls/net.h"
  6. #include "mbedtls/pk.h"
  7. #include "mbedtls/x509.h"
  8. #include "mbedtls/x509_crt.h"
  9. #include "mbedtls/ctr_drbg.h"
  10. #include "mbedtls/entropy.h"
  11. #include "mbedtls/error.h"
  12. #include "mbedtls/debug.h"
  13. #include "mbedtls/platform.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, int level, const char *file, int line, const char *str);
  30. static int mbed_ssl_handshake(mbedtls_ssl_context *ssl);
  31. int
  32. mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
  33. {
  34. mbedtls_ssl_config *conf;
  35. int rc;
  36. fprintf(stdout, "Initializing MbedTLS SSL\n");
  37. if (ctx == NULL || crt == NULL) {
  38. return -1;
  39. }
  40. mbedtls_entropy_init(&ctx->entropy);
  41. conf = &ctx->conf;
  42. mbedtls_ssl_config_init(conf);
  43. // set debug level
  44. mbedtls_debug_set_threshold(1);
  45. mbedtls_ssl_conf_dbg(conf, mbed_debug, stdout);
  46. mbedtls_pk_init(&ctx->pkey);
  47. mbedtls_ctr_drbg_init(&ctx->ctr);
  48. mbedtls_x509_crt_init(&ctx->cert);
  49. if ((rc = mbedtls_ctr_drbg_seed(&ctx->ctr,
  50. mbedtls_entropy_func,
  51. &ctx->entropy,
  52. (unsigned char *)"CivetWeb",
  53. strlen("CivetWeb")))
  54. != 0) {
  55. fprintf(stderr, "Cannot seed rng\n");
  56. return -1;
  57. }
  58. if (mbedtls_pk_parse_keyfile(&ctx->pkey, crt, NULL) != 0) {
  59. fprintf(stderr, "parse key file failed\n");
  60. return -1;
  61. }
  62. if (mbedtls_x509_crt_parse_file(&ctx->cert, crt) != 0) {
  63. fprintf(stderr, "parse crt file faied\n");
  64. return -1;
  65. }
  66. if ((rc = mbedtls_ssl_config_defaults(conf,
  67. MBEDTLS_SSL_IS_SERVER,
  68. MBEDTLS_SSL_TRANSPORT_STREAM,
  69. MBEDTLS_SSL_PRESET_DEFAULT))
  70. != 0) {
  71. fprintf(stderr, "Cannot set mbedtls defaults\n");
  72. return -1;
  73. }
  74. mbedtls_ssl_conf_rng(conf, mbedtls_ctr_drbg_random, &ctx->ctr);
  75. // Set auth mode if peer cert should be verified
  76. mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
  77. mbedtls_ssl_conf_ca_chain(conf, NULL, NULL);
  78. // Configure server cert and key
  79. if ((rc = mbedtls_ssl_conf_own_cert(conf, &ctx->cert, &ctx->pkey))
  80. != 0) {
  81. fprintf(stderr, "Cannot define certificate and private key\n");
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. void
  87. mbed_sslctx_uninit(SSL_CTX *ctx)
  88. {
  89. mbedtls_ctr_drbg_free(&ctx->ctr);
  90. mbedtls_pk_free(&ctx->pkey);
  91. mbedtls_x509_crt_free(&ctx->cert);
  92. mbedtls_entropy_free(&ctx->entropy);
  93. mbedtls_ssl_config_free(&ctx->conf);
  94. }
  95. int
  96. mbed_ssl_accept(mbedtls_ssl_context **ssl, SSL_CTX *ssl_ctx, int *sock)
  97. {
  98. *ssl = calloc(1, sizeof(**ssl));
  99. if (*ssl == NULL) {
  100. fprintf(stderr, "malloc ssl failed\n");
  101. return -1;
  102. }
  103. mbedtls_ssl_init(*ssl);
  104. mbedtls_ssl_setup(*ssl, &ssl_ctx->conf);
  105. mbedtls_ssl_set_bio(*ssl, sock, mbedtls_net_send, mbedtls_net_recv, NULL);
  106. if (mbed_ssl_handshake(*ssl) != 0) {
  107. fprintf(stderr, "handshake failed\n");
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. void
  113. mbed_ssl_close(mbedtls_ssl_context *ssl)
  114. {
  115. mbedtls_ssl_close_notify(ssl);
  116. mbedtls_ssl_free(ssl);
  117. ssl = NULL;
  118. }
  119. static int
  120. mbed_ssl_handshake(mbedtls_ssl_context *ssl)
  121. {
  122. int rc;
  123. while ((rc = mbedtls_ssl_handshake(ssl)) != 0) {
  124. if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE && rc != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  125. break;
  126. }
  127. }
  128. fprintf(stdout, "mbedtls handshake rc:%d state:%d\n", rc, ssl->state);
  129. return rc;
  130. }
  131. int
  132. mbed_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, int len)
  133. {
  134. int rc = mbedtls_ssl_read(ssl, buf, len);
  135. fprintf(stdout, "mbedtls: mbedtls_ssl_read %d\n", rc);
  136. return rc;
  137. }
  138. int
  139. mbed_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, int len)
  140. {
  141. int rc = mbedtls_ssl_write(ssl, buf, len);
  142. fprintf(stdout, "mbedtls: mbedtls_ssl_write:%d\n", rc);
  143. return rc;
  144. }
  145. static void
  146. mbed_debug(void *context, int level, const char *file, int line, const char *str)
  147. {
  148. (void)level;
  149. mbedtls_fprintf((FILE *)context, "file:%s line:%d str:%s\n", file, line, str);
  150. }
  151. #endif /* USE_MBEDTLS */