Botan 2.19.5 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
12 namespace Botan {
13
14 namespace PKCS11 {
15
16 AttributeContainer::AttributeContainer(ObjectClass object_class)
17 {
18 add_class(object_class);
19 }
20
21 void AttributeContainer::add_class(ObjectClass object_class)
22 {
23 m_numerics.emplace_back(static_cast< uint64_t >(object_class));
24 add_attribute(AttributeType::Class,
25 reinterpret_cast< uint8_t* >(&m_numerics.back()),
26 static_cast<uint32_t>(sizeof(ObjectClass)));
27 }
28
29 void 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
37 void 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
45 void AttributeContainer::add_bool(AttributeType attribute, bool value)
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
53 void 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.remove_if([ &existing_attribute ](const std::string& data)
63 {
64 return data.data() == existing_attribute.pValue;
65 });
66
67 m_numerics.remove_if([ &existing_attribute ](const uint64_t& data)
68 {
69 return &data == existing_attribute.pValue;
70 });
71
72 m_vectors.remove_if([ &existing_attribute ](const secure_vector<uint8_t>& data)
73 {
74 return data.data() == existing_attribute.pValue;
75 });
76
77 existing_attribute.pValue = const_cast< uint8_t* >(value);
78 existing_attribute.ulValueLen = size;
79 exists = true;
80 break;
81 }
82 }
83
84 if(!exists)
85 {
86 m_attributes.push_back(Attribute{ static_cast< CK_ATTRIBUTE_TYPE >(attribute), const_cast< uint8_t* >(value), size });
87 }
88 }
89
90// ====================================================================================================
91
92 ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template)
93 : m_session(session), m_search_terminated(false)
94 {
95 module()->C_FindObjectsInit(m_session.get().handle(),
96 const_cast< Attribute* >(search_template.data()),
97 static_cast<Ulong>(search_template.size()));
98 }
99
100 ObjectFinder::~ObjectFinder() noexcept
101 {
102 try
103 {
104 if(m_search_terminated == false)
105 {
106 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
107 }
108 }
109 catch(...)
110 {
111 // ignore error during noexcept function
112 }
113 }
114
115std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const
116 {
117 std::vector<ObjectHandle> result(max_count);
118 Ulong objectCount = 0;
119 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
120 if(objectCount < max_count)
121 {
122 result.resize(objectCount);
123 }
124 return result;
125 }
126
127 void ObjectFinder::finish()
128 {
129 module()->C_FindObjectsFinal(m_session.get().handle());
130 m_search_terminated = true;
131 }
132
133// ====================================================================================================
134
135 ObjectProperties::ObjectProperties(ObjectClass object_class)
136 : AttributeContainer(object_class), m_object_class(object_class)
137 {}
138
139// ====================================================================================================
140
141 StorageObjectProperties::StorageObjectProperties(ObjectClass object_class)
142 : ObjectProperties(object_class)
143 {}
144
145// ====================================================================================================
146
147 DataObjectProperties::DataObjectProperties()
148 : StorageObjectProperties(ObjectClass::Data)
149 {}
150
151// ====================================================================================================
152
153 CertificateProperties::CertificateProperties(CertificateType cert_type)
154 : StorageObjectProperties(ObjectClass::Certificate), m_cert_type(cert_type)
155 {
156 add_numeric(AttributeType::CertificateType, static_cast< CK_CERTIFICATE_TYPE >(m_cert_type));
157 }
158
159// ====================================================================================================
160
161 KeyProperties::KeyProperties(ObjectClass object_class, KeyType key_type)
162 : StorageObjectProperties(object_class), m_key_type(key_type)
163 {
164 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
165 }
166
167// ====================================================================================================
168
169 PublicKeyProperties::PublicKeyProperties(KeyType key_type)
170 : KeyProperties(ObjectClass::PublicKey, key_type)
171 {}
172
173// ====================================================================================================
174
175 PrivateKeyProperties::PrivateKeyProperties(KeyType key_type)
176 : KeyProperties(ObjectClass::PrivateKey, key_type)
177 {}
178
179// ====================================================================================================
180
181 SecretKeyProperties::SecretKeyProperties(KeyType key_type)
182 : KeyProperties(ObjectClass::SecretKey, key_type)
183 {}
184
185// ====================================================================================================
186
187 DomainParameterProperties::DomainParameterProperties(KeyType key_type)
188 : StorageObjectProperties(ObjectClass::DomainParameters), m_key_type(key_type)
189 {
190 add_numeric(AttributeType::KeyType, static_cast< CK_ULONG >(m_key_type));
191 }
192
193// ====================================================================================================
194
195 Object::Object(Session& session, ObjectHandle handle)
196 : m_session(session), m_handle(handle)
197 {}
198
199 Object::Object(Session& session, const ObjectProperties& obj_props)
200 : m_session(session), m_handle(0)
201 {
202 m_session.get().module()->C_CreateObject(m_session.get().handle(), obj_props.data(), static_cast<Ulong>(obj_props.count()), &m_handle);
203 }
204
205 secure_vector<uint8_t> Object::get_attribute_value(AttributeType attribute) const
206 {
207 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, secure_vector<uint8_t>() } };
208 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
209 return attribute_map.at(attribute);
210 }
211
212 void Object::set_attribute_value(AttributeType attribute, const secure_vector<uint8_t>& value) const
213 {
214 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = { { attribute, value } };
215 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
216 }
217
218 void Object::destroy() const
219 {
220 module()->C_DestroyObject(m_session.get().handle(), m_handle);
221 }
222
223 ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const
224 {
225 ObjectHandle copied_handle;
226 module()->C_CopyObject(m_session.get().handle(), m_handle,
227 modified_attributes.data(), static_cast<Ulong>(modified_attributes.count()),
228 &copied_handle);
229 return copied_handle;
230 }
231}
232}
Botan::PKCS11::AttributeContainer
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition: p11_object.h:29
Botan::PKCS11::AttributeContainer::add_string
void add_string(AttributeType attribute, const std::string &value)
Definition: p11_object.cpp:29
Botan::PKCS11::AttributeContainer::AttributeContainer
AttributeContainer()=default
Botan::PKCS11::AttributeContainer::add_attribute
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
Definition: p11_object.cpp:53
Botan::PKCS11::AttributeContainer::count
size_t count() const
Definition: p11_object.h:58
Botan::PKCS11::AttributeContainer::add_numeric
void add_numeric(AttributeType attribute, T value)
Definition: p11_object.h:108
Botan::PKCS11::AttributeContainer::data
Attribute * data() const
Definition: p11_object.h:52
Botan::PKCS11::AttributeContainer::add_bool
void add_bool(AttributeType attribute, bool value)
Definition: p11_object.cpp:45
Botan::PKCS11::AttributeContainer::add_binary
void add_binary(AttributeType attribute, const uint8_t *value, size_t length)
Definition: p11_object.cpp:37
Botan::PKCS11::AttributeContainer::add_class
void add_class(ObjectClass object_class)
Definition: p11_object.cpp:21
Botan::PKCS11::CertificateProperties::CertificateProperties
CertificateProperties(CertificateType cert_type)
Definition: p11_object.cpp:153
Botan::PKCS11::DataObjectProperties::DataObjectProperties
DataObjectProperties()
Definition: p11_object.cpp:147
Botan::PKCS11::DomainParameterProperties::DomainParameterProperties
DomainParameterProperties(KeyType key_type)
Definition: p11_object.cpp:187
Botan::PKCS11::KeyProperties
Common attributes of all key objects.
Definition: p11_object.h:314
Botan::PKCS11::KeyProperties::KeyProperties
KeyProperties(ObjectClass object_class, KeyType key_type)
Definition: p11_object.cpp:161
Botan::PKCS11::LowLevel::C_SetAttributeValue
bool C_SetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:350
Botan::PKCS11::LowLevel::C_FindObjectsInit
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:360
Botan::PKCS11::LowLevel::C_GetAttributeValue
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:340
Botan::PKCS11::LowLevel::C_DestroyObject
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:325
Botan::PKCS11::LowLevel::C_CopyObject
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
Botan::PKCS11::LowLevel::C_FindObjectsFinal
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition: p11.cpp:378
Botan::PKCS11::LowLevel::C_FindObjects
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
Botan::PKCS11::ObjectFinder::find
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Definition: p11_object.cpp:115
Botan::PKCS11::ObjectFinder::module
Module & module() const
Definition: p11_object.h:157
Botan::PKCS11::ObjectFinder::~ObjectFinder
~ObjectFinder() noexcept
Terminates a search for token and session objects (calls C_FindObjectsFinal)
Definition: p11_object.cpp:100
Botan::PKCS11::ObjectFinder::finish
void finish()
Finishes the search operation manually to allow a new ObjectFinder to exist.
Definition: p11_object.cpp:127
Botan::PKCS11::ObjectFinder::ObjectFinder
ObjectFinder(Session &session, const std::vector< Attribute > &search_template)
Definition: p11_object.cpp:92
Botan::PKCS11::ObjectProperties
Common attributes of all objects.
Definition: p11_object.h:169
Botan::PKCS11::ObjectProperties::ObjectProperties
ObjectProperties(ObjectClass object_class)
Definition: p11_object.cpp:135
Botan::PKCS11::Object::module
Module & module() const
Definition: p11_object.h:703
Botan::PKCS11::Object::get_attribute_value
secure_vector< uint8_t > get_attribute_value(AttributeType attribute) const
Definition: p11_object.cpp:205
Botan::PKCS11::Object::destroy
void destroy() const
Destroys the object.
Definition: p11_object.cpp:218
Botan::PKCS11::Object::Object
Object(Session &session, ObjectHandle handle)
Definition: p11_object.cpp:195
Botan::PKCS11::Object::set_attribute_value
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:212
Botan::PKCS11::Object::copy
ObjectHandle copy(const AttributeContainer &modified_attributes) const
Definition: p11_object.cpp:223
Botan::PKCS11::PrivateKeyProperties::PrivateKeyProperties
PrivateKeyProperties(KeyType key_type)
Definition: p11_object.cpp:175
Botan::PKCS11::PublicKeyProperties::PublicKeyProperties
PublicKeyProperties(KeyType key_type)
Definition: p11_object.cpp:169
Botan::PKCS11::SecretKeyProperties::SecretKeyProperties
SecretKeyProperties(KeyType key_type)
Definition: p11_object.cpp:181
Botan::PKCS11::Session
Represents a PKCS#11 session.
Definition: p11_types.h:131
Botan::PKCS11::StorageObjectProperties
Common attributes of all storage objects.
Definition: p11_object.h:186
Botan::PKCS11::StorageObjectProperties::StorageObjectProperties
StorageObjectProperties(ObjectClass object_class)
Definition: p11_object.cpp:141
Botan::PKCS11::Bbool
CK_BBOOL Bbool
Definition: p11.h:836
Botan::PKCS11::AttributeType
AttributeType
Definition: p11.h:66
Botan::PKCS11::AttributeType::CertificateType
@ CertificateType
Botan::PKCS11::AttributeType::KeyType
@ KeyType
Botan::PKCS11::AttributeType::Class
@ Class
Botan::PKCS11::CertificateType
CertificateType
Definition: p11.h:178
Botan::PKCS11::Ulong
CK_ULONG Ulong
Definition: p11.h:838
Botan::PKCS11::ObjectClass
ObjectClass
Definition: p11.h:681
Botan::PKCS11::ObjectClass::SecretKey
@ SecretKey
Botan::PKCS11::ObjectClass::DomainParameters
@ DomainParameters
Botan::PKCS11::ObjectClass::PublicKey
@ PublicKey
Botan::PKCS11::ObjectClass::PrivateKey
@ PrivateKey
Botan::PKCS11::ObjectClass::Certificate
@ Certificate
Botan::PKCS11::ObjectClass::Data
@ Data
Botan::PKCS11::KeyType
KeyType
Definition: p11.h:290
Botan::PKCS11::True
const Bbool True
Definition: p11.h:857
Botan::PKCS11::False
const Bbool False
Definition: p11.h:858
Botan::PKCS11::ObjectHandle
CK_OBJECT_HANDLE ObjectHandle
Definition: p11.h:848
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
CK_ULONG
unsigned long int CK_ULONG
Definition: pkcs11t.h:48
CK_CERTIFICATE_TYPE
CK_ULONG CK_CERTIFICATE_TYPE
Definition: pkcs11t.h:393
CK_ATTRIBUTE_TYPE
CK_ULONG CK_ATTRIBUTE_TYPE
Definition: pkcs11t.h:416
CK_ATTRIBUTE
Definition: pkcs11t.h:564
CK_ATTRIBUTE::pValue
CK_VOID_PTR pValue
Definition: pkcs11t.h:566