@@ -0,0 +1,409 @@ |
| 1 | +//! TLS certificate management for HyprKVM |
| 2 | +//! |
| 3 | +//! Handles certificate generation, loading, and fingerprint calculation. |
| 4 | + |
| 5 | +use std::fs; |
| 6 | +use std::io::{self, BufReader}; |
| 7 | +use std::path::Path; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use rcgen::{CertificateParams, DistinguishedName, DnType, KeyPair, PKCS_ECDSA_P256_SHA256}; |
| 11 | +use rustls::pki_types::{CertificateDer, PrivateKeyDer}; |
| 12 | +use rustls::{ClientConfig, ServerConfig}; |
| 13 | +use rustls_pemfile::{certs, private_key}; |
| 14 | +use sha2::{Digest, Sha256}; |
| 15 | +use tokio_rustls::{TlsAcceptor, TlsConnector}; |
| 16 | + |
| 17 | +/// TLS-related errors |
| 18 | +#[derive(Debug, thiserror::Error)] |
| 19 | +pub enum TlsError { |
| 20 | + #[error("IO error: {0}")] |
| 21 | + Io(#[from] io::Error), |
| 22 | + |
| 23 | + #[error("Certificate generation error: {0}")] |
| 24 | + CertGen(String), |
| 25 | + |
| 26 | + #[error("Certificate loading error: {0}")] |
| 27 | + CertLoad(String), |
| 28 | + |
| 29 | + #[error("TLS configuration error: {0}")] |
| 30 | + Config(String), |
| 31 | + |
| 32 | + #[error("Certificate verification failed: {0}")] |
| 33 | + Verification(String), |
| 34 | +} |
| 35 | + |
| 36 | +/// Certificate fingerprint (SHA-256) |
| 37 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 38 | +pub struct Fingerprint(pub [u8; 32]); |
| 39 | + |
| 40 | +impl Fingerprint { |
| 41 | + /// Calculate fingerprint from DER-encoded certificate |
| 42 | + pub fn from_der(der: &[u8]) -> Self { |
| 43 | + let mut hasher = Sha256::new(); |
| 44 | + hasher.update(der); |
| 45 | + let result = hasher.finalize(); |
| 46 | + let mut bytes = [0u8; 32]; |
| 47 | + bytes.copy_from_slice(&result); |
| 48 | + Self(bytes) |
| 49 | + } |
| 50 | + |
| 51 | + /// Convert to hex string (colon-separated, uppercase) |
| 52 | + pub fn to_hex(&self) -> String { |
| 53 | + self.0 |
| 54 | + .iter() |
| 55 | + .map(|b| format!("{:02X}", b)) |
| 56 | + .collect::<Vec<_>>() |
| 57 | + .join(":") |
| 58 | + } |
| 59 | + |
| 60 | + /// Parse from hex string (colon-separated or continuous) |
| 61 | + pub fn from_hex(s: &str) -> Result<Self, TlsError> { |
| 62 | + let clean: String = s.chars().filter(|c| c.is_ascii_hexdigit()).collect(); |
| 63 | + if clean.len() != 64 { |
| 64 | + return Err(TlsError::Verification(format!( |
| 65 | + "Invalid fingerprint length: expected 64 hex chars, got {}", |
| 66 | + clean.len() |
| 67 | + ))); |
| 68 | + } |
| 69 | + |
| 70 | + let mut bytes = [0u8; 32]; |
| 71 | + for (i, chunk) in clean.as_bytes().chunks(2).enumerate() { |
| 72 | + let hex = std::str::from_utf8(chunk).unwrap(); |
| 73 | + bytes[i] = u8::from_str_radix(hex, 16) |
| 74 | + .map_err(|e| TlsError::Verification(format!("Invalid hex: {}", e)))?; |
| 75 | + } |
| 76 | + |
| 77 | + Ok(Self(bytes)) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl std::fmt::Display for Fingerprint { |
| 82 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 83 | + write!(f, "{}", self.to_hex()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Generate a self-signed ECDSA P-256 certificate |
| 88 | +pub fn generate_certificate(machine_name: &str) -> Result<(String, String), TlsError> { |
| 89 | + tracing::info!("Generating new self-signed certificate for '{}'", machine_name); |
| 90 | + |
| 91 | + // Generate ECDSA P-256 key pair |
| 92 | + let key_pair = KeyPair::generate_for(&PKCS_ECDSA_P256_SHA256) |
| 93 | + .map_err(|e| TlsError::CertGen(format!("Failed to generate key pair: {}", e)))?; |
| 94 | + |
| 95 | + // Build certificate parameters |
| 96 | + let mut params = CertificateParams::default(); |
| 97 | + |
| 98 | + // Set distinguished name |
| 99 | + let mut dn = DistinguishedName::new(); |
| 100 | + dn.push(DnType::CommonName, machine_name); |
| 101 | + dn.push(DnType::OrganizationName, "HyprKVM"); |
| 102 | + params.distinguished_name = dn; |
| 103 | + |
| 104 | + // Set validity (10 years) |
| 105 | + params.not_before = time::OffsetDateTime::now_utc(); |
| 106 | + params.not_after = params.not_before + time::Duration::days(3650); |
| 107 | + |
| 108 | + // Add Subject Alternative Names |
| 109 | + params.subject_alt_names = vec![ |
| 110 | + rcgen::SanType::DnsName(machine_name.try_into().map_err(|e| { |
| 111 | + TlsError::CertGen(format!("Invalid machine name for SAN: {}", e)) |
| 112 | + })?), |
| 113 | + ]; |
| 114 | + |
| 115 | + // Generate certificate |
| 116 | + let cert = params |
| 117 | + .self_signed(&key_pair) |
| 118 | + .map_err(|e| TlsError::CertGen(format!("Failed to generate certificate: {}", e)))?; |
| 119 | + |
| 120 | + // Serialize to PEM |
| 121 | + let cert_pem = cert.pem(); |
| 122 | + let key_pem = key_pair.serialize_pem(); |
| 123 | + |
| 124 | + tracing::info!("Certificate generated successfully"); |
| 125 | + |
| 126 | + Ok((cert_pem, key_pem)) |
| 127 | +} |
| 128 | + |
| 129 | +/// Ensure certificate exists, generating if needed |
| 130 | +pub fn ensure_certificate( |
| 131 | + cert_path: &Path, |
| 132 | + key_path: &Path, |
| 133 | + machine_name: &str, |
| 134 | +) -> Result<(), TlsError> { |
| 135 | + if cert_path.exists() && key_path.exists() { |
| 136 | + tracing::debug!("Certificate files exist: {:?}, {:?}", cert_path, key_path); |
| 137 | + return Ok(()); |
| 138 | + } |
| 139 | + |
| 140 | + tracing::info!("Certificate files not found, generating new certificate"); |
| 141 | + |
| 142 | + let (cert_pem, key_pem) = generate_certificate(machine_name)?; |
| 143 | + |
| 144 | + // Ensure parent directories exist |
| 145 | + if let Some(parent) = cert_path.parent() { |
| 146 | + fs::create_dir_all(parent)?; |
| 147 | + } |
| 148 | + if let Some(parent) = key_path.parent() { |
| 149 | + fs::create_dir_all(parent)?; |
| 150 | + } |
| 151 | + |
| 152 | + // Write certificate |
| 153 | + fs::write(cert_path, &cert_pem)?; |
| 154 | + tracing::info!("Wrote certificate to {:?}", cert_path); |
| 155 | + |
| 156 | + // Write private key with restrictive permissions |
| 157 | + #[cfg(unix)] |
| 158 | + { |
| 159 | + use std::os::unix::fs::OpenOptionsExt; |
| 160 | + let mut opts = fs::OpenOptions::new(); |
| 161 | + opts.write(true).create(true).truncate(true).mode(0o600); |
| 162 | + let mut file = opts.open(key_path)?; |
| 163 | + std::io::Write::write_all(&mut file, key_pem.as_bytes())?; |
| 164 | + } |
| 165 | + #[cfg(not(unix))] |
| 166 | + { |
| 167 | + fs::write(key_path, &key_pem)?; |
| 168 | + } |
| 169 | + tracing::info!("Wrote private key to {:?}", key_path); |
| 170 | + |
| 171 | + Ok(()) |
| 172 | +} |
| 173 | + |
| 174 | +/// Load certificate chain from PEM file |
| 175 | +pub fn load_certs(path: &Path) -> Result<Vec<CertificateDer<'static>>, TlsError> { |
| 176 | + let file = fs::File::open(path) |
| 177 | + .map_err(|e| TlsError::CertLoad(format!("Failed to open {:?}: {}", path, e)))?; |
| 178 | + let mut reader = BufReader::new(file); |
| 179 | + |
| 180 | + let certs: Vec<CertificateDer<'static>> = certs(&mut reader) |
| 181 | + .collect::<Result<Vec<_>, _>>() |
| 182 | + .map_err(|e| TlsError::CertLoad(format!("Failed to parse certificates: {}", e)))?; |
| 183 | + |
| 184 | + if certs.is_empty() { |
| 185 | + return Err(TlsError::CertLoad("No certificates found in file".into())); |
| 186 | + } |
| 187 | + |
| 188 | + Ok(certs) |
| 189 | +} |
| 190 | + |
| 191 | +/// Load private key from PEM file |
| 192 | +pub fn load_private_key(path: &Path) -> Result<PrivateKeyDer<'static>, TlsError> { |
| 193 | + let file = fs::File::open(path) |
| 194 | + .map_err(|e| TlsError::CertLoad(format!("Failed to open {:?}: {}", path, e)))?; |
| 195 | + let mut reader = BufReader::new(file); |
| 196 | + |
| 197 | + private_key(&mut reader) |
| 198 | + .map_err(|e| TlsError::CertLoad(format!("Failed to parse private key: {}", e)))? |
| 199 | + .ok_or_else(|| TlsError::CertLoad("No private key found in file".into())) |
| 200 | +} |
| 201 | + |
| 202 | +/// Get fingerprint of certificate file |
| 203 | +pub fn get_cert_fingerprint(path: &Path) -> Result<Fingerprint, TlsError> { |
| 204 | + let certs = load_certs(path)?; |
| 205 | + if let Some(cert) = certs.first() { |
| 206 | + Ok(Fingerprint::from_der(cert.as_ref())) |
| 207 | + } else { |
| 208 | + Err(TlsError::CertLoad("No certificate in file".into())) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +/// Create a TLS acceptor for server-side connections |
| 213 | +pub fn create_tls_acceptor(cert_path: &Path, key_path: &Path) -> Result<TlsAcceptor, TlsError> { |
| 214 | + let certs = load_certs(cert_path)?; |
| 215 | + let key = load_private_key(key_path)?; |
| 216 | + |
| 217 | + let config = ServerConfig::builder() |
| 218 | + .with_no_client_auth() |
| 219 | + .with_single_cert(certs, key) |
| 220 | + .map_err(|e| TlsError::Config(format!("Failed to create server config: {}", e)))?; |
| 221 | + |
| 222 | + Ok(TlsAcceptor::from(Arc::new(config))) |
| 223 | +} |
| 224 | + |
| 225 | +/// Custom certificate verifier that supports TOFU and fingerprint pinning |
| 226 | +#[derive(Debug)] |
| 227 | +pub struct HyprkvmCertVerifier { |
| 228 | + /// Expected fingerprint (if pinned) |
| 229 | + expected_fingerprint: Option<Fingerprint>, |
| 230 | + /// Callback to handle TOFU (returns true to accept, false to reject) |
| 231 | + /// If None, TOFU is disabled |
| 232 | + tofu_enabled: bool, |
| 233 | +} |
| 234 | + |
| 235 | +impl HyprkvmCertVerifier { |
| 236 | + /// Create verifier with pinned fingerprint |
| 237 | + pub fn with_fingerprint(fingerprint: Fingerprint) -> Self { |
| 238 | + Self { |
| 239 | + expected_fingerprint: Some(fingerprint), |
| 240 | + tofu_enabled: false, |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + /// Create verifier with TOFU enabled |
| 245 | + pub fn with_tofu() -> Self { |
| 246 | + Self { |
| 247 | + expected_fingerprint: None, |
| 248 | + tofu_enabled: true, |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + /// Create verifier with fingerprint, falling back to TOFU if not set |
| 253 | + pub fn new(fingerprint: Option<Fingerprint>, tofu_enabled: bool) -> Self { |
| 254 | + Self { |
| 255 | + expected_fingerprint: fingerprint, |
| 256 | + tofu_enabled, |
| 257 | + } |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +impl rustls::client::danger::ServerCertVerifier for HyprkvmCertVerifier { |
| 262 | + fn verify_server_cert( |
| 263 | + &self, |
| 264 | + end_entity: &CertificateDer<'_>, |
| 265 | + _intermediates: &[CertificateDer<'_>], |
| 266 | + _server_name: &rustls::pki_types::ServerName<'_>, |
| 267 | + _ocsp_response: &[u8], |
| 268 | + _now: rustls::pki_types::UnixTime, |
| 269 | + ) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> { |
| 270 | + let fingerprint = Fingerprint::from_der(end_entity.as_ref()); |
| 271 | + |
| 272 | + if let Some(ref expected) = self.expected_fingerprint { |
| 273 | + // Fingerprint pinning mode |
| 274 | + if fingerprint == *expected { |
| 275 | + tracing::debug!("Certificate fingerprint matches pinned value"); |
| 276 | + return Ok(rustls::client::danger::ServerCertVerified::assertion()); |
| 277 | + } else { |
| 278 | + tracing::error!( |
| 279 | + "Certificate fingerprint mismatch! Expected: {}, Got: {}", |
| 280 | + expected, |
| 281 | + fingerprint |
| 282 | + ); |
| 283 | + return Err(rustls::Error::General( |
| 284 | + "Certificate fingerprint mismatch".into(), |
| 285 | + )); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + if self.tofu_enabled { |
| 290 | + // TOFU mode - accept any certificate but log for manual verification |
| 291 | + tracing::warn!( |
| 292 | + "TOFU: Accepting certificate with fingerprint: {}", |
| 293 | + fingerprint |
| 294 | + ); |
| 295 | + tracing::warn!("TOFU: Add this fingerprint to config to pin the certificate"); |
| 296 | + return Ok(rustls::client::danger::ServerCertVerified::assertion()); |
| 297 | + } |
| 298 | + |
| 299 | + Err(rustls::Error::General( |
| 300 | + "No verification method configured".into(), |
| 301 | + )) |
| 302 | + } |
| 303 | + |
| 304 | + fn verify_tls12_signature( |
| 305 | + &self, |
| 306 | + message: &[u8], |
| 307 | + cert: &CertificateDer<'_>, |
| 308 | + dss: &rustls::DigitallySignedStruct, |
| 309 | + ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> { |
| 310 | + rustls::crypto::verify_tls12_signature( |
| 311 | + message, |
| 312 | + cert, |
| 313 | + dss, |
| 314 | + &rustls::crypto::aws_lc_rs::default_provider().signature_verification_algorithms, |
| 315 | + ) |
| 316 | + } |
| 317 | + |
| 318 | + fn verify_tls13_signature( |
| 319 | + &self, |
| 320 | + message: &[u8], |
| 321 | + cert: &CertificateDer<'_>, |
| 322 | + dss: &rustls::DigitallySignedStruct, |
| 323 | + ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> { |
| 324 | + rustls::crypto::verify_tls13_signature( |
| 325 | + message, |
| 326 | + cert, |
| 327 | + dss, |
| 328 | + &rustls::crypto::aws_lc_rs::default_provider().signature_verification_algorithms, |
| 329 | + ) |
| 330 | + } |
| 331 | + |
| 332 | + fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> { |
| 333 | + rustls::crypto::aws_lc_rs::default_provider() |
| 334 | + .signature_verification_algorithms |
| 335 | + .supported_schemes() |
| 336 | + } |
| 337 | +} |
| 338 | + |
| 339 | +/// Create a TLS connector for client-side connections |
| 340 | +pub fn create_tls_connector( |
| 341 | + expected_fingerprint: Option<&str>, |
| 342 | + tofu_enabled: bool, |
| 343 | +) -> Result<TlsConnector, TlsError> { |
| 344 | + let fingerprint = match expected_fingerprint { |
| 345 | + Some(fp) => Some(Fingerprint::from_hex(fp)?), |
| 346 | + None => None, |
| 347 | + }; |
| 348 | + |
| 349 | + let verifier = HyprkvmCertVerifier::new(fingerprint, tofu_enabled); |
| 350 | + |
| 351 | + let config = ClientConfig::builder() |
| 352 | + .dangerous() |
| 353 | + .with_custom_certificate_verifier(Arc::new(verifier)) |
| 354 | + .with_no_client_auth(); |
| 355 | + |
| 356 | + Ok(TlsConnector::from(Arc::new(config))) |
| 357 | +} |
| 358 | + |
| 359 | +#[cfg(test)] |
| 360 | +mod tests { |
| 361 | + use super::*; |
| 362 | + use tempfile::tempdir; |
| 363 | + |
| 364 | + #[test] |
| 365 | + fn test_fingerprint_roundtrip() { |
| 366 | + let bytes = [0xABu8; 32]; |
| 367 | + let fp = Fingerprint(bytes); |
| 368 | + let hex = fp.to_hex(); |
| 369 | + let parsed = Fingerprint::from_hex(&hex).unwrap(); |
| 370 | + assert_eq!(fp, parsed); |
| 371 | + } |
| 372 | + |
| 373 | + #[test] |
| 374 | + fn test_generate_certificate() { |
| 375 | + let (cert_pem, key_pem) = generate_certificate("test-machine").unwrap(); |
| 376 | + assert!(cert_pem.contains("-----BEGIN CERTIFICATE-----")); |
| 377 | + assert!(key_pem.contains("-----BEGIN PRIVATE KEY-----")); |
| 378 | + } |
| 379 | + |
| 380 | + #[test] |
| 381 | + fn test_ensure_certificate() { |
| 382 | + let dir = tempdir().unwrap(); |
| 383 | + let cert_path = dir.path().join("cert.pem"); |
| 384 | + let key_path = dir.path().join("key.pem"); |
| 385 | + |
| 386 | + ensure_certificate(&cert_path, &key_path, "test-machine").unwrap(); |
| 387 | + |
| 388 | + assert!(cert_path.exists()); |
| 389 | + assert!(key_path.exists()); |
| 390 | + |
| 391 | + // Second call should not regenerate |
| 392 | + let cert_content = fs::read_to_string(&cert_path).unwrap(); |
| 393 | + ensure_certificate(&cert_path, &key_path, "test-machine").unwrap(); |
| 394 | + let cert_content2 = fs::read_to_string(&cert_path).unwrap(); |
| 395 | + assert_eq!(cert_content, cert_content2); |
| 396 | + } |
| 397 | + |
| 398 | + #[test] |
| 399 | + fn test_fingerprint_calculation() { |
| 400 | + let dir = tempdir().unwrap(); |
| 401 | + let cert_path = dir.path().join("cert.pem"); |
| 402 | + let key_path = dir.path().join("key.pem"); |
| 403 | + |
| 404 | + ensure_certificate(&cert_path, &key_path, "test-machine").unwrap(); |
| 405 | + |
| 406 | + let fp = get_cert_fingerprint(&cert_path).unwrap(); |
| 407 | + assert_eq!(fp.to_hex().len(), 95); // 32 bytes * 2 + 31 colons |
| 408 | + } |
| 409 | +} |