summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto/mod.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-12-19 10:32:00 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-12-19 10:35:19 +0100
commit2b2b5c8905d0e823d03b5ba2a115298e80e08b74 (patch)
tree500a8fb3df95c45325670b98089da1d23dead2bd /openpgp/src/crypto/mod.rs
parentc97666274f71ee10dc90804b5483e12c8bfeef31 (diff)
openpgp: Encrypt passwords in memory.
Diffstat (limited to 'openpgp/src/crypto/mod.rs')
-rw-r--r--openpgp/src/crypto/mod.rs21
1 files changed, 14 insertions, 7 deletions
diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs
index fc2a0fef..e4b26810 100644
--- a/openpgp/src/crypto/mod.rs
+++ b/openpgp/src/crypto/mod.rs
@@ -112,19 +112,22 @@ impl fmt::Debug for SessionKey {
/// Holds a password.
///
-/// The password is cleared when dropped.
+/// The password is encrypted in memory and only decrypted on demand.
+/// See [`mem::Encrypted`] for details.
+///
+/// [`mem::Encrypted`]: mem/struct.Encrypted.html
#[derive(Clone, PartialEq, Eq)]
-pub struct Password(mem::Protected);
+pub struct Password(mem::Encrypted);
impl From<Vec<u8>> for Password {
fn from(v: Vec<u8>) -> Self {
- Password(v.into())
+ Password(mem::Encrypted::new(v.into()))
}
}
impl From<Box<[u8]>> for Password {
fn from(v: Box<[u8]>) -> Self {
- Password(v.into())
+ Password(mem::Encrypted::new(v.into()))
}
}
@@ -148,16 +151,20 @@ impl From<&[u8]> for Password {
impl fmt::Debug for Password {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "Password ({:?})", self.0)
+ if cfg!(debug_assertions) {
+ self.map(|p| write!(f, "Password({:?})", p))
+ } else {
+ f.write_str("Password(<Encrypted>)")
+ }
}
}
impl Password {
/// Maps the given function over the password.
- pub fn map<F, T>(&self, mut fun: F) -> T
+ pub fn map<F, T>(&self, fun: F) -> T
where F: FnMut(&mem::Protected) -> T
{
- fun(&self.0)
+ self.0.map(fun)
}
}