This repository was archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_pool_impl.cc
More file actions
262 lines (235 loc) · 7.92 KB
/
object_pool_impl.cc
File metadata and controls
262 lines (235 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "object_pool_impl.h"
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <base/logging.h>
#include <boost/thread/lock_guard.hpp>
#include "p11net.h"
#include "p11net_factory.h"
#include "p11net_utility.h"
#include "handle_generator.h"
#include "object.h"
#include "object_store.h"
#include "proto_bindings/attributes.pb.h"
using brillo::SecureBlob;
using std::map;
using std::string;
using std::shared_ptr;
using std::vector;
namespace p11net {
ObjectPoolImpl::ObjectPoolImpl(std::shared_ptr<P11NetFactory> factory,
std::shared_ptr<HandleGenerator> handle_generator,
std::unique_ptr<ObjectStore> store)
: factory_(factory),
handle_generator_(handle_generator),
store_(std::move(store))
{
store_.reset();
}
ObjectPoolImpl::~ObjectPoolImpl() {}
bool ObjectPoolImpl::Init() {
boost::lock_guard<boost::mutex> lock(lock_);
if (store_.get()) {
if (!LoadPublicObjects())
return false;
}
return true;
}
bool ObjectPoolImpl::GetInternalBlob(int blob_id, string* blob) {
boost::lock_guard<boost::mutex> lock(lock_);
if (store_.get())
return store_->GetInternalBlob(blob_id, blob);
return false;
}
bool ObjectPoolImpl::SetInternalBlob(int blob_id, const string& blob) {
boost::lock_guard<boost::mutex> lock(lock_);
if (store_.get())
return store_->SetInternalBlob(blob_id, blob);
return false;
}
bool ObjectPoolImpl::SetEncryptionKey(const SecureBlob& key) {
boost::lock_guard<boost::mutex> lock(lock_);
if (key.empty())
LOG(WARNING) << "WARNING: Private object services will not be available.";
if (store_.get() && !key.empty()) {
if (!store_->SetEncryptionKey(key))
return false;
// Once we have the encryption key we can load private objects.
if (!LoadPrivateObjects())
LOG(WARNING) << "Failed to load private objects.";
}
return true;
}
bool ObjectPoolImpl::Insert(Object* object) {
return Import(object);
}
bool ObjectPoolImpl::Import(Object* object) {
boost::lock_guard<boost::mutex> lock(lock_);
if (objects_.find(object) != objects_.end())
return false;
if (store_.get()) {
ObjectBlob serialized;
if (!Serialize(object, &serialized))
return false;
// Parsing the serialized blob will normalize the object attribute values.
// e.g. If the caller specified 32-bits for a CK_ULONG on a 64-bit system,
// the value will be resized correctly.
if (!Parse(serialized, object))
return false;
int store_id;
if (!store_->InsertObjectBlob(serialized, &store_id))
return false;
object->set_store_id(store_id);
}
object->set_handle(handle_generator_->CreateHandle());
objects_.insert(object);
handle_object_map_[object->handle()] = shared_ptr<const Object>(object);
return true;
}
bool ObjectPoolImpl::Delete(const Object* object) {
boost::lock_guard<boost::mutex> lock(lock_);
if (objects_.find(object) == objects_.end())
return false;
if (store_.get()) {
if (!store_->DeleteObjectBlob(object->store_id()))
return false;
}
handle_object_map_.erase(object->handle());
objects_.erase(object);
return true;
}
bool ObjectPoolImpl::DeleteAll() {
boost::lock_guard<boost::mutex> lock(lock_);
objects_.clear();
handle_object_map_.clear();
if (store_.get())
return store_->DeleteAllObjectBlobs();
return true;
}
bool ObjectPoolImpl::Find(const Object* search_template,
vector<const Object*>* matching_objects) {
boost::lock_guard<boost::mutex> lock(lock_);
for (ObjectSet::iterator it = objects_.begin(); it != objects_.end(); ++it) {
if (Matches(search_template, *it))
matching_objects->push_back(*it);
}
return true;
}
bool ObjectPoolImpl::FindByHandle(int handle, const Object** object) {
boost::lock_guard<boost::mutex> lock(lock_);
CHECK(object);
HandleObjectMap::iterator it = handle_object_map_.find(handle);
if (it == handle_object_map_.end())
return false;
*object = it->second.get();
return true;
}
Object* ObjectPoolImpl::GetModifiableObject(const Object* object) {
return const_cast<Object*>(object);
}
bool ObjectPoolImpl::Flush(const Object* object) {
boost::lock_guard<boost::mutex> lock(lock_);
if (objects_.find(object) == objects_.end())
return false;
if (store_.get()) {
ObjectBlob serialized;
if (!Serialize(object, &serialized))
return false;
if (!store_->UpdateObjectBlob(object->store_id(), serialized))
return false;
}
return true;
}
bool ObjectPoolImpl::Matches(const Object* object_template,
const Object* object) {
const AttributeMap* attributes = object_template->GetAttributeMap();
AttributeMap::const_iterator it;
for (it = attributes->begin(); it != attributes->end(); ++it) {
if (!object->IsAttributePresent(it->first))
return false;
if (it->second != object->GetAttributeString(it->first))
return false;
}
return true;
}
bool ObjectPoolImpl::Parse(const ObjectBlob& object_blob, Object* object) {
AttributeList attribute_list;
if (!attribute_list.ParseFromString(object_blob.blob)) {
LOG(ERROR) << "Failed to parse proto-buffer.";
return false;
}
for (int i = 0; i < attribute_list.attribute_size(); ++i) {
const Attribute& attribute = attribute_list.attribute(i);
if (!attribute.has_value()) {
LOG(WARNING) << "No value found for attribute: " << attribute.type();
continue;
}
object->SetAttributeString(attribute.type(), attribute.value());
// Correct the length of integral attributes since they may have been
// serialized with a different sizeof(CK_ULONG).
if (IsIntegralAttribute(attribute.type()) &&
attribute.value().length() != sizeof(CK_ULONG)) {
int int_value = object->GetAttributeInt(attribute.type(), 0);
object->SetAttributeInt(attribute.type(), int_value);
}
if (attribute.type() == CKA_PRIVATE &&
object->IsPrivate() != object_blob.is_private) {
// Assume this object has been tampered with.
LOG(ERROR) << "Privacy attribute mismatch.";
return false;
}
}
return true;
}
bool ObjectPoolImpl::Serialize(const Object* object, ObjectBlob* serialized) {
const AttributeMap* attribute_map = object->GetAttributeMap();
AttributeMap::const_iterator it;
AttributeList attribute_list;
for (it = attribute_map->begin(); it != attribute_map->end(); ++it) {
Attribute* next = attribute_list.add_attribute();
next->set_type(it->first);
next->set_length(it->second.length());
next->set_value(it->second);
}
if (!attribute_list.SerializeToString(&serialized->blob)) {
LOG(ERROR) << "Failed to serialize object.";
return false;
}
serialized->is_private = object->IsPrivate();
return true;
}
bool ObjectPoolImpl::LoadBlobs(const map<int, ObjectBlob>& object_blobs) {
map<int, ObjectBlob>::const_iterator it;
for (it = object_blobs.begin(); it != object_blobs.end(); ++it) {
shared_ptr<Object> object(factory_->CreateObject());
// An object that is not parsable will be ignored.
if (Parse(it->second, object.get())) {
object->set_handle(handle_generator_->CreateHandle());
object->set_store_id(it->first);
objects_.insert(object.get());
handle_object_map_[object->handle()] = object;
} else {
LOG(WARNING) << "Object not parsable: " << it->first;
}
}
return true;
}
bool ObjectPoolImpl::LoadPublicObjects() {
CHECK(store_.get());
map<int, ObjectBlob> object_blobs;
if (!store_->LoadPublicObjectBlobs(&object_blobs))
return false;
return LoadBlobs(object_blobs);
}
bool ObjectPoolImpl::LoadPrivateObjects() {
CHECK(store_.get());
map<int, ObjectBlob> object_blobs;
if (!store_->LoadPrivateObjectBlobs(&object_blobs))
return false;
return LoadBlobs(object_blobs);
}
} // namespace p11net