|
| 1 | +#!/usr/bin/env -S cargo +nightly -Z script |
| 2 | +```cargo |
| 3 | +package.edition = "2021" |
| 4 | +dependencies.anyhow = "1" |
| 5 | +dependencies.reqwest.version = "0.11" |
| 6 | +dependencies.reqwest.default-features = false |
| 7 | +dependencies.reqwest.features = ["blocking", "rustls-tls-webpki-roots"] |
| 8 | +``` |
| 9 | + |
| 10 | +use std::{fs, path::Path}; |
| 11 | + |
| 12 | +fn main() -> anyhow::Result<()> { |
| 13 | + for (domain, output_path) in [ |
| 14 | + ("my.1password.com", "1password_com_valid_1.crt"), |
| 15 | + ("agilebits.com", "agilebits_com_valid_1.crt"), |
| 16 | + ("lencr.org", "letsencrypt_org_valid_1.crt"), |
| 17 | + ] { |
| 18 | + query(domain, output_path)?; |
| 19 | + } |
| 20 | + Ok(()) |
| 21 | +} |
| 22 | + |
| 23 | +fn query(domain: &str, path: &str) -> anyhow::Result<()> { |
| 24 | + let url = format!("https://{domain}"); |
| 25 | + let response = reqwest::blocking::Client::builder() |
| 26 | + .tls_info(true) |
| 27 | + // avoids agilebits.com redirect, which will result in the wrong cert... |
| 28 | + // we want the cert of agilebits.com, not of 1password.com |
| 29 | + .redirect(reqwest::redirect::Policy::none()) |
| 30 | + .build()? |
| 31 | + .get(url) |
| 32 | + .send()?; |
| 33 | + let Some(tls_info): Option<&reqwest::tls::TlsInfo> = response.extensions().get() else { |
| 34 | + anyhow::bail!("no TLS info found"); |
| 35 | + }; |
| 36 | + let Some(der) = tls_info.peer_certificate() else { |
| 37 | + anyhow::bail!("no TLS certificate found"); |
| 38 | + }; |
| 39 | + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join(path); |
| 40 | + eprintln!("writing DER of {domain} to {}", path.display()); |
| 41 | + fs::write(path, der)?; |
| 42 | + Ok(()) |
| 43 | +} |
0 commit comments