logo
pub struct Cert { /* private fields */ }
Expand description

A collection of components and their associated signatures.

The Cert data structure mirrors the TPK and TSK data structures defined in RFC 4880. Specifically, it contains components (Keys, UserIDs, and UserAttributes), their associated self signatures, self revocations, third-party signatures, and third-party revocations, as well as useful methods.

Certs are canonicalized in the sense that their Components are deduplicated, and their signatures and revocations are deduplicated and checked for validity. The canonicalization routine does not throw away components that have no self signatures. These are returned as usual by, e.g., Cert::userids.

Keys are deduplicated by comparing their public bits using Key::public_cmp. If two keys are considered equal, and only one of them has secret key material, the key with the secret key material is preferred. If both keys have secret material, then one of them is chosen in a deterministic, but undefined manner, which is subject to change. Note: the secret key material is not integrity checked. Hence when updating a certificate with secret key material, it is essential to first strip the secret key material from copies that came from an untrusted source.

Signatures are deduplicated using their Eq implementation, which compares the data that is hashed and the MPIs. That is, it does not compare the unhashed data, the digest prefix and the unhashed subpacket area. If two signatures are considered equal, but have different unhashed data, the unhashed data are merged in a deterministic, but undefined manner, which is subject to change. This policy prevents an attacker from flooding a certificate with valid signatures that only differ in their unhashed data.

Self signatures and self revocations are checked for validity by making sure that the signature is mathematically correct. At this point, the signature is not checked against a Policy.

Third-party signatures and revocations are checked for validity by making sure the computed digest matches the digest prefix stored in the signature packet. This is not an integrity check and is easily spoofed. Unfortunately, at the time of canonicalization, the actual signatures cannot be checked, because the public keys are not available. If you rely on these signatures, it is up to you to check their validity by using an appropriate signature verification method, e.g., Signature::verify_userid_binding or Signature::verify_userid_revocation.

If a signature or a revocation is not valid, we check to see whether it is simply out of place (i.e., belongs to a different component) and, if so, we reorder it. If not, it is added to a list of bad signatures. These can be retrieved using Cert::bad_signatures.

Signatures and revocations are sorted so that the newest signature comes first. Components are sorted, but in an undefined manner (i.e., when parsing the same certificate multiple times, the components will be in the same order, but we reserve the right to change the sort function between versions).

Secret Keys

Any key in a certificate may include secret key material. To protect secret key material from being leaked, secret keys are not written out when a Cert is serialized. To also serialize secret key material, you need to serialize the object returned by Cert::as_tsk().

Secret key material may be protected with a password. In such cases, it needs to be decrypted before it can be used to decrypt data or generate a signature. Refer to Key::decrypt_secret for details.

Filtering Certificates

Component-wise filtering of userids, user attributes, and subkeys can be done with Cert::retain_userids, Cert::retain_user_attributes, and Cert::retain_subkeys.

If you need even more control, iterate over all components, clone what you want to keep, and then reassemble the certificate. The following example simply copies all the packets, and can be adapted to suit your policy:

use std::convert::TryFrom;
use openpgp::cert::prelude::*;

fn identity_filter(cert: &Cert) -> Result<Cert> {
    // Iterate over all of the Cert components, pushing packets we
    // want to keep into the accumulator.
    let mut acc = Vec::new();

    // Primary key and related signatures.
    let c = cert.primary_key();
    acc.push(c.key().clone().into());
    for s in c.self_signatures()   { acc.push(s.clone().into()) }
    for s in c.certifications()    { acc.push(s.clone().into()) }
    for s in c.self_revocations()  { acc.push(s.clone().into()) }
    for s in c.other_revocations() { acc.push(s.clone().into()) }

    // UserIDs and related signatures.
    for c in cert.userids() {
        acc.push(c.userid().clone().into());
        for s in c.self_signatures()   { acc.push(s.clone().into()) }
        for s in c.attestations()      { acc.push(s.clone().into()) }
        for s in c.certifications()    { acc.push(s.clone().into()) }
        for s in c.self_revocations()  { acc.push(s.clone().into()) }
        for s in c.other_revocations() { acc.push(s.clone().into()) }
    }

    // UserAttributes and related signatures.
    for c in cert.user_attributes() {
        acc.push(c.user_attribute().clone().into());
        for s in c.self_signatures()   { acc.push(s.clone().into()) }
        for s in c.attestations()      { acc.push(s.clone().into()) }
        for s in c.certifications()    { acc.push(s.clone().into()) }
        for s in c.self_revocations()  { acc.push(s.clone().into()) }
        for s in c.other_revocations() { acc.push(s.clone().into()) }
    }

    // Subkeys and related signatures.
    for c in cert.keys().subkeys() {
        acc.push(c.key().clone().into());
        for s in c.self_signatures()   { acc.push(s.clone().into()) }
        for s in c.certifications()    { acc.push(s.clone().into()) }
        for s in c.self_revocations()  { acc.push(s.clone().into()) }
        for s in c.other_revocations() { acc.push(s.clone().into()) }
    }

    // Unknown components and related signatures.
    for c in cert.unknowns() {
        acc.push(c.unknown().clone().into());
        for s in c.self_signatures()   { acc.push(s.clone().into()) }
        for s in c.certifications()    { acc.push(s.clone().into()) }
        for s in c.self_revocations()  { acc.push(s.clone().into()) }
        for s in c.other_revocations() { acc.push(s.clone().into()) }
    }

    // Any signatures that we could not associate with a component.
    for s in cert.bad_signatures()     { acc.push(s.clone().into()) }

    // Finally, parse into Cert.
    Cert::try_from(acc)
}

let (cert, _) =
    CertBuilder::general_purpose(None, Some("alice@example.org"))
    .generate()?;
assert_eq!(cert, identity_filter(&cert)?);

A note on equality

We define equality on Cert as the equality of the serialized form as defined by RFC 4880. That is, two certs are considered equal if and only if their serialized forms are equal, modulo the OpenPGP packet framing (see Packet#a-note-on-equality).

Because secret key material is not emitted when a Cert is serialized, two certs are considered equal even if only one of them has secret key material. To take secret key material into account, compare the TSKs instead:

use openpgp::cert::prelude::*;

// Generate a cert with secrets.
let (cert_with_secrets, _) =
    CertBuilder::general_purpose(None, Some("alice@example.org"))
    .generate()?;

// Derive a cert without secrets.
let cert_without_secrets =
    cert_with_secrets.clone().strip_secret_key_material();

// Both are considered equal.
assert!(cert_with_secrets == cert_without_secrets);

// But not if we compare their TSKs:
assert!(cert_with_secrets.as_tsk() != cert_without_secrets.as_tsk());

Examples

Parse a certificate:

use std::convert::TryFrom;
use sequoia_openpgp as openpgp;
use openpgp::Cert;

match Cert::try_from(ppr) {
    Ok(cert) => {
        println!("Key: {}", cert.fingerprint());
        for uid in cert.userids() {
            println!("User ID: {}", uid.userid());
        }
    }
    Err(err) => {
        eprintln!("Error parsing Cert: {}", err);
    }
}

Implementations

Returns the primary key.

Unlike getting the certificate’s primary key using the Cert::keys method, this method does not erase the key’s role.

A key’s secret key material may be protected with a password. In such cases, it needs to be decrypted before it can be used to decrypt data or generate a signature. Refer to Key::decrypt_secret for details.

Examples

The first key returned by Cert::keys is the primary key, but its role has been erased:

assert_eq!(cert.primary_key().key().role_as_unspecified(),
           cert.keys().nth(0).unwrap().key());

Returns the certificate’s revocation status.

Normally, methods that take a policy and a reference time are only provided by ValidCert. This method is provided here because there are two revocation criteria, and one of them is independent of the reference time. That is, even if it is not possible to turn a Cert into a ValidCert at time t, it may still be considered revoked at time t.

A certificate is considered revoked at time t if:

  • There is a valid and live revocation at time t that is newer than all valid and live self signatures at time t, or

  • There is a valid hard revocation (even if it is not live at time t, and even if there is a newer self signature).

Note: certificates and subkeys have different revocation criteria from User IDs and User Attributes.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::types::RevocationStatus;
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

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

assert_eq!(cert.revocation_status(p, None), RevocationStatus::NotAsFarAsWeKnow);

// Merge the revocation certificate.  `cert` is now considered
// to be revoked.
let cert = cert.insert_packets(rev.clone())?;
assert_eq!(cert.revocation_status(p, None),
           RevocationStatus::Revoked(vec![&rev.into()]));

Generates a revocation certificate.

This is a convenience function around CertRevocationBuilder to generate a revocation certificate. To use the revocation certificate, merge it into the certificate using Cert::insert_packets.

If you want to revoke an individual component, use SubkeyRevocationBuilder, UserIDRevocationBuilder, or UserAttributeRevocationBuilder, as appropriate.

Examples
use sequoia_openpgp as openpgp;
use openpgp::types::{ReasonForRevocation, RevocationStatus, SignatureType};
use openpgp::cert::prelude::*;
use openpgp::crypto::KeyPair;
use openpgp::parse::Parse;
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

let (cert, rev) = CertBuilder::new()
    .set_cipher_suite(CipherSuite::Cv25519)
    .generate()?;

// A new certificate is not revoked.
assert_eq!(cert.revocation_status(p, None),
           RevocationStatus::NotAsFarAsWeKnow);

// The default revocation certificate is a generic
// revocation.
assert_eq!(rev.reason_for_revocation().unwrap().0,
           ReasonForRevocation::Unspecified);

// Create a revocation to explain what *really* happened.
let mut keypair = cert.primary_key()
    .key().clone().parts_into_secret()?.into_keypair()?;
let rev = cert.revoke(&mut keypair,
                      ReasonForRevocation::KeyCompromised,
                      b"It was the maid :/")?;
let cert = cert.insert_packets(rev)?;
if let RevocationStatus::Revoked(revs) = cert.revocation_status(p, None) {
    assert_eq!(revs.len(), 1);
    let rev = revs[0];

    assert_eq!(rev.typ(), SignatureType::KeyRevocation);
    assert_eq!(rev.reason_for_revocation(),
               Some((ReasonForRevocation::KeyCompromised,
                     "It was the maid :/".as_bytes())));
} else {
    unreachable!()
}

Sets the certificate to expire at the specified time.

If no time (None) is specified, then the certificate is set to not expire.

This function creates new binding signatures that cause the certificate to expire at the specified time. Specifically, it updates the current binding signature on each of the valid, non-revoked User IDs, and the direct key signature, if any. This is necessary, because the primary User ID is first consulted when determining the certificate’s expiration time, and certificates can be distributed with a possibly empty subset of User IDs.

A policy is needed, because the expiration is updated by updating the current binding signatures.

Examples
use std::time;
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::crypto::KeyPair;
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

// The certificate is alive (not expired).
assert!(cert.with_policy(p, None)?.alive().is_ok());

// Make cert expire now.
let mut keypair = cert.primary_key()
    .key().clone().parts_into_secret()?.into_keypair()?;
let sigs = cert.set_expiration_time(p, None, &mut keypair,
                                    Some(time::SystemTime::now()))?;

let cert = cert.insert_packets(sigs)?;
assert!(cert.with_policy(p, None)?.alive().is_err());

Returns an iterator over the certificate’s User IDs.

Note: This returns all User IDs, even those without a binding signature. This is not what you want, unless you are doing a low-level inspection of the certificate. Use ValidCert::userids instead.

Examples
println!("{}'s User IDs:", cert.fingerprint());
for ua in cert.userids() {
    println!("  {}", String::from_utf8_lossy(ua.value()));
}

Returns an iterator over the certificate’s User Attributes.

Note: This returns all User Attributes, even those without a binding signature. This is not what you want, unless you are doing a low-level inspection of the certificate. Use ValidCert::user_attributes instead.

Examples
println!("{}'s has {} User Attributes.",
         cert.fingerprint(),
         cert.user_attributes().count());

Returns an iterator over the certificate’s keys.

That is, this returns an iterator over the primary key and any subkeys.

Note: This returns all keys, even those without a binding signature. This is not what you want, unless you are doing a low-level inspection of the certificate. Use ValidCert::keys instead.

By necessity, this function erases the returned keys’ roles. If you are only interested in the primary key, use Cert::primary_key. If you are only interested in the subkeys, use KeyAmalgamationIter::subkeys. These functions preserve the keys’ role in the type system.

A key’s secret key material may be protected with a password. In such cases, it needs to be decrypted before it can be used to decrypt data or generate a signature. Refer to Key::decrypt_secret for details.

Examples
println!("{}'s has {} keys.",
         cert.fingerprint(),
         cert.keys().count());

Returns an iterator over the certificate’s unknown components.

This function returns all unknown components even those without a binding signature.

Examples
println!("{}'s has {} unknown components.",
         cert.fingerprint(),
         cert.unknowns().count());
for ua in cert.unknowns() {
    println!("  Unknown component with tag {} ({}), error: {}",
             ua.tag(), u8::from(ua.tag()), ua.error());
}

Returns a slice containing the bad signatures.

Bad signatures are signatures and revocations that we could not associate with one of the certificate’s components.

For self signatures and self revocations, we check that the signature is correct. For third-party signatures and third-party revocations, we only check that the digest prefix is correct, because third-party keys are not available. Checking the digest prefix is not an integrity check; third party-signatures and third-party revocations may be invalid and must still be checked for validity before use.

Examples
println!("{}'s has {} bad signatures.",
         cert.fingerprint(),
         cert.bad_signatures().count());

Returns a list of any designated revokers for this certificate.

This function returns the designated revokers listed on the primary key’s binding signatures and the certificate’s direct key signatures.

Note: the returned list is deduplicated.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::policy::StandardPolicy;
use openpgp::types::RevocationKey;

let p = &StandardPolicy::new();

let (alice, _) =
    CertBuilder::general_purpose(None, Some("alice@example.org"))
    .generate()?;
// Make Alice a designated revoker for Bob.
let (bob, _) =
    CertBuilder::general_purpose(None, Some("bob@example.org"))
    .set_revocation_keys(vec![(&alice).into()])
    .generate()?;

// Make sure Alice is listed as a designated revoker for Bob.
assert_eq!(bob.revocation_keys(p).collect::<Vec<&RevocationKey>>(),
           vec![&(&alice).into()]);

Converts the certificate into an iterator over a sequence of packets.

WARNING: When serializing a Cert, any secret key material is dropped. In order to serialize the secret key material, it is first necessary to convert the Cert into a TSK and serialize that. This behavior makes it harder to accidentally leak secret key material. This function is different. If a key contains secret key material, it is exported as a SecretKey or SecretSubkey, as appropriate. This means that if you serialize the resulting packets, the secret key material will be serialized too.

Examples
println!("Cert contains {} packets",
         cert.into_packets().count());

Returns the first certificate found in the sequence of packets.

If the sequence of packets 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;

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.
assert!(Cert::from_packets(cert.into_packets()).is_ok());

// But a revocation certificate is not a certificate, so this
// will fail.
let p : Vec<Packet> = vec![rev.into()];
assert!(Cert::from_packets(p.into_iter()).is_err());

Converts the certificate into a PacketPile.

Examples
let pp = cert.into_packet_pile();

Returns the certificate’s fingerprint as a KeyHandle.

Examples
println!("{}", cert.key_handle());

// This always returns a fingerprint.
match cert.key_handle() {
    KeyHandle::Fingerprint(_) => (),
    KeyHandle::KeyID(_) => unreachable!(),
}

Returns the certificate’s fingerprint.

Examples
println!("{}", cert.fingerprint());

Returns the certificate’s Key ID.

As a general rule of thumb, you should prefer the fingerprint as it is possible to create keys with a colliding Key ID using a birthday attack.

Examples
println!("{}", cert.keyid());

Merges other into self, ignoring secret key material in other.

If other is a different certificate, then an error is returned.

This routine merges duplicate packets. This is different from Cert::insert_packets, which prefers keys in the packets that are being merged into the certificate.

This function is appropriate to merge certificate material from untrusted sources like keyservers. If other contains secret key material, it is ignored. See Cert::merge_public_and_secret on how to merge certificates containing secret key material from trusted sources.

Examples
// Merge the local version with the version from the keyserver.
let cert = local.merge_public(keyserver)?;

Merges other into self, including secret key material.

If other is a different certificate, then an error is returned.

This routine merges duplicate packets. If the same (sub)key is present in both self and other, then

  • if both keys have secret key material, then the version in other is preferred,

  • if only one key has secret key material, then this copy is preferred.

This is different from Cert::insert_packets, which unconditionally prefers keys in the packets that are being merged into the certificate.

It is important to only merge key material from trusted sources using this function, because it may be used to import secret key material. Secret key material is not authenticated by OpenPGP, and there are plausible attack scenarios where a malicious actor injects secret key material.

To merge only public key material, which is always safe, use Cert::merge_public.

Examples
// Merge the local version with the version from your other device.
let cert = local.merge_public_and_secret(other_device)?;

Adds packets to the certificate.

This function turns the certificate into a sequence of packets, appends the packets to the end of it, and canonicalizes the result. Known packets that don’t belong in a TPK or TSK cause this function to return an error. Unknown packets are retained and added to the list of unknown components. The goal is to provide some future compatibility.

If a key is merged that already exists in the certificate, it replaces the existing key. This way, secret key material can be added, removed, encrypted, or decrypted.

Similarly, if a signature is merged that already exists in the certificate, it replaces the existing signature. This way, the unhashed subpacket area can be updated.

On success, this function returns the certificate with the packets merged in, and a boolean indicating whether the certificate actually changed. Changed here means that at least one new packet was added, or an existing packet was updated. Alternatively, changed means that the serialized form has changed.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::packet::prelude::*;
use openpgp::serialize::Serialize;
use openpgp::parse::Parse;
use openpgp::types::DataFormat;

// Create a new key.
let (cert, rev) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .generate()?;
assert!(cert.is_tsk());


// Merging in the certificate doesn't change it.
let identical_cert = cert.clone();
let (cert, changed) =
    cert.insert_packets2(identical_cert.into_packets())?;
assert!(! changed);


// Merge in the revocation certificate.
assert_eq!(cert.primary_key().self_revocations().count(), 0);
let (cert, changed) = cert.insert_packets2(rev)?;
assert!(changed);
assert_eq!(cert.primary_key().self_revocations().count(), 1);


// Add an unknown packet.
let tag = Tag::Private(61.into());
let unknown = Unknown::new(tag,
    openpgp::Error::UnsupportedPacketType(tag).into());

// It shows up as an unknown component.
let (cert, changed) = cert.insert_packets2(unknown)?;
assert!(changed);
assert_eq!(cert.unknowns().count(), 1);
for p in cert.unknowns() {
    assert_eq!(p.tag(), tag);
}


// Try and merge a literal data packet.
let mut lit = Literal::new(DataFormat::Text);
lit.set_body(b"test".to_vec());

// Merging packets that are known to not belong to a
// certificate result in an error.
assert!(cert.insert_packets(lit).is_err());

Remove secret key material:

use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::packet::prelude::*;

// Create a new key.
let (cert, _) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .generate()?;
assert!(cert.is_tsk());

// We just created the key, so all of the keys have secret key
// material.
let mut pk = cert.primary_key().key().clone();

// Split off the secret key material.
let (pk, sk) = pk.take_secret();
assert!(sk.is_some());
assert!(! pk.has_secret());

// Merge in the public key.  Recall: the packets that are
// being merged into the certificate take precedence.
let (cert, changed) = cert.insert_packets2(pk)?;
assert!(changed);

// The secret key material is stripped.
assert!(! cert.primary_key().has_secret());

Update a binding signature’s unhashed subpacket area:

use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::packet::prelude::*;
use openpgp::packet::signature::subpacket::*;

// Create a new key.
let (cert, _) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .generate()?;
assert_eq!(cert.userids().nth(0).unwrap().self_signatures().count(), 1);

// Grab the binding signature so that we can modify it.
let mut sig =
    cert.userids().nth(0).unwrap().self_signatures().nth(0)
    .unwrap().clone();

// Add a notation subpacket.  Note that the information is not
// authenticated, therefore it may only be trusted if the
// certificate with the signature is placed in a trusted store.
let notation = NotationData::new("retrieved-from@example.org",
                                 "generated-locally",
                                 NotationDataFlags::empty()
                                     .set_human_readable());
sig.unhashed_area_mut().add(
    Subpacket::new(SubpacketValue::NotationData(notation), false)?)?;

// Merge in the signature.  Recall: the packets that are
// being merged into the certificate take precedence.
let (cert, changed) = cert.insert_packets2(sig)?;
assert!(changed);

// The old binding signature is replaced.
assert_eq!(cert.userids().nth(0).unwrap().self_signatures().count(), 1);
assert_eq!(cert.userids().nth(0).unwrap().self_signatures().nth(0)
               .unwrap()
               .unhashed_area()
               .subpackets(SubpacketTag::NotationData).count(), 1);

Adds packets to the certificate with an explicit merge policy.

Like Cert::insert_packets2, but also takes a function that will be called on inserts and replacements that can be used to log changes to the certificate, and to influence how packets are merged. The merge function takes two parameters, an optional existing packet, and the packet to be merged in.

If a new packet is inserted, there is no packet currently in the certificate. Hence, the first parameter to the merge function is None.

If an existing packet is updated, there is a packet currently in the certificate that matches the given packet. Hence, the first parameter to the merge function is Some(existing_packet).

Both packets given to the merge function are considered equal when considering the normalized form (only comparing public key parameters and ignoring unhashed signature subpackets, see Packet::normalized_hash). It must return a packet that equals the input packet. In practice that means that the merge function returns either the old packet, the new packet, or a combination of both packets. If the merge function returns a different packet, this function returns Error::InvalidOperation.

If the merge function returns the existing packet, this function will still consider this as a change to the certificate. In other words, it may return that the certificate has changed even if the serialized representation has not changed.

Examples

In the first example, we give an explicit merge function that just returns the new packet. This policy prefers the new packet. This is the policy used by Cert::insert_packets2.

use sequoia_openpgp as openpgp;
use openpgp::crypto::Password;
use openpgp::cert::prelude::CertBuilder;

let p0 = Password::from("old password");
let p1 = Password::from("new password");

// Create a new key.
let (cert, rev) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .set_password(Some(p0.clone()))
      .generate()?;
assert!(cert.is_tsk());

// Change the password for the primary key.
let pk = cert.primary_key().key().clone().parts_into_secret()?
    .decrypt_secret(&p0)?
    .encrypt_secret(&p1)?;

// Merge it back in, with a policy projecting to the new packet.
let (cert, changed) =
    cert.insert_packets_merge(pk, |_old, new| Ok(new))?;
assert!(changed);

// Make sure we can still decrypt the primary key using the
// new password.
assert!(cert.primary_key().key().clone().parts_into_secret()?
        .decrypt_secret(&p1).is_ok());

In the second example, we give an explicit merge function that returns the old packet if given, falling back to the new packet, if not. This policy prefers the existing packets.

use sequoia_openpgp as openpgp;
use openpgp::crypto::Password;
use openpgp::cert::prelude::CertBuilder;

let p0 = Password::from("old password");
let p1 = Password::from("new password");

// Create a new key.
let (cert, rev) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .set_password(Some(p0.clone()))
      .generate()?;
assert!(cert.is_tsk());

// Change the password for the primary key.
let pk = cert.primary_key().key().clone().parts_into_secret()?
    .decrypt_secret(&p0)?
    .encrypt_secret(&p1)?;

// Merge it back in, with a policy preferring to the old packet.
let (cert, changed) =
    cert.insert_packets_merge(pk, |old, new| Ok(old.unwrap_or(new)))?;
assert!(changed); // Overestimates changes.

// Make sure we can still decrypt the primary key using the
// old password.
assert!(cert.primary_key().key().clone().parts_into_secret()?
        .decrypt_secret(&p0).is_ok());

Adds packets to the certificate.

Like Cert::insert_packets2, but does not return whether the certificate changed.

Returns whether at least one of the keys includes secret key material.

This returns true if either the primary key or at least one of the subkeys includes secret key material.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::policy::StandardPolicy;
use openpgp::serialize::Serialize;
use openpgp::parse::Parse;

let p = &StandardPolicy::new();

// Create a new key.
let (cert, _) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .generate()?;
assert!(cert.is_tsk());

// If we serialize the certificate, the secret key material is
// stripped, unless we first convert it to a TSK.

let mut buffer = Vec::new();
cert.as_tsk().serialize(&mut buffer);
let cert = Cert::from_bytes(&buffer)?;
assert!(cert.is_tsk());

// Now round trip it without first converting it to a TSK.  This
// drops the secret key material.
let mut buffer = Vec::new();
cert.serialize(&mut buffer);
let cert = Cert::from_bytes(&buffer)?;
assert!(!cert.is_tsk());

Strips any secret key material.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;


// Create a new key.
let (cert, _) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .generate()?;
assert!(cert.is_tsk());

let cert = cert.strip_secret_key_material();
assert!(! cert.is_tsk());

Retains only the userids specified by the predicate.

Removes all the userids for which the given predicate returns false.

Warning

Because userid binding signatures are traditionally used to provide additional information like the certificate holder’s algorithm preferences (see Preferences) and primary key flags (see ValidKeyAmalgamation::key_flags). Removing a userid may inadvertently change this information.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;

// Create a new key.
let (cert, _) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      .add_userid("Alice Lovelace <alice@lovelace.name>")
      .generate()?;
assert_eq!(cert.userids().count(), 2);

let cert = cert.retain_userids(|ua| {
    if let Ok(Some(address)) = ua.email() {
        address == "alice@example.org" // Only keep this one.
    } else {
        false                          // Drop malformed userids.
    }
});
assert_eq!(cert.userids().count(), 1);
assert_eq!(cert.userids().nth(0).unwrap().email()?.unwrap(),
           "alice@example.org");

Retains only the user attributes specified by the predicate.

Removes all the user attributes for which the given predicate returns false.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;

// Create a new key.
let (cert, _) =
      CertBuilder::general_purpose(None, Some("alice@example.org"))
      // Add nonsensical user attribute.
      .add_user_attribute(vec![0, 1, 2])
      .generate()?;
assert_eq!(cert.user_attributes().count(), 1);

// Strip all user attributes
let cert = cert.retain_user_attributes(|_| false);
assert_eq!(cert.user_attributes().count(), 0);

Retains only the subkeys specified by the predicate.

Removes all the subkeys for which the given predicate returns false.

Examples
use sequoia_openpgp as openpgp;
use openpgp::policy::StandardPolicy;
use openpgp::cert::prelude::*;

// Create a new key.
let (cert, _) =
      CertBuilder::new()
      .add_userid("Alice Lovelace <alice@lovelace.name>")
      .add_transport_encryption_subkey()
      .add_storage_encryption_subkey()
      .generate()?;
assert_eq!(cert.keys().subkeys().count(), 2);

// Retain only the transport encryption subkey.  For that, we
// need to examine the key flags, therefore we need to turn
// the `KeyAmalgamation` into a `ValidKeyAmalgamation` under a
// policy.
let p = &StandardPolicy::new();
let cert = cert.retain_subkeys(|ka| {
    if let Ok(vka) = ka.with_policy(p, None) {
        vka.key_flags().map(|flags| flags.for_transport_encryption())
            .unwrap_or(false)      // Keep transport encryption keys.
    } else {
        false                      // Drop unbound keys.
    }
});
assert_eq!(cert.keys().subkeys().count(), 1);
assert!(cert.with_policy(p, None)?.keys().subkeys().nth(0).unwrap()
            .key_flags().unwrap().for_transport_encryption());

Associates a policy and a reference time with the certificate.

This is used to turn a Cert into a ValidCert. (See also ValidateAmalgamation, which does the same for component amalgamations.)

A certificate is considered valid if:

  • It has a self signature that is live at time t.

  • The policy considers it acceptable.

This doesn’t say anything about whether the certificate itself is alive (see ValidCert::alive) or revoked (see ValidCert::revocation_status).

Examples
use sequoia_openpgp as openpgp;
use openpgp::policy::StandardPolicy;

let p = &StandardPolicy::new();

let vc = cert.with_policy(p, None)?;

Derive a TSK object from this key.

This object writes out secret keys during serialization.

Creates descriptive armor headers.

Returns armor headers that describe this Cert. The Cert’s primary fingerprint and valid userids (according to the default policy) are included as comments, so that it is easier to identify the Cert when looking at the armored data.

Wraps this Cert in an armor structure when serialized.

Derives an object from this Cert that adds an armor structure to the serialized Cert when it is serialized. Additionally, the Cert’s User IDs are added as comments, so that it is easier to identify the Cert when looking at the armored data.

Examples
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::serialize::SerializeInto;

let (cert, _) =
    CertBuilder::general_purpose(None, Some("Mr. Pink ☮☮☮"))
    .generate()?;
let armored = String::from_utf8(cert.armored().to_vec()?)?;

assert!(armored.starts_with("-----BEGIN PGP PUBLIC KEY BLOCK-----"));
assert!(armored.contains("Mr. Pink ☮☮☮"));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

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.

The associated error which can be returned from parsing.

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Writes a serialized version of the object to o.

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

Computes the maximal length of the serialized representation. Read more

Serializes into the given buffer. Read more

Exports into the given buffer. Read more

Serializes the packet to a vector.

Exports to a vector. Read more

Returns the first Cert encountered in the reader.

Returns the first Cert encountered in the file.

Returns the first Cert found in buf.

buf must be an OpenPGP-encoded message.

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

This method tests for !=.

Writes a serialized version of the object to o.

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

Computes the maximal length of the serialized representation. Read more

Serializes into the given buffer. Read more

Serializes the packet to a vector.

Exports into the given buffer. Read more

Exports to a vector. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Returns the Cert found in the packet stream.

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.

The type returned in the event of a conversion error.

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());

The type returned in the event of a conversion error.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

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

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.