From 8a607733a3abbbbf18a2be1ed731eed2f288eea2 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Mon, 27 Mar 2023 12:32:26 -0400 Subject: [PATCH] generate keys for api --- tfclient/src/apiworker.rs | 47 +++++++++++++++++++++++++++++++++++++-- tfclient/src/config.rs | 6 +++-- tfclient/src/daemon.rs | 3 ++- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/tfclient/src/apiworker.rs b/tfclient/src/apiworker.rs index 1ee773e..1b2707e 100644 --- a/tfclient/src/apiworker.rs +++ b/tfclient/src/apiworker.rs @@ -1,13 +1,56 @@ use std::sync::mpsc::{Receiver, TryRecvError}; use log::{error, info}; -use crate::config::TFClientConfig; +use trifid_pki::ed25519_dalek::{SecretKey, SigningKey}; +use trifid_pki::rand_core::OsRng; +use trifid_pki::x25519_dalek::StaticSecret; +use crate::config::{load_cdata, save_cdata, TFClientConfig}; use crate::daemon::ThreadMessageSender; pub enum APIWorkerMessage { Shutdown } -pub fn apiworker_main(_config: TFClientConfig, _transmitters: ThreadMessageSender, rx: Receiver) { +pub fn apiworker_main(config: TFClientConfig, instance: String, _transmitters: ThreadMessageSender, rx: Receiver) { + // Generate dhPubkey and edPubkey if it doesn't exist + // Load vardata + let mut vdata = match load_cdata(&instance) { + Ok(d) => d, + Err(e) => { + error!("Error loading vdata: {}", e); + error!("APIWorker exiting with error"); + return; + } + }; + + if vdata.ed_privkey.is_none() { + info!("Generating ed25519 key"); + let mut csprng = OsRng; + let key = SigningKey::generate(&mut csprng); + let ed_key_bytes = key.to_bytes().to_vec(); + vdata.ed_privkey = Some(ed_key_bytes.try_into().unwrap()); + } + if vdata.dh_privkey.is_none() { + info!("Generating ecdh key"); + let mut csprng = OsRng; + let key = StaticSecret::new(&mut csprng); + let dh_key_bytes = key.to_bytes(); + vdata.dh_privkey = Some(dh_key_bytes); + } + + info!("Loading keys"); + let ed_key = SigningKey::from_bytes(&SecretKey::from(vdata.ed_privkey.unwrap())); + let dh_key = StaticSecret::from(vdata.dh_privkey.unwrap()); + info!("Keys loaded successfully"); + + // Save vardata + match save_cdata(&instance, vdata) { + Ok(_) => (), + Err(e) => { + error!("Error saving vdata: {}", e); + error!("APIWorker exiting with error"); + return; + } + } loop { match rx.try_recv() { Ok(msg) => { diff --git a/tfclient/src/config.rs b/tfclient/src/config.rs index 9341f6c..39be10e 100644 --- a/tfclient/src/config.rs +++ b/tfclient/src/config.rs @@ -15,7 +15,9 @@ pub struct TFClientConfig { #[derive(Serialize, Deserialize, Clone)] pub struct TFClientData { - pub host_id: Option + pub host_id: Option, + pub ed_privkey: Option<[u8; 32]>, + pub dh_privkey: Option<[u8; 32]> } pub fn create_config(instance: &str) -> Result<(), Box> { @@ -50,7 +52,7 @@ pub fn create_cdata(instance: &str) -> Result<(), Box> { info!("Creating data directory..."); fs::create_dir_all(get_cdata_dir(instance).ok_or("Unable to load data dir")?)?; info!("Copying default data file to config directory..."); - let config = TFClientData { host_id: None }; + let config = TFClientData { host_id: None, ed_privkey: None, dh_privkey: None }; let config_str = toml::to_string(&config)?; fs::write(get_cdata_file(instance).ok_or("Unable to load data dir")?, config_str)?; Ok(()) diff --git a/tfclient/src/daemon.rs b/tfclient/src/daemon.rs index c462e6d..7f16b30 100644 --- a/tfclient/src/daemon.rs +++ b/tfclient/src/daemon.rs @@ -73,8 +73,9 @@ pub fn daemon_main(name: String, server: String) { let config_api = config.clone(); let transmitter_api = transmitter.clone(); + let name_api = name.clone(); let api_thread = thread::spawn(move || { - apiworker_main(config_api, transmitter_api, rx_api); + apiworker_main(config_api, name_api, transmitter_api, rx_api); }); info!("Starting Nebula thread...");