diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-06-29 11:53:32 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-06-29 11:54:43 +0200 |
commit | 5ed0cf74023d08643e388f614acb608c4a3d9c5e (patch) | |
tree | cc11fee6ac95b5f765e072fda422b273f8a38762 /openpgp | |
parent | ac4d003e6162ee8eed31bb48fa9dd79a8197f2ab (diff) |
openpgp: Fix serializing embedded signatures.
- Embedded signatures must be serialized without the frame.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/serialize/mod.rs | 27 | ||||
-rw-r--r-- | openpgp/src/subpacket.rs | 13 |
2 files changed, 33 insertions, 7 deletions
diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index fd145f27..b336f579 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -463,8 +463,11 @@ impl<'a> Serialize for SubpacketValue<'a> { o.write_all(&[*pk_algo, *hash_algo])?; o.write_all(hash)?; }, - EmbeddedSignature(ref p) => - p.serialize(o)?, + EmbeddedSignature(ref p) => match p { + &Packet::Signature(ref sig) => sig.serialize_naked(o)?, + _ => return Err(Error::InvalidArgument( + format!("Not a signature: {:?}", p)).into()), + }, IssuerFingerprint(ref fp) => match fp { Fingerprint::V4(_) => { o.write_all(&[4])?; @@ -512,6 +515,26 @@ impl Serialize for Signature { CTB::new(Tag::Signature).serialize(o)?; BodyLength::Full(len as u32).serialize(o)?; + self.serialize_naked(o) + } +} + +impl Signature { + /// Writes a serialized version of the specified `Signature` + /// packet without framing to `o`. + /// + /// Note: this function does not compute the signature (which + /// would require access to the private key); it assumes that + /// sig.mpis is up to date. + /// + /// # Errors + /// + /// Returns [`Error::InvalidArgument`] if invoked on a + /// non-version 4 signature, or if either the hashed-area or the + /// unhashed-area exceeds the size limit of 2^16. + /// + /// [`Error::InvalidArgument`]: enum.Error.html#variant.InvalidArgument + pub(crate) fn serialize_naked<W: io::Write>(&self, o: &mut W) -> Result<()> { if self.version != 4 { return Err(Error::InvalidArgument( "Don't know how to serialize \ diff --git a/openpgp/src/subpacket.rs b/openpgp/src/subpacket.rs index 25a6bba8..416d5e91 100644 --- a/openpgp/src/subpacket.rs +++ b/openpgp/src/subpacket.rs @@ -614,11 +614,14 @@ impl<'a> SubpacketValue<'a> { ReasonForRevocation((_, r)) => 1 + r.len(), Features(f) => f.len(), SignatureTarget((_, _, h)) => 1 + 1 + h.len(), - EmbeddedSignature(p) => { - use serialize::Serialize; - let mut w = Vec::new(); - p.serialize(&mut w).unwrap(); - w.len() + EmbeddedSignature(p) => match p { + &Packet::Signature(ref sig) => { + let mut w = Vec::new(); + sig.serialize_naked(&mut w).unwrap(); + w.len() + }, + // Bogus. + _ => 0, }, IssuerFingerprint(ref fp) => match fp { Fingerprint::V4(_) => 1 + 20, |