diff options
-rw-r--r-- | net/src/async.rs | 2 | ||||
-rw-r--r-- | openpgp/src/autocrypt.rs | 1 | ||||
-rw-r--r-- | openpgp/src/tpk/mod.rs | 134 | ||||
-rw-r--r-- | openpgp/src/tsk.rs | 4 | ||||
-rw-r--r-- | store/src/backend/mod.rs | 1 | ||||
-rw-r--r-- | store/src/lib.rs | 1 | ||||
-rw-r--r-- | tool/src/sq.rs | 1 |
7 files changed, 76 insertions, 68 deletions
diff --git a/net/src/async.rs b/net/src/async.rs index 77ec00bc..b3a5f7a3 100644 --- a/net/src/async.rs +++ b/net/src/async.rs @@ -17,7 +17,7 @@ use tokio_core::reactor::Handle; use url::Url; use openpgp::TPK; -use openpgp::{KeyID, armor}; +use openpgp::{KeyID, armor, serialize::Serialize}; use sequoia_core::{Context, NetworkPolicy}; use super::{Error, Result}; diff --git a/openpgp/src/autocrypt.rs b/openpgp/src/autocrypt.rs index 6e20d099..ff75ab96 100644 --- a/openpgp/src/autocrypt.rs +++ b/openpgp/src/autocrypt.rs @@ -30,6 +30,7 @@ use TSK; use parse::{ PacketParserResult, PacketParser, }; +use serialize::Serialize; use serialize::stream::{ Message, LiteralWriter, Encryptor, EncryptionMode, }; diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs index d45760f4..52a7ff47 100644 --- a/openpgp/src/tpk/mod.rs +++ b/openpgp/src/tpk/mod.rs @@ -2163,8 +2163,75 @@ impl TPK { PacketPile::from_packets(self.to_packets()) } + /// Merges `other` into `self`. + /// + /// If `other` is a different key, then nothing is merged into + /// `self`, but `self` is still canonicalized. + pub fn merge(mut self, mut other: TPK) -> Result<Self> { + if self.primary != other.primary { + // The primary key is not the same. There is nothing to + // do. + return Ok(self.canonicalize()); + } + + self.primary_selfsigs.append( + &mut other.primary_selfsigs); + self.primary_certifications.append( + &mut other.primary_certifications); + self.primary_self_revocations.append( + &mut other.primary_self_revocations); + self.primary_other_revocations.append( + &mut other.primary_other_revocations); + + self.userids.append(&mut other.userids); + self.user_attributes.append(&mut other.user_attributes); + self.subkeys.append(&mut other.subkeys); + self.bad.append(&mut other.bad); + + Ok(self.canonicalize()) + } + + /// Adds packets to the TPK. + /// + /// This recanonicalizes the TPK. If the packets are invalid, + /// they are dropped. + pub fn merge_packets(self, packets: &[ Packet ]) -> Result<Self> { + let mut combined = self.to_packets(); + combined.extend_from_slice(packets); + TPK::from_packet_pile(PacketPile::from_packets(combined)) + } + + /// Cast the public key into a secret key that allows using the secret + /// parts of the containing keys. + pub fn into_tsk(self) -> TSK { + TSK::from_tpk(self) + } + + /// Cast the public key into a secret key that allows using the secret + /// parts of the containing keys. Only packets for which `filter` returns + /// true are included in the TSK. + pub fn filter_into_tsk<F: Fn(&Packet) -> bool>(self, f: F) -> Result<TSK> { + let pkts = self.to_packet_pile().into_children().filter(f).collect::<Vec<_>>(); + let pile = PacketPile::from_packets(pkts); + + Ok(TSK::from_tpk(TPK::from_packet_pile(pile)?)) + } + + /// Returns whether at least one of the keys includes a secret + /// part. + pub fn is_tsk(&self) -> bool { + if self.primary().secret().is_some() { + return true; + } + self.subkeys().any(|sk| { + sk.binding_signature().is_some() && sk.subkey().secret().is_some() + }) + } +} + +impl Serialize for TPK { /// Serializes the TPK. - pub fn serialize<W: io::Write>(&self, o: &mut W) -> Result<()> { + fn serialize<W: io::Write>(&self, o: &mut W) -> Result<()> { self.primary.serialize(o, Tag::PublicKey)?; for s in self.primary_selfsigs.iter() { @@ -2229,71 +2296,6 @@ impl TPK { } Ok(()) } - - /// Merges `other` into `self`. - /// - /// If `other` is a different key, then nothing is merged into - /// `self`, but `self` is still canonicalized. - pub fn merge(mut self, mut other: TPK) -> Result<Self> { - if self.primary != other.primary { - // The primary key is not the same. There is nothing to - // do. - return Ok(self.canonicalize()); - } - - self.primary_selfsigs.append( - &mut other.primary_selfsigs); - self.primary_certifications.append( - &mut other.primary_certifications); - self.primary_self_revocations.append( - &mut other.primary_self_revocations); - self.primary_other_revocations.append( - &mut other.primary_other_revocations); - - self.userids.append(&mut other.userids); - self.user_attributes.append(&mut other.user_attributes); - self.subkeys.append(&mut other.subkeys); - self.bad.append(&mut other.bad); - - Ok(self.canonicalize()) - } - - /// Adds packets to the TPK. - /// - /// This recanonicalizes the TPK. If the packets are invalid, - /// they are dropped. - pub fn merge_packets(self, packets: &[ Packet ]) -> Result<Self> { - let mut combined = self.to_packets(); - combined.extend_from_slice(packets); - TPK::from_packet_pile(PacketPile::from_packets(combined)) - } - - /// Cast the public key into a secret key that allows using the secret - /// parts of the containing keys. - pub fn into_tsk(self) -> TSK { - TSK::from_tpk(self) - } - - /// Cast the public key into a secret key that allows using the secret - /// parts of the containing keys. Only packets for which `filter` returns - /// true are included in the TSK. - pub fn filter_into_tsk<F: Fn(&Packet) -> bool>(self, f: F) -> Result<TSK> { - let pkts = self.to_packet_pile().into_children().filter(f).collect::<Vec<_>>(); - let pile = PacketPile::from_packets(pkts); - - Ok(TSK::from_tpk(TPK::from_packet_pile(pile)?)) - } - - /// Returns whether at least one of the keys includes a secret - /// part. - pub fn is_tsk(&self) -> bool { - if self.primary().secret().is_some() { - return true; - } - self.subkeys().any(|sk| { - sk.binding_signature().is_some() && sk.subkey().secret().is_some() - }) - } } #[cfg(test)] diff --git a/openpgp/src/tsk.rs b/openpgp/src/tsk.rs index 820b35cb..53b0ff0c 100644 --- a/openpgp/src/tsk.rs +++ b/openpgp/src/tsk.rs @@ -89,9 +89,11 @@ impl TSK { pub fn into_tpk(self) -> TPK { self.key } +} +impl Serialize for TSK { /// Serializes the TSK. - pub fn serialize<W: io::Write>(&self, o: &mut W) -> Result<()> { + fn serialize<W: io::Write>(&self, o: &mut W) -> Result<()> { self.key.primary.serialize(o, Tag::SecretKey)?; for s in self.key.primary_selfsigs.iter() { diff --git a/store/src/backend/mod.rs b/store/src/backend/mod.rs index f46c1ff9..377f2c80 100644 --- a/store/src/backend/mod.rs +++ b/store/src/backend/mod.rs @@ -22,6 +22,7 @@ use tokio_core; use tokio_io::io::ReadHalf; use openpgp::{self, TPK, KeyID, Fingerprint}; +use openpgp::serialize::Serialize; use sequoia_core as core; use sequoia_net as net; use sequoia_net::ipc; diff --git a/store/src/lib.rs b/store/src/lib.rs index a7f6d242..ca15daee 100644 --- a/store/src/lib.rs +++ b/store/src/lib.rs @@ -81,6 +81,7 @@ extern crate sequoia_net; use openpgp::Fingerprint; use openpgp::KeyID; use openpgp::TPK; +use openpgp::serialize::Serialize; use sequoia_core as core; use sequoia_core::Context; use sequoia_net::ipc; diff --git a/tool/src/sq.rs b/tool/src/sq.rs index 52a63d28..e54b690d 100644 --- a/tool/src/sq.rs +++ b/tool/src/sq.rs @@ -21,6 +21,7 @@ extern crate sequoia_net; extern crate sequoia_store; use openpgp::{armor, autocrypt, Fingerprint, TPK}; +use openpgp::serialize::Serialize; use sequoia_core::{Context, NetworkPolicy}; use sequoia_net::KeyServer; use sequoia_store::{Store, LogIter}; |