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
//! Platform-specific trust store implementations.
//!
//! This module provides integration with system and application trust stores
//! across different platforms. It supports:
//! - System trust stores (macOS Keychain, Linux CA certificates, Windows Certificate Store)
//! - NSS-based browsers (Firefox, Chrome on Linux)
//! - Java KeyStore
//!
//! Trust store selection can be controlled via the `TRUST_STORES` environment
//! variable (comma-separated list of: system, nss, java).

use crate::Result;
use std::env;
use std::path::Path;

/// Parse TRUST_STORES environment variable to determine which stores to use.
///
/// If the environment variable is not set, all available stores are enabled
/// by default (system, nss, java).
///
/// # Returns
///
/// A vector of enabled store names (lowercase).
pub fn get_enabled_stores() -> Vec<String> {
    if let Ok(trust_stores) = env::var("TRUST_STORES") {
        trust_stores
            .split(',')
            .map(|s| s.trim().to_lowercase())
            .filter(|s| !s.is_empty())
            .collect()
    } else {
        // Default: all stores
        vec!["system".to_string(), "nss".to_string(), "java".to_string()]
    }
}

/// Check if a specific trust store is enabled via environment variable.
///
/// # Arguments
///
/// * `store` - The store name to check (case-insensitive)
///
/// # Returns
///
/// `true` if the store is enabled, `false` otherwise.
pub fn is_store_enabled(store: &str) -> bool {
    let enabled = get_enabled_stores();
    enabled.contains(&store.to_lowercase())
}

/// Enumerate all available trust stores on this system.
///
/// Checks for the presence of required tools (certutil, keytool) and
/// returns a list of trust stores that can be used.
///
/// # Returns
///
/// A vector of human-readable store descriptions.
pub fn enumerate_available_stores() -> Vec<String> {
    let mut stores = Vec::new();

    // Check for system store
    #[cfg(target_os = "macos")]
    stores.push("system (macOS Keychain)".to_string());

    #[cfg(target_os = "linux")]
    stores.push("system (Linux CA certificates)".to_string());

    #[cfg(target_os = "windows")]
    stores.push("system (Windows Certificate Store)".to_string());

    // Check for NSS/Firefox
    if nss::NssTrustStore::is_available() && nss::NssTrustStore::has_certutil() {
        stores.push("nss (Firefox/Chromium)".to_string());
    }

    // Check for Java
    if java::JavaTrustStore::is_available() && java::JavaTrustStore::has_keytool() {
        stores.push("java (Java Keystore)".to_string());
    }

    stores
}

#[cfg(target_os = "macos")]
pub mod macos;

#[cfg(target_os = "linux")]
pub mod linux;

#[cfg(target_os = "windows")]
pub mod windows;

pub mod java;
pub mod nss;

/// Common interface for trust store operations.
///
/// Implementations handle platform-specific certificate installation
/// and removal from trust stores.
pub trait TrustStore {
    /// Check if the certificate is installed in this trust store.
    fn check(&self) -> Result<bool>;

    /// Install the certificate to this trust store.
    fn install(&self) -> Result<()>;

    /// Remove the certificate from this trust store.
    fn uninstall(&self) -> Result<()>;
}

/// Install CA certificate to macOS trust stores.
///
/// Installs the certificate to the System Keychain and optionally to
/// Firefox NSS and Java KeyStore if available.
///
/// # Arguments
///
/// * `cert_path` - Path to the CA certificate file
///
/// # Returns
///
/// `Ok(())` on success, or an error if installation fails.
#[cfg(target_os = "macos")]
pub fn install_macos(cert_path: &Path) -> Result<()> {
    // Install to system store if enabled
    if is_store_enabled("system") {
        eprintln!("Installing to system trust store...");
        let store = macos::MacOSTrustStore::new(cert_path);
        store.install()?;
    }

    let ca = crate::ca::get_ca()?;
    let unique_name = ca.unique_name()?;

    // Also install to NSS/Firefox if available and enabled
    if is_store_enabled("nss")
        && nss::NssTrustStore::is_available()
        && nss::NssTrustStore::has_certutil()
    {
        eprintln!("Installing to Firefox/NSS trust store...");
        let nss_store = nss::NssTrustStore::new(cert_path, unique_name.clone());
        if let Err(e) = nss_store.install() {
            eprintln!("Warning: Failed to install certificate in Firefox: {}", e);
        } else {
            println!("The local CA is now installed in Firefox trust store!");
        }
    }

    // Also install to Java keystore if available and enabled
    if is_store_enabled("java")
        && java::JavaTrustStore::is_available()
        && java::JavaTrustStore::has_keytool()
    {
        eprintln!("Installing to Java trust store...");
        let java_store = java::JavaTrustStore::new(cert_path, unique_name.clone());
        if let Err(e) = java_store.install() {
            eprintln!(
                "Warning: Failed to install certificate in Java keystore: {}",
                e
            );
        } else {
            println!("The local CA is now installed in Java trust store!");
        }
    }

    Ok(())
}

/// Uninstall CA certificate from macOS trust stores.
///
/// Removes the certificate from the System Keychain and optionally from
/// Firefox NSS and Java KeyStore if they were previously installed.
///
/// # Arguments
///
/// * `cert_path` - Path to the CA certificate file
///
/// # Returns
///
/// `Ok(())` on success, or an error if uninstallation fails.
#[cfg(target_os = "macos")]
pub fn uninstall_macos(cert_path: &Path) -> Result<()> {
    let store = macos::MacOSTrustStore::new(cert_path);
    store.uninstall()?;

    // Also uninstall from NSS/Firefox and Java if available
    let ca = crate::ca::get_ca()?;
    if let Ok(unique_name) = ca.unique_name() {
        if nss::NssTrustStore::is_available() && nss::NssTrustStore::has_certutil() {
            let nss_store = nss::NssTrustStore::new(cert_path, unique_name.clone());
            if let Err(e) = nss_store.uninstall() {
                eprintln!(
                    "Warning: Failed to uninstall certificate from Firefox: {}",
                    e
                );
            }
        }

        if java::JavaTrustStore::is_available() && java::JavaTrustStore::has_keytool() {
            let java_store = java::JavaTrustStore::new(cert_path, unique_name.clone());
            if let Err(e) = java_store.uninstall() {
                eprintln!(
                    "Warning: Failed to uninstall certificate from Java keystore: {}",
                    e
                );
            }
        }
    }

    Ok(())
}

/// Install CA certificate to Linux trust stores.
///
/// Installs the certificate to the system CA directory and optionally to
/// Firefox NSS and Java KeyStore if available.
///
/// # Arguments
///
/// * `cert_path` - Path to the CA certificate file
///
/// # Returns
///
/// `Ok(())` on success, or an error if installation fails.
#[cfg(target_os = "linux")]
pub fn install_linux(cert_path: &Path) -> Result<()> {
    // Install to system store if enabled
    if is_store_enabled("system") {
        eprintln!("Installing to system trust store...");
        let store = linux::LinuxTrustStore::new(cert_path);
        store.install()?;
    }

    let ca = crate::ca::get_ca()?;
    let unique_name = ca.unique_name()?;

    // Also install to NSS/Firefox if available and enabled
    if is_store_enabled("nss")
        && nss::NssTrustStore::is_available()
        && nss::NssTrustStore::has_certutil()
    {
        eprintln!("Installing to Firefox/Chromium trust store...");
        let nss_store = nss::NssTrustStore::new(cert_path, unique_name.clone());
        if let Err(e) = nss_store.install() {
            eprintln!(
                "Warning: Failed to install certificate in Firefox/Chromium: {}",
                e
            );
        } else {
            println!(
                "The local CA is now installed in the Firefox and/or Chrome/Chromium trust store!"
            );
        }
    }

    // Also install to Java keystore if available and enabled
    if is_store_enabled("java")
        && java::JavaTrustStore::is_available()
        && java::JavaTrustStore::has_keytool()
    {
        eprintln!("Installing to Java trust store...");
        let java_store = java::JavaTrustStore::new(cert_path, unique_name.clone());
        if let Err(e) = java_store.install() {
            eprintln!(
                "Warning: Failed to install certificate in Java keystore: {}",
                e
            );
        } else {
            println!("The local CA is now installed in Java trust store!");
        }
    }

    Ok(())
}

/// Uninstall CA certificate from Linux trust stores.
///
/// Removes the certificate from the system CA directory and optionally from
/// Firefox NSS and Java KeyStore if they were previously installed.
///
/// # Arguments
///
/// * `cert_path` - Path to the CA certificate file
///
/// # Returns
///
/// `Ok(())` on success, or an error if uninstallation fails.
#[cfg(target_os = "linux")]
pub fn uninstall_linux(cert_path: &Path) -> Result<()> {
    let store = linux::LinuxTrustStore::new(cert_path);
    store.uninstall()?;

    // Also uninstall from NSS/Firefox and Java if available
    let ca = crate::ca::get_ca()?;
    if let Ok(unique_name) = ca.unique_name() {
        if nss::NssTrustStore::is_available() && nss::NssTrustStore::has_certutil() {
            let nss_store = nss::NssTrustStore::new(cert_path, unique_name.clone());
            if let Err(e) = nss_store.uninstall() {
                eprintln!(
                    "Warning: Failed to uninstall certificate from Firefox/Chromium: {}",
                    e
                );
            }
        }

        if java::JavaTrustStore::is_available() && java::JavaTrustStore::has_keytool() {
            let java_store = java::JavaTrustStore::new(cert_path, unique_name.clone());
            if let Err(e) = java_store.uninstall() {
                eprintln!(
                    "Warning: Failed to uninstall certificate from Java keystore: {}",
                    e
                );
            }
        }
    }

    Ok(())
}

/// Install CA certificate to Windows trust stores.
///
/// Installs the certificate to the Windows Certificate Store and optionally
/// to Firefox NSS and Java KeyStore if available.
///
/// # Arguments
///
/// * `cert_path` - Path to the CA certificate file
///
/// # Returns
///
/// `Ok(())` on success, or an error if installation fails.
#[cfg(target_os = "windows")]
pub fn install_windows(cert_path: &Path) -> Result<()> {
    // Install to system store if enabled
    if is_store_enabled("system") {
        eprintln!("Installing to system trust store...");
        let store = windows::WindowsTrustStore::new(cert_path);
        store.install()?;
    }

    let ca = crate::ca::get_ca()?;
    let unique_name = ca.unique_name()?;

    // Also install to NSS/Firefox if available and enabled
    if is_store_enabled("nss")
        && nss::NssTrustStore::is_available()
        && nss::NssTrustStore::has_certutil()
    {
        eprintln!("Installing to Firefox trust store...");
        let nss_store = nss::NssTrustStore::new(cert_path, unique_name.clone());
        if let Err(e) = nss_store.install() {
            eprintln!("Warning: Failed to install certificate in Firefox: {}", e);
        } else {
            println!("The local CA is now installed in Firefox trust store!");
        }
    }

    // Also install to Java keystore if available and enabled
    if is_store_enabled("java")
        && java::JavaTrustStore::is_available()
        && java::JavaTrustStore::has_keytool()
    {
        eprintln!("Installing to Java trust store...");
        let java_store = java::JavaTrustStore::new(cert_path, unique_name.clone());
        if let Err(e) = java_store.install() {
            eprintln!(
                "Warning: Failed to install certificate in Java keystore: {}",
                e
            );
        } else {
            println!("The local CA is now installed in Java trust store!");
        }
    }

    Ok(())
}

/// Uninstall CA certificate from Windows trust stores.
///
/// Removes the certificate from the Windows Certificate Store and optionally
/// from Firefox NSS and Java KeyStore if they were previously installed.
///
/// # Arguments
///
/// * `cert_path` - Path to the CA certificate file
///
/// # Returns
///
/// `Ok(())` on success, or an error if uninstallation fails.
#[cfg(target_os = "windows")]
pub fn uninstall_windows(cert_path: &Path) -> Result<()> {
    let store = windows::WindowsTrustStore::new(cert_path);
    store.uninstall()?;

    // Also uninstall from NSS/Firefox and Java if available
    let ca = crate::ca::get_ca()?;
    if let Ok(unique_name) = ca.unique_name() {
        if nss::NssTrustStore::is_available() && nss::NssTrustStore::has_certutil() {
            let nss_store = nss::NssTrustStore::new(cert_path, unique_name.clone());
            if let Err(e) = nss_store.uninstall() {
                eprintln!(
                    "Warning: Failed to uninstall certificate from Firefox: {}",
                    e
                );
            }
        }

        if java::JavaTrustStore::is_available() && java::JavaTrustStore::has_keytool() {
            let java_store = java::JavaTrustStore::new(cert_path, unique_name.clone());
            if let Err(e) = java_store.uninstall() {
                eprintln!(
                    "Warning: Failed to uninstall certificate from Java keystore: {}",
                    e
                );
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_enumerate_available_stores() {
        let stores = enumerate_available_stores();

        // Should have at least the system store
        assert!(!stores.is_empty(), "Should find at least one trust store");

        // Check that system store is present
        assert!(
            stores.iter().any(|s| s.contains("system")),
            "System store should be available"
        );
    }

    #[test]
    fn test_get_enabled_stores_default() {
        // Clear TRUST_STORES env var for this test
        unsafe {
            std::env::remove_var("TRUST_STORES");
        }

        let stores = get_enabled_stores();
        assert!(stores.contains(&"system".to_string()));
        assert!(stores.contains(&"nss".to_string()));
        assert!(stores.contains(&"java".to_string()));
    }

    #[test]
    fn test_is_store_enabled() {
        unsafe {
            std::env::remove_var("TRUST_STORES");
        }

        assert!(is_store_enabled("system"));
        assert!(is_store_enabled("nss"));
        assert!(is_store_enabled("java"));
    }
}