Skip to main content

Key

Struct Key 

Source
pub struct Key { /* private fields */ }
Expand description

Describes a key.

A Backend provides access to one or more slots, which may or may not contain a token. Each token holds one or more Keys.

This data structure contains a reference to a PKCS#11 context, and everything needed to find a particular key.

We don’t store a Session in this data structure for two reasons. First, an open session locks the token, which may interfere with other users of the token. Second, Sessions are not Sync, which means that Key would not be Sync without wrapping the Session in something like a Mutex. Instead, we open a session on demand.

Because we don’t have a session, we can’t store an object handle, which is only meaningful for a particular session. According to PKCS#11, v3.2, draft 13, Section 3.4:

When an object is created or found on a token by an application, Cryptoki assigns it an object handle for that application’s sessions to use to access it. A particular object on a token does not necessarily have a handle which is fixed for the lifetime of the object; however, if a particular session can use a particular handle to access a particular object, then that session will continue to be able to use that handle to access that object as long as the session continues to exist, the object continues to exist, and the object continues to be accessible to the session.

To workaround this, we cache what we can about a key so that we can unambiguously find it later. In principle, the unique ID (CKA_UNIQUE_ID) attribute is enough. From PKCS#11, v3.2, draft 13, Section 4.4.1:

Any time a new object is created, a value for CKA_UNIQUE_ID MUST be generated by the token and stored with the object. The specific algorithm used to generate unique ID values for objects is token-specific, but values generated MUST be unique across all objects visible to any particular session, and SHOULD be unique across all objects created by the token.

Unfortunately, this was only added in PKCS#11 v3.0, and we want to support older tokens.

So, in addition to opportunistically using the unique ID, we also use the following to distinguish keys, and hope for the best:

  • CKA_LABEL: Set by the user.

  • CKA_ID: Set at key generation time by the application. By convention, this is the same for the public key, private key and certificate tuple.

  • CKA_URL: Set at key generation time by the application. By convention, this is used as a unique identifier.

Implementations§

Source§

impl Key

Source

pub fn signing_capable(&self) -> Result<bool>

Returns whether the key supports signing.

This is derived from the cryptoki objects and not from an OpenPGP self-signature, which is not available.

Source

pub fn decryption_capable(&self) -> Result<bool>

Returns whether the key supports decryption.

This is derived from the cryptoki objects and not from an OpenPGP self-signature, which is not available.

Source§

impl Key

Source

pub fn to_openpgp_key(&self) -> Result<Key<PublicParts, UnspecifiedRole>>

Source

pub fn armor_headers(&self) -> Vec<(&'static str, String)>

Return armor headers.

Source§

impl Key

Source

pub fn backend(&self) -> &Backend

Returns the backend.

Source

pub fn token(&self) -> &Token

Returns the token.

Source

pub fn slot(&self) -> &Slot

Returns the slot.

Source

pub fn info(&self) -> &TokenInfo

Returns information about the token.

See cryptoki::TokenInfo for more details.

Source

pub fn object_class(&self) -> Option<ObjectClass>

Returns the key’s object class, if any.

See cryptoki::ObjectClass for more details.

Source

pub fn key_unique_id(&self) -> Option<&[u8]>

Returns the key’s unique ID, if any.

Source

pub fn key_unique_id_hex(&self) -> Option<String>

Returns the key’s hex encoded unique ID, if any.

Source

pub fn key_label(&self) -> Option<&[u8]>

Returns the key’s label, if any.

Source

pub fn key_id(&self) -> Option<&[u8]>

Returns the key’s key ID, if any.

Source

pub fn key_url(&self) -> Option<&[u8]>

Returns the key’s URL, if any.

Source

pub fn open_ro_session(&self) -> Result<(Session, ObjectHandle)>

Returns a new session and an object handle to the object designated by the key descriptor.

Source

pub fn from_object( token: Token, session: &Session, object: ObjectHandle, ) -> Result<Self>

Wraps an object handle designating a key.

This function does not cache the session or the object. But you can use Self::open_ro_session to get a new session and a new object handle.

Source

pub fn fingerprint(&self) -> Option<Fingerprint>

Returns the key’s OpenPGP fingerprint.

If the key is not an OpenPGP key, then this returns None.

Trait Implementations§

Source§

impl Clone for Key

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Key

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Key

§

impl !RefUnwindSafe for Key

§

impl !UnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin for Key

§

impl UnsafeUnpin for Key

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

Source§

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>,

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.