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_IDMUST 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
impl Key
Sourcepub fn signing_capable(&self) -> Result<bool>
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.
Sourcepub fn decryption_capable(&self) -> Result<bool>
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
impl Key
pub fn to_openpgp_key(&self) -> Result<Key<PublicParts, UnspecifiedRole>>
Sourcepub fn armor_headers(&self) -> Vec<(&'static str, String)>
pub fn armor_headers(&self) -> Vec<(&'static str, String)>
Return armor headers.
Source§impl Key
impl Key
Sourcepub fn info(&self) -> &TokenInfo
pub fn info(&self) -> &TokenInfo
Returns information about the token.
See cryptoki::TokenInfo for more details.
Sourcepub fn object_class(&self) -> Option<ObjectClass>
pub fn object_class(&self) -> Option<ObjectClass>
Returns the key’s object class, if any.
See cryptoki::ObjectClass for more details.
Sourcepub fn key_unique_id(&self) -> Option<&[u8]>
pub fn key_unique_id(&self) -> Option<&[u8]>
Returns the key’s unique ID, if any.
Sourcepub fn key_unique_id_hex(&self) -> Option<String>
pub fn key_unique_id_hex(&self) -> Option<String>
Returns the key’s hex encoded unique ID, if any.
Sourcepub fn open_ro_session(&self) -> Result<(Session, ObjectHandle)>
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.
Sourcepub fn from_object(
token: Token,
session: &Session,
object: ObjectHandle,
) -> Result<Self>
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.
Sourcepub fn fingerprint(&self) -> Option<Fingerprint>
pub fn fingerprint(&self) -> Option<Fingerprint>
Returns the key’s OpenPGP fingerprint.
If the key is not an OpenPGP key, then this returns None.