Botan 3.13.0
Crypto and TLS for C&
p11_module.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 Module
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
11#include <botan/internal/dyn_load.h>
12
13namespace Botan::PKCS11 {
14
15Module::Module(std::string_view file_path, C_InitializeArgs init_args) : m_file_path(file_path) {
16 if(file_path.empty()) {
17 throw Invalid_Argument("PKCS11 no module path specified");
18 }
19 reload(init_args);
20}
21
22Module::Module(Module&& other) noexcept = default;
23
24Module::~Module() noexcept {
25 try {
26 if(m_low_level) {
27 m_low_level->C_Finalize(nullptr, nullptr);
28 }
29 } catch(...) {
30 // we are noexcept and must swallow any exception here
31 }
32}
33
35 // Build the new library + LowLevel in locals so library load and interface
36 // discovery failures leave m_library / m_low_level untouched.
37 auto new_library = std::make_unique<Dynamically_Loaded_Library>(m_file_path);
38 auto new_low_level = std::make_unique<LowLevel>(InterfaceWrapper::latest_p11_interface(*new_library));
39
40 // m_file_path is const, so dlopen typically returns the same handle as the
41 // old module and PKCS#11 state is shared; we must finalize before
42 // re-initializing. Clear the old members up front so a C_Initialize failure
43 // below leaves the Module empty rather than holding a function table for an
44 // already-finalized library.
45 if(m_low_level) {
46 m_low_level->C_Finalize(nullptr);
47 }
48 m_low_level.reset();
49 m_library.reset();
50
51 new_low_level->C_Initialize(&init_args);
52
53 m_low_level = std::move(new_low_level);
54 m_library = std::move(new_library);
55}
56
57} // namespace Botan::PKCS11
static InterfaceWrapper latest_p11_interface(Dynamically_Loaded_Library &library)
~Module() noexcept
Calls C_Finalize().
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})
CK_C_INITIALIZE_ARGS C_InitializeArgs
Definition p11.h:1194