Botan 2.19.3
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#include <map>
11
12namespace Botan {
13
14namespace PKCS11 {
15
17 {
18 add_class(object_class);
19 }
20
22 {
23 m_numerics.emplace_back(static_cast< uint64_t >(object_class));
25 reinterpret_cast< uint8_t* >(&m_numerics.back()),
26 static_cast<uint32_t>(sizeof(ObjectClass)));
27 }
28
29void AttributeContainer::add_string(AttributeType attribute, const std::string& value)
30 {
31 m_strings.push_back(value);
32 add_attribute(attribute,
33 reinterpret_cast<const uint8_t*>(m_strings.back().data()),
34 static_cast<uint32_t>(value.size()));
35 }
36
37void AttributeContainer::add_binary(AttributeType attribute, const uint8_t* value, size_t length)
38 {
39 m_vectors.push_back(secure_vector<uint8_t>(value, value + length));
40 add_attribute(attribute,
41 reinterpret_cast<const uint8_t*>(m_vectors.back().data()),
42 static_cast<uint32_t>(length));
43 }
44
46 {
47 m_numerics.push_back(value ? True : False);
48 add_attribute(attribute,
49 reinterpret_cast<uint8_t*>(&m_numerics.back()),
50 sizeof(Bbool));
51 }
52
53void AttributeContainer::add_attribute(AttributeType attribute, const uint8_t* value, uint32_t size)
54 {
55 bool exists = false;
56 // check if the attribute has been added already
57 for(auto& existing_attribute : m_attributes)
58 {
59 if(existing_attribute.type == static_cast< CK_ATTRIBUTE_TYPE >(attribute))
60 {
61 // remove old entries
62 m_strings.erase(std::remove_if(m_strings.begin(), m_strings.end(), [ &existing_attribute ](const std::string& data)
63 {
64 return data.data() == existing_attribute.pValue;
65 }), m_strings.end());
66
67 m_numerics.erase(std::remove_if(m_numerics.begin(), m_numerics.end(), [ &existing_attribute ](const uint64_t& data)
68 {
69 return &data == existing_attribute.pValue;
70 }), m_numerics.end());
71
72 m_vectors.erase(std::remove_if(m_vectors.begin(),
73 m_vectors.end(), [ &existing_attribute ](const secure_vector<uint8_t>& data)
74 {
75 return data.data() == existing_attribute.pValue;
76 }), m_vectors.end());
77
78 existing_attribute.pValue = const_cast< uint8_t* >(value);
79 existing_attribute.ulValueLen = size;
80 exists = true;
81 break;
82 }
83 }
84
85 if(!exists)
86 {
87 m_attributes.push_back(Attribute{ static_cast< CK_ATTRIBUTE_TYPE >(attribute), const_cast< uint8_t* >(value), size });
88 }
89 }
90
91// ====================================================================================================
92
93ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template)
94 : m_session(session), m_search_terminated(false)
95 {
96 module()->C_FindObjectsInit(m_session.get().handle(),
97 const_cast< Attribute* >(search_template.data()),
98 static_cast<Ulong>(search_template.size()));
99 }
100
102 {
103 try
104 {
105 if(m_search_terminated == false)
106 {
107 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
108 }
109 }
110 catch(...)
111 {
112 // ignore error during noexcept function
113 }
114 }
115
116std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const
117 {
118 std::vector<ObjectHandle> result(max_count);
119 Ulong objectCount = 0;
120 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
121 if(objectCount < max_count)
122 {
123 result.resize(objectCount);
124 }
125 return result;
126 }
127
129 {
130 module()->C_FindObjectsFinal(m_session.get().handle());
131 m_search_terminated = true;
132 }
133
134// ====================================================================================================
135
137 : AttributeContainer(object_class), m_object_class(object_class)
138 {}
139
140// ====================================================================================================
141
145
146// ====================================================================================================
147
151
152// ====================================================================================================
153
159
160// ====================================================================================================
161
163 : StorageObjectProperties(object_class), m_key_type(key_type)
164 {
165 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
166 }
167
168// ====================================================================================================
169
173
174// ====================================================================================================
175
179
180// ====================================================================================================
181
185
186// ====================================================================================================
187
193
194// ====================================================================================================
195
197 : m_session(session), m_handle(handle)
198 {}
199
200Object::Object(Session& session, const ObjectProperties& obj_props)
201 : m_session(session), m_handle(0)
202 {
203 m_session.get().module()->C_CreateObject(m_session.get().handle(), obj_props.data(), static_cast<Ulong>(obj_props.count()), &m_handle);
204 }
205
207 {
208 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, secure_vector<uint8_t>() } };
209 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
210 return attribute_map.at(attribute);
211 }
212
214 {
215 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, value } };
216 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
217 }
218
219void Object::destroy() const
220 {
221 module()->C_DestroyObject(m_session.get().handle(), m_handle);
222 }
223
224ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const
225 {
226 ObjectHandle copied_handle;
227 module()->C_CopyObject(m_session.get().handle(), m_handle,
228 modified_attributes.data(), static_cast<Ulong>(modified_attributes.count()),
229 &copied_handle);
230 return copied_handle;
231 }
232}
233}
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition p11_object.h:29
void add_string(AttributeType attribute, const std::string &value)
void add_attribute(AttributeType attribute, const uint8_t *value, uint32_t 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:108
void add_bool(AttributeType attribute, bool value)
void add_binary(AttributeType attribute, const uint8_t *value, size_t length)
void add_class(ObjectClass object_class)
CertificateProperties(CertificateType cert_type)
Common attributes of all key objects.
Definition p11_object.h:314
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:350
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:360
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:340
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:325
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:314
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:378
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:368
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Module & module() const
Definition p11_object.h:157
~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:169
ObjectProperties(ObjectClass object_class)
Module & module() const
Definition p11_object.h:703
secure_vector< uint8_t > get_attribute_value(AttributeType attribute) const
void destroy() const
Destroys the object.
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
Represents a PKCS#11 session.
Definition p11_types.h:131
Common attributes of all storage objects.
Definition p11_object.h:186
StorageObjectProperties(ObjectClass object_class)
CK_BBOOL Bbool
Definition p11.h:836
AttributeType
Definition p11.h:66
CertificateType
Definition p11.h:178
CK_ULONG Ulong
Definition p11.h:838
const Bbool True
Definition p11.h:857
const Bbool False
Definition p11.h:858
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:848
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:65
unsigned long int CK_ULONG
Definition pkcs11t.h:48
CK_ULONG CK_CERTIFICATE_TYPE
Definition pkcs11t.h:393
CK_ULONG CK_ATTRIBUTE_TYPE
Definition pkcs11t.h:416