Botan  2.7.0
Crypto and TLS for C++11
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_session.h>
10 
11 namespace Botan {
12 namespace PKCS11 {
13 
14 Session::Session(Slot& slot, bool read_only)
15  : Session(slot, PKCS11::flags(Flag::SerialSession | (read_only ? Flag::None : Flag::RwSession)), nullptr, nullptr)
16  {}
17 
18 Session::Session(Slot& slot, Flags flags, VoidPtr callback_data, Notify notify_callback)
19  : m_slot(slot), m_handle(0), m_logged_in(false)
20  {
21  module()->C_OpenSession(m_slot.slot_id(), flags, callback_data, notify_callback, &m_handle);
22  }
23 
25  : m_slot(slot), m_handle(handle)
26  {
27  SessionInfo info = get_info();
28  if(info.state == static_cast<CK_STATE>(SessionState::RoPublicSession)
29  || info.state == static_cast<CK_STATE>(SessionState::RwPublicSession))
30  {
31  m_logged_in = false;
32  }
33  else
34  {
35  m_logged_in = true;
36  }
37  }
38 
40  {
41  try
42  {
43  if(m_handle)
44  {
45  if(m_logged_in)
46  {
47  module()->C_Logout(m_handle, nullptr);
48  }
49  module()->C_CloseSession(m_handle, nullptr);
50  m_handle = 0;
51  }
52  }
53  catch(...)
54  {
55  // exception during noexcept destructor is ignored
56  }
57  }
58 
60  {
62  std::swap(handle, m_handle);
63  return handle;
64  }
65 
66 void Session::login(UserType user_type, const secure_string& pin)
67  {
68  module()->C_Login(m_handle, user_type, pin);
69  m_logged_in = true;
70  }
71 
73  {
74  module()->C_Logout(m_handle);
75  m_logged_in = false;
76  }
77 
79  {
80  SessionInfo info;
81  module()->C_GetSessionInfo(m_handle, &info);
82  return info;
83  }
84 
85 void Session::set_pin(const secure_string& old_pin, const secure_string& new_pin) const
86  {
87  module()->C_SetPIN(m_handle, old_pin, new_pin);
88  }
89 
90 void Session::init_pin(const secure_string& new_pin)
91  {
92  module()->C_InitPIN(m_handle, new_pin);
93  }
94 
95 }
96 }
~Session() BOTAN_NOEXCEPT
Logout user and close the session on destruction.
Definition: p11_session.cpp:39
CK_NOTIFY Notify
Definition: p11.h:842
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) ...
Definition: p11_session.cpp:90
Module & module() const
Definition: p11_session.h:68
void login(UserType userType, const secure_string &pin)
Definition: p11_session.cpp:66
CK_STATE state
Definition: pkcs11t.h:281
Flags flags(Flag flags)
Definition: p11.h:858
CK_VOID_PTR VoidPtr
Definition: p11.h:826
#define BOTAN_NOEXCEPT
Definition: compiler.h:154
SessionHandle handle() const
Definition: p11_session.h:62
CK_SESSION_HANDLE SessionHandle
Definition: p11.h:843
Session(Slot &slot, bool read_only)
Definition: p11_session.cpp:14
bool C_CloseSession(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:246
bool C_Logout(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:295
bool C_OpenSession(SlotId slot_id, Flags flags, VoidPtr application, Notify notify, SessionHandle *session_ptr, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:235
Definition: alg_id.cpp:13
bool C_Login(SessionHandle session, UserType user_type, Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:285
bool C_SetPIN(SessionHandle session, Utf8Char *old_pin_ptr, Ulong old_len, Utf8Char *new_pin_ptr, Ulong new_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:222
CK_FLAGS Flags
Definition: p11.h:832
void set_pin(const secure_string &old_pin, const secure_string &new_pin) const
Calls C_SetPIN to change the PIN using the old PIN (requires a logged in session) ...
Definition: p11_session.cpp:85
bool C_InitPIN(SessionHandle session, Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:214
SessionInfo get_info() const
Definition: p11_session.cpp:78
secure_vector< uint8_t > secure_string
Definition: p11.h:61
SessionHandle release()
Definition: p11_session.cpp:59
Represents a PKCS#11 session.
Definition: p11_session.h:21
bool C_GetSessionInfo(SessionHandle session, SessionInfo *info_ptr, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:258
Represents a PKCS#11 Slot, i.e., a card reader.
Definition: p11_slot.h:22
SlotId slot_id() const
Definition: p11_slot.h:38
void logoff()
Logout from this session.
Definition: p11_session.cpp:72