diff --git a/core/src/commands/generate.rs b/core/src/commands/generate.rs index 2d20375..2d95356 100644 --- a/core/src/commands/generate.rs +++ b/core/src/commands/generate.rs @@ -1,8 +1,8 @@ +use super::utils::{ + create_distinguished_name, generate_install, save_csr_certificate, save_pem_certificate, + save_pem_key_pair, +}; use crate::{ - trust_stores::{ - firefox::FirefoxTrustStore, nss::NSSValue, nss_profile::NSSProfile, - utils::check_if_firefox_exists, CAValue, - }, utils::{get_certificates_from_data_dir, save_generated_cert_key_files}, x509::{ ca_cert::CACert, ca_req::CAReq, distinguished_name::DistinguishedName, leaf_cert::LeafCert, @@ -13,10 +13,7 @@ use openssl::{ pkey::{PKey, Private}, x509::{X509Req, X509}, }; -use std::{ - error, fs, - path::{Path, PathBuf}, -}; +use std::error; pub fn generate( domains: Vec, @@ -33,55 +30,11 @@ pub fn generate( ) -> Result<(), Box> { if request { for domain in &domains { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: commonname.clone(), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let (ca_req_certificate, private_key) = CAReq::new(distinguished_name)?.generate_certificate()?; - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join(format!("csr-{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - CAReq::save_certificate_to_file(&ca_req_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = output_path.join(format!("csr-{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - CAReq::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - CAReq::save_certificate_to_file(&ca_req_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = output_path.join(format!("csr-{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - CAReq::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } + save_csr_certificate(domain.to_string(), &output, ca_req_certificate, private_key)?; } return Ok(()); } @@ -90,12 +43,8 @@ pub fn generate( if let Some(keyfile) = keyfile { let (cert, pkey) = CACert::load_ca_cert(&certfile, &keyfile)?; if let Some(csr) = &csr { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: commonname.clone(), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let csr_object: X509Req = CAReq::read_csr_from_file(csr)?; let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?; let (leaf_certificate, _private_key) = LeafCert::generate_certificate( @@ -104,88 +53,21 @@ pub fn generate( &pkey, Some(&csr_object), )?; - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join("csr_cert.pem"); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for generated Certificate :"); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join("csr_cert.pem"); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for generated Certificate :"); - } - } + save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate)?; } else { for domain in &domains { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: Some(domain.to_string()), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?; let (leaf_certificate, private_key) = LeafCert::generate_certificate(leaf_cert_object, &cert, &pkey, None)?; if let Some(private_key) = private_key { - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = - output_path.join(format!("{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - LeafCert::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = - output_path.join(format!("{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - LeafCert::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } + save_pem_key_pair( + &output, + leaf_certificate, + domain.to_string(), + private_key, + )?; } else { eprintln!( "Oops! We lost your private key for domain {}. Please try again!", @@ -205,12 +87,8 @@ pub fn generate( let default_cert_key_files: Option<(X509, PKey)> = get_certificates_from_data_dir(); if let Some((d_cert, d_pkey)) = default_cert_key_files { if let Some(csr) = &csr { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: commonname.clone(), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let csr_object: X509Req = CAReq::read_csr_from_file(csr)?; let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?; let (leaf_certificate, _private_key) = LeafCert::generate_certificate( @@ -219,88 +97,16 @@ pub fn generate( &d_pkey, Some(&csr_object), )?; - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join("csr_cert.pem"); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for generated Certificate :"); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join("csr_cert.pem"); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for generated Certificate :"); - } - } + save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate)?; } else { for domain in &domains { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: Some(domain.to_string()), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?; let (leaf_certificate, private_key) = LeafCert::generate_certificate(leaf_cert_object, &d_cert, &d_pkey, None)?; if let Some(private_key) = private_key { - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = - output_path.join(format!("{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - LeafCert::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = - output_path.join(format!("{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - LeafCert::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } + save_pem_key_pair(&output, leaf_certificate, domain.to_string(), private_key)?; } else { eprintln!( "Oops! We lost your private key for domain {}. Please try again!", @@ -311,55 +117,21 @@ pub fn generate( } if install { - let ca_value_object: CAValue = CAValue { - certificate: d_cert, - }; - ca_value_object.install_certificate()?; - let nss_profile_object: NSSProfile = NSSProfile::new(); - let ca_unique_name: String = "vanish-root-test-123456-shubham-brr".to_string(); - let caroot: String = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string(); - let mkcert: NSSValue = - NSSValue::new(nss_profile_object, ca_unique_name.clone(), caroot.clone()); - let success: bool = mkcert.install_nss(); - let firefox_exists: bool = check_if_firefox_exists()?; - if firefox_exists { - let firefox_trust_store_object: FirefoxTrustStore = - FirefoxTrustStore::new(ca_unique_name, caroot)?; - let paths_with_trust_stores: Vec = - FirefoxTrustStore::find_cert_directories(&firefox_trust_store_object)?; - FirefoxTrustStore::install_firefox_certificates( - &firefox_trust_store_object, - paths_with_trust_stores, - ); - } - if success { - println!("Certificate installed successfully."); - } else { - eprintln!("Failed to install the certificate."); - } + generate_install(d_cert)?; } } else { if noca { eprintln!("Error: No CA Certificates found and generation of a new one is disabled by `--no-ca`"); std::process::exit(1) } - // Replace with correct variables - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: commonname.clone(), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let (created_cert, created_key) = CACert::new(distinguished_name)?.generate_certificate()?; save_generated_cert_key_files(&created_cert, &created_key)?; if let Some(csr) = &csr { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: commonname.clone(), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let csr_object: X509Req = CAReq::read_csr_from_file(csr)?; let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?; let (leaf_certificate, _private_key) = LeafCert::generate_certificate( @@ -368,41 +140,11 @@ pub fn generate( &created_key, Some(&csr_object), )?; - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join("csr_cert.pem"); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for generated Certificate :"); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join("csr_cert.pem"); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for generated Certificate :"); - } - } + save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate)?; } else { for domain in &domains { - let distinguished_name: DistinguishedName = DistinguishedName { - common_name: Some(domain.to_string()), - organization: "Vanish".to_string(), - country: country.clone(), - state: state.clone(), - }; + let distinguished_name: DistinguishedName = + create_distinguished_name(&commonname, &country, &state); let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?; let (leaf_certificate, private_key) = LeafCert::generate_certificate( leaf_cert_object, @@ -411,49 +153,7 @@ pub fn generate( None, )?; if let Some(private_key) = private_key { - if let Some(output) = &output { - let output_path: &Path = Path::new(output); - if !output_path.exists() { - fs::create_dir_all(output_path)?; - } - let output_path: PathBuf = if output_path.is_absolute() { - output_path.to_path_buf() - } else { - std::env::current_dir()?.join(output_path) - }; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = - output_path.join(format!("{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - LeafCert::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } else { - let output_path: PathBuf = std::env::current_dir()?; - let file_name: PathBuf = output_path.join(format!("{}.pem", domain)); - let file_name_str: Option<&str> = file_name.to_str(); - if let Some(file_name_str) = file_name_str { - LeafCert::save_cert(&leaf_certificate, file_name_str)?; - } else { - eprintln!("Error: Error creating file for domain : {}", domain); - } - let key_file_name: PathBuf = - output_path.join(format!("{}-key.pem", domain)); - let key_file_name_str: Option<&str> = key_file_name.to_str(); - if let Some(key_file_name_str) = key_file_name_str { - LeafCert::save_key(&private_key, key_file_name_str)?; - } else { - eprintln!("Error: Error creating file for key : {}", domain); - } - } + save_pem_key_pair(&output, leaf_certificate, domain.to_string(), private_key)?; } else { eprintln!( "Oops! We lost your private key for domain {}. Please try again!", @@ -463,35 +163,8 @@ pub fn generate( } } if install { - let ca_value_object: CAValue = CAValue { - certificate: created_cert, - }; - ca_value_object.install_certificate()?; - let nss_profile_object: NSSProfile = NSSProfile::new(); - let ca_unique_name: String = "vanish-root-testing-1234-shubham-brr".to_string(); - let caroot: String = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string(); - let mkcert: NSSValue = - NSSValue::new(nss_profile_object, ca_unique_name.clone(), caroot.clone()); - let success: bool = mkcert.install_nss(); - let firefox_exists: bool = check_if_firefox_exists()?; - if firefox_exists { - let firefox_trust_store_object: FirefoxTrustStore = - FirefoxTrustStore::new(ca_unique_name, caroot)?; - let paths_with_trust_stores: Vec = - FirefoxTrustStore::find_cert_directories(&firefox_trust_store_object)?; - println!("{:?}", paths_with_trust_stores); - FirefoxTrustStore::install_firefox_certificates( - &firefox_trust_store_object, - paths_with_trust_stores, - ); - } - if success { - println!("Certificate installed successfully."); - } else { - eprintln!("Failed to install the certificate."); - } + generate_install(created_cert)?; } } - Ok(()) } diff --git a/core/src/commands/mod.rs b/core/src/commands/mod.rs index 57e6a9d..8fc0866 100644 --- a/core/src/commands/mod.rs +++ b/core/src/commands/mod.rs @@ -1 +1,2 @@ pub mod generate; +mod utils; diff --git a/core/src/commands/utils.rs b/core/src/commands/utils.rs new file mode 100644 index 0000000..cf55798 --- /dev/null +++ b/core/src/commands/utils.rs @@ -0,0 +1,192 @@ +use crate::{ + trust_stores::{ + firefox::FirefoxTrustStore, nss::NSSValue, nss_profile::NSSProfile, + utils::check_if_firefox_exists, CAValue, + }, + x509::{ca_req::CAReq, distinguished_name::DistinguishedName, leaf_cert::LeafCert}, +}; +use openssl::{ + pkey::{PKey, Private}, + x509::{X509Req, X509}, +}; +use std::{ + error::Error, + fs, + path::{Path, PathBuf}, +}; + +pub fn generate_install(cert: X509) -> Result<(), Box> { + let ca_value_object: CAValue = CAValue { certificate: cert }; + ca_value_object.install_certificate()?; + let nss_profile_object: NSSProfile = NSSProfile::new(); + let ca_unique_name: String = "vanish-root-test-123456-shubham-brr".to_string(); + let caroot: String = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string(); + let mkcert: NSSValue = + NSSValue::new(nss_profile_object, ca_unique_name.clone(), caroot.clone()); + let success: bool = mkcert.install_nss(); + let firefox_exists: bool = check_if_firefox_exists()?; + if firefox_exists { + let firefox_trust_store_object: FirefoxTrustStore = + FirefoxTrustStore::new(ca_unique_name, caroot)?; + let paths_with_trust_stores: Vec = + FirefoxTrustStore::find_cert_directories(&firefox_trust_store_object)?; + FirefoxTrustStore::install_firefox_certificates( + &firefox_trust_store_object, + paths_with_trust_stores, + ); + } + if success { + println!("Certificate installed successfully."); + } else { + eprintln!("Failed to install the certificate."); + } + Ok(()) +} + +pub fn save_pem_certificate( + name: String, + output: Option, + leaf_certificate: X509, +) -> Result<(), Box> { + if let Some(output) = &output { + let output_path: &Path = Path::new(output); + if !output_path.exists() { + fs::create_dir_all(output_path)?; + } + let output_path: PathBuf = if output_path.is_absolute() { + output_path.to_path_buf() + } else { + std::env::current_dir()?.join(output_path) + }; + let file_name: PathBuf = output_path.join(name); + let file_name_str: Option<&str> = file_name.to_str(); + if let Some(file_name_str) = file_name_str { + LeafCert::save_cert(&leaf_certificate, file_name_str)?; + } else { + eprintln!("Error: Error creating file for generated Certificate :"); + } + } else { + let output_path: PathBuf = std::env::current_dir()?; + let file_name: PathBuf = output_path.join("csr_cert.pem"); + let file_name_str: Option<&str> = file_name.to_str(); + if let Some(file_name_str) = file_name_str { + LeafCert::save_cert(&leaf_certificate, file_name_str)?; + } else { + eprintln!("Error: Error creating file for generated Certificate"); + } + } + Ok(()) +} + +pub fn save_pem_key_pair( + output: &Option, + leaf_certificate: X509, + name: String, + private_key: PKey, +) -> Result<(), Box> { + if let Some(output) = &output { + let output_path: &Path = Path::new(output); + if !output_path.exists() { + fs::create_dir_all(output_path)?; + } + let output_path: PathBuf = if output_path.is_absolute() { + output_path.to_path_buf() + } else { + std::env::current_dir()?.join(output_path) + }; + let file_name: PathBuf = output_path.join("csr_cert.pem"); + let file_name_str: Option<&str> = file_name.to_str(); + if let Some(file_name_str) = file_name_str { + LeafCert::save_cert(&leaf_certificate, file_name_str)?; + } else { + eprintln!("Error: Error creating file for generated Certificate :"); + } + let key_file_name: PathBuf = output_path.join(format!("{}-key.pem", name)); + let key_file_name_str: Option<&str> = key_file_name.to_str(); + if let Some(key_file_name_str) = key_file_name_str { + LeafCert::save_key(&private_key, key_file_name_str)?; + } else { + eprintln!("Error: Error creating file for key : {}", name); + } + } else { + let output_path: PathBuf = std::env::current_dir()?; + let file_name: PathBuf = output_path.join("csr_cert.pem"); + let file_name_str: Option<&str> = file_name.to_str(); + if let Some(file_name_str) = file_name_str { + LeafCert::save_cert(&leaf_certificate, file_name_str)?; + } else { + eprintln!("Error: Error creating file for generated Certificate"); + } + let key_file_name: PathBuf = output_path.join(format!("{}-key.pem", name)); + let key_file_name_str: Option<&str> = key_file_name.to_str(); + if let Some(key_file_name_str) = key_file_name_str { + LeafCert::save_key(&private_key, key_file_name_str)?; + } else { + eprintln!("Error: Error creating file for key : {}", name); + } + } + Ok(()) +} + +pub fn save_csr_certificate( + name: String, + output: &Option, + ca_req_certificate: X509Req, + private_key: PKey, +) -> Result<(), Box> { + if let Some(output) = &output { + let output_path: &Path = Path::new(output); + if !output_path.exists() { + fs::create_dir_all(output_path)?; + } + let output_path: PathBuf = if output_path.is_absolute() { + output_path.to_path_buf() + } else { + std::env::current_dir()?.join(output_path) + }; + let file_name: PathBuf = output_path.join(format!("csr-{}.pem", name)); + let file_name_str: Option<&str> = file_name.to_str(); + if let Some(file_name_str) = file_name_str { + CAReq::save_certificate_to_file(&ca_req_certificate, file_name_str)?; + } else { + eprintln!("Error: Error creating file for domain : {}", name); + } + let key_file_name: PathBuf = output_path.join(format!("csr-{}-key.pem", name)); + let key_file_name_str: Option<&str> = key_file_name.to_str(); + if let Some(key_file_name_str) = key_file_name_str { + CAReq::save_key(&private_key, key_file_name_str)?; + } else { + eprintln!("Error: Error creating file for key : {}", name); + } + } else { + let output_path: PathBuf = std::env::current_dir()?; + let file_name: PathBuf = output_path.join(format!("{}.pem", name)); + let file_name_str: Option<&str> = file_name.to_str(); + if let Some(file_name_str) = file_name_str { + CAReq::save_certificate_to_file(&ca_req_certificate, file_name_str)?; + } else { + eprintln!("Error: Error creating file for domain : {}", name); + } + let key_file_name: PathBuf = output_path.join(format!("csr-{}-key.pem", name)); + let key_file_name_str: Option<&str> = key_file_name.to_str(); + if let Some(key_file_name_str) = key_file_name_str { + CAReq::save_key(&private_key, key_file_name_str)?; + } else { + eprintln!("Error: Error creating file for key : {}", name); + } + } + Ok(()) +} + +pub fn create_distinguished_name( + commonname: &Option, + country: &Option, + state: &Option, +) -> DistinguishedName { + DistinguishedName { + common_name: commonname.clone(), + organization: "Vanish".to_string(), + country: country.clone(), + state: state.clone(), + } +}