Botan 3.6.1
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 add_class(object_class);
16}
17
19 m_numerics.emplace_back(static_cast<uint64_t>(object_class));
21 AttributeType::Class, reinterpret_cast<uint8_t*>(&m_numerics.back()), static_cast<Ulong>(sizeof(ObjectClass)));
22}
23
24void AttributeContainer::add_string(AttributeType attribute, std::string_view value) {
25 m_strings.push_back(std::string(value));
27 attribute, reinterpret_cast<const uint8_t*>(m_strings.back().data()), static_cast<Ulong>(value.size()));
28}
29
30void AttributeContainer::add_binary(AttributeType attribute, const uint8_t* value, size_t length) {
31 m_vectors.push_back(secure_vector<uint8_t>(value, value + length));
32 add_attribute(attribute, reinterpret_cast<const uint8_t*>(m_vectors.back().data()), static_cast<Ulong>(length));
33}
34
35void AttributeContainer::add_bool(AttributeType attribute, bool value) {
36 m_numerics.push_back(value ? True : False);
37 add_attribute(attribute, reinterpret_cast<uint8_t*>(&m_numerics.back()), sizeof(Bbool));
38}
39
40void AttributeContainer::add_attribute(AttributeType attribute, const uint8_t* value, Ulong size) {
41 bool exists = false;
42 // check if the attribute has been added already
43 for(auto& existing_attribute : m_attributes) {
44 if(existing_attribute.type == static_cast<CK_ATTRIBUTE_TYPE>(attribute)) {
45 // remove old entries
46 m_strings.remove_if(
47 [&existing_attribute](std::string_view data) { return data.data() == existing_attribute.pValue; });
48
49 m_numerics.remove_if(
50 [&existing_attribute](const uint64_t& data) { return &data == existing_attribute.pValue; });
51
52 m_vectors.remove_if([&existing_attribute](const secure_vector<uint8_t>& data) {
53 return data.data() == existing_attribute.pValue;
54 });
55
56 existing_attribute.pValue = const_cast<uint8_t*>(value);
57 existing_attribute.ulValueLen = size;
58 exists = true;
59 break;
60 }
61 }
62
63 if(!exists) {
64 m_attributes.push_back(Attribute{static_cast<CK_ATTRIBUTE_TYPE>(attribute), const_cast<uint8_t*>(value), size});
65 }
66}
67
68// ====================================================================================================
69
70ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template) :
71 m_session(session), m_search_terminated(false) {
72 module()->C_FindObjectsInit(m_session.get().handle(),
73 const_cast<Attribute*>(search_template.data()),
74 static_cast<Ulong>(search_template.size()));
75}
76
78 try {
79 if(m_search_terminated == false) {
80 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
81 }
82 } catch(...) {
83 // ignore error during noexcept function
84 }
85}
86
87std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const {
88 std::vector<ObjectHandle> result(max_count);
89 Ulong objectCount = 0;
90 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
91 if(objectCount < max_count) {
92 result.resize(objectCount);
93 }
94 return result;
95}
96
98 module()->C_FindObjectsFinal(m_session.get().handle());
99 m_search_terminated = true;
100}
101
102// ====================================================================================================
103
105 AttributeContainer(object_class), m_object_class(object_class) {}
106
107// ====================================================================================================
108
110
111// ====================================================================================================
112
114
115// ====================================================================================================
116
121
122// ====================================================================================================
123
125 StorageObjectProperties(object_class), m_key_type(key_type) {
126 add_numeric(AttributeType::KeyType, static_cast<CK_ULONG>(m_key_type));
127}
128
129// ====================================================================================================
130
132
133// ====================================================================================================
134
136
137// ====================================================================================================
138
140
141// ====================================================================================================
142
147
148// ====================================================================================================
149
150Object::Object(Session& session, ObjectHandle handle) : m_session(session), m_handle(handle) {}
151
152Object::Object(Session& session, const ObjectProperties& obj_props) : m_session(session), m_handle(0) {
153 m_session.get().module()->C_CreateObject(
154 m_session.get().handle(), obj_props.data(), static_cast<Ulong>(obj_props.count()), &m_handle);
155}
156
158 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, secure_vector<uint8_t>()}};
159 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
160 return attribute_map.at(attribute);
161}
162
164 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, value}};
165 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
166}
167
168void Object::destroy() const {
169 module()->C_DestroyObject(m_session.get().handle(), m_handle);
170}
171
172ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const {
173 ObjectHandle copied_handle;
174 module()->C_CopyObject(m_session.get().handle(),
175 m_handle,
176 modified_attributes.data(),
177 static_cast<Ulong>(modified_attributes.count()),
178 &copied_handle);
179 return copied_handle;
180}
181} // namespace Botan::PKCS11
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition p11_object.h:27
void add_numeric(AttributeType attribute, T value)
Definition p11_object.h:97
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
void add_bool(AttributeType attribute, bool value)
void add_string(AttributeType attribute, std::string_view value)
void add_binary(AttributeType attribute, const uint8_t *value, size_t length)
void add_class(ObjectClass object_class)
CertificateProperties(CertificateType cert_type)
Common attributes of all key objects.
Definition p11_object.h:253
KeyProperties(ObjectClass object_class, KeyType key_type)
bool C_SetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:282
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:291
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:273
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:262
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:252
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:307
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:298
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Module & module() const
Definition p11_object.h:143
~ObjectFinder() noexcept
Terminates a search for token and session objects (calls C_FindObjectsFinal)
void finish()
Finishes the search operation manually to allow a new ObjectFinder to exist.
ObjectFinder(Session &session, const std::vector< Attribute > &search_template)
Common attributes of all objects.
Definition p11_object.h:151
ObjectProperties(ObjectClass object_class)
Module & module() const
Definition p11_object.h:531
secure_vector< uint8_t > get_attribute_value(AttributeType attribute) const
void destroy() const
Destroys the object.
Object(Session &session, ObjectHandle handle)
void set_attribute_value(AttributeType attribute, const secure_vector< uint8_t > &value) const
Sets the given value for the attribute (using C_SetAttributeValue)
ObjectHandle copy(const AttributeContainer &modified_attributes) const
Represents a PKCS#11 session.
Definition p11_types.h:121
Common attributes of all storage objects.
Definition p11_object.h:164
StorageObjectProperties(ObjectClass object_class)
AttributeType
Definition p11.h:61
CertificateType
Definition p11.h:172
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:826
const Bbool True
Definition p11.h:835
CK_ULONG Ulong
Definition p11.h:816
const Bbool False
Definition p11.h:836
CK_BBOOL Bbool
Definition p11.h:814
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
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