Botan 3.0.0-alpha0
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));
23 reinterpret_cast< uint8_t* >(&m_numerics.back()),
24 static_cast<Ulong>(sizeof(ObjectClass)));
25 }
26
27void AttributeContainer::add_string(AttributeType attribute, const std::string& value)
28 {
29 m_strings.push_back(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.erase(std::remove_if(m_strings.begin(), m_strings.end(), [ &existing_attribute ](const std::string& data)
55 {
56 return data.data() == existing_attribute.pValue;
57 }), m_strings.end());
58
59 m_numerics.erase(std::remove_if(m_numerics.begin(), m_numerics.end(), [ &existing_attribute ](const uint64_t& data)
60 {
61 return &data == existing_attribute.pValue;
62 }), m_numerics.end());
63
64 m_vectors.erase(std::remove_if(m_vectors.begin(),
65 m_vectors.end(), [ &existing_attribute ](const secure_vector<uint8_t>& data)
66 {
67 return data.data() == existing_attribute.pValue;
68 }), m_vectors.end());
69
70 existing_attribute.pValue = const_cast<uint8_t*>(value);
71 existing_attribute.ulValueLen = size;
72 exists = true;
73 break;
74 }
75 }
76
77 if(!exists)
78 {
79 m_attributes.push_back(
80 Attribute {
81 static_cast<CK_ATTRIBUTE_TYPE>(attribute),
82 const_cast<uint8_t*>(value),
83 size }
84 );
85 }
86 }
87
88// ====================================================================================================
89
90ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template)
91 : m_session(session), m_search_terminated(false)
92 {
93 module()->C_FindObjectsInit(m_session.get().handle(),
94 const_cast< Attribute* >(search_template.data()),
95 static_cast<Ulong>(search_template.size()));
96 }
97
99 {
100 try
101 {
102 if(m_search_terminated == false)
103 {
104 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
105 }
106 }
107 catch(...)
108 {
109 // ignore error during noexcept function
110 }
111 }
112
113std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const
114 {
115 std::vector<ObjectHandle> result(max_count);
116 Ulong objectCount = 0;
117 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
118 if(objectCount < max_count)
119 {
120 result.resize(objectCount);
121 }
122 return result;
123 }
124
126 {
127 module()->C_FindObjectsFinal(m_session.get().handle());
128 m_search_terminated = true;
129 }
130
131// ====================================================================================================
132
134 : AttributeContainer(object_class), m_object_class(object_class)
135 {}
136
137// ====================================================================================================
138
140 : ObjectProperties(object_class)
141 {}
142
143// ====================================================================================================
144
147 {}
148
149// ====================================================================================================
150
152 : StorageObjectProperties(ObjectClass::Certificate), m_cert_type(cert_type)
153 {
155 }
156
157// ====================================================================================================
158
160 : StorageObjectProperties(object_class), m_key_type(key_type)
161 {
162 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
163 }
164
165// ====================================================================================================
166
169 {}
170
171// ====================================================================================================
172
175 {}
176
177// ====================================================================================================
178
181 {}
182
183// ====================================================================================================
184
187 {
188 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
189 }
190
191// ====================================================================================================
192
194 : m_session(session), m_handle(handle)
195 {}
196
197Object::Object(Session& session, const ObjectProperties& obj_props)
198 : m_session(session), m_handle(0)
199 {
200 m_session.get().module()->C_CreateObject(m_session.get().handle(), obj_props.data(), static_cast<Ulong>(obj_props.count()), &m_handle);
201 }
202
204 {
205 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, secure_vector<uint8_t>() } };
206 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
207 return attribute_map.at(attribute);
208 }
209
211 {
212 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, value } };
213 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
214 }
215
216void Object::destroy() const
217 {
218 module()->C_DestroyObject(m_session.get().handle(), m_handle);
219 }
220
221ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const
222 {
223 ObjectHandle copied_handle;
224 module()->C_CopyObject(m_session.get().handle(), m_handle,
225 modified_attributes.data(), static_cast<Ulong>(modified_attributes.count()),
226 &copied_handle);
227 return copied_handle;
228 }
229}
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition: p11_object.h:29
void add_string(AttributeType attribute, const std::string &value)
Definition: p11_object.cpp:27
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
Definition: p11_object.cpp:45
void add_numeric(AttributeType attribute, T value)
Definition: p11_object.h:108
Attribute * data() const
Definition: p11_object.h:52
void add_bool(AttributeType attribute, bool value)
Definition: p11_object.cpp:39
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:151
Common attributes of all key objects.
Definition: p11_object.h:314
KeyProperties(ObjectClass object_class, KeyType key_type)
Definition: p11_object.cpp:159
bool C_SetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:347
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:357
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:337
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:322
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:311
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:375
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:365
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Definition: p11_object.cpp:113
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:98
void finish()
Finishes the search operation manually to allow a new ObjectFinder to exist.
Definition: p11_object.cpp:125
ObjectFinder(Session &session, const std::vector< Attribute > &search_template)
Definition: p11_object.cpp:90
Common attributes of all objects.
Definition: p11_object.h:169
ObjectProperties(ObjectClass object_class)
Definition: p11_object.cpp:133
Module & module() const
Definition: p11_object.h:703
secure_vector< uint8_t > get_attribute_value(AttributeType attribute) const
Definition: p11_object.cpp:203
void destroy() const
Destroys the object.
Definition: p11_object.cpp:216
Object(Session &session, ObjectHandle handle)
Definition: p11_object.cpp:193
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:210
ObjectHandle copy(const AttributeContainer &modified_attributes) const
Definition: p11_object.cpp:221
PrivateKeyProperties(KeyType key_type)
Definition: p11_object.cpp:173
PublicKeyProperties(KeyType key_type)
Definition: p11_object.cpp:167
SecretKeyProperties(KeyType key_type)
Definition: p11_object.cpp:179
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:139
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