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