pkcs11.h
Functions
| Name | |
|---|---|
| int | wc_Pkcs11_Initialize(Pkcs11Dev * dev, const char * library, void * heap) |
| void | wc_Pkcs11_Finalize(Pkcs11Dev * dev) |
| int | wc_Pkcs11Token_Init(Pkcs11Token * token, Pkcs11Dev * dev, int slotId, const char * tokenName, const unsigned char * userPin, int userPinSz) Sets up a token by slot id or name. A user PIN is copied into memory the token owns, which wc_Pkcs11Token_Final zeroizes and frees. Call wc_Pkcs11Token_Final before initializing the token again. |
| int | wc_Pkcs11Token_InitName(Pkcs11Token * token, Pkcs11Dev * dev, const char * tokenName, int tokenNameSz, const unsigned char * userPin, int userPinSz) Sets up a token by name. A user PIN is copied as by wc_Pkcs11Token_Init. Call wc_Pkcs11Token_Final before initializing the token again. |
| void | wc_Pkcs11Token_Final(Pkcs11Token * token) |
| int | wc_Pkcs11Token_Open(Pkcs11Token * token, int readWrite) Opens a session on the token that all later operations use, until wc_Pkcs11Token_Close. Without it, wc_Pkcs11StoreKey and generating a key with an id or label on the token return BAD_STATE_E, and a key with neither is generated in software. |
| int | wc_Pkcs11Token_Open_ex(Pkcs11Token * token, int readWrite, int discardPin) Opens a session as wc_Pkcs11Token_Open does. With discardPin set, the token's copy of the user PIN is zeroized and freed once this call has opened the session and logged it in, by its own login or because another token already logged the application in. A session that was already open keeps the PIN. The login then lasts while a session of the application is open. After wc_Pkcs11Token_Close, opening again returns BAD_STATE_E until wc_Pkcs11Token_Final and a new init with the PIN, and operations open sessions without logging in. Discarding the PIN is not safe while other threads use the token. |
| void | wc_Pkcs11Token_Close(Pkcs11Token * token) Closes the session opened with wc_Pkcs11Token_Open. It does not log out, so the application stays logged in while another of its sessions on the token is open. |
| int | wc_Pkcs11Token_SetKeyGenPersistent(Pkcs11Token * token, int persistent) Makes the crypto callback generate RSA, ECC, ML-DSA and ML-KEM keys that have an id or label as token objects (CKA_TOKEN set). LMS keys with an id or label are always generated as token objects. |
| int | wc_Pkcs11StoreKey(Pkcs11Token * token, int type, int clear, void * key) Creates the key on the token as a session object in the session opened with wc_Pkcs11Token_Open, so it lasts until that session closes. An ECC key gets the usages its flags ask for: sign for WC_ECC_FLAG_DEC_SIGN and derive for WC_ECC_FLAG_DERIVE. With neither flag it gets derive, plus sign when the key has an id or label, or only sign when PKCS#11 ECDH is not compiled in, as far as the token supports them. When the key has its public part and the token has ECDSA, the public part is stored too. |
| int | wc_Pkcs11StoreKey_ex(Pkcs11Token * token, int type, int clear, void * key, int persistent) Stores a private key on the token like wc_Pkcs11StoreKey. With persistent set, the objects are created with CKA_TOKEN set and stay on the token after the session closes; this needs a read/write session and a logged in token, even for a public key only, but not wc_Pkcs11Token_Open. A persistent private or secret key also gets CKA_PRIVATE and CKA_SENSITIVE set and CKA_EXTRACTABLE cleared. A persistent key needs an id or label that no token or session key of the type has, or the call fails with BAD_FUNC_ARG or BAD_STATE_E. A key with only the same id or only the same label, such as one another application made, blocks the key too. With WC_PKCS11_FIND_WITH_ID_ONLY, a key of another type with the same id blocks it if id lookups for the key type search its class, as wc_Pkcs11DeleteKey describes. The check and the create are separate PKCS#11 calls, so sessions storing keys with the same id or label at the same time can both succeed. |
| int | wc_Pkcs11PersistKey(Pkcs11Token * token, int type, void * key) Makes a private or secret key that lives in a session object persistent. The session object, from key generation or wc_Pkcs11StoreKey, is copied with C_CopyObject to a token object with the key's id and label and then destroyed. RSA, ECC, ML-DSA and ML-KEM keys that hold their public key also get a token object with the public key, which for ECC, ML-DSA and ML-KEM replaces any session public key with the same names. An ECC, ML-DSA or ML-KEM key without its public key gets a token copy of that session public key instead. As in wc_Pkcs11StoreKey_ex, the public key an ECC key holds is stored only when the token has ECDSA, and is left out when it cannot be created for a key that cannot sign. The session public key is destroyed either way, so the token then keeps the private key only. The id and label are checked as for wc_Pkcs11StoreKey_ex, except that session keys may have them. RSA, ECC, ML-DSA and ML-KEM keys refer to the new private object through devCtx. Freeing the key does not remove token objects. The token session must be read/write and logged in, and stay open from key generation until this call. |
| int | wc_Pkcs11DeleteKey(Pkcs11Token * token, int type, void * key) Removes a key from the token: every token object of the key type with the key's id and label is destroyed. The user must be logged in to see private objects. For RSA, ECC, ML-DSA and ML-KEM keys a session object in devCtx is destroyed as well and devCtx is cleared; when a PKCS#11 call fails part way, devCtx is kept only while its object still exists. An HMAC key is removed under its HMAC key type and as a generic secret. Finding nothing to remove is not an error. A token key with only the same id or only the same label is kept, and still keeps a new key from using that id or label. A key with only a label, or only an id, removes every token key of the type with that label or id, including keys other applications made. With WC_PKCS11_FIND_WITH_ID_ONLY, a key with only an id removes token keys of any type with that id, in the classes id lookups search for the key type: private and public keys for asymmetric keys, secret keys for AES and HMAC keys. |
| int | wc_Pkcs11_CryptoDevCb(int devId, wc_CryptoInfo * info, void * ctx) |
Functions Documentation
function wc_Pkcs11_Initialize
int wc_Pkcs11_Initialize(
Pkcs11Dev * dev,
const char * library,
void * heap
)
function wc_Pkcs11_Finalize
void wc_Pkcs11_Finalize(
Pkcs11Dev * dev
)
function wc_Pkcs11Token_Init
int wc_Pkcs11Token_Init(
Pkcs11Token * token,
Pkcs11Dev * dev,
int slotId,
const char * tokenName,
const unsigned char * userPin,
int userPinSz
)
Sets up a token by slot id or name. A user PIN is copied into memory the token owns, which wc_Pkcs11Token_Final zeroizes and frees. Call wc_Pkcs11Token_Final before initializing the token again.
function wc_Pkcs11Token_InitName
int wc_Pkcs11Token_InitName(
Pkcs11Token * token,
Pkcs11Dev * dev,
const char * tokenName,
int tokenNameSz,
const unsigned char * userPin,
int userPinSz
)
Sets up a token by name. A user PIN is copied as by wc_Pkcs11Token_Init. Call wc_Pkcs11Token_Final before initializing the token again.
function wc_Pkcs11Token_Final
void wc_Pkcs11Token_Final(
Pkcs11Token * token
)
function wc_Pkcs11Token_Open
int wc_Pkcs11Token_Open(
Pkcs11Token * token,
int readWrite
)
Opens a session on the token that all later operations use, until wc_Pkcs11Token_Close. Without it, wc_Pkcs11StoreKey and generating a key with an id or label on the token return BAD_STATE_E, and a key with neither is generated in software.
Parameters:
- token Token to open the session on.
- readWrite 1 for a read-write session, 0 for read-only.
See:
Return:
- 0 on success.
- BAD_FUNC_ARG when token is NULL.
- BAD_STATE_E when wc_Pkcs11Token_Open_ex discarded the PIN and its session was closed.
- WC_HW_E when opening the session or logging in fails. A session whose login failed is closed again.
Calling this again while the session is open does nothing and keeps the session's read/write mode.
function wc_Pkcs11Token_Open_ex
int wc_Pkcs11Token_Open_ex(
Pkcs11Token * token,
int readWrite,
int discardPin
)
Opens a session as wc_Pkcs11Token_Open does. With discardPin set, the token's copy of the user PIN is zeroized and freed once this call has opened the session and logged it in, by its own login or because another token already logged the application in. A session that was already open keeps the PIN. The login then lasts while a session of the application is open. After wc_Pkcs11Token_Close, opening again returns BAD_STATE_E until wc_Pkcs11Token_Final and a new init with the PIN, and operations open sessions without logging in. Discarding the PIN is not safe while other threads use the token.
Parameters:
- token Token to open the session on.
- readWrite 1 for a read-write session, 0 for read-only.
- discardPin 1 to discard the PIN once the session is logged in.
See:
Return:
- 0 on success.
- BAD_FUNC_ARG when token is NULL.
- BAD_STATE_E when an earlier open discarded the PIN and its session was closed.
- WC_HW_E when opening the session or logging in fails. A session whose login failed is closed again.
function wc_Pkcs11Token_Close
void wc_Pkcs11Token_Close(
Pkcs11Token * token
)
Closes the session opened with wc_Pkcs11Token_Open. It does not log out, so the application stays logged in while another of its sessions on the token is open.
Parameters:
- token Token whose session to close.
See: wc_Pkcs11Token_Open
function wc_Pkcs11Token_SetKeyGenPersistent
int wc_Pkcs11Token_SetKeyGenPersistent(
Pkcs11Token * token,
int persistent
)
Makes the crypto callback generate RSA, ECC, ML-DSA and ML-KEM keys that have an id or label as token objects (CKA_TOKEN set). LMS keys with an id or label are always generated as token objects.
Parameters:
- token Token used by the crypto callback.
- persistent Generate token objects when non-zero.
See:
Return:
- 0 on success.
- BAD_FUNC_ARG when token is NULL.
Use it for tokens that cannot make a session key persistent later with wc_Pkcs11PersistKey. The public key is kept as a token object too. Key generation fails with BAD_STATE_E when the user is not logged in or a key of the type has the id or label, as for wc_Pkcs11StoreKey_ex. A key without a name, such as a TLS ephemeral key, stays a session object, or is generated in software when no session from wc_Pkcs11Token_Open is open.
The private key gets CKA_PRIVATE, so the session must be logged in, and a session from wc_Pkcs11Token_Open must be read/write. Without such a session a token key is later found by its id or label.
function wc_Pkcs11StoreKey
int wc_Pkcs11StoreKey(
Pkcs11Token * token,
int type,
int clear,
void * key
)
Creates the key on the token as a session object in the session opened with wc_Pkcs11Token_Open, so it lasts until that session closes. An ECC key gets the usages its flags ask for: sign for WC_ECC_FLAG_DEC_SIGN and derive for WC_ECC_FLAG_DERIVE. With neither flag it gets derive, plus sign when the key has an id or label, or only sign when PKCS#11 ECDH is not compiled in, as far as the token supports them. When the key has its public part and the token has ECDSA, the public part is stored too.
Parameters:
- token Token to store the key on.
- type Key type, one of enum Pkcs11KeyType.
- clear 1 to zeroize the private data in the software key after storing it.
- key Key object of that type.
See: wc_Pkcs11Token_Open
Return:
- 0 on success.
- BAD_FUNC_ARG when token or key is NULL.
- BAD_STATE_E when no session was opened with wc_Pkcs11Token_Open.
- MEMORY_E when memory allocation fails.
- NOT_COMPILED_IN when the key type is not supported, or the token lacks the mechanism for it or for a usage the key's flags ask for.
- WC_HW_E when a PKCS#11 call fails.
function wc_Pkcs11StoreKey_ex
int wc_Pkcs11StoreKey_ex(
Pkcs11Token * token,
int type,
int clear,
void * key,
int persistent
)
Stores a private key on the token like wc_Pkcs11StoreKey. With persistent set, the objects are created with CKA_TOKEN set and stay on the token after the session closes; this needs a read/write session and a logged in token, even for a public key only, but not wc_Pkcs11Token_Open. A persistent private or secret key also gets CKA_PRIVATE and CKA_SENSITIVE set and CKA_EXTRACTABLE cleared. A persistent key needs an id or label that no token or session key of the type has, or the call fails with BAD_FUNC_ARG or BAD_STATE_E. A key with only the same id or only the same label, such as one another application made, blocks the key too. With WC_PKCS11_FIND_WITH_ID_ONLY, a key of another type with the same id blocks it if id lookups for the key type search its class, as wc_Pkcs11DeleteKey describes. The check and the create are separate PKCS#11 calls, so sessions storing keys with the same id or label at the same time can both succeed.
Parameters:
- token Token to store the key on.
- type One of the PKCS11_KEY_TYPE_* values.
- clear Clear the private key data from the software key.
- key Key object of the type.
- persistent Store the key as token objects when non-zero.
See: wc_Pkcs11StoreKey
Return:
- 0 on success.
- BAD_FUNC_ARG when token or key is NULL, or a persistent key has neither id nor label.
- BAD_STATE_E when persistent is 0 and no session was opened with wc_Pkcs11Token_Open, or for a persistent key the user is not logged in or a key of the type already has its id or label.
- MEMORY_E when a memory allocation fails.
- NOT_COMPILED_IN when the key type or mechanism is not available.
- WC_HW_E when a PKCS#11 call fails.
function wc_Pkcs11PersistKey
int wc_Pkcs11PersistKey(
Pkcs11Token * token,
int type,
void * key
)
Makes a private or secret key that lives in a session object persistent. The session object, from key generation or wc_Pkcs11StoreKey, is copied with C_CopyObject to a token object with the key's id and label and then destroyed. RSA, ECC, ML-DSA and ML-KEM keys that hold their public key also get a token object with the public key, which for ECC, ML-DSA and ML-KEM replaces any session public key with the same names. An ECC, ML-DSA or ML-KEM key without its public key gets a token copy of that session public key instead. As in wc_Pkcs11StoreKey_ex, the public key an ECC key holds is stored only when the token has ECDSA, and is left out when it cannot be created for a key that cannot sign. The session public key is destroyed either way, so the token then keeps the private key only. The id and label are checked as for wc_Pkcs11StoreKey_ex, except that session keys may have them. RSA, ECC, ML-DSA and ML-KEM keys refer to the new private object through devCtx. Freeing the key does not remove token objects. The token session must be read/write and logged in, and stay open from key generation until this call.
Parameters:
- token Token holding the key.
- type PKCS11_KEY_TYPE_RSA, PKCS11_KEY_TYPE_EC, PKCS11_KEY_TYPE_MLDSA, PKCS11_KEY_TYPE_MLKEM, PKCS11_KEY_TYPE_AES_GCM, PKCS11_KEY_TYPE_AES_CBC or PKCS11_KEY_TYPE_HMAC.
- key RsaKey, ecc_key, wc_MlDsaKey, MlKemKey, Aes or Hmac with an id or label. An Hmac needs its hash type (macType), as wc_HmacSetKey sets.
See: wc_Pkcs11StoreKey_ex
Return:
- 0 on success.
- BAD_FUNC_ARG when token or key is NULL, the key has neither an id nor a label, or an Hmac has no hash type.
- BAD_STATE_E when the user is not logged in, the token already holds a key with the same id or label, or the key already refers to a token object.
- NOT_COMPILED_IN when the key type is not supported.
- WC_HW_E when no session object is found or a PKCS#11 call fails.
Example
ecc_key key;
wc_ecc_init_id(&key, id, idLen, NULL, devId);
wc_ecc_make_key_ex(&rng, 32, &key, ECC_SECP256R1);
// use the key, for example to sign a CSR, then keep it
wc_Pkcs11PersistKey(&token, PKCS11_KEY_TYPE_EC, &key);
function wc_Pkcs11DeleteKey
int wc_Pkcs11DeleteKey(
Pkcs11Token * token,
int type,
void * key
)
Removes a key from the token: every token object of the key type with the key's id and label is destroyed. The user must be logged in to see private objects. For RSA, ECC, ML-DSA and ML-KEM keys a session object in devCtx is destroyed as well and devCtx is cleared; when a PKCS#11 call fails part way, devCtx is kept only while its object still exists. An HMAC key is removed under its HMAC key type and as a generic secret. Finding nothing to remove is not an error. A token key with only the same id or only the same label is kept, and still keeps a new key from using that id or label. A key with only a label, or only an id, removes every token key of the type with that label or id, including keys other applications made. With WC_PKCS11_FIND_WITH_ID_ONLY, a key with only an id removes token keys of any type with that id, in the classes id lookups search for the key type: private and public keys for asymmetric keys, secret keys for AES and HMAC keys.
Parameters:
- token Token holding the key.
- type PKCS11_KEY_TYPE_RSA, PKCS11_KEY_TYPE_EC, PKCS11_KEY_TYPE_MLDSA, PKCS11_KEY_TYPE_MLKEM, PKCS11_KEY_TYPE_LMS, PKCS11_KEY_TYPE_AES_GCM, PKCS11_KEY_TYPE_AES_CBC or PKCS11_KEY_TYPE_HMAC.
- key RsaKey, ecc_key, wc_MlDsaKey, MlKemKey, LmsKey, Aes or Hmac with an id or label. An Hmac needs its hash type (macType), as wc_HmacSetKey sets.
See: wc_Pkcs11PersistKey
Return:
- 0 on success.
- BAD_FUNC_ARG when token or key is NULL, the key has neither an id nor a label, or an Hmac has no hash type.
- BAD_STATE_E when the user is not logged in.
- NOT_COMPILED_IN when the key type is not supported.
- WC_HW_E when a PKCS#11 call fails.
function wc_Pkcs11_CryptoDevCb
int wc_Pkcs11_CryptoDevCb(
int devId,
wc_CryptoInfo * info,
void * ctx
)
Source code
int wc_Pkcs11_Initialize(Pkcs11Dev* dev, const char* library,
void* heap);
void wc_Pkcs11_Finalize(Pkcs11Dev* dev);
int wc_Pkcs11Token_Init(Pkcs11Token* token, Pkcs11Dev* dev,
int slotId, const char* tokenName, const unsigned char *userPin,
int userPinSz);
int wc_Pkcs11Token_InitName(Pkcs11Token* token, Pkcs11Dev* dev,
const char* tokenName, int tokenNameSz,
const unsigned char* userPin, int userPinSz);
void wc_Pkcs11Token_Final(Pkcs11Token* token);
int wc_Pkcs11Token_Open(Pkcs11Token* token, int readWrite);
int wc_Pkcs11Token_Open_ex(Pkcs11Token* token, int readWrite, int discardPin);
void wc_Pkcs11Token_Close(Pkcs11Token* token);
int wc_Pkcs11Token_SetKeyGenPersistent(Pkcs11Token* token, int persistent);
int wc_Pkcs11StoreKey(Pkcs11Token* token, int type, int clear,
void* key);
int wc_Pkcs11StoreKey_ex(Pkcs11Token* token, int type, int clear,
void* key, int persistent);
int wc_Pkcs11PersistKey(Pkcs11Token* token, int type, void* key);
int wc_Pkcs11DeleteKey(Pkcs11Token* token, int type, void* key);
int wc_Pkcs11_CryptoDevCb(int devId, wc_CryptoInfo* info,
void* ctx);
Updated on 2026-12-04 at 01:12:11 +0000