-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhtool_key_rotation.c
More file actions
548 lines (517 loc) · 16.4 KB
/
htool_key_rotation.c
File metadata and controls
548 lines (517 loc) · 16.4 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright 2025 Google LLC
// 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
// https://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.guage governing permissions and
// limitations under the License.
#include "htool_key_rotation.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "htool.h"
#include "htool_cmd.h"
#include "protocol/host_cmd.h"
#include "protocol/key_rotation.h"
static const char* get_error_code_string(enum key_rotation_err err) {
if (err < KEY_ROTATION_ERR_HOTH_BASE) {
switch (err) {
case KEY_ROTATION_CMD_SUCCESS:
return "Success";
case KEY_ROTATION_INTERNAL_ERR:
return "Internal Error";
case KEY_ROTATION_ERR_INVALID_PARAM:
return "Invalid Parameter";
case KEY_ROTATION_ERR_INVALID_RESPONSE_SIZE:
return "Invalid Response Size";
case KEY_ROTATION_INITIATE_FAIL:
return "Key Rotation Record Initiate Failed";
case KEY_ROTATION_COMMIT_FAIL:
return "Key Rotation Record Commit Failed";
case KEY_ROTATION_ROOT_OF_TRUST_UNAVAILABLE:
return "Root of Trust Unavailable";
default:
return "Unknown";
}
}
int ret = err - KEY_ROTATION_ERR_HOTH_BASE;
switch (ret) {
case HOTH_RES_SUCCESS:
return "Success";
case HOTH_RES_INVALID_COMMAND:
return "Key Rotation Feature Not Supported";
case HOTH_RES_ERROR:
return "Hoth Error";
case HOTH_RES_INVALID_PARAM:
return "Invalid Parameter";
case HOTH_RES_ACCESS_DENIED:
return "Access Denied";
case HOTH_RES_INVALID_RESPONSE:
return "Invalid Response";
case HOTH_RES_INVALID_VERSION:
return "Invalid Version";
case HOTH_RES_INVALID_CHECKSUM:
return "Invalid Checksum";
case HOTH_RES_IN_PROGRESS:
return "In Progress";
case HOTH_RES_UNAVAILABLE:
return "Unavailable";
case HOTH_RES_TIMEOUT:
return "Timeout";
case HOTH_RES_OVERFLOW:
return "Overflow";
case HOTH_RES_INVALID_HEADER:
return "Invalid Header";
case HOTH_RES_REQUEST_TRUNCATED:
return "Request Truncated";
case HOTH_RES_RESPONSE_TOO_BIG:
return "Response Too Big";
case HOTH_RES_BUS_ERROR:
return "Bus Error";
case HOTH_RES_BUSY:
return "Busy";
case HOTH_RES_INVALID_HEADER_VERSION:
return "Invalid Header Version";
case HOTH_RES_INVALID_HEADER_CRC:
return "Invalid Header CRC";
case HOTH_RES_INVALID_DATA_CRC:
return "Invalid Data CRC";
case HOTH_RES_DUP_UNAVAILABLE:
return "Duplicate Request Unavailable";
default:
return "Unknown";
}
}
static const char* get_validation_method_string(uint32_t validation_method) {
switch (validation_method) {
case 1:
return "Embedded Key";
case 2:
return "Payload Key";
case 3:
return "Hash";
default:
return "Unknown";
}
}
int htool_key_rotation_get_status(void) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
struct hoth_response_key_rotation_status status;
enum key_rotation_err ret = libhoth_key_rotation_get_status(dev, &status);
if (ret) {
fprintf(stderr, "HOTH_KEY_ROTATION_GET_STATUS error: %s\n",
get_error_code_string(ret));
fprintf(stderr, "Failed to get key rotation status\n");
return -1;
}
printf("version : %u\n", status.version);
printf("image_family : %u\n", status.image_family);
printf("image_family_variant : %u\n", status.image_family_variant);
printf("validation_method : %s\n",
get_validation_method_string(status.validation_method));
printf("validation_key_data : 0x%x\n", status.validation_key_data);
printf("validation_hash_data : 0x%x\n", status.validation_hash_data);
return 0;
}
int htool_key_rotation_get_version(void) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
struct hoth_response_key_rotation_record_version version;
enum key_rotation_err ret = libhoth_key_rotation_get_version(dev, &version);
if (ret) {
fprintf(stderr, "HOTH_KEY_ROTATION_GET_VERSION error: %s\n",
get_error_code_string(ret));
fprintf(stderr, "Failed to get key rotation version\n");
return -1;
}
printf("version : %u\n", version.version);
return 0;
}
static int read_image_file(const char *image_file, uint8_t **image,
size_t *size) {
int fd = -1;
int result = 0;
struct stat statbuf;
fd = open(image_file, O_RDONLY, 0);
if (fd == -1) {
fprintf(stderr, "Error opening file %s: %s\n", image_file, strerror(errno));
return -1;
}
if (fstat(fd, &statbuf)) {
fprintf(stderr, "fstat error: %s\n", strerror(errno));
result = -1;
goto cleanup;
}
if (statbuf.st_size > SIZE_MAX) {
fprintf(stderr, "file too large\n");
result = -1;
goto cleanup;
}
*size = statbuf.st_size;
*image = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
if (*image == MAP_FAILED) {
fprintf(stderr, "mmap error: %s\n", strerror(errno));
result = -1;
goto cleanup;
}
cleanup:
if (fd != -1) {
if (close(fd) != 0) {
fprintf(stderr, "close error: %s\n", strerror(errno));
if (result == 0) {
result = -1;
}
}
}
if (result != 0 && *image != MAP_FAILED && *size > 0) {
if (munmap(*image, *size) != 0) {
fprintf(stderr, "munmap error: %s\n", strerror(errno));
}
}
return result;
}
int htool_key_rotation_update(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
const char *image_file;
if (htool_get_param_string(inv, "source-file", &image_file) != 0) {
return -1;
}
uint8_t *image = MAP_FAILED;
size_t size = 0;
int result = read_image_file(image_file, &image, &size);
if (result != 0) {
return result;
}
enum key_rotation_err key_ret = libhoth_key_rotation_update(dev, image, size);
if (key_ret) {
fprintf(stderr, "HOTH_KEY_ROTATION_UPDATE error: %s\n",
get_error_code_string(key_ret));
fprintf(stderr, "Failed to update key rotation record\n");
result = key_ret;
}
if (image != MAP_FAILED) {
if (munmap(image, size) != 0) {
fprintf(stderr, "munmap error: %s\n", strerror(errno));
result = -1;
}
}
return result;
}
int htool_key_rotation_payload_status() {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
struct hoth_response_key_rotation_payload_status payload_status;
enum key_rotation_err ret =
libhoth_key_rotation_payload_status(dev, &payload_status);
if (ret) {
fprintf(stderr, "HOTH_KEY_ROTATION_PAYLOAD_STATUS error: %s\n",
get_error_code_string(ret));
fprintf(stderr, "Failed to get key rotation payload status\n");
return -1;
}
printf("validation_method : %s\n",
get_validation_method_string(payload_status.validation_method));
printf("validation_key_data : 0x%x\n", payload_status.validation_key_data);
printf("validation_hash_data : 0x%x\n",
payload_status.validation_hash_data);
return 0;
}
static int get_key_rotation_read_half(const char *read_half,
uint32_t *read_half_value) {
if (!strcmp(read_half, "active")) {
*read_half_value = KEY_ROTATION_RECORD_READ_HALF_ACTIVE;
} else if (!strcmp(read_half, "staging")) {
*read_half_value = KEY_ROTATION_RECORD_READ_HALF_STAGING;
} else if (!strcmp(read_half, "a")) {
*read_half_value = KEY_ROTATION_RECORD_READ_HALF_A;
} else if (!strcmp(read_half, "b")) {
*read_half_value = KEY_ROTATION_RECORD_READ_HALF_B;
} else {
fprintf(stderr, "Invalid read_half value: %s\n", read_half);
return -1;
}
return 0;
}
int htool_key_rotation_read(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
uint32_t offset = 0;
uint32_t size = 0;
const char *read_half_string;
if (htool_get_param_u32(inv, "offset", &offset) ||
htool_get_param_u32(inv, "size", &size) ||
htool_get_param_string(inv, "half", &read_half_string)) {
return -1;
}
uint32_t read_half = 0;
int ret_half = get_key_rotation_read_half(read_half_string, &read_half);
if (ret_half) {
return -1;
}
const char *output_file = NULL;
bool output_to_file =
(htool_get_param_string(inv, "output_file", &output_file) == 0) &&
(output_file != NULL) && (strlen(output_file) > 0);
int fd = -1;
if (output_to_file) {
if (access(output_file, F_OK) == 0) {
fprintf(stderr,
"Warning: File '%s' exists and will be overwritten. Continue? "
"(y/N) ",
output_file);
char confirmation;
if (scanf(" %c", &confirmation) != 1 ||
(confirmation != 'y' && confirmation != 'Y')) {
fprintf(stderr, "Operation cancelled.\n");
return -1;
}
}
fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
fprintf(stderr, "Error opening file %s: %s\n", output_file,
strerror(errno));
return -1;
}
}
struct hoth_response_key_rotation_record_read read_response;
enum key_rotation_err ret_read =
libhoth_key_rotation_read(dev, offset, size, read_half, &read_response);
if (ret_read) {
fprintf(stderr, "HOTH_KEY_ROTATION_READ error: %s\n",
get_error_code_string(ret_read));
fprintf(stderr, "Failed to read key rotation record\n");
if (fd != -1) {
close(fd);
}
return -1;
}
if (output_to_file) {
if (write(fd, read_response.data, size) != size) {
fprintf(stderr, "Error writing to file %s: %s\n", output_file,
strerror(errno));
close(fd);
return -1;
}
if (close(fd) != 0) {
fprintf(stderr, "close error: %s\n", strerror(errno));
return -1;
}
} else {
printf("Key rotation record data:\n");
for (int i = 0; i < size; i++) {
printf("0x%02X ", read_response.data[i]);
if ((i + 1) % 16 == 0) {
printf("\n");
}
}
printf("\n");
}
return 0;
}
static int get_key_rotation_chunk_type(const char *chunk_type_string,
uint32_t *chunk_typecode) {
if (!strcmp(chunk_type_string, "pkey")) {
*chunk_typecode = KEY_ROTATION_CHUNK_TYPE_CODE_PKEY;
} else if (!strcmp(chunk_type_string, "hash")) {
*chunk_typecode = KEY_ROTATION_CHUNK_TYPE_CODE_HASH;
} else if (!strcmp(chunk_type_string, "bkey")) {
*chunk_typecode = KEY_ROTATION_CHUNK_TYPE_CODE_BKEY;
} else if (!strcmp(chunk_type_string, "bash")) {
*chunk_typecode = KEY_ROTATION_CHUNK_TYPE_CODE_BASH;
} else {
fprintf(stderr, "Invalid chunk_type value: %s\n", chunk_type_string);
return -1;
}
return 0;
}
int htool_key_rotation_read_chunk_type(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
uint32_t offset = 0;
uint32_t size = 0;
uint32_t chunk_index = 0;
const char *chunk_type_string;
if (htool_get_param_u32(inv, "offset", &offset) ||
htool_get_param_u32(inv, "size", &size) ||
htool_get_param_string(inv, "type", &chunk_type_string) ||
htool_get_param_u32(inv, "idx", &chunk_index)) {
return -1;
}
uint32_t chunk_typecode = 0;
int ret_half =
get_key_rotation_chunk_type(chunk_type_string, &chunk_typecode);
if (ret_half) {
return -1;
}
const char *output_file = NULL;
bool output_to_file =
(htool_get_param_string(inv, "output_file", &output_file) == 0) &&
(output_file != NULL) && (strlen(output_file) > 0);
int fd = -1;
if (output_to_file) {
if (access(output_file, F_OK) == 0) {
fprintf(stderr,
"Warning: File '%s' exists and will be overwritten. Continue? "
"(y/N) ",
output_file);
char confirmation;
if (scanf(" %c", &confirmation) != 1 ||
(confirmation != 'y' && confirmation != 'Y')) {
fprintf(stderr, "Operation cancelled.\n");
return -1;
}
}
fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
fprintf(stderr, "Error opening file %s: %s\n", output_file,
strerror(errno));
return -1;
}
}
uint16_t response_size = 0;
struct hoth_response_key_rotation_record_read read_response;
enum key_rotation_err ret_read = libhoth_key_rotation_read_chunk_type(
dev, chunk_typecode, chunk_index, offset, size, &read_response,
&response_size);
if (ret_read) {
fprintf(stderr, "HOTH_KEY_ROTATION_READ_CHUNK_TYPE error: %s\n",
get_error_code_string(ret_read));
fprintf(stderr, "Failed to read chunk from key rotation record\n");
return -1;
}
if (size == 0) {
size = response_size;
}
if (size != response_size) {
fprintf(
stderr,
"Error reading chunk from key rotation record. Expected %u; Got %u\n",
size, response_size);
return -1;
}
if (output_to_file) {
if (write(fd, read_response.data, size) != size) {
fprintf(stderr, "Error writing to file %s: %s\n", output_file,
strerror(errno));
close(fd);
return -1;
}
if (close(fd) != 0) {
fprintf(stderr, "close error: %s\n", strerror(errno));
return -1;
}
} else {
printf("Key rotation record data:\n");
for (int i = 0; i < size; i++) {
printf("0x%02X ", read_response.data[i]);
if ((i + 1) % 16 == 0) {
printf("\n");
}
}
printf("\n");
}
return 0;
}
int htool_key_rotation_chunk_type_count(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
fprintf(stderr, "Failed to get libhoth device\n");
return -1;
}
const char *chunk_type_string;
if (htool_get_param_string(inv, "type", &chunk_type_string)) {
return -1;
}
uint32_t chunk_typecode = 0;
int ret_half =
get_key_rotation_chunk_type(chunk_type_string, &chunk_typecode);
if (ret_half) {
return -1;
}
uint16_t chunk_count = 0;
enum key_rotation_err ret_count =
libhoth_key_rotation_chunk_type_count(dev, chunk_typecode, &chunk_count);
if (ret_count) {
fprintf(stderr, "HOTH_KEY_ROTATION_CHUNK_TYPE_COUNT error: %s\n",
get_error_code_string(ret_count));
fprintf(stderr, "Failed to get chunk type count\n");
return -1;
}
printf("chunk_count: %u\n", chunk_count);
return 0;
}
int htool_key_rotation_erase_record(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
return -1;
}
enum key_rotation_err ret = libhoth_key_rotation_erase_record(dev);
if (ret) {
fprintf(stderr, "Failed to erase key rotation record\n");
return -1;
}
printf("Key rotation record erased successfully\n");
return 0;
}
int htool_key_rotation_set_mauv(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
return -1;
}
uint32_t mauv;
if (htool_get_param_u32(inv, "mauv", &mauv)) {
return -1;
}
enum key_rotation_err ret = libhoth_key_rotation_set_mauv(dev, mauv);
if (ret) {
fprintf(stderr, "Failed to set key rotation MAUV\n");
return -1;
}
printf("Key rotation MAUV set successfully\n");
return 0;
}
int htool_key_rotation_get_mauv(const struct htool_invocation *inv) {
struct libhoth_device *dev = htool_libhoth_device();
if (!dev) {
return -1;
}
struct hoth_response_key_rotation_mauv mauv;
enum key_rotation_err ret = libhoth_key_rotation_get_mauv(dev, &mauv);
if (ret) {
fprintf(stderr, "Failed to get key rotation MAUV\n");
return -1;
}
printf("Key rotation MAUV: %u\n", mauv.mauv);
return 0;
}