Botan 3.14.1
Crypto and TLS for C&
p11_object.cpp
Go to the documentation of this file.
1/*
2* PKCS#11 Object
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_object.h>
10
11#include <map>
12
13namespace Botan::PKCS11 {
14
16 add_class(object_class);
17}
18
20 add_ulong(AttributeType::Class, static_cast<CK_OBJECT_CLASS>(object_class));
21}
22
23void AttributeContainer::add_ulong(AttributeType attribute, Ulong value) {
24 m_numerics.push_back(value);
25 add_attribute(attribute, reinterpret_cast<uint8_t*>(&m_numerics.back()), sizeof(Ulong));
26}
27
28void AttributeContainer::add_string(AttributeType attribute, std::string_view value) {
29 m_strings.push_back(std::string(value));
31 attribute, reinterpret_cast<const uint8_t*>(m_strings.back().data()), checked_ulong_cast(value.size()));
32}
33
34void AttributeContainer::add_binary(AttributeType attribute, const uint8_t* value, size_t length) {
35 m_vectors.push_back(secure_vector<uint8_t>(value, value + length));
36 add_attribute(attribute, reinterpret_cast<const uint8_t*>(m_vectors.back().data()), checked_ulong_cast(length));
37}
38
39void AttributeContainer::add_bool(AttributeType attribute, bool value) {
40 m_bools.push_back(value ? True : False);
41 add_attribute(attribute, &m_bools.back(), sizeof(Bbool));
42}
43
44void AttributeContainer::add_attribute(AttributeType attribute, const uint8_t* value, Ulong size) {
45 bool exists = false;
46 // check if the attribute has been added already
47 for(auto& existing_attribute : m_attributes) {
48 if(existing_attribute.type == static_cast<CK_ATTRIBUTE_TYPE>(attribute)) {
49 // remove old entries
50 m_strings.remove_if(
51 [&existing_attribute](std::string_view data) { return data.data() == existing_attribute.pValue; });
52
53 m_numerics.remove_if([&existing_attribute](const Ulong& data) { return &data == existing_attribute.pValue; });
54
55 m_bools.remove_if([&existing_attribute](const Bbool& data) { return &data == existing_attribute.pValue; });
56
57 m_vectors.remove_if([&existing_attribute](const secure_vector<uint8_t>& data) {
58 return data.data() == existing_attribute.pValue;
59 });
60
61 existing_attribute.pValue = const_cast<uint8_t*>(value);
62 existing_attribute.ulValueLen = size;
63 exists = true;
64 break;
65 }
66 }
67
68 if(!exists) {
69 m_attributes.push_back(Attribute{static_cast<CK_ATTRIBUTE_TYPE>(attribute), const_cast<uint8_t*>(value), size});
70 }
71}
72
73// ====================================================================================================
74
75ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template) :
76 m_session(session), m_search_terminated(false) {
77 module()->C_FindObjectsInit(m_session.get().handle(),
78 const_cast<Attribute*>(search_template.data()),
79 checked_ulong_cast(search_template.size()));
80}
81
83 try {
84 if(!m_search_terminated) {
85 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
86 }
87 } catch(...) {
88 // ignore error during noexcept function
89 }
90}
91
92std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const {
93 std::vector<ObjectHandle> result(max_count);
94 Ulong objectCount = 0;
95 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
96 if(objectCount < max_count) {
97 result.resize(objectCount);
98 }
99 return result;
100}
101
103 module()->C_FindObjectsFinal(m_session.get().handle());
104 m_search_terminated = true;
105}
106
107// ====================================================================================================
108
111
112// ====================================================================================================
113
115
116// ====================================================================================================
117
119
120// ====================================================================================================
121
126
127// ====================================================================================================
128
133
134// ====================================================================================================
135
137
138// ====================================================================================================
139
141
142// ====================================================================================================
143
145
146// ====================================================================================================
147
152
153// ====================================================================================================
154
156
157Object::Object(Session& session, const ObjectProperties& obj_props) : m_session(session), m_handle(0) {
158 m_session.get().module()->C_CreateObject(
159 m_session.get().handle(), obj_props.data(), checked_ulong_cast(obj_props.count()), &m_handle);
160}
161
163 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, secure_vector<uint8_t>()}};
164 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
165 return attribute_map.at(attribute);
166}
167
169 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, value}};
170 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
171}
172
173void Object::destroy() const {
174 module()->C_DestroyObject(m_session.get().handle(), m_handle);
175 // Prevent later calls from acting on an object which may reuse this handle
176 m_handle = CK_INVALID_HANDLE;
177}
178
179ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const {
180 ObjectHandle copied_handle = {};
181 module()->C_CopyObject(m_session.get().handle(),
182 m_handle,
183 modified_attributes.data(),
184 checked_ulong_cast(modified_attributes.count()),
185 &copied_handle);
186 return copied_handle;
187}
188} // namespace Botan::PKCS11
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition p11_object.h:28
void add_attribute(AttributeType attribute, const uint8_t *value, Ulong size)
Add an attribute with the given value and size to the attribute collection m_attributes.
void add_numeric(AttributeType attribute, T value)
Definition p11_object.h:98
void add_bool(AttributeType attribute, bool value)
void add_string(AttributeType attribute, std::string_view value)
void add_binary(AttributeType attribute, const uint8_t *value, size_t length)
void add_class(ObjectClass object_class)
BOTAN_FUTURE_EXPLICIT CertificateProperties(CertificateType cert_type)
CertificateType cert_type() const
Definition p11_object.h:256
BOTAN_FUTURE_EXPLICIT DomainParameterProperties(KeyType key_type)
KeyProperties(ObjectClass object_class, KeyType key_type)
bool C_SetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:396
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:406
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:386
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:375
bool C_CopyObject(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ObjectHandle *new_object_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:364
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:424
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:414
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Module & module() const
Definition p11_object.h:153
~ObjectFinder() noexcept
Terminates a search for token and session objects (calls C_FindObjectsFinal).
void finish()
Finishes the search operation manually to allow a new ObjectFinder to exist.
ObjectFinder(Session &session, const std::vector< Attribute > &search_template)
Common attributes of all objects.
Definition p11_object.h:161
ObjectClass object_class() const
Definition p11_object.h:167
BOTAN_FUTURE_EXPLICIT ObjectProperties(ObjectClass object_class)
Module & module() const
Definition p11_object.h:559
ObjectHandle handle() const
Definition p11_object.h:553
Session & session() const
Definition p11_object.h:556
secure_vector< uint8_t > get_attribute_value(AttributeType attribute) const
Object(Session &session, ObjectHandle handle)
void set_attribute_value(AttributeType attribute, const secure_vector< uint8_t > &value) const
Sets the given value for the attribute (using C_SetAttributeValue).
ObjectHandle copy(const AttributeContainer &modified_attributes) const
BOTAN_FUTURE_EXPLICIT PrivateKeyProperties(KeyType key_type)
BOTAN_FUTURE_EXPLICIT PublicKeyProperties(KeyType key_type)
BOTAN_FUTURE_EXPLICIT SecretKeyProperties(KeyType key_type)
Represents a PKCS#11 session.
Definition p11_types.h:139
BOTAN_FUTURE_EXPLICIT StorageObjectProperties(ObjectClass object_class)
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
AttributeType
Definition p11.h:50
CertificateType
Definition p11.h:213
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:1214
CK_ATTRIBUTE Attribute
Definition p11.h:1213
const Bbool True
Definition p11.h:1225
CK_ULONG Ulong
Definition p11.h:1204
const Bbool False
Definition p11.h:1226
CK_BBOOL Bbool
Definition p11.h:1202
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
CK_RV C_CreateObject(CK_SESSION_HANDLE, CK_ATTRIBUTE *, CK_ULONG, CK_OBJECT_HANDLE *)
#define CK_INVALID_HANDLE
Definition pkcs11.h:35
unsigned long int CK_ULONG
Definition pkcs11.h:20
CK_ULONG CK_OBJECT_CLASS
Definition pkcs11.h:63
CK_ULONG CK_CERTIFICATE_TYPE
Definition pkcs11.h:46
CK_ULONG CK_ATTRIBUTE_TYPE
Definition pkcs11.h:44