Object classes
| Class | Constant | Description |
|---|---|---|
| Data | CKO_DATA | Arbitrary application data. |
| Certificate | CKO_CERTIFICATE | X.509 or other certificate types. |
| Public key | CKO_PUBLIC_KEY | Asymmetric public key (RSA, EC, DSA, …). |
| Private key | CKO_PRIVATE_KEY | Asymmetric private key. Typically sensitive. |
| Secret key | CKO_SECRET_KEY | Symmetric key (AES, 3DES, HMAC, …). |
| Mechanism parameter | CKO_DOMAIN_PARAMETERS | Algorithm domain parameters (DSA/DH primes, EC curves). |
Common attributes
Every object carries a set of attributes. The most frequently used ones are:| Attribute | Type | Description |
|---|---|---|
CKA_CLASS | CK_OBJECT_CLASS | Object class (required on creation). |
CKA_TOKEN | CK_BBOOL | CK_TRUE = persistent token object; CK_FALSE = session object. |
CKA_PRIVATE | CK_BBOOL | CK_TRUE = requires login to access. |
CKA_LABEL | byte array | Human-readable label (UTF-8). |
CKA_ID | byte array | Application-assigned identifier, used to link public/private key pairs. |
CKA_SENSITIVE | CK_BBOOL | Key value cannot be extracted in plaintext. Once set to CK_TRUE, cannot be changed back. |
CKA_EXTRACTABLE | CK_BBOOL | Key can be wrapped out. Once set to CK_FALSE, cannot be changed back. |
CKA_WRAP_WITH_TRUSTED | CK_BBOOL | Key can only be wrapped by a key with CKA_TRUSTED = CK_TRUE. Once set to CK_TRUE, cannot be changed back. |
CKA_MODIFIABLE | CK_BBOOL | Object attributes can be changed after creation. |
CKA_COPYABLE | CK_BBOOL | Object can be duplicated with C_CopyObject. |
C_CreateObject
Creates a new object from a caller-supplied attribute template.Handle of an open session. Must be read/write if creating a token object (
CKA_TOKEN = CK_TRUE).Array of
CK_ATTRIBUTE structures specifying the object’s attributes. CKA_CLASS is mandatory. Key-type objects also require CKA_KEY_TYPE.Number of attributes in
pTemplate.Receives the handle of the newly created object.
CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_SESSION_READ_ONLY, CKR_TEMPLATE_INCOMPLETE (required attribute missing), CKR_TEMPLATE_INCONSISTENT (conflicting attribute values), CKR_ATTRIBUTE_READ_ONLY, CKR_USER_NOT_LOGGED_IN (creating a private object without login), CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_CopyObject
Creates a copy of an existing object, optionally overriding specific attributes.Handle of an open session.
Handle of the source object to copy. The object must have
CKA_COPYABLE = CK_TRUE.Optional array of attributes to override in the copy. Only attributes flagged as modifiable (check 8 in the PKCS#11 spec) may be changed during a copy operation.
Number of attributes in
pTemplate. Pass 0 if pTemplate is NULL_PTR.Receives the handle of the new copy.
CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OBJECT_HANDLE_INVALID, CKR_ACTION_PROHIBITED (object not copyable), CKR_TEMPLATE_INCONSISTENT, CKR_ATTRIBUTE_READ_ONLY, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_DestroyObject
Deletes an object from the token or session.Handle of an open session. Must be read/write to destroy a token object.
Handle of the object to destroy. After this call succeeds, the handle is no longer valid.
CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OBJECT_HANDLE_INVALID, CKR_SESSION_READ_ONLY, CKR_ACTION_PROHIBITED, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_GetObjectSize
Returns the size in bytes of an object’s storage representation.Handle of an open session.
Handle of the object to measure.
Receives the size in bytes. SoftHSM v2 may return
CK_UNAVAILABLE_INFORMATION (~0UL) if the size cannot be determined.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OBJECT_HANDLE_INVALID, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_GetAttributeValue
Retrieves the values of one or more attributes from an object.Handle of an open session.
Handle of the object whose attributes you want to read.
Array of
CK_ATTRIBUTE structures. Set each type field to the desired attribute type and pValue to NULL_PTR on the first call to retrieve the sizes. On the second call, set pValue to an appropriately sized buffer and ulValueLen to its capacity.Number of attributes in
pTemplate.CKR_OK (all attributes retrieved), CKR_ATTRIBUTE_SENSITIVE (one or more attributes are sensitive and cannot be read — those entries have ulValueLen set to CK_UNAVAILABLE_INFORMATION), CKR_ATTRIBUTE_TYPE_INVALID, CKR_BUFFER_TOO_SMALL, CKR_SESSION_HANDLE_INVALID, CKR_OBJECT_HANDLE_INVALID, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
Sensitive key values (
CKA_VALUE on secret keys or CKA_PRIVATE_EXPONENT on RSA keys) are not readable when CKA_SENSITIVE = CK_TRUE or CKA_EXTRACTABLE = CK_FALSE. The corresponding ulValueLen field is set to CK_UNAVAILABLE_INFORMATION rather than returning an error for the entire call.C_SetAttributeValue
Modifies one or more attributes on an existing object.Handle of an open read/write session.
Handle of the object to modify. The object must have
CKA_MODIFIABLE = CK_TRUE.Array of
CK_ATTRIBUTE structures, each containing the attribute type and its new value.Number of attributes in
pTemplate.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OBJECT_HANDLE_INVALID, CKR_SESSION_READ_ONLY, CKR_ATTRIBUTE_READ_ONLY (attribute is immutable), CKR_TEMPLATE_INCONSISTENT, CKR_ACTION_PROHIBITED, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
Only attributes explicitly marked as modifiable in the PKCS#11 specification may be changed. In particular, CKA_CLASS, CKA_KEY_TYPE, and sticky boolean attributes such as CKA_SENSITIVE (when already CK_TRUE) are read-only once set.
C_FindObjectsInit / C_FindObjects / C_FindObjectsFinal
Searches for objects matching a template. The search is a three-step operation.C_FindObjectsInit
Initializes an object search operation on a session.Handle of an open session.
Array of
CK_ATTRIBUTE structures specifying the search criteria. An object matches if all listed attributes match. Pass NULL_PTR (with ulCount = 0) to match all accessible objects.Number of attributes in
pTemplate. Pass 0 to retrieve all objects.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OPERATION_ACTIVE (a find is already in progress on this session), CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_FindObjects
Retrieves the next batch of matching object handles.Handle of the session on which
C_FindObjectsInit was called.Buffer that receives up to
ulMaxObjectCount object handles.Capacity of
phObject. A common value is 10 or 64; call in a loop until *pulObjectCount == 0.Receives the number of handles actually written to
phObject. When this is 0, the search is exhausted.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OPERATION_NOT_INITIALIZED, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_FindObjectsFinal
Terminates an object search operation and releases associated resources.Handle of the session on which the find operation was initialized.
CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OPERATION_NOT_INITIALIZED, CKR_CRYPTOKI_NOT_INITIALIZED.
Always call C_FindObjectsFinal after a search, even if you found what you were looking for before exhausting the results.
Example: create an AES key and search for it
Build a creation template
Set
CKA_CLASS, CKA_KEY_TYPE, and all required attributes. Use CKA_TOKEN = CK_TRUE to persist the object across sessions.Generate or create the object
Call
C_GenerateKey (for secret keys), C_GenerateKeyPair (for asymmetric key pairs), or C_CreateObject (to import existing key material).Initialize a search
Call
C_FindObjectsInit with a template containing the attributes to match. An empty template matches all accessible objects.Finalize the search
Always call
C_FindObjectsFinal to release the search context, even if you stopped early.