Skip to content

Commit

Permalink
Allow items in KeyStore to expire
Browse files Browse the repository at this point in the history
  • Loading branch information
Bren2010 committed Feb 22, 2024
1 parent 0da7dcb commit 3992d09
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion basic_credential/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl SignatureKeyPair {
where
T: OpenMlsKeyStore,
{
key_store.store(&self.id(), self)
key_store.store(&self.id(), self, None)
}

/// Read a signature key pair from the key store.
Expand Down
2 changes: 1 addition & 1 deletion cli/src/persistent_key_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl OpenMlsKeyStore for PersistentKeyStore {
/// serialization for ID `k`.
///
/// Returns an error if storing fails.
fn store<V: MlsEntity>(&self, k: &[u8], v: &V) -> Result<(), Self::Error> {
fn store<V: MlsEntity>(&self, k: &[u8], v: &V, _: Option<u64>) -> Result<(), Self::Error> {
let value =
serde_json::to_vec(v).map_err(|_| PersistentKeyStoreError::SerializationError)?;
// We unwrap here, because this is the only function claiming a write
Expand Down
5 changes: 3 additions & 2 deletions interop_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl MlsClient for MlsClientImpl {
// Store keys so OpenMLS can find them.
crypto_provider
.key_store()
.store(my_key_package.hpke_init_key().as_slice(), &private_key)
.store(my_key_package.hpke_init_key().as_slice(), &private_key, None)
.map_err(|_| Status::aborted("failed to interact with the key store"))?;

// Store the key package in the key store with the hash reference as id
Expand All @@ -414,6 +414,7 @@ impl MlsClient for MlsClientImpl {
.map_err(into_status)?
.as_slice(),
&my_key_package,
None,
)
.map_err(into_status)?;

Expand All @@ -424,7 +425,7 @@ impl MlsClient for MlsClientImpl {
// The key is the public key.
crypto_provider
.key_store()
.store::<HpkePrivateKey>(my_key_package.hpke_init_key().as_slice(), &private_key)
.store::<HpkePrivateKey>(my_key_package.hpke_init_key().as_slice(), &private_key, None)
.map_err(into_status)?;

let welcome_msg = MlsMessageIn::tls_deserialize(&mut request.welcome.as_slice())
Expand Down
2 changes: 1 addition & 1 deletion memory_keystore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl OpenMlsKeyStore for MemoryKeyStore {
/// serialization for ID `k`.
///
/// Returns an error if storing fails.
fn store<V: MlsEntity>(&self, k: &[u8], v: &V) -> Result<(), Self::Error> {
fn store<V: MlsEntity>(&self, k: &[u8], v: &V, _: Option<u64>) -> Result<(), Self::Error> {
let value = serde_json::to_vec(v).map_err(|_| MemoryKeyStoreError::SerializationError)?;
// We unwrap here, because this is the only function claiming a write
// lock on `credential_bundles`. It only holds the lock very briefly and
Expand Down
4 changes: 3 additions & 1 deletion openmls/src/group/core_group/kat_passive_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl PassiveClient {
.unwrap()
.as_slice(),
&key_package,
None,
)
.unwrap();

Expand All @@ -272,6 +273,7 @@ impl PassiveClient {
.store::<HpkePrivateKey>(
key_package.hpke_init_key().as_slice(),
key_package_bundle.private_key(),
None,
)
.unwrap();

Expand All @@ -282,7 +284,7 @@ impl PassiveClient {
));

key_pair
.write_to_key_store(self.provider.key_store())
.write_to_key_store(self.provider.key_store(), None)
.unwrap();
}

Expand Down
2 changes: 2 additions & 0 deletions openmls/src/group/core_group/kat_welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ pub fn run_test_vector(test_vector: WelcomeTestVector) -> Result<(), &'static st
.store(
key_package.hash_ref(provider.crypto()).unwrap().as_slice(),
&key_package,
None,
)
.unwrap();

Expand All @@ -179,6 +180,7 @@ pub fn run_test_vector(test_vector: WelcomeTestVector) -> Result<(), &'static st
.store::<HpkePrivateKey>(
key_package.hpke_init_key().as_slice(),
key_package_bundle.private_key(),
None,
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion openmls/src/group/core_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl CoreGroup {
self.context().epoch().as_u64(),
self.own_leaf_index(),
);
store.store(&k.0, &keypair_references.to_vec())
store.store(&k.0, &keypair_references.to_vec(), None)
}

/// Read the [`EncryptionKeyPair`]s of this group and its current
Expand Down
2 changes: 1 addition & 1 deletion openmls/src/group/mls_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl MlsGroup {
&mut self,
store: &KeyStore,
) -> Result<(), KeyStore::Error> {
store.store(self.group_id().as_slice(), &*self)?;
store.store(self.group_id().as_slice(), &*self, None)?;

self.state_changed = InnerState::Persisted;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion openmls/src/group/mls_group/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl MlsGroup {
)?;
// TODO #1207: Move to the top of the function.
keypair
.write_to_key_store(provider.key_store())
.write_to_key_store(provider.key_store(), None)
.map_err(ProposeSelfUpdateError::KeyStoreError)?;
};

Expand Down
2 changes: 1 addition & 1 deletion openmls/src/group/tests/test_proposal_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ fn test_valsem110(ciphersuite: Ciphersuite, provider: &impl OpenMlsProvider) {
.find(|keypair| keypair.public_key() == &alice_encryption_key)
.unwrap();
leaf_keypair
.write_to_key_store(provider.key_store())
.write_to_key_store(provider.key_store(), None)
.unwrap();

// Have bob process the resulting plaintext
Expand Down
28 changes: 17 additions & 11 deletions openmls/src/key_packages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ impl KeyPackage {
leaf_node_extensions,
init_key,
)?;
let expiration = Some(key_package.life_time().not_after());

// Store the key package in the key store with the hash reference as id
// for retrieval when parsing welcome messages.
Expand All @@ -430,12 +431,13 @@ impl KeyPackage {
.store(
key_package.hash_ref(provider.crypto())?.as_slice(),
&key_package,
expiration,
)
.map_err(KeyPackageNewError::KeyStoreError)?;

// Store the encryption key pair in the key store.
encryption_key_pair
.write_to_key_store(provider.key_store())
.write_to_key_store(provider.key_store(), expiration)
.map_err(KeyPackageNewError::KeyStoreError)?;

Ok(key_package)
Expand All @@ -461,13 +463,6 @@ impl KeyPackage {
.crypto()
.derive_hpke_keypair(config.ciphersuite.hpke_config(), ikm.as_slice());

// Store the private part of the init_key into the key store.
// The key is the public key.
provider
.key_store()
.store::<HpkePrivateKey>(&init_key.public, &init_key.private)
.map_err(KeyPackageNewError::KeyStoreError)?;

// We don't need the private key here. It's stored in the key store for
// use later when creating a group with this key package.
let leaf_node = LeafNode::create_new_with_key(
Expand All @@ -484,12 +479,20 @@ impl KeyPackage {
let key_package = KeyPackageTbs {
protocol_version: config.version,
ciphersuite: config.ciphersuite,
init_key: init_key.public.into(),
init_key: init_key.public.clone().into(),
leaf_node,
extensions,
};

let key_package = key_package.sign(signer)?;
let expiration = Some(key_package.life_time().not_after());

// Store the private part of the init_key into the key store.
// The key is the public key.
provider
.key_store()
.store::<HpkePrivateKey>(&init_key.public, &init_key.private, expiration)
.map_err(KeyPackageNewError::KeyStoreError)?;

// Store the key package in the key store with the hash reference as id
// for retrieval when parsing welcome messages.
Expand All @@ -498,6 +501,7 @@ impl KeyPackage {
.store(
key_package.hash_ref(provider.crypto())?.as_slice(),
&key_package,
expiration,
)
.map_err(KeyPackageNewError::KeyStoreError)?;

Expand Down Expand Up @@ -643,6 +647,7 @@ impl KeyPackageBuilder {
self.leaf_node_capabilities.unwrap_or_default(),
self.leaf_node_extensions.unwrap_or_default(),
)?;
let expiration = Some(key_package.life_time().not_after());

// Store the key package in the key store with the hash reference as id
// for retrieval when parsing welcome messages.
Expand All @@ -651,19 +656,20 @@ impl KeyPackageBuilder {
.store(
key_package.hash_ref(provider.crypto())?.as_slice(),
&key_package,
expiration,
)
.map_err(KeyPackageNewError::KeyStoreError)?;

// Store the encryption key pair in the key store.
encryption_keypair
.write_to_key_store(provider.key_store())
.write_to_key_store(provider.key_store(), expiration)
.map_err(KeyPackageNewError::KeyStoreError)?;

// Store the private part of the init_key into the key store.
// The key is the public key.
provider
.key_store()
.store::<HpkePrivateKey>(key_package.hpke_init_key().as_slice(), &init_private_key)
.store::<HpkePrivateKey>(key_package.hpke_init_key().as_slice(), &init_private_key, expiration)
.map_err(KeyPackageNewError::KeyStoreError)?;

Ok(key_package)
Expand Down
5 changes: 3 additions & 2 deletions openmls/src/messages/tests/test_welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,16 @@ fn test_welcome_context_mismatch(ciphersuite: Ciphersuite, provider: &impl OpenM
.store(
bob_kp.hash_ref(provider.crypto()).unwrap().as_slice(),
bob_kp,
None,
)
.unwrap();
provider
.key_store()
.store::<HpkePrivateKey>(bob_kp.hpke_init_key().as_slice(), bob_private_key)
.store::<HpkePrivateKey>(bob_kp.hpke_init_key().as_slice(), bob_private_key, None)
.unwrap();

encryption_keypair
.write_to_key_store(provider.key_store())
.write_to_key_store(provider.key_store(), None)
.unwrap();

let _group = MlsGroup::new_from_welcome(
Expand Down
2 changes: 1 addition & 1 deletion openmls/src/schedule/psk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl PreSharedKeyId {

provider
.key_store()
.store(&keystore_id, &psk_bundle)
.store(&keystore_id, &psk_bundle, None)
.map_err(|_| PskError::KeyStore)
}

Expand Down
5 changes: 3 additions & 2 deletions openmls/src/treesync/node/encryption_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ impl EncryptionKeyPair {
pub(crate) fn write_to_key_store<KeyStore: OpenMlsKeyStore>(
&self,
store: &KeyStore,
expiration: Option<u64>,
) -> Result<(), KeyStore::Error> {
store.store(&self.public_key().to_bytes_with_prefix(), self)
store.store(&self.public_key().to_bytes_with_prefix(), self, expiration)
}

/// Read the [`EncryptionKeyPair`] from the key store of the `provider`. This
Expand Down Expand Up @@ -239,7 +240,7 @@ pub mod test_utils {
pub fn write_keys_from_key_store(provider: &impl OpenMlsProvider, encryption_key: HpkeKeyPair) {
let keypair = EncryptionKeyPair::from(encryption_key);

keypair.write_to_key_store(provider.key_store()).unwrap();
keypair.write_to_key_store(provider.key_store(), None).unwrap();
}
}

Expand Down
2 changes: 1 addition & 1 deletion openmls/src/treesync/node/leaf_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl LeafNode {

// Store the encryption key pair in the key store.
encryption_key_pair
.write_to_key_store(provider.key_store())
.write_to_key_store(provider.key_store(), None)
.map_err(LeafNodeGenerationError::KeyStoreError)?;

Ok(leaf_node)
Expand Down
2 changes: 1 addition & 1 deletion traits/src/key_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait OpenMlsKeyStore {
/// serialization for ID `k`.
///
/// Returns an error if storing fails.
fn store<V: MlsEntity>(&self, k: &[u8], v: &V) -> Result<(), Self::Error>
fn store<V: MlsEntity>(&self, k: &[u8], v: &V, expiration: Option<u64>) -> Result<(), Self::Error>
where
Self: Sized;

Expand Down

0 comments on commit 3992d09

Please sign in to comment.