Botan 3.14.1
Crypto and TLS for C&
p11_object_guard.h
Go to the documentation of this file.
1/*
2* PKCS#11 Object Guard
3* (C) 2026 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_P11_OBJECT_GUARD_H_
9#define BOTAN_P11_OBJECT_GUARD_H_
10
11#include <botan/p11_types.h>
12#include <initializer_list>
13#include <vector>
14
15namespace Botan::PKCS11 {
16
17/**
18* Destroys newly created token objects unless released, so that a failure
19* after creating an object (for example while reading back its attributes)
20* does not leave an unreachable object behind on the token.
21*/
23 public:
24 Object_Creation_Guard(Session& session, std::initializer_list<ObjectHandle> handles) :
25 m_session(session), m_handles(handles) {}
26
28 for(const auto handle : m_handles) {
29 try {
30 m_session.module()->C_DestroyObject(m_session.handle(), handle, nullptr);
31 } catch(...) { // NOLINT(*-empty-catch)
32 }
33 }
34 }
35
36 /// Call once construction has succeeded
37 void release() { m_handles.clear(); }
38
43
44 private:
45 Session& m_session;
46 std::vector<ObjectHandle> m_handles;
47};
48
49} // namespace Botan::PKCS11
50
51#endif
Object_Creation_Guard(Session &session, std::initializer_list< ObjectHandle > handles)
Object_Creation_Guard & operator=(Object_Creation_Guard &&)=delete
void release()
Call once construction has succeeded.
Object_Creation_Guard(Object_Creation_Guard &&)=delete
Object_Creation_Guard(const Object_Creation_Guard &)=delete
Object_Creation_Guard & operator=(const Object_Creation_Guard &)=delete
Represents a PKCS#11 session.
Definition p11_types.h:139