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
21 changes: 21 additions & 0 deletions client/src/file/api/encrypted_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ impl EncryptedItem {
let n_iv = crypto::iv_len();

// The encrypted data, the iv, and the mac are concatenated into blob.
if n < n_mac + n_iv {
return Err(Error::NoData);
}
let (encrypted_data_with_iv, mac_tag) = &self.blob.split_at(n - n_mac);

// verify item
Expand Down Expand Up @@ -132,3 +135,21 @@ impl EncryptedItem {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn decrypt_short_blob_is_an_error() {
// The placeholder items of a pending migration carry no blob
let item = EncryptedItem {
hashed_attributes: HashMap::new(),
blob: Vec::new(),
};
let key = Key::new(vec![
132, 3, 113, 222, 81, 209, 49, 43, 81, 232, 243, 46, 1, 103, 184, 42,
]);
assert!(matches!(item.decrypt(Some(&key)), Err(Error::NoData)));
}
}
13 changes: 10 additions & 3 deletions server/src/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ impl Collection {
let prompt_path = OwnedObjectPath::from(prompt.path().clone());

let collection = self.clone();
let service = self.service.clone();
let caller_owned = caller;
let action =
crate::prompt::PromptAction::new(move |unlock_secret: Option<Secret>| async move {
// Unlock the collection
collection.set_locked(false, unlock_secret).await?;
// Unlock the collection, completing a pending migration
service
.unlock_collection(&collection, unlock_secret)
.await?;

collection.delete_unlocked(&caller_owned).await?;

Expand Down Expand Up @@ -201,9 +204,13 @@ impl Collection {
let prompt_path = OwnedObjectPath::from(prompt.path().clone());

let collection = self.clone();
let service = self.service.clone();
let action =
crate::prompt::PromptAction::new(move |unlock_secret: Option<Secret>| async move {
collection.set_locked(false, unlock_secret).await?;
// Unlock the collection, completing a pending migration
service
.unlock_collection(&collection, unlock_secret)
.await?;

let item_path = collection
.create_item_unlocked(properties, secret, replace)
Expand Down
25 changes: 25 additions & 0 deletions server/src/collection/tests/deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,28 @@ async fn delete_locked_collection_with_prompt_gnome() -> Result<(), Box<dyn std:
async fn delete_locked_collection_with_prompt_plasma() -> Result<(), Box<dyn std::error::Error>> {
delete_locked_collection_with_prompt_impl(PrompterType::Plasma).await
}

// https://github.com/linux-credentials/oo7/issues/696
#[tokio::test]
async fn delete_locked_pending_migration() -> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = crate::tests::pending_migration_setup(None).await?;

let collection =
oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?;
assert!(collection.is_locked().await?);

setup
.set_password_queue(vec![oo7::Secret::from("test")])
.await;
collection.delete(None).await?;

assert_eq!(setup.server.pending_migrations.lock().await.len(), 0);
let collections = setup.service_api.collections().await?;
assert!(
collections
.iter()
.all(|c| c.inner().path() != collection.inner().path())
);

Ok(())
}
67 changes: 67 additions & 0 deletions server/src/collection/tests/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,70 @@ async fn concurrent_replace_and_lookup() -> Result<(), Box<dyn std::error::Error

Ok(())
}

// https://github.com/linux-credentials/oo7/issues/696
#[tokio::test]
async fn create_item_in_locked_pending_migration() -> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = crate::tests::pending_migration_setup(None).await?;

let collection =
oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?;
assert!(collection.is_locked().await?);

setup
.set_password_queue(vec![oo7::Secret::from("test")])
.await;
let item = collection
.create_item(
"New Item",
&[("application", "test-app")],
&setup.create_dbus_secret("item-secret")?,
false,
None,
)
.await?;

assert_eq!(setup.server.pending_migrations.lock().await.len(), 0);
assert!(!collection.is_locked().await?);
let items = collection.items().await?;
assert_eq!(items.len(), 2);
assert_eq!(item.label().await?, "New Item");
assert_eq!(item.secret(&setup.session).await?.value(), b"item-secret");
let mut has_legacy_item = false;
for i in &items {
if i.label().await? == "foo" {
has_legacy_item = true;
}
}
assert!(has_legacy_item, "The legacy item should have been migrated");
Ok(())
}

// https://github.com/linux-credentials/oo7/issues/696
#[tokio::test]
async fn delete_item_pending_migration() -> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = crate::tests::pending_migration_setup(None).await?;

let collection =
oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?;

// Delete the placeholder item reported by SearchItems
let (_, locked) = setup
.service_api
.search_items(&[("xdg:schema", "org.gnome.keyring.Note")])
.await?;
assert_eq!(locked.len(), 1);

setup
.set_password_queue(vec![oo7::Secret::from("test")])
.await;
locked[0].delete(None).await?;

// The migration completed and the item was deleted from the migrated
// keyring
assert_eq!(setup.server.pending_migrations.lock().await.len(), 0);
assert!(!collection.is_locked().await?);
assert_eq!(collection.items().await?.len(), 0);

Ok(())
}
98 changes: 64 additions & 34 deletions server/src/gnome/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
use oo7::{Secret, dbus};
use zbus::wire::{ObjectPath, OwnedObjectPath};

use crate::tests::TestServiceSetup;
use crate::tests::{TestServiceSetup, pending_migration_setup};

/// Proxy for the InternalUnsupportedGuiltRiddenInterface
#[zbus::proxy(
Expand Down Expand Up @@ -455,6 +455,66 @@ mod tests {
Ok(())
}

// https://github.com/linux-credentials/oo7/issues/696
#[tokio::test]
async fn test_change_with_prompt_pending_migration() -> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?;
let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn)
.build()
.await?;

let collection =
oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?;
assert!(collection.is_locked().await?);

// Queue: old password first (to unlock and migrate), then the new
// password (to change to)
let new_password = Secret::text("changed-via-prompt");
setup
.set_password_queue(vec![Secret::from("test"), new_password.clone()])
.await;

let prompt_path = internal_proxy
.change_with_prompt(&collection_path.as_ref())
.await?;
let prompt_proxy = dbus::api::Prompt::new(&setup.client_conn, prompt_path)
.await?
.unwrap();
prompt_proxy.receive_completed(None).await?;

// The migration completed and the keyring holds its items
assert_eq!(setup.server.pending_migrations.lock().await.len(), 0);
assert!(!collection.is_locked().await?);
assert_eq!(collection.items().await?.len(), 1);
assert_eq!(collection.items().await?[0].label().await?, "foo");

// The keyring now opens with the new password, not the old one
setup
.service_api
.lock(std::slice::from_ref(&collection_path), None)
.await?;
internal_proxy
.unlock_with_master_password(
&collection_path.as_ref(),
setup.create_dbus_secret(new_password)?.into(),
)
.await?;
assert!(!collection.is_locked().await?);
setup
.service_api
.lock(std::slice::from_ref(&collection_path), None)
.await?;
let result = internal_proxy
.unlock_with_master_password(
&collection_path.as_ref(),
setup.create_dbus_secret("test")?.into(),
)
.await;
assert!(result.is_err(), "The old password should no longer unlock");

Ok(())
}

#[tokio::test]
async fn test_change_with_prompt_locked_collection() -> Result<(), Box<dyn std::error::Error>> {
let setup = TestServiceSetup::plain_session(true).await?;
Expand Down Expand Up @@ -649,41 +709,11 @@ mod tests {
Ok(())
}

/// Set up a service with a legacy v0 keyring awaiting migration
async fn pending_migration_setup()
-> Result<(TestServiceSetup, tempfile::TempDir, OwnedObjectPath), Box<dyn std::error::Error>>
{
let temp_dir = tempfile::tempdir()?;
let keyrings_dir = temp_dir.path().join("keyrings");
tokio::fs::create_dir_all(keyrings_dir.join("v1")).await?;

let fixture_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("client/fixtures/legacy.keyring");
tokio::fs::copy(&fixture_path, keyrings_dir.join("legacy.keyring")).await?;

let setup =
TestServiceSetup::with_disk_keyrings(temp_dir.path().to_path_buf(), None, None).await?;

let collections = setup.service_api.collections().await?;
let mut legacy_path = None;
for collection in &collections {
if collection.label().await? == "Legacy" {
legacy_path = Some(collection.inner().path().to_owned().into());
break;
}
}
let legacy_path = legacy_path.expect("Should have Legacy placeholder collection");

Ok((setup, temp_dir, legacy_path))
}

// https://github.com/linux-credentials/oo7/issues/689
#[tokio::test]
async fn test_unlock_with_master_password_pending_migration()
-> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = pending_migration_setup().await?;
let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?;
let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn)
.build()
.await?;
Expand Down Expand Up @@ -713,7 +743,7 @@ mod tests {
#[tokio::test]
async fn test_unlock_with_master_password_pending_migration_wrong_password()
-> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = pending_migration_setup().await?;
let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?;
let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn)
.build()
.await?;
Expand Down Expand Up @@ -741,7 +771,7 @@ mod tests {
#[tokio::test]
async fn test_change_with_master_password_pending_migration()
-> Result<(), Box<dyn std::error::Error>> {
let (setup, _temp_dir, collection_path) = pending_migration_setup().await?;
let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?;
let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn)
.build()
.await?;
Expand Down
3 changes: 2 additions & 1 deletion server/src/gnome/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ impl GNOMEPrompterCallback {
let collection = prompt
.collection()
.expect("ChangePassword requires a collection");
let properties = match collection.set_locked(false, secret).await {
let properties = match self.service.unlock_collection(collection, secret).await
{
Ok(()) => {
self.change_password_unlocked.store(true, Ordering::Relaxed);
Properties::for_change_password_new(
Expand Down
12 changes: 9 additions & 3 deletions server/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ impl Item {

let item_self = self.clone();
let coll = collection.clone();
let service = self.service.clone();
let caller = caller.to_owned();
let action = crate::prompt::PromptAction::new(
move |unlock_secret: Option<oo7::Secret>| async move {
// Unlock the collection
coll.set_locked(false, unlock_secret).await?;
service.unlock_collection(&coll, unlock_secret).await?;

// The migration replaced the placeholder items
let item = coll
.item_from_path(item_self.path())
.await
.ok_or_else(|| ServiceError::NoSuchObject(item_self.path().to_string()))?;

// Now delete the item
item_self.delete_unlocked(&coll, &caller).await?;
item.delete_unlocked(&coll, &caller).await?;

Ok(zbus::wire::Variant::new(OwnedObjectPath::default())
.try_into_owned()
Expand Down
21 changes: 16 additions & 5 deletions server/src/pam_listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,17 @@ impl PamListener {
tracing::debug!("Attempting to unlock collection: {}", collection.path());

// Try to unlock with the provided secret
if let Err(e) = collection.set_locked(false, Some(secret.clone())).await {
tracing::debug!("Failed to unlock collection {}: {}", collection.path(), e);
} else {
tracing::info!("Unlocked collection: {}", collection.path());
match self
.service
.unlock_collection(collection, Some(secret.clone()))
.await
{
Ok(()) => {
tracing::info!("Unlocked collection: {}", collection.path());
}
Err(e) => {
tracing::debug!("Failed to unlock collection {}: {}", collection.path(), e);
}
}
}
}
Expand Down Expand Up @@ -352,7 +359,11 @@ impl PamListener {
);
drop(keyring_guard);

if let Err(e) = collection.set_locked(false, Some(old_secret.clone())).await {
if let Err(e) = self
.service
.unlock_collection(collection, Some(old_secret.clone()))
.await
{
tracing::warn!(
"Failed to unlock collection {} with old password: {}",
path,
Expand Down
Loading
Loading