Skip to content

Commit 98a6e36

Browse files
committed
implement zeroizing of secrets after use
1 parent 063ab19 commit 98a6e36

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/matrix-sdk-sqlite/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ thiserror.workspace = true
3838
tokio = { workspace = true, features = ["fs"] }
3939
tracing.workspace = true
4040
vodozemac.workspace = true
41+
zeroize.workspace = true
4142

4243
[dev-dependencies]
4344
assert_matches.workspace = true

crates/matrix-sdk-sqlite/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::{
3030
};
3131

3232
use deadpool_sqlite::PoolConfig;
33+
use zeroize::Zeroize;
3334

3435
#[cfg(feature = "crypto-store")]
3536
pub use self::crypto_store::SqliteCryptoStore;
@@ -42,9 +43,11 @@ pub use self::state_store::{SqliteStateStore, DATABASE_NAME as STATE_STORE_DATAB
4243
#[cfg(test)]
4344
matrix_sdk_test::init_tracing_for_tests!();
4445

45-
#[derive(Clone, Debug, Eq, PartialEq)]
46+
#[derive(Clone, Debug, Eq, PartialEq, Zeroize)]
4647
pub enum Secret {
48+
#[zeroize]
4749
Key([u8; 32]),
50+
#[zeroize]
4851
PassPhrase(String),
4952
}
5053

crates/matrix-sdk-sqlite/src/utils.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use ruma::{serde::Raw, time::SystemTime, OwnedEventId, OwnedRoomId};
2828
use rusqlite::{limits::Limit, OptionalExtension, Params, Row, Statement, Transaction};
2929
use serde::{de::DeserializeOwned, Serialize};
3030
use tracing::{error, warn};
31+
use zeroize::Zeroize;
3132

3233
use crate::{
3334
error::{Error, Result},
@@ -457,31 +458,31 @@ pub(crate) trait SqliteKeyValueStoreAsyncConnExt: SqliteAsyncConnExt {
457458
/// Get the [`StoreCipher`] of the database or create it.
458459
async fn get_or_create_store_cipher(
459460
&self,
460-
secret: Secret,
461+
mut secret: Secret,
461462
) -> Result<StoreCipher, OpenStoreError> {
462463
let encrypted_cipher = self.get_kv("cipher").await.map_err(OpenStoreError::LoadCipher)?;
463464

464465
let cipher = if let Some(encrypted) = encrypted_cipher {
465466
match secret {
466-
Secret::PassPhrase(passphrase) => StoreCipher::import(passphrase, &encrypted)?,
467-
Secret::Key(key) => StoreCipher::import_with_key(key, &encrypted)?,
467+
Secret::PassPhrase(ref passphrase) => StoreCipher::import(&passphrase, &encrypted)?,
468+
Secret::Key(key) => StoreCipher::import_with_key(&key, &encrypted)?,
468469
}
469470
} else {
470471
let cipher = StoreCipher::new()?;
471472
let export = match secret {
472-
Secret::PassPhrase(passphrase) => {
473+
Secret::PassPhrase(ref passphrase) => {
473474
if cfg!(not(test)) {
474475
cipher.export(passphrase)
475476
} else {
476477
cipher._insecure_export_fast_for_testing(passphrase)
477478
}
478479
}
479-
Secret::Key(key) => cipher.export_with_key(key),
480+
Secret::Key(key) => cipher.export_with_key(&key),
480481
};
481482
self.set_kv("cipher", export?).await.map_err(OpenStoreError::SaveCipher)?;
482483
cipher
483484
};
484-
485+
secret.zeroize();
485486
Ok(cipher)
486487
}
487488
}

0 commit comments

Comments
 (0)