Botan 3.14.1
Crypto and TLS for C&
p11_session.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 Session
3* (C) 2016 Daniel Neus, Sirrix AG
4* (C) 2016 Philipp Weber, Sirrix AG
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/p11_types.h>
10
11namespace Botan::PKCS11 {
12
13Session::Session(Slot& slot, bool read_only) :
14 Session(slot, PKCS11::flags(Flag::SerialSession | (read_only ? Flag::None : Flag::RwSession)), nullptr, nullptr) {
15}
16
17Session::Session(Slot& slot, Flags flags, VoidPtr callback_data, Notify notify_callback) :
18 m_slot(slot), m_handle(0), m_logged_in(false) {
19 module()->C_OpenSession(m_slot.slot_id(), flags, callback_data, notify_callback, &m_handle);
20}
21
23 const SessionInfo info = get_info();
24 if(info.state == static_cast<CK_STATE>(SessionState::RoPublicSession) ||
25 info.state == static_cast<CK_STATE>(SessionState::RwPublicSession)) {
26 m_logged_in = false;
27 } else {
28 m_logged_in = true;
29 }
30}
31
32Session::Session(Session&& other) noexcept :
33 m_slot(other.m_slot), m_handle(other.release()), m_logged_in(other.m_logged_in) {
34 other.m_logged_in = false;
35}
36
38 try {
39 if(m_handle != 0) {
40 if(m_logged_in) {
41 module()->C_Logout(m_handle, nullptr);
42 }
43 module()->C_CloseSession(m_handle, nullptr);
44 m_handle = 0;
45 }
46 } catch(...) {
47 // exception during noexcept destructor is ignored
48 }
49}
50
53 std::swap(handle, m_handle);
54 return handle;
55}
56
57void Session::login(UserType user_type, const secure_string& pin) {
58 module()->C_Login(m_handle, user_type, pin);
59 // A context specific login only authorizes the next operation, and
60 // logging out would end the token wide login of the user
61 if(user_type != UserType::ContextSpecific) {
62 m_logged_in = true;
63 }
64}
65
67 module()->C_Logout(m_handle);
68 m_logged_in = false;
69}
70
72 SessionInfo info;
73 module()->C_GetSessionInfo(m_handle, &info);
74 return info;
75}
76
77// NOLINTNEXTLINE(readability-make-member-function-const)
78void Session::set_pin(const secure_string& old_pin, const secure_string& new_pin) {
79 module()->C_SetPIN(m_handle, old_pin, new_pin);
80}
81
82// NOLINTNEXTLINE(readability-make-member-function-const)
83void Session::init_pin(const secure_string& new_pin) {
84 module()->C_InitPIN(m_handle, new_pin);
85}
86
87} // namespace Botan::PKCS11
bool C_GetSessionInfo(SessionHandle session, SessionInfo *info_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:288
bool C_Logout(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:341
bool C_InitPIN(SessionHandle session, const Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:248
bool C_Login(SessionHandle session, UserType user_type, const Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:317
bool C_CloseSession(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:280
bool C_OpenSession(SlotId slot_id, Flags flags, void *application, Notify notify, SessionHandle *session_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:270
bool C_SetPIN(SessionHandle session, const Utf8Char *old_pin_ptr, Ulong old_len, const Utf8Char *new_pin_ptr, Ulong new_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:256
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).
Module & module() const
Definition p11_types.h:175
void logoff()
Logout from this session.
const Slot & slot() const
Definition p11_types.h:169
SessionHandle handle() const
Definition p11_types.h:172
SessionHandle release()
SessionInfo get_info() const
Session(Slot &slot, bool read_only)
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)
~Session() noexcept
Logout user and close the session on destruction.
Represents a PKCS#11 Slot, i.e., a card reader.
Definition p11_types.h:92
secure_vector< uint8_t > secure_string
Definition p11.h:46
CK_NOTIFY Notify
Definition p11.h:1210
CK_VOID_PTR VoidPtr
Definition p11.h:1193
CK_FLAGS Flags
Definition p11.h:1199
CK_SESSION_INFO SessionInfo
Definition p11.h:1212
Flags flags(Flag flags)
Definition p11.h:1235
CK_SESSION_HANDLE SessionHandle
Definition p11.h:1211
CK_ULONG CK_STATE
Definition pkcs11.h:79
CK_STATE state
Definition pkcs11.h:1325