diff --git a/keystore/src/entities/mls.rs b/keystore/src/entities/mls.rs index b36a8358c2..c7bf8b1323 100644 --- a/keystore/src/entities/mls.rs +++ b/keystore/src/entities/mls.rs @@ -20,13 +20,15 @@ use openmls_traits::types::SignatureScheme; use zeroize::Zeroize; /// Entity representing a persisted `MlsGroup` -#[derive(Debug, Clone, PartialEq, Eq, Zeroize)] +#[derive(Debug, Clone, PartialEq, Eq, Zeroize, core_crypto_macros::Entity)] #[zeroize(drop)] +#[entity(collection_name = "mls_groups")] #[cfg_attr( any(target_family = "wasm", feature = "serde"), derive(serde::Serialize, serde::Deserialize) )] pub struct PersistedMlsGroup { + #[id(hex, column = "id_hex")] pub id: Vec, pub state: Vec, pub parent_id: Option>, @@ -158,10 +160,12 @@ pub struct MlsEncryptionKeyPair { } /// Entity representing a list of [MlsEncryptionKeyPair] -#[derive(Debug, Clone, PartialEq, Eq, Zeroize)] +#[derive(Debug, Clone, PartialEq, Eq, Zeroize, core_crypto_macros::Entity)] #[zeroize(drop)] +#[entity(collection_name = "mls_epoch_encryption_keypairs")] #[cfg_attr(target_family = "wasm", derive(serde::Serialize, serde::Deserialize))] pub struct MlsEpochEncryptionKeyPair { + #[id(hex, column = "id_hex")] pub id: Vec, pub keypairs: Vec, } @@ -179,13 +183,15 @@ pub struct MlsPskBundle { } /// Entity representing a persisted `KeyPackage` -#[derive(Debug, Clone, PartialEq, Eq, Zeroize)] +#[derive(Debug, Clone, PartialEq, Eq, Zeroize, core_crypto_macros::Entity)] #[zeroize(drop)] +#[entity(collection_name = "mls_keypackages")] #[cfg_attr( any(target_family = "wasm", feature = "serde"), derive(serde::Serialize, serde::Deserialize) )] pub struct MlsKeyPackage { + #[id(hex, column = "keypackage_ref_hex")] pub keypackage_ref: Vec, pub keypackage: Vec, } diff --git a/keystore/src/entities/platform/generic/mls/epoch_encryption_keypair.rs b/keystore/src/entities/platform/generic/mls/epoch_encryption_keypair.rs deleted file mode 100644 index 9a87836018..0000000000 --- a/keystore/src/entities/platform/generic/mls/epoch_encryption_keypair.rs +++ /dev/null @@ -1,182 +0,0 @@ -// Wire -// Copyright (C) 2022 Wire Swiss GmbH - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/. - -use crate::{ - connection::TransactionWrapper, - entities::{EntityIdStringExt, EntityTransactionExt, MlsEpochEncryptionKeyPair}, - CryptoKeystoreResult, -}; -use crate::{ - connection::{DatabaseConnection, KeystoreDatabaseConnection}, - entities::{Entity, EntityBase, EntityFindParams, StringEntityId}, - MissingKeyErrorKind, -}; -use rusqlite::ToSql; -use std::io::{Read, Write}; - -#[async_trait::async_trait] -impl Entity for MlsEpochEncryptionKeyPair { - fn id_raw(&self) -> &[u8] { - self.id.as_slice() - } - - async fn find_all( - conn: &mut Self::ConnectionType, - params: EntityFindParams, - ) -> crate::CryptoKeystoreResult> { - let mut conn = conn.conn().await; - let transaction = conn.transaction()?; - let query: String = format!( - "SELECT rowid, id_hex FROM mls_epoch_encryption_keypairs {}", - params.to_sql() - ); - - let mut stmt = transaction.prepare_cached(&query)?; - let mut rows = stmt.query_map([], |r| { - let rowid: i64 = r.get(0)?; - let id_hex: String = r.get(1)?; - Ok((rowid, id_hex)) - })?; - let entities = rows.try_fold(Vec::new(), |mut acc, row_result| { - use std::io::Read as _; - let (rowid, id_hex) = row_result?; - - let id = Self::id_from_hex(&id_hex)?; - - let mut blob = transaction.blob_open( - rusqlite::DatabaseName::Main, - "mls_epoch_encryption_keypairs", - "keypairs", - rowid, - true, - )?; - - let mut keypairs = vec![]; - blob.read_to_end(&mut keypairs)?; - blob.close()?; - - acc.push(Self { id, keypairs }); - - crate::CryptoKeystoreResult::Ok(acc) - })?; - - Ok(entities) - } - - async fn find_one( - conn: &mut Self::ConnectionType, - id: &StringEntityId, - ) -> crate::CryptoKeystoreResult> { - let mut conn = conn.conn().await; - let transaction = conn.transaction()?; - use rusqlite::OptionalExtension as _; - let maybe_rowid = transaction - .query_row( - "SELECT rowid FROM mls_epoch_encryption_keypairs WHERE id_hex = ?", - [id.as_hex_string().to_sql()?], - |r| r.get::<_, i64>(0), - ) - .optional()?; - - if let Some(rowid) = maybe_rowid { - let id = id.as_slice().to_vec(); - - let mut blob = transaction.blob_open( - rusqlite::DatabaseName::Main, - "mls_epoch_encryption_keypairs", - "keypairs", - rowid, - true, - )?; - - let mut keypairs = Vec::with_capacity(blob.len()); - blob.read_to_end(&mut keypairs)?; - blob.close()?; - - Ok(Some(Self { id, keypairs })) - } else { - Ok(None) - } - } - - async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult { - let conn = conn.conn().await; - conn.query_row("SELECT COUNT(*) FROM mls_epoch_encryption_keypairs", [], |r| r.get(0)) - .map_err(Into::into) - } -} - -#[async_trait::async_trait] -impl EntityBase for MlsEpochEncryptionKeyPair { - type ConnectionType = KeystoreDatabaseConnection; - type AutoGeneratedFields = (); - const COLLECTION_NAME: &'static str = "mls_epoch_encryption_keypairs"; - - fn to_missing_key_err_kind() -> MissingKeyErrorKind { - MissingKeyErrorKind::MlsEpochEncryptionKeyPair - } - - fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity { - crate::transaction::dynamic_dispatch::Entity::EpochEncryptionKeyPair(self) - } -} - -#[async_trait::async_trait] -impl EntityTransactionExt for MlsEpochEncryptionKeyPair { - async fn save(&self, transaction: &TransactionWrapper<'_>) -> CryptoKeystoreResult<()> { - Self::ConnectionType::check_buffer_size(self.keypairs.len())?; - - let zb_keypairs = rusqlite::blob::ZeroBlob(self.keypairs.len() as i32); - - // Use UPSERT (ON CONFLICT DO UPDATE) - let sql = " - INSERT INTO mls_epoch_encryption_keypairs (id_hex, keypairs) - VALUES (?, ?) - ON CONFLICT(id_hex) DO UPDATE SET keypairs = excluded.keypairs - RETURNING rowid"; - - let row_id: i64 = - transaction.query_row(sql, [&self.id_hex().to_sql()?, &zb_keypairs.to_sql()?], |r| r.get(0))?; - - let mut blob = transaction.blob_open( - rusqlite::DatabaseName::Main, - "mls_epoch_encryption_keypairs", - "keypairs", - row_id, - false, - )?; - - blob.write_all(&self.keypairs)?; - blob.close()?; - - Ok(()) - } - async fn delete_fail_on_missing_id( - transaction: &TransactionWrapper<'_>, - id: StringEntityId<'_>, - ) -> CryptoKeystoreResult<()> { - let updated = transaction.execute( - "DELETE FROM mls_epoch_encryption_keypairs WHERE id_hex = ?", - [id.as_hex_string()], - )?; - - if updated > 0 { - Ok(()) - } else { - Err(Self::to_missing_key_err_kind().into()) - } - } -} diff --git a/keystore/src/entities/platform/generic/mls/group.rs b/keystore/src/entities/platform/generic/mls/group.rs index 67f945ca00..f4e39ff32f 100644 --- a/keystore/src/entities/platform/generic/mls/group.rs +++ b/keystore/src/entities/platform/generic/mls/group.rs @@ -15,186 +15,9 @@ // along with this program. If not, see http://www.gnu.org/licenses/. use crate::{ - connection::TransactionWrapper, - entities::{EntityIdStringExt, EntityTransactionExt}, + entities::{Entity, EntityBase, PersistedMlsGroup, PersistedMlsGroupExt}, + CryptoKeystoreResult, }; -use crate::{ - connection::{DatabaseConnection, KeystoreDatabaseConnection}, - entities::{Entity, EntityBase, EntityFindParams, PersistedMlsGroup, PersistedMlsGroupExt, StringEntityId}, - CryptoKeystoreResult, MissingKeyErrorKind, -}; - -#[async_trait::async_trait] -impl Entity for PersistedMlsGroup { - fn id_raw(&self) -> &[u8] { - self.id.as_slice() - } - async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult> { - let mut conn = conn.conn().await; - let transaction = conn.transaction()?; - let query: String = format!("SELECT rowid, id_hex FROM mls_groups {}", params.to_sql()); - - let mut stmt = transaction.prepare_cached(&query)?; - let mut rows = stmt.query_map([], |r| { - let rowid: i64 = r.get(0)?; - let id_hex: String = r.get(1)?; - Ok((rowid, id_hex)) - })?; - let entities = rows.try_fold(Vec::new(), |mut acc, row_result| { - use std::io::Read as _; - let (rowid, id_hex) = row_result?; - - let id = Self::id_from_hex(&id_hex)?; - - let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, "mls_groups", "state", rowid, true)?; - let mut state = vec![]; - blob.read_to_end(&mut state)?; - blob.close()?; - - let mut parent_id = None; - if let Ok(mut blob) = - transaction.blob_open(rusqlite::DatabaseName::Main, "mls_groups", "parent_id", rowid, true) - { - if !blob.is_empty() { - let mut tmp = Vec::with_capacity(blob.len()); - blob.read_to_end(&mut tmp)?; - parent_id.replace(tmp); - } - blob.close()?; - } - - acc.push(Self { id, parent_id, state }); - crate::CryptoKeystoreResult::Ok(acc) - })?; - - Ok(entities) - } - - async fn find_one( - conn: &mut Self::ConnectionType, - id: &StringEntityId, - ) -> crate::CryptoKeystoreResult> { - use rusqlite::OptionalExtension as _; - let mut conn = conn.conn().await; - let transaction = conn.transaction()?; - let mut rowid: Option = transaction - .query_row( - "SELECT rowid FROM mls_groups WHERE id_hex = ?", - [id.as_hex_string()], - |r| r.get::<_, i64>(0), - ) - .optional()?; - - if let Some(rowid) = rowid.take() { - let id = id.as_slice().to_vec(); - - use std::io::Read as _; - let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, "mls_groups", "state", rowid, true)?; - let mut state = Vec::with_capacity(blob.len()); - blob.read_to_end(&mut state)?; - blob.close()?; - - let mut parent_id = None; - if let Ok(mut blob) = - transaction.blob_open(rusqlite::DatabaseName::Main, "mls_groups", "parent_id", rowid, true) - { - if !blob.is_empty() { - let mut tmp = Vec::with_capacity(blob.len()); - blob.read_to_end(&mut tmp)?; - parent_id.replace(tmp); - } - blob.close()?; - } - - Ok(Some(Self { id, parent_id, state })) - } else { - Ok(None) - } - } - - async fn find_many( - conn: &mut Self::ConnectionType, - _ids: &[StringEntityId], - ) -> crate::CryptoKeystoreResult> { - // Plot twist: we always select ALL the persisted groups. Unsure if we want to make it a real API with selection - Self::find_all(conn, EntityFindParams::default()).await - } - - async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult { - let conn = conn.conn().await; - conn.query_row("SELECT COUNT(*) FROM mls_groups", [], |r| r.get(0)) - .map_err(Into::into) - } -} - -#[async_trait::async_trait] -impl EntityBase for PersistedMlsGroup { - type ConnectionType = KeystoreDatabaseConnection; - type AutoGeneratedFields = (); - const COLLECTION_NAME: &'static str = "mls_groups"; - - fn to_missing_key_err_kind() -> MissingKeyErrorKind { - MissingKeyErrorKind::MlsGroup - } - - fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity { - crate::transaction::dynamic_dispatch::Entity::PersistedMlsGroup(self) - } -} - -#[async_trait::async_trait] -impl EntityTransactionExt for PersistedMlsGroup { - async fn save(&self, transaction: &TransactionWrapper<'_>) -> CryptoKeystoreResult<()> { - use rusqlite::ToSql as _; - - let state = &self.state; - let parent_id = self.parent_id.as_ref(); - - Self::ConnectionType::check_buffer_size(state.len())?; - Self::ConnectionType::check_buffer_size(parent_id.map(Vec::len).unwrap_or_default())?; - - let zbs = rusqlite::blob::ZeroBlob(state.len() as i32); - let zbpid = rusqlite::blob::ZeroBlob(parent_id.map(Vec::len).unwrap_or_default() as i32); - - // Use UPSERT (ON CONFLICT DO UPDATE) - let sql = " - INSERT INTO mls_groups (id_hex, state, parent_id) - VALUES (?, ?, ?) - ON CONFLICT(id_hex) DO UPDATE SET state = excluded.state, parent_id = excluded.parent_id - RETURNING rowid"; - - let rowid: i64 = - transaction.query_row(sql, [&self.id_hex().to_sql()?, &zbs.to_sql()?, &zbpid.to_sql()?], |r| { - r.get(0) - })?; - - let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, "mls_groups", "state", rowid, false)?; - use std::io::Write as _; - blob.write_all(state)?; - blob.close()?; - - let mut blob = transaction.blob_open(rusqlite::DatabaseName::Main, "mls_groups", "parent_id", rowid, false)?; - if let Some(parent_id) = parent_id { - blob.write_all(parent_id)?; - } - blob.close()?; - - Ok(()) - } - - async fn delete_fail_on_missing_id( - transaction: &TransactionWrapper<'_>, - id: StringEntityId<'_>, - ) -> CryptoKeystoreResult<()> { - let updated = transaction.execute("DELETE FROM mls_groups WHERE id_hex = ?", [id.as_hex_string()])?; - - if updated > 0 { - Ok(()) - } else { - Err(Self::to_missing_key_err_kind().into()) - } - } -} #[async_trait::async_trait] impl PersistedMlsGroupExt for PersistedMlsGroup { diff --git a/keystore/src/entities/platform/generic/mls/keypackage.rs b/keystore/src/entities/platform/generic/mls/keypackage.rs deleted file mode 100644 index b1c3f58809..0000000000 --- a/keystore/src/entities/platform/generic/mls/keypackage.rs +++ /dev/null @@ -1,189 +0,0 @@ -// Wire -// Copyright (C) 2022 Wire Swiss GmbH - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/. - -use crate::{ - connection::TransactionWrapper, - entities::{EntityIdStringExt, EntityTransactionExt}, - CryptoKeystoreResult, -}; -use crate::{ - connection::{DatabaseConnection, KeystoreDatabaseConnection}, - entities::{Entity, EntityBase, EntityFindParams, MlsKeyPackage, StringEntityId}, - MissingKeyErrorKind, -}; -use rusqlite::ToSql; -use std::io::{Read, Write}; - -#[async_trait::async_trait] -impl Entity for MlsKeyPackage { - fn id_raw(&self) -> &[u8] { - self.keypackage_ref.as_slice() - } - - async fn find_all( - conn: &mut Self::ConnectionType, - params: EntityFindParams, - ) -> crate::CryptoKeystoreResult> { - let mut conn = conn.conn().await; - let transaction = conn.transaction()?; - let query: String = format!( - "SELECT rowid, keypackage_ref_hex FROM mls_keypackages {}", - params.to_sql() - ); - - let mut stmt = transaction.prepare_cached(&query)?; - let mut rows = stmt.query_map([], |r| { - let rowid: i64 = r.get(0)?; - let keypackage_ref_hex: String = r.get(1)?; - Ok((rowid, keypackage_ref_hex)) - })?; - let entities = rows.try_fold(Vec::new(), |mut acc, row_result| { - use std::io::Read as _; - let (rowid, keypackage_ref_hex) = row_result?; - - let keypackage_ref = Self::id_from_hex(&keypackage_ref_hex)?; - - let mut blob = transaction.blob_open( - rusqlite::DatabaseName::Main, - "mls_keypackages", - "keypackage", - rowid, - true, - )?; - let mut keypackage = vec![]; - blob.read_to_end(&mut keypackage)?; - blob.close()?; - - acc.push(Self { - keypackage_ref, - keypackage, - }); - - crate::CryptoKeystoreResult::Ok(acc) - })?; - - Ok(entities) - } - - async fn find_one( - conn: &mut Self::ConnectionType, - id: &StringEntityId, - ) -> crate::CryptoKeystoreResult> { - let mut conn = conn.conn().await; - let transaction = conn.transaction()?; - use rusqlite::OptionalExtension as _; - let mut row_id = transaction - .query_row( - "SELECT rowid FROM mls_keypackages WHERE keypackage_ref_hex = ?", - [id.as_hex_string()], - |r| r.get::<_, i64>(0), - ) - .optional()?; - - if let Some(rowid) = row_id.take() { - let keypackage_ref = id.as_slice().to_vec(); - - let mut blob = transaction.blob_open( - rusqlite::DatabaseName::Main, - "mls_keypackages", - "keypackage", - rowid, - true, - )?; - - let mut keypackage = Vec::with_capacity(blob.len()); - blob.read_to_end(&mut keypackage)?; - blob.close()?; - - transaction.commit()?; - - Ok(Some(Self { - keypackage_ref, - keypackage, - })) - } else { - Ok(None) - } - } - - async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult { - let conn = conn.conn().await; - conn.query_row("SELECT COUNT(*) FROM mls_keypackages", [], |r| r.get(0)) - .map_err(Into::into) - } -} - -#[async_trait::async_trait] -impl EntityBase for MlsKeyPackage { - type ConnectionType = KeystoreDatabaseConnection; - type AutoGeneratedFields = (); - const COLLECTION_NAME: &'static str = "mls_keypackages"; - - fn to_missing_key_err_kind() -> MissingKeyErrorKind { - MissingKeyErrorKind::MlsKeyPackageBundle - } - - fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity { - crate::transaction::dynamic_dispatch::Entity::KeyPackage(self) - } -} - -#[async_trait::async_trait] -impl EntityTransactionExt for MlsKeyPackage { - async fn save(&self, transaction: &TransactionWrapper<'_>) -> CryptoKeystoreResult<()> { - Self::ConnectionType::check_buffer_size(self.keypackage.len())?; - - // Create zero blobs for keypackage and keypackage_ref - let kp_zb = rusqlite::blob::ZeroBlob(self.keypackage.len() as i32); - - // Use UPSERT (ON CONFLICT DO UPDATE) - let sql = " - INSERT INTO mls_keypackages (keypackage_ref_hex, keypackage) - VALUES (?, ?) - ON CONFLICT(keypackage_ref_hex) DO UPDATE SET keypackage = excluded.keypackage - RETURNING rowid"; - - let row_id: i64 = transaction.query_row(sql, [&self.id_hex().to_sql()?, &kp_zb.to_sql()?], |r| r.get(0))?; - - let mut blob = transaction.blob_open( - rusqlite::DatabaseName::Main, - "mls_keypackages", - "keypackage", - row_id, - false, - )?; - blob.write_all(&self.keypackage)?; - blob.close()?; - - Ok(()) - } - - async fn delete_fail_on_missing_id( - transaction: &TransactionWrapper<'_>, - id: StringEntityId<'_>, - ) -> CryptoKeystoreResult<()> { - let updated = transaction.execute( - "DELETE FROM mls_keypackages WHERE keypackage_ref_hex = ?", - [id.as_hex_string()], - )?; - - if updated > 0 { - Ok(()) - } else { - Err(Self::to_missing_key_err_kind().into()) - } - } -} diff --git a/keystore/src/entities/platform/generic/mls/mod.rs b/keystore/src/entities/platform/generic/mls/mod.rs index 84496aa399..ab0efab3cc 100644 --- a/keystore/src/entities/platform/generic/mls/mod.rs +++ b/keystore/src/entities/platform/generic/mls/mod.rs @@ -17,10 +17,8 @@ pub mod credential; pub mod e2ei_acme_ca; pub mod encryption_keypair; -pub mod epoch_encryption_keypair; pub mod group; pub mod hpke_private_key; -pub mod keypackage; pub mod pending_group; pub mod pending_message; pub mod psk_bundle; diff --git a/keystore/src/entities/platform/wasm/mls/epoch_encryption_keypair.rs b/keystore/src/entities/platform/wasm/mls/epoch_encryption_keypair.rs deleted file mode 100644 index f978ab0977..0000000000 --- a/keystore/src/entities/platform/wasm/mls/epoch_encryption_keypair.rs +++ /dev/null @@ -1,75 +0,0 @@ -// Wire -// Copyright (C) 2022 Wire Swiss GmbH - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/. - -use crate::{ - connection::{DatabaseConnection, KeystoreDatabaseConnection}, - entities::{Entity, EntityBase, EntityFindParams, EntityTransactionExt, MlsEpochEncryptionKeyPair, StringEntityId}, - CryptoKeystoreResult, MissingKeyErrorKind, -}; - -#[async_trait::async_trait(?Send)] -impl EntityBase for MlsEpochEncryptionKeyPair { - type ConnectionType = KeystoreDatabaseConnection; - type AutoGeneratedFields = (); - const COLLECTION_NAME: &'static str = "mls_epoch_encryption_keypairs"; - - fn to_missing_key_err_kind() -> MissingKeyErrorKind { - MissingKeyErrorKind::MlsEpochEncryptionKeyPair - } - - fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity { - crate::transaction::dynamic_dispatch::Entity::EpochEncryptionKeyPair(self) - } -} - -#[async_trait::async_trait(?Send)] -impl EntityTransactionExt for MlsEpochEncryptionKeyPair {} - -#[async_trait::async_trait(?Send)] -impl Entity for MlsEpochEncryptionKeyPair { - fn id_raw(&self) -> &[u8] { - self.id.as_slice() - } - - async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult> { - let storage = conn.storage(); - storage.get_all(Self::COLLECTION_NAME, Some(params)).await - } - - async fn find_one( - conn: &mut Self::ConnectionType, - id: &StringEntityId, - ) -> crate::CryptoKeystoreResult> { - conn.storage().get(Self::COLLECTION_NAME, id.as_slice()).await - } - - async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult { - conn.storage().count(Self::COLLECTION_NAME).await - } - - fn encrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()> { - self.keypairs = self.encrypt_data(cipher, self.keypairs.as_slice())?; - Self::ConnectionType::check_buffer_size(self.keypairs.len())?; - - Ok(()) - } - - fn decrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()> { - self.keypairs = self.decrypt_data(cipher, self.keypairs.as_slice())?; - - Ok(()) - } -} diff --git a/keystore/src/entities/platform/wasm/mls/group.rs b/keystore/src/entities/platform/wasm/mls/group.rs index ea86237ce4..c6ae5476e6 100644 --- a/keystore/src/entities/platform/wasm/mls/group.rs +++ b/keystore/src/entities/platform/wasm/mls/group.rs @@ -15,7 +15,7 @@ // along with this program. If not, see http://www.gnu.org/licenses/. use crate::{ - connection::{DatabaseConnection, KeystoreDatabaseConnection}, + connection::KeystoreDatabaseConnection, entities::{ Entity, EntityBase, EntityFindParams, EntityTransactionExt, PersistedMlsGroup, PersistedMlsGroupExt, PersistedMlsPendingGroup, StringEntityId, @@ -23,71 +23,6 @@ use crate::{ CryptoKeystoreResult, MissingKeyErrorKind, }; -#[async_trait::async_trait(?Send)] -impl EntityBase for PersistedMlsGroup { - type ConnectionType = KeystoreDatabaseConnection; - type AutoGeneratedFields = (); - const COLLECTION_NAME: &'static str = "mls_groups"; - - fn to_missing_key_err_kind() -> MissingKeyErrorKind { - MissingKeyErrorKind::PersistedMlsGroup - } - - fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity { - crate::transaction::dynamic_dispatch::Entity::PersistedMlsGroup(self) - } -} - -#[async_trait::async_trait(?Send)] -impl EntityTransactionExt for PersistedMlsGroup {} - -#[async_trait::async_trait(?Send)] -impl Entity for PersistedMlsGroup { - fn id_raw(&self) -> &[u8] { - self.id.as_slice() - } - - async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult> { - let storage = conn.storage(); - storage.get_all(Self::COLLECTION_NAME, Some(params)).await - } - - async fn find_one( - conn: &mut Self::ConnectionType, - id: &StringEntityId, - ) -> crate::CryptoKeystoreResult> { - let storage = conn.storage(); - storage.get(Self::COLLECTION_NAME, id.as_slice()).await - } - - async fn find_many( - conn: &mut Self::ConnectionType, - _ids: &[StringEntityId], - ) -> crate::CryptoKeystoreResult> { - let storage = conn.storage(); - // Plot twist: we always select ALL the persisted groups. Unsure if we want to make it a real API with selection - storage.get_all(Self::COLLECTION_NAME, None).await - } - - async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult { - let storage = conn.storage(); - storage.count(Self::COLLECTION_NAME).await - } - - fn encrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()> { - self.state = self.encrypt_data(cipher, self.state.as_slice())?; - Self::ConnectionType::check_buffer_size(self.state.len())?; - - Ok(()) - } - - fn decrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()> { - self.state = self.decrypt_data(cipher, self.state.as_slice())?; - - Ok(()) - } -} - #[async_trait::async_trait(?Send)] impl PersistedMlsGroupExt for PersistedMlsGroup { fn parent_id(&self) -> Option<&[u8]> { diff --git a/keystore/src/entities/platform/wasm/mls/keypackage.rs b/keystore/src/entities/platform/wasm/mls/keypackage.rs deleted file mode 100644 index 452bcc5c2f..0000000000 --- a/keystore/src/entities/platform/wasm/mls/keypackage.rs +++ /dev/null @@ -1,75 +0,0 @@ -// Wire -// Copyright (C) 2022 Wire Swiss GmbH - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/. - -use crate::{ - connection::{DatabaseConnection, KeystoreDatabaseConnection}, - entities::{Entity, EntityBase, EntityFindParams, EntityTransactionExt, MlsKeyPackage, StringEntityId}, - CryptoKeystoreResult, MissingKeyErrorKind, -}; - -#[async_trait::async_trait(?Send)] -impl EntityBase for MlsKeyPackage { - type ConnectionType = KeystoreDatabaseConnection; - type AutoGeneratedFields = (); - const COLLECTION_NAME: &'static str = "mls_keypackages"; - - fn to_missing_key_err_kind() -> MissingKeyErrorKind { - MissingKeyErrorKind::MlsKeyPackageBundle - } - - fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity { - crate::transaction::dynamic_dispatch::Entity::KeyPackage(self) - } -} - -#[async_trait::async_trait(?Send)] -impl EntityTransactionExt for MlsKeyPackage {} - -#[async_trait::async_trait(?Send)] -impl Entity for MlsKeyPackage { - fn id_raw(&self) -> &[u8] { - self.keypackage_ref.as_slice() - } - - async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult> { - let storage = conn.storage(); - storage.get_all(Self::COLLECTION_NAME, Some(params)).await - } - - async fn find_one( - conn: &mut Self::ConnectionType, - id: &StringEntityId, - ) -> crate::CryptoKeystoreResult> { - conn.storage().get(Self::COLLECTION_NAME, id.as_slice()).await - } - - async fn count(conn: &mut Self::ConnectionType) -> crate::CryptoKeystoreResult { - conn.storage().count(Self::COLLECTION_NAME).await - } - - fn encrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()> { - self.keypackage = self.encrypt_data(cipher, self.keypackage.as_slice())?; - Self::ConnectionType::check_buffer_size(self.keypackage.len())?; - - Ok(()) - } - - fn decrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()> { - self.keypackage = self.decrypt_data(cipher, self.keypackage.as_slice())?; - - Ok(()) - } -} diff --git a/keystore/src/entities/platform/wasm/mls/mod.rs b/keystore/src/entities/platform/wasm/mls/mod.rs index 309c8c1987..7f08864af2 100644 --- a/keystore/src/entities/platform/wasm/mls/mod.rs +++ b/keystore/src/entities/platform/wasm/mls/mod.rs @@ -17,10 +17,8 @@ pub mod credential; pub mod e2ei_acme_ca; pub mod encryption_keypair; -pub mod epoch_encryption_keypair; pub mod group; pub mod hpke_private_key; -pub mod keypackage; pub mod pending_message; pub mod psk_bundle; pub mod refresh_token;