Botan 3.13.0
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
11#include <botan/assert.h>
12#include <cstring>
13#include <map>
14
15namespace Botan::PKCS11 {
16
18 add_class(object_class);
19}
20
22 m_numerics.emplace_back(static_cast<uint64_t>(object_class));
24 AttributeType::Class, reinterpret_cast<uint8_t*>(&m_numerics.back()), static_cast<Ulong>(sizeof(ObjectClass)));
25}
26
27void AttributeContainer::add_string(AttributeType attribute, std::string_view value) {
28 m_strings.push_back(std::string(value));
30 attribute, reinterpret_cast<const uint8_t*>(m_strings.back().data()), checked_ulong_cast(value.size()));
31}
32
33void AttributeContainer::add_binary(AttributeType attribute, const uint8_t* value, size_t length) {
34 m_vectors.push_back(secure_vector<uint8_t>(value, value + length));
35 add_attribute(attribute, reinterpret_cast<const uint8_t*>(m_vectors.back().data()), checked_ulong_cast(length));
36}
37
38void AttributeContainer::add_bool(AttributeType attribute, bool value) {
39 m_numerics.push_back(value ? True : False);
40 add_attribute(attribute, reinterpret_cast<uint8_t*>(&m_numerics.back()), sizeof(Bbool));
41}
42
43void AttributeContainer::add_attribute(AttributeType attribute, const uint8_t* value, Ulong size) {
44 bool exists = false;
45 // check if the attribute has been added already
46 for(auto& existing_attribute : m_attributes) {
47 if(existing_attribute.type == static_cast<CK_ATTRIBUTE_TYPE>(attribute)) {
48 // remove old entries
49 m_strings.remove_if(
50 [&existing_attribute](std::string_view data) { return data.data() == existing_attribute.pValue; });
51
52 m_numerics.remove_if(
53 [&existing_attribute](const uint64_t& data) { return &data == existing_attribute.pValue; });
54
55 m_vectors.remove_if([&existing_attribute](const secure_vector<uint8_t>& data) {
56 return data.data() == existing_attribute.pValue;
57 });
58
59 existing_attribute.pValue = const_cast<uint8_t*>(value);
60 existing_attribute.ulValueLen = size;
61 exists = true;
62 break;
63 }
64 }
65
66 if(!exists) {
67 m_attributes.push_back(Attribute{static_cast<CK_ATTRIBUTE_TYPE>(attribute), const_cast<uint8_t*>(value), size});
68 }
69}
70
71// ====================================================================================================
72
73ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template) :
74 m_session(session), m_search_terminated(false) {
75 module()->C_FindObjectsInit(m_session.get().handle(),
76 const_cast<Attribute*>(search_template.data()),
77 checked_ulong_cast(search_template.size()));
78}
79
81 try {
82 if(!m_search_terminated) {
83 module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
84 }
85 } catch(...) {
86 // ignore error during noexcept function
87 }
88}
89
90std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const {
91 std::vector<ObjectHandle> result(max_count);
92 Ulong objectCount = 0;
93 module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
94 if(objectCount < max_count) {
95 result.resize(objectCount);
96 }
97 return result;
98}
99
101 module()->C_FindObjectsFinal(m_session.get().handle());
102 m_search_terminated = true;
103}
104
105// ====================================================================================================
106
109
110// ====================================================================================================
111
113
114// ====================================================================================================
115
117
118// ====================================================================================================
119
124
125// ====================================================================================================
126
131
132// ====================================================================================================
133
135
136// ====================================================================================================
137
139
140// ====================================================================================================
141
143
144// ====================================================================================================
145
150
151// ====================================================================================================
152
154
155Object::Object(Session& session, const ObjectProperties& obj_props) : m_session(session), m_handle(0) {
156 m_session.get().module()->C_CreateObject(
157 m_session.get().handle(), obj_props.data(), checked_ulong_cast(obj_props.count()), &m_handle);
158}
159
161 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, secure_vector<uint8_t>()}};
162 module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
163 return attribute_map.at(attribute);
164}
165
167 std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, value}};
168 module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
169}
170
171void Object::destroy() const {
172 module()->C_DestroyObject(m_session.get().handle(), m_handle);
173}
174
175ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const {
176 ObjectHandle copied_handle = {};
177 module()->C_CopyObject(m_session.get().handle(),
178 m_handle,
179 modified_attributes.data(),
180 checked_ulong_cast(modified_attributes.count()),
181 &copied_handle);
182 return copied_handle;
183}
184} // namespace Botan::PKCS11
Helper class to build the Attribute / CK_ATTRIBUTE structures.
Definition p11_object.h: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.
void add_numeric(AttributeType attribute, T value)
Definition p11_object.h:96
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)
BOTAN_FUTURE_EXPLICIT CertificateProperties(CertificateType cert_type)
CertificateType cert_type() const
Definition p11_object.h:249
BOTAN_FUTURE_EXPLICIT DomainParameterProperties(KeyType key_type)
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:369
bool C_FindObjectsInit(SessionHandle session, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:379
bool C_GetAttributeValue(SessionHandle session, ObjectHandle object, Attribute *attribute_template_ptr, Ulong count, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:359
bool C_DestroyObject(SessionHandle session, ObjectHandle object, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:348
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:337
bool C_FindObjectsFinal(SessionHandle session, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:397
bool C_FindObjects(SessionHandle session, ObjectHandle *object_ptr, Ulong max_object_count, Ulong *object_count_ptr, ReturnValue *return_value=ThrowException) const
Definition p11.cpp:387
std::vector< ObjectHandle > find(std::uint32_t max_count=100) const
Module & module() const
Definition p11_object.h:146
~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:154
ObjectClass object_class() const
Definition p11_object.h:160
BOTAN_FUTURE_EXPLICIT ObjectProperties(ObjectClass object_class)
Module & module() const
Definition p11_object.h:547
ObjectHandle handle() const
Definition p11_object.h:541
Session & session() const
Definition p11_object.h:544
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
BOTAN_FUTURE_EXPLICIT PrivateKeyProperties(KeyType key_type)
BOTAN_FUTURE_EXPLICIT PublicKeyProperties(KeyType key_type)
BOTAN_FUTURE_EXPLICIT SecretKeyProperties(KeyType key_type)
Represents a PKCS#11 session.
Definition p11_types.h:125
BOTAN_FUTURE_EXPLICIT StorageObjectProperties(ObjectClass object_class)
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
AttributeType
Definition p11.h:50
CertificateType
Definition p11.h:213
CK_OBJECT_HANDLE ObjectHandle
Definition p11.h:1214
CK_ATTRIBUTE Attribute
Definition p11.h:1213
const Bbool True
Definition p11.h:1225
CK_ULONG Ulong
Definition p11.h:1204
const Bbool False
Definition p11.h:1226
CK_BBOOL Bbool
Definition p11.h:1202
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
CK_RV C_CreateObject(CK_SESSION_HANDLE, CK_ATTRIBUTE *, CK_ULONG, CK_OBJECT_HANDLE *)
unsigned long int CK_ULONG
Definition pkcs11.h:20
CK_ULONG CK_CERTIFICATE_TYPE
Definition pkcs11.h:46
CK_ULONG CK_ATTRIBUTE_TYPE
Definition pkcs11.h:44