#[cfg(feature = "x509")]
fn main() {
use uselesskey::prelude::*;
let fx = Factory::deterministic(Seed::new([0u8; 32]));
let chain_spec = ChainSpec::new("test.example.com")
.with_sans(vec!["localhost".to_string(), "127.0.0.1".to_string()]);
let chain = fx.x509_chain("tls-server", chain_spec);
println!("=== Certificate Chain Generated ===");
println!("Root CA: {}...", &chain.root_cert_pem()[..50]);
println!("Intermediate: {}...", &chain.intermediate_cert_pem()[..50]);
println!("Leaf: {}...", &chain.leaf_cert_pem()[..50]);
println!("\n=== Chain Components ===");
println!(
"Full chain (leaf + intermediate): {} bytes PEM",
chain.chain_pem().len()
);
println!("Leaf cert DER: {} bytes", chain.leaf_cert_der().len());
println!(
"Private key: {} bytes PKCS#8",
chain.leaf_private_key_pkcs8_der().len()
);
println!("\n=== TLS Usage ===");
println!("Server presents: leaf + intermediate certificates");
println!("Client verifies: against root CA certificate");
println!("Server identity: test.example.com");
println!("\n=== Self-signed Certificate Option ===");
let self_signed = fx.x509_self_signed("self-signed-server", X509Spec::self_signed("localhost"));
println!("Certificate: {}...", &self_signed.cert_pem()[..50]);
println!(
"Private key: {}...",
&self_signed.private_key_pkcs8_pem()[..50]
);
println!(
"Combined identity: {} bytes",
self_signed.identity_pem().len()
);
println!("\n=== Negative Fixture: Expired Certificate ===");
let _expired = self_signed.expired();
println!("Created expired certificate variant for testing error handling");
println!("(In a real app, this would fail TLS handshake validation)");
println!("\n=== Tempfile Outputs ===");
let temp_cert = self_signed
.write_cert_pem()
.expect("Failed to create temp file");
let temp_key = self_signed
.write_private_key_pem()
.expect("Failed to create temp file");
println!("Certificate tempfile: {}", temp_cert.path().display());
println!("Private key tempfile: {}", temp_key.path().display());
println!("(Tempfiles auto-delete when dropped)");
}
#[cfg(not(feature = "x509"))]
fn main() {
eprintln!("Enable required feature to run this example:");
eprintln!(" cargo run -p uselesskey --example tls_server --features \"x509\"");
}