10#include <botan/p11_types.h>
11#include <botan/internal/dyn_load.h>
35 else if(return_value !=
nullptr)
38 *return_value =
static_cast< ReturnValue >(function_result);
54 session.
set_pin(old_pin, new_pin);
61 session.
set_pin(old_so_pin, new_so_pin);
74 if(m_func_list_ptr ==
nullptr)
105 get_function_list get_function_list_ptr = pkcs11_module.
resolve<get_function_list>(
"C_GetFunctionList");
117 return handle_return_value(m_func_list_ptr->C_GetSlotList(token_present, slot_list_ptr, count_ptr), return_value);
121 std::vector<SlotId>& slot_ids,
127 Ulong number_slots = 0;
129 bool success =
C_GetSlotList(token_present,
nullptr, &number_slots, return_value);
131 if(!success || !number_slots)
137 slot_ids.resize(number_slots);
138 return C_GetSlotList(token_present, slot_ids.data(), &number_slots, return_value);
145 return handle_return_value(m_func_list_ptr->C_GetSlotInfo(slot_id, info_ptr), return_value);
152 return handle_return_value(m_func_list_ptr->C_GetTokenInfo(slot_id, info_ptr), return_value);
173 std::vector<MechanismType>& mechanisms,
179 Ulong number_mechanisms = 0;
181 bool success =
C_GetMechanismList(slot_id,
nullptr, &number_mechanisms, return_value);
183 if(!success || !number_mechanisms)
189 mechanisms.resize(number_mechanisms);
200 info_ptr), return_value);
209 return handle_return_value(m_func_list_ptr->C_InitToken(slot_id, so_pin_ptr, so_pin_len, label_ptr), return_value);
217 return handle_return_value(m_func_list_ptr->C_InitPIN(session, pin_ptr, pin_len), return_value);
227 return handle_return_value(m_func_list_ptr->C_SetPIN(session, old_pin_ptr, old_len, new_pin_ptr, new_len),
260 return handle_return_value(m_func_list_ptr->C_GetSessionInfo(session, info_ptr), return_value);
264 Byte* operation_state_ptr,
265 Ulong* operation_state_len_ptr,
268 return handle_return_value(m_func_list_ptr->C_GetOperationState(session, operation_state_ptr, operation_state_len_ptr),
273 Byte* operation_state_ptr,
274 Ulong operation_state_len,
279 return handle_return_value(m_func_list_ptr->C_SetOperationState(session, operation_state_ptr, operation_state_len,
280 encryption_key, authentication_key), return_value);
307 return handle_return_value(m_func_list_ptr->C_CreateObject(session, attribute_template_ptr, count, object_ptr),
318 return handle_return_value(m_func_list_ptr->C_CopyObject(session,
object, attribute_template_ptr, count,
319 new_object_ptr), return_value);
326 return handle_return_value(m_func_list_ptr->C_DestroyObject(session,
object), return_value);
334 return handle_return_value(m_func_list_ptr->C_GetObjectSize(session,
object, size_ptr), return_value);
343 return handle_return_value(m_func_list_ptr->C_GetAttributeValue(session,
object, attribute_template_ptr, count),
353 return handle_return_value(m_func_list_ptr->C_SetAttributeValue(session,
object, attribute_template_ptr, count),
362 return handle_return_value(m_func_list_ptr->C_FindObjectsInit(session, attribute_template_ptr, count), return_value);
367 Ulong max_object_count,
368 Ulong* object_count_ptr,
371 return handle_return_value(m_func_list_ptr->C_FindObjects(session, object_ptr, max_object_count, object_count_ptr),
388 return handle_return_value(m_func_list_ptr->C_EncryptInit(session, mechanism_ptr, key), return_value);
394 Byte* encrypted_data_ptr,
395 Ulong* encrypted_data_len_ptr,
398 return handle_return_value(m_func_list_ptr->C_Encrypt(session, data_ptr, data_len, encrypted_data_ptr,
399 encrypted_data_len_ptr), return_value);
405 Byte* encrypted_part_ptr,
406 Ulong* encrypted_part_len_ptr,
409 return handle_return_value(m_func_list_ptr->C_EncryptUpdate(session, part_ptr, part_len, encrypted_part_ptr,
410 encrypted_part_len_ptr), return_value);
414 Byte* last_encrypted_part_ptr,
415 Ulong* last_encrypted_part_len_ptr,
418 return handle_return_value(m_func_list_ptr->C_EncryptFinal(session, last_encrypted_part_ptr,
419 last_encrypted_part_len_ptr), return_value);
429 return handle_return_value(m_func_list_ptr->C_DecryptInit(session, mechanism_ptr, key), return_value);
433 Byte* encrypted_data_ptr,
434 Ulong encrypted_data_len,
439 return handle_return_value(m_func_list_ptr->C_Decrypt(session, encrypted_data_ptr, encrypted_data_len, data_ptr,
440 data_len_ptr), return_value);
444 Byte* encrypted_part_ptr,
445 Ulong encrypted_part_len,
450 return handle_return_value(m_func_list_ptr->C_DecryptUpdate(session, encrypted_part_ptr, encrypted_part_len, part_ptr,
451 part_len_ptr), return_value);
456 Ulong* last_part_len_ptr,
459 return handle_return_value(m_func_list_ptr->C_DecryptFinal(session, last_part_ptr, last_part_len_ptr), return_value);
468 return handle_return_value(m_func_list_ptr->C_DigestInit(session, mechanism), return_value);
475 Ulong* digest_len_ptr,
478 return handle_return_value(m_func_list_ptr->C_Digest(session, data_ptr, data_len, digest_ptr, digest_len_ptr),
487 return handle_return_value(m_func_list_ptr->C_DigestUpdate(session, part_ptr, part_len), return_value);
499 Ulong* digest_len_ptr,
502 return handle_return_value(m_func_list_ptr->C_DigestFinal(session, digest_ptr, digest_len_ptr), return_value);
512 return handle_return_value(m_func_list_ptr->C_SignInit(session, mechanism_ptr, key), return_value);
516 const Byte* data_ptr,
519 Ulong* signature_len_ptr,
523 m_func_list_ptr->C_Sign(session,
const_cast<Byte*
>(data_ptr), data_len, signature_ptr, signature_len_ptr),
528 const Byte* part_ptr,
533 m_func_list_ptr->C_SignUpdate(session,
const_cast<Byte*
>(part_ptr), part_len),
539 Ulong* signature_len_ptr,
542 return handle_return_value(m_func_list_ptr->C_SignFinal(session, signature_ptr, signature_len_ptr), return_value);
550 return handle_return_value(m_func_list_ptr->C_SignRecoverInit(session, mechanism_ptr, key), return_value);
557 Ulong* signature_len,
560 return handle_return_value(m_func_list_ptr->C_SignRecover(session, data, data_len, signature, signature_len),
571 return handle_return_value(m_func_list_ptr->C_VerifyInit(session, mechanism_ptr, key), return_value);
575 const Byte* data_ptr,
577 const Byte* signature_ptr,
582 m_func_list_ptr->C_Verify(session,
const_cast<Byte*
>(data_ptr), data_len,
583 const_cast<Byte*
>(signature_ptr), signature_len),
588 const Byte* part_ptr,
593 m_func_list_ptr->C_VerifyUpdate(session,
const_cast<Byte*
>(part_ptr), part_len),
598 const Byte* signature_ptr,
603 m_func_list_ptr->C_VerifyFinal(session,
const_cast<Byte*
>(signature_ptr), signature_len),
612 return handle_return_value(m_func_list_ptr->C_VerifyRecoverInit(session, mechanism_ptr, key), return_value);
622 return handle_return_value(m_func_list_ptr->C_VerifyRecover(session, signature_ptr, signature_len, data_ptr,
623 data_len_ptr), return_value);
631 Byte* encrypted_part_ptr,
632 Ulong* encrypted_part_len_ptr,
635 return handle_return_value(m_func_list_ptr->C_DigestEncryptUpdate(session, part_ptr, part_len, encrypted_part_ptr,
636 encrypted_part_len_ptr), return_value);
640 Byte* encrypted_part_ptr,
641 Ulong encrypted_part_len,
646 return handle_return_value(m_func_list_ptr->C_DecryptDigestUpdate(session, encrypted_part_ptr, encrypted_part_len,
647 part_ptr, part_len_ptr), return_value);
653 Byte* encrypted_part_ptr,
654 Ulong* encrypted_part_len_ptr,
657 return handle_return_value(m_func_list_ptr->C_SignEncryptUpdate(session, part_ptr, part_len, encrypted_part_ptr,
658 encrypted_part_len_ptr), return_value);
662 Byte* encrypted_part_ptr,
663 Ulong encrypted_part_len,
668 return handle_return_value(m_func_list_ptr->C_DecryptVerifyUpdate(session, encrypted_part_ptr, encrypted_part_len,
669 part_ptr, part_len_ptr), return_value);
681 return handle_return_value(m_func_list_ptr->C_GenerateKey(session, mechanism_ptr, attribute_template_ptr, count,
682 key_ptr), return_value);
688 Ulong public_key_attribute_count,
690 Ulong private_key_attribute_count,
695 return handle_return_value(m_func_list_ptr->C_GenerateKeyPair(session, mechanism_ptr, public_key_template_ptr,
696 public_key_attribute_count, private_key_template_ptr,
697 private_key_attribute_count, public_key_ptr, private_key_ptr), return_value);
704 Byte* wrapped_key_ptr,
705 Ulong* wrapped_key_len_ptr,
708 return handle_return_value(m_func_list_ptr->C_WrapKey(session, mechanism_ptr, wrapping_key, key, wrapped_key_ptr,
709 wrapped_key_len_ptr), return_value);
715 Byte* wrapped_key_ptr,
716 Ulong wrapped_key_len,
718 Ulong attribute_count,
722 return handle_return_value(m_func_list_ptr->C_UnwrapKey(session, mechanism_ptr, unwrapping_key, wrapped_key_ptr,
723 wrapped_key_len, attribute_template_ptr,
724 attribute_count, key_ptr), return_value);
731 Ulong attribute_count,
735 return handle_return_value(m_func_list_ptr->C_DeriveKey(session, mechanism_ptr, base_key, attribute_template_ptr,
736 attribute_count, key_ptr), return_value);
742 const Byte* seed_ptr,
747 m_func_list_ptr->C_SeedRandom(session,
const_cast<Byte*
>(seed_ptr), seed_len),
752 Byte* random_data_ptr,
756 return handle_return_value(m_func_list_ptr->C_GenerateRandom(session, random_data_ptr, random_len), return_value);
T resolve(const std::string &symbol)
bool C_CancelFunction(SessionHandle session, ReturnValue *return_value=ThrowException) const
bool C_Finalize(VoidPtr reserved, ReturnValue *return_value=ThrowException) const
bool C_GetSessionInfo(SessionHandle session, SessionInfo *info_ptr, ReturnValue *return_value=ThrowException) const
bool C_DecryptUpdate(SessionHandle session, Byte *encrypted_part_ptr, Ulong encrypted_part_len, Byte *part_ptr, Ulong *part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_Logout(SessionHandle session, ReturnValue *return_value=ThrowException) const
bool C_GetOperationState(SessionHandle session, Byte *operation_state_ptr, Ulong *operation_state_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_DigestKey(SessionHandle session, ObjectHandle key, ReturnValue *return_value=ThrowException) const
bool C_EncryptInit(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
bool C_WaitForSlotEvent(Flags flags, SlotId *slot_ptr, VoidPtr reserved, ReturnValue *return_value=ThrowException) const
bool C_GetTokenInfo(SlotId slot_id, TokenInfo *info_ptr, ReturnValue *return_value=ThrowException) const
bool C_SetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
static bool C_GetFunctionList(Dynamically_Loaded_Library &pkcs11_module, FunctionListPtr *function_list_ptr_ptr, ReturnValue *return_value=ThrowException)
bool C_DecryptFinal(SessionHandle session, Byte *last_part_ptr, Ulong *last_part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_SignInit(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
LowLevel(FunctionListPtr ptr)
bool C_GetSlotInfo(SlotId slot_id, SlotInfo *info_ptr, ReturnValue *return_value=ThrowException) const
bool C_EncryptFinal(SessionHandle session, Byte *last_encrypted_part_ptr, Ulong *last_encrypted_part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_DecryptInit(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
bool C_VerifyUpdate(SessionHandle session, const Byte *part_ptr, Ulong part_len, ReturnValue *return_value=ThrowException) const
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
bool C_Digest(SessionHandle session, Byte *data_ptr, Ulong data_len, Byte *digest_ptr, Ulong *digest_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_OpenSession(SlotId slot_id, Flags flags, VoidPtr application, Notify notify, SessionHandle *session_ptr, ReturnValue *return_value=ThrowException) const
bool C_DeriveKey(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle base_key, Attribute *attribute_template_ptr, Ulong attribute_count, ObjectHandle *key_ptr, ReturnValue *return_value=ThrowException) const
bool C_VerifyRecoverInit(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
bool C_InitPIN(SessionHandle session, Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
bool C_CopyObject(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ObjectHandle *new_object_ptr, ReturnValue *return_value=ThrowException) const
bool C_SignEncryptUpdate(SessionHandle session, Byte *part_ptr, Ulong part_len, Byte *encrypted_part_ptr, Ulong *encrypted_part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_Initialize(VoidPtr init_args, ReturnValue *return_value=ThrowException) const
bool C_GenerateRandom(SessionHandle session, Byte *random_data_ptr, Ulong random_len, ReturnValue *return_value=ThrowException) const
bool C_DigestEncryptUpdate(SessionHandle session, Byte *part_ptr, Ulong part_len, Byte *encrypted_part_ptr, Ulong *encrypted_part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_VerifyInit(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
bool C_Sign(SessionHandle session, const Byte *data_ptr, Ulong data_len, Byte *signature_ptr, Ulong *signature_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_GetFunctionStatus(SessionHandle session, ReturnValue *return_value=ThrowException) const
bool C_CloseSession(SessionHandle session, ReturnValue *return_value=ThrowException) const
bool C_DigestInit(SessionHandle session, Mechanism *mechanism_ptr, ReturnValue *return_value=ThrowException) const
bool C_GenerateKeyPair(SessionHandle session, Mechanism *mechanism_ptr, Attribute *public_key_template_ptr, Ulong public_key_attribute_count, Attribute *private_key_template_ptr, Ulong private_key_attribute_count, ObjectHandle *public_key_ptr, ObjectHandle *private_key_ptr, ReturnValue *return_value=ThrowException) const
bool C_SignUpdate(SessionHandle session, const Byte *part_ptr, Ulong part_len, ReturnValue *return_value=ThrowException) const
bool C_SignRecoverInit(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle key, ReturnValue *return_value=ThrowException) const
bool C_GetInfo(Info *info_ptr, ReturnValue *return_value=ThrowException) const
bool C_WrapKey(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle wrapping_key, ObjectHandle key, Byte *wrapped_key_ptr, Ulong *wrapped_key_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_SignRecover(SessionHandle session, Byte *data_ptr, Ulong data_len, Byte *signature_ptr, Ulong *signature_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_GetMechanismList(SlotId slot_id, MechanismType *mechanism_list_ptr, Ulong *count_ptr, ReturnValue *return_value=ThrowException) const
bool C_DigestUpdate(SessionHandle session, Byte *part_ptr, Ulong part_len, ReturnValue *return_value=ThrowException) const
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
bool C_Decrypt(SessionHandle session, Byte *encrypted_data_ptr, Ulong encrypted_data_len, Byte *data_ptr, Ulong *data_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_SetOperationState(SessionHandle session, Byte *operation_state_ptr, Ulong operation_state_len, ObjectHandle encryption_key, ObjectHandle authentication_key, ReturnValue *return_value=ThrowException) const
bool C_CloseAllSessions(SlotId slot_id, ReturnValue *return_value=ThrowException) const
bool C_DecryptVerifyUpdate(SessionHandle session, Byte *encrypted_part_ptr, Ulong encrypted_part_len, Byte *part_ptr, Ulong *part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_SeedRandom(SessionHandle session, const Byte *seed_ptr, Ulong seed_len, ReturnValue *return_value=ThrowException) const
bool C_CreateObject(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ObjectHandle *object_ptr, ReturnValue *return_value=ThrowException) const
bool C_VerifyRecover(SessionHandle session, Byte *signature_ptr, Ulong signature_len, Byte *data_ptr, Ulong *data_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_DecryptDigestUpdate(SessionHandle session, Byte *encrypted_part_ptr, Ulong encrypted_part_len, Byte *part_ptr, Ulong *part_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_GenerateKey(SessionHandle session, Mechanism *mechanism_ptr, Attribute *attribute_template_ptr, Ulong count, ObjectHandle *key_ptr, ReturnValue *return_value=ThrowException) const
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
static bool handle_return_value(const CK_RV function_result, ReturnValue *return_value)
bool C_Encrypt(SessionHandle session, Byte *data_ptr, Ulong data_len, Byte *encrypted_data, Ulong *encrypted_data_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_GetSlotList(Bbool token_present, SlotId *slot_list_ptr, Ulong *count_ptr, ReturnValue *return_value=ThrowException) const
bool C_GetMechanismInfo(SlotId slot_id, MechanismType type, MechanismInfo *info_ptr, ReturnValue *return_value=ThrowException) const
bool C_VerifyFinal(SessionHandle session, const Byte *signature_ptr, Ulong signature_len, ReturnValue *return_value=ThrowException) const
bool C_Login(SessionHandle session, UserType user_type, Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
bool C_SignFinal(SessionHandle session, Byte *signature_ptr, Ulong *signature_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_InitToken(SlotId slot_id, Utf8Char *so_pin_ptr, Ulong so_pin_len, Utf8Char *label_ptr, ReturnValue *return_value=ThrowException) const
bool C_DigestFinal(SessionHandle session, Byte *digest_ptr, Ulong *digest_len_ptr, ReturnValue *return_value=ThrowException) const
bool C_Verify(SessionHandle session, const Byte *data_ptr, Ulong data_len, const Byte *signature_ptr, Ulong signature_len, ReturnValue *return_value=ThrowException) const
bool C_GetObjectSize(SessionHandle session, ObjectHandle object, Ulong *size_ptr, ReturnValue *return_value=ThrowException) const
bool C_SetPIN(SessionHandle session, Utf8Char *old_pin_ptr, Ulong old_len, Utf8Char *new_pin_ptr, Ulong new_len, ReturnValue *return_value=ThrowException) const
bool C_UnwrapKey(SessionHandle session, Mechanism *mechanism_ptr, ObjectHandle unwrapping_key, Byte *wrapped_key_ptr, Ulong wrapped_key_len, Attribute *attribute_template_ptr, Ulong attribute_count, ObjectHandle *key_ptr, ReturnValue *return_value=ThrowException) const
bool C_EncryptUpdate(SessionHandle session, Byte *part_ptr, Ulong part_len, Byte *encrypted_part_ptr, Ulong *encrypted_part_len_ptr, ReturnValue *return_value=ThrowException) const
Represents a PKCS#11 session.
void set_pin(const secure_string &old_pin, const secure_string &new_pin)
Calls C_SetPIN to change the PIN using the old PIN (requires a logged in session)
void init_pin(const secure_string &new_pin)
Calls C_InitPIN to change or initialize the PIN using the SO_PIN (requires a logged in session)
void login(UserType userType, const secure_string &pin)
Represents a PKCS#11 Slot, i.e., a card reader.
void initialize(const std::string &label, const secure_string &so_pin) const
ReturnValue * ThrowException
void change_pin(Slot &slot, const secure_string &old_pin, const secure_string &new_pin)
void change_so_pin(Slot &slot, const secure_string &old_so_pin, const secure_string &new_so_pin)
CK_FUNCTION_LIST_PTR FunctionListPtr
secure_vector< uint8_t > secure_string
void initialize_token(Slot &slot, const std::string &label, const secure_string &so_pin, const secure_string &pin)
CK_SESSION_HANDLE SessionHandle
void set_pin(Slot &slot, const secure_string &so_pin, const secure_string &pin)
CK_OBJECT_HANDLE ObjectHandle
CK_MECHANISM_TYPE CK_PTR CK_MECHANISM_TYPE_PTR
CK_ULONG CK_MECHANISM_TYPE