use std::fmt;
use std::ops::{Deref, DerefMut};
use std::slice;
use crate::Error;
use crate::Result;
use crate::Packet;
#[macro_use]
mod container;
pub(crate) use container::Container;
pub mod prelude;
use crate::crypto::KeyPair;
mod tag;
pub use self::tag::Tag;
pub mod header;
pub use self::header::Header;
mod unknown;
pub use self::unknown::Unknown;
pub mod signature;
pub mod one_pass_sig;
pub mod key;
mod marker;
pub use self::marker::Marker;
mod trust;
pub use self::trust::Trust;
mod userid;
pub use self::userid::UserID;
pub mod user_attribute;
pub use self::user_attribute::UserAttribute;
mod literal;
pub use self::literal::Literal;
mod compressed_data;
pub use self::compressed_data::CompressedData;
pub mod seip;
pub mod skesk;
pub mod pkesk;
mod mdc;
pub use self::mdc::MDC;
pub mod aed;
impl<'a> Deref for Packet {
type Target = Common;
fn deref(&self) -> &Self::Target {
match self {
&Packet::Unknown(ref packet) => &packet.common,
&Packet::Signature(Signature::V4(ref packet)) => &packet.common,
&Packet::OnePassSig(ref packet) => &packet.common,
&Packet::PublicKey(ref packet) => &packet.common,
&Packet::PublicSubkey(ref packet) => &packet.common,
&Packet::SecretKey(ref packet) => &packet.common,
&Packet::SecretSubkey(ref packet) => &packet.common,
&Packet::Marker(ref packet) => &packet.common,
&Packet::Trust(ref packet) => &packet.common,
&Packet::UserID(ref packet) => &packet.common,
&Packet::UserAttribute(ref packet) => &packet.common,
&Packet::Literal(ref packet) => &packet.common,
&Packet::CompressedData(ref packet) => &packet.common,
&Packet::PKESK(ref packet) => &packet.common,
&Packet::SKESK(SKESK::V4(ref packet)) => &packet.common,
&Packet::SKESK(SKESK::V5(ref packet)) => &packet.skesk4.common,
&Packet::SEIP(ref packet) => &packet.common,
&Packet::MDC(ref packet) => &packet.common,
&Packet::AED(AED::V1(ref packet)) => &packet.common,
}
}
}
impl<'a> DerefMut for Packet {
fn deref_mut(&mut self) -> &mut Common {
match self {
&mut Packet::Unknown(ref mut packet) => &mut packet.common,
&mut Packet::Signature(Signature::V4(ref mut packet)) =>
&mut packet.common,
&mut Packet::OnePassSig(ref mut packet) => &mut packet.common,
&mut Packet::PublicKey(ref mut packet) => &mut packet.common,
&mut Packet::PublicSubkey(ref mut packet) => &mut packet.common,
&mut Packet::SecretKey(ref mut packet) => &mut packet.common,
&mut Packet::SecretSubkey(ref mut packet) => &mut packet.common,
&mut Packet::Marker(ref mut packet) => &mut packet.common,
&mut Packet::Trust(ref mut packet) => &mut packet.common,
&mut Packet::UserID(ref mut packet) => &mut packet.common,
&mut Packet::UserAttribute(ref mut packet) => &mut packet.common,
&mut Packet::Literal(ref mut packet) => &mut packet.common,
&mut Packet::CompressedData(ref mut packet) => &mut packet.common,
&mut Packet::PKESK(ref mut packet) => &mut packet.common,
&mut Packet::SKESK(SKESK::V4(ref mut packet)) => &mut packet.common,
&mut Packet::SKESK(SKESK::V5(ref mut packet)) => &mut packet.skesk4.common,
&mut Packet::SEIP(ref mut packet) => &mut packet.common,
&mut Packet::MDC(ref mut packet) => &mut packet.common,
&mut Packet::AED(AED::V1(ref mut packet)) => &mut packet.common,
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Common {
}
impl Default for Common {
fn default() -> Common {
Common {
}
}
}
pub struct Iter<'a> {
children: slice::Iter<'a, Packet>,
child: Option<&'a Packet>,
grandchildren: Option<Box<Iter<'a>>>,
depth: usize,
}
impl<'a> Default for Iter<'a> {
fn default() -> Self {
Iter {
children: [].iter(),
child: None,
grandchildren: None,
depth: 0,
}
}
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a Packet;
fn next(&mut self) -> Option<Self::Item> {
if let Some(ref mut grandchildren) = self.grandchildren {
let grandchild = grandchildren.next();
if grandchild.is_some() {
self.depth = grandchildren.depth + 1;
return grandchild;
}
}
self.child = self.children.next();
if let Some(child) = self.child {
self.grandchildren = Some(Box::new(child.descendants()));
}
self.depth = 0;
return self.child;
}
}
impl<'a> Iter<'a> {
pub fn paths(self) -> impl Iterator<Item = (Vec<usize>, &'a Packet)> {
PacketPathIter {
iter: self,
path: None,
}
}
}
struct PacketPathIter<'a> {
iter: Iter<'a>,
path: Option<Vec<usize>>,
}
impl<'a> Iterator for PacketPathIter<'a> {
type Item = (Vec<usize>, &'a Packet);
fn next(&mut self) -> Option<Self::Item> {
if let Some(packet) = self.iter.next() {
if self.path.is_none() {
let mut path = Vec::with_capacity(4);
path.push(0);
self.path = Some(path);
} else {
let mut path = self.path.take().unwrap();
let old_depth = path.len() - 1;
let depth = self.iter.depth;
if old_depth > depth {
path.truncate(depth + 1);
path[depth] += 1;
} else if old_depth == depth {
path[old_depth] += 1;
} else if old_depth + 1 == depth {
path.push(0);
}
self.path = Some(path);
}
Some((self.path.as_ref().unwrap().clone(), packet))
} else {
None
}
}
}
#[test]
fn packet_path_iter() {
use crate::parse::Parse;
use crate::PacketPile;
fn paths(iter: slice::Iter<Packet>) -> Vec<Vec<usize>> {
let mut lpaths : Vec<Vec<usize>> = Vec::new();
for (i, packet) in iter.enumerate() {
let mut v = Vec::new();
v.push(i);
lpaths.push(v);
if let Some(ref container) = packet.container_ref() {
for mut path in paths(container.children()).into_iter() {
path.insert(0, i);
lpaths.push(path);
}
}
}
lpaths
}
for i in 1..5 {
let pile = PacketPile::from_bytes(
crate::tests::message(&format!("recursive-{}.gpg", i)[..])).unwrap();
let mut paths1 : Vec<Vec<usize>> = Vec::new();
for path in paths(pile.children()).iter() {
paths1.push(path.clone());
}
let mut paths2 : Vec<Vec<usize>> = Vec::new();
for (path, packet) in pile.descendants().paths() {
assert_eq!(Some(packet), pile.path_ref(&path[..]));
paths2.push(path);
}
if paths1 != paths2 {
eprintln!("PacketPile:");
pile.pretty_print();
eprintln!("Expected paths:");
for p in paths1 {
eprintln!(" {:?}", p);
}
eprintln!("Got paths:");
for p in paths2 {
eprintln!(" {:?}", p);
}
panic!("Something is broken. Don't panic.");
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum Signature {
V4(self::signature::Signature4),
}
impl Signature {
pub fn version(&self) -> u8 {
match self {
&Signature::V4(_) => 4,
}
}
}
impl From<Signature> for Packet {
fn from(s: Signature) -> Self {
Packet::Signature(s)
}
}
impl Deref for Signature {
type Target = signature::Signature4;
fn deref(&self) -> &Self::Target {
match self {
Signature::V4(sig) => sig,
}
}
}
impl DerefMut for Signature {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Signature::V4(ref mut sig) => sig,
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum OnePassSig {
V3(self::one_pass_sig::OnePassSig3),
}
impl OnePassSig {
pub fn version(&self) -> u8 {
match self {
&OnePassSig::V3(_) => 3,
}
}
}
impl From<OnePassSig> for Packet {
fn from(s: OnePassSig) -> Self {
Packet::OnePassSig(s)
}
}
impl Deref for OnePassSig {
type Target = one_pass_sig::OnePassSig3;
fn deref(&self) -> &Self::Target {
match self {
OnePassSig::V3(ops) => ops,
}
}
}
impl DerefMut for OnePassSig {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
OnePassSig::V3(ref mut ops) => ops,
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum PKESK {
V3(self::pkesk::PKESK3),
}
impl PKESK {
pub fn version(&self) -> u8 {
match self {
PKESK::V3(_) => 3,
}
}
}
impl From<PKESK> for Packet {
fn from(p: PKESK) -> Self {
Packet::PKESK(p)
}
}
impl Deref for PKESK {
type Target = self::pkesk::PKESK3;
fn deref(&self) -> &Self::Target {
match self {
PKESK::V3(ref p) => p,
}
}
}
impl DerefMut for PKESK {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
PKESK::V3(ref mut p) => p,
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum SKESK {
V4(self::skesk::SKESK4),
V5(self::skesk::SKESK5),
}
impl SKESK {
pub fn version(&self) -> u8 {
match self {
&SKESK::V4(_) => 4,
&SKESK::V5(_) => 5,
}
}
}
impl From<SKESK> for Packet {
fn from(p: SKESK) -> Self {
Packet::SKESK(p)
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum Key<P: key::KeyParts, R: key::KeyRole> {
V4(self::key::Key4<P, R>),
}
impl<P: key::KeyParts, R: key::KeyRole> fmt::Display for Key<P, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Key::V4(k) => k.fmt(f),
}
}
}
impl<P: key::KeyParts, R: key::KeyRole> Key<P, R> {
pub fn version(&self) -> u8 {
match self {
Key::V4(_) => 4,
}
}
pub fn public_cmp<PB, RB>(&self, b: &Key<PB, RB>) -> std::cmp::Ordering
where PB: key::KeyParts,
RB: key::KeyRole,
{
match (self, b) {
(Key::V4(a), Key::V4(b)) => a.public_cmp(b),
}
}
}
impl From<Key<key::PublicParts, key::PrimaryRole>> for Packet {
fn from(k: Key<key::PublicParts, key::PrimaryRole>) -> Self {
Packet::PublicKey(k.into())
}
}
impl From<Key<key::PublicParts, key::SubordinateRole>> for Packet {
fn from(k: Key<key::PublicParts, key::SubordinateRole>) -> Self {
Packet::PublicSubkey(k.into())
}
}
impl From<Key<key::SecretParts, key::PrimaryRole>> for Packet {
fn from(k: Key<key::SecretParts, key::PrimaryRole>) -> Self {
Packet::SecretKey(k.into())
}
}
impl From<Key<key::SecretParts, key::SubordinateRole>> for Packet {
fn from(k: Key<key::SecretParts, key::SubordinateRole>) -> Self {
Packet::SecretSubkey(k.into())
}
}
impl<R: key::KeyRole> Key<key::SecretParts, R> {
pub fn into_keypair(mut self) -> Result<KeyPair> {
use crate::packet::key::SecretKeyMaterial;
let secret = match self.set_secret(None) {
Some(SecretKeyMaterial::Unencrypted(secret)) => secret,
Some(SecretKeyMaterial::Encrypted(_)) =>
return Err(Error::InvalidArgument(
"secret key is encrypted".into()).into()),
None =>
return Err(Error::InvalidArgument(
"no secret key".into()).into()),
};
KeyPair::new(self.mark_role_unspecified().into(), secret)
}
}
impl<R: key::KeyRole> key::Key4<key::SecretParts, R> {
pub fn into_keypair(mut self) -> Result<KeyPair> {
use crate::packet::key::SecretKeyMaterial;
let secret = match self.set_secret(None) {
Some(SecretKeyMaterial::Unencrypted(secret)) => secret,
Some(SecretKeyMaterial::Encrypted(_)) =>
return Err(Error::InvalidArgument(
"secret key is encrypted".into()).into()),
None =>
return Err(Error::InvalidArgument(
"no secret key".into()).into()),
};
KeyPair::new(self.mark_role_unspecified().mark_parts_public().into(),
secret)
}
}
impl<P: key::KeyParts, R: key::KeyRole> Deref for Key<P, R> {
type Target = self::key::Key4<P, R>;
fn deref(&self) -> &Self::Target {
match self {
Key::V4(ref p) => p,
}
}
}
impl<P: key::KeyParts, R: key::KeyRole> DerefMut for Key<P, R> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
Key::V4(ref mut p) => p,
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum SEIP {
V1(self::seip::SEIP1),
}
impl SEIP {
pub fn version(&self) -> u8 {
match self {
SEIP::V1(_) => 1,
}
}
}
impl From<SEIP> for Packet {
fn from(p: SEIP) -> Self {
Packet::SEIP(p)
}
}
impl Deref for SEIP {
type Target = self::seip::SEIP1;
fn deref(&self) -> &Self::Target {
match self {
SEIP::V1(ref p) => p,
}
}
}
impl DerefMut for SEIP {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
SEIP::V1(ref mut p) => p,
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum AED {
V1(self::aed::AED1),
}
impl AED {
pub fn version(&self) -> u8 {
match self {
AED::V1(_) => 1,
}
}
}
impl From<AED> for Packet {
fn from(p: AED) -> Self {
Packet::AED(p)
}
}
impl Deref for AED {
type Target = self::aed::AED1;
fn deref(&self) -> &Self::Target {
match self {
AED::V1(ref p) => p,
}
}
}
impl DerefMut for AED {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
AED::V1(ref mut p) => p,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn packet_is_send_and_sync() {
fn f<T: Send + Sync>(_: T) {}
f(Packet::Marker(Default::default()));
}
}