summaryrefslogtreecommitdiffstats
path: root/openpgp/src/parse
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-05-21 17:50:22 +0200
committerJustus Winter <justus@sequoia-pgp.org>2019-05-22 15:45:29 +0200
commit387ac1cc1477d37003e659c3183b81300afcb2c3 (patch)
tree8d7180c9e3b776147feacbc06dc475455016afac /openpgp/src/parse
parent5d59509fd7e09f19eb5d21ec94b7905ac30af562 (diff)
openpgp: Trait Serialize/Parse cleanup.
- Currently, when we serialize a packet structure, like Signature, we get a full packet with CTB and length, even though we didn't really ask for that. If we want to create an embedded signature, we need to use the special interface Signature::serialize_naked() to get it without frame. - Also consider Key. Here, we don't know whether it is supposed to be primary or subkey, or public or secret. Therefore, we have SerializeKey, which is like Serialize, but also gets a tag. Now, if Key::serialize() would only emit the body, it wouldn't need to know what kind of key to emit. - The same applies to trait Parse. If we use, say, Signature::from_bytes(), the parser expects a framed signature. If we want to parse an embedded signature, we need to use a special interface again. - This patch changes how we parse and serialize packet structures to not expect or emit the frame. If we want to include the frame, we need to explicitly wrap it into an enum Packet. - This patch does not include any cleanups and optimizations to keep the size manageable. - See #255.
Diffstat (limited to 'openpgp/src/parse')
-rw-r--r--openpgp/src/parse/parse.rs36
-rw-r--r--openpgp/src/parse/stream.rs6
2 files changed, 17 insertions, 25 deletions
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index 736e3b08..23f86730 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -100,18 +100,14 @@ macro_rules! impl_parse_generic_packet {
($typ: ident) => {
impl<'a> Parse<'a, $typ> for $typ {
fn from_reader<R: 'a + Read>(reader: R) -> Result<Self> {
- let ppr = PacketParserBuilder::from_reader(reader)?
- .buffer_unread_content().finalize()?;
- let (p, ppr) = match ppr {
- PacketParserResult::Some(mut pp) => {
- pp.next()?
- },
- PacketParserResult::EOF(_) =>
- return Err(Error::InvalidOperation(
- "Unexpected EOF".into()).into()),
- };
+ let bio = buffered_reader::Generic::with_cookie(
+ reader, None, Cookie::default());
+ let parser = PacketHeaderParser::new_naked(Box::new(bio));
+
+ let mut pp = Self::parse(parser)?;
+ pp.buffer_unread_content()?;
- match (p, ppr) {
+ match pp.next()? {
(Packet::$typ(o), PacketParserResult::EOF(_))
=> Ok(o),
(p, PacketParserResult::EOF(_)) =>
@@ -1462,18 +1458,14 @@ impl Key4 {
impl<'a> Parse<'a, Key> for Key {
fn from_reader<R: 'a + Read>(reader: R) -> Result<Self> {
- let ppr = PacketParserBuilder::from_reader(reader)?
- .buffer_unread_content().finalize()?;
- let (p, ppr) = match ppr {
- PacketParserResult::Some(mut pp) => {
- pp.next()?
- },
- PacketParserResult::EOF(_) =>
- return Err(Error::InvalidOperation(
- "Unexpected EOF".into()).into()),
- };
+ let bio = buffered_reader::Generic::with_cookie(
+ reader, None, Cookie::default());
+ let parser = PacketHeaderParser::new_naked(Box::new(bio));
- match (p, ppr) {
+ let mut pp = Self::parse(parser)?;
+ pp.buffer_unread_content()?;
+
+ match pp.next()? {
(Packet::PublicKey(o), PacketParserResult::EOF(_))
| (Packet::PublicSubkey(o), PacketParserResult::EOF(_))
| (Packet::SecretKey(o), PacketParserResult::EOF(_))
diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs
index 8c0a70bc..6a379687 100644
--- a/openpgp/src/parse/stream.rs
+++ b/openpgp/src/parse/stream.rs
@@ -748,7 +748,7 @@ impl<'a> Transformer<'a> {
ops.set_last(true);
}
- ops.serialize(&mut buf)?;
+ Packet::OnePassSig(ops.into()).serialize(&mut buf)?;
}
// We need to decide whether to use partial body encoding or
@@ -845,8 +845,8 @@ impl<'a> Transformer<'a> {
},
TransformationState::Sigs => {
- for sig in self.sigs.iter() {
- sig.serialize(&mut self.buffer)?;
+ for sig in self.sigs.drain(..) {
+ Packet::Signature(sig).serialize(&mut self.buffer)?;
}
TransformationState::Done