forked from trusty-ia/trusty_app_keymaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure_storage.cpp
478 lines (428 loc) · 15.6 KB
/
secure_storage.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "secure_storage.h"
#include <errno.h>
#include <stdio.h>
#include <uapi/err.h>
#include <lib/storage/storage.h>
#include <keymaster/UniquePtr.h>
#include "trusty_keymaster_context.h"
#include "trusty_logger.h"
namespace keymaster {
namespace {
// Name of the attestation key file is kAttestKeyPrefix.%algorithm, where
// algorithm is either "ec" or "rsa".
const char* kAttestKeyPrefix = "AttestKey.";
// Name of the attestation certificate file is
// kAttestCertPrefix.%algorithm.%index, where index is the index within the
// certificate chain.
const char* kAttestCertPrefix = "AttestCert.";
const char* kAttestUuidFileName = "AttestUuid";
const char* kProductIdFileName = "ProductId";
// Maximum file name size.
static const int kStorageIdLengthMax = 64;
// RAII wrapper for storage_session_t
class StorageSession {
public:
StorageSession() {
error_ = storage_open_session(&handle_, STORAGE_CLIENT_TP_PORT);
if (error_ < 0) {
LOG_E("Error: [%d] opening storage session", error_);
}
}
~StorageSession() {
if (error_ < 0) {
return;
}
storage_close_session(handle_);
error_ = -EINVAL;
}
int error() const { return error_; }
storage_session_t handle() { return handle_; }
private:
storage_session_t handle_ = 0;
int error_ = -EINVAL;
};
// RAII wrapper for file_handle_t
class FileHandle {
public:
FileHandle(const char* filename, int flags) {
if (session_.error() == 0) {
error_ = storage_open_file(session_.handle(), &handle_,
const_cast<char*>(filename), flags, 0);
} else {
error_ = session_.error();
}
}
~FileHandle() {
if (error_ != 0) {
return;
}
storage_close_file(handle_);
error_ = -EINVAL;
}
int error() const { return error_; }
file_handle_t handle() { return handle_; }
private:
StorageSession session_;
int error_ = -EINVAL;
file_handle_t handle_ = 0;
};
bool SecureStorageWrite(const char* filename, const void* data, uint32_t size) {
FileHandle file(filename,
STORAGE_FILE_OPEN_CREATE | STORAGE_FILE_OPEN_TRUNCATE);
if (file.error() < 0) {
return false;
}
int rc = storage_write(file.handle(), 0, data, size, STORAGE_OP_COMPLETE);
if (rc < 0) {
LOG_E("Error: [%d] writing storage object '%s'", rc, filename);
return false;
}
if (static_cast<uint32_t>(rc) < size) {
LOG_E("Error: invalid object size [%d] from '%s'", rc, filename);
return false;
}
return true;
}
bool SecureStorageRead(const char* filename, void* data, uint32_t size) {
FileHandle file(filename, STORAGE_FILE_OPEN_CREATE);
if (file.error() < 0) {
return false;
}
int rc = storage_read(file.handle(), 0, data, size);
if (rc < 0) {
LOG_E("Error: [%d] reading storage object '%s'", rc, filename);
return false;
}
if (static_cast<uint32_t>(rc) < size) {
LOG_E("Error: invalid object size [%d] from '%s'", rc, filename);
return false;
}
return true;
}
bool SecureStorageGetFileSize(const char* filename, uint64_t* size) {
FileHandle file(filename, STORAGE_FILE_OPEN_CREATE);
if (file.error() < 0) {
return false;
}
int rc = storage_get_file_size(file.handle(), size);
if (rc < 0) {
LOG_E("Error: [%d] reading storage object '%s'", rc, filename);
return false;
}
return true;
}
bool SecureStorageDeleteFile(const char* filename) {
StorageSession session;
if (session.error() < 0) {
return false;
}
int rc = storage_delete_file(session.handle(), filename,
STORAGE_OP_COMPLETE);
if (rc < 0 && rc != ERR_NOT_FOUND) {
LOG_E("Error: [%d] deleting storage object '%s'", rc, filename);
return false;
}
return true;
}
const char* GetKeySlotStr(AttestationKeySlot key_slot) {
switch (key_slot) {
case AttestationKeySlot::kRsa:
return "rsa";
case AttestationKeySlot::kEcdsa:
return "ec";
case AttestationKeySlot::kEddsa:
return "ed";
case AttestationKeySlot::kEpid:
return "epid";
case AttestationKeySlot::kClaimable0:
return "c0";
case AttestationKeySlot::kSomRsa:
return "s_rsa";
case AttestationKeySlot::kSomEcdsa:
return "s_ec";
case AttestationKeySlot::kSomEddsa:
return "s_ed";
case AttestationKeySlot::kSomEpid:
return "s_epid";
default:
return "";
}
}
} // unnamed namespace
keymaster_error_t WriteKeyToStorage(AttestationKeySlot key_slot,
const uint8_t* key,
uint32_t key_size) {
UniquePtr<char[]> key_file(new char[kStorageIdLengthMax]);
snprintf(key_file.get(), kStorageIdLengthMax, "%s.%s", kAttestKeyPrefix,
GetKeySlotStr(key_slot));
if (!SecureStorageWrite(key_file.get(), key, key_size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
KeymasterKeyBlob ReadKeyFromStorage(AttestationKeySlot key_slot,
keymaster_error_t* error) {
UniquePtr<char[]> key_file(new char[kStorageIdLengthMax]);
snprintf(key_file.get(), kStorageIdLengthMax, "%s.%s", kAttestKeyPrefix,
GetKeySlotStr(key_slot));
uint64_t key_size_64;
if (!SecureStorageGetFileSize(key_file.get(), &key_size_64) ||
key_size_64 == 0) {
if (error)
*error = KM_ERROR_UNKNOWN_ERROR;
return {};
}
KeymasterKeyBlob result(key_size_64);
if (result.key_material == nullptr) {
if (error)
*error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
return {};
}
if (!SecureStorageRead(key_file.get(), result.writable_data(),
result.key_material_size)) {
if (error)
*error = KM_ERROR_UNKNOWN_ERROR;
return {};
}
if (error)
*error = KM_ERROR_OK;
return result;
}
keymaster_error_t AttestationKeyExists(AttestationKeySlot key_slot,
bool* exists) {
UniquePtr<char[]> key_file(new char[kStorageIdLengthMax]);
snprintf(key_file.get(), kStorageIdLengthMax, "%s.%s", kAttestKeyPrefix,
GetKeySlotStr(key_slot));
uint64_t size;
if (!SecureStorageGetFileSize(key_file.get(), &size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
*exists = size > 0;
return KM_ERROR_OK;
}
keymaster_error_t WriteCertToStorage(AttestationKeySlot key_slot,
const uint8_t* cert,
uint32_t cert_size,
uint32_t index) {
UniquePtr<char[]> cert_file(new char[kStorageIdLengthMax]);
uint32_t cert_chain_length;
bool update_cert_chain_length = false;
if (ReadCertChainLength(key_slot, &cert_chain_length) != KM_ERROR_OK) {
return KM_ERROR_UNKNOWN_ERROR;
}
if (index > cert_chain_length) {
return KM_ERROR_INVALID_ARGUMENT;
} else if (index == cert_chain_length) {
cert_chain_length += 1;
update_cert_chain_length = true;
}
snprintf(cert_file.get(), kStorageIdLengthMax, "%s.%s.%d",
kAttestCertPrefix, GetKeySlotStr(key_slot), index);
if (!SecureStorageWrite(cert_file.get(), cert, cert_size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
if (update_cert_chain_length &&
WriteCertChainLength(key_slot, cert_chain_length) != KM_ERROR_OK) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t ReadCertChainFromStorage(AttestationKeySlot key_slot,
keymaster_cert_chain_t* cert_chain) {
UniquePtr<char[]> cert_file(new char[kStorageIdLengthMax]);
uint32_t cert_chain_length;
uint64_t cert_size;
if (ReadCertChainLength(key_slot, &cert_chain_length) != KM_ERROR_OK ||
cert_chain_length == 0) {
return KM_ERROR_UNKNOWN_ERROR;
}
cert_chain->entry_count = cert_chain_length;
cert_chain->entries = new keymaster_blob_t[cert_chain_length];
if (!cert_chain->entries) {
return KM_ERROR_MEMORY_ALLOCATION_FAILED;
}
memset(cert_chain->entries, 0,
sizeof(cert_chain->entries[0]) * cert_chain_length);
// Read |cert_chain_length| certs from storage
for (uint32_t i = 0; i < cert_chain_length; i++) {
snprintf(cert_file.get(), kStorageIdLengthMax, "%s.%s.%d",
kAttestCertPrefix, GetKeySlotStr(key_slot), i);
if (!SecureStorageGetFileSize(cert_file.get(), &cert_size) ||
cert_size == 0) {
return KM_ERROR_UNKNOWN_ERROR;
}
UniquePtr<uint8_t[]> cert_data(new uint8_t[cert_size]);
if (!cert_data.get()) {
return KM_ERROR_MEMORY_ALLOCATION_FAILED;
}
if (!SecureStorageRead(cert_file.get(), cert_data.get(), cert_size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
cert_chain->entries[i].data_length = static_cast<uint32_t>(cert_size);
cert_chain->entries[i].data = cert_data.release();
}
return KM_ERROR_OK;
}
keymaster_error_t WriteCertChainLength(AttestationKeySlot key_slot,
uint32_t cert_chain_length) {
UniquePtr<char[]> cert_chain_length_file(new char[kStorageIdLengthMax]);
snprintf(cert_chain_length_file.get(), kStorageIdLengthMax, "%s.%s.length",
kAttestKeyPrefix, GetKeySlotStr(key_slot));
if (cert_chain_length > kMaxCertChainLength) {
return KM_ERROR_UNKNOWN_ERROR;
}
uint32_t current_cert_chain_length = 0;
if (ReadCertChainLength(key_slot, ¤t_cert_chain_length) !=
KM_ERROR_OK ||
cert_chain_length > current_cert_chain_length + 1) {
LOG_E("Error: Cannot increase certificate chain length by more than 1.",
0);
return KM_ERROR_INVALID_ARGUMENT;
}
if (!SecureStorageWrite(cert_chain_length_file.get(), &cert_chain_length,
sizeof(uint32_t))) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t ReadCertChainLength(AttestationKeySlot key_slot,
uint32_t* cert_chain_length) {
UniquePtr<char[]> cert_chain_length_file(new char[kStorageIdLengthMax]);
snprintf(cert_chain_length_file.get(), kStorageIdLengthMax, "%s.%s.length",
kAttestKeyPrefix, GetKeySlotStr(key_slot));
uint64_t file_size;
if (!SecureStorageGetFileSize(cert_chain_length_file.get(), &file_size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
if (file_size == 0) {
*cert_chain_length = 0;
return KM_ERROR_OK;
}
if (!SecureStorageRead(cert_chain_length_file.get(), cert_chain_length,
sizeof(uint32_t)) ||
*cert_chain_length > kMaxCertChainLength) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t WriteAttestationUuid(
const uint8_t attestation_uuid[kAttestationUuidSize]) {
if (!SecureStorageWrite(kAttestUuidFileName, attestation_uuid,
kAttestationUuidSize)) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t ReadAttestationUuid(
uint8_t attestation_uuid[kAttestationUuidSize]) {
uint64_t size;
if (!SecureStorageGetFileSize(kAttestUuidFileName, &size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
if (size < kAttestationUuidSize) {
memset(attestation_uuid, '\0', kAttestationUuidSize);
return KM_ERROR_OK;
}
if (!SecureStorageRead(kAttestUuidFileName, attestation_uuid,
kAttestationUuidSize)) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t DeleteKey(AttestationKeySlot key_slot) {
UniquePtr<char[]> key_file(new char[kStorageIdLengthMax]);
snprintf(key_file.get(), kStorageIdLengthMax, "%s.%s", kAttestKeyPrefix,
GetKeySlotStr(key_slot));
if (!SecureStorageDeleteFile(key_file.get())) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t DeleteCertChain(AttestationKeySlot key_slot) {
UniquePtr<char[]> cert_file(new char[kStorageIdLengthMax]);
uint32_t cert_chain_length;
if (ReadCertChainLength(key_slot, &cert_chain_length) != KM_ERROR_OK) {
return KM_ERROR_UNKNOWN_ERROR;
}
for (uint32_t i = 0; i < cert_chain_length; ++i) {
snprintf(cert_file.get(), kStorageIdLengthMax, "%s.%s.%d",
kAttestCertPrefix, GetKeySlotStr(key_slot), i);
if (!SecureStorageDeleteFile(cert_file.get())) {
return KM_ERROR_UNKNOWN_ERROR;
}
}
if (WriteCertChainLength(key_slot, 0) != KM_ERROR_OK) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t SetProductId(const uint8_t product_id[kProductIdSize]) {
uint64_t product_id_size;
if (!SecureStorageGetFileSize(kProductIdFileName, &product_id_size)) {
return KM_ERROR_UNKNOWN_ERROR;
}
#ifndef KEYMASTER_DEBUG
if (product_id_size != 0) {
LOG_E("Error: Product ID already set!\n", 0);
return KM_ERROR_INVALID_ARGUMENT;
}
#endif /* KEYMASTER_DEBUG */
if (!SecureStorageWrite(kProductIdFileName, product_id, kProductIdSize)) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t ReadProductId(uint8_t product_id[kProductIdSize]) {
uint64_t product_id_size;
if (!SecureStorageGetFileSize(kProductIdFileName, &product_id_size) ||
product_id_size != kProductIdSize) {
return KM_ERROR_UNKNOWN_ERROR;
}
if (!SecureStorageRead(kProductIdFileName, product_id, kProductIdSize)) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t DeleteProductId() {
if (!SecureStorageDeleteFile(kProductIdFileName)) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
static keymaster_error_t DeleteAttestationData(AttestationKeySlot key_slot) {
if (DeleteKey(key_slot) != KM_ERROR_OK ||
DeleteCertChain(key_slot) != KM_ERROR_OK) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
keymaster_error_t DeleteAllAttestationData() {
if (DeleteAttestationData(AttestationKeySlot::kRsa) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kEcdsa) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kEddsa) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kEpid) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kClaimable0) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kSomRsa) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kSomEcdsa) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kSomEddsa) != KM_ERROR_OK ||
DeleteAttestationData(AttestationKeySlot::kSomEpid) != KM_ERROR_OK) {
return KM_ERROR_UNKNOWN_ERROR;
}
return KM_ERROR_OK;
}
} // namespace keymaster