Skip to content

Commit 11554ef

Browse files
Implement Zeroize on various types
1 parent b9deb66 commit 11554ef

7 files changed

Lines changed: 23 additions & 17 deletions

File tree

client/src/dbus/api/secret.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
77
use super::Session;
88
use crate::{Key, Secret, crypto, dbus::Error, secret::ContentType};
99

10-
#[derive(Debug, Serialize, Deserialize, Type)]
10+
#[derive(Debug, Serialize, Deserialize, Type, Zeroize, ZeroizeOnDrop)]
1111
#[zvariant(signature = "(oayays)")]
1212
/// Same as [`DBusSecret`] without tying the session path to a [`Session`] type.
1313
pub struct DBusSecretInner(
14-
pub OwnedObjectPath,
14+
#[zeroize(skip)] pub OwnedObjectPath,
1515
#[serde(with = "serde_bytes")] pub Vec<u8>,
1616
#[serde(with = "serde_bytes")] pub Vec<u8>,
17-
pub ContentType,
17+
#[zeroize(skip)] pub ContentType,
1818
);
1919

2020
#[derive(Debug, Type, Zeroize, ZeroizeOnDrop)]
@@ -58,9 +58,9 @@ impl DBusSecret {
5858

5959
pub async fn from_inner(cnx: &zbus::Connection, inner: DBusSecretInner) -> Result<Self, Error> {
6060
Ok(Self {
61-
session: Arc::new(Session::new(cnx, inner.0).await?),
62-
parameters: inner.1,
63-
value: inner.2,
61+
session: Arc::new(Session::new(cnx, inner.0.clone()).await?),
62+
parameters: inner.1.clone(),
63+
value: inner.2.clone(),
6464
content_type: inner.3,
6565
})
6666
}

client/src/file/api/encrypted_item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use std::collections::HashMap;
22

33
use serde::{Deserialize, Serialize};
44
use zbus::zvariant::Type;
5+
use zeroize::{Zeroize, ZeroizeOnDrop};
56

67
use super::{Error, UnlockedItem};
78
use crate::{Key, Mac, crypto};
89

9-
#[derive(Deserialize, Serialize, Type, Debug, Clone)]
10+
#[derive(Deserialize, Serialize, Type, Debug, Clone, Zeroize, ZeroizeOnDrop)]
1011
pub(crate) struct EncryptedItem {
12+
#[zeroize(skip)]
1113
pub(crate) hashed_attributes: HashMap<String, Mac>,
1214
#[serde(with = "serde_bytes")]
1315
pub(crate) blob: Vec<u8>,

client/src/file/api/legacy_keyring.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
use endi::{Endian, ReadBytes};
9+
use zeroize::{Zeroize, ZeroizeOnDrop};
910

1011
use super::{Secret, UnlockedItem};
1112
use crate::{
@@ -19,7 +20,7 @@ const FILE_HEADER_LEN: usize = FILE_HEADER.len();
1920
pub const MAJOR_VERSION: u8 = 0;
2021
pub const MINOR_VERSION: u8 = 0;
2122

22-
#[derive(Debug)]
23+
#[derive(Debug, Zeroize, ZeroizeOnDrop)]
2324
pub struct Keyring {
2425
salt: Vec<u8>,
2526
iteration_count: u32,

client/src/file/api/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod legacy_keyring;
4444

4545
pub(super) use encrypted_item::EncryptedItem;
4646
pub(super) use legacy_keyring::{Keyring as LegacyKeyring, MAJOR_VERSION as LEGACY_MAJOR_VERSION};
47+
use zeroize::{Zeroize, ZeroizeOnDrop};
4748

4849
use crate::{
4950
AsAttributes, Key, Secret, crypto,
@@ -67,14 +68,15 @@ pub(crate) static GVARIANT_ENCODING: LazyLock<Context> =
6768
LazyLock::new(|| Context::new_gvariant(Endian::Little, 0));
6869

6970
/// Logical contents of a keyring file
70-
#[derive(Deserialize, Serialize, Type, Debug)]
71+
#[derive(Deserialize, Serialize, Type, Debug, Zeroize, ZeroizeOnDrop)]
7172
pub struct Keyring {
7273
salt_size: u32,
7374
#[serde(with = "serde_bytes")]
7475
salt: Vec<u8>,
7576
iteration_count: u32,
7677
modified_time: u64,
7778
usage_count: u32,
79+
#[zeroize(skip)]
7880
pub(in crate::file) items: Vec<EncryptedItem>,
7981
}
8082

client/src/mac.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use serde::{Deserialize, Serialize};
22
#[cfg(feature = "native_crypto")]
33
use subtle::ConstantTimeEq;
44
use zbus::zvariant::Type;
5+
use zeroize::{Zeroize, ZeroizeOnDrop};
56

67
// There is no constructor to avoid performing sanity checks, e.g. length.
78
/// A message authentication code. It provides constant-time comparison when
89
/// compared against another mac or against a slice of bytes.
9-
#[derive(Deserialize, Serialize, Type, Clone)]
10+
#[derive(Deserialize, Serialize, Type, Clone, Zeroize, ZeroizeOnDrop)]
1011
pub struct Mac(#[serde(with = "serde_bytes")] Vec<u8>);
1112

1213
impl std::fmt::Debug for Mac {

server/src/collection/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,22 @@ impl Collection {
225225
let keyring = self.keyring.read().await;
226226
let keyring = keyring.as_ref().unwrap().as_unlocked();
227227

228-
let DBusSecretInner(session_path, iv, secret_bytes, content_type) = secret;
228+
let DBusSecretInner(ref session_path, ref iv, ref secret_bytes, ref content_type) = secret;
229229
let label = properties.label();
230230
// Safe to unwrap as an item always has attributes
231231
let mut attributes = properties.attributes().unwrap().to_owned();
232232

233-
let Some(session) = self.service.session(&session_path).await else {
233+
let Some(session) = self.service.session(session_path).await else {
234234
tracing::error!("The session `{}` does not exist.", session_path);
235235
return Err(ServiceError::NoSession(format!(
236236
"The session `{session_path}` does not exist."
237237
)));
238238
};
239239

240240
let secret = match session.aes_key() {
241-
Some(key) => oo7::crypto::decrypt(secret_bytes, &key, &iv)
241+
Some(key) => oo7::crypto::decrypt(secret_bytes, &key, iv)
242242
.map_err(|err| custom_service_error(&format!("Failed to decrypt secret {err}.")))?,
243-
None => zeroize::Zeroizing::new(secret_bytes),
243+
None => zeroize::Zeroizing::new(secret_bytes.clone()),
244244
};
245245

246246
// Ensure content-type attribute is stored

server/src/item/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ impl Item {
139139
}
140140

141141
pub async fn set_secret(&self, secret: DBusSecretInner) -> Result<(), ServiceError> {
142-
let DBusSecretInner(session, iv, secret, content_type) = secret;
142+
let DBusSecretInner(ref session, ref iv, ref secret, ref content_type) = secret;
143143

144-
let Some(session) = self.service.session(&session).await else {
144+
let Some(session) = self.service.session(session).await else {
145145
tracing::error!("The session `{}` does not exist.", session);
146146
return Err(ServiceError::NoSession(format!(
147147
"The session `{session}` does not exist."
@@ -162,7 +162,7 @@ impl Item {
162162

163163
match session.aes_key() {
164164
Some(key) => {
165-
let decrypted = oo7::crypto::decrypt(secret, &key, &iv).map_err(|err| {
165+
let decrypted = oo7::crypto::decrypt(secret, &key, iv).map_err(|err| {
166166
custom_service_error(&format!("Failed to decrypt secret {err}."))
167167
})?;
168168
inner.as_mut_unlocked().set_secret(decrypted);

0 commit comments

Comments
 (0)