Botan 3.13.0
Crypto and TLS for C&
Botan::PKCS11::Session Class Referencefinal

Represents a PKCS#11 session. More...

#include <p11_types.h>

Public Member Functions

SessionInfo get_info () const
SessionHandle handle () const
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)
void logoff ()
 Logout from this session.
Modulemodule () const
Sessionoperator= (const Session &other)=delete
Sessionoperator= (Session &&other)=delete
SessionHandle release ()
 Session (const Session &other)=delete
 Session (Session &&other) noexcept
 Session (Slot &slot, bool read_only)
 Session (Slot &slot, Flags flags, VoidPtr callback_data, Notify notify_callback)
 Session (Slot &slot, SessionHandle handle)
 Takes ownership of a 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).
const Slotslot () const
 ~Session () noexcept
 Logout user and close the session on destruction.

Detailed Description

Represents a PKCS#11 session.

Definition at line 125 of file p11_types.h.

Constructor & Destructor Documentation

◆ Session() [1/5]

Botan::PKCS11::Session::Session ( Slot & slot,
bool read_only )
Parameters
slotthe slot to use
read_onlytrue if the session should be read only, false to create a read-write session

Definition at line 13 of file p11_session.cpp.

13 :
14 Session(slot, PKCS11::flags(Flag::SerialSession | (read_only ? Flag::None : Flag::RwSession)), nullptr, nullptr) {
15}
const Slot & slot() const
Definition p11_types.h:155
Session(Slot &slot, bool read_only)
Flags flags(Flag flags)
Definition p11.h:1235

References Botan::PKCS11::flags(), Session(), and slot().

Referenced by operator=(), operator=(), Session(), Session(), and Session().

◆ Session() [2/5]

Botan::PKCS11::Session::Session ( Slot & slot,
Flags flags,
VoidPtr callback_data,
Notify notify_callback )
Parameters
slotthe slot to use
flagsthe flags to use for the session. Remark: Flag::SerialSession is mandatory
callback_dataapplication-defined pointer to be passed to the notification callback
notify_callbackaddress of the notification callback function

Definition at line 17 of file p11_session.cpp.

17 :
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}
bool C_OpenSession(SlotId slot_id, Flags flags, void *application, Notify notify, SessionHandle *session_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:243
Module & module() const
Definition p11_types.h:161

References Botan::PKCS11::LowLevel::C_OpenSession(), Botan::PKCS11::flags(), module(), and slot().

◆ Session() [3/5]

Botan::PKCS11::Session::Session ( Slot & slot,
SessionHandle handle )

Takes ownership of a session.

Definition at line 22 of file p11_session.cpp.

22 : m_slot(slot), m_handle(handle) {
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}
SessionHandle handle() const
Definition p11_types.h:158
SessionInfo get_info() const
CK_SESSION_INFO SessionInfo
Definition p11.h:1212
CK_ULONG CK_STATE
Definition pkcs11.h:79

References get_info(), handle(), Botan::PKCS11::RoPublicSession, Botan::PKCS11::RwPublicSession, slot(), and CK_SESSION_INFO::state.

◆ Session() [4/5]

Botan::PKCS11::Session::Session ( Session && other)
noexcept

Definition at line 32 of file p11_session.cpp.

32 :
33 m_slot(other.m_slot), m_handle(other.release()), m_logged_in(other.m_logged_in) {
34 other.m_logged_in = false;
35}

References Session().

◆ Session() [5/5]

Botan::PKCS11::Session::Session ( const Session & other)
delete

References Session().

◆ ~Session()

Botan::PKCS11::Session::~Session ( )
noexcept

Logout user and close the session on destruction.

Definition at line 37 of file p11_session.cpp.

37 {
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}
bool C_Logout(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:314
bool C_CloseSession(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:253

References Botan::PKCS11::LowLevel::C_CloseSession(), Botan::PKCS11::LowLevel::C_Logout(), and module().

Member Function Documentation

◆ get_info()

SessionInfo Botan::PKCS11::Session::get_info ( ) const
Returns
information about this session

Definition at line 67 of file p11_session.cpp.

67 {
68 SessionInfo info;
69 module()->C_GetSessionInfo(m_handle, &info);
70 return info;
71}
bool C_GetSessionInfo(SessionHandle session, SessionInfo *info_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:261

References Botan::PKCS11::LowLevel::C_GetSessionInfo(), and module().

Referenced by Session().

◆ handle()

SessionHandle Botan::PKCS11::Session::handle ( ) const
inline
Returns
the session handle of this session

Definition at line 158 of file p11_types.h.

158{ return m_handle; }

Referenced by release(), and Session().

◆ init_pin()

void Botan::PKCS11::Session::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 at line 79 of file p11_session.cpp.

79 {
80 module()->C_InitPIN(m_handle, new_pin);
81}
bool C_InitPIN(SessionHandle session, const Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:221

References Botan::PKCS11::LowLevel::C_InitPIN(), and module().

Referenced by Botan::PKCS11::set_pin().

◆ login()

void Botan::PKCS11::Session::login ( UserType userType,
const secure_string & pin )

Login to this session

Parameters
userTypethe user type to use for the login
pinthe PIN of the user

Definition at line 57 of file p11_session.cpp.

57 {
58 module()->C_Login(m_handle, user_type, pin);
59 m_logged_in = true;
60}
bool C_Login(SessionHandle session, UserType user_type, const Utf8Char *pin_ptr, Ulong pin_len, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:290

References Botan::PKCS11::LowLevel::C_Login(), and module().

Referenced by Botan::PKCS11::change_pin(), Botan::PKCS11::change_so_pin(), and Botan::PKCS11::set_pin().

◆ logoff()

void Botan::PKCS11::Session::logoff ( )

Logout from this session.

Definition at line 62 of file p11_session.cpp.

62 {
63 module()->C_Logout(m_handle);
64 m_logged_in = false;
65}

References Botan::PKCS11::LowLevel::C_Logout(), and module().

◆ module()

Module & Botan::PKCS11::Session::module ( ) const
inline
Returns
a reference to the used module

Definition at line 161 of file p11_types.h.

161{ return m_slot.module(); }

Referenced by get_info(), init_pin(), login(), logoff(), Session(), set_pin(), and ~Session().

◆ operator=() [1/2]

Session & Botan::PKCS11::Session::operator= ( const Session & other)
delete

References Session().

◆ operator=() [2/2]

Session & Botan::PKCS11::Session::operator= ( Session && other)
delete

References Session().

◆ release()

SessionHandle Botan::PKCS11::Session::release ( )
Returns
the released session handle

Definition at line 51 of file p11_session.cpp.

51 {
53 std::swap(handle, m_handle);
54 return handle;
55}
CK_SESSION_HANDLE SessionHandle
Definition p11.h:1211

References handle().

◆ set_pin()

void Botan::PKCS11::Session::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).

Definition at line 74 of file p11_session.cpp.

74 {
75 module()->C_SetPIN(m_handle, old_pin, new_pin);
76}
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:229

References Botan::PKCS11::LowLevel::C_SetPIN(), and module().

Referenced by Botan::PKCS11::change_pin(), and Botan::PKCS11::change_so_pin().

◆ slot()

const Slot & Botan::PKCS11::Session::slot ( ) const
inline
Returns
a reference to the slot

Definition at line 155 of file p11_types.h.

155{ return m_slot; }

References slot().

Referenced by Session(), Session(), Session(), and slot().


The documentation for this class was generated from the following files: