Skip to content

Commit

Permalink
Remove empty ephemeral keys
Browse files Browse the repository at this point in the history
  • Loading branch information
neacsu committed Jan 22, 2025
1 parent 1aec8be commit f4dfa8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
14 changes: 10 additions & 4 deletions common/client-core/src/client/base_client/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// TODO: combine those more closely. Perhaps into a single underlying store.
// Like for persistent, on-disk, storage, what's the point of having 3 different databases?

use rand::rngs::OsRng;

use crate::client::key_manager::persistence::{InMemEphemeralKeys, KeyStore};
use crate::client::replies::reply_storage;
use crate::client::replies::reply_storage::ReplyStorageBackend;
Expand Down Expand Up @@ -63,17 +65,21 @@ pub trait MixnetClientStorage {
fn gateway_details_store(&self) -> &Self::GatewaysDetailsStore;
}

#[derive(Default)]
pub struct Ephemeral {
key_store: InMemEphemeralKeys,
reply_store: reply_storage::Empty,
credential_store: EphemeralCredentialStorage,
gateway_details_store: InMemGatewaysDetails,
}

impl Ephemeral {
pub fn new() -> Self {
Default::default()
impl Default for Ephemeral {
fn default() -> Self {
Ephemeral {
key_store: InMemEphemeralKeys::new(&mut OsRng),
reply_store: Default::default(),
credential_store: Default::default(),
gateway_details_store: Default::default(),
}
}
}

Expand Down
19 changes: 15 additions & 4 deletions common/client-core/src/client/key_manager/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::client::key_manager::ClientKeys;
use async_trait::async_trait;
use rand::{CryptoRng, RngCore};
use std::error::Error;
use tokio::sync::Mutex;

Expand Down Expand Up @@ -193,9 +194,19 @@ impl KeyStore for OnDiskKeys {
}
}

#[derive(Default)]
pub struct InMemEphemeralKeys {
keys: Mutex<Option<ClientKeys>>,
keys: Mutex<ClientKeys>,
}

impl InMemEphemeralKeys {
pub fn new<R>(rng: &mut R) -> Self
where
R: RngCore + CryptoRng,
{
InMemEphemeralKeys {
keys: Mutex::new(ClientKeys::generate_new(rng)),
}
}
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -208,11 +219,11 @@ impl KeyStore for InMemEphemeralKeys {
type StorageError = EphemeralKeysError;

async fn load_keys(&self) -> Result<ClientKeys, Self::StorageError> {
self.keys.lock().await.clone().ok_or(EphemeralKeysError)
Ok(self.keys.lock().await.clone())
}

async fn store_keys(&self, keys: &ClientKeys) -> Result<(), Self::StorageError> {
*self.keys.lock().await = Some(keys.clone());
*self.keys.lock().await = keys.clone();
Ok(())
}
}

0 comments on commit f4dfa8d

Please sign in to comment.