Struct sequoia_openpgp::PacketPile[][src]

pub struct PacketPile { /* fields omitted */ }

An unstructured packet sequence.

To parse an OpenPGP packet stream into a PacketPile, you can use PacketParser, PacketPileParser, or PacketPile::from_file (or related routines).

You can also convert a Cert into a PacketPile using PacketPile::from. Unlike serializing a Cert, this does not drop any secret key material.

Normally, you’ll want to convert the PacketPile to a Cert or a Message.

Examples

This example shows how to modify packets in PacketPile using pathspecs.

use std::convert::TryFrom;
use openpgp::{Packet, PacketPile};
use openpgp::packet::signature::Signature4;
use openpgp::packet::Signature;
use openpgp::cert::prelude::*;
use openpgp::parse::Parse;
use openpgp::serialize::Serialize;
use openpgp::policy::StandardPolicy;
use openpgp::crypto::mpi;
use openpgp::types::RevocationStatus::{Revoked, CouldBe};

let (cert, revocation) = CertBuilder::new().generate()?;

let mut buffer = Vec::new();
cert.serialize(&mut buffer)?;
let packet: Packet = revocation.into();
packet.serialize(&mut buffer)?;

let policy = &StandardPolicy::new();

// Certificate is considered revoked because it is accompanied with its
// revocation signature
let pp: PacketPile = PacketPile::from_bytes(&buffer)?;
let cert = Cert::try_from(pp)?;
if let Revoked(_) = cert.revocation_status(policy, None) {
    // cert is considered revoked
}

// Breaking the revocation signature changes certificate's status
let mut pp: PacketPile = PacketPile::from_bytes(&buffer)?;
if let Some(Packet::Signature(ref mut sig)) = pp.path_ref_mut(&[2]) {
    *sig = Signature4::new(
        sig.typ(),
        sig.pk_algo(),
        sig.hash_algo(),
        sig.hashed_area().clone(),
        sig.unhashed_area().clone(),
        *sig.digest_prefix(),
        // MPI is replaced with a dummy one
        mpi::Signature::RSA {
            s: mpi::MPI::from(vec![1, 2, 3])
        }).into();
}

let cert = Cert::try_from(pp)?;
if let NotAsFarAsWeKnow = cert.revocation_status(policy, None) {
    // revocation signature is broken and the key is not definitely revoked
    assert_eq!(cert.bad_signatures().count(), 1);
}

Implementations

impl PacketPile[src]

pub fn pretty_print(&self)[src]

Pretty prints the message to stderr.

This function is primarily intended for debugging purposes.

pub fn path_ref(&self, pathspec: &[usize]) -> Option<&Packet>[src]

Returns a reference to the packet at the location described by pathspec.

pathspec is a slice of the form [0, 1, 2]. Each element is the index of packet in a container. Thus, the previous path specification means: return the third child of the second child of the first top-level packet. In other words, the starred packet in the following tree:

        PacketPile
       /     |     \
      0      1      2  ...
    /   \
   /     \
 0         1  ...
       /   |   \  ...
      0    1    2
                *

And, [10] means return the 11th top-level packet.

Note: there is no packet at the root. Thus, the path [] returns None.

Examples

let pile = PacketPile::from(packets);

if let Some(packet) = pile.path_ref(&[0]) {
    // There is a packet at this path.
}

if let None = pile.path_ref(&[0, 1, 2]) {
    // But none here.
}

pub fn path_ref_mut(&mut self, pathspec: &[usize]) -> Option<&mut Packet>[src]

Returns a mutable reference to the packet at the location described by pathspec.

See the description of the path_spec for more details.

Examples

let mut pile = PacketPile::from(packets);

if let Some(ref packet) = pile.path_ref_mut(&[0]) {
    // There is a packet at this path.
}

if let None = pile.path_ref_mut(&[0, 1, 2]) {
    // But none here.
}

pub fn replace(
    &mut self,
    pathspec: &[usize],
    count: usize,
    packets: Vec<Packet>
) -> Result<Vec<Packet>>
[src]

Replaces the specified packets at the location described by pathspec with packets.

If a packet is a container, the sub-tree rooted at the container is removed.

Note: the number of packets to remove need not match the number of packets to insert.

The removed packets are returned.

If the path was invalid, then Error::IndexOutOfRange is returned instead.

Examples

// A compressed data packet that contains a literal data packet.
let mut literal = Literal::new(DataFormat::Text);
literal.set_body(b"old".to_vec());
let mut compressed =
    CompressedData::new(CompressionAlgorithm::Uncompressed);
compressed.children_mut().unwrap().push(literal.into());
let mut pile = PacketPile::from(Packet::from(compressed));

// Replace the literal data packet.
let mut literal = Literal::new(DataFormat::Text);
literal.set_body(b"new".to_vec());
pile.replace(
    &[0, 0], 1,
    [literal.into()].to_vec())?;

pub fn descendants(&self) -> Iter<'_>

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = &'a Packet;
[src]

Returns an iterator over all of the packet’s descendants, in depth-first order.

let mut lit = Literal::new(DataFormat::Text);
lit.set_body(b"test".to_vec());

let pile = PacketPile::from(vec![lit.into()]);

for packet in pile.descendants() {
    assert_eq!(packet.tag(), Tag::Literal);
}

pub fn children(
    &self
) -> impl Iterator<Item = &Packet> + ExactSizeIterator + Send + Sync
[src]

Returns an iterator over the top-level packets.

let mut lit = Literal::new(DataFormat::Text);
lit.set_body(b"test".to_vec());

let pile = PacketPile::from(vec![lit.into()]);

assert_eq!(pile.children().len(), 1);

pub fn into_children(
    self
) -> impl Iterator<Item = Packet> + ExactSizeIterator + Send + Sync
[src]

Returns an IntoIter over the top-level packets.

let mut lit = Literal::new(DataFormat::Text);
lit.set_body(b"test".to_vec());

let pile = PacketPile::from(vec![lit.into()]);

for packet in pile.into_children() {
    assert_eq!(packet.tag(), Tag::Literal);
}

Trait Implementations

impl Clone for PacketPile[src]

fn clone(&self) -> PacketPile[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for PacketPile[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Default for PacketPile[src]

fn default() -> PacketPile[src]

Returns the “default value” for a type. Read more

impl From<Cert> for PacketPile[src]

fn from(cert: Cert) -> PacketPile[src]

Converts the Cert into a PacketPile.

If any packets include secret key material, that secret key material is not dropped, as it is when serializing a Cert.

impl From<Message> for PacketPile[src]

fn from(m: Message) -> Self[src]

Performs the conversion.

impl From<Packet> for PacketPile[src]

fn from(p: Packet) -> Self[src]

Performs the conversion.

impl From<PacketPile> for Vec<Packet>[src]

fn from(pp: PacketPile) -> Self[src]

Performs the conversion.

impl From<Vec<Packet, Global>> for PacketPile[src]

fn from(p: Vec<Packet>) -> Self[src]

Performs the conversion.

impl FromIterator<Packet> for PacketPile[src]

fn from_iter<I: IntoIterator<Item = Packet>>(iter: I) -> Self[src]

Creates a value from an iterator. Read more

impl FromStr for PacketPile[src]

type Err = Error

The associated error which can be returned from parsing.

fn from_str(s: &str) -> Result<Self, Self::Err>[src]

Parses a string s to return a value of this type. Read more

impl Marshal for PacketPile[src]

fn serialize(&self, o: &mut dyn Write) -> Result<()>[src]

Writes a serialized version of the specified PacketPile to o.

fn export(&self, o: &mut dyn Write) -> Result<()>[src]

Exports a serialized version of the specified PacketPile to o.

impl MarshalInto for PacketPile[src]

fn serialized_len(&self) -> usize[src]

Computes the maximal length of the serialized representation. Read more

fn serialize_into(&self, buf: &mut [u8]) -> Result<usize>[src]

Serializes into the given buffer. Read more

fn export_into(&self, buf: &mut [u8]) -> Result<usize>[src]

Exports into the given buffer. Read more

fn to_vec(&self) -> Result<Vec<u8>>[src]

Serializes the packet to a vector.

fn export_to_vec(&self) -> Result<Vec<u8>>[src]

Exports to a vector. Read more

impl<'a> Parse<'a, PacketPile> for PacketPile[src]

fn from_reader<R: 'a + Read + Send + Sync>(reader: R) -> Result<PacketPile>[src]

Deserializes the OpenPGP message stored in a std::io::Read object.

Although this method is easier to use to parse a sequence of OpenPGP packets than a PacketParser or a PacketPileParser, this interface buffers the whole message in memory. Thus, the caller must be certain that the deserialized message is not too large.

Note: this interface does buffer the contents of packets.

fn from_file<P: AsRef<Path>>(path: P) -> Result<PacketPile>[src]

Deserializes the OpenPGP message stored in the file named by path.

See from_reader for more details and caveats.

fn from_bytes<D: AsRef<[u8]> + ?Sized>(data: &'a D) -> Result<PacketPile>[src]

Deserializes the OpenPGP message stored in the provided buffer.

See from_reader for more details and caveats.

impl PartialEq<PacketPile> for PacketPile[src]

fn eq(&self, other: &PacketPile) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &PacketPile) -> bool[src]

This method tests for !=.

impl Serialize for PacketPile[src]

fn serialize(&self, o: &mut dyn Write) -> Result<()>[src]

Writes a serialized version of the object to o.

fn export(&self, o: &mut dyn Write) -> Result<()>[src]

Exports a serialized version of the object to o. Read more

impl SerializeInto for PacketPile[src]

fn serialized_len(&self) -> usize[src]

Computes the maximal length of the serialized representation. Read more

fn serialize_into(&self, buf: &mut [u8]) -> Result<usize>[src]

Serializes into the given buffer. Read more

fn to_vec(&self) -> Result<Vec<u8>>[src]

Serializes the packet to a vector.

fn export_into(&self, buf: &mut [u8]) -> Result<usize>[src]

Exports into the given buffer. Read more

fn export_to_vec(&self) -> Result<Vec<u8>>[src]

Exports to a vector. Read more

impl<'a> TryFrom<PacketParserResult<'a>> for PacketPile[src]

fn try_from(ppr: PacketParserResult<'a>) -> Result<PacketPile>[src]

Reads all of the packets from a PacketParser, and turns them into a message.

Note: this assumes that ppr points to a top-level packet.

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<PacketPile> for Cert[src]

fn try_from(p: PacketPile) -> Result<Self>[src]

Returns the certificate found in the PacketPile.

If the PacketPile does not start with a certificate (specifically, if it does not start with a primary key packet), then this fails.

If the sequence contains multiple certificates (i.e., it is a keyring), or the certificate is followed by an invalid packet this function will fail. To parse keyrings, use CertParser instead of this function.

Examples

use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::packet::prelude::*;
use openpgp::PacketPile;
use std::convert::TryFrom;

let (cert, rev) =
    CertBuilder::general_purpose(None, Some("alice@example.org"))
    .generate()?;

// We should be able to turn a certificate into a PacketPile
// and back.
let pp : PacketPile = cert.into();
assert!(Cert::try_from(pp).is_ok());

// But a revocation certificate is not a certificate, so this
// will fail.
let pp : PacketPile = Packet::from(rev).into();
assert!(Cert::try_from(pp).is_err());

type Error = Error

The type returned in the event of a conversion error.

impl TryFrom<PacketPile> for Message[src]

fn try_from(pile: PacketPile) -> Result<Self>[src]

Converts the PacketPile to a Message.

Converting a PacketPile to a Message doesn’t change the packets; it asserts that the packet sequence is an optionally encrypted, optionally signed, optionally compressed literal data packet. The exact grammar is defined in Section 11.3 of RFC 4880.

Caveats: this function assumes that any still encrypted parts or still compressed parts are valid messages.

type Error = Error

The type returned in the event of a conversion error.

impl StructuralPartialEq for PacketPile[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> DynClone for T where
    T: Clone
[src]

pub fn __clone_box(&self, Private) -> *mut ()[src]

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.