diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-06-18 19:18:56 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-06-19 11:41:00 +0200 |
commit | b2ee991c96698043f922b2b2d2dea77436db7549 (patch) | |
tree | bec005196021436ce88405ee40323aace72f4747 /openpgp/src | |
parent | b6f578b052ca4356d787ee428b1b00820e0775ab (diff) |
openpgp, buffered-reader: Make compression an optional feature.
- This helps to reduce the trusted computing base if compression is
not needed.
- This makes it easier to fuzz Sequoia, previously the fuzzer was
doing a great job creating compression bombs that triggered the
fuzzers timeout.
- Currently, the cargo workspace feature does not propagate
--no-default-features, but it is possible to depend on the openpgp
crate with `default-features = false`.
- Unfortunately, a lot of test cases include compressed packages.
This change conditionally disables these tests.
Diffstat (limited to 'openpgp/src')
-rw-r--r-- | openpgp/src/lib.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet_pile.rs | 9 | ||||
-rw-r--r-- | openpgp/src/parse/packet_pile_parser.rs | 2 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 23 | ||||
-rw-r--r-- | openpgp/src/parse/subpacket.rs | 1 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 1 | ||||
-rw-r--r-- | openpgp/src/serialize/stream.rs | 4 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/mod.rs (renamed from openpgp/src/serialize/writer.rs) | 195 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/writer_bzip2.rs | 68 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/writer_deflate.rs | 129 | ||||
-rw-r--r-- | openpgp/src/signature.rs | 12 |
11 files changed, 252 insertions, 194 deletions
diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index 8bfd504e..af011720 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -45,7 +45,9 @@ extern crate buffered_reader; extern crate nettle; +#[cfg(feature = "compression-deflate")] extern crate flate2; +#[cfg(feature = "compression-bzip2")] extern crate bzip2; #[cfg(test)] diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index 55851fb1..65ca1b79 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -27,6 +27,7 @@ macro_rules! bytes { use std::path::PathBuf; #[cfg(test)] +#[allow(dead_code)] fn path_to(artifact: &str) -> PathBuf { [env!("CARGO_MANIFEST_DIR"), "tests", "data", "messages", artifact] .iter().collect() @@ -434,8 +435,6 @@ mod message_test { use SEIP; use packet::Tag; - use std::io::Read; - #[test] fn deserialize_test_1 () { // XXX: This test should be more thorough. Right now, we mostly @@ -456,6 +455,7 @@ mod message_test { assert_eq!(count, 61); } + #[cfg(feature = "compression-deflate")] #[test] fn deserialize_test_2 () { // A message containing a compressed packet that contains a @@ -474,6 +474,7 @@ mod message_test { assert_eq!(count, 2); } + #[cfg(feature = "compression-deflate")] #[test] fn deserialize_test_3 () { let path = path_to("signed.gpg"); @@ -526,6 +527,7 @@ mod message_test { assert_eq!(pile.children().len(), 77); } + #[cfg(feature = "compression-deflate")] #[test] fn compression_quine_test_1 () { // Use the PacketPile::from_file interface to parse an OpenPGP @@ -547,6 +549,7 @@ mod message_test { assert_eq!(count, 1 + max_recursion_depth); } + #[cfg(feature = "compression-deflate")] #[test] fn compression_quine_test_2 () { // Use the iterator interface to parse an OpenPGP quine. @@ -577,8 +580,10 @@ mod message_test { assert_eq!(count, 1 + max_recursion_depth as usize); } + #[cfg(feature = "compression-deflate")] #[test] fn consume_content_1 () { + use std::io::Read; // A message containing a compressed packet that contains a // literal packet. When we read some of the compressed // packet, we expect recurse() to not recurse. diff --git a/openpgp/src/parse/packet_pile_parser.rs b/openpgp/src/parse/packet_pile_parser.rs index 3d48a8b0..f19deb81 100644 --- a/openpgp/src/parse/packet_pile_parser.rs +++ b/openpgp/src/parse/packet_pile_parser.rs @@ -13,6 +13,7 @@ use buffered_reader::{BufferedReader, BufferedReaderGeneric, BufferedReaderMemory}; #[cfg(test)] +#[allow(unused_macros)] macro_rules! bytes { ( $x:expr ) => { include_bytes!(concat!("../../tests/data/messages/", $x)) }; } @@ -282,6 +283,7 @@ fn message_parser_test() { // Check that we can use the read interface to stream the contents of // a packet. +#[cfg(feature = "compression-deflate")] #[test] fn message_parser_reader_interface() { use std::io::Read; diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 1b431b3d..145ace09 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -1122,11 +1122,19 @@ impl CompressedData { php.recursion_depth); } + #[allow(unreachable_patterns)] match algo { + CompressionAlgorithm::Uncompressed => (), + #[cfg(feature = "compression-deflate")] + CompressionAlgorithm::Zip + | CompressionAlgorithm::Zlib => (), + #[cfg(feature = "compression-bzip2")] + CompressionAlgorithm::BZip2 => (), CompressionAlgorithm::Unknown(_) | CompressionAlgorithm::Private(_) => return php.fail("unknown compression algorithm"), - _ => (), + _ => + return php.fail("unsupported compression algorithm"), } let recursion_depth = php.recursion_depth as usize; @@ -1143,14 +1151,18 @@ impl CompressedData { for a compression filter: this is an \ \"uncompressed compression packet\"."); } + let _ = recursion_depth; reader }, + #[cfg(feature = "compression-deflate")] CompressionAlgorithm::Zip => Box::new(BufferedReaderDeflate::with_cookie( reader, Cookie::new(recursion_depth))), + #[cfg(feature = "compression-deflate")] CompressionAlgorithm::Zlib => Box::new(BufferedReaderZlib::with_cookie( reader, Cookie::new(recursion_depth))), + #[cfg(feature = "compression-bzip2")] CompressionAlgorithm::BZip2 => Box::new(BufferedReaderBzip::with_cookie( reader, Cookie::new(recursion_depth))), @@ -1162,11 +1174,19 @@ impl CompressedData { } } +#[cfg(any(feature = "compression-deflate", feature = "compression-bzip2"))] #[test] fn compressed_data_parser_test () { let expected = bytes!("a-cypherpunks-manifesto.txt"); for i in 1..4 { + match CompressionAlgorithm::from(i) { + #[cfg(feature = "compression-deflate")] + CompressionAlgorithm::Zip | CompressionAlgorithm::Zlib => (), + #[cfg(feature = "compression-bzip2")] + CompressionAlgorithm::BZip2 => (), + _ => continue, + } let path = path_to(&format!("compressed-data-algo-{}.gpg", i)[..]); let mut pp = PacketParser::from_file(path).unwrap().unwrap(); @@ -2591,6 +2611,7 @@ impl<'a> BufferedReader<Cookie> for PacketParser<'a> { // Check that we can use the read interface to stream the contents of // a packet. +#[cfg(feature = "compression-deflate")] #[test] fn packet_parser_reader_interface() { // We need the Read trait. diff --git a/openpgp/src/parse/subpacket.rs b/openpgp/src/parse/subpacket.rs index 3d8b612a..0c58d76c 100644 --- a/openpgp/src/parse/subpacket.rs +++ b/openpgp/src/parse/subpacket.rs @@ -1480,6 +1480,7 @@ impl Signature { } } +#[cfg(feature = "compression-deflate")] #[test] fn subpacket_test_1 () { use PacketPile; diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 298536a2..01d51479 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -1034,6 +1034,7 @@ mod serialize_test { } + #[cfg(feature = "compression-deflate")] #[test] fn serialize_test_2() { // Given a packet in serialized form: diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs index 1434b677..012ceca8 100644 --- a/openpgp/src/serialize/stream.rs +++ b/openpgp/src/serialize/stream.rs @@ -542,10 +542,13 @@ impl<'a> Compressor<'a> { let inner: writer::Stack<'a, Cookie> = match algo { CompressionAlgorithm::Uncompressed => writer::Identity::new(inner, Cookie::new(level)), + #[cfg(feature = "compression-deflate")] CompressionAlgorithm::Zip => writer::ZIP::new(inner, Cookie::new(level)), + #[cfg(feature = "compression-deflate")] CompressionAlgorithm::Zlib => writer::ZLIB::new(inner, Cookie::new(level)), + #[cfg(feature = "compression-bzip2")] CompressionAlgorithm::BZip2 => writer::BZ::new(inner, Cookie::new(level)), _ => unimplemented!(), @@ -1045,6 +1048,7 @@ mod test { } } + #[cfg(feature = "compression-deflate")] #[test] fn stream_big() { let mut zeros = Vec::<u8>::new(); diff --git a/openpgp/src/serialize/writer.rs b/openpgp/src/serialize/writer/mod.rs index 628187d1..39d21bcd 100644 --- a/openpgp/src/serialize/writer.rs +++ b/openpgp/src/serialize/writer/mod.rs @@ -1,9 +1,14 @@ //! Stackable writers. -use bzip2::Compression as BzCompression; -use bzip2::write::BzEncoder; -use flate2::Compression as FlateCompression; -use flate2::write::{DeflateEncoder, ZlibEncoder}; +#[cfg(feature = "compression-bzip2")] +mod writer_bzip2; +#[cfg(feature = "compression-bzip2")] +pub use self::writer_bzip2::BZ; +#[cfg(feature = "compression-deflate")] +mod writer_deflate; +#[cfg(feature = "compression-deflate")] +pub use self::writer_deflate::{ZIP, ZLIB}; + use std::fmt; use std::io; @@ -280,188 +285,6 @@ impl<'a, W: io::Write, C> Stackable<'a, C> for Generic<W, C> { } } -/// ZIPing writer. -pub struct ZIP<'a, C> { - inner: Generic<DeflateEncoder<Stack<'a, C>>, C>, -} - -impl<'a, C> ZIP<'a, C> { - pub fn new(inner: Stack<'a, C>, cookie: C) -> Box<Self> { - Box::new(ZIP { - inner: Generic::new_unboxed( - DeflateEncoder::new(inner, FlateCompression::default()), - cookie), - }) - } -} - -impl<'a, C:> fmt::Debug for ZIP<'a, C> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("writer::ZIP") - .field("inner", &self.inner) - .finish() - } -} - -impl<'a, C> io::Write for ZIP<'a, C> { - fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { - self.inner.write(bytes) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -impl<'a, C> Stackable<'a, C> for ZIP<'a, C> { - fn into_inner(self: Box<Self>) -> Result<Option<Stack<'a, C>>> { - let inner = self.inner.inner.finish()?; - Ok(Some(inner)) - } - fn pop(&mut self) -> Result<Option<Stack<'a, C>>> { - unimplemented!() - } - fn mount(&mut self, _new: Stack<'a, C>) { - unimplemented!() - } - fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() - } - fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() - } - fn cookie_set(&mut self, cookie: C) -> C { - self.inner.cookie_set(cookie) - } - fn cookie_ref(&self) -> &C { - self.inner.cookie_ref() - } - fn cookie_mut(&mut self) -> &mut C { - self.inner.cookie_mut() - } -} - -/// ZLIBing writer. -pub struct ZLIB<'a, C> { - inner: Generic<ZlibEncoder<Stack<'a, C>>, C>, -} - -impl<'a, C> ZLIB<'a, C> { - pub fn new(inner: Stack<'a, C>, cookie: C) -> Box<Self> { - Box::new(ZLIB { - inner: Generic::new_unboxed( - ZlibEncoder::new(inner, FlateCompression::default()), - cookie), - }) - } -} - -impl<'a, C:> fmt::Debug for ZLIB<'a, C> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("writer::ZLIB") - .field("inner", &self.inner) - .finish() - } -} - -impl<'a, C> io::Write for ZLIB<'a, C> { - fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { - self.inner.write(bytes) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -impl<'a, C> Stackable<'a, C> for ZLIB<'a, C> { - fn into_inner(self: Box<Self>) -> Result<Option<Stack<'a, C>>> { - let inner = self.inner.inner.finish()?; - Ok(Some(inner)) - } - fn pop(&mut self) -> Result<Option<Stack<'a, C>>> { - unimplemented!() - } - fn mount(&mut self, _new: Stack<'a, C>) { - unimplemented!() - } - fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() - } - fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() - } - fn cookie_set(&mut self, cookie: C) -> C { - self.inner.cookie_set(cookie) - } - fn cookie_ref(&self) -> &C { - self.inner.cookie_ref() - } - fn cookie_mut(&mut self) -> &mut C { - self.inner.cookie_mut() - } -} - -/// BZing writer. -pub struct BZ<'a, C> { - inner: Generic<BzEncoder<Stack<'a, C>>, C>, -} - -impl<'a, C> BZ<'a, C> { - pub fn new(inner: Stack<'a, C>, cookie: C) -> Box<Self> { - Box::new(BZ { - inner: Generic::new_unboxed( - BzEncoder::new(inner, BzCompression::Default), - cookie), - }) - } -} - -impl<'a, C:> fmt::Debug for BZ<'a, C> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("writer::BZ") - .field("inner", &self.inner) - .finish() - } -} - -impl<'a, C> io::Write for BZ<'a, C> { - fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { - self.inner.write(bytes) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -impl<'a, C> Stackable<'a, C> for BZ<'a, C> { - fn into_inner(self: Box<Self>) -> Result<Option<Stack<'a, C>>> { - let inner = self.inner.inner.finish()?; - Ok(Some(inner)) - } - fn pop(&mut self) -> Result<Option<Stack<'a, C>>> { - unimplemented!() - } - fn mount(&mut self, _new: Stack<'a, C>) { - unimplemented!() - } - fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { - self.inner.inner_mut() - } - fn inner_ref(&self) -> Option<&Stackable<'a, C>> { - self.inner.inner_ref() - } - fn cookie_set(&mut self, cookie: C) -> C { - self.inner.cookie_set(cookie) - } - fn cookie_ref(&self) -> &C { - self.inner.cookie_ref() - } - fn cookie_mut(&mut self) -> &mut C { - self.inner.cookie_mut() - } -} /// Encrypting writer. pub struct Encryptor<'a, C> { diff --git a/openpgp/src/serialize/writer/writer_bzip2.rs b/openpgp/src/serialize/writer/writer_bzip2.rs new file mode 100644 index 00000000..4411e574 --- /dev/null +++ b/openpgp/src/serialize/writer/writer_bzip2.rs @@ -0,0 +1,68 @@ +use bzip2::Compression as BzCompression; +use bzip2::write::BzEncoder; +use std::fmt; +use std::io; + +use Result; +use super::{Generic, Stack, Stackable}; + +/// BZing writer. +pub struct BZ<'a, C> { + inner: Generic<BzEncoder<Stack<'a, C>>, C>, +} + +impl<'a, C> BZ<'a, C> { + pub fn new(inner: Stack<'a, C>, cookie: C) -> Box<Self> { + Box::new(BZ { + inner: Generic::new_unboxed( + BzEncoder::new(inner, BzCompression::Default), + cookie), + }) + } +} + +impl<'a, C:> fmt::Debug for BZ<'a, C> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("writer::BZ") + .field("inner", &self.inner) + .finish() + } +} + +impl<'a, C> io::Write for BZ<'a, C> { + fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { + self.inner.write(bytes) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +impl<'a, C> Stackable<'a, C> for BZ<'a, C> { + fn into_inner(self: Box<Self>) -> Result<Option<Stack<'a, C>>> { + let inner = self.inner.inner.finish()?; + Ok(Some(inner)) + } + fn pop(&mut self) -> Result<Option<Stack<'a, C>>> { + unimplemented!() + } + fn mount(&mut self, _new: Stack<'a, C>) { + unimplemented!() + } + fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { + self.inner.inner_mut() + } + fn inner_ref(&self) -> Option<&Stackable<'a, C>> { + self.inner.inner_ref() + } + fn cookie_set(&mut self, cookie: C) -> C { + self.inner.cookie_set(cookie) + } + fn cookie_ref(&self) -> &C { + self.inner.cookie_ref() + } + fn cookie_mut(&mut self) -> &mut C { + self.inner.cookie_mut() + } +} diff --git a/openpgp/src/serialize/writer/writer_deflate.rs b/openpgp/src/serialize/writer/writer_deflate.rs new file mode 100644 index 00000000..54900dff --- /dev/null +++ b/openpgp/src/serialize/writer/writer_deflate.rs @@ -0,0 +1,129 @@ +use flate2::Compression as FlateCompression; +use flate2::write::{DeflateEncoder, ZlibEncoder}; +use std::fmt; +use std::io; + +use Result; +use super::{Generic, Stack, Stackable}; + +/// ZIPing writer. +pub struct ZIP<'a, C> { + inner: Generic<DeflateEncoder<Stack<'a, C>>, C>, +} + +impl<'a, C> ZIP<'a, C> { + pub fn new(inner: Stack<'a, C>, cookie: C) -> Box<Self> { + Box::new(ZIP { + inner: Generic::new_unboxed( + DeflateEncoder::new(inner, FlateCompression::default()), + cookie), + }) + } +} + +impl<'a, C:> fmt::Debug for ZIP<'a, C> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("writer::ZIP") + .field("inner", &self.inner) + .finish() + } +} + +impl<'a, C> io::Write for ZIP<'a, C> { + fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { + self.inner.write(bytes) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +impl<'a, C> Stackable<'a, C> for ZIP<'a, C> { + fn into_inner(self: Box<Self>) -> Result<Option<Stack<'a, C>>> { + let inner = self.inner.inner.finish()?; + Ok(Some(inner)) + } + fn pop(&mut self) -> Result<Option<Stack<'a, C>>> { + unimplemented!() + } + fn mount(&mut self, _new: Stack<'a, C>) { + unimplemented!() + } + fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { + self.inner.inner_mut() + } + fn inner_ref(&self) -> Option<&Stackable<'a, C>> { + self.inner.inner_ref() + } + fn cookie_set(&mut self, cookie: C) -> C { + self.inner.cookie_set(cookie) + } + fn cookie_ref(&self) -> &C { + self.inner.cookie_ref() + } + fn cookie_mut(&mut self) -> &mut C { + self.inner.cookie_mut() + } +} + +/// ZLIBing writer. +pub struct ZLIB<'a, C> { + inner: Generic<ZlibEncoder<Stack<'a, C>>, C>, +} + +impl<'a, C> ZLIB<'a, C> { + pub fn new(inner: Stack<'a, C>, cookie: C) -> Box<Self> { + Box::new(ZLIB { + inner: Generic::new_unboxed( + ZlibEncoder::new(inner, FlateCompression::default()), + cookie), + }) + } +} + +impl<'a, C:> fmt::Debug for ZLIB<'a, C> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("writer::ZLIB") + .field("inner", &self.inner) + .finish() + } +} + +impl<'a, C> io::Write for ZLIB<'a, C> { + fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { + self.inner.write(bytes) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +impl<'a, C> Stackable<'a, C> for ZLIB<'a, C> { + fn into_inner(self: Box<Self>) -> Result<Option<Stack<'a, C>>> { + let inner = self.inner.inner.finish()?; + Ok(Some(inner)) + } + fn pop(&mut self) -> Result<Option<Stack<'a, C>>> { + unimplemented!() + } + fn mount(&mut self, _new: Stack<'a, C>) { + unimplemented!() + } + fn inner_mut(&mut self) -> Option<&mut Stackable<'a, C>> { + self.inner.inner_mut() + } + fn inner_ref(&self) -> Option<&Stackable<'a, C>> { + self.inner.inner_ref() + } + fn cookie_set(&mut self, cookie: C) -> C { + self.inner.cookie_set(cookie) + } + fn cookie_ref(&self) -> &C { + self.inner.cookie_ref() + } + fn cookie_mut(&mut self) -> &mut C { + self.inner.cookie_mut() + } +} diff --git a/openpgp/src/signature.rs b/openpgp/src/signature.rs index 970d88b0..7efb649e 100644 --- a/openpgp/src/signature.rs +++ b/openpgp/src/signature.rs @@ -24,6 +24,7 @@ use std::path::PathBuf; const TRACE : bool = false; #[cfg(test)] +#[allow(dead_code)] fn path_to(artifact: &str) -> PathBuf { [env!("CARGO_MANIFEST_DIR"), "tests", "data", artifact] .iter().collect() @@ -362,13 +363,14 @@ impl Signature { #[cfg(test)] mod test { - use super::*; - - use TPK; - use parse::PacketParser; - + #[cfg(feature = "compression-deflate")] #[test] fn signature_verification_test() { + use super::*; + + use TPK; + use parse::PacketParser; + struct Test<'a> { key: &'a str, data: &'a str, |