summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-05-11 14:48:09 +0200
committerNeal H. Walfield <neal@pep.foundation>2019-05-11 14:48:09 +0200
commit5a2688066b934f73ff4e69640f2bc47211a84451 (patch)
treebc7c83efc28b9df3cfb8b0b1bf0198be623ffcaa /openpgp
parentccaccf692688451e984e75b2d020a7077dc15546 (diff)
openpgp: Make {is,possible}_{message,keyring,tpk} return a Result
- PacketParserEOF::is_message, PacketParserEOF::is_keyring, PacketParserEOF::is_tpk, PacketParserResult::possible_message, PacketParserResult::possible_keyring, and PacketParserResult::possible_tpk returned a boolean. - Change them to return a Result<()> instead, which is more Rusty, and, in particular, allows the caller to determine why the message didn't parse.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/examples/notarize.rs8
-rw-r--r--openpgp/src/autocrypt.rs6
-rw-r--r--openpgp/src/parse/parse.rs82
-rw-r--r--openpgp/src/parse/stream.rs24
4 files changed, 77 insertions, 43 deletions
diff --git a/openpgp/examples/notarize.rs b/openpgp/examples/notarize.rs
index b46fbe1a..44ebb0f1 100644
--- a/openpgp/examples/notarize.rs
+++ b/openpgp/examples/notarize.rs
@@ -76,8 +76,8 @@ fn main() {
.expect("Failed to build parser");
while let PacketParserResult::Some(mut pp) = ppr {
- if ! pp.possible_message() {
- panic!("Malformed OpenPGP message");
+ if let Err(err) = pp.possible_message() {
+ panic!("Malformed OpenPGP message: {}", err);
}
match pp.packet {
@@ -108,8 +108,8 @@ fn main() {
ppr = pp.recurse().expect("Failed to recurse").1;
}
if let PacketParserResult::EOF(eof) = ppr {
- if ! eof.is_message() {
- panic!("Malformed OpenPGP message")
+ if let Err(err) = eof.is_message() {
+ panic!("Malformed OpenPGP message: {}", err)
}
} else {
unreachable!()
diff --git a/openpgp/src/autocrypt.rs b/openpgp/src/autocrypt.rs
index d99b5852..53545bd5 100644
--- a/openpgp/src/autocrypt.rs
+++ b/openpgp/src/autocrypt.rs
@@ -752,9 +752,9 @@ impl<'a> AutocryptSetupMessageParser<'a> {
// If we've gotten this far, then the outer message
// has the right sequence of packets, but we haven't
// carefully checked the nesting. We do that now.
- if ! pp.is_message() {
- return Err(Error::MalformedMessage(
- "Invalid OpenPGP Message".into()).into());
+ if let Err(err) = pp.is_message() {
+ return Err(err.context(
+ "Invalid OpenPGP Message").into());
}
}
PacketParserResult::Some(pp) =>
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index 7d8d2213..a706e0c5 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -2596,22 +2596,40 @@ impl PacketParserEOF {
/// Whether the message is an OpenPGP Message.
///
/// As opposed to a TPK or just a bunch of packets.
- pub fn is_message(&self) -> bool {
- self.state.message_validator.is_message()
+ pub fn is_message(&self) -> Result<()> {
+ use message::MessageValidity;
+
+ match self.state.message_validator.check() {
+ MessageValidity::Message => Ok(()),
+ MessageValidity::MessagePrefix => unreachable!(),
+ MessageValidity::Error(err) => Err(err),
+ }
}
/// Whether the message is an OpenPGP keyring.
///
/// As opposed to a Message or just a bunch of packets.
- pub fn is_keyring(&self) -> bool {
- self.state.keyring_validator.is_keyring()
+ pub fn is_keyring(&self) -> Result<()> {
+ use tpk::KeyringValidity;
+
+ match self.state.keyring_validator.check() {
+ KeyringValidity::Keyring => Ok(()),
+ KeyringValidity::KeyringPrefix => unreachable!(),
+ KeyringValidity::Error(err) => Err(err),
+ }
}
/// Whether the message is an OpenPGP TPK.
///
/// As opposed to a Message or just a bunch of packets.
- pub fn is_tpk(&self) -> bool {
- self.state.tpk_validator.is_tpk()
+ pub fn is_tpk(&self) -> Result<()> {
+ use tpk::TPKValidity;
+
+ match self.state.tpk_validator.check() {
+ TPKValidity::TPK => Ok(()),
+ TPKValidity::TPKPrefix => unreachable!(),
+ TPKValidity::Error(err) => Err(err),
+ }
}
/// Returns the path of the last packet.
@@ -2866,8 +2884,14 @@ impl <'a> PacketParser<'a> {
/// to say whether the message is definitely an OpenPGP Message.
/// Before that, it is only possible to say that the message is a
/// valid prefix or definitely not an OpenPGP message.
- pub fn possible_message(&self) -> bool {
- self.state.message_validator.check().is_message_prefix()
+ pub fn possible_message(&self) -> Result<()> {
+ use message::MessageValidity;
+
+ match self.state.message_validator.check() {
+ MessageValidity::Message => unreachable!(),
+ MessageValidity::MessagePrefix => Ok(()),
+ MessageValidity::Error(err) => Err(err),
+ }
}
/// Returns whether the message appears to be an OpenPGP keyring.
@@ -2876,8 +2900,14 @@ impl <'a> PacketParser<'a> {
/// to say whether the message is definitely an OpenPGP keyring.
/// Before that, it is only possible to say that the message is a
/// valid prefix or definitely not an OpenPGP keyring.
- pub fn possible_keyring(&self) -> bool {
- self.state.keyring_validator.check().is_keyring_prefix()
+ pub fn possible_keyring(&self) -> Result<()> {
+ use tpk::KeyringValidity;
+
+ match self.state.keyring_validator.check() {
+ KeyringValidity::Keyring => unreachable!(),
+ KeyringValidity::KeyringPrefix => Ok(()),
+ KeyringValidity::Error(err) => Err(err),
+ }
}
/// Returns whether the message appears to be an OpenPGP TPK.
@@ -2886,8 +2916,14 @@ impl <'a> PacketParser<'a> {
/// to say whether the message is definitely an OpenPGP TPK.
/// Before that, it is only possible to say that the message is a
/// valid prefix or definitely not an OpenPGP TPK.
- pub fn possible_tpk(&self) -> bool {
- self.state.tpk_validator.check().is_tpk_prefix()
+ pub fn possible_tpk(&self) -> Result<()> {
+ use tpk::TPKValidity;
+
+ match self.state.tpk_validator.check() {
+ TPKValidity::TPK => unreachable!(),
+ TPKValidity::TPKPrefix => Ok(()),
+ TPKValidity::Error(err) => Err(err),
+ }
}
/// Returns Ok if the data appears to be a legal packet.
@@ -4108,7 +4144,7 @@ mod test {
// Make sure we actually decrypted...
let mut saw_literal = false;
while let PacketParserResult::Some(mut pp) = ppr {
- assert!(pp.possible_message());
+ assert!(pp.possible_message().is_ok());
match pp.packet {
Packet::SEIP(_) | Packet::AED(_) => {
@@ -4127,7 +4163,7 @@ mod test {
}
assert!(saw_literal);
if let PacketParserResult::EOF(eof) = ppr {
- assert!(eof.is_message());
+ assert!(eof.is_message().is_ok());
} else {
unreachable!();
}
@@ -4149,12 +4185,12 @@ mod test {
.expect(&format!("Error reading {:?}", path));
while let PacketParserResult::Some(mut pp) = ppr {
- assert!(pp.possible_keyring());
+ assert!(pp.possible_keyring().is_ok());
ppr = pp.recurse().unwrap().1;
}
if let PacketParserResult::EOF(eof) = ppr {
- assert!(eof.is_keyring());
- assert!(! eof.is_tpk());
+ assert!(eof.is_keyring().is_ok());
+ assert!(eof.is_tpk().is_err());
} else {
unreachable!();
}
@@ -4174,13 +4210,13 @@ mod test {
.expect(&format!("Error reading {:?}", path));
while let PacketParserResult::Some(mut pp) = ppr {
- assert!(pp.possible_keyring());
- assert!(pp.possible_tpk());
+ assert!(pp.possible_keyring().is_ok());
+ assert!(pp.possible_tpk().is_ok());
ppr = pp.recurse().unwrap().1;
}
if let PacketParserResult::EOF(eof) = ppr {
- assert!(eof.is_keyring());
- assert!(eof.is_tpk());
+ assert!(eof.is_keyring().is_ok());
+ assert!(eof.is_tpk().is_ok());
} else {
unreachable!();
}
@@ -4199,7 +4235,7 @@ mod test {
let mut saw_literal = false;
while let PacketParserResult::Some(mut pp) = ppr {
- assert!(pp.possible_message());
+ assert!(pp.possible_message().is_ok());
match pp.packet {
Packet::Literal(_) => {
@@ -4214,7 +4250,7 @@ mod test {
assert!(! saw_literal);
if let PacketParserResult::EOF(eof) = ppr {
eprintln!("eof: {:?}", eof);
- assert!(eof.is_message());
+ assert!(eof.is_message().is_ok());
} else {
unreachable!();
}
diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs
index d9791479..957a75bf 100644
--- a/openpgp/src/parse/stream.rs
+++ b/openpgp/src/parse/stream.rs
@@ -482,9 +482,8 @@ impl<'a, H: VerificationHelper> Verifier<'a, H> {
let mut issuers = Vec::new();
while let PacketParserResult::Some(pp) = ppr {
- if ! pp.possible_message() {
- return Err(Error::MalformedMessage(
- "Malformed OpenPGP message".into()).into());
+ if let Err(err) = pp.possible_message() {
+ return Err(err.context("Malformed OpenPGP message").into());
}
match pp.packet {
@@ -586,9 +585,9 @@ impl<'a, H: VerificationHelper> Verifier<'a, H> {
// Process the rest of the packets.
let mut ppr = PacketParserResult::Some(pp);
while let PacketParserResult::Some(mut pp) = ppr {
- if ! pp.possible_message() {
- return Err(Error::MalformedMessage(
- "Malformed OpenPGP message".into()).into());
+ if let Err(err) = pp.possible_message() {
+ return Err(err.context(
+ "Malformed OpenPGP message").into());
}
let (p, ppr_tmp) = pp.recurse()?;
@@ -1232,10 +1231,9 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> {
while let PacketParserResult::Some(mut pp) = ppr {
v.helper.inspect(&pp)?;
- if ! pp.possible_message() {
- t!("Malformed message");
- return Err(Error::MalformedMessage(
- "Malformed OpenPGP message".into()).into());
+ if let Err(err) = pp.possible_message() {
+ t!("Malformed message: {}", err);
+ return Err(err.context("Malformed OpenPGP message").into());
}
match pp.packet {
@@ -1388,9 +1386,9 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> {
self.helper.inspect(&pp)?;
}
- if ! pp.possible_message() {
- return Err(Error::MalformedMessage(
- "Malformed OpenPGP message".into()).into());
+ if let Err(err) = pp.possible_message() {
+ return Err(err.context(
+ "Malformed OpenPGP message").into());
}
match pp.packet {