summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-08-12 15:42:18 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-08-12 15:42:18 +0200
commit33d23cd76a6342faf2b809c665a91772112d43b3 (patch)
tree4d2c39814ed6a4c4133c3ab6a8d61187575a26aa
parente007e23d2a73cad230bc7513275d946c8b9b825f (diff)
openpgp: Make SKESK?::esk and SKESK5::aead_iv fallible.
-rw-r--r--openpgp/src/packet/skesk.rs10
-rw-r--r--openpgp/src/serialize.rs12
-rw-r--r--tool/src/commands/dump.rs10
3 files changed, 17 insertions, 15 deletions
diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs
index fc6b9437..7d4bcfab 100644
--- a/openpgp/src/packet/skesk.rs
+++ b/openpgp/src/packet/skesk.rs
@@ -161,8 +161,8 @@ impl SKESK4 {
}
/// Gets the encrypted session key.
- pub fn esk(&self) -> Option<&[u8]> {
- self.esk.as_ref().map(|esk| esk.as_slice())
+ pub fn esk(&self) -> Result<Option<&[u8]>> {
+ Ok(self.esk.as_ref().map(|esk| esk.as_slice()))
}
/// Sets the encrypted session key.
@@ -361,7 +361,7 @@ impl SKESK5 {
let key = self.s2k().derive_key(password,
self.symmetric_algo().key_size()?)?;
- if let Some(ref esk) = self.esk() {
+ if let Some(ref esk) = self.esk()? {
// Use the derived key to decrypt the ESK.
let mut cipher = self.aead_algo.context(
self.symmetric_algo(), &key, &self.aead_iv)?;
@@ -396,8 +396,8 @@ impl SKESK5 {
}
/// Gets the AEAD initialization vector.
- pub fn aead_iv(&self) -> &[u8] {
- &self.aead_iv
+ pub fn aead_iv(&self) -> Result<&[u8]> {
+ Ok(&self.aead_iv)
}
/// Sets the AEAD initialization vector.
diff --git a/openpgp/src/serialize.rs b/openpgp/src/serialize.rs
index d54689c3..e57fdacc 100644
--- a/openpgp/src/serialize.rs
+++ b/openpgp/src/serialize.rs
@@ -2306,7 +2306,7 @@ impl Marshal for SKESK4 {
write_byte(o, 4)?; // Version.
write_byte(o, self.symmetric_algo().into())?;
self.s2k().serialize(o)?;
- if let Some(ref esk) = self.esk() {
+ if let Some(ref esk) = self.esk()? {
o.write_all(&esk[..])?;
}
@@ -2319,7 +2319,7 @@ impl NetLength for SKESK4 {
1 // Version.
+ 1 // Algo.
+ self.s2k().serialized_len()
- + self.esk().map(|esk| esk.len()).unwrap_or(0)
+ + self.esk().unwrap().map(|esk| esk.len()).unwrap_or(0)
}
}
@@ -2339,8 +2339,8 @@ impl Marshal for SKESK5 {
write_byte(o, self.symmetric_algo().into())?;
write_byte(o, self.aead_algo().into())?;
self.s2k().serialize(o)?;
- o.write_all(self.aead_iv())?;
- if let Some(ref esk) = self.esk() {
+ o.write_all(self.aead_iv()?)?;
+ if let Some(ref esk) = self.esk()? {
o.write_all(&esk[..])?;
}
o.write_all(self.aead_digest())?;
@@ -2355,8 +2355,8 @@ impl NetLength for SKESK5 {
+ 1 // Cipher algo.
+ 1 // AEAD algo.
+ self.s2k().serialized_len()
- + self.aead_iv().len()
- + self.esk().map(|esk| esk.len()).unwrap_or(0)
+ + self.aead_iv().unwrap().len()
+ + self.esk().unwrap().map(|esk| esk.len()).unwrap_or(0)
+ self.aead_digest().len()
}
}
diff --git a/tool/src/commands/dump.rs b/tool/src/commands/dump.rs
index b149f837..12752a44 100644
--- a/tool/src/commands/dump.rs
+++ b/tool/src/commands/dump.rs
@@ -654,7 +654,7 @@ impl PacketDumper {
s.symmetric_algo())?;
write!(output, "{} S2K: ", i)?;
self.dump_s2k(output, i, s.s2k())?;
- if let Some(esk) = s.esk() {
+ if let Ok(Some(esk)) = s.esk() {
writeln!(output, "{} ESK: {}", i,
hex::encode(esk))?;
}
@@ -667,9 +667,11 @@ impl PacketDumper {
s.aead_algo())?;
write!(output, "{} S2K: ", i)?;
self.dump_s2k(output, i, s.s2k())?;
- writeln!(output, "{} IV: {}", i,
- hex::encode(s.aead_iv()))?;
- if let Some(esk) = s.esk() {
+ if let Ok(iv) = s.aead_iv() {
+ writeln!(output, "{} IV: {}", i,
+ hex::encode(iv))?;
+ }
+ if let Ok(Some(esk)) = s.esk() {
writeln!(output, "{} ESK: {}", i,
hex::encode(esk))?;
}