Skip to content

Commit

Permalink
New storage APIs (openmls#1565)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Winkelmann (keks) <[email protected]>
  • Loading branch information
franziskuskiefer and keks authored Apr 26, 2024
1 parent e2cba32 commit f7335a7
Show file tree
Hide file tree
Showing 124 changed files with 5,023 additions and 2,141 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
"fuzz",
"cli",
"interop_client",
"memory_keystore",
"memory_storage",
"delivery-service/ds",
"delivery-service/ds-lib",
"basic_credential",
Expand Down
43 changes: 31 additions & 12 deletions basic_credential/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use std::fmt::Debug;

use openmls_traits::{
key_store::{MlsEntity, MlsEntityId, OpenMlsKeyStore},
signatures::{Signer, SignerError},
storage::{self, StorageProvider, CURRENT_VERSION},
types::{CryptoError, SignatureScheme},
};

use p256::ecdsa::{signature::Signer as P256Signer, Signature, SigningKey};

use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use tls_codec::{TlsDeserialize, TlsDeserializeBytes, TlsSerialize, TlsSize};

/// A signature key pair for the basic credential.
Expand Down Expand Up @@ -75,10 +76,6 @@ fn id(public_key: &[u8], signature_scheme: SignatureScheme) -> Vec<u8> {
id
}

impl MlsEntity for SignatureKeyPair {
const ID: MlsEntityId = MlsEntityId::SignatureKeyPair;
}

impl SignatureKeyPair {
/// Generates a fresh signature keypair using the [`SignatureScheme`].
pub fn new(signature_scheme: SignatureScheme) -> Result<Self, CryptoError> {
Expand Down Expand Up @@ -112,25 +109,32 @@ impl SignatureKeyPair {
}
}

fn id(&self) -> Vec<u8> {
id(&self.public, self.signature_scheme)
fn id(&self) -> StorageId {
StorageId {
value: id(&self.public, self.signature_scheme),
}
}

/// Store this signature key pair in the key store.
pub fn store<T>(&self, key_store: &T) -> Result<(), <T as OpenMlsKeyStore>::Error>
pub fn store<T>(&self, store: &T) -> Result<(), T::Error>
where
T: OpenMlsKeyStore,
T: StorageProvider<CURRENT_VERSION>,
{
key_store.store(&self.id(), self)
store.write_signature_key_pair(&self.id(), self)
}

/// Read a signature key pair from the key store.
pub fn read(
key_store: &impl OpenMlsKeyStore,
store: &impl StorageProvider<CURRENT_VERSION>,
public_key: &[u8],
signature_scheme: SignatureScheme,
) -> Option<Self> {
key_store.read(&id(public_key, signature_scheme))
store
.signature_key_pair(&StorageId {
value: id(public_key, signature_scheme),
})
.ok()
.flatten()
}

/// Get the public key as byte slice.
Expand All @@ -153,3 +157,18 @@ impl SignatureKeyPair {
&self.private
}
}

// Storage

#[derive(Debug, Serialize, Deserialize)]
struct StorageId {
value: Vec<u8>,
}

// Implement key traits for the storage id
impl storage::Key<CURRENT_VERSION> for StorageId {}
impl storage::traits::SignaturePublicKey<CURRENT_VERSION> for StorageId {}

// Implement entity trait for the signature key pair
impl storage::Entity<CURRENT_VERSION> for SignatureKeyPair {}
impl storage::traits::SignatureKeyPair<CURRENT_VERSION> for SignatureKeyPair {}
4 changes: 3 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ openmls = { path = "../openmls", features = ["test-utils"] }
ds-lib = { path = "../delivery-service/ds-lib" }
openmls_traits = { path = "../traits" }
openmls_rust_crypto = { path = "../openmls_rust_crypto" }
openmls_memory_keystore = { path = "../memory_keystore" }
openmls_memory_storage = { path = "../memory_storage", features = [
"persistence",
] }
openmls_basic_credential = { path = "../basic_credential" }
serde = { version = "^1.0" }
thiserror = "1.0"
Expand Down
6 changes: 0 additions & 6 deletions cli/src/file_helpers.rs

This file was deleted.

10 changes: 6 additions & 4 deletions cli/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Identity {
credential: credential.into(),
signature_key: signature_keys.to_public_vec().into(),
};
signature_keys.store(crypto.key_store()).unwrap();
signature_keys.store(crypto.storage()).unwrap();

let key_package = KeyPackage::builder()
.build(
Expand All @@ -43,11 +43,12 @@ impl Identity {
Self {
kp: HashMap::from([(
key_package
.key_package()
.hash_ref(crypto.crypto())
.unwrap()
.as_slice()
.to_vec(),
key_package,
key_package.key_package().clone(),
)]),
credential_with_key,
signer: signature_keys,
Expand All @@ -71,13 +72,14 @@ impl Identity {

self.kp.insert(
key_package
.key_package()
.hash_ref(crypto.crypto())
.unwrap()
.as_slice()
.to_vec(),
key_package.clone(),
key_package.key_package().clone(),
);
key_package
key_package.key_package().clone()
}

/// Get the plain identity as byte vector.
Expand Down
2 changes: 0 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use termion::input::TermRead;

mod backend;
mod conversation;
mod file_helpers;
mod identity;
mod networking;
mod openmls_rust_persistent_crypto;
mod persistent_key_store;
mod serialize_any_hashmap;
mod user;

Expand Down
15 changes: 7 additions & 8 deletions cli/src/openmls_rust_persistent_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
//! This is an implementation of the [`OpenMlsCryptoProvider`] trait to use with
//! OpenMLS.
use super::persistent_key_store::PersistentKeyStore;
use openmls_rust_crypto::RustCrypto;
use openmls_rust_crypto::{MemoryStorage, RustCrypto};
use openmls_traits::OpenMlsProvider;

#[derive(Default, Debug)]
pub struct OpenMlsRustPersistentCrypto {
crypto: RustCrypto,
key_store: PersistentKeyStore,
storage: MemoryStorage,
}

impl OpenMlsProvider for OpenMlsRustPersistentCrypto {
type CryptoProvider = RustCrypto;
type RandProvider = RustCrypto;
type KeyStoreProvider = PersistentKeyStore;
type StorageProvider = MemoryStorage;

fn crypto(&self) -> &Self::CryptoProvider {
&self.crypto
Expand All @@ -26,17 +25,17 @@ impl OpenMlsProvider for OpenMlsRustPersistentCrypto {
&self.crypto
}

fn key_store(&self) -> &Self::KeyStoreProvider {
&self.key_store
fn storage(&self) -> &Self::StorageProvider {
&self.storage
}
}

impl OpenMlsRustPersistentCrypto {
pub fn save_keystore(&self, user_name: String) -> Result<(), String> {
self.key_store.save(user_name)
self.storage.save(user_name)
}

pub fn load_keystore(&mut self, user_name: String) -> Result<(), String> {
self.key_store.load(user_name)
self.storage.load(user_name)
}
}
131 changes: 0 additions & 131 deletions cli/src/persistent_key_store.rs

This file was deleted.

Loading

0 comments on commit f7335a7

Please sign in to comment.