فهرست منبع

Merge pull request #957 from xhpohanka/mbedtls

mbedtls: read out unprocessed TLS data
bel2125 4 سال پیش
والد
کامیت
5ccd583cc4
3فایلهای تغییر یافته به همراه31 افزوده شده و 14 حذف شده
  1. 1 1
      CMakeLists.txt
  2. 27 12
      src/civetweb.c
  3. 3 1
      src/mod_mbedtls.inl

+ 1 - 1
CMakeLists.txt

@@ -603,7 +603,7 @@ install(
   FILES
     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
     "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
-    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLibDL.cmake"
+    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLibDl.cmake"
     "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLibRt.cmake"
     "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindWinSock.cmake"
   DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"

+ 27 - 12
src/civetweb.c

@@ -6815,24 +6815,39 @@ pull_inner(FILE *fp,
 
 #if defined(USE_MBEDTLS)
 	} else if (conn->ssl != NULL) {
-		/* We already know there is no more data buffered in conn->buf
-		 * but there is more available in the SSL layer. So don't poll
-		 * conn->client.sock yet. */
 		struct pollfd pfd[1];
+		int to_read;
 		int pollres;
 
-		pfd[0].fd = conn->client.sock;
-		pfd[0].events = POLLIN;
-		pollres = mg_poll(pfd,
-		                  1,
-		                  (int)(timeout * 1000.0),
-		                  &(conn->phys_ctx->stop_flag));
-		if (conn->phys_ctx->stop_flag) {
-			return -2;
+		to_read = mbedtls_ssl_get_bytes_avail(conn->ssl);
+
+		if (to_read > 0) {
+			/* We already know there is no more data buffered in conn->buf
+			 * but there is more available in the SSL layer. So don't poll
+			 * conn->client.sock yet. */
+
+			pollres = 1;
+			if (to_read > len)
+				to_read = len;
+		}
+		else {
+			pfd[0].fd = conn->client.sock;
+			pfd[0].events = POLLIN;
+
+			to_read = len;
+
+			pollres = mg_poll(pfd,
+			                  1,
+			                  (int)(timeout * 1000.0),
+			                  &(conn->phys_ctx->stop_flag));
+
+			if (conn->phys_ctx->stop_flag) {
+				return -2;
+			}
 		}
 
 		if (pollres > 0) {
-			nread = mbed_ssl_read(conn->ssl, (unsigned char *)buf, len);
+			nread = mbed_ssl_read(conn->ssl, (unsigned char *)buf, to_read);
 			if (nread <= 0) {
 				if ((nread == MBEDTLS_ERR_SSL_WANT_READ)
 				    || (nread == MBEDTLS_ERR_SSL_WANT_WRITE)

+ 3 - 1
src/mod_mbedtls.inl

@@ -51,8 +51,10 @@ mbed_sslctx_init(SSL_CTX *ctx, const char *crt)
     mbedtls_ssl_config_init(conf);
 
     // set debug level
+#if defined(CONFIG_MBEDTLS_DEBUG)
     mbedtls_debug_set_threshold(2);
     mbedtls_ssl_conf_dbg(conf, mbed_debug, stdout);
+#endif
     mbedtls_pk_init(&ctx->pkey);
     mbedtls_ctr_drbg_init(&ctx->ctr);
     mbedtls_x509_crt_init(&ctx->cert);
@@ -175,7 +177,7 @@ static void
 mbed_debug(void *context, int level, const char *file, int line, const char *str)
 {
     (void)level;
-    mbedtls_fprintf((FILE *)context, "file:%s line:%d str:%s\n", file, line, str);
+    mbedtls_fprintf((FILE *)context, "file:%s line:%d str:%s", file, line, str);
 }
 
 #endif /* USE_MBEDTLS */