local-ssl 0.1.0

Local HTTPS certificate generation for development — pair with local-dns
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use colored::Colorize;
use rcgen::{CertificateParams, DnType, ExtendedKeyUsagePurpose, Issuer, KeyPair, KeyUsagePurpose};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::time::Duration;
use x509_parser::extensions::GeneralName;
use x509_parser::prelude::*;

use crate::ca::CaStore;
use crate::util;

pub struct CertBundle {
    #[allow(dead_code)]
    pub domain: String,
    pub cert_path: String,
    pub key_path: String,
}

struct ParsedCert {
    cn: String,
    not_before: ::time::OffsetDateTime,
    not_after: ::time::OffsetDateTime,
    cert_path: PathBuf,
    key_path: PathBuf,
}

pub fn generate(domain: &str, ca_store: &CaStore, sans: &[String]) -> Result<CertBundle, String> {
    let out_dir = ca_store.dir.join("certs").join(domain);
    fs::create_dir_all(&out_dir).map_err(|e| format!("Cannot create {out_dir:?}: {e}"))?;

    let key_pair = KeyPair::generate().map_err(|e| format!("Cannot generate key: {e}"))?;

    let mut alt_names = vec![domain.to_string()];
    for san in sans {
        if san != domain {
            alt_names.push(san.clone());
        }
    }
    if !domain.starts_with("*.") {
        alt_names.push(format!("*.{domain}"));
    }

    let mut params =
        CertificateParams::new(alt_names).map_err(|e| format!("Cannot create params: {e}"))?;
    params.distinguished_name.push(DnType::CommonName, domain);
    params.key_usages = vec![
        KeyUsagePurpose::DigitalSignature,
        KeyUsagePurpose::KeyEncipherment,
    ];
    params.extended_key_usages = vec![
        ExtendedKeyUsagePurpose::ServerAuth,
        ExtendedKeyUsagePurpose::ClientAuth,
    ];
    params.not_before = ::time::OffsetDateTime::now_utc();
    params.not_after = ::time::OffsetDateTime::now_utc() + Duration::from_secs(365 * 86400);

    let ca_key = ca_store.load_key()?;
    let ca_params = crate::ca::CaStore::ca_params();
    let issuer = Issuer::new(ca_params, &ca_key);

    let cert = params
        .signed_by(&key_pair, &issuer)
        .map_err(|e| format!("Cannot sign cert: {e}"))?;

    let cert_path = out_dir.join("cert.pem");
    let key_path = out_dir.join("key.pem");

    fs::write(&cert_path, cert.pem()).map_err(|e| format!("Cannot write cert: {e}"))?;
    fs::write(&key_path, key_pair.serialize_pem()).map_err(|e| format!("Cannot write key: {e}"))?;

    Ok(CertBundle {
        domain: domain.to_string(),
        cert_path: cert_path.to_string_lossy().to_string(),
        key_path: key_path.to_string_lossy().to_string(),
    })
}

pub fn list(ca_store: &CaStore) -> Result<Vec<String>, String> {
    let certs_dir = ca_store.dir.join("certs");
    if !certs_dir.is_dir() {
        return Ok(Vec::new());
    }
    let mut domains = Vec::new();
    for entry in fs::read_dir(&certs_dir).map_err(|e| format!("{e}"))? {
        let entry = entry.map_err(|e| format!("{e}"))?;
        if entry.path().is_dir() && entry.path().join("cert.pem").exists() {
            domains.push(entry.file_name().to_string_lossy().to_string());
        }
    }
    domains.sort();
    Ok(domains)
}

fn parse_local_cert(domain: &str, ca_store: &CaStore) -> Result<ParsedCert, String> {
    let cert_path = ca_store.dir.join("certs").join(domain).join("cert.pem");
    if !cert_path.exists() {
        return Err(format!("No certificate for '{domain}'"));
    }

    let pem_data = fs::read_to_string(&cert_path).map_err(|e| format!("Cannot read cert: {e}"))?;
    let der = util::pem_decode(&pem_data)?;
    let (_, parsed) =
        X509Certificate::from_der(&der).map_err(|e| format!("Cannot parse cert: {e}"))?;

    let cn = parsed
        .subject()
        .iter_common_name()
        .next()
        .and_then(|a| a.as_str().ok())
        .unwrap_or("(unknown)")
        .to_string();

    Ok(ParsedCert {
        cn,
        not_before: parsed.validity().not_before.to_datetime(),
        not_after: parsed.validity().not_after.to_datetime(),
        cert_path: cert_path.clone(),
        key_path: ca_store.dir.join("certs").join(domain).join("key.pem"),
    })
}

pub fn show(domain: &str, ca_store: &CaStore) -> Result<String, String> {
    let p = parse_local_cert(domain, ca_store)?;

    let issuer_str = {
        let pem_data =
            fs::read_to_string(&p.cert_path).map_err(|e| format!("Cannot read cert: {e}"))?;
        let der = util::pem_decode(&pem_data)?;
        let (_, parsed) =
            X509Certificate::from_der(&der).map_err(|e| format!("Cannot parse cert: {e}"))?;
        let out = parsed
            .issuer()
            .iter_common_name()
            .next()
            .and_then(|a| a.as_str().ok())
            .unwrap_or("(unknown)")
            .to_string();
        out
    };

    let sans: Vec<String> = {
        let pem_data =
            fs::read_to_string(&p.cert_path).map_err(|e| format!("Cannot read cert: {e}"))?;
        let der = util::pem_decode(&pem_data)?;
        let (_, parsed) =
            X509Certificate::from_der(&der).map_err(|e| format!("Cannot parse cert: {e}"))?;
        parsed
            .subject_alternative_name()
            .map_err(|e| format!("Cannot parse SANs: {e}"))?
            .map(|ext| {
                ext.value
                    .general_names
                    .iter()
                    .filter_map(|gn| {
                        if let GeneralName::DNSName(s) = gn {
                            Some(s.to_string())
                        } else {
                            None
                        }
                    })
                    .collect()
            })
            .unwrap_or_default()
    };

    let mut out = format!("Domain:        {}\n", p.cn);
    out.push_str(&format!("Issuer:        {issuer_str}\n"));
    out.push_str(&format!("Valid from:    {}\n", p.not_before));
    out.push_str(&format!("Valid until:   {}\n", p.not_after));
    out.push_str(&format!("SANs:          {}\n", sans.join(", ")));
    out.push_str(&format!("Cert:          {}\n", p.cert_path.display()));
    out.push_str(&format!("Key:           {}\n", p.key_path.display()));

    Ok(out)
}

pub fn check_local(domain: &str, ca_store: &CaStore) -> Result<String, String> {
    let p = parse_local_cert(domain, ca_store)?;
    let now = ::time::OffsetDateTime::now_utc();

    let validity_str = if now < p.not_before {
        format!("{}", "not yet valid".red().bold())
    } else if now > p.not_after {
        format!("{}", "expired".red().bold())
    } else {
        let days_left = (p.not_after - now).whole_days();
        if days_left < 30 {
            format!("{} ({days_left} days remaining)", "valid".yellow())
        } else {
            format!("{} ({days_left} days remaining)", "valid".green())
        }
    };

    let gen_date = fs::metadata(&p.cert_path)
        .ok()
        .and_then(|m| m.created().or(m.modified()).ok())
        .map(|t| {
            let dt: ::time::OffsetDateTime = t.into();
            dt.to_string()
        })
        .unwrap_or_else(|| "(unknown)".to_string());

    let key_ok = p.key_path.exists();

    let mut out = format!("{} {}\n", "Domain:".cyan().bold(), p.cn.green());
    out.push_str(&format!("{} {}\n", "Validity:".bold(), validity_str));
    out.push_str(&format!("{} {}\n", "Generated:".bold(), gen_date));
    out.push_str(&format!("{} {}\n", "Expires:".bold(), p.not_after));
    let key_status = if key_ok { "present".green() } else { "missing".red() };
    out.push_str(&format!("{} {}\n", "Key file:".bold(), key_status));
    out.push_str(&format!(
        "{} {}\n",
        "Reload needed:".bold(),
        "no (certs are file-based, restart your server)".green()
    ));

    Ok(out)
}

pub fn check_remote(host: &str, port: u16) -> Result<String, String> {
    let addr = format!("{host}:{port}");
    let mut tcp = std::net::TcpStream::connect(&addr)
        .map_err(|e| format!("Cannot connect to {addr}: {e}"))?;

    let server_name = rustls::pki_types::ServerName::try_from(host)
        .map_err(|_| format!("Invalid hostname: {host}"))?
        .to_owned();

    let root_store =
        rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    let config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();
    let mut conn = rustls::ClientConnection::new(std::sync::Arc::new(config), server_name)
        .map_err(|e| format!("TLS error: {e}"))?;

    let mut tls = rustls::Stream::new(&mut conn, &mut tcp);
    tls.flush()
        .map_err(|e| format!("TLS handshake error: {e}"))?;

    let certs = conn.peer_certificates().unwrap_or(&[]);
    if certs.is_empty() {
        return Err("No certificate presented by server".into());
    }

    let mut out = format!(
        "{} {}:{}\n",
        "Remote server:".cyan().bold(),
        host.cyan(),
        port.to_string().cyan()
    );
    out.push_str(&format!(
        "{} {}\n\n",
        "Certificate chain:".bold(),
        format!("{} certs", certs.len()).cyan()
    ));

    for (i, cert_der) in certs.iter().enumerate() {
        let (_, parsed) = X509Certificate::from_der(cert_der)
            .map_err(|e| format!("Cannot parse cert #{i}: {e}"))?;

        let cn = parsed
            .subject()
            .iter_common_name()
            .next()
            .and_then(|a| a.as_str().ok())
            .unwrap_or("(unknown)");

        let issuer = parsed
            .issuer()
            .iter_common_name()
            .next()
            .and_then(|a| a.as_str().ok())
            .unwrap_or("(unknown)");

        let not_before = parsed.validity().not_before.to_datetime();
        let not_after = parsed.validity().not_after.to_datetime();
        let now = ::time::OffsetDateTime::now_utc();

        let validity = if now < not_before {
            format!("{}", "not yet valid".red())
        } else if now > not_after {
            format!("{}", "expired".red().bold())
        } else {
            let days = (not_after - now).whole_days();
            format!("{} ({days} days)", "valid".green())
        };

        let sans: Vec<String> = parsed
            .subject_alternative_name()
            .ok()
            .flatten()
            .map(|ext| {
                ext.value
                    .general_names
                    .iter()
                    .filter_map(|gn| {
                        if let GeneralName::DNSName(s) = gn {
                            Some(s.to_string())
                        } else {
                            None
                        }
                    })
                    .collect()
            })
            .unwrap_or_default();

        out.push_str(&format!(
            "  {} {} (index {i})\n",
            "Certificate".bold(),
            (i + 1).to_string().cyan()
        ));
        out.push_str(&format!("    {} {}\n", "Subject:".bold(), cn));
        out.push_str(&format!("    {} {}\n", "Issuer:".bold(), issuer));
        out.push_str(&format!("    {} {}\n", "Validity:".bold(), validity));
        out.push_str(&format!("    {} {}\n", "Valid from:".bold(), not_before));
        out.push_str(&format!("    {} {}\n", "Valid until:".bold(), not_after));
        if !sans.is_empty() {
            out.push_str(&format!("    {} {}\n", "SANs:".bold(), sans.join(", ")));
        }
        out.push('\n');
    }

    Ok(out.trim().to_string())
}

pub fn check_all_local(ca_store: &CaStore) -> Result<String, String> {
    let domains = list(ca_store)?;
    if domains.is_empty() {
        return Ok(format!("{}", "No certificates found.".yellow()));
    }

    let mut out = String::new();
    for d in &domains {
        let result = check_local(d, ca_store)?;
        out.push_str(&result);
        out.push('\n');
    }
    Ok(out.trim().to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ca::CaStore;
    use std::path::Path;
    use tempfile::tempdir;

    fn setup() -> (CaStore, tempfile::TempDir) {
        let dir = tempdir().unwrap();
        let store = CaStore {
            dir: dir.path().to_path_buf(),
            key_path: dir.path().join("ca-key.pem"),
            cert_path: dir.path().join("ca-cert.pem"),
        };
        store.init().unwrap();
        (store, dir)
    }

    #[test]
    fn test_generate_creates_files() {
        let (store, _dir) = setup();
        let bundle = generate("test.local", &store, &[]).unwrap();
        assert!(Path::new(&bundle.cert_path).exists());
        assert!(Path::new(&bundle.key_path).exists());
        assert_eq!(bundle.domain, "test.local");
    }

    #[test]
    fn test_generate_with_sans() {
        let (store, _dir) = setup();
        let _bundle = generate(
            "primary.test",
            &store,
            &["san1.test".to_string(), "san2.test".to_string()],
        )
        .unwrap();
        let info = show("primary.test", &store).unwrap();
        assert!(info.contains("san1.test"));
        assert!(info.contains("san2.test"));
    }

    #[test]
    fn test_list_returns_domains() {
        let (store, _dir) = setup();
        generate("beta.test", &store, &[]).unwrap();
        generate("alpha.test", &store, &[]).unwrap();
        let domains = list(&store).unwrap();
        assert_eq!(domains, vec!["alpha.test", "beta.test"]);
    }

    #[test]
    fn test_list_empty_when_no_certs() {
        let (store, _dir) = setup();
        let domains = list(&store).unwrap();
        assert!(domains.is_empty());
    }

    #[test]
    fn test_show_returns_info() {
        let (store, _dir) = setup();
        generate("show.test", &store, &[]).unwrap();
        let info = show("show.test", &store).unwrap();
        assert!(info.contains("Domain:        show.test"));
        assert!(info.contains("Issuer:        local-ssl Development CA"));
        assert!(info.contains("Valid from:"));
        assert!(info.contains("Valid until:"));
        assert!(info.contains("SANs:"));
        assert!(info.contains("Cert:"));
        assert!(info.contains("Key:"));
    }

    #[test]
    fn test_show_nonexistent_domain() {
        let dir = tempdir().unwrap();
        let store = CaStore {
            dir: dir.path().to_path_buf(),
            key_path: dir.path().join("ca-key.pem"),
            cert_path: dir.path().join("ca-cert.pem"),
        };
        let result = show("nonexistent.test", &store);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), "No certificate for 'nonexistent.test'");
    }

    #[test]
    fn test_check_local_returns_all_fields() {
        let (store, _dir) = setup();
        generate("check.test", &store, &[]).unwrap();
        let result = check_local("check.test", &store).unwrap();
        assert!(result.contains("Domain:"));
        assert!(result.contains("Validity:"));
        assert!(result.contains("Generated:"));
        assert!(result.contains("Expires:"));
        assert!(result.contains("Key file:"));
        assert!(result.contains("Reload needed:"));
    }

    #[test]
    fn test_check_all_local_returns_all() {
        let (store, _dir) = setup();
        generate("alpha.test", &store, &[]).unwrap();
        generate("beta.test", &store, &[]).unwrap();
        let result = check_all_local(&store).unwrap();
        assert!(result.contains("alpha.test"));
        assert!(result.contains("beta.test"));
    }

    #[test]
    fn test_check_local_nonexistent_domain() {
        let (store, _dir) = setup();
        let result = check_local("ghost.test", &store);
        assert!(result.is_err());
    }

    #[test]
    fn test_check_all_local_empty() {
        let (store, _dir) = setup();
        let result = check_all_local(&store).unwrap();
        assert!(result.contains("No certificates found"));
    }
}