• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.08
/src/lib/prov/pkcs11/p11_object.cpp
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::PKCS11 {
13

14
AttributeContainer::AttributeContainer(ObjectClass object_class) { add_class(object_class); }
46✔
15

16
void AttributeContainer::add_class(ObjectClass object_class) {
47✔
17
   m_numerics.emplace_back(static_cast<uint64_t>(object_class));
47✔
18
   add_attribute(
47✔
19
      AttributeType::Class, reinterpret_cast<uint8_t*>(&m_numerics.back()), static_cast<Ulong>(sizeof(ObjectClass)));
47✔
20
}
47✔
21

22
void AttributeContainer::add_string(AttributeType attribute, std::string_view value) {
44✔
23
   m_strings.push_back(std::string(value));
44✔
24
   add_attribute(
44✔
25
      attribute, reinterpret_cast<const uint8_t*>(m_strings.back().data()), static_cast<Ulong>(value.size()));
44✔
26
}
44✔
27

28
void AttributeContainer::add_binary(AttributeType attribute, const uint8_t* value, size_t length) {
59✔
29
   m_vectors.push_back(secure_vector<uint8_t>(value, value + length));
118✔
30
   add_attribute(attribute, reinterpret_cast<const uint8_t*>(m_vectors.back().data()), static_cast<Ulong>(length));
59✔
31
}
59✔
32

33
void AttributeContainer::add_bool(AttributeType attribute, bool value) {
187✔
34
   m_numerics.push_back(value ? True : False);
222✔
35
   add_attribute(attribute, reinterpret_cast<uint8_t*>(&m_numerics.back()), sizeof(Bbool));
187✔
36
}
187✔
37

38
void AttributeContainer::add_attribute(AttributeType attribute, const uint8_t* value, Ulong size) {
394✔
39
   bool exists = false;
394✔
40
   // check if the attribute has been added already
41
   for(auto& existing_attribute : m_attributes) {
1,851✔
42
      if(existing_attribute.type == static_cast<CK_ATTRIBUTE_TYPE>(attribute)) {
1,458✔
43
         // remove old entries
44
         m_strings.remove_if(
1✔
45
            [&existing_attribute](std::string_view data) { return data.data() == existing_attribute.pValue; });
1✔
46

47
         m_numerics.remove_if(
1✔
48
            [&existing_attribute](const uint64_t& data) { return &data == existing_attribute.pValue; });
6✔
49

50
         m_vectors.remove_if([&existing_attribute](const secure_vector<uint8_t>& data) {
1✔
51
            return data.data() == existing_attribute.pValue;
1✔
52
         });
53

54
         existing_attribute.pValue = const_cast<uint8_t*>(value);
1✔
55
         existing_attribute.ulValueLen = size;
1✔
56
         exists = true;
1✔
57
         break;
1✔
58
      }
59
   }
60

61
   if(!exists) {
1✔
62
      m_attributes.push_back(Attribute{static_cast<CK_ATTRIBUTE_TYPE>(attribute), const_cast<uint8_t*>(value), size});
393✔
63
   }
64
}
394✔
65

66
// ====================================================================================================
67

68
ObjectFinder::ObjectFinder(Session& session, const std::vector<Attribute>& search_template) :
3✔
69
      m_session(session), m_search_terminated(false) {
3✔
70
   module()->C_FindObjectsInit(m_session.get().handle(),
3✔
71
                               const_cast<Attribute*>(search_template.data()),
3✔
72
                               static_cast<Ulong>(search_template.size()));
3✔
73
}
3✔
74

75
ObjectFinder::~ObjectFinder() noexcept {
3✔
76
   try {
3✔
77
      if(m_search_terminated == false) {
3✔
78
         module()->C_FindObjectsFinal(m_session.get().handle(), nullptr);
2✔
79
      }
80
   } catch(...) {
×
81
      // ignore error during noexcept function
82
   }
×
83
}
3✔
84

85
std::vector<ObjectHandle> ObjectFinder::find(uint32_t max_count) const {
3✔
86
   std::vector<ObjectHandle> result(max_count);
3✔
87
   Ulong objectCount = 0;
3✔
88
   module()->C_FindObjects(m_session.get().handle(), result.data(), max_count, &objectCount);
3✔
89
   if(objectCount < max_count) {
3✔
90
      result.resize(objectCount);
3✔
91
   }
92
   return result;
3✔
93
}
×
94

95
void ObjectFinder::finish() {
1✔
96
   module()->C_FindObjectsFinal(m_session.get().handle());
1✔
97
   m_search_terminated = true;
1✔
98
}
1✔
99

100
// ====================================================================================================
101

102
ObjectProperties::ObjectProperties(ObjectClass object_class) :
46✔
103
      AttributeContainer(object_class), m_object_class(object_class) {}
46✔
104

105
// ====================================================================================================
106

107
StorageObjectProperties::StorageObjectProperties(ObjectClass object_class) : ObjectProperties(object_class) {}
46✔
108

109
// ====================================================================================================
110

111
DataObjectProperties::DataObjectProperties() : StorageObjectProperties(ObjectClass::Data) {}
4✔
112

113
// ====================================================================================================
114

115
CertificateProperties::CertificateProperties(CertificateType cert_type) :
1✔
116
      StorageObjectProperties(ObjectClass::Certificate), m_cert_type(cert_type) {
1✔
117
   add_numeric(AttributeType::CertificateType, static_cast<CK_CERTIFICATE_TYPE>(m_cert_type));
1✔
118
}
1✔
119

120
// ====================================================================================================
121

122
KeyProperties::KeyProperties(ObjectClass object_class, KeyType key_type) :
41✔
123
      StorageObjectProperties(object_class), m_key_type(key_type) {
41✔
124
   add_numeric(AttributeType::KeyType, static_cast<CK_ULONG>(m_key_type));
41✔
125
}
41✔
126

127
// ====================================================================================================
128

129
PublicKeyProperties::PublicKeyProperties(KeyType key_type) : KeyProperties(ObjectClass::PublicKey, key_type) {}
20✔
130

131
// ====================================================================================================
132

133
PrivateKeyProperties::PrivateKeyProperties(KeyType key_type) : KeyProperties(ObjectClass::PrivateKey, key_type) {}
21✔
134

135
// ====================================================================================================
136

137
SecretKeyProperties::SecretKeyProperties(KeyType key_type) : KeyProperties(ObjectClass::SecretKey, key_type) {}
×
138

139
// ====================================================================================================
140

141
DomainParameterProperties::DomainParameterProperties(KeyType key_type) :
×
142
      StorageObjectProperties(ObjectClass::DomainParameters), m_key_type(key_type) {
×
143
   add_numeric(AttributeType::KeyType, static_cast<CK_ULONG>(m_key_type));
×
144
}
×
145

146
// ====================================================================================================
147

148
Object::Object(Session& session, ObjectHandle handle) : m_session(session), m_handle(handle) {}
32✔
149

150
Object::Object(Session& session, const ObjectProperties& obj_props) : m_session(session), m_handle(0) {
16✔
151
   m_session.get().module()->C_CreateObject(
16✔
152
      m_session.get().handle(), obj_props.data(), static_cast<Ulong>(obj_props.count()), &m_handle);
16✔
153
}
16✔
154

155
secure_vector<uint8_t> Object::get_attribute_value(AttributeType attribute) const {
61✔
156
   std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, secure_vector<uint8_t>()}};
122✔
157
   module()->C_GetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
61✔
158
   return attribute_map.at(attribute);
61✔
159
}
61✔
160

161
void Object::set_attribute_value(AttributeType attribute, const secure_vector<uint8_t>& value) const {
1✔
162
   std::map<AttributeType, secure_vector<uint8_t>> attribute_map = {{attribute, value}};
2✔
163
   module()->C_SetAttributeValue(m_session.get().handle(), m_handle, attribute_map);
1✔
164
}
1✔
165

166
void Object::destroy() const { module()->C_DestroyObject(m_session.get().handle(), m_handle); }
44✔
167

168
ObjectHandle Object::copy(const AttributeContainer& modified_attributes) const {
1✔
169
   ObjectHandle copied_handle;
1✔
170
   module()->C_CopyObject(m_session.get().handle(),
1✔
171
                          m_handle,
1✔
172
                          modified_attributes.data(),
173
                          static_cast<Ulong>(modified_attributes.count()),
1✔
174
                          &copied_handle);
175
   return copied_handle;
1✔
176
}
177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc