|
| 1 | +use std::{fs, io::Write, net::TcpStream, sync::Arc}; |
| 2 | + |
| 3 | +use rustls::{pki_types::ServerName, ClientConfig, ClientConnection, RootCertStore, Stream}; |
| 4 | +use webpki_root_certs::TLS_SERVER_ROOT_CERTS; |
| 5 | + |
| 6 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 7 | + let mut roots = RootCertStore::empty(); |
| 8 | + let (_, ignored) = roots.add_parsable_certificates(TLS_SERVER_ROOT_CERTS.iter().cloned()); |
| 9 | + assert_eq!(ignored, 0, "{ignored} root certificates were ignored"); |
| 10 | + let config = Arc::new( |
| 11 | + ClientConfig::builder() |
| 12 | + .with_root_certificates(roots) |
| 13 | + .with_no_client_auth(), |
| 14 | + ); |
| 15 | + |
| 16 | + for &host in HOSTS { |
| 17 | + let server_name = ServerName::try_from(host)?; |
| 18 | + let mut conn = ClientConnection::new(config.clone(), server_name)?; |
| 19 | + let mut sock = TcpStream::connect((host, 443))?; |
| 20 | + let mut stream = Stream::new(&mut conn, &mut sock); |
| 21 | + |
| 22 | + eprintln!("connecting to {host}..."); |
| 23 | + if let Err(err) = stream.write_all(b"GET / HTTP/1.1\r\n\r\n") { |
| 24 | + eprintln!("failed to write to {host}: {err}"); |
| 25 | + } |
| 26 | + |
| 27 | + let Some(certs) = conn.peer_certificates() else { |
| 28 | + eprintln!("no certificates received for {host}"); |
| 29 | + continue; |
| 30 | + }; |
| 31 | + |
| 32 | + for (i, der) in certs.iter().enumerate() { |
| 33 | + let host_name = host.replace('.', "_"); |
| 34 | + let fname = format!( |
| 35 | + "{}/src/tests/verification_real_world/{host_name}_valid_{}.crt", |
| 36 | + env!("CARGO_MANIFEST_DIR"), |
| 37 | + i + 1 |
| 38 | + ); |
| 39 | + fs::write(&fname, der.as_ref())?; |
| 40 | + eprintln!("wrote certificate to {fname}"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +const HOSTS: &[&str] = &["letsencrypt.org"]; |
0 commit comments