diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-11-16 11:04:36 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-12-14 14:05:21 +0100 |
commit | d411fb80983a4d4ebb9f023599c38e34a26551e7 (patch) | |
tree | fc970ff950923f7c400b24163f4079b3c6ba48cf | |
parent | 251541318fdf453c5d756794b906cce2fd69b675 (diff) |
openpgp: Introduce trait Parse.
- Trait Parse introduces a uniform interface to parse packets,
messages, keys, and related data structures.
44 files changed, 623 insertions, 212 deletions
diff --git a/examples/guide-exploring-openpgp.rs b/examples/guide-exploring-openpgp.rs index 88d4559d..66b95588 100644 --- a/examples/guide-exploring-openpgp.rs +++ b/examples/guide-exploring-openpgp.rs @@ -2,6 +2,7 @@ #[macro_use] // For armored! extern crate sequoia_openpgp as openpgp; +use openpgp::parse::Parse; fn main() { let mut reader = armored!( diff --git a/examples/guide-the-keystore.rs b/examples/guide-the-keystore.rs index 8ede6ac9..dac57c85 100644 --- a/examples/guide-the-keystore.rs +++ b/examples/guide-the-keystore.rs @@ -4,6 +4,7 @@ extern crate sequoia_openpgp as openpgp; extern crate sequoia; use sequoia::{core, store}; +use openpgp::parse::Parse; fn main() { let mut reader = armored!( diff --git a/ffi/src/openpgp.rs b/ffi/src/openpgp.rs index ef796144..3db1aa70 100644 --- a/ffi/src/openpgp.rs +++ b/ffi/src/openpgp.rs @@ -38,7 +38,12 @@ use self::openpgp::tpk::{ KeyIter }; use self::openpgp::packet; -use self::openpgp::parse::{PacketParserResult, PacketParser, PacketParserEOF}; +use self::openpgp::parse::{ + Parse, + PacketParserResult, + PacketParser, + PacketParserEOF, +}; use self::openpgp::parse::stream::{ DecryptionHelper, Decryptor, diff --git a/guide/src/chapter_02.md b/guide/src/chapter_02.md index f39cecd1..80d5cc1c 100644 --- a/guide/src/chapter_02.md +++ b/guide/src/chapter_02.md @@ -17,6 +17,7 @@ success, we can use or examine the resulting [`TPK`]: ```rust extern crate sequoia_openpgp as openpgp; +use openpgp::parse::Parse; const KEY: &str = "-----BEGIN PGP PUBLIC KEY BLOCK----- @@ -71,6 +72,7 @@ structure using this grammar: ```rust extern crate sequoia_openpgp as openpgp; +use openpgp::parse::Parse; const MESSAGE: &str = "-----BEGIN PGP MESSAGE----- @@ -105,6 +107,7 @@ turned into a vector of [`Packet`]s: ```rust extern crate sequoia_openpgp as openpgp; +use openpgp::parse::Parse; const MESSAGE: &str = "-----BEGIN PGP MESSAGE----- diff --git a/net/src/async.rs b/net/src/async.rs index 9cd298b6..df82a7aa 100644 --- a/net/src/async.rs +++ b/net/src/async.rs @@ -18,6 +18,7 @@ use url::Url; use openpgp::TPK; use openpgp::{KeyID, armor, serialize::Serialize}; +use openpgp::parse::Parse; use sequoia_core::{Context, NetworkPolicy}; use super::{Error, Result}; diff --git a/net/tests/hkp.rs b/net/tests/hkp.rs index fc960eed..cf8e27b8 100644 --- a/net/tests/hkp.rs +++ b/net/tests/hkp.rs @@ -25,6 +25,7 @@ extern crate sequoia_net; use openpgp::armor::Reader; use openpgp::TPK; use openpgp::{Fingerprint, KeyID}; +use openpgp::parse::Parse; use sequoia_core::{Context, NetworkPolicy}; use sequoia_net::KeyServer; diff --git a/openpgp/examples/decrypt-with.rs b/openpgp/examples/decrypt-with.rs index bd8a2b13..c273a202 100644 --- a/openpgp/examples/decrypt-with.rs +++ b/openpgp/examples/decrypt-with.rs @@ -9,8 +9,15 @@ extern crate failure; extern crate sequoia_openpgp as openpgp; use openpgp::packet::key::SecretKey; -use openpgp::parse::stream::{ - Decryptor, DecryptionHelper, Secret, VerificationHelper, VerificationResult, +use openpgp::parse::{ + Parse, + stream::{ + DecryptionHelper, + Decryptor, + Secret, + VerificationHelper, + VerificationResult, + }, }; pub fn main() { diff --git a/openpgp/examples/encrypt-for.rs b/openpgp/examples/encrypt-for.rs index 18abdb79..51f5a981 100644 --- a/openpgp/examples/encrypt-for.rs +++ b/openpgp/examples/encrypt-for.rs @@ -7,6 +7,7 @@ use std::io; extern crate sequoia_openpgp as openpgp; use openpgp::armor; use openpgp::constants::DataFormat; +use openpgp::parse::Parse; use openpgp::serialize::stream::{ Message, LiteralWriter, Encryptor, EncryptionMode, }; diff --git a/openpgp/examples/notarize.rs b/openpgp/examples/notarize.rs index 3138f478..49cb2069 100644 --- a/openpgp/examples/notarize.rs +++ b/openpgp/examples/notarize.rs @@ -9,7 +9,7 @@ use openpgp::{ armor, Packet, constants::DataFormat, - parse::PacketParserResult, + parse::{Parse, PacketParserResult}, serialize::Serialize, }; use openpgp::serialize::stream::{Message, LiteralWriter, Signer}; diff --git a/openpgp/examples/sign-detached.rs b/openpgp/examples/sign-detached.rs index 5eda3ba5..46c0df1d 100644 --- a/openpgp/examples/sign-detached.rs +++ b/openpgp/examples/sign-detached.rs @@ -5,6 +5,7 @@ use std::io; extern crate sequoia_openpgp as openpgp; use openpgp::armor; +use openpgp::parse::Parse; use openpgp::serialize::stream::{Message, Signer}; fn main() { diff --git a/openpgp/examples/sign.rs b/openpgp/examples/sign.rs index 83e47c07..961c9597 100644 --- a/openpgp/examples/sign.rs +++ b/openpgp/examples/sign.rs @@ -6,6 +6,7 @@ use std::io; extern crate sequoia_openpgp as openpgp; use openpgp::armor; use openpgp::constants::DataFormat; +use openpgp::parse::Parse; use openpgp::serialize::stream::{Message, LiteralWriter, Signer}; fn main() { diff --git a/openpgp/examples/statistics.rs b/openpgp/examples/statistics.rs index ae650aed..0087a05f 100644 --- a/openpgp/examples/statistics.rs +++ b/openpgp/examples/statistics.rs @@ -11,7 +11,7 @@ extern crate sequoia_openpgp as openpgp; use openpgp::Packet; use openpgp::constants::SignatureType; use openpgp::packet::{BodyLength, Tag}; -use openpgp::parse::{PacketParserResult, PacketParser}; +use openpgp::parse::{Parse, PacketParserResult, PacketParser}; fn main() { let args: Vec<String> = env::args().collect(); diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs index 772f3dcd..0120822c 100644 --- a/openpgp/src/armor.rs +++ b/openpgp/src/armor.rs @@ -388,6 +388,7 @@ impl<'a> Reader<'a> { /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::{Result, Message}; /// # use openpgp::armor::Reader; + /// # use openpgp::parse::Parse; /// # use std::io; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { diff --git a/openpgp/src/autocrypt.rs b/openpgp/src/autocrypt.rs index c5617f83..8367d62a 100644 --- a/openpgp/src/autocrypt.rs +++ b/openpgp/src/autocrypt.rs @@ -28,6 +28,7 @@ use packet::SKESK; use TPK; use TSK; use parse::{ + Parse, PacketParserResult, PacketParser, }; use serialize::Serialize; diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs index da0dd4b8..d0494853 100644 --- a/openpgp/src/crypto/hash.rs +++ b/openpgp/src/crypto/hash.rs @@ -347,6 +347,7 @@ impl Signature { mod test { use super::*; use TPK; + use parse::Parse; macro_rules! bytes { ( $x:expr ) => { include_bytes!(concat!("../../tests/data/keys/", $x)) }; diff --git a/openpgp/src/crypto/mpis.rs b/openpgp/src/crypto/mpis.rs index 4a69b99f..c7552aac 100644 --- a/openpgp/src/crypto/mpis.rs +++ b/openpgp/src/crypto/mpis.rs @@ -707,15 +707,14 @@ impl Arbitrary for Signature { #[cfg(test)] mod tests { use super::*; + use parse::Parse; + use serialize::Serialize; quickcheck! { fn mpi_roundtrip(mpi: MPI) -> bool { - use std::io::Cursor; - use serialize::Serialize; - let mut buf = Vec::new(); mpi.serialize(&mut buf).unwrap(); - MPI::parse_naked(Cursor::new(buf)).unwrap() == mpi + MPI::from_bytes(&buf).unwrap() == mpi } } diff --git a/openpgp/src/crypto/s2k.rs b/openpgp/src/crypto/s2k.rs index 1c2a5cf2..98aacf69 100644 --- a/openpgp/src/crypto/s2k.rs +++ b/openpgp/src/crypto/s2k.rs @@ -262,7 +262,7 @@ mod tests { use conversions::to_hex; use SymmetricAlgorithm; use Packet; - use parse::PacketParser; + use parse::{Parse, PacketParser}; use serialize::Serialize; use std::path::PathBuf; @@ -413,7 +413,7 @@ mod tests { assert_eq!(buf.len(), l); let mut r = Cursor::new(buf.into_boxed_slice()); - let s = S2K::parse_naked(&mut r).unwrap(); + let s = S2K::from_reader(&mut r).unwrap(); eprintln!("out {:?}", s); s2k == s diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index 1137a72d..a81ec457 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -374,7 +374,7 @@ pub struct PacketPile { /// ```rust /// # extern crate sequoia_openpgp as openpgp; /// # use openpgp::Result; -/// # use openpgp::parse::{PacketParserResult, PacketParser}; +/// # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; /// use openpgp::TPK; /// /// # fn main() { f().unwrap(); } diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index b1019183..957dcbde 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -22,8 +22,8 @@ use Packet; use PacketPile; use Message; use packet::Literal; - use packet::Tag; +use parse::Parse; mod lexer; mod grammar; @@ -289,6 +289,35 @@ impl fmt::Debug for Message { } } +impl<'a> Parse<'a, Message> for Message { + /// Reads a `Message` from the specified reader. + /// + /// See [`Message::from_packet_pile`] for more details. + /// + /// [`Message::from_packet_pile`]: #method.from_packet_pile + fn from_reader<R: 'a + io::Read>(reader: R) -> Result<Self> { + Self::from_packet_pile(PacketPile::from_reader(reader)?) + } + + /// Reads a `Message` from the specified file. + /// + /// See [`Message::from_packet_pile`] for more details. + /// + /// [`Message::from_packet_pile`]: #method.from_packet_pile + fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> { + Self::from_packet_pile(PacketPile::from_file(path)?) + } + + /// Reads a `Message` from `buf`. + /// + /// See [`Message::from_packet_pile`] for more details. + /// + /// [`Message::from_packet_pile`]: #method.from_packet_pile + fn from_bytes(buf: &'a [u8]) -> Result<Self> { + Self::from_packet_pile(PacketPile::from_bytes(buf)?) + } +} + impl Message { /// Converts the `PacketPile` to a `Message`. /// @@ -342,33 +371,6 @@ impl Message { Self::from_packet_pile(PacketPile::from_packets(packets)) } - /// Reads a `Message` from the specified reader. - /// - /// See [`Message::from_packet_pile`] for more details. - /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile - pub fn from_reader<R: io::Read>(reader: R) -> Result<Self> { - Self::from_packet_pile(PacketPile::from_reader(reader)?) - } - - /// Reads a `Message` from the specified file. - /// - /// See [`Message::from_packet_pile`] for more details. - /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile - pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> { - Self::from_packet_pile(PacketPile::from_file(path)?) - } - - /// Reads a `Message` from `buf`. - /// - /// See [`Message::from_packet_pile`] for more details. - /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile - pub fn from_bytes(buf: &[u8]) -> Result<Self> { - Self::from_packet_pile(PacketPile::from_bytes(buf)?) - } - /// Converts the `Message` to a `PacketPile`. pub fn to_packet_pile(self) -> PacketPile { self.pile diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 6265f9cb..98b064d0 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -326,6 +326,7 @@ mod tests { use super::*; use PacketPile; use serialize::SerializeKey; + use parse::Parse; fn path_to(artifact: &str) -> PathBuf { [env!("CARGO_MANIFEST_DIR"), "tests", "data", "keys", artifact] diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index 03708c7d..2491ffdd 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -552,7 +552,7 @@ impl<'a> Iterator for PacketPathIter<'a> { #[test] fn packet_path_iter() { use std::path::PathBuf; - + use parse::Parse; use PacketPile; fn path_to(artifact: &str) -> PathBuf { diff --git a/openpgp/src/packet/pkesk.rs b/openpgp/src/packet/pkesk.rs index 7c9847d8..41f1a544 100644 --- a/openpgp/src/packet/pkesk.rs +++ b/openpgp/src/packet/pkesk.rs @@ -227,6 +227,7 @@ mod tests { use packet::key::SecretKey; use Packet; use std::path::PathBuf; + use parse::Parse; fn path_to_key(artifact: &str) -> PathBuf { [env!("CARGO_MANIFEST_DIR"), "tests", "data", "keys", artifact] diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs index 98a9f615..1ce65cdb 100644 --- a/openpgp/src/packet/signature/mod.rs +++ b/openpgp/src/packet/signature/mod.rs @@ -879,6 +879,7 @@ impl From<Signature> for Packet { mod test { use super::*; use TPK; + use parse::Parse; #[cfg(feature = "compression-deflate")] #[test] diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index f88f3862..daaa0861 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -34,7 +34,7 @@ //! # extern crate sequoia_openpgp as openpgp; //! # use openpgp::Result; //! # use openpgp::Packet; -//! # use openpgp::parse::{PacketParserResult, PacketParser}; +//! # use openpgp::parse::{Parse, PacketParserResult, PacketParser}; //! # //! # f(include_bytes!("../../../tests/data/messages/signed.gpg")); //! # @@ -2472,6 +2472,7 @@ fn accessors() { #[test] fn subpacket_test_1 () { use PacketPile; + use parse::Parse; let path = path_to("signed.gpg"); let pile = PacketPile::from_file(&path).unwrap(); @@ -2525,6 +2526,7 @@ fn subpacket_test_1 () { #[test] fn subpacket_test_2() { use conversions::Time; + use parse::Parse; use PacketPile; // Test # Subpacket diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs index e307b823..3c40abc7 100644 --- a/openpgp/src/pack |