Struct sequoia_openpgp::serialize::stream::Recipient

source ·
pub struct Recipient<'a> { /* private fields */ }
Expand description

A recipient of an encrypted message.

OpenPGP messages are encrypted with the subkeys of recipients, identified by the keyid of said subkeys in the recipient field of PKESK packets (see Section 5.1 of RFC 4880). The keyid may be a wildcard (as returned by KeyID::wildcard()) to obscure the identity of the recipient.

Note that several subkeys in a certificate may be suitable encryption subkeys. OpenPGP does not specify what should happen in this case. Some implementations arbitrarily pick one encryption subkey, while others use all of them. This crate does not dictate a policy, but allows for arbitrary policies. We do, however, suggest to encrypt to all suitable subkeys.

Implementations§

source§

impl<'a> Recipient<'a>

source

pub fn new<P, R>(keyid: KeyID, key: &'a Key<P, R>) -> Recipient<'a>
where P: KeyParts, R: KeyRole,

Creates a new recipient with an explicit recipient keyid.

Note: If you don’t want to change the recipient keyid, Recipients can be created from Key and ValidKeyAmalgamation using From.

§Examples
use std::io::Write;
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::serialize::stream::{
    Recipient, Message, Encryptor2,
};
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

let cert = Cert::from_bytes(
    "-----BEGIN PGP PUBLIC KEY BLOCK-----

     xjMEWlNvABYJKwYBBAHaRw8BAQdA+EC2pvebpEbzPA9YplVgVXzkIG5eK+7wEAez
     ...
     -----END PGP PUBLIC KEY BLOCK-----"
)?;

let recipients =
    cert.keys().with_policy(p, None).supported().alive().revoked(false)
    // Or `for_storage_encryption()`, for data at rest.
    .for_transport_encryption()
    .map(|ka| Recipient::new(ka.key().keyid(), ka.key()));

let message = Message::new(&mut sink);
let message = Encryptor2::for_recipients(message, recipients).build()?;
source

pub fn keyid(&self) -> &KeyID

Gets the recipient keyid.

§Examples
use std::io::Write;
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::serialize::stream::Recipient;
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

let cert = Cert::from_bytes(
    "-----BEGIN PGP PUBLIC KEY BLOCK-----

     xjMEWlNvABYJKwYBBAHaRw8BAQdA+EC2pvebpEbzPA9YplVgVXzkIG5eK+7wEAez
     ...
     -----END PGP PUBLIC KEY BLOCK-----"
)?;

let recipients =
    cert.keys().with_policy(p, None).supported().alive().revoked(false)
    // Or `for_storage_encryption()`, for data at rest.
    .for_transport_encryption()
    .map(Into::into)
    .collect::<Vec<Recipient>>();

assert_eq!(recipients[0].keyid(),
           &"8BD8 8E94 C0D2 0333".parse()?);
source

pub fn set_keyid(self, keyid: KeyID) -> Self

Sets the recipient keyid.

§Examples
use std::io::Write;
use sequoia_openpgp as openpgp;
use openpgp::KeyID;
use openpgp::cert::prelude::*;
use openpgp::serialize::stream::{
    Recipient, Message, Encryptor2,
};
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

let cert = Cert::from_bytes(
    "-----BEGIN PGP PUBLIC KEY BLOCK-----

     xjMEWlNvABYJKwYBBAHaRw8BAQdA+EC2pvebpEbzPA9YplVgVXzkIG5eK+7wEAez
     ...
     -----END PGP PUBLIC KEY BLOCK-----"
)?;

let recipients =
    cert.keys().with_policy(p, None).supported().alive().revoked(false)
    // Or `for_storage_encryption()`, for data at rest.
    .for_transport_encryption()
    .map(|ka| Recipient::from(ka)
        // Set the recipient keyid to the wildcard id.
        .set_keyid(KeyID::wildcard())
    );

let message = Message::new(&mut sink);
let message = Encryptor2::for_recipients(message, recipients).build()?;

Trait Implementations§

source§

impl<'a> Debug for Recipient<'a>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, P, R> From<&'a Key<P, R>> for Recipient<'a>
where P: KeyParts, R: KeyRole,

source§

fn from(key: &'a Key<P, R>) -> Self

Converts to this type from the input type.
source§

impl<'a, P, R, R2> From<ValidKeyAmalgamation<'a, P, R, R2>> for Recipient<'a>
where P: KeyParts, R: KeyRole, R2: Copy,

source§

fn from(ka: ValidKeyAmalgamation<'a, P, R, R2>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for Recipient<'a>

§

impl<'a> RefUnwindSafe for Recipient<'a>

§

impl<'a> Send for Recipient<'a>

§

impl<'a> Sync for Recipient<'a>

§

impl<'a> Unpin for Recipient<'a>

§

impl<'a> UnwindSafe for Recipient<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T