Debian Codegraph @

crt/s2n/bindings/rust-examples/client-hello-config-resolution/src/bin/client.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use clap::Parser;
5 use s2n_tls::security::DEFAULT_TLS13;
6
7 use std::error::Error;
8 use tokio::{
9 io::{AsyncReadExt, AsyncWriteExt},
10 net::*,
11 };
12
13 const PORT: u16 = 1738;
14
15 #[derive(Debug, Parser)]
16 struct Cli {
17 /// value to specify for the SNI
18 sni: String,
19 }
20
21 #[tokio::main]
22 async fn main() -> Result<(), Box<dyn Error>> {
23 let args = Cli::parse();
24 let mut config = s2n_tls::config::Config::builder();
25 let ca: Vec<u8> =
26 std::fs::read(env!("CARGO_MANIFEST_DIR").to_owned() + "/../certs/ca-cert.pem")?;
27 config.set_security_policy(&DEFAULT_TLS13)?;
28 config.trust_pem(&ca)?;
29
30 let client = s2n_tls_tokio::TlsConnector::new(config.build()?);
31 let stream = TcpStream::connect(("127.0.0.1", PORT)).await?;
32 // request a TLS connection on the TCP stream while setting the sni
33 let mut tls = match client.connect(&args.sni, stream).await {
34 Ok(tls) => tls,
35 Err(e) => {
36 println!("error during handshake: {:?}", e);
37 return Ok(());
38 }
39 };
40 println!("{:#?}", tls);
41
42 let mut server_response = String::new();
43 tls.read_to_string(&mut server_response).await?;
44 println!("The server said {server_response}");
45 tls.shutdown().await?;
46
47 Ok(())
48 }