core_crypto_keystore/entities/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// 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/.

pub(crate) mod general;
pub(crate) mod mls;

pub use self::general::*;
pub use self::mls::*;

cfg_if::cfg_if! {
    if #[cfg(feature = "proteus-keystore")] {
        pub(crate) mod proteus;
        pub use self::proteus::*;
    }
}

mod platform {
    cfg_if::cfg_if! {
        if #[cfg(target_family = "wasm")] {
            mod wasm;
            pub use self::wasm::*;
        } else {
            mod generic;
            pub use self::generic::*;
        }
    }
}

pub use self::platform::*;

use crate::connection::DatabaseConnection;
#[cfg(not(target_family = "wasm"))]
use crate::sha256;
use crate::{CryptoKeystoreError, CryptoKeystoreResult, MissingKeyErrorKind};
#[cfg(target_family = "wasm")]
use aes_gcm::Aes256Gcm;

#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(
    any(target_family = "wasm", feature = "serde"),
    derive(serde::Serialize, serde::Deserialize)
)]
#[repr(transparent)]
pub struct StringEntityId<'a>(&'a [u8]);

impl<'a> StringEntityId<'a> {
    pub fn new(bytes: &'a [u8]) -> Self {
        Self(bytes)
    }

    pub fn as_hex_string(&self) -> String {
        hex::encode(self.0)
    }

    #[cfg(not(target_family = "wasm"))]
    pub(crate) fn sha256(&self) -> String {
        sha256(self.0)
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.0.into()
    }

    pub fn as_slice(&self) -> &[u8] {
        self.0
    }

    pub fn try_as_str(&self) -> Result<&str, ::core::str::Utf8Error> {
        std::str::from_utf8(self.0)
    }
}

impl TryInto<String> for &StringEntityId<'_> {
    type Error = CryptoKeystoreError;

    fn try_into(self) -> CryptoKeystoreResult<String> {
        Ok(String::from_utf8(self.0.into())?)
    }
}

impl std::fmt::Display for StringEntityId<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_hex_string())
    }
}

impl<'a> From<&'a [u8]> for StringEntityId<'a> {
    fn from(bytes: &'a [u8]) -> Self {
        Self::new(bytes)
    }
}

#[derive(Debug, Clone, Default)]
pub struct EntityFindParams {
    pub limit: Option<u32>,
    pub offset: Option<u32>,
    pub reverse: bool,
}

#[cfg(not(target_family = "wasm"))]
impl EntityFindParams {
    pub fn to_sql(&self) -> String {
        use std::fmt::Write as _;
        let mut query: String = "".into();
        if let Some(offset) = self.offset {
            let _ = write!(query, " OFFSET {offset}");
        }
        let _ = write!(query, " ORDER BY rowid");
        if self.reverse {
            let _ = write!(query, " DESC");
        }
        if let Some(limit) = self.limit {
            let _ = write!(query, " LIMIT {limit}");
        }

        query
    }
}

#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
pub trait EntityBase: Send + Sized + Clone + PartialEq + Eq + std::fmt::Debug {
    type ConnectionType: DatabaseConnection;
    type AutoGeneratedFields: Default;
    /// Beware: if you change the value of this constant on any WASM entity, you'll need to do a data migration
    ///     not only because it is used as reference to the object store names but also for the value of the aad.
    const COLLECTION_NAME: &'static str;

    fn to_missing_key_err_kind() -> MissingKeyErrorKind;

    fn downcast<T: EntityBase>(&self) -> Option<&T> {
        if T::COLLECTION_NAME == Self::COLLECTION_NAME {
            // SAFETY: The above check ensures that this transmutation is safe.
            Some(unsafe { std::mem::transmute::<&Self, &T>(self) })
        } else {
            None
        }
    }

    fn to_transaction_entity(self) -> crate::transaction::dynamic_dispatch::Entity;
}

cfg_if::cfg_if! {
    if #[cfg(target_family = "wasm")] {
        const AES_GCM_256_NONCE_SIZE: usize = 12;

        #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
        struct Aad {
            type_name: Vec<u8>,
            id: Vec<u8>,
        }

        #[async_trait::async_trait(?Send)]
        pub trait EntityTransactionExt: Entity<ConnectionType = crate::connection::KeystoreDatabaseConnection> {
            async fn save<'a>(&'a self, tx: &crate::connection::storage::WasmStorageTransaction<'a>) -> CryptoKeystoreResult<()> {
                tx.save(self.clone()).await
            }
            async fn pre_save<'a>(&'a mut self) -> CryptoKeystoreResult<Self::AutoGeneratedFields> {
                Ok(Default::default())
            }
            async fn delete_fail_on_missing_id<'a>(tx: &crate::connection::storage::WasmStorageTransaction<'a>, id: StringEntityId<'a>) -> CryptoKeystoreResult<()> {
                tx.delete(Self::COLLECTION_NAME, id.as_slice()).await
            }

            async fn delete<'a>(tx: &crate::connection::storage::WasmStorageTransaction<'a>, id: StringEntityId<'a>) -> CryptoKeystoreResult<()> {
                match Self::delete_fail_on_missing_id(tx, id).await{
                    Ok(_) => Ok(()),
                    Err(CryptoKeystoreError::IdbError(idb::Error::DeleteFailed(_))) => Ok(()),
                    Err(e) => Err(e),
                }
            }
        }

        #[async_trait::async_trait(?Send)]
        pub trait Entity: EntityBase + serde::Serialize + serde::de::DeserializeOwned {
            fn id(&self) -> CryptoKeystoreResult<wasm_bindgen::JsValue> {
                Ok(js_sys::Uint8Array::from(self.id_raw()).into())
            }

            fn id_raw(&self) -> &[u8];

            /// The query results that are obtained during a transaction
            /// from the transaction cache and the database are merged by this key.
            fn merge_key(&self) -> Vec<u8> {
                self.id_raw().into()
            }

            fn aad(&self) -> CryptoKeystoreResult<Vec<u8>> {
                let aad = Aad {
                    type_name: Self::COLLECTION_NAME.as_bytes().to_vec(),
                    id: self.id_raw().into(),
                };
                serde_json::to_vec(&aad).map_err(Into::into)
            }

            async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult<Vec<Self>>;
            async fn find_one(conn: &mut Self::ConnectionType, id: &StringEntityId) -> CryptoKeystoreResult<Option<Self>>;
            async fn find_many(conn: &mut Self::ConnectionType, ids: &[StringEntityId]) -> CryptoKeystoreResult<Vec<Self>> {
                // Default, inefficient & naive method
                let mut ret = Vec::with_capacity(ids.len());
                for id in ids {
                    if let Some(entity) = Self::find_one(conn, id).await? {
                        ret.push(entity);
                    }
                }
                Ok(ret)
            }
            async fn count(conn: &mut Self::ConnectionType) -> CryptoKeystoreResult<usize>;

            // About WASM Encryption:
            // The store key (i.e. passphrase) is hashed using SHA256 to obtain 32 bytes
            // The AES256-GCM cipher is then initialized and is used to encrypt individual values
            // Entities shall decide which fields need to be encrypted
            // Internal layout:
            // - Cleartext: [u8] bytes
            // - Ciphertext: [12 bytes of nonce..., ...encrypted data]
            fn encrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()>;
            fn encrypt_with_nonce_and_aad(cipher: &aes_gcm::Aes256Gcm, data: &[u8], nonce: &[u8], aad: &[u8]) -> CryptoKeystoreResult<Vec<u8>> {
                use aes_gcm::aead::Aead as _;
                let nonce = aes_gcm::Nonce::from_slice(nonce);
                let msg = data;
                let payload = aes_gcm::aead::Payload {
                    msg,
                    aad,
                };

                let mut encrypted = cipher.encrypt(nonce, payload).map_err(|_| CryptoKeystoreError::AesGcmError)?;
                let mut message = Vec::with_capacity(nonce.len() + encrypted.len());
                message.extend_from_slice(nonce);
                message.append(&mut encrypted);
                Ok(message)
            }

            fn encrypt_data(&self, cipher: &aes_gcm::Aes256Gcm, data: &[u8]) -> CryptoKeystoreResult<Vec<u8>> {
                let nonce_bytes: [u8; AES_GCM_256_NONCE_SIZE] = rand::random();
                Self::encrypt_with_nonce_and_aad(cipher, data, &nonce_bytes, &self.aad()?)
            }

            fn decrypt(&mut self, cipher: &aes_gcm::Aes256Gcm) -> CryptoKeystoreResult<()>;
            fn decrypt_data(&self, cipher: &aes_gcm::Aes256Gcm, data: &[u8]) -> CryptoKeystoreResult<Vec<u8>> {
                use aes_gcm::aead::Aead as _;

                if data.is_empty() {
                    return Err(CryptoKeystoreError::MissingKeyInStore(Self::to_missing_key_err_kind()));
                }
                if data.len() < AES_GCM_256_NONCE_SIZE {
                    return Err(CryptoKeystoreError::AesGcmError);
                }

                let nonce_bytes = &data[..AES_GCM_256_NONCE_SIZE];
                let nonce = aes_gcm::Nonce::from_slice(nonce_bytes);
                let msg = &data[AES_GCM_256_NONCE_SIZE..];
                let aad = &self.aad()?;
                let payload = aes_gcm::aead::Payload {
                    msg,
                    aad,
                };
                let cleartext = cipher.decrypt(nonce, payload).map_err(|_| CryptoKeystoreError::AesGcmError)?;
                Ok(cleartext)
            }
        }

        #[async_trait::async_trait(?Send)]
        impl<T: UniqueEntity + serde::Serialize + serde::de::DeserializeOwned> Entity for T {
            fn id_raw(&self) -> &[u8] {
                &Self::ID
            }

            async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult<Vec<Self>> {
                    <Self as UniqueEntity>::find_all(conn, params).await
                }

            async fn find_one(conn: &mut Self::ConnectionType, _id: &StringEntityId) -> CryptoKeystoreResult<Option<Self>> {
                    <Self as UniqueEntity>::find_one(conn).await
                }

            async fn count(conn: &mut Self::ConnectionType) -> CryptoKeystoreResult<usize> {
                    <Self as UniqueEntity>::count(conn).await
                }

            fn encrypt(&mut self, cipher: &Aes256Gcm) -> CryptoKeystoreResult<()> {
                self.set_content(self.encrypt_data(cipher, self.content())?);
                Self::ConnectionType::check_buffer_size(self.content().len())?;
                Ok(())
            }

            fn decrypt(&mut self, cipher: &Aes256Gcm) -> CryptoKeystoreResult<()> {
                self.set_content(self.decrypt_data(cipher, self.content())?);
                Self::ConnectionType::check_buffer_size(self.content().len())?;
                Ok(())
            }
        }
    } else {
        #[async_trait::async_trait]
        pub trait EntityTransactionExt: Entity {
            async fn save(&self, tx: &crate::connection::TransactionWrapper<'_>) -> CryptoKeystoreResult<()>;
            async fn pre_save<'a>(&'a mut self) -> CryptoKeystoreResult<Self::AutoGeneratedFields> {
                Ok(Default::default())
            }
            async fn delete_fail_on_missing_id(
                tx: &crate::connection::TransactionWrapper<'_>,
                id: StringEntityId<'_>,
            ) -> CryptoKeystoreResult<()>;

            async fn delete(
                tx: &crate::connection::TransactionWrapper<'_>,
                id: StringEntityId<'_>,
            ) -> CryptoKeystoreResult<()> {
                match Self::delete_fail_on_missing_id(tx, id).await{
                    Ok(_) => Ok(()),
                    Err(CryptoKeystoreError::MissingKeyInStore(_)) => Ok(()),
                    Err(e) => Err(e),
                }
            }
        }

        #[async_trait::async_trait]
        pub trait Entity: EntityBase {
            fn id_raw(&self) -> &[u8];

            /// The query results that are obtained during a transaction
            /// from the transaction cache and the database are merged by this key.
            fn merge_key(&self) -> Vec<u8> {
                self.id_raw().into()
            }

            async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult<Vec<Self>>;
            async fn find_one(conn: &mut Self::ConnectionType, id: &StringEntityId) -> CryptoKeystoreResult<Option<Self>>;
            async fn find_many(conn: &mut Self::ConnectionType, ids: &[StringEntityId]) -> CryptoKeystoreResult<Vec<Self>> {
                // Default, inefficient & naive method
                let mut ret = Vec::with_capacity(ids.len());
                for id in ids {
                    if let Some(entity) = Self::find_one(conn, id).await? {
                        ret.push(entity);
                    }
                }
                Ok(ret)
            }
            async fn count(conn: &mut Self::ConnectionType) -> CryptoKeystoreResult<usize>;
        }

        #[async_trait::async_trait]
        impl<T: UniqueEntity> Entity for T {
            fn id_raw(&self) -> &[u8] {
                &[Self::ID as u8]
            }

            async fn find_all(conn: &mut Self::ConnectionType, params: EntityFindParams) -> CryptoKeystoreResult<Vec<Self>> {
                <Self as UniqueEntity>::find_all(conn, params).await
            }

            async fn find_one(conn: &mut Self::ConnectionType, _id: &StringEntityId) -> CryptoKeystoreResult<Option<Self>> {
                <Self as UniqueEntity>::find_one(conn).await
            }

            async fn count(conn: &mut Self::ConnectionType) -> CryptoKeystoreResult<usize> {
               <Self as UniqueEntity>::count(conn).await
            }
        }

        pub trait EntityIdStringExt: Entity {
            fn id_hex(&self) -> String {
                hex::encode(self.id_raw())
            }

            fn id_sha256(&self) -> String {
                sha256(self.id_raw())
            }

            fn id_from_hex(id_hex: &str) -> CryptoKeystoreResult<Vec<u8>> {
                hex::decode(id_hex).map_err(Into::into)
            }
        }

        impl<T: Entity> EntityIdStringExt for T {}
    }
}