generate keys for api
This commit is contained in:
parent
990758c27e
commit
8a607733a3
|
@ -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<APIWorkerMessage>) {
|
||||
pub fn apiworker_main(config: TFClientConfig, instance: String, _transmitters: ThreadMessageSender, rx: Receiver<APIWorkerMessage>) {
|
||||
// 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) => {
|
||||
|
|
|
@ -15,7 +15,9 @@ pub struct TFClientConfig {
|
|||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct TFClientData {
|
||||
pub host_id: Option<String>
|
||||
pub host_id: Option<String>,
|
||||
pub ed_privkey: Option<[u8; 32]>,
|
||||
pub dh_privkey: Option<[u8; 32]>
|
||||
}
|
||||
|
||||
pub fn create_config(instance: &str) -> Result<(), Box<dyn Error>> {
|
||||
|
@ -50,7 +52,7 @@ pub fn create_cdata(instance: &str) -> Result<(), Box<dyn Error>> {
|
|||
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(())
|
||||
|
|
|
@ -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...");
|
||||
|
|
Loading…
Reference in New Issue