Skip to main content

size_server/
size_server.rs

1//! A minimal TLS 1.3 server for size measurements (plan ยง0): loads a certificate and key from
2//! PEM files, reads one client flight from stdin and writes the server flight to stdout. Built
3//! with the `dist` profile and compared with `size_empty`, it shows what the server side of
4//! simoom-tls adds to a binary for a given feature set:
5//!
6//! `cargo build -p simoom-tls --profile dist --no-default-features --features rsa,p384,ed25519,chacha20 --example size_server --example size_empty`
7
8use simoom_tls::{ServerConfig, ServerConnection};
9use std::io::{Read, Write};
10use std::sync::Arc;
11
12fn main() {
13    let args: Vec<String> = std::env::args().collect();
14    if args.len() < 3 {
15        eprintln!("usage: size_server CERT.pem KEY.pem < client_flight");
16        std::process::exit(2);
17    }
18    let cert = std::fs::read(&args[1]).expect("certificate");
19    let key = std::fs::read(&args[2]).expect("key");
20    let cfg = Arc::new(ServerConfig::from_pem(&cert, &key).expect("server config").with_alpn(&[b"h2", b"http/1.1"]));
21    let mut conn = ServerConnection::new(cfg);
22    let mut input = Vec::new();
23    std::io::stdin().read_to_end(&mut input).expect("stdin");
24    conn.read_tls(&input);
25    if let Err(e) = conn.process_new_packets() {
26        eprintln!("{e:?}");
27    }
28    let out = conn.tls_output().to_vec();
29    conn.consume_tls_output(out.len());
30    std::io::stdout().write_all(&out).expect("stdout");
31    let mut buf = [0u8; 1024];
32    let n = conn.read_plaintext(&mut buf);
33    std::io::stdout().write_all(&buf[..n]).expect("stdout");
34}