diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-08-12 15:42:18 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-08-20 12:17:21 +0200 |
commit | 05df451257320bf8dfd86f7fdd4d63f9c78dd691 (patch) | |
tree | 7d7cfbf8a88de0138bb2cd10785743d17955adee | |
parent | bb447441a4a94563736760e28f368e1186064c57 (diff) |
openpgp: Make SKESK?::esk and SKESK5::aead_iv fallible.
-rw-r--r-- | openpgp/src/packet/skesk.rs | 10 | ||||
-rw-r--r-- | openpgp/src/serialize.rs | 12 | ||||
-rw-r--r-- | tool/src/commands/dump.rs | 10 |
3 files changed, 17 insertions, 15 deletions
diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs index f77ee76b..59d1e857 100644 --- a/openpgp/src/packet/skesk.rs +++ b/openpgp/src/packet/skesk.rs @@ -160,8 +160,8 @@ impl SKESK4 { } /// Gets the encrypted session key. - pub fn esk(&self) -> Option<&[u8]> { - self.esk.as_ref().map(|esk| &esk[..]) + pub fn esk(&self) -> Result<Option<&[u8]>> { + Ok(self.esk.as_ref().map(|esk| &esk[..])) } /// Sets the encrypted session key. @@ -359,7 +359,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)?; @@ -394,8 +394,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 4aababcd..fe1d36cc 100644 --- a/openpgp/src/serialize.rs +++ b/openpgp/src/serialize.rs @@ -2313,7 +2313,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[..])?; } @@ -2326,7 +2326,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) } } @@ -2346,8 +2346,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())?; @@ -2362,8 +2362,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))?; } |