@@ -4,11 +4,10 @@ use attestation::{measurements::Measurements, AttestationError, AttestationType}
44pub use attestation:: { DcapTdxQuoteGenerator , NoQuoteGenerator , QuoteGenerator } ;
55use bytes:: Bytes ;
66use http:: HeaderValue ;
7- use http_body_util:: combinators:: BoxBody ;
8- use http_body_util:: BodyExt ;
9- use hyper:: service:: service_fn;
10- use hyper:: Response ;
7+ use http_body_util:: { combinators:: BoxBody , BodyExt } ;
8+ use hyper:: { service:: service_fn, Response } ;
119use hyper_util:: rt:: TokioIo ;
10+ use parity_scale_codec:: { Decode , Encode } ;
1211use thiserror:: Error ;
1312use tokio:: sync:: { mpsc, oneshot} ;
1413use tokio_rustls:: rustls:: server:: { VerifierBuilderError , WebPkiClientVerifier } ;
@@ -191,15 +190,12 @@ impl ProxyServer {
191190 let remote_cert_chain = connection. peer_certificates ( ) . map ( |c| c. to_owned ( ) ) ;
192191
193192 // If we are in a CVM, generate an attestation
194- let attestation = if local_quote_generator. attestation_type ( ) != AttestationType :: None {
195- serde_json:: to_vec ( & AttesationPayload :: from_attestation_generator (
196- & cert_chain,
197- exporter,
198- local_quote_generator,
199- ) ?) ?
200- } else {
201- Vec :: new ( )
202- } ;
193+ let attestation = AttesationPayload :: from_attestation_generator (
194+ & cert_chain,
195+ exporter,
196+ local_quote_generator,
197+ ) ?
198+ . encode ( ) ;
203199
204200 // Write our attestation to the channel, with length prefix
205201 let attestation_length_prefix = length_prefix ( & attestation) ;
@@ -215,24 +211,20 @@ impl ProxyServer {
215211 let mut buf = vec ! [ 0 ; length] ;
216212 tls_stream. read_exact ( & mut buf) . await ?;
217213
214+ let remote_attestation_payload = AttesationPayload :: decode ( & mut & buf[ ..] ) ?;
215+ let remote_attestation_type = remote_attestation_payload. attestation_type ;
216+
218217 // If we expect an attestaion from the client, verify it and get measurements
219- let ( measurements, remote_attestation_type) = if attestation_verifier. has_remote_attestion ( )
220- {
221- let remote_attestation_payload: AttesationPayload = serde_json:: from_slice ( & buf) ?;
222-
223- let remote_attestation_type = remote_attestation_payload. attestation_type ;
224- (
225- attestation_verifier
226- . verify_attestation (
227- remote_attestation_payload,
228- & remote_cert_chain. ok_or ( ProxyError :: NoClientAuth ) ?,
229- exporter,
230- )
231- . await ?,
232- remote_attestation_type,
233- )
218+ let measurements = if attestation_verifier. has_remote_attestion ( ) {
219+ attestation_verifier
220+ . verify_attestation (
221+ remote_attestation_payload,
222+ & remote_cert_chain. ok_or ( ProxyError :: NoClientAuth ) ?,
223+ exporter,
224+ )
225+ . await ?
234226 } else {
235- ( None , AttestationType :: None )
227+ None
236228 } ;
237229
238230 // Setup an HTTP server
@@ -607,7 +599,7 @@ impl ProxyClient {
607599 let mut buf = vec ! [ 0 ; length] ;
608600 tls_stream. read_exact ( & mut buf) . await ?;
609601
610- let remote_attestation_payload: AttesationPayload = serde_json :: from_slice ( & buf) ?;
602+ let remote_attestation_payload = AttesationPayload :: decode ( & mut & buf[ .. ] ) ?;
611603 let remote_attestation_type = remote_attestation_payload. attestation_type ;
612604
613605 // Verify the remote attestation against our accepted measurements
@@ -617,13 +609,14 @@ impl ProxyClient {
617609
618610 // If we are in a CVM, provide an attestation
619611 let attestation = if local_quote_generator. attestation_type ( ) != AttestationType :: None {
620- serde_json :: to_vec ( & AttesationPayload :: from_attestation_generator (
612+ AttesationPayload :: from_attestation_generator (
621613 & cert_chain. ok_or ( ProxyError :: NoClientAuth ) ?,
622614 exporter,
623615 local_quote_generator,
624- ) ?) ?
616+ ) ?
617+ . encode ( )
625618 } else {
626- Vec :: new ( )
619+ AttesationPayload :: without_attestation ( ) . encode ( )
627620 } ;
628621
629622 // Send our attestation (or zero bytes) prefixed with length
@@ -705,7 +698,7 @@ async fn get_tls_cert_with_config(
705698 let mut buf = vec ! [ 0 ; length] ;
706699 tls_stream. read_exact ( & mut buf) . await ?;
707700
708- let remote_attestation_payload: AttesationPayload = serde_json :: from_slice ( & buf) ?;
701+ let remote_attestation_payload = AttesationPayload :: decode ( & mut & buf[ .. ] ) ?;
709702
710703 let _measurements = attestation_verifier
711704 . verify_attestation ( remote_attestation_payload, & remote_cert_chain, exporter)
@@ -741,6 +734,8 @@ pub enum ProxyError {
741734 OneShotRecv ( #[ from] oneshot:: error:: RecvError ) ,
742735 #[ error( "Failed to send request, connection to proxy-server dropped" ) ]
743736 MpscSend ,
737+ #[ error( "Serialization: {0}" ) ]
738+ Serialization ( #[ from] parity_scale_codec:: Error ) ,
744739}
745740
746741impl From < mpsc:: error:: SendError < RequestWithResponseSender > > for ProxyError {
0 commit comments