Botan 3.11.0
Crypto and TLS for C&
p11_types.h
Go to the documentation of this file.
1/*
2* PKCS#11 Module/Slot/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#ifndef BOTAN_P11_TYPES_H_
10#define BOTAN_P11_TYPES_H_
11
12#include <botan/p11.h>
13#include <functional>
14#include <memory>
15#include <string>
16
17namespace Botan {
18
20
21namespace PKCS11 {
22
23/**
24* Loads the PKCS#11 shared library
25* Calls C_Initialize on load and C_Finalize on destruction
26*/
27class BOTAN_PUBLIC_API(2, 0) Module final {
28 public:
29 /**
30 * Loads the shared library and calls C_Initialize. The latest supported
31 * "PKCS 11" interface is used.
32 * @param file_path the path to the PKCS#11 shared library
33 * @param init_args flags to use for `C_Initialize`
34 */
36 std::string_view file_path,
37 C_InitializeArgs init_args = {
38 nullptr, nullptr, nullptr, nullptr, static_cast<CK_FLAGS>(Flag::OsLockingOk), nullptr});
39
40 Module(Module&& other) noexcept;
41 Module& operator=(Module&& other) = delete;
42
43 // Dtor calls C_Finalize(). A copy could be deleted while the origin still exists
44 // Furthermore std::unique_ptr member -> not copyable
45 Module(const Module& other) = delete;
46 Module& operator=(const Module& other) = delete;
47
48 /// Calls C_Finalize()
49 ~Module() noexcept;
50
51 /**
52 * Reloads the module and reinitializes it.
53 * @param init_args flags to use for `C_Initialize`
54 */
55 void reload(C_InitializeArgs init_args = {
56 nullptr, nullptr, nullptr, nullptr, static_cast<CK_FLAGS>(Flag::OsLockingOk), nullptr});
57
58 inline LowLevel* operator->() const { return m_low_level.get(); }
59
60 /// @return general information about Cryptoki
61 inline Info get_info() const {
62 Info info;
63 m_low_level->C_GetInfo(&info);
64 return info;
65 }
66
67 std::string_view library_path() const { return m_file_path; }
68
69 const Dynamically_Loaded_Library& library() { return *m_library; }
70
71 private:
72 const std::string m_file_path;
73 std::unique_ptr<Dynamically_Loaded_Library> m_library;
74 std::unique_ptr<LowLevel> m_low_level = nullptr;
75};
76
77/// Represents a PKCS#11 Slot, i.e., a card reader
78class BOTAN_PUBLIC_API(2, 0) Slot final {
79 public:
80 /**
81 * @param module the PKCS#11 module to use
82 * @param slot_id the slot id to use
83 */
85
86 /// @return a reference to the module that is used
87 inline Module& module() const { return m_module; }
88
89 /// @return the slot id
90 inline SlotId slot_id() const { return m_slot_id; }
91
92 /**
93 * Get available slots
94 * @param module the module to use
95 * @param token_present true if only slots with attached tokens should be returned, false for all slots
96 * @return a list of available slots (calls C_GetSlotList)
97 */
98 static std::vector<SlotId> get_available_slots(Module& module, bool token_present);
99
100 /// @return information about the slot (`C_GetSlotInfo`)
101 SlotInfo get_slot_info() const;
102
103 /// Obtains a list of mechanism types supported by the slot (`C_GetMechanismList`)
104 std::vector<MechanismType> get_mechanism_list() const;
105
106 /// Obtains information about a particular mechanism possibly supported by a slot (`C_GetMechanismInfo`)
107 MechanismInfo get_mechanism_info(MechanismType mechanism_type) const;
108
109 /// Obtains information about a particular token in the system (`C_GetTokenInfo`)
110 TokenInfo get_token_info() const;
111
112 /**
113 * Calls `C_InitToken` to initialize the token
114 * @param label the label for the token (must not exceed 32 bytes according to PKCS#11)
115 * @param so_pin the PIN of the security officer
116 */
117 void initialize(std::string_view label, const secure_string& so_pin) const;
118
119 private:
120 const std::reference_wrapper<Module> m_module;
121 const SlotId m_slot_id;
122};
123
124/// Represents a PKCS#11 session
125class BOTAN_PUBLIC_API(2, 0) Session final {
126 public:
127 /**
128 * @param slot the slot to use
129 * @param read_only true if the session should be read only, false to create a read-write session
130 */
131 Session(Slot& slot, bool read_only);
132
133 /**
134 * @param slot the slot to use
135 * @param flags the flags to use for the session. Remark: Flag::SerialSession is mandatory
136 * @param callback_data application-defined pointer to be passed to the notification callback
137 * @param notify_callback address of the notification callback function
138 */
139 Session(Slot& slot, Flags flags, VoidPtr callback_data, Notify notify_callback);
140
141 /// Takes ownership of a session
143
144 Session(Session&& other) = default;
145 Session& operator=(Session&& other) = delete;
146
147 // Dtor calls C_CloseSession() and eventually C_Logout. A copy could close the session while the origin still exists
148 Session(const Session& other) = delete;
149 Session& operator=(const Session& other) = delete;
150
151 /// Logout user and close the session on destruction
152 ~Session() noexcept;
153
154 /// @return a reference to the slot
155 inline const Slot& slot() const { return m_slot; }
156
157 /// @return the session handle of this session
158 inline SessionHandle handle() const { return m_handle; }
159
160 /// @return a reference to the used module
161 inline Module& module() const { return m_slot.module(); }
162
163 /// @return the released session handle
164 SessionHandle release();
165
166 /**
167 * Login to this session
168 * @param userType the user type to use for the login
169 * @param pin the PIN of the user
170 */
171 void login(UserType userType, const secure_string& pin);
172
173 /// Logout from this session
174 void logoff();
175
176 /// @return information about this session
177 SessionInfo get_info() const;
178
179 /// Calls `C_SetPIN` to change the PIN using the old PIN (requires a logged in session)
180 void set_pin(const secure_string& old_pin, const secure_string& new_pin);
181
182 /// Calls `C_InitPIN` to change or initialize the PIN using the SO_PIN (requires a logged in session)
183 void init_pin(const secure_string& new_pin);
184
185 private:
186 const Slot& m_slot;
187 SessionHandle m_handle;
188 bool m_logged_in;
189};
190
191} // namespace PKCS11
192} // namespace Botan
193
194#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
Provides access to all PKCS #11 functions.
Definition p11.h:1322
void reload(C_InitializeArgs init_args={ nullptr, nullptr, nullptr, nullptr, static_cast< CK_FLAGS >(Flag::OsLockingOk), nullptr})
BOTAN_FUTURE_EXPLICIT Module(std::string_view file_path, C_InitializeArgs init_args={ nullptr, nullptr, nullptr, nullptr, static_cast< CK_FLAGS >(Flag::OsLockingOk), nullptr})
const Dynamically_Loaded_Library & library()
Definition p11_types.h:69
Info get_info() const
Definition p11_types.h:61
Module(Module &&other) noexcept
LowLevel * operator->() const
Definition p11_types.h:58
Module & operator=(Module &&other)=delete
std::string_view library_path() const
Definition p11_types.h:67
Module & operator=(const Module &other)=delete
Module(const Module &other)=delete
Session(Session &&other)=default
Session & operator=(Session &&other)=delete
Session(const Session &other)=delete
Module & module() const
Definition p11_types.h:161
const Slot & slot() const
Definition p11_types.h:155
SessionHandle handle() const
Definition p11_types.h:158
Session & operator=(const Session &other)=delete
Session(Slot &slot, bool read_only)
Represents a PKCS#11 Slot, i.e., a card reader.
Definition p11_types.h:78
Module & module() const
Definition p11_types.h:87
Slot(Module &module, SlotId slot_id)
Definition p11_slot.cpp:13
SlotId slot_id() const
Definition p11_types.h:90
CK_SLOT_ID SlotId
Definition p11.h:1202
secure_vector< uint8_t > secure_string
Definition p11.h:45
CK_C_INITIALIZE_ARGS C_InitializeArgs
Definition p11.h:1193
CK_NOTIFY Notify
Definition p11.h:1209
CK_INFO Info
Definition p11.h:1199
CK_SLOT_INFO SlotInfo
Definition p11.h:1204
CK_VOID_PTR VoidPtr
Definition p11.h:1192
CK_FLAGS Flags
Definition p11.h:1198
CK_SESSION_INFO SessionInfo
Definition p11.h:1211
CK_TOKEN_INFO TokenInfo
Definition p11.h:1205
void set_pin(Slot &slot, const secure_string &so_pin, const secure_string &pin)
Definition p11.cpp:58
CK_MECHANISM_INFO MechanismInfo
Definition p11.h:1207
Flags flags(Flag flags)
Definition p11.h:1227
CK_SESSION_HANDLE SessionHandle
Definition p11.h:1210
CK_ULONG CK_FLAGS
Definition pkcs11.h:49