[server] Remove old code, fix double deletion

This commit is contained in:
Armin Friedl 2019-11-30 19:52:06 +01:00
parent 83999d7680
commit 83d2b0a5a1
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
3 changed files with 5 additions and 57 deletions

View file

@ -53,9 +53,9 @@ pub struct Keyring {
} }
impl Keyring { impl Keyring {
pub fn new_from_path(path: &PathBuf, keep: bool) -> Result<Keyring, KeyringError> { pub fn new_from_path(path: &PathBuf) -> Result<Keyring, KeyringError> {
let keyring = Keyring { let keyring = Keyring {
master: key_from_path(path, keep)?, master: key_from_path(path)?,
clients: HashMap::new(), clients: HashMap::new(),
}; };
@ -63,21 +63,19 @@ impl Keyring {
} }
pub fn add_key_from_path(&mut self, path: &PathBuf, keep: bool) -> Result<(), KeyringError> { pub fn add_key_from_path(&mut self, path: &PathBuf, keep: bool) -> Result<(), KeyringError> {
let client_key: ClientKey = key_from_path(path, keep)?; let client_key: ClientKey = key_from_path(path)?;
self.clients.insert(client_key.id.clone(), client_key); self.clients.insert(client_key.id.clone(), client_key);
Ok(()) Ok(())
} }
} }
fn key_from_path<T>(path: &PathBuf, keep: bool) -> Result<T, KeyringError> fn key_from_path<T>(path: &PathBuf) -> Result<T, KeyringError>
where T: serde::de::DeserializeOwned where T: serde::de::DeserializeOwned
{ {
let mk_file = File::open(path)?; let mk_file = File::open(path)?;
let key = serde_cbor::from_reader(mk_file)?; let key = serde_cbor::from_reader(mk_file)?;
if !keep { std::fs::remove_file(path)? };
Ok(key) Ok(key)
} }

View file

@ -44,7 +44,7 @@ impl Coffer {
/// public key /// public key
pub fn new_from_path_encrypted(masterkey: &PathBuf, secrets: &PathBuf, keep: bool) -> Result<Coffer> { pub fn new_from_path_encrypted(masterkey: &PathBuf, secrets: &PathBuf, keep: bool) -> Result<Coffer> {
debug!{"Initializing keyring"} debug!{"Initializing keyring"}
let keyring = Keyring::new_from_path(masterkey, keep)?; let keyring = Keyring::new_from_path(masterkey)?;
debug!{"Loading secrets"} debug!{"Loading secrets"}
let mut sec_data = Vec::new(); let mut sec_data = Vec::new();

View file

@ -1,50 +0,0 @@
use std::collections::HashMap;
use sodiumoxide::crypto::box_;
use serde::{Serialize, Deserialize};
#[derive(Debug,Serialize,Deserialize)]
pub struct MasterKey (String, box_::SecretKey, box_::PublicKey);
#[derive(Debug,Serialize,Deserialize)]
pub struct ClientKey (String, box_::PublicKey);
#[derive(Default)]
struct KeyStore {
keys: HashMap<String, ClientKey>
}
impl KeyStore {
fn get(&self, key: &str) -> Option<&ClientKey> {
self.keys.get(key)
}
}
pub struct KeyRing {
master: MasterKey,
keystore: KeyStore
}
impl KeyRing {
pub fn new(master: MasterKey) -> KeyRing {
KeyRing {master, keystore: KeyStore::default()}
}
pub fn seal(&self, data: &[u8], nonce: &[u8], id: String) -> Vec<u8> {
let nonce = box_::Nonce::from_slice(nonce).unwrap();
let sender_sk = &self.master.1;
let receiver_pk = &self.keystore.get(&id).unwrap().1;
box_::seal(&data, &nonce, &receiver_pk, &sender_sk)
}
pub fn unseal(&self, data: &[u8], nonce: &[u8], id: String) -> Vec<u8> {
let nonce = box_::Nonce::from_slice(nonce).unwrap();
let receiver_sk = &self.master.1;
let sender_pk = &self.keystore.get(&id).unwrap().1;
box_::open(&data, &nonce, &sender_pk, &receiver_sk).unwrap()
}
pub fn add_key(&mut self, id: String, pubkey: [u8;32]) {
self.keystore.keys.insert(id.clone(), ClientKey(id, box_::PublicKey::from_slice(&pubkey).unwrap()));
}
}