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