Botan 3.0.0
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::PKCS11 {
13
15 {
16 add_class(object_class);
17 }
18
20 {
21 m_numerics.emplace_back(static_cast< uint64_t >(object_class));
22 add_attribute(AttributeType::Class,
23 reinterpret_cast< uint8_t* >(&m_numerics.back()),
24 static_cast<Ulong>(sizeof(ObjectClass)));
25 }
26
27void AttributeContainer::add_string(AttributeType attribute, std::string_view value)
28 {
29 m_strings.push_back(std::string(value));
30 add_attribute(attribute, reinterpret_cast<const uint8_t*>(m_strings.back().data()), static_cast<Ulong>(value.size()));
31 }
32
33void AttributeContainer::add_binary(AttributeType attribute, const uint8_t* value, size_t length)
34 {
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()), static_cast<Ulong>(length));
37 }
38
40 {
41 m_numerics.push_back(value ? True : False);
42 add_attribute(attribute, reinterpret_cast< uint8_t* >(&m_numerics.back()), sizeof(Bbool));
43 }
44
45void AttributeContainer::add_attribute(AttributeType attribute, const uint8_t* value, Ulong size)
46 {
47 bool exists = false;
48 // check if the attribute has been added already
49 for(auto& existing_attribute : m_attributes)
50 {
51 if(existing_attribute.type == static_cast< CK_ATTRIBUTE_TYPE >(attribute))
52 {
53 // remove old entries
54 m_strings.remove_if([ &existing_attribute ](std::string_view data)
55 {
56 return data.data() == existing_attribute.pValue;
57 });
58
59 m_numerics.remove_if([ &existing_attribute ](const uint64_t& data)
60 {
61 return &data == existing_attribute.pValue;
62 });
63
64 m_vectors.remove_if([ &existing_attribute ](const secure_vector<uint8_t>& data)
65 {
66 return data.data() == existing_attribute.pValue;
67 });
68
69 existing_attribute.pValue = const_cast<uint8_t*>(value);
70 existing_attribute.ulValueLen = size;
71 exists = true;
72 break;
73 }
74 }
75
76 if(!exists)
77 {
78 m_attributes.push_back(
79 Attribute {
80 static_cast<CK_ATTRIBUTE_TYPE>(attribute),
81 const_cast<uint8_t*>(value),
82 size }
83 );
84 }
85 }
86
87// ====================================================================================================
88
89ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template)
90 : m_session(session), m_search_terminated(false)
91 {
92 module()->C_FindObjectsInit(m_session.get().handle(),
93 const_cast< Attribute* >(search_template.data()),
94 static_cast<Ulong>(search_template.size()));
95 }
96
98 {
99 try
100 {
101 if(m_search_terminated == false)
102 {
103 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
104 }
105 }
106 catch(...)
107 {
108 // ignore error during noexcept function
109 }
110 }
111
112std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const
113 {
114 std::vector<ObjectHandle> result(max_count);
115 Ulong objectCount = 0;
116 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
117 if(objectCount < max_count)
118 {
119 result.resize(objectCount);
120 }
121 return result;
122 }
123
125 {
126 module()->C_FindObjectsFinal(m_session.get().handle());
127 m_search_terminated = true;
128 }
129
130// ====================================================================================================
131
133 : AttributeContainer(object_class), m_object_class(object_class)
134 {}
135
136// ====================================================================================================
137
139 : ObjectProperties(object_class)
140 {}
141
142// ====================================================================================================
143
146 {}
147
148// ====================================================================================================
149
151 : StorageObjectProperties(ObjectClass::Certificate), m_cert_type(cert_type)
152 {
153 add_numeric(AttributeType::CertificateType, static_cast< CK_CERTIFICATE_TYPE >(m_cert_type));
154 }
155
156// ====================================================================================================
157
159 : StorageObjectProperties(object_class), m_key_type(key_type)
160 {
161 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
162 }
163
164// ====================================================================================================
165
168 {}
169
170// ====================================================================================================
171
174 {}
175
176// ====================================================================================================
177
180 {}
181
182// ====================================================================================================
183
186 {
187 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
188 }
189
190// ====================================================================================================
191
193 : m_session(session), m_handle(handle)
194 {}
195
196Object::Object(Session& session, const ObjectProperties& obj_props)
197 : m_session(session), m_handle(0)
198 {
199 m_session.get().module()->C_CreateObject(m_session.get().handle(), obj_props.data(), static_cast<Ulong>(obj_props.count()), &m_handle);
200 }
201
203 {
204 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, secure_vector<uint8_t>() } };
205 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
206 return attribute_map.at(attribute);
207 }
208
210 {
211 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, value } };
212 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
213 }
214
215void Object::destroy() const
216 {
217 module()->C_DestroyObject(m_session.get().handle(), m_handle);
218 }
219
220ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const
221 {
222 ObjectHandle copied_handle;
223 module()->C_CopyObject(m_session.get().handle(), m_handle,
224 modified_attributes.data(), static_cast<Ulong>(modified_attributes.count()),
225 &copied_handle);
226 return copied_handle;
227 }
228}
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition: p11_object.h:29
Attribute * data() const
Definition: p11_object.h:52
void add_bool(AttributeType attribute, bool value)
Definition: p11_object.cpp:39
void add_string(AttributeType attribute, std::string_view value)
Definition: p11_object.cpp:27
void add_binary(AttributeType attribute, const uint8_t *value, size_t length)
Definition: p11_object.cpp:33
void add_class(ObjectClass object_class)
Definition: p11_object.cpp:19
CertificateProperties(CertificateType cert_type)
Definition: p11_object.cpp:150
Common attributes of all key objects.
Definition: p11_object.h:314
KeyProperties(ObjectClass object_class, KeyType key_type)
Definition: p11_object.cpp:158
bool C_SetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:348
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:358
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:338
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:323
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:312
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:376
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:366
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Definition: p11_object.cpp:112
Module & module() const
Definition: p11_object.h:157
~ObjectFinder() noexcept
Terminates a search for token and session objects (calls C_FindObjectsFinal)
Definition: p11_object.cpp:97
void finish()
Finishes the search operation manually to allow a new ObjectFinder to exist.
Definition: p11_object.cpp:124
ObjectFinder(Session &session, const std::vector< Attribute > &search_template)
Definition: p11_object.cpp:89
Common attributes of all objects.
Definition: p11_object.h:169
ObjectProperties(ObjectClass object_class)
Definition: p11_object.cpp:132
Module & module() const
Definition: p11_object.h:703
secure_vector< uint8_t > get_attribute_value(AttributeType attribute) const
Definition: p11_object.cpp:202
void destroy() const
Destroys the object.
Definition: p11_object.cpp:215
Object(Session &session, ObjectHandle handle)
Definition: p11_object.cpp:192
void set_attribute_value(AttributeType attribute, const secure_vector< uint8_t > &value) const
Sets the given value for the attribute (using C_SetAttributeValue)
Definition: p11_object.cpp:209
ObjectHandle copy(const AttributeContainer &modified_attributes) const
Definition: p11_object.cpp:220
PrivateKeyProperties(KeyType key_type)
Definition: p11_object.cpp:172
PublicKeyProperties(KeyType key_type)
Definition: p11_object.cpp:166
SecretKeyProperties(KeyType key_type)
Definition: p11_object.cpp:178
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)
Definition: p11_object.cpp:138
CK_BBOOL Bbool
Definition: p11.h:836
CK_ATTRIBUTE Attribute
Definition: p11.h:847
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:64
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
CK_VOID_PTR pValue
Definition: pkcs11t.h:566