fastcert 0.3.1

A simple zero-config tool for making locally-trusted development certificates
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Security-focused tests for fastcert
//!
//! These tests verify security properties like file permissions, key security,
//! certificate validation, and error handling.

mod common;

use common::get_test_lock;
use std::env;
use std::fs;
use tempfile::TempDir;

#[test]
#[cfg(unix)]
fn test_security_private_key_permissions() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    let hosts = vec!["security-test.local".to_string()];
    let cert_file = temp_dir.path().join("test.pem");
    let key_file = temp_dir.path().join("test-key.pem");

    fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        false,
        false,
        false,
    )
    .unwrap();

    // Verify private key has restrictive permissions (0600)
    use std::os::unix::fs::PermissionsExt;
    let key_perms = fs::metadata(&key_file).unwrap().permissions();
    let mode = key_perms.mode() & 0o777;

    assert_eq!(
        mode, 0o600,
        "Private key should have 0600 permissions, got {:o}",
        mode
    );

    // Verify CA private key also has restrictive permissions (0400 or 0600)
    let ca_key = temp_dir.path().join("rootCA-key.pem");
    let ca_key_perms = fs::metadata(&ca_key).unwrap().permissions();
    let ca_mode = ca_key_perms.mode() & 0o777;

    assert!(
        ca_mode == 0o400 || ca_mode == 0o600,
        "CA private key should have 0400 or 0600 permissions, got {:o}",
        ca_mode
    );

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_security_certificate_not_self_signed() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    let hosts = vec!["not-self-signed.local".to_string()];
    let cert_file = temp_dir.path().join("test.pem");
    let key_file = temp_dir.path().join("test-key.pem");

    fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        false,
        false,
        false,
    )
    .unwrap();

    // Read certificate and verify it's signed by CA
    use std::process::Command;
    let output = Command::new("openssl")
        .args(["x509", "-noout", "-issuer", "-subject"])
        .arg("-in")
        .arg(&cert_file)
        .output()
        .unwrap();

    let text = String::from_utf8_lossy(&output.stdout);

    // Certificate should have different issuer and subject
    assert!(text.contains("issuer="), "Should have issuer field");
    assert!(text.contains("subject="), "Should have subject field");

    // Issuer should contain "fastcert" (CA name)
    assert!(text.contains("fastcert"), "Should be signed by fastcert CA");

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_security_unique_serial_numbers() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    // Generate multiple certificates
    let mut serials = Vec::new();

    for i in 1..=5 {
        let hosts = vec![format!("test{}.local", i)];
        let cert_file = temp_dir.path().join(format!("test{}.pem", i));
        let key_file = temp_dir.path().join(format!("test{}-key.pem", i));

        fastcert::cert::generate_certificate(
            &hosts,
            Some(cert_file.to_str().unwrap()),
            Some(key_file.to_str().unwrap()),
            None,
            false,
            false,
            false,
        )
        .unwrap();

        // Get serial number
        use std::process::Command;
        let output = Command::new("openssl")
            .args(["x509", "-noout", "-serial"])
            .arg("-in")
            .arg(&cert_file)
            .output()
            .unwrap();

        let serial = String::from_utf8_lossy(&output.stdout).trim().to_string();
        serials.push(serial);
    }

    // Verify all serials are unique
    for i in 0..serials.len() {
        for j in (i + 1)..serials.len() {
            assert_ne!(
                serials[i],
                serials[j],
                "Certificates {} and {} have duplicate serial numbers: {}",
                i + 1,
                j + 1,
                serials[i]
            );
        }
    }

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_security_ca_certificate_validity() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    // Generate a certificate (creates CA)
    let hosts = vec!["test.local".to_string()];
    let cert_file = temp_dir.path().join("test.pem");
    let key_file = temp_dir.path().join("test-key.pem");

    fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        false,
        false,
        false,
    )
    .unwrap();

    // Verify CA certificate properties
    use std::process::Command;
    let ca_cert = temp_dir.path().join("rootCA.pem");
    let output = Command::new("openssl")
        .args(["x509", "-noout", "-text"])
        .arg("-in")
        .arg(&ca_cert)
        .output()
        .unwrap();

    let text = String::from_utf8_lossy(&output.stdout);

    // CA should have CA:TRUE basic constraint
    assert!(
        text.contains("CA:TRUE"),
        "CA certificate should have CA:TRUE"
    );

    // CA should be able to sign certificates
    assert!(
        text.contains("Certificate Sign"),
        "CA should have Certificate Sign usage"
    );

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_error_empty_host_list() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    let hosts = vec![];
    let result =
        fastcert::cert::generate_certificate(&hosts, None, None, None, false, false, false);

    assert!(result.is_err(), "Should fail with empty host list");

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_error_invalid_wildcard() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    // Test double wildcard (should fail)
    let hosts = vec!["**.example.com".to_string()];
    let result =
        fastcert::cert::generate_certificate(&hosts, None, None, None, false, false, false);

    assert!(result.is_err(), "Should fail with double wildcard");

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_certificate_expiration_date() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    let hosts = vec!["expiry-test.local".to_string()];
    let cert_file = temp_dir.path().join("test.pem");
    let key_file = temp_dir.path().join("test-key.pem");

    fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        false,
        false,
        false,
    )
    .unwrap();

    // Verify certificate validity period
    use std::process::Command;
    let output = Command::new("openssl")
        .args(["x509", "-noout", "-dates"])
        .arg("-in")
        .arg(&cert_file)
        .output()
        .unwrap();

    let dates = String::from_utf8_lossy(&output.stdout);

    // Should have both notBefore and notAfter
    assert!(
        dates.contains("notBefore="),
        "Certificate should have notBefore date"
    );
    assert!(
        dates.contains("notAfter="),
        "Certificate should have notAfter date"
    );

    // Verify it's currently valid
    let verify_output = Command::new("openssl")
        .args(["x509", "-noout", "-checkend", "0"])
        .arg("-in")
        .arg(&cert_file)
        .output()
        .unwrap();

    assert!(
        verify_output.status.success(),
        "Certificate should be currently valid"
    );

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_certificate_key_usage() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    // Test server certificate
    let hosts = vec!["server.local".to_string()];
    let cert_file = temp_dir.path().join("server.pem");
    let key_file = temp_dir.path().join("server-key.pem");

    fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        false,
        false,
        false,
    )
    .unwrap();

    use std::process::Command;
    let output = Command::new("openssl")
        .args(["x509", "-noout", "-text"])
        .arg("-in")
        .arg(&cert_file)
        .output()
        .unwrap();

    let text = String::from_utf8_lossy(&output.stdout);

    // Server certificate should have TLS Web Server Authentication
    assert!(
        text.contains("TLS Web Server Authentication"),
        "Server certificate should have server auth usage"
    );

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_client_certificate_key_usage() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    // Test client certificate
    let hosts = vec!["client@example.com".to_string()];
    let cert_file = temp_dir.path().join("client.pem");
    let key_file = temp_dir.path().join("client-key.pem");

    fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        true, // Client cert
        false,
        false,
    )
    .unwrap();

    use std::process::Command;
    let output = Command::new("openssl")
        .args(["x509", "-noout", "-text"])
        .arg("-in")
        .arg(&cert_file)
        .output()
        .unwrap();

    let text = String::from_utf8_lossy(&output.stdout);

    // Client certificate should have TLS Web Client Authentication
    assert!(
        text.contains("TLS Web Client Authentication"),
        "Client certificate should have client auth usage"
    );

    unsafe {
        env::remove_var("CAROOT");
    }
}

#[test]
fn test_san_types_validation() {
    let _lock = get_test_lock();

    let temp_dir = TempDir::new().unwrap();
    unsafe {
        env::set_var("CAROOT", temp_dir.path().to_str().unwrap());
    }

    // Test various SAN types
    let hosts = vec![
        "dns.example.com".to_string(),
        "192.168.1.1".to_string(),
        "email@example.com".to_string(),
    ];

    let cert_file = temp_dir.path().join("san.pem");
    let key_file = temp_dir.path().join("san-key.pem");

    let result = fastcert::cert::generate_certificate(
        &hosts,
        Some(cert_file.to_str().unwrap()),
        Some(key_file.to_str().unwrap()),
        None,
        false,
        false,
        false,
    );

    assert!(result.is_ok(), "Should handle mixed SAN types");

    // Verify all SANs are present
    use std::process::Command;
    let output = Command::new("openssl")
        .args(["x509", "-noout", "-text"])
        .arg("-in")
        .arg(&cert_file)
        .output()
        .unwrap();

    let text = String::from_utf8_lossy(&output.stdout);

    assert!(
        text.contains("DNS:dns.example.com"),
        "Should contain DNS SAN"
    );
    assert!(
        text.contains("IP Address:192.168.1.1"),
        "Should contain IP SAN"
    );
    assert!(
        text.contains("email:email@example.com"),
        "Should contain email SAN"
    );

    unsafe {
        env::remove_var("CAROOT");
    }
}