1
- use futures:: AsyncWriteExt ;
2
1
use http_body_util:: Empty ;
3
2
use hyper:: { body:: Bytes , Request , StatusCode , Uri } ;
4
3
use hyper_util:: rt:: TokioIo ;
@@ -49,6 +48,8 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
49
48
let server_port = uri. port_u16 ( ) . unwrap_or ( 443 ) ;
50
49
51
50
// Create prover and connect to verifier.
51
+ //
52
+ // Perform the setup phase with the verifier.
52
53
let prover = Prover :: new (
53
54
ProverConfig :: builder ( )
54
55
. id ( id)
@@ -64,9 +65,18 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
64
65
let tls_client_socket = tokio:: net:: TcpStream :: connect ( ( server_domain, server_port) )
65
66
. await
66
67
. unwrap ( ) ;
68
+
69
+ // Pass server connection into the prover.
67
70
let ( mpc_tls_connection, prover_fut) =
68
71
prover. connect ( tls_client_socket. compat ( ) ) . await . unwrap ( ) ;
72
+
73
+ // Grab a controller for the Prover so we can enable deferred decryption.
74
+ let ctrl = prover_fut. control ( ) ;
75
+
76
+ // Wrap the connection in a TokioIo compatibility layer to use it with hyper.
69
77
let mpc_tls_connection = TokioIo :: new ( mpc_tls_connection. compat ( ) ) ;
78
+
79
+ // Spawn the Prover to run in the background.
70
80
let prover_task = tokio:: spawn ( prover_fut) ;
71
81
72
82
// MPC-TLS Handshake.
@@ -75,7 +85,12 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
75
85
. await
76
86
. unwrap ( ) ;
77
87
78
- let connection_task = tokio:: spawn ( connection. without_shutdown ( ) ) ;
88
+ // Spawn the connection to run in the background.
89
+ tokio:: spawn ( connection) ;
90
+
91
+ // Enable deferred decryption. This speeds up the proving time, but doesn't
92
+ // let us see the decrypted data until after the connection is closed.
93
+ ctrl. defer_decryption ( ) . await . unwrap ( ) ;
79
94
80
95
// MPC-TLS: Send Request and wait for Response.
81
96
let request = Request :: builder ( )
@@ -90,10 +105,6 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
90
105
91
106
assert ! ( response. status( ) == StatusCode :: OK ) ;
92
107
93
- // Close TLS Connection.
94
- let tls_connection = connection_task. await . unwrap ( ) . unwrap ( ) . io . into_inner ( ) ;
95
- tls_connection. compat ( ) . close ( ) . await . unwrap ( ) ;
96
-
97
108
// Create proof for the Verifier.
98
109
let mut prover = prover_task. await . unwrap ( ) . unwrap ( ) . start_prove ( ) ;
99
110
redact_and_reveal_received_data ( & mut prover) ;
@@ -128,6 +139,7 @@ async fn verifier<T: AsyncWrite + AsyncRead + Send + Sync + Unpin + 'static>(
128
139
response
129
140
. find ( "BEGIN PUBLIC KEY" )
130
141
. expect ( "Expected valid public key in JSON response" ) ;
142
+
131
143
// Check Session info: server name.
132
144
assert_eq ! ( session_info. server_name. as_str( ) , SERVER_DOMAIN ) ;
133
145
0 commit comments