certificate_manager 0.2.2

A library for managing and spoofing X.509 certificates
Documentation
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use argh::FromArgs;
use certificate_manager::certificate::CertificateAuthority;
use rustls_pki_types::{pem::PemObject, PrivateKeyDer};
use std::io;

/// Sign a x509 v2 certificate for a given domain and save it out to a file
#[derive(FromArgs)]
struct StartServer {
    /// pem file containing the ca certificate
    #[argh(
        option,
        short = 'c',
        default = "\"./examples/ca_certs/example.com.pem\".to_string()"
    )]
    cert_file: String,

    /// pem file containing ca key
    #[argh(
        option,
        short = 'k',
        default = "\"./examples/ca_certs/key.pem\".to_string()"
    )]
    key_file: String,

    /// passphrase_for_key
    #[argh(option, short = 'p')]
    passphrase: Option<String>,
}

async fn index() -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok().content_type("text/html").body(
        "<html><head><title>Welcome</title></head><body>Welcome to the Actix server!</body></html>",
    ))
}

#[actix_web::main]
async fn main() -> io::Result<()> {
    let args: StartServer = argh::from_env();

    // load TLS certs and key
    let ca =
        CertificateAuthority::load_from_pem_files(args.cert_file, args.key_file, args.passphrase)
            .unwrap();

    let key = PrivateKeyDer::from_pem_slice(ca.key_pair.serialize_pem().as_bytes()).unwrap();

    let tls_config = rustls::ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(vec![ca.cert.der().clone()], key)
        .unwrap();

    // Start the server
    println!("Server is starting at https://127.0.0.1:4443");
    HttpServer::new(|| {
        App::new()
            .wrap(middleware::Logger::default())
            .service(web::resource("/").to(index))
    })
    .bind_rustls_0_23(("127.0.0.1", 4443), tls_config)?
    .run()
    .await?;

    Ok(())
}