//! Wipe in-memory secrets on drop — keys, not just the file at rest. //! //! Not covered here (see the PR body): allocator copies, swap/hibernation, //! core dumps, OS `String` interning, `src/file.rs` + TUI buffers, and the //! Noise handshake/transport state that lives inside `snow`. use onionwire::backup::BackupKeys; use onionwire::profile; use onionwire::session::Keys; use onionwire::wallet::Wallet; use onionwire::Store; use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing}; /// Type-level: the wrapper is applied to the type, not just mentioned in a /// comment. Removing the derive breaks the build here. fn assert_zeroize_on_drop() {} #[test] fn session_and_backup_key_types_are_zeroize_on_drop() { assert_zeroize_on_drop::(); assert_zeroize_on_drop::(); } /// Behavioural: the derived impl must wipe the secret halves and leave the /// public halves alone. A `Zeroize` that only touched the publics would still /// satisfy a bare drop assertion and leak anyway. #[test] fn session_keys_zeroize_wipes_secrets_and_keeps_publics() { let mut k = Keys::generate(); let (id_pk, prekey_pk) = (k.identity_pk, k.prekey_pk); assert_ne!(k.identity_sk, [0u8; 32]); assert_ne!(k.prekey_sk, [0u8; 32]); k.zeroize(); assert_eq!(k.identity_sk, [0u8; 32], "identity secret not wiped"); assert_eq!(k.prekey_sk, [0u8; 32], "prekey secret not wiped"); assert_eq!(k.identity_pk, id_pk, "identity public key was wiped"); assert_eq!(k.prekey_pk, prekey_pk, "prekey public key was wiped"); } /// Drops `v` in place and reads its secret field back, so a drop test cannot /// pass on a copy that the caller still holds. /// /// SAFETY: only for `ManuallyDrop` locals — the value is dropped exactly /// once, at its own address, and its storage is still alive when it is read. unsafe fn drop_in_place_and_peek_identity_sk(v: &mut Keys) -> [u8; 32] { let p = std::ptr::addr_of!(v.identity_sk); // SAFETY: the caller guarantees `v` is a `ManuallyDrop` dropped once. unsafe { std::ptr::drop_in_place(v as *mut Keys); std::ptr::read_volatile(p) } } /// Drops a `Zeroizing<[u8; 32]>` in place and reads its buffer back. /// /// SAFETY: only for `ManuallyDrop>` locals — same contract /// as `drop_in_place_and_peek_identity_sk`. unsafe fn drop_in_place_and_peek_zeroizing_u8x32(v: &mut Zeroizing<[u8; 32]>) -> [u8; 32] { let p: *const [u8; 32] = &**v; // SAFETY: the caller guarantees `v` is dropped exactly once and is still // alive at its own address when read back. unsafe { std::ptr::drop_in_place(v as *mut Zeroizing<[u8; 32]>); std::ptr::read_volatile(p) } } /// Drop behaviour, not a comment: after the value drops, the local secret key /// material that feeds the Noise IK handshake is gone from its memory. #[test] fn dropped_session_keys_leave_no_secret_behind() { let mut k = std::mem::ManuallyDrop::new(Keys::generate()); // SAFETY: `k` is a live local; the pointer targets its own field. assert_ne!(unsafe { *std::ptr::addr_of!(k.identity_sk) }, [0u8; 32]); let after = unsafe { drop_in_place_and_peek_identity_sk(&mut k) }; assert_eq!(after, [0u8; 32], "identity secret survived drop"); } /// `Keys` is `Clone`, so a cloned copy is itself live secret material. It must /// wipe on *its* drop, and dropping it must not wipe the original it came from. #[test] fn cloned_session_keys_wipe_their_own_copy_only() { let original = std::mem::ManuallyDrop::new(Keys::generate()); let mut copy = std::mem::ManuallyDrop::new((*original).clone()); let after = unsafe { drop_in_place_and_peek_identity_sk(&mut copy) }; assert_eq!(after, [0u8; 32], "cloned secret survived its drop"); // SAFETY: `original` is a live local; the pointer targets its own field. assert_ne!( unsafe { *std::ptr::addr_of!(original.identity_sk) }, [0u8; 32], "dropping the clone wiped the original's key" ); } /// The identity secret handed out by the store is a `Zeroizing`, not a bare /// `Vec` that outlives every caller. #[test] fn self_identity_secrets_are_zeroizing_and_wipe() { let dir = tempfile::tempdir().expect("tempdir"); let store = Store::open_at_with_passphrase(dir.path(), "zeroize-pass").expect("open"); let mut me = store.self_identity().expect("self"); let _: &Zeroizing> = &me.identity_sk; let _: &Zeroizing> = &me.prekey_sk; assert_eq!(me.identity_sk.len(), 32); assert!(me.identity_sk.iter().any(|b| *b != 0)); me.identity_sk.zeroize(); me.prekey_sk.zeroize(); assert!(me.identity_sk.iter().all(|b| *b == 0)); assert!(me.prekey_sk.iter().all(|b| *b == 0)); } /// The wallet RPC password is a secret, so it is `Zeroizing` and its `Debug` /// redacts it instead of printing it. #[test] fn wallet_endpoint_debug_redacts_the_rpc_password() { let w = Wallet::from_url("http://monero:hunter2@127.0.0.1:18083/json_rpc").expect("url"); let dbg = format!("{w:?}"); assert!( !dbg.contains("hunter2"), "wallet RPC password leaked into Debug: {dbg}" ); assert!(dbg.contains(""), "{dbg}"); } /// `profile::sign` needs a raw 32-byte copy of the identity secret to build the /// dalek `SigningKey`. That copy is a `Zeroizing` buffer, not a bare array that /// stays on the stack until the function returns. #[test] fn profile_signing_key_bytes_are_zeroizing_and_wipe() { let secret = [0x5au8; 32]; let bytes = profile::signing_key_bytes(&secret).expect("32 bytes"); let _: &Zeroizing<[u8; 32]> = &bytes; assert_eq!(&bytes[..], &secret[..], "buffer must hold the secret"); let mut probe = std::mem::ManuallyDrop::new(bytes); let after = unsafe { drop_in_place_and_peek_zeroizing_u8x32(&mut probe) }; assert_eq!(after, [0u8; 32], "identity secret survived its drop"); } /// A length-mismatched secret is an error, not a truncated/padded key. #[test] fn profile_signing_key_bytes_rejects_a_short_secret() { assert!(profile::signing_key_bytes(&[7u8; 31]).is_err()); assert!(profile::signing_key_bytes(&[7u8; 33]).is_err()); }