Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions client/src/crypto/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ pub(crate) fn verify_checksum_md5(digest: impl AsRef<[u8]>, content: impl AsRef<
.into()
}

/// The lowercase hex encoded MD5 digest of `value`, as stored in the
/// cleartext attribute section of legacy keyring files.
pub fn md5_hex(value: &str) -> String {
let mut hasher = Md5::new();
hasher.update(value.as_bytes());
hasher
.finalize_fixed()
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}

pub(crate) fn derive_key(
secret: impl AsRef<[u8]>,
key_strength: Result<(), file::WeakKeyError>,
Expand Down
10 changes: 10 additions & 0 deletions client/src/crypto/openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ pub(crate) fn verify_checksum_md5(digest: impl AsRef<[u8]>, content: impl AsRef<
)
}

/// The lowercase hex encoded MD5 digest of `value`, as stored in the
/// cleartext attribute section of legacy keyring files.
pub fn md5_hex(value: &str) -> String {
hash(MessageDigest::md5(), value.as_bytes())
.unwrap()
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}

pub(crate) fn derive_key(
secret: impl AsRef<[u8]>,
key_strength: Result<(), file::WeakKeyError>,
Expand Down
98 changes: 79 additions & 19 deletions client/src/file/api/legacy_keyring/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ use std::{
use endi::{Endian, ReadBytes};
use zeroize::{Zeroize, ZeroizeOnDrop};

#[cfg(any(feature = "unstable", test))]
use super::super::EncryptedItem;
use super::{FILE_HEADER, FILE_HEADER_LEN, item_type_schema};
#[cfg(any(feature = "unstable", test))]
use crate::{
Mac,
file::{Item, LockedItem},
};
use crate::{
Secret, XDG_SCHEMA_ATTRIBUTE, crypto,
file::{Error, UnlockedItem},
Expand All @@ -17,13 +24,22 @@ use crate::{
pub const MAJOR_VERSION: u8 = 0;
pub const MINOR_VERSION: u8 = 0;

/// An item of the cleartext attribute section of the keyring file.
#[derive(Debug)]
struct HashedItem {
item_type: u32,
#[cfg_attr(not(feature = "unstable"), allow(dead_code))]
attributes: HashMap<String, String>,
}

#[derive(Debug, Zeroize, ZeroizeOnDrop)]
pub struct Keyring {
salt: Vec<u8>,
iteration_count: u32,
encrypted_content: Vec<u8>,
// In the same order as the encrypted items
item_types: Vec<u32>,
#[zeroize(skip)]
hashed_items: Vec<HashedItem>,
}

impl Keyring {
Expand Down Expand Up @@ -86,10 +102,36 @@ impl Keyring {
Ok(result)
}

/// The items of the keyring, locked.
///
/// The items have no label nor secret until the keyring is migrated
/// and unlocked; they only carry the cleartext attributes of the
/// keyring file.
#[cfg(any(feature = "unstable", test))]
pub fn items(&self) -> Vec<Item> {
self.hashed_items
.iter()
.map(|item| {
Item::Locked(LockedItem {
inner: EncryptedItem {
hashed_attributes: item
.attributes
.iter()
.map(|(key, value)| (key.clone(), Mac::new(value.as_bytes().to_vec())))
.collect(),
// The secret is only available once decrypted
blob: Vec::new(),
},
})
})
.collect()
}

fn read_items(self, decrypted: &[u8]) -> Result<Vec<UnlockedItem>, Error> {
let mut cursor = Cursor::new(decrypted);
let mut items = Vec::with_capacity(self.item_types.len());
for &item_type in &self.item_types {
let mut items = Vec::with_capacity(self.hashed_items.len());
for hashed_item in &self.hashed_items {
let item_type = hashed_item.item_type;
let display_name = Self::read_string(&mut cursor)?.unwrap_or_else(|| {
#[cfg(feature = "tracing")]
tracing::warn!("Item has no label, defaulting to empty");
Expand Down Expand Up @@ -150,32 +192,50 @@ impl Keyring {
Ok((hi << 32) | lo)
}

fn read_hashed_item_types(cursor: &mut Cursor<&[u8]>, count: usize) -> Result<Vec<u32>, Error> {
let mut item_types = Vec::new();
fn read_hashed_items(
cursor: &mut Cursor<&[u8]>,
count: usize,
) -> Result<Vec<HashedItem>, Error> {
let mut items = Vec::with_capacity(count);
for _ in 0..count {
let _id = cursor.read_u32(Endian::Big)?;
item_types.push(cursor.read_u32(Endian::Big)?);
let num_attributes = cursor.read_u32(Endian::Big)?;
let item_type = cursor.read_u32(Endian::Big)?;
let num_attributes = cursor.read_u32(Endian::Big)? as usize;
let mut attributes = HashMap::with_capacity(num_attributes);
for _ in 0..num_attributes {
let _name = Self::read_string(cursor)?;
match cursor.read_u32(Endian::Big)? {
0 => {
let _value = Self::read_string(cursor);
}
1 => {
let _value = cursor.read_u32(Endian::Big);
}
let name = Self::read_string(cursor)?.ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "empty attribute name")
})?;
let value = match cursor.read_u32(Endian::Big)? {
0 => Self::read_string(cursor)?
.ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "empty attribute value")
})?
.to_string(),
1 => cursor.read_u32(Endian::Big)?.to_string(),
_ => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"unknown attribute type",
)
.into());
}
}
};
attributes.insert(name.to_string(), value);
}
// Mirror the schema synthesis applied to the decrypted items,
// so schema based searches match the migrated items
if !attributes.contains_key(XDG_SCHEMA_ATTRIBUTE)
&& let Some(schema) = item_type_schema(item_type)
{
attributes.insert(XDG_SCHEMA_ATTRIBUTE.to_owned(), crypto::md5_hex(schema));
}
items.push(HashedItem {
item_type,
attributes,
});
}
Ok(item_types)
Ok(items)
}

fn skip_acls(cursor: &mut Cursor<&[u8]>, count: usize) -> Result<(), Error> {
Expand Down Expand Up @@ -211,7 +271,7 @@ impl Keyring {
let _ = cursor.read_u32(Endian::Big)?;
}
let item_count = cursor.read_u32(Endian::Big)? as usize;
let item_types = Self::read_hashed_item_types(&mut cursor, item_count)?;
let hashed_items = Self::read_hashed_items(&mut cursor, item_count)?;
let mut size = cursor.read_u32(Endian::Big)? as usize;
let pos = cursor.position() as usize;
if size > cursor.get_ref()[pos..].len() {
Expand All @@ -226,7 +286,7 @@ impl Keyring {
salt,
iteration_count,
encrypted_content,
item_types,
hashed_items,
})
}
}
Expand Down
41 changes: 41 additions & 0 deletions client/src/file/api/legacy_keyring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod plain;

pub use encrypted::MAJOR_VERSION;

#[cfg(any(feature = "unstable", test))]
use crate::file::Item;
use crate::{
Secret,
file::{Error, UnlockedItem},
Expand Down Expand Up @@ -64,6 +66,20 @@ impl Keyring {
Self::Plain(_) => Ok(true),
}
}

/// The items of the keyring.
///
/// Items of an encrypted keyring are locked: they carry no label nor
/// secret until the keyring is migrated and unlocked, only the
/// cleartext attributes of the keyring file. Items of a plain
/// keyring are unlocked and carry their data.
#[cfg(any(feature = "unstable", test))]
pub fn items(&self) -> Vec<Item> {
match self {
Self::Encrypted(keyring) => keyring.items(),
Self::Plain(keyring) => keyring.items(),
}
}
}

impl TryFrom<&[u8]> for Keyring {
Expand Down Expand Up @@ -107,6 +123,20 @@ mod tests {
.expect("validation should not fail")
);

// The cleartext attributes are searchable without the secret,
// values are stored as their MD5 digests
let items = keyring.items();
assert_eq!(items.len(), 1);
assert!(items[0].is_locked());
assert!(items[0].matches_attributes(
&[(
crate::XDG_SCHEMA_ATTRIBUTE,
crate::crypto::md5_hex("org.gnome.keyring.Note")
)],
None
));
assert!(!items[0].matches_attributes(&[("unique", "nope")], None));

let items = keyring
.decrypt_items(&Secret::from("test"))
.expect("decryption should not fail");
Expand All @@ -125,5 +155,16 @@ mod tests {
.validate_secret(&Secret::from("anything"))
.expect("validation should not fail")
);

// The items are unlocked and carry their data
let items = keyring.items();
assert_eq!(items.len(), 3);
assert!(items.iter().all(|item| !item.is_locked()));
assert!(items.iter().any(|item| {
item.matches_attributes(
&[(crate::XDG_SCHEMA_ATTRIBUTE, "org.remmina.Password")],
None,
)
}));
}
}
13 changes: 13 additions & 0 deletions client/src/file/api/legacy_keyring/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::{collections::HashMap, io};
use zeroize::Zeroizing;

use super::{FILE_HEADER_LEN, item_type_schema};
#[cfg(any(feature = "unstable", test))]
use crate::file::Item;
use crate::{
Secret, XDG_SCHEMA_ATTRIBUTE,
file::{Error, UnlockedItem},
Expand All @@ -43,6 +45,17 @@ impl Keyring {
self.items
}

/// The items of this keyring, unlocked.
///
/// As the keyring is not encrypted, the items carry their data.
#[cfg(any(feature = "unstable", test))]
pub fn items(&self) -> Vec<Item> {
self.items
.iter()
.map(|item| Item::Unlocked(item.clone()))
.collect()
}

fn read_item(id: &str, group: &Group, attribute_groups: &[&Group]) -> UnlockedItem {
let label = group.string("display-name").unwrap_or_else(|| {
#[cfg(feature = "tracing")]
Expand Down
50 changes: 45 additions & 5 deletions server/src/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,25 @@ impl Collection {
return Ok(Vec::new());
};
if keyring.is_locked() {
return Ok(Vec::new());
drop(keyring_guard);

// The placeholder items of a pending migration store their
// attribute values as MD5 digests; hash the searched values
// to match them. The generic schema fallback is compared
// as-is.
let hashed: HashMap<String, String> = attributes
.iter()
.map(|(key, value)| {
if key.as_str() == oo7::XDG_SCHEMA_ATTRIBUTE
&& value.as_str() == oo7::GENERIC_SCHEMA
{
(key.clone(), value.clone())
} else {
(key.clone(), oo7::crypto::md5_hex(value))
}
})
.collect();
return self.search_items_with_key(&hashed, None, false).await;
}
let key = keyring
.as_unlocked()
Expand Down Expand Up @@ -667,16 +685,25 @@ impl Collection {
}

pub async fn dispatch_items(&self) -> Result<(), Error> {
let keyring_guard = self.keyring.read().await;
let keyring = keyring_guard.as_ref().unwrap();
let keyring_items = {
let keyring_guard = self.keyring.read().await;
let keyring = keyring_guard.as_ref().unwrap();
keyring.items().await?
};
self.dispatch_file_items(keyring_items).await
}

let keyring_items = keyring.items().await?;
/// Register the given file items on the object server and notify
/// clients.
pub async fn dispatch_file_items(
&self,
keyring_items: Vec<oo7::file::Item>,
) -> Result<(), Error> {
tracing::debug!(
"Dispatching {} items for collection `{}`.",
keyring_items.len(),
self.path
);
drop(keyring_guard);

let mut dispatched_items = Vec::new();
{
Expand Down Expand Up @@ -713,11 +740,24 @@ impl Collection {
Ok(())
}

/// Remove all items from the object server and the collection.
async fn clear_items(&self) -> Result<(), Error> {
let mut items = self.items.lock().await;
let object_server = self.service.object_server();
for item in items.iter() {
object_server.remove::<item::Item, _>(item.path()).await?;
}
items.clear();
Ok(())
}

/// Replace the keyring with a just-migrated one, dispatch its items
/// and emit the corresponding change signals.
pub async fn apply_migrated_keyring(&self, keyring: Keyring) -> Result<(), Error> {
*self.keyring.write().await = Some(keyring);

// Drop the placeholder items, the migrated items take their paths
self.clear_items().await?;
self.dispatch_items().await?;

// The keyring swap bypasses set_locked, so emit Locked here.
Expand Down
6 changes: 5 additions & 1 deletion server/src/gnome/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ mod tests {
let collection =
oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?;
assert!(collection.is_locked().await?);
assert!(collection.items().await?.is_empty());
// The placeholder only exposes the locked stand-in item
assert_eq!(collection.items().await?.len(), 1);
assert!(collection.items().await?[0].is_locked().await?);

let dbus_secret = setup.create_dbus_secret("test")?;
internal_proxy
Expand All @@ -700,6 +702,8 @@ mod tests {

assert!(!collection.is_locked().await?);
assert_eq!(collection.items().await?.len(), 1);
assert!(!collection.items().await?[0].is_locked().await?);
assert_eq!(collection.items().await?[0].label().await?, "foo");
assert_eq!(setup.server.pending_migrations.lock().await.len(), 0);

Ok(())
Expand Down
Loading
Loading