Skip to main content

Recipient

Struct 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 9580). 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<F, H, P, R>( features: F, handle: H, key: &'a Key<P, R>, ) -> Recipient<'a>

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, Encryptor,
};
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()
    // Make an anonymous recipient.
    .map(|ka| Recipient::new(ka.valid_cert().features(), None, ka.key()));

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

pub fn key_handle(&self) -> Option<KeyHandle>

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].key_handle().unwrap(),
           "8BD8 8E94 C0D2 0333".parse()?);
Source

pub fn set_key_handle<H>(self, handle: H) -> Result<Recipient<'a>, Error>
where H: Into<Option<KeyHandle>>,

Sets the recipient key ID or fingerprint.

When setting the recipient for a v6 key, either None or a fingerprint must be supplied. Returns Error::InvalidOperation if a key ID is given instead.

§Examples
use std::io::Write;
use sequoia_openpgp as openpgp;
use openpgp::{KeyHandle, KeyID};
use openpgp::cert::prelude::*;
use openpgp::serialize::stream::{
    Recipient, Message, Encryptor,
};
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_key_handle(None)
            .expect("always safe")
        // Same, but explicit.  Don't do this.
        .set_key_handle(KeyHandle::KeyID(KeyID::wildcard()))
            .expect("safe for v4 recipient")
    );

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

Trait Implementations§

Source§

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

Source§

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

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

impl<'a, P> From<ValidKeyAmalgamation<'a, P, SubordinateRole, ()>> for Recipient<'a>
where P: KeyParts,

Source§

fn from(ka: ValidKeyAmalgamation<'a, P, SubordinateRole, ()>) -> Recipient<'a>

Converts to this type from the input type.
Source§

impl<'a, P> From<ValidKeyAmalgamation<'a, P, UnspecifiedRole, bool>> for Recipient<'a>
where P: KeyParts,

Source§

fn from(ka: ValidKeyAmalgamation<'a, P, UnspecifiedRole, bool>) -> Recipient<'a>

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> UnsafeUnpin 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> MaybeSend for T

Source§

impl<T> MaybeSync for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more