mux: sound groundwork for auto pki
This is a step towards automatic management of TLS certs for the mux stuff.
This commit is contained in:
1 parent
18a45657be
commit
28243b2a32
7 files changed
+344
-10
No files matched your search
Generated
+192
@@ -286,6 +286,12 @@ dependencies = [
|
||||
"alloc-stdlib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fb8038c1ddc0a5f73787b130f4cc75151e96ed33e417fde765eb5a81e3532f4"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.2"
|
||||
@@ -1184,6 +1190,17 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hostname"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01b1af8d6d068ba9de1c39c6ff0d879aed20f74873d4d3929a4535000bb07886"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"match_cfg",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "1.3.0"
|
||||
@@ -1303,6 +1320,15 @@ dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7889c7c36282151f6bf465be4700359318aef36baa951462382eae49e9577cf9"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kernel32-sys"
|
||||
version = "0.2.2"
|
||||
@@ -1502,6 +1528,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "match_cfg"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.8"
|
||||
@@ -2034,6 +2066,18 @@ dependencies = [
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pem"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39eb474073dfddbf7156515344266245d91ce698ddbf15e0498cef22b836f45a"
|
||||
dependencies = [
|
||||
"base64 0.10.1",
|
||||
"failure",
|
||||
"lazy_static",
|
||||
"regex 1.3.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.1.0"
|
||||
@@ -2378,6 +2422,18 @@ dependencies = [
|
||||
"num_cpus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rcgen"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6d6cbbf5f43710b9242a4897f4671a469198e2d826d9df043fc16e046f45d8a"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"pem",
|
||||
"ring",
|
||||
"yasna",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rdrand"
|
||||
version = "0.4.0"
|
||||
@@ -2459,6 +2515,21 @@ version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9e653e390eafbfebb2b3c5fcfbc90d801bc410d0de1f44f266ffbf2151d28aa"
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.16.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "113f53b644c5442e20ff3a299be3d6c61ba143737af5bd2ab298e248a7575b2d"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"spin",
|
||||
"untrusted",
|
||||
"web-sys",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-argon2"
|
||||
version = "0.7.0"
|
||||
@@ -2775,6 +2846,18 @@ dependencies = [
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sourcefile"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4bf77cb82ba8453b42b6ae1d692e4cdc92f9a47beaf89a847c8be83f4e328ad3"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "ssh2"
|
||||
version = "0.7.1"
|
||||
@@ -3229,6 +3312,12 @@ version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60369ef7a31de49bcb3f6ca728d4ba7300d9a1658f94c727d4cab8c8d9f4aece"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.1.1"
|
||||
@@ -3343,6 +3432,76 @@ version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5205e9afdf42282b192e2310a5b463a6d1c1d774e30dc3c791ac37ab42d2616c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11cdb95816290b525b32587d76419facd99662a07e59d3cdb560488a819d9a45"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"proc-macro2 1.0.8",
|
||||
"quote 1.0.2",
|
||||
"syn 1.0.14",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "574094772ce6921576fb6f2e3f7497b8a76273b6db092be18fc48a082de09dc3"
|
||||
dependencies = [
|
||||
"quote 1.0.2",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e85031354f25eaebe78bb7db1c3d86140312a911a106b2e29f9cc440ce3e7668"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8",
|
||||
"quote 1.0.2",
|
||||
"syn 1.0.14",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f5e7e61fc929f4c0dddb748b102ebf9f632e2b8d739f2016542b4de2965a9601"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-webidl"
|
||||
version = "0.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ef012a0d93fc0432df126a8eaf547b2dce25a8ce9212e1d3cbeef5c11157975d"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"heck",
|
||||
"log",
|
||||
"proc-macro2 1.0.8",
|
||||
"quote 1.0.2",
|
||||
"syn 1.0.14",
|
||||
"wasm-bindgen-backend",
|
||||
"weedle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wayland-client"
|
||||
version = "0.23.6"
|
||||
@@ -3400,6 +3559,28 @@ dependencies = [
|
||||
"dlib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aaf97caf6aa8c2b1dac90faf0db529d9d63c93846cca4911856f78a83cebf53b"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"js-sys",
|
||||
"sourcefile",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-webidl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "weedle"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bb43f70885151e629e2a19ce9e50bd730fd436cfd4b666894c9ce4de9141164"
|
||||
dependencies = [
|
||||
"nom 4.2.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wezterm"
|
||||
version = "0.1.0"
|
||||
@@ -3430,6 +3611,7 @@ dependencies = [
|
||||
"freetype 0.1.0",
|
||||
"harfbuzz",
|
||||
"hdrhistogram",
|
||||
"hostname",
|
||||
"image",
|
||||
"lazy_static",
|
||||
"leb128",
|
||||
@@ -3448,6 +3630,7 @@ dependencies = [
|
||||
"promise",
|
||||
"rangeset",
|
||||
"ratelimit_meter",
|
||||
"rcgen",
|
||||
"serde",
|
||||
"serial",
|
||||
"shared_library",
|
||||
@@ -3675,6 +3858,15 @@ version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5"
|
||||
|
||||
[[package]]
|
||||
name = "yasna"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a563d10ead87e2d798e357d44f40f495ad70bcee4d5c0d3f77a5b1b7376645d9"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zstd"
|
||||
version = "0.4.28+zstd.1.4.3"
|
||||
|
||||
@@ -32,6 +32,7 @@ failure_derive = "0.1"
|
||||
freetype = { path = "deps/freetype" }
|
||||
image = "0.21"
|
||||
harfbuzz = { path = "deps/harfbuzz" }
|
||||
hostname = "0.3"
|
||||
lazy_static = "1.3"
|
||||
leb128 = "0.2"
|
||||
libc = "0.2"
|
||||
@@ -47,6 +48,7 @@ palette = "0.5"
|
||||
portable-pty = { path = "pty", features = ["serde_support", "ssh"]}
|
||||
promise = { path = "promise" }
|
||||
ratelimit_meter = "5.0"
|
||||
rcgen = "0.7"
|
||||
serde = {version="1.0", features = ["rc", "derive"]}
|
||||
serial = "0.4"
|
||||
ssh2 = "0.7"
|
||||
|
||||
@@ -845,6 +845,10 @@ fn compute_runtime_dir() -> Result<PathBuf, Error> {
|
||||
Ok(home.join(".local/share/wezterm"))
|
||||
}
|
||||
|
||||
pub fn pki_dir() -> anyhow::Result<PathBuf> {
|
||||
compute_runtime_dir().map(|d| d.join("pki"))
|
||||
}
|
||||
|
||||
fn default_read_timeout() -> Duration {
|
||||
Duration::from_secs(60)
|
||||
}
|
||||
|
||||
+18
-3
@@ -446,13 +446,28 @@ impl Reconnectable {
|
||||
let mut connector = SslConnector::builder(SslMethod::tls())?;
|
||||
|
||||
if let Some(cert_file) = tls_client.pem_cert.as_ref() {
|
||||
connector.set_certificate_file(cert_file, SslFiletype::PEM)?;
|
||||
connector
|
||||
.set_certificate_file(&cert_file, SslFiletype::PEM)
|
||||
.context(format!(
|
||||
"set_certificate_file to {} for TLS client",
|
||||
cert_file.display()
|
||||
))?;
|
||||
}
|
||||
if let Some(chain_file) = tls_client.pem_ca.as_ref() {
|
||||
connector.set_certificate_chain_file(chain_file)?;
|
||||
connector
|
||||
.set_certificate_chain_file(&chain_file)
|
||||
.context(format!(
|
||||
"set_certificate_chain_file to {} for TLS client",
|
||||
chain_file.display()
|
||||
))?;
|
||||
}
|
||||
if let Some(key_file) = tls_client.pem_private_key.as_ref() {
|
||||
connector.set_private_key_file(key_file, SslFiletype::PEM)?;
|
||||
connector
|
||||
.set_private_key_file(&key_file, SslFiletype::PEM)
|
||||
.context(format!(
|
||||
"set_private_key_file to {} for TLS client",
|
||||
key_file.display()
|
||||
))?;
|
||||
}
|
||||
fn load_cert(name: &Path) -> anyhow::Result<X509> {
|
||||
let cert_bytes = std::fs::read(name)?;
|
||||
|
||||
@@ -13,6 +13,7 @@ mod clientsession;
|
||||
mod local;
|
||||
mod not_ossl;
|
||||
mod ossl;
|
||||
mod pki;
|
||||
mod umask;
|
||||
|
||||
#[cfg(not(any(feature = "openssl", unix)))]
|
||||
|
||||
@@ -101,17 +101,42 @@ impl OpenSSLNetListener {
|
||||
pub fn spawn_tls_listener(tls_server: &TlsDomainServer) -> Result<(), Error> {
|
||||
openssl::init();
|
||||
|
||||
let pki = super::pki::Pki::init()?;
|
||||
pki.generate_client_cert()?;
|
||||
|
||||
let mut acceptor = SslAcceptor::mozilla_modern(SslMethod::tls())?;
|
||||
|
||||
if let Some(cert_file) = tls_server.pem_cert.as_ref() {
|
||||
acceptor.set_certificate_file(cert_file, SslFiletype::PEM)?;
|
||||
}
|
||||
let cert_file = tls_server
|
||||
.pem_cert
|
||||
.clone()
|
||||
.unwrap_or_else(|| pki.server_pem());
|
||||
acceptor
|
||||
.set_certificate_file(&cert_file, SslFiletype::PEM)
|
||||
.context(format!(
|
||||
"set_certificate_file to {} for TLS listener",
|
||||
cert_file.display()
|
||||
))?;
|
||||
|
||||
if let Some(chain_file) = tls_server.pem_ca.as_ref() {
|
||||
acceptor.set_certificate_chain_file(chain_file)?;
|
||||
}
|
||||
if let Some(key_file) = tls_server.pem_private_key.as_ref() {
|
||||
acceptor.set_private_key_file(key_file, SslFiletype::PEM)?;
|
||||
acceptor
|
||||
.set_certificate_chain_file(&chain_file)
|
||||
.context(format!(
|
||||
"set_certificate_chain_file to {} for TLS listener",
|
||||
chain_file.display()
|
||||
))?;
|
||||
}
|
||||
|
||||
let key_file = tls_server
|
||||
.pem_private_key
|
||||
.clone()
|
||||
.unwrap_or_else(|| pki.server_pem());
|
||||
acceptor
|
||||
.set_private_key_file(&key_file, SslFiletype::PEM)
|
||||
.context(format!(
|
||||
"set_private_key_file to {} for TLS listener",
|
||||
key_file.display()
|
||||
))?;
|
||||
|
||||
fn load_cert(name: &Path) -> anyhow::Result<X509> {
|
||||
let cert_bytes = std::fs::read(name)?;
|
||||
log::trace!("loaded {}", name.display());
|
||||
@@ -129,6 +154,10 @@ pub fn spawn_tls_listener(tls_server: &TlsDomainServer) -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
acceptor
|
||||
.cert_store_mut()
|
||||
.add_cert(load_cert(&pki.ca_pem())?)?;
|
||||
|
||||
acceptor.set_verify(SslVerifyMode::PEER | SslVerifyMode::FAIL_IF_NO_PEER_CERT);
|
||||
|
||||
let acceptor = acceptor.build();
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
use anyhow::{anyhow, Context as _};
|
||||
use rcgen::{BasicConstraints, Certificate, CertificateParams, DistinguishedName, DnType, IsCa};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// A helper for managing keys for the TLS server component.
|
||||
/// Each time the server is started, a new CA is generated
|
||||
/// and used to generate a new server key, invalidating all
|
||||
/// prior keys.
|
||||
/// The idea is that the client connects via some other secure
|
||||
/// channel (eg: ssh to reach the host, then unix domain to access
|
||||
/// the server) to make a request for the key information.
|
||||
/// We'll generate that request a new client cert and return
|
||||
/// both the public CA certificate informat and that key to the client.
|
||||
/// The client will use both of those things to connect to the TLS
|
||||
/// server.
|
||||
pub struct Pki {
|
||||
ca_cert: Certificate,
|
||||
pki_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Pki {
|
||||
pub fn init() -> anyhow::Result<Self> {
|
||||
let pki_dir = crate::config::pki_dir()?;
|
||||
std::fs::create_dir_all(&pki_dir)?;
|
||||
log::error!("runtime dir is {}", pki_dir.display());
|
||||
|
||||
let alt_names = vec![
|
||||
hostname::get()?
|
||||
.into_string()
|
||||
.map_err(|_| anyhow!("hostname is not representable as unicode"))?,
|
||||
"localhost".to_owned(),
|
||||
];
|
||||
let unix_name = std::env::var("USER")?;
|
||||
|
||||
// Create the CA certificate
|
||||
let mut ca_params = CertificateParams::new(alt_names.clone());
|
||||
ca_params.is_ca = IsCa::Ca(BasicConstraints::Constrained(1));
|
||||
ca_params.serial_number = Some(0);
|
||||
let ca_cert = Certificate::from_params(ca_params)?;
|
||||
let ca_pem = ca_cert.serialize_pem()?;
|
||||
let ca_pem_path = pki_dir.join("ca.pem");
|
||||
std::fs::write(&ca_pem_path, ca_pem.as_bytes())
|
||||
.context(format!("saving {}", ca_pem_path.display()))?;
|
||||
|
||||
let mut params = CertificateParams::new(alt_names);
|
||||
let mut dn = DistinguishedName::new();
|
||||
dn.push(DnType::CommonName, unix_name);
|
||||
params.distinguished_name = dn;
|
||||
|
||||
let server_cert = Certificate::from_params(params)?;
|
||||
let mut signed_cert = server_cert.serialize_pem_with_signer(&ca_cert)?;
|
||||
let key_bits = server_cert.get_key_pair().serialize_pem();
|
||||
signed_cert.push_str(&key_bits);
|
||||
|
||||
let server_pem_path = pki_dir.join("server.pem");
|
||||
std::fs::write(&server_pem_path, signed_cert.as_bytes())
|
||||
.context(format!("saving {}", server_pem_path.display()))?;
|
||||
|
||||
Ok(Self { pki_dir, ca_cert })
|
||||
}
|
||||
|
||||
pub fn generate_client_cert(&self) -> anyhow::Result<String> {
|
||||
let unix_name = std::env::var("USER")?;
|
||||
|
||||
let mut params = CertificateParams::new(vec![unix_name.clone()]);
|
||||
let mut dn = DistinguishedName::new();
|
||||
dn.push(DnType::CommonName, unix_name);
|
||||
params.distinguished_name = dn;
|
||||
|
||||
let client_cert = Certificate::from_params(params)?;
|
||||
let mut signed_cert = client_cert.serialize_pem_with_signer(&self.ca_cert)?;
|
||||
let key_bits = client_cert.get_key_pair().serialize_pem();
|
||||
signed_cert.push_str(&key_bits);
|
||||
|
||||
/* TODO: remove the bit that writes out the client pem here;
|
||||
* this is just so I can test this locally */
|
||||
let client_pem_path = self.pki_dir.join("client.pem");
|
||||
std::fs::write(&client_pem_path, signed_cert.as_bytes())
|
||||
.context(format!("saving {}", client_pem_path.display()))?;
|
||||
|
||||
Ok(signed_cert)
|
||||
}
|
||||
|
||||
pub fn ca_pem(&self) -> PathBuf {
|
||||
self.pki_dir.join("ca.pem")
|
||||
}
|
||||
|
||||
pub fn server_pem(&self) -> PathBuf {
|
||||
self.pki_dir.join("server.pem")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user