summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-01-31 12:28:47 +0100
committerNeal H. Walfield <neal@pep.foundation>2019-01-31 12:28:47 +0100
commit68c5ccc29ceb2167516d37d20d952d56865e327d (patch)
tree320ab46a98ecd2c344fe387cf988c5a57fc00d12
parent0d0a558a5864fc105d1f7e1d3ac7a6eef4a15642 (diff)
openpgp: Check for plausible OpenPGP packets using Header::plausible
- Deduplicate. - Header::plausible has a more robust check.
-rw-r--r--openpgp/src/armor.rs25
-rw-r--r--openpgp/src/parse/parse.rs3
2 files changed, 7 insertions, 21 deletions
diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs
index 4309431b..72f80682 100644
--- a/openpgp/src/armor.rs
+++ b/openpgp/src/armor.rs
@@ -682,8 +682,6 @@ impl<'a> Reader<'a> {
/// Checks whether the given bytes contain armored OpenPGP data.
fn is_armored_pgp_blob(bytes: &[u8]) -> bool {
- use packet::Tag::*;
-
let bytes = if let Some(msg) = get_base64_prefix(bytes) {
msg
} else {
@@ -695,25 +693,12 @@ fn is_armored_pgp_blob(bytes: &[u8]) -> bool {
loop {
match base64::decode_config(&bytes[..end], base64::MIME) {
Ok(d) => {
- let mut br = BufferedReaderMemory::new(&d);
- let header = Header::parse(&mut br);
- break match header {
- Ok(h) => match h.ctb.tag {
- // Might be a message?
- PKESK | SKESK | OnePassSig | CompressedData | Literal =>
- true,
- // Might be a key?
- SecretKey | PublicKey =>
- true,
- // Might be a detached signature?
- Signature =>
- true,
- // ... otherwise, looks like garbage.
- _ =>
- false,
- },
- Err(_) => false,
+ // Don't consider an empty message to be valid.
+ if d.len() == 0 {
+ break false;
}
+ let mut br = BufferedReaderMemory::new(&d);
+ break Header::plausible(&mut br).is_ok()
},
Err(_) =>
if end == 0 {
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index 28cdbf15..12d1eb57 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -797,7 +797,8 @@ impl Header {
/// Returns whether the byte stream starts with a plausible
/// OpenPGP header.
///
- /// This is a heuristic.
+ /// This is a heuristic. It considers an empty message to be
+ /// plausible.
///
/// This function does not consume any data from `reader`.
pub fn plausible<R: BufferedReader<C>, C>(reader: &mut R)