• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 22022566023

14 Feb 2026 06:55PM UTC coverage: 90.067%. Remained the same
22022566023

push

github

web-flow
Merge pull request #5334 from randombit/jack/test-h-remove-confirm

Remove confirm test helper, use test_is_true or test_is_false instead

102260 of 113538 relevant lines covered (90.07%)

11510296.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.69
/src/tests/test_pkcs11_low_level.cpp
1
/*
2
* (C) 2016 Daniel Neus
3
* (C) 2016 Philipp Weber
4
* (C) 2019 Michael Boric
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_PKCS11)
12
   #include "test_pkcs11.h"
13
   #include <botan/p11.h>
14
   #include <botan/internal/dyn_load.h>
15
   #include <array>
16
   #include <functional>
17
   #include <map>
18
   #include <memory>
19
   #include <string>
20
   #include <vector>
21
#endif
22

23
namespace Botan_Tests {
24

25
namespace {
26

27
#if defined(BOTAN_HAS_PKCS11)
28
   #if defined(BOTAN_HAS_DYNAMIC_LOADER)
29

30
using namespace Botan;
31
using namespace PKCS11;
32

33
class RAII_LowLevel {
34
   public:
35
      RAII_LowLevel() :
20✔
36
            m_module(Test::pkcs11_lib()),
20✔
37
            m_func_list(nullptr),
20✔
38
            m_session_handle(0),
20✔
39
            m_is_session_open(false),
20✔
40
            m_is_logged_in(false) {
20✔
41
         LowLevel::C_GetFunctionList(m_module, &m_func_list);
20✔
42
         m_low_level = std::make_unique<LowLevel>(m_func_list);
20✔
43

44
         C_InitializeArgs init_args = {
20✔
45
            nullptr, nullptr, nullptr, nullptr, static_cast<CK_FLAGS>(Flag::OsLockingOk), nullptr};
46

47
         m_low_level->C_Initialize(&init_args);
20✔
48
      }
20✔
49

50
      ~RAII_LowLevel() noexcept {
20✔
51
         try {
20✔
52
            if(m_is_session_open) {
20✔
53
               if(m_is_logged_in) {
10✔
54
                  m_low_level->C_Logout(m_session_handle, nullptr);
7✔
55
               }
56

57
               m_low_level->C_CloseSession(m_session_handle, nullptr);
10✔
58
            }
59
            m_low_level->C_Finalize(nullptr, nullptr);
20✔
60
         } catch(...) {
×
61
            // ignore errors here
62
         }
×
63
      }
40✔
64

65
      RAII_LowLevel(const RAII_LowLevel& other) = delete;
66
      RAII_LowLevel(RAII_LowLevel&& other) = delete;
67
      RAII_LowLevel& operator=(const RAII_LowLevel& other) = delete;
68
      RAII_LowLevel& operator=(RAII_LowLevel&& other) = delete;
69

70
      std::vector<SlotId> get_slots(bool token_present) const {
19✔
71
         std::vector<SlotId> slots;
19✔
72
         m_low_level->C_GetSlotList(token_present, slots);
19✔
73

74
         if(slots.empty()) {
19✔
75
            throw Test_Error("No slot with attached token found");
×
76
         }
77

78
         return slots;
19✔
79
      }
×
80

81
      SessionHandle open_session(Flags session_flags) {
11✔
82
         std::vector<SlotId> slots = get_slots(true);
11✔
83
         m_low_level->C_OpenSession(slots.at(0), session_flags, nullptr, nullptr, &m_session_handle);
11✔
84
         m_is_session_open = true;
11✔
85
         return m_session_handle;
11✔
86
      }
11✔
87

88
      SessionHandle open_rw_session_with_user_login() {
3✔
89
         const Flags session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
3✔
90
         const SessionHandle handle = open_session(session_flags);
3✔
91
         login(UserType::User, PIN());
3✔
92
         return handle;
3✔
93
      }
94

95
      SessionHandle get_session_handle() const {
4✔
96
         if(!m_is_session_open) {
4✔
97
            throw Test_Error("no open session");
×
98
         }
99
         return m_session_handle;
4✔
100
      }
101

102
      void close_session() {
1✔
103
         if(!m_is_session_open) {
1✔
104
            throw Test_Error("no open session");
×
105
         }
106

107
         m_low_level->C_CloseSession(m_session_handle);
1✔
108
         m_is_session_open = false;
1✔
109
      }
1✔
110

111
      void login(UserType user_type, const secure_vector<uint8_t>& pin) {
8✔
112
         if(!m_is_session_open) {
8✔
113
            throw Test_Error("no open session");
×
114
         }
115

116
         if(m_is_logged_in) {
8✔
117
            throw Test_Error("Already logged in");
×
118
         }
119

120
         m_low_level->C_Login(m_session_handle, user_type, pin);
8✔
121
         m_is_logged_in = true;
8✔
122
      }
8✔
123

124
      void logout() {
1✔
125
         if(!m_is_logged_in) {
1✔
126
            throw Test_Error("Not logged in");
×
127
         }
128

129
         m_low_level->C_Logout(m_session_handle);
1✔
130
         m_is_logged_in = false;
1✔
131
      }
1✔
132

133
      LowLevel* get() const { return m_low_level.get(); }
1✔
134

135
   private:
136
      Dynamically_Loaded_Library m_module;
137
      FunctionList* m_func_list;
138
      std::unique_ptr<LowLevel> m_low_level;
139
      SessionHandle m_session_handle;
140
      bool m_is_session_open;
141
      bool m_is_logged_in;
142
};
143

144
bool no_op(ReturnValue* /*unused*/) {
×
145
   return true;
×
146
}
147

148
using PKCS11_BoundTestFunction = std::function<bool(ReturnValue* return_value)>;
149

150
// tests all 3 variants
151
Test::Result test_function(const std::string& name,
26✔
152
                           const PKCS11_BoundTestFunction& test_func,
153
                           const std::string& revert_fn_name,
154
                           const PKCS11_BoundTestFunction& revert_func,
155
                           bool expect_failure,
156
                           ReturnValue expected_return_value) {
157
   const std::string test_name =
26✔
158
      revert_fn_name.empty() ? "PKCS 11 low level - " + name : "PKCS 11 low level - " + name + "/" + revert_fn_name;
56✔
159
   Test::Result result(test_name);
26✔
160

161
   // test throw variant
162
   if(expect_failure) {
26✔
163
      result.test_throws(name + " fails as expected", [&test_func]() { test_func(ThrowException); });
3✔
164
   } else {
165
      test_func(ThrowException);
25✔
166
      result.test_success(name + " did not throw and completed successfully");
25✔
167

168
      if(!revert_fn_name.empty()) {
25✔
169
         revert_func(ThrowException);
10✔
170
         result.test_success(revert_fn_name + " did not throw and completed successfully");
20✔
171
      }
172
   }
173

174
   // test bool return variant
175
   bool success = test_func(nullptr);
26✔
176
   result.test_bool_eq(name, success, !expect_failure);
26✔
177
   if(success && !revert_fn_name.empty()) {
26✔
178
      success = revert_func(nullptr);
10✔
179
      result.test_bool_eq(revert_fn_name, success, !expect_failure);
10✔
180
   }
181

182
   // test ReturnValue variant
183
   ReturnValue rv = ReturnValue::OK;
26✔
184
   success = test_func(&rv);
26✔
185
   result.test_bool_eq(name, success, !expect_failure);
26✔
186
   if(!expect_failure) {
26✔
187
      result.test_u32_eq(name, static_cast<uint32_t>(rv), 0);
25✔
188
   } else {
189
      result.test_u32_eq(name, static_cast<uint32_t>(rv), static_cast<uint32_t>(expected_return_value));
1✔
190
   }
191

192
   if(success && !revert_fn_name.empty()) {
26✔
193
      success = revert_func(&rv);
10✔
194
      result.test_bool_eq(revert_fn_name, success, !expect_failure);
10✔
195
      result.test_u32_eq(revert_fn_name, static_cast<uint32_t>(rv), 0);
10✔
196
   }
197

198
   return result;
26✔
199
}
26✔
200

201
Test::Result test_function(const std::string& name,
1✔
202
                           const PKCS11_BoundTestFunction& test_func,
203
                           bool expect_failure,
204
                           ReturnValue expected_return_value) {
205
   return test_function(name, test_func, std::string(), no_op, expect_failure, expected_return_value);
3✔
206
}
207

208
Test::Result test_function(const std::string& name, const PKCS11_BoundTestFunction& test_func) {
15✔
209
   return test_function(name, test_func, std::string(), no_op, false, ReturnValue::OK);
45✔
210
}
211

212
Test::Result test_function(const std::string& name,
10✔
213
                           const PKCS11_BoundTestFunction& test_func,
214
                           const std::string& revert_fn_name,
215
                           const PKCS11_BoundTestFunction& revert_func) {
216
   return test_function(name, test_func, revert_fn_name, revert_func, false, ReturnValue::OK);
10✔
217
}
218

219
Test::Result test_low_level_ctor() {
1✔
220
   Test::Result result("PKCS 11 low level - LowLevel ctor");
1✔
221

222
   const Dynamically_Loaded_Library pkcs11_module(Test::pkcs11_lib());
1✔
223
   FunctionList* func_list(nullptr);
1✔
224
   LowLevel::C_GetFunctionList(pkcs11_module, &func_list);
1✔
225

226
   const LowLevel p11_low_level(func_list);
1✔
227
   result.test_success("LowLevel ctor does complete for valid function list");
1✔
228

229
   result.test_throws("LowLevel ctor fails for invalid function list pointer", []() { LowLevel(nullptr); });
2✔
230

231
   return result;
1✔
232
}
1✔
233

234
// NOLINTBEGIN(*-avoid-bind)
235

236
Test::Result test_c_get_function_list() {
1✔
237
   Dynamically_Loaded_Library pkcs11_module(Test::pkcs11_lib());
1✔
238
   // NOLINTNEXTLINE(*-const-correctness) bug in clang-tidy
239
   FunctionList* func_list = nullptr;
1✔
240
   return test_function(
2✔
241
      "C_GetFunctionList",
242
      std::bind(&LowLevel::C_GetFunctionList, std::ref(pkcs11_module), &func_list, std::placeholders::_1));
3✔
243
}
1✔
244

245
Test::Result test_initialize_finalize() {
1✔
246
   const Dynamically_Loaded_Library pkcs11_module(Test::pkcs11_lib());
1✔
247
   FunctionList* func_list = nullptr;
1✔
248
   LowLevel::C_GetFunctionList(pkcs11_module, &func_list);
1✔
249

250
   LowLevel p11_low_level(func_list);
1✔
251

252
   // setting Flag::OsLockingOk should be the normal use case
253
   C_InitializeArgs init_args = {nullptr, nullptr, nullptr, nullptr, static_cast<CK_FLAGS>(Flag::OsLockingOk), nullptr};
1✔
254

255
   auto init_bind = std::bind(&LowLevel::C_Initialize, std::ref(p11_low_level), &init_args, std::placeholders::_1);
1✔
256
   auto finalize_bind = std::bind(&LowLevel::C_Finalize, std::ref(p11_low_level), nullptr, std::placeholders::_1);
1✔
257
   return test_function("C_Initialize", init_bind, "C_Finalize", finalize_bind);
4✔
258
}
1✔
259

260
Test::Result test_c_get_info() {
1✔
261
   const RAII_LowLevel p11_low_level;
1✔
262

263
   Info info = {};
1✔
264
   Test::Result result =
1✔
265
      test_function("C_GetInfo", std::bind(&LowLevel::C_GetInfo, p11_low_level.get(), &info, std::placeholders::_1));
2✔
266
   result.test_sz_ne("C_GetInfo crypto major version", info.cryptokiVersion.major, 0);
1✔
267

268
   return result;
1✔
269
}
1✔
270

271
Test::Result test_c_get_slot_list() {
1✔
272
   const RAII_LowLevel p11_low_level;
1✔
273

274
   std::vector<SlotId> slot_vec;
1✔
275

276
   // assumes smartcard reader is attached without card
277

278
   auto slots_no_card = std::bind(
1✔
279
      static_cast<bool (LowLevel::*)(bool, std::vector<SlotId>&, ReturnValue*) const>(&LowLevel::C_GetSlotList),
1✔
280
      p11_low_level.get(),
1✔
281
      false,  // no card present
1✔
282
      std::ref(slot_vec),
1✔
283
      std::placeholders::_1);
1✔
284

285
   Test::Result result = test_function("C_GetSlotList", slots_no_card);
2✔
286
   result.test_sz_ne("C_GetSlotList number of slots without attached token > 0", slot_vec.size(), 0);
1✔
287

288
   // assumes smartcard reader is attached with a card
289

290
   auto slots_with_card = std::bind(
1✔
291
      static_cast<bool (LowLevel::*)(bool, std::vector<SlotId>&, ReturnValue*) const>(&LowLevel::C_GetSlotList),
1✔
292
      p11_low_level.get(),
1✔
293
      true,  // card present
1✔
294
      std::ref(slot_vec),
1✔
295
      std::placeholders::_1);
1✔
296

297
   slot_vec.clear();
1✔
298
   result.merge(test_function("C_GetSlotList", slots_with_card));
1✔
299
   result.test_sz_ne("C_GetSlotList number of slots with attached token > 0", slot_vec.size(), 0);
1✔
300

301
   return result;
1✔
302
}
1✔
303

304
Test::Result test_c_get_slot_info() {
1✔
305
   const RAII_LowLevel p11_low_level;
1✔
306
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(false);
1✔
307

308
   SlotInfo slot_info = {};
1✔
309
   Test::Result result = test_function(
2✔
310
      "C_GetSlotInfo",
311
      std::bind(&LowLevel::C_GetSlotInfo, p11_low_level.get(), slot_vec.at(0), &slot_info, std::placeholders::_1));
2✔
312

313
   const std::string slot_desc(reinterpret_cast<char*>(slot_info.slotDescription));
1✔
314
   result.test_sz_ne("C_GetSlotInfo returns non empty description", slot_desc.size(), 0);
1✔
315

316
   return result;
2✔
317
}
1✔
318

319
Test::Result test_c_get_token_info() {
1✔
320
   const RAII_LowLevel p11_low_level;
1✔
321
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
322

323
   TokenInfo token_info = {};
1✔
324
   Test::Result result = test_function(
1✔
325
      "C_GetTokenInfo",
326
      std::bind(&LowLevel::C_GetTokenInfo, p11_low_level.get(), slot_vec.at(0), &token_info, std::placeholders::_1));
3✔
327

328
   const std::string serial(reinterpret_cast<char*>(token_info.serialNumber));
1✔
329
   result.test_sz_ne("C_GetTokenInfo returns non empty serial number", serial.size(), 0);
1✔
330

331
   return result;
2✔
332
}
1✔
333

334
Test::Result test_c_wait_for_slot_event() {
1✔
335
   const RAII_LowLevel p11_low_level;
1✔
336

337
   const Flags flags = PKCS11::flags(Flag::DontBlock);
1✔
338
   SlotId slot_id = 0;
1✔
339

340
   return test_function(
1✔
341
      "C_WaitForSlotEvent",
342
      std::bind(&LowLevel::C_WaitForSlotEvent, p11_low_level.get(), flags, &slot_id, nullptr, std::placeholders::_1),
1✔
343
      true,
344
      ReturnValue::NoEvent);
4✔
345
}
1✔
346

347
Test::Result test_c_get_mechanism_list() {
1✔
348
   const RAII_LowLevel p11_low_level;
1✔
349
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
350

351
   std::vector<MechanismType> mechanisms;
1✔
352

353
   auto binder = std::bind(static_cast<bool (LowLevel::*)(SlotId, std::vector<MechanismType>&, ReturnValue*) const>(
1✔
354
                              &LowLevel::C_GetMechanismList),
355
                           p11_low_level.get(),
1✔
356
                           slot_vec.at(0),
1✔
357
                           std::ref(mechanisms),
1✔
358
                           std::placeholders::_1);
1✔
359

360
   Test::Result result = test_function("C_GetMechanismList", binder);
2✔
361
   result.test_is_true("C_GetMechanismList returns non empty mechanisms list", !mechanisms.empty());
1✔
362

363
   return result;
1✔
364
}
1✔
365

366
Test::Result test_c_get_mechanism_info() {
1✔
367
   const RAII_LowLevel p11_low_level;
1✔
368
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
369

370
   std::vector<MechanismType> mechanisms;
1✔
371
   p11_low_level.get()->C_GetMechanismList(slot_vec.at(0), mechanisms);
1✔
372

373
   MechanismInfo mechanism_info = {};
1✔
374
   return test_function("C_GetMechanismInfo",
1✔
375
                        std::bind(&LowLevel::C_GetMechanismInfo,
2✔
376
                                  p11_low_level.get(),
1✔
377
                                  slot_vec.at(0),
1✔
378
                                  mechanisms.at(0),
1✔
379
                                  &mechanism_info,
1✔
380
                                  std::placeholders::_1));
4✔
381
}
1✔
382

383
Test::Result test_c_init_token() {
1✔
384
   const RAII_LowLevel p11_low_level;
1✔
385
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
386

387
   const std::string token_label = "Botan PKCS#11 tests";
1✔
388
   std::string_view label_view(token_label);
1✔
389

390
   auto sec_vec_binder = std::bind(
1✔
391
      static_cast<bool (LowLevel::*)(SlotId, const secure_vector<uint8_t>&, std::string_view, ReturnValue*) const>(
1✔
392
         &LowLevel::C_InitToken<secure_allocator<uint8_t>>),
393
      p11_low_level.get(),
1✔
394
      slot_vec.at(0),
1✔
395
      SO_PIN(),
×
396
      std::ref(label_view),
1✔
397
      std::placeholders::_1);
1✔
398

399
   return test_function("C_InitToken", sec_vec_binder);
4✔
400
}
1✔
401

402
Test::Result test_open_close_session() {
1✔
403
   const RAII_LowLevel p11_low_level;
1✔
404
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
405

406
   // public read only session
407
   const Flags ro_flags = PKCS11::flags(Flag::SerialSession);
1✔
408
   SessionHandle session_handle = 0;
1✔
409

410
   auto open_session_ro = std::bind(&LowLevel::C_OpenSession,
2✔
411
                                    p11_low_level.get(),
1✔
412
                                    slot_vec.at(0),
1✔
413
                                    ro_flags,
414
                                    nullptr,
1✔
415
                                    nullptr,
416
                                    &session_handle,
417
                                    std::placeholders::_1);
1✔
418

419
   auto close_session =
1✔
420
      std::bind(&LowLevel::C_CloseSession, p11_low_level.get(), std::ref(session_handle), std::placeholders::_1);
1✔
421

422
   Test::Result result = test_function("C_OpenSession", open_session_ro, "C_CloseSession", close_session);
3✔
423

424
   // public read write session
425
   const Flags rw_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
426

427
   auto open_session_rw = std::bind(&LowLevel::C_OpenSession,
2✔
428
                                    p11_low_level.get(),
1✔
429
                                    slot_vec.at(0),
1✔
430
                                    rw_flags,
431
                                    nullptr,
1✔
432
                                    nullptr,
433
                                    &session_handle,
434
                                    std::placeholders::_1);
1✔
435

436
   result.merge(test_function("C_OpenSession", open_session_rw, "C_CloseSession", close_session));
3✔
437

438
   return result;
1✔
439
}
1✔
440

441
Test::Result test_c_close_all_sessions() {
1✔
442
   RAII_LowLevel p11_low_level;
1✔
443
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
444

445
   auto open_two_sessions = [&slot_vec, &p11_low_level]() -> void {
4✔
446
      // public read only session
447
      Flags flags = PKCS11::flags(Flag::SerialSession);
3✔
448
      SessionHandle first_session_handle = 0;
3✔
449
      SessionHandle second_session_handle = 0;
3✔
450

451
      p11_low_level.get()->C_OpenSession(slot_vec.at(0), flags, nullptr, nullptr, &first_session_handle);
3✔
452

453
      flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
3✔
454
      p11_low_level.get()->C_OpenSession(slot_vec.at(0), flags, nullptr, nullptr, &second_session_handle);
3✔
455
   };
4✔
456

457
   open_two_sessions();
1✔
458

459
   Test::Result result("PKCS 11 low level - C_CloseAllSessions");
1✔
460

461
   // test throw variant
462
   p11_low_level.get()->C_CloseAllSessions(slot_vec.at(0));
1✔
463
   result.test_success("C_CloseAllSessions does not throw");
1✔
464

465
   // test bool return variant
466
   open_two_sessions();
1✔
467

468
   bool success = p11_low_level.get()->C_CloseAllSessions(slot_vec.at(0), nullptr);
1✔
469
   result.test_is_true("C_CloseAllSessions", success);
1✔
470

471
   // test ReturnValue variant
472
   open_two_sessions();
1✔
473

474
   ReturnValue rv = ReturnValue::OK;
1✔
475
   success = p11_low_level.get()->C_CloseAllSessions(slot_vec.at(0), &rv);
1✔
476
   result.test_is_true("C_CloseAllSessions", success);
1✔
477
   result.test_u32_eq("C_CloseAllSessions", static_cast<uint32_t>(rv), 0);
1✔
478

479
   return result;
1✔
480
}
1✔
481

482
Test::Result test_c_get_session_info() {
1✔
483
   RAII_LowLevel p11_low_level;
1✔
484
   std::vector<SlotId> slot_vec = p11_low_level.get_slots(true);
1✔
485

486
   // public read only session
487
   const Flags flags = PKCS11::flags(Flag::SerialSession);
1✔
488
   const SessionHandle session_handle = p11_low_level.open_session(flags);
1✔
489

490
   SessionInfo session_info = {};
1✔
491
   Test::Result result = test_function(
1✔
492
      "C_GetSessionInfo",
493
      std::bind(
1✔
494
         &LowLevel::C_GetSessionInfo, p11_low_level.get(), session_handle, &session_info, std::placeholders::_1));
3✔
495

496
   result.test_is_true("C_GetSessionInfo returns same slot id as during call to C_OpenSession",
3✔
497
                       session_info.slotID == slot_vec.at(0));
1✔
498
   result.test_is_true("C_GetSessionInfo returns same flags as during call to C_OpenSession",
2✔
499
                       session_info.flags == flags);
1✔
500
   result.test_is_true("C_GetSessionInfo returns public read only session state",
2✔
501
                       session_info.state == static_cast<CK_FLAGS>(SessionState::RoPublicSession));
1✔
502

503
   return result;
1✔
504
}
1✔
505

506
Test::Result login_logout_helper(const RAII_LowLevel& p11_low_level,
3✔
507
                                 SessionHandle handle,
508
                                 UserType user_type,
509
                                 std::string_view pin) {
510
   secure_vector<uint8_t> pin_as_sec_vec(pin.begin(), pin.end());
3✔
511

512
   auto login_secvec_binder = std::bind(
3✔
513
      static_cast<bool (LowLevel::*)(SessionHandle, UserType, const secure_vector<uint8_t>&, ReturnValue*) const>(
3✔
514
         &LowLevel::C_Login<secure_allocator<uint8_t>>),
515
      p11_low_level.get(),
3✔
516
      handle,
517
      user_type,
518
      std::ref(pin_as_sec_vec),
3✔
519
      std::placeholders::_1);
3✔
520

521
   auto logout_binder =
3✔
522
      std::bind(static_cast<bool (LowLevel::*)(SessionHandle, ReturnValue*) const>(&LowLevel::C_Logout),
3✔
523
                p11_low_level.get(),
3✔
524
                handle,
525
                std::placeholders::_1);
3✔
526

527
   return test_function("C_Login", login_secvec_binder, "C_Logout", logout_binder);
12✔
528
}
3✔
529

530
Test::Result test_c_login_logout_security_officier() {
1✔
531
   RAII_LowLevel p11_low_level;
1✔
532

533
   // can only login to R/W session
534
   const Flags session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
535
   const SessionHandle session_handle = p11_low_level.open_session(session_flags);
1✔
536

537
   return login_logout_helper(p11_low_level, session_handle, UserType::SO, PKCS11_SO_PIN);
2✔
538
}
1✔
539

540
Test::Result test_c_login_logout_user() {
1✔
541
   RAII_LowLevel p11_low_level;
1✔
542

543
   // R/O session
544
   Flags session_flags = PKCS11::flags(Flag::SerialSession);
1✔
545
   SessionHandle session_handle = p11_low_level.open_session(session_flags);
1✔
546
   Test::Result result = login_logout_helper(p11_low_level, session_handle, UserType::User, PKCS11_USER_PIN);
1✔
547
   p11_low_level.close_session();
1✔
548

549
   // R/W session
550
   session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
551
   session_handle = p11_low_level.open_session(session_flags);
1✔
552

553
   result.merge(login_logout_helper(p11_low_level, session_handle, UserType::User, PKCS11_USER_PIN));
1✔
554

555
   return result;
1✔
556
}
1✔
557

558
Test::Result test_c_init_pin() {
1✔
559
   RAII_LowLevel p11_low_level;
1✔
560

561
   // C_InitPIN can only be called in the "R/W SO Functions" state
562
   const Flags session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
563
   const SessionHandle session_handle = p11_low_level.open_session(session_flags);
1✔
564

565
   p11_low_level.login(UserType::SO, SO_PIN());
1✔
566

567
   auto sec_vec_binder =
1✔
568
      std::bind(static_cast<bool (LowLevel::*)(SessionHandle, const secure_vector<uint8_t>&, ReturnValue*) const>(
1✔
569
                   &LowLevel::C_InitPIN<secure_allocator<uint8_t>>),
570
                p11_low_level.get(),
1✔
571
                session_handle,
572
                PIN(),
1✔
573
                std::placeholders::_1);
1✔
574

575
   return test_function("C_InitPIN", sec_vec_binder);
4✔
576
}
1✔
577

578
Test::Result test_c_set_pin() {
1✔
579
   RAII_LowLevel p11_low_level;
1✔
580

581
   // C_SetPIN can only be called in the "R / W Public Session" state, "R / W SO Functions" state, or "R / W User Functions" state
582
   const Flags session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
583
   SessionHandle session_handle = p11_low_level.open_session(session_flags);
1✔
584

585
   // now we are in "R / W Public Session" state: this will change the pin of the user
586

587
   auto get_pin_bind = [&session_handle, &p11_low_level](
5✔
588
                          const secure_vector<uint8_t>& old_pin,
589
                          const secure_vector<uint8_t>& new_pin) -> PKCS11_BoundTestFunction {
590
      return std::bind(
12✔
591
         static_cast<bool (LowLevel::*)(
4✔
592
            SessionHandle, const secure_vector<uint8_t>&, const secure_vector<uint8_t>&, ReturnValue*) const>(
593
            &LowLevel::C_SetPIN<secure_allocator<uint8_t>>),
594
         p11_low_level.get(),
4✔
595
         session_handle,
596
         old_pin,
597
         new_pin,
598
         std::placeholders::_1);
8✔
599
   };
1✔
600

601
   const std::string test_pin("654321");
1✔
602
   const auto test_pin_secvec = secure_vector<uint8_t>(test_pin.begin(), test_pin.end());
1✔
603

604
   const PKCS11_BoundTestFunction set_pin_bind = get_pin_bind(PIN(), test_pin_secvec);
1✔
605
   const PKCS11_BoundTestFunction revert_pin_bind = get_pin_bind(test_pin_secvec, PIN());
1✔
606

607
   Test::Result result = test_function("C_SetPIN", set_pin_bind, "C_SetPIN", revert_pin_bind);
2✔
608

609
   // change pin in "R / W User Functions" state
610
   p11_low_level.login(UserType::User, PIN());
1✔
611

612
   result.merge(test_function("C_SetPIN", set_pin_bind, "C_SetPIN", revert_pin_bind));
2✔
613
   p11_low_level.logout();
1✔
614

615
   // change so_pin in "R / W SO Functions" state
616
   const std::string test_so_pin = "87654321";
1✔
617
   const secure_vector<uint8_t> test_so_pin_secvec(test_so_pin.begin(), test_so_pin.end());
1✔
618
   p11_low_level.login(UserType::SO, SO_PIN());
1✔
619

620
   const PKCS11_BoundTestFunction set_so_pin_bind = get_pin_bind(SO_PIN(), test_so_pin_secvec);
1✔
621
   const PKCS11_BoundTestFunction revert_so_pin_bind = get_pin_bind(test_so_pin_secvec, SO_PIN());
1✔
622

623
   result.merge(test_function("C_SetPIN", set_so_pin_bind, "C_SetPIN", revert_so_pin_bind));
2✔
624

625
   return result;
2✔
626
}
6✔
627

628
// Simple data object
629
const ObjectClass object_class = ObjectClass::Data;
630
const std::string_view label = "A data object";
631
const std::string_view data = "Sample data";
632
const Bbool btrue = True;
633

634
const std::array<Attribute, 4> data_template = {
635
   {{static_cast<CK_ATTRIBUTE_TYPE>(AttributeType::Class),
636
     const_cast<ObjectClass*>(&object_class),
637
     sizeof(object_class)},
638
    {static_cast<CK_ATTRIBUTE_TYPE>(AttributeType::Token), const_cast<Bbool*>(&btrue), sizeof(btrue)},
639
    {static_cast<CK_ATTRIBUTE_TYPE>(AttributeType::Label),
640
     const_cast<char*>(label.data()),
641
     static_cast<CK_ULONG>(label.size())},
642
    {static_cast<CK_ATTRIBUTE_TYPE>(AttributeType::Value),
643
     const_cast<char*>(data.data()),
644
     static_cast<CK_ULONG>(data.size())}}};
645

646
ObjectHandle create_simple_data_object(const RAII_LowLevel& p11_low_level) {
4✔
647
   ObjectHandle object_handle = {};
4✔
648

649
   auto dtemplate = data_template;
4✔
650
   p11_low_level.get()->C_CreateObject(
4✔
651
      p11_low_level.get_session_handle(), dtemplate.data(), static_cast<Ulong>(dtemplate.size()), &object_handle);
652
   return object_handle;
4✔
653
}
654

655
Test::Result test_c_create_object_c_destroy_object() {
1✔
656
   RAII_LowLevel p11_low_level;
1✔
657
   const SessionHandle session_handle = p11_low_level.open_rw_session_with_user_login();
1✔
658

659
   ObjectHandle object_handle(0);
1✔
660

661
   auto dtemplate = data_template;
1✔
662

663
   auto create_bind = std::bind(&LowLevel::C_CreateObject,
1✔
664
                                p11_low_level.get(),
1✔
665
                                session_handle,
666
                                dtemplate.data(),
1✔
667
                                static_cast<Ulong>(dtemplate.size()),
1✔
668
                                &object_handle,
669
                                std::placeholders::_1);
1✔
670

671
   auto destroy_bind = std::bind(
1✔
672
      &LowLevel::C_DestroyObject, p11_low_level.get(), session_handle, std::ref(object_handle), std::placeholders::_1);
1✔
673

674
   return test_function("C_CreateObject", create_bind, "C_DestroyObject", destroy_bind);
4✔
675
}
1✔
676

677
Test::Result test_c_get_object_size() {
1✔
678
   RAII_LowLevel p11_low_level;
1✔
679

680
   const Flags session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
681
   const SessionHandle session_handle = p11_low_level.open_session(session_flags);
1✔
682

683
   p11_low_level.login(UserType::User, PIN());
1✔
684

685
   const ObjectHandle object_handle = create_simple_data_object(p11_low_level);
1✔
686
   Ulong object_size = 0;
1✔
687

688
   auto bind = std::bind(&LowLevel::C_GetObjectSize,
1✔
689
                         p11_low_level.get(),
1✔
690
                         session_handle,
691
                         object_handle,
692
                         &object_size,
1✔
693
                         std::placeholders::_1);
1✔
694

695
   Test::Result result = test_function("C_GetObjectSize", bind);
2✔
696
   result.test_sz_ne("Object size", object_size, 0);
1✔
697

698
   // cleanup
699
   p11_low_level.get()->C_DestroyObject(session_handle, object_handle);
1✔
700

701
   return result;
1✔
702
}
1✔
703

704
Test::Result test_c_get_attribute_value() {
1✔
705
   RAII_LowLevel p11_low_level;
1✔
706
   const SessionHandle session_handle = p11_low_level.open_rw_session_with_user_login();
1✔
707

708
   const ObjectHandle object_handle = create_simple_data_object(p11_low_level);
1✔
709

710
   std::map<AttributeType, secure_vector<uint8_t>> getter = {{AttributeType::Label, secure_vector<uint8_t>()},
1✔
711
                                                             {AttributeType::Value, secure_vector<uint8_t>()}};
3✔
712

713
   auto bind =
1✔
714
      std::bind(static_cast<bool (LowLevel::*)(
1✔
715
                   SessionHandle, ObjectHandle, std::map<AttributeType, secure_vector<uint8_t>>&, ReturnValue*) const>(
716
                   &LowLevel::C_GetAttributeValue<secure_allocator<uint8_t>>),
717
                p11_low_level.get(),
1✔
718
                session_handle,
719
                object_handle,
720
                std::ref(getter),
1✔
721
                std::placeholders::_1);
1✔
722

723
   Test::Result result = test_function("C_GetAttributeValue", bind);
2✔
724

725
   const std::string _label(getter[AttributeType::Label].begin(), getter[AttributeType::Label].end());
2✔
726
   const std::string value(getter[AttributeType::Value].begin(), getter[AttributeType::Value].end());
2✔
727
   result.test_eq("label", _label, "A data object");
1✔
728
   result.test_eq("value", value, "Sample data");
1✔
729

730
   // cleanup
731
   p11_low_level.get()->C_DestroyObject(session_handle, object_handle);
1✔
732

733
   return result;
2✔
734
}
1✔
735

736
std::map<AttributeType, std::vector<uint8_t>> get_attribute_values(const RAII_LowLevel& p11_low_level,
2✔
737
                                                                   SessionHandle session_handle,
738
                                                                   ObjectHandle object_handle,
739
                                                                   const std::vector<AttributeType>& attribute_types) {
740
   std::map<AttributeType, std::vector<uint8_t>> received_attributes;
2✔
741

742
   for(const auto& type : attribute_types) {
6✔
743
      received_attributes.emplace(type, std::vector<uint8_t>());
4✔
744
   }
745

746
   p11_low_level.get()->C_GetAttributeValue(session_handle, object_handle, received_attributes);
2✔
747

748
   return received_attributes;
2✔
749
}
×
750

751
Test::Result test_c_set_attribute_value() {
1✔
752
   RAII_LowLevel p11_low_level;
1✔
753

754
   const Flags session_flags = PKCS11::flags(Flag::SerialSession | Flag::RwSession);
1✔
755
   const SessionHandle session_handle = p11_low_level.open_session(session_flags);
1✔
756

757
   p11_low_level.login(UserType::User, PIN());
1✔
758

759
   const ObjectHandle object_handle = create_simple_data_object(p11_low_level);
1✔
760

761
   std::string new_label = "A modified data object";
1✔
762

763
   std::map<AttributeType, secure_vector<uint8_t>> new_attributes = {
1✔
764
      {AttributeType::Label, secure_vector<uint8_t>(new_label.begin(), new_label.end())}};
3✔
765

766
   auto bind =
1✔
767
      std::bind(static_cast<bool (LowLevel::*)(
1✔
768
                   SessionHandle, ObjectHandle, std::map<AttributeType, secure_vector<uint8_t>>&, ReturnValue*) const>(
769
                   &LowLevel::C_SetAttributeValue<secure_allocator<uint8_t>>),
770
                p11_low_level.get(),
1✔
771
                session_handle,
772
                object_handle,
773
                std::ref(new_attributes),
1✔
774
                std::placeholders::_1);
1✔
775

776
   Test::Result result = test_function("C_SetAttributeValue", bind);
2✔
777

778
   // get attributes and check if they are changed correctly
779
   const std::vector<AttributeType> types = {AttributeType::Label, AttributeType::Value};
1✔
780
   auto received_attributes = get_attribute_values(p11_low_level, session_handle, object_handle, types);
1✔
781

782
   const std::string retrieved_label(received_attributes[AttributeType::Label].begin(),
1✔
783
                                     received_attributes[AttributeType::Label].end());
2✔
784

785
   result.test_eq("label", new_label, retrieved_label);
1✔
786

787
   // cleanup
788
   p11_low_level.get()->C_DestroyObject(session_handle, object_handle);
1✔
789

790
   return result;
2✔
791
}
2✔
792

793
Test::Result test_c_copy_object() {
1✔
794
   RAII_LowLevel p11_low_level;
1✔
795
   const SessionHandle session_handle = p11_low_level.open_rw_session_with_user_login();
1✔
796

797
   const ObjectHandle object_handle = create_simple_data_object(p11_low_level);
1✔
798
   ObjectHandle copied_object_handle = 0;
1✔
799

800
   const std::string copied_label = "A copied data object";
1✔
801

802
   Attribute copy_attribute_values = {static_cast<CK_ATTRIBUTE_TYPE>(AttributeType::Label),
1✔
803
                                      const_cast<char*>(copied_label.c_str()),
1✔
804
                                      static_cast<CK_ULONG>(copied_label.size())};
1✔
805

806
   auto binder = std::bind(&LowLevel::C_CopyObject,
1✔
807
                           p11_low_level.get(),
1✔
808
                           session_handle,
809
                           object_handle,
810
                           &copy_attribute_values,
1✔
811
                           1,
812
                           &copied_object_handle,
813
                           std::placeholders::_1);
1✔
814

815
   Test::Result result = test_function("C_CopyObject", binder);
2✔
816

817
   // get attributes and check if its copied correctly
818
   const std::vector<AttributeType> types = {AttributeType::Label, AttributeType::Value};
1✔
819
   auto received_attributes = get_attribute_values(p11_low_level, session_handle, copied_object_handle, types);
1✔
820

821
   const std::string retrieved_label(received_attributes[AttributeType::Label].begin(),
1✔
822
                                     received_attributes[AttributeType::Label].end());
2✔
823

824
   result.test_eq("label", copied_label, retrieved_label);
1✔
825

826
   // cleanup
827
   p11_low_level.get()->C_DestroyObject(session_handle, object_handle);
1✔
828
   p11_low_level.get()->C_DestroyObject(session_handle, copied_object_handle);
1✔
829

830
   return result;
2✔
831
}
1✔
832

833
// NOLINTEND(*-avoid-bind)
834

835
Test::Result test_load_latest_interface() {
1✔
836
   Test::Result res("Load latest PKCS #11 interface");
1✔
837
   Botan::Dynamically_Loaded_Library pkcs11_module(Test::pkcs11_lib());
1✔
838
   res.test_no_throw("Get function lists of latest interface", [&] {
1✔
839
      auto latest_interface = InterfaceWrapper::latest_p11_interface(pkcs11_module);
1✔
840
      latest_interface.func_2_40();
1✔
841
      if(latest_interface.version().major >= 3) {
1✔
842
         latest_interface.func_3_0();
×
843

844
         if(latest_interface.version().major > 3 || latest_interface.version().minor >= 2) {
×
845
            latest_interface.func_3_2();
×
846
         }
847
      }
848
   });
1✔
849
   return res;
1✔
850
}
1✔
851

852
class LowLevelTests final : public Test {
1✔
853
   public:
854
      std::vector<Test::Result> run() override {
1✔
855
         std::vector<std::pair<std::string, std::function<Test::Result()>>> fns = {
1✔
856
            {STRING_AND_FUNCTION(test_c_get_function_list)},
857
            {STRING_AND_FUNCTION(test_low_level_ctor)},
858
            {STRING_AND_FUNCTION(test_initialize_finalize)},
859
            {STRING_AND_FUNCTION(test_c_get_info)},
860
            {STRING_AND_FUNCTION(test_c_get_slot_list)},
861
            {STRING_AND_FUNCTION(test_c_get_slot_info)},
862
            {STRING_AND_FUNCTION(test_c_get_token_info)},
863
            {STRING_AND_FUNCTION(test_c_wait_for_slot_event)},
864
            {STRING_AND_FUNCTION(test_c_get_mechanism_list)},
865
            {STRING_AND_FUNCTION(test_c_get_mechanism_info)},
866
            {STRING_AND_FUNCTION(test_open_close_session)},
867
            {STRING_AND_FUNCTION(test_c_close_all_sessions)},
868
            {STRING_AND_FUNCTION(test_c_get_session_info)},
869
            {STRING_AND_FUNCTION(test_c_init_token)},
870
            {STRING_AND_FUNCTION(test_c_login_logout_security_officier)}, /* only possible if token is initialized */
871
            {STRING_AND_FUNCTION(test_c_init_pin)},
872
            {STRING_AND_FUNCTION(
873
               test_c_login_logout_user)}, /* only possible if token is initialized and user pin is set */
874
            {STRING_AND_FUNCTION(test_c_set_pin)},
875
            {STRING_AND_FUNCTION(test_c_create_object_c_destroy_object)},
876
            {STRING_AND_FUNCTION(test_c_get_object_size)},
877
            {STRING_AND_FUNCTION(test_c_get_attribute_value)},
878
            {STRING_AND_FUNCTION(test_c_set_attribute_value)},
879
            {STRING_AND_FUNCTION(test_c_copy_object)},
880
            {STRING_AND_FUNCTION(test_load_latest_interface)},
881
         };
25✔
882

883
         return run_pkcs11_tests("PKCS11 low level", fns);
2✔
884
      }
2✔
885
};
886

887
BOTAN_REGISTER_SERIALIZED_TEST("pkcs11", "pkcs11-lowlevel", LowLevelTests);
888

889
   #endif
890
#endif
891

892
}  // namespace
893
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc