11mod attestation;
22
33pub use attestation:: { AttestationPlatform , MockAttestation , NoAttestation } ;
4- use tokio_rustls:: rustls:: server:: WebPkiClientVerifier ;
4+ use thiserror:: Error ;
5+ use tokio_rustls:: rustls:: server:: { VerifierBuilderError , WebPkiClientVerifier } ;
56
67#[ cfg( test) ]
78mod test_helpers;
@@ -60,26 +61,23 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyServer<L, R> {
6061 local_attestation_platform : L ,
6162 remote_attestation_platform : R ,
6263 client_auth : bool ,
63- ) -> Self {
64+ ) -> Result < Self , ProxyError > {
6465 if remote_attestation_platform. is_cvm ( ) && !client_auth {
65- panic ! ( "Client auth is required when the client is running in a CVM" ) ;
66+ return Err ( ProxyError :: NoClientAuth ) ;
6667 }
6768
6869 let server_config = if client_auth {
6970 let root_store =
7071 RootCertStore :: from_iter ( webpki_roots:: TLS_SERVER_ROOTS . iter ( ) . cloned ( ) ) ;
71- let verifier = WebPkiClientVerifier :: builder ( Arc :: new ( root_store) )
72- . build ( )
73- . expect ( "invalid client verifier" ) ;
72+ let verifier = WebPkiClientVerifier :: builder ( Arc :: new ( root_store) ) . build ( ) ?;
73+
7474 ServerConfig :: builder ( )
7575 . with_client_cert_verifier ( verifier)
76- . with_single_cert ( cert_and_key. cert_chain . clone ( ) , cert_and_key. key )
77- . expect ( "Failed to create rustls server config" )
76+ . with_single_cert ( cert_and_key. cert_chain . clone ( ) , cert_and_key. key ) ?
7877 } else {
7978 ServerConfig :: builder ( )
8079 . with_no_client_auth ( )
81- . with_single_cert ( cert_and_key. cert_chain . clone ( ) , cert_and_key. key )
82- . expect ( "Failed to create rustls server config" )
80+ . with_single_cert ( cert_and_key. cert_chain . clone ( ) , cert_and_key. key ) ?
8381 } ;
8482
8583 Self :: new_with_tls_config (
@@ -103,26 +101,27 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyServer<L, R> {
103101 target : SocketAddr ,
104102 local_attestation_platform : L ,
105103 remote_attestation_platform : R ,
106- ) -> Self {
104+ ) -> Result < Self , ProxyError > {
107105 let acceptor = tokio_rustls:: TlsAcceptor :: from ( server_config) ;
108- let listener = TcpListener :: bind ( local) . await . unwrap ( ) ;
106+ let listener = TcpListener :: bind ( local) . await ? ;
109107
110108 let inner = Proxy {
111109 listener,
112110 local_attestation_platform,
113111 remote_attestation_platform,
114112 } ;
115- Self {
113+
114+ Ok ( Self {
116115 acceptor,
117116 target,
118117 inner,
119118 cert_chain,
120- }
119+ } )
121120 }
122121
123122 /// Accept an incoming connection
124- pub async fn accept ( & self ) -> io :: Result < ( ) > {
125- let ( inbound, _client_addr) = self . inner . listener . accept ( ) . await . unwrap ( ) ;
123+ pub async fn accept ( & self ) -> Result < ( ) , ProxyError > {
124+ let ( inbound, _client_addr) = self . inner . listener . accept ( ) . await ? ;
126125
127126 let acceptor = self . acceptor . clone ( ) ;
128127 let target = self . target ;
@@ -215,9 +214,9 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyClient<L, R> {
215214 server_name : ServerName < ' static > ,
216215 local_attestation_platform : L ,
217216 remote_attestation_platform : R ,
218- ) -> Self {
217+ ) -> Result < Self , ProxyError > {
219218 if local_attestation_platform. is_cvm ( ) && cert_and_key. is_none ( ) {
220- panic ! ( "Client auth is required when the client is running in a CVM" ) ;
219+ return Err ( ProxyError :: NoClientAuth ) ;
221220 }
222221
223222 let root_store = RootCertStore :: from_iter ( webpki_roots:: TLS_SERVER_ROOTS . iter ( ) . cloned ( ) ) ;
@@ -228,8 +227,7 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyClient<L, R> {
228227 . with_client_auth_cert (
229228 cert_and_key. cert_chain . clone ( ) ,
230229 cert_and_key. key . clone_key ( ) ,
231- )
232- . unwrap ( )
230+ ) ?
233231 } else {
234232 ClientConfig :: builder ( )
235233 . with_root_certificates ( root_store)
@@ -256,8 +254,8 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyClient<L, R> {
256254 local_attestation_platform : L ,
257255 remote_attestation_platform : R ,
258256 cert_chain : Option < Vec < CertificateDer < ' static > > > ,
259- ) -> Self {
260- let listener = TcpListener :: bind ( local) . await . unwrap ( ) ;
257+ ) -> Result < Self , ProxyError > {
258+ let listener = TcpListener :: bind ( local) . await ? ;
261259 let connector = TlsConnector :: from ( client_config. clone ( ) ) ;
262260
263261 let inner = Proxy {
@@ -266,17 +264,17 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyClient<L, R> {
266264 remote_attestation_platform,
267265 } ;
268266
269- Self {
267+ Ok ( Self {
270268 inner,
271269 connector,
272270 target,
273271 target_name,
274272 cert_chain,
275- }
273+ } )
276274 }
277275
278276 pub async fn accept ( & self ) -> io:: Result < ( ) > {
279- let ( inbound, _client_addr) = self . inner . listener . accept ( ) . await . unwrap ( ) ;
277+ let ( inbound, _client_addr) = self . inner . listener . accept ( ) . await ? ;
280278
281279 let connector = self . connector . clone ( ) ;
282280 let target_name = self . target_name . clone ( ) ;
@@ -348,6 +346,18 @@ impl<L: AttestationPlatform, R: AttestationPlatform> ProxyClient<L, R> {
348346 }
349347}
350348
349+ #[ derive( Error , Debug ) ]
350+ pub enum ProxyError {
351+ #[ error( "Client auth is required when the client is running in a CVM" ) ]
352+ NoClientAuth ,
353+ #[ error( "TLS: {0}" ) ]
354+ Rustls ( #[ from] tokio_rustls:: rustls:: Error ) ,
355+ #[ error( "Verifier builder: {0}" ) ]
356+ VerifierBuilder ( #[ from] VerifierBuilderError ) ,
357+ #[ error( "IO: {0}" ) ]
358+ Io ( #[ from] std:: io:: Error ) ,
359+ }
360+
351361fn length_prefix ( input : & [ u8 ] ) -> [ u8 ; 4 ] {
352362 let len = input. len ( ) as u32 ;
353363 len. to_be_bytes ( )
@@ -377,7 +387,9 @@ mod tests {
377387 MockAttestation ,
378388 NoAttestation ,
379389 )
380- . await ;
390+ . await
391+ . unwrap ( ) ;
392+
381393 let proxy_addr = proxy_server. local_addr ( ) . unwrap ( ) ;
382394
383395 tokio:: spawn ( async move {
@@ -393,7 +405,8 @@ mod tests {
393405 MockAttestation ,
394406 None ,
395407 )
396- . await ;
408+ . await
409+ . unwrap ( ) ;
397410
398411 let proxy_client_addr = proxy_client. local_addr ( ) . unwrap ( ) ;
399412
@@ -439,7 +452,9 @@ mod tests {
439452 MockAttestation ,
440453 MockAttestation ,
441454 )
442- . await ;
455+ . await
456+ . unwrap ( ) ;
457+
443458 let proxy_addr = proxy_server. local_addr ( ) . unwrap ( ) ;
444459
445460 tokio:: spawn ( async move {
@@ -455,7 +470,8 @@ mod tests {
455470 MockAttestation ,
456471 Some ( client_cert_chain) ,
457472 )
458- . await ;
473+ . await
474+ . unwrap ( ) ;
459475
460476 let proxy_client_addr = proxy_client. local_addr ( ) . unwrap ( ) ;
461477
@@ -491,7 +507,9 @@ mod tests {
491507 local_attestation_platform,
492508 NoAttestation ,
493509 )
494- . await ;
510+ . await
511+ . unwrap ( ) ;
512+
495513 let proxy_server_addr = proxy_server. local_addr ( ) . unwrap ( ) ;
496514
497515 tokio:: spawn ( async move {
@@ -507,7 +525,9 @@ mod tests {
507525 MockAttestation ,
508526 None ,
509527 )
510- . await ;
528+ . await
529+ . unwrap ( ) ;
530+
511531 let proxy_client_addr = proxy_client. local_addr ( ) . unwrap ( ) ;
512532
513533 tokio:: spawn ( async move {
0 commit comments