Skip to content

Commit

Permalink
feat(version): bump
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Sep 5, 2024
1 parent 9ac6609 commit 7e4e6fe
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 144 deletions.
74 changes: 1 addition & 73 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vanish"
version = "0.1.2"
version = "0.2.0"
description = "A simple config tool to make locally trusted X.509 development certificates for your domains"
edition = "2021"
license-file = "LICENSE"
Expand All @@ -14,7 +14,6 @@ readme = "README.md"
openssl = "0.10"
clap = { version = "4.4.8", features = ["derive"] }
dirs = "5.0"
sha2 = "0.10"
lazy_static = "1.4"
colored = "2.0"
base64 = "0.21"
Expand Down
17 changes: 10 additions & 7 deletions core/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ pub fn generate(

let default_cert_key_files: Option<(X509, PKey<Private>)> = get_certificates_from_data_dir();
if let Some((d_cert, d_pkey)) = default_cert_key_files {
if install {
println!();
generate_install(&d_cert)?;
}
if let Some(csr) = &csr {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
Expand Down Expand Up @@ -230,10 +234,6 @@ pub fn generate(
output.unwrap()
);
}

if install {
generate_install(d_cert)?;
}
} else {
if noca {
eprintln!(
Expand All @@ -247,6 +247,12 @@ pub fn generate(
let (created_cert, created_key) =
CACert::new(distinguished_name)?.generate_certificate()?;
save_generated_cert_key_files(&created_cert, &created_key)?;

if install {
println!();
generate_install(&created_cert)?;
}

if let Some(csr) = &csr {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
Expand Down Expand Up @@ -322,9 +328,6 @@ pub fn generate(
output.unwrap()
);
}
if install {
generate_install(created_cert)?;
}
}
println!();
Ok(())
Expand Down
10 changes: 7 additions & 3 deletions core/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::{
trust_stores::{
firefox::FirefoxTrustStore, nss::NSSValue, nss_profile::NSSProfile,
utils::check_if_firefox_exists, CAValue,
}, utils::get_unique_hash, x509::{ca_req::CAReq, distinguished_name::DistinguishedName, leaf_cert::LeafCert}
},
utils::get_unique_hash,
x509::{ca_req::CAReq, distinguished_name::DistinguishedName, leaf_cert::LeafCert},
};
use colored::*;
use openssl::{
Expand All @@ -15,8 +17,10 @@ use std::{
path::{Path, PathBuf},
};

pub fn generate_install(cert: X509) -> Result<(), Box<dyn Error>> {
let ca_value_object: CAValue = CAValue { certificate: cert };
pub fn generate_install(cert: &X509) -> Result<(), Box<dyn Error>> {
let ca_value_object: CAValue = CAValue {
certificate: cert.clone(),
};
ca_value_object.install_certificate()?;
let nss_profile_object: NSSProfile = NSSProfile::new();
let caroot: String = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod utils_tests;
#[derive(Parser)]
#[clap(
name = "A simple config tool to make locally trusted X.509 development certificates for your domains",
version = "0.1.2",
version = "0.2.0",
author = "Shubham Singh"
)]
struct CLI {
Expand Down
112 changes: 82 additions & 30 deletions core/src/trust_stores/firefox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use colored::Colorize;

use super::errors::FirefoxTrustStoreError;
use std::borrow::Cow;
use std::process::{exit, Stdio};
use std::{
env, fs, io,
Expand Down Expand Up @@ -80,6 +83,39 @@ impl FirefoxTrustStore {
})
}

fn is_certificate_installed(&self, cert_dir: &Path) -> Result<bool, FirefoxTrustStoreError> {
match &self.certutil_path {
Some(certutil) => {
let output = Command::new(certutil)
.arg("-L")
.arg("-d")
.arg(cert_dir.to_str().unwrap())
.output()
.map_err(|err: io::Error| FirefoxTrustStoreError::IOError(err))?;

if output.status.success() {
let stdout: Cow<'_, str> = String::from_utf8_lossy(&output.stdout);

if stdout.contains(&self.ca_unique_name) {
return Ok(true);
}
} else {
eprintln!(
"{}: Failed to list certificates in {:?}",
"Error".red(),
cert_dir
);
}
}
None => {
eprint!("{}: No certutil found. Please install!", "Error".red());
exit(1);
}
}

Ok(false)
}

pub fn find_cert_directories(&self) -> Result<Vec<PathBuf>, FirefoxTrustStoreError> {
let mut cert_dirs: Vec<PathBuf> = Vec::new();
for profile_dir in &self.firefox_profile {
Expand Down Expand Up @@ -111,39 +147,55 @@ impl FirefoxTrustStore {
}

pub fn install_firefox_certificates(&self, cert_paths: Vec<PathBuf>) {
match &self.certutil_path {
Some(path) => {
for cert_dir in cert_paths {
let cmd_result: Result<ExitStatus, io::Error> = Command::new(path)
.arg("-A")
.arg("-d")
.arg(cert_dir.to_str().unwrap())
.arg("-t")
.arg("C,,")
.arg("-n")
.arg(&self.ca_unique_name)
.arg("-i")
.arg(&self.vanish_ca_path)
.stdout(Stdio::null())
.status();

match cmd_result {
Ok(status) if status.success() => {
println!("Successfully installed certificate in {:?}", cert_dir);
}
Ok(_) => {
eprintln!("Failed to install certificate in {:?}", cert_dir);
let all_installed: bool = cert_paths.iter().all(|cert_dir: &PathBuf| {
match self.is_certificate_installed(cert_dir) {
Ok(true) => true,
Ok(false) => false,
Err(_) => false,
}
});

if all_installed {
println!(
"{}: Certificate already installed in all Firefox profiles ✅.",
"Note".green()
);
return;
} else {
match &self.certutil_path {
Some(path) => {
for cert_dir in cert_paths {
if let Ok(true) = self.is_certificate_installed(&cert_dir) {
continue;
}
Err(err) => {
eprintln!("Error executing certutil: {:?}", err);

let cmd_result: Result<ExitStatus, io::Error> = Command::new(path)
.arg("-A")
.arg("-d")
.arg(cert_dir.to_str().unwrap())
.arg("-t")
.arg("C,,")
.arg("-n")
.arg(&self.ca_unique_name)
.arg("-i")
.arg(&self.vanish_ca_path)
.stdout(Stdio::null())
.status();

match cmd_result {
Ok(_) => {}
Err(err) => {
eprintln!("{}: executing certutil: {:?}", "Error".red(), err);
}
}
}
println!("Certificate successfully installed in all Firefox profiles ✅.");
}
}
None => {
eprint!("No certutil found. Please install!");
exit(1);
}
};
None => {
eprint!("No certutil found. Please install!");
exit(1);
}
};
}
}
}
Loading

0 comments on commit 7e4e6fe

Please sign in to comment.