forked from openenclave/openenclave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcbinfo.c
1511 lines (1328 loc) · 48 KB
/
tcbinfo.c
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#include "tcbinfo.h"
#include <openenclave/internal/hexdump.h>
#include <openenclave/internal/raise.h>
#include <openenclave/internal/safecrt.h>
#include <openenclave/internal/trace.h>
#include <openenclave/internal/utils.h>
#include "../common.h"
#define SGX_TCB_STATUS_UP_TO_DATE "UpToDate"
#define SGX_TCB_STATUS_OUT_OF_DATE "OutOfDate"
#define SGX_TCB_STATUS_REVOKED "Revoked"
#define SGX_TCB_STATUS_CONFIGURATION_NEEDED "ConfigurationNeeded"
#define SGX_TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED \
"OutOfDateConfigurationNeeded"
#define SGX_TCB_STATUS_SW_HARDENING_NEEDED "SWHardeningNeeded"
#define SGX_TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED \
"ConfigurationAndSWHardeningNeeded"
#define SGX_TCB_STATUS_INVALID "Invalid"
// Public key of Intel's root certificate.
static const char* _trusted_root_key_pem =
"-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEC6nEwMDIYZOj/iPWsCzaEKi71OiO\n"
"SLRFhWGjbnBVJfVnkY4u3IjkDYYL0MxO4mqsyYjlBalTVYxFP2sJBK5zlA==\n"
"-----END PUBLIC KEY-----\n";
OE_INLINE uint8_t _is_space(uint8_t c)
{
return (
c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' ||
c == '\r' || c == '\0');
}
// Skip white space.
static const uint8_t* _skip_ws(const uint8_t* itr, const uint8_t* end)
{
while (itr < end && _is_space(*itr))
++itr;
return itr;
}
OE_INLINE uint8_t _is_digit(uint8_t c)
{
return (c >= '0' && c <= '9');
}
// Read a specific character at current position.
// Consume and skip trailing whitespace.
static oe_result_t _read(char ch, const uint8_t** itr, const uint8_t* end)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* p = *itr;
if (p < end && *p == ch)
{
*itr = _skip_ws(++p, end);
result = OE_OK;
}
return result;
}
// Read an integer literal in current position.
// Only the necessary subset of json numbers are supported.
// Integers must be a sequence of digits.
// Negative and floating point json numbers are not supported.
// Value must fit within an uint64_t.
static oe_result_t _read_integer(
const uint8_t** itr,
const uint8_t* end,
uint64_t* value)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* p = *itr;
*value = 0;
if (p < end && _is_digit(*p))
{
*value = (uint64_t)(*p - '0');
++p;
while (p < end && _is_digit(*p))
{
// Detect overflows.
if (*value >= OE_UINT64_MAX / 10)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
*value = *value * 10 + (uint64_t)(*p - '0');
++p;
}
*itr = _skip_ws(p, end);
result = OE_OK;
}
done:
return result;
}
// Read a string literal in current position.
// Only the necessary subset of json strings are supported.
// JSON escape sequences are not supported.
static oe_result_t _read_string(
const uint8_t** itr,
const uint8_t* end,
const uint8_t** str,
size_t* length)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* p = *itr;
*length = 0;
p = _skip_ws(p, end);
if (p < end && *p == '"')
{
*str = ++p;
while (p < end && *p != '"')
{
if (*p == '\\')
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
++p;
}
if (p < end && *p == '"')
{
*length = (size_t)(p - *str);
*itr = _skip_ws(++p, end);
result = OE_OK;
}
}
done:
return result;
}
static uint32_t _hex_to_dec(uint8_t hex)
{
if (hex >= '0' && hex <= '9')
return (uint32_t)hex - '0';
if (hex >= 'a' && hex <= 'f')
return (uint32_t)(hex - 'a') + 10;
if (hex >= 'A' && hex <= 'F')
return (uint32_t)(hex - 'A') + 10;
return 16;
}
// Read a hex string in current position
static oe_result_t _read_hex_string(
const uint8_t** itr,
const uint8_t* end,
uint8_t* bytes,
size_t length)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* str = NULL;
size_t str_length = 0;
uint32_t value = 0;
OE_CHECK(_read_string(itr, end, &str, &str_length));
// Each byte takes up two hex digits.
if (str_length == length * 2)
{
for (size_t i = 0; i < length; ++i)
{
value =
(_hex_to_dec(str[i * 2]) << 4) | _hex_to_dec(str[i * 2 + 1]);
if (value > OE_UCHAR_MAX)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
bytes[i] = (uint8_t)value;
}
result = OE_OK;
}
done:
return result;
}
static oe_result_t _read_property_name_and_colon(
const char* property_name,
const uint8_t** itr,
const uint8_t* end)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* name = NULL;
size_t name_length = 0;
const uint8_t* tmp_itr = *itr;
OE_CHECK(_read_string(&tmp_itr, end, &name, &name_length));
if (name_length == oe_strlen(property_name) &&
memcmp(property_name, name, name_length) == 0)
{
OE_CHECK(_read(':', &tmp_itr, end));
*itr = tmp_itr;
result = OE_OK;
}
done:
return result;
}
static bool _json_str_equal(
const uint8_t* str1,
size_t str1_length,
const char* str2)
{
size_t str2_length = oe_strlen(str2);
// Strings in json stream are not zero terminated.
// Hence the special comparison function.
return (str1_length == str2_length) &&
(memcmp(str1, str2, str2_length) == 0);
}
static oe_result_t _trace_json_string(const uint8_t* str, size_t str_length)
{
oe_result_t result = OE_OK;
if (oe_get_current_logging_level() >= OE_LOG_LEVEL_VERBOSE)
{
char* buffer = (char*)oe_malloc(str_length + 1);
if (buffer)
{
OE_CHECK(oe_memcpy_s(buffer, str_length + 1, str, str_length));
buffer[str_length] = 0;
OE_TRACE_VERBOSE("value = %s\n", buffer);
oe_free(buffer);
}
else
{
OE_RAISE(OE_OUT_OF_MEMORY);
}
}
done:
return result;
}
static oe_tcb_level_status_t _parse_tcb_status(
const uint8_t* tcb_status_string,
size_t length)
{
oe_tcb_level_status_t status;
status.AsUINT32 = OE_TCB_LEVEL_STATUS_UNKNOWN;
if (_json_str_equal(tcb_status_string, length, SGX_TCB_STATUS_UP_TO_DATE))
status.fields.up_to_date = 1;
else if (_json_str_equal(
tcb_status_string, length, SGX_TCB_STATUS_OUT_OF_DATE))
status.fields.outofdate = 1;
else if (_json_str_equal(tcb_status_string, length, SGX_TCB_STATUS_REVOKED))
status.fields.revoked = 1;
else if (_json_str_equal(
tcb_status_string,
length,
SGX_TCB_STATUS_CONFIGURATION_NEEDED))
status.fields.configuration_needed = 1;
else if (_json_str_equal(
tcb_status_string,
length,
SGX_TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED))
{
status.fields.outofdate = 1;
status.fields.configuration_needed = 1;
}
// Due to sgx LVI update, UpToDate tcb would be marked as SWHardeningNeeded,
// as sgx cannot tell if enclave writer has implemented SW mitigations for
// LVI. Set status SWHardeningNeeded as up_to_date for now to make sure
// services for those tcbs are not affected.
else if (_json_str_equal(
tcb_status_string, length, SGX_TCB_STATUS_SW_HARDENING_NEEDED))
{
status.fields.up_to_date = 1;
status.fields.sw_hardening_needed = 1;
}
else if (_json_str_equal(
tcb_status_string,
length,
SGX_TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED))
{
status.fields.configuration_needed = 1;
status.fields.sw_hardening_needed = 1;
}
return status;
}
oe_sgx_tcb_status_t oe_tcb_level_status_to_sgx_tcb_status(
oe_tcb_level_status_t tcb_level_status)
{
if (tcb_level_status.fields.revoked == 1)
return OE_SGX_TCB_STATUS_REVOKED;
if (tcb_level_status.fields.up_to_date == 1 &&
tcb_level_status.fields.sw_hardening_needed == 1)
return OE_SGX_TCB_STATUS_SW_HARDENING_NEEDED;
if (tcb_level_status.fields.up_to_date == 1)
return OE_SGX_TCB_STATUS_UP_TO_DATE;
if (tcb_level_status.fields.sw_hardening_needed == 1 &&
tcb_level_status.fields.configuration_needed == 1)
return OE_SGX_TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED;
if (tcb_level_status.fields.outofdate == 1 &&
tcb_level_status.fields.configuration_needed == 1)
return OE_SGX_TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED;
if (tcb_level_status.fields.configuration_needed == 1)
return OE_SGX_TCB_STATUS_CONFIGURATION_NEEDED;
if (tcb_level_status.fields.outofdate == 1)
return OE_SGX_TCB_STATUS_OUT_OF_DATE;
return OE_SGX_TCB_STATUS_INVALID;
}
const char* oe_sgx_tcb_status_str(const oe_sgx_tcb_status_t sgx_tcb_status)
{
switch (sgx_tcb_status)
{
case OE_SGX_TCB_STATUS_UP_TO_DATE:
return SGX_TCB_STATUS_UP_TO_DATE;
case OE_SGX_TCB_STATUS_OUT_OF_DATE:
return SGX_TCB_STATUS_OUT_OF_DATE;
case OE_SGX_TCB_STATUS_REVOKED:
return SGX_TCB_STATUS_REVOKED;
case OE_SGX_TCB_STATUS_CONFIGURATION_NEEDED:
return SGX_TCB_STATUS_CONFIGURATION_NEEDED;
case OE_SGX_TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED:
return SGX_TCB_STATUS_OUT_OF_DATE_CONFIGURATION_NEEDED;
case OE_SGX_TCB_STATUS_SW_HARDENING_NEEDED:
return SGX_TCB_STATUS_SW_HARDENING_NEEDED;
case OE_SGX_TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED:
return SGX_TCB_STATUS_CONFIGURATION_AND_SW_HARDENING_NEEDED;
default:
break;
}
return SGX_TCB_STATUS_INVALID;
}
/**
* Type: tcb in TCB Info tcbLevels
* Schema:
* {
* "sgxtcbcomp01svn": uint8_t,
* "sgxtcbcomp02svn": uint8_t,
* ...
* "sgxtcbcomp16svn": uint8_t,
* "pcesvn": uint16_t
* }
*/
static oe_result_t _read_tcb_info_tcb_level(
const uint8_t** itr,
const uint8_t* end,
oe_tcb_info_tcb_level_t* tcb_level)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
uint64_t value = 0;
static const char* _comp_names[] = {
"sgxtcbcomp01svn",
"sgxtcbcomp02svn",
"sgxtcbcomp03svn",
"sgxtcbcomp04svn",
"sgxtcbcomp05svn",
"sgxtcbcomp06svn",
"sgxtcbcomp07svn",
"sgxtcbcomp08svn",
"sgxtcbcomp09svn",
"sgxtcbcomp10svn",
"sgxtcbcomp11svn",
"sgxtcbcomp12svn",
"sgxtcbcomp13svn",
"sgxtcbcomp14svn",
"sgxtcbcomp15svn",
"sgxtcbcomp16svn"};
OE_STATIC_ASSERT(
OE_COUNTOF(_comp_names) == OE_COUNTOF(tcb_level->sgx_tcb_comp_svn));
OE_CHECK(_read('{', itr, end));
for (uint32_t i = 0; i < OE_COUNTOF(_comp_names); ++i)
{
OE_TRACE_VERBOSE("Reading %s", _comp_names[i]);
OE_CHECK(_read_property_name_and_colon(_comp_names[i], itr, end));
OE_CHECK(_read_integer(itr, end, &value));
OE_TRACE_VERBOSE("value = %lu", value);
OE_CHECK(_read(',', itr, end));
if (value > OE_UCHAR_MAX)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
tcb_level->sgx_tcb_comp_svn[i] = (uint8_t)value;
}
OE_TRACE_VERBOSE("Reading pcesvn");
OE_CHECK(_read_property_name_and_colon("pcesvn", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
OE_TRACE_VERBOSE("value = %lu", value);
OE_CHECK(_read('}', itr, end));
if (value > OE_USHRT_MAX)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
tcb_level->pce_svn = (uint16_t)value;
result = OE_OK;
done:
return result;
}
// Algorithm specified by Intel, reworded:
// 1. Go over the sorted collection of TCB levels in the JSON.
// 2. Choose the first tcb level for which all of the platform's comp svn
// values and pcesvn values are greater than or equal to corresponding values of
// the tcb level.
// 3. The status of the platform's tcb level is the status of the chosen tcb
// level.
// 4. If no tcb level was chosen, then the status of the platform is unknown.
static void _determine_platform_tcb_info_tcb_level(
oe_tcb_info_tcb_level_t* platform_tcb_level,
oe_tcb_info_tcb_level_t* tcb_level)
{
// If the platform's status has already been determined, return.
if (platform_tcb_level->status.AsUINT32 != OE_TCB_LEVEL_STATUS_UNKNOWN)
return;
// Compare all of the platform's comp svn values with the corresponding
// values in the current tcb level.
for (uint32_t i = 0; i < OE_COUNTOF(platform_tcb_level->sgx_tcb_comp_svn);
++i)
{
if (platform_tcb_level->sgx_tcb_comp_svn[i] <
tcb_level->sgx_tcb_comp_svn[i])
return;
}
if (platform_tcb_level->pce_svn < tcb_level->pce_svn)
return;
// If all the values of the tcb level are less than corresponding values of
// the platform, then the platform's status is the status of the current tcb
// level.
platform_tcb_level->status.AsUINT32 = tcb_level->status.AsUINT32;
}
// Found matching TCB level, move itr to the end of the array.
static void _move_to_end_of_tcb_levels(const uint8_t** itr, const uint8_t* end)
{
// Need a counter for '[', ']' to avoid early itr stop due to
// potential '[', ']' inside array;
uint64_t square_bracket_count = 0;
while (*itr < end && (**itr != ']' || square_bracket_count != 0))
{
if (**itr == '[')
square_bracket_count++;
else if (**itr == ']')
square_bracket_count--;
(*itr)++;
}
}
/**
* Type: tcbLevel in TCB Info (V1)
* Schema:
* {
* "tcb" : object of type tcb
* "status": one of "UpToDate" or "OutOfDate" or "Revoked" or
* "ConfigurationNeeded"
* }
*/
static oe_result_t _read_tcb_info_tcb_level_v1(
const uint8_t** itr,
const uint8_t* end,
oe_tcb_info_tcb_level_t* platform_tcb_level,
oe_tcb_info_tcb_level_t* tcb_level)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* status = NULL;
size_t status_length = 0;
OE_CHECK(_read_property_name_and_colon("tcbLevels", itr, end));
OE_CHECK(_read('[', itr, end));
// Read each tcbLevel
while (*itr < end)
{
OE_CHECK(_read('{', itr, end));
OE_TRACE_VERBOSE("Reading tcb");
OE_CHECK(_read_property_name_and_colon("tcb", itr, end));
OE_CHECK(_read_tcb_info_tcb_level(itr, end, tcb_level));
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading status");
OE_CHECK(_read_property_name_and_colon("status", itr, end));
OE_CHECK(_read_string(itr, end, &status, &status_length));
OE_CHECK(_trace_json_string(status, status_length));
OE_CHECK(_read('}', itr, end));
tcb_level->status = _parse_tcb_status(status, status_length);
if (tcb_level->status.AsUINT32 != OE_TCB_LEVEL_STATUS_UNKNOWN)
{
_determine_platform_tcb_info_tcb_level(
platform_tcb_level, tcb_level);
}
// Read end of array or comma separator.
if (*itr < end && **itr == ']')
break;
OE_CHECK(_read(',', itr, end));
}
OE_CHECK(_read(']', itr, end));
result = OE_OK;
done:
return result;
}
/**
* Type: tcbLevel in TCB Info (V2)
* Schema:
* {
* "tcb" : object of type tcb (Note: QE Identity info has the same object,
* but with different set of values). "tcbDate" : oe_datetime_t when TCB level
* was certified not to be vulnerable. ISO 8601 standard(YYYY-MM-DDThh:mm:ssZ).
* "tcbStatus" : one of "UpToDate" or "OutOfDate" or "Revoked" or
* "ConfigurationNeeded" or "OutOfDateConfigurationNeeded" or
* "SWHardeningNeeded" or "ConfigurationAndSWHardeningNeeded"
* "advisoryIDs" :
* array of strings describing vulnerabilities that this TCB level is vulnerable
* to. Example: ["INTEL-SA-00079", "INTEL-SA-00076"]
* }
*/
static oe_result_t _read_tcb_info_tcb_level_v2(
const uint8_t* info_json,
const uint8_t** itr,
const uint8_t* end,
oe_tcb_info_tcb_level_t* platform_tcb_level,
oe_tcb_info_tcb_level_t* tcb_level)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* status = NULL;
size_t status_length = 0;
const uint8_t* date_str = NULL;
size_t date_size = 0;
OE_CHECK(_read_property_name_and_colon("tcbLevels", itr, end));
OE_CHECK(_read('[', itr, end));
// Read each tcbLevel
while (*itr < end)
{
OE_CHECK(_read('{', itr, end));
OE_TRACE_VERBOSE("Reading tcb");
OE_CHECK(_read_property_name_and_colon("tcb", itr, end));
OE_CHECK(_read_tcb_info_tcb_level(itr, end, tcb_level));
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading tcbDate");
OE_CHECK(_read_property_name_and_colon("tcbDate", itr, end));
OE_CHECK(_read_string(itr, end, &date_str, &date_size));
if (oe_datetime_from_string(
(const char*)date_str, date_size, &tcb_level->tcb_date) !=
OE_OK)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading tcbStatus");
OE_CHECK(_read_property_name_and_colon("tcbStatus", itr, end));
OE_CHECK(_read_string(itr, end, &status, &status_length));
OE_CHECK(_trace_json_string(status, status_length));
// Optional advisoryIDs field
if (OE_JSON_INFO_PARSE_ERROR != _read(',', itr, end))
{
OE_TRACE_VERBOSE("Reading advisoryIDs");
OE_CHECK(_read_property_name_and_colon("advisoryIDs", itr, end));
OE_CHECK(_read('[', itr, end));
tcb_level->advisory_ids_offset = (size_t)(*itr - info_json);
size_t size = 0;
while (*itr < end && **itr != ']')
{
(*itr)++;
size++;
}
OE_CHECK(_read(']', itr, end));
tcb_level->advisory_ids_size = size;
}
OE_CHECK(_read('}', itr, end));
tcb_level->status = _parse_tcb_status(status, status_length);
if (tcb_level->status.AsUINT32 != OE_TCB_LEVEL_STATUS_UNKNOWN)
{
_determine_platform_tcb_info_tcb_level(
platform_tcb_level, tcb_level);
}
// Optimization
if (platform_tcb_level->status.AsUINT32 != OE_TCB_LEVEL_STATUS_UNKNOWN)
{
// tcb_date represents the date and time when the TCB level was
// certified.
platform_tcb_level->tcb_date = tcb_level->tcb_date;
// Found matching TCB level, go to the end of the array.
_move_to_end_of_tcb_levels(itr, end);
}
// Read end of array or comma separator.
if (*itr < end && **itr == ']')
break;
OE_CHECK(_read(',', itr, end));
}
OE_CHECK(_read(']', itr, end));
result = OE_OK;
done:
return result;
}
/**
* type = tcbInfo
* V1 Schema:
* {
* "version" : integer,
* "issueDate" : string,
* "nextUpdate" : string,
* "fmspc" : "hex string (12 nibbles)"
* "pceId" : "hex string (4 nibbles)"
* "tcbLevels" : [ objects of type oe_tcb_info_tcb_level_t ]
* }
*
* V2 Schema:
* {
* "version" : integer,
* "issueDate" : string,
* "nextUpdate" : string,
* "fmspc" : "hex string (12 nibbles)"
* "pceId" : "hex string (4 nibbles)"
* "tcbType" : integer
* "tcbEvaluationDataNumber" : integer
* "tcbLevels" : [ objects of type oe_tcb_info_tcb_level_t ]
* }
*/
static oe_result_t _read_tcb_info(
const uint8_t* tcb_info_json,
const uint8_t** itr,
const uint8_t* end,
oe_tcb_info_tcb_level_t* platform_tcb_level,
oe_parsed_tcb_info_t* parsed_info)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
uint64_t value = 0;
const uint8_t* date_str = NULL;
size_t date_size = 0;
parsed_info->tcb_info_start = *itr;
OE_CHECK(_read('{', itr, end));
OE_TRACE_VERBOSE("Reading version");
OE_CHECK(_read_property_name_and_colon("version", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
parsed_info->version = (uint32_t)value;
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading issueDate");
OE_CHECK(_read_property_name_and_colon("issueDate", itr, end));
OE_CHECK(_read_string(itr, end, &date_str, &date_size));
if (oe_datetime_from_string(
(const char*)date_str, date_size, &parsed_info->issue_date) !=
OE_OK)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading nextUpdate");
OE_CHECK(_read_property_name_and_colon("nextUpdate", itr, end));
OE_CHECK(_read_string(itr, end, &date_str, &date_size));
if (oe_datetime_from_string(
(const char*)date_str, date_size, &parsed_info->next_update) !=
OE_OK)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading fmspc");
OE_CHECK(_read_property_name_and_colon("fmspc", itr, end));
OE_CHECK(_read_hex_string(
itr, end, parsed_info->fmspc, sizeof(parsed_info->fmspc)));
OE_CHECK(_read(',', itr, end));
{
const uint8_t* old_itr = *itr;
// read optional "pceId", if it does not exist, restore the read
// pointers
OE_TRACE_VERBOSE("Attempt reading optional pceId field...");
parsed_info->pceid[0] = 0;
parsed_info->pceid[1] = 0;
result = _read_property_name_and_colon("pceId", itr, end);
if (result == OE_OK)
{
OE_CHECK(_read_hex_string(
itr, end, parsed_info->pceid, sizeof(parsed_info->pceid)));
OE_CHECK(_read(',', itr, end));
}
else if (result == OE_JSON_INFO_PARSE_ERROR)
{
*itr = old_itr;
}
}
if (parsed_info->version == 2)
{
OE_TRACE_VERBOSE("Reading tcbType");
OE_CHECK(_read_property_name_and_colon("tcbType", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
// HW representation of CPUSVN for a given FMSPC is not architecturally
// defined to provide designers more flexibility. SW needs "tcbType" to
// determine how to decompose the CPUSVN. Each FMSPC has its own
// tcbType. For now, there is only one tcbType(0) has been defined.
if (value != 0)
OE_RAISE_MSG(
OE_JSON_INFO_PARSE_ERROR, "Unsupported tcbType(%d).", value);
parsed_info->tcb_type = (uint32_t)value;
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading tcbEvaluationDataNumber");
OE_CHECK(
_read_property_name_and_colon("tcbEvaluationDataNumber", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
parsed_info->tcb_evaluation_data_number = (uint32_t)value;
OE_CHECK(_read(',', itr, end));
}
OE_TRACE_VERBOSE("Reading tcbLevels");
if (parsed_info->version == 1)
OE_CHECK(_read_tcb_info_tcb_level_v1(
itr, end, platform_tcb_level, &parsed_info->tcb_level));
else if (parsed_info->version == 2)
OE_CHECK(_read_tcb_info_tcb_level_v2(
tcb_info_json,
itr,
end,
platform_tcb_level,
&parsed_info->tcb_level));
else
{
OE_RAISE_MSG(
OE_JSON_INFO_PARSE_ERROR,
"Unsupported TCB level info version %d",
parsed_info->version);
}
// itr is expected to point to the '}' that denotes the end of the tcb
// object. The signature is generated over the entire object including the
// '}'.
parsed_info->tcb_info_size =
(size_t)(*itr - parsed_info->tcb_info_start + 1);
OE_CHECK(_read('}', itr, end));
result = OE_OK;
done:
return result;
}
/**
* Schema:
* {
* "tcbInfo" : object of type tcbInfo,
* "signature" : "hex string"
* }
*/
oe_result_t oe_parse_tcb_info_json(
const uint8_t* tcb_info_json,
size_t tcb_info_json_size,
oe_tcb_info_tcb_level_t* platform_tcb_level,
oe_parsed_tcb_info_t* parsed_info)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
const uint8_t* itr = tcb_info_json;
const uint8_t* end = tcb_info_json + tcb_info_json_size;
if (tcb_info_json == NULL || tcb_info_json_size == 0 ||
platform_tcb_level == NULL || parsed_info == NULL)
OE_RAISE(OE_INVALID_PARAMETER);
// Pointer wrapping.
if (end <= itr)
OE_RAISE(OE_INVALID_PARAMETER);
// Initialize status
platform_tcb_level->status.AsUINT32 = OE_TCB_LEVEL_STATUS_UNKNOWN;
itr = _skip_ws(itr, end);
OE_CHECK(_read('{', &itr, end));
OE_TRACE_VERBOSE("Reading tcbInfo");
OE_CHECK(_read_property_name_and_colon("tcbInfo", &itr, end));
OE_CHECK(_read_tcb_info(
tcb_info_json, &itr, end, platform_tcb_level, parsed_info));
OE_CHECK(_read(',', &itr, end));
OE_TRACE_VERBOSE("Reading signature");
OE_CHECK(_read_property_name_and_colon("signature", &itr, end));
OE_CHECK(_read_hex_string(
&itr, end, parsed_info->signature, sizeof(parsed_info->signature)));
OE_CHECK(_read('}', &itr, end));
// Finish TCB info parsing, check TCB status and advisory IDs
if (itr == end)
{
result = OE_OK;
if (platform_tcb_level->status.fields.up_to_date != 1)
{
oe_sgx_tcb_status_t sgx_tcb_status =
oe_tcb_level_status_to_sgx_tcb_status(
platform_tcb_level->status);
for (uint32_t i = 0;
i < OE_COUNTOF(platform_tcb_level->sgx_tcb_comp_svn);
++i)
OE_TRACE_VERBOSE(
"sgx_tcb_comp_svn[%d] = 0x%x",
i,
platform_tcb_level->sgx_tcb_comp_svn[i]);
OE_TRACE_ERROR(
"Invalid platform TCB level: %s "
"(cpu_svn[0] = 0x%x, pce_svn = 0x%x)",
oe_sgx_tcb_status_str(sgx_tcb_status),
platform_tcb_level->sgx_tcb_comp_svn[0],
platform_tcb_level->pce_svn);
result = OE_TCB_LEVEL_INVALID;
}
// Display any advisory IDs as warnings
if (platform_tcb_level->advisory_ids_size > 0)
{
OE_TRACE_WARNING(
"Found %d AdvisoryIDs for this tcb level.",
platform_tcb_level->advisory_ids_size);
}
}
done:
return result;
}
OE_INLINE uint32_t read_uint32(const uint8_t* p)
{
return (uint32_t)(p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
}
OE_INLINE uint64_t read_uint64(const uint8_t* p)
{
uint64_t temp = 0;
uint64_t result = 0;
for (int i = 7; i >= 0; i--)
{
temp = p[i];
result = (result << 8) | temp;
}
return result;
}
/**
* type = qe_identity
* V1 Schema:
* {
* "version" : integer,
* "issueDate" : string,
* "nextUpdate" : string,
* "miscselect" : hex string,
* "miscselectMask" : hex string,
* "attributes" : hex string,
* "attributesMask" : hex string,
* "mrsigner" : hex string,
* "isvprodid" : integer,
* "isvsvn" : integer,
* }
*/
static oe_result_t _read_qe_identity_info_v1(
const uint8_t** itr,
const uint8_t* end,
oe_parsed_qe_identity_info_t* parsed_info)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
uint64_t value = 0;
const uint8_t* date_str = NULL;
size_t date_size = 0;
uint8_t four_bytes_buf[4];
uint8_t sixteen_bytes_buf[16];
parsed_info->info_start = *itr;
OE_CHECK(_read('{', itr, end));
OE_TRACE_VERBOSE("Reading version");
OE_CHECK(_read_property_name_and_colon("version", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
parsed_info->version = (uint32_t)value;
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading issueDate");
OE_CHECK(_read_property_name_and_colon("issueDate", itr, end));
OE_CHECK(_read_string(itr, end, &date_str, &date_size));
if (oe_datetime_from_string(
(const char*)date_str, date_size, &parsed_info->issue_date) !=
OE_OK)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading nextUpdate");
OE_CHECK(_read_property_name_and_colon("nextUpdate", itr, end));
OE_CHECK(_read_string(itr, end, &date_str, &date_size));
if (oe_datetime_from_string(
(const char*)date_str, date_size, &parsed_info->next_update) !=
OE_OK)
OE_RAISE(OE_JSON_INFO_PARSE_ERROR);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading miscselect");
OE_CHECK(_read_property_name_and_colon("miscselect", itr, end));
OE_CHECK(
_read_hex_string(itr, end, four_bytes_buf, sizeof(four_bytes_buf)));
parsed_info->miscselect = read_uint32(four_bytes_buf);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading miscselectMask");
OE_CHECK(_read_property_name_and_colon("miscselectMask", itr, end));
OE_CHECK(
_read_hex_string(itr, end, four_bytes_buf, sizeof(four_bytes_buf)));
parsed_info->miscselect_mask = read_uint32(four_bytes_buf);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading attributes.flags");
OE_CHECK(_read_property_name_and_colon("attributes", itr, end));
OE_CHECK(_read_hex_string(
itr, end, sixteen_bytes_buf, sizeof(sixteen_bytes_buf)));
parsed_info->attributes.flags = read_uint64(sixteen_bytes_buf);
parsed_info->attributes.xfrm = read_uint64(sixteen_bytes_buf + 8);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading attributesMask");
OE_CHECK(_read_property_name_and_colon("attributesMask", itr, end));
OE_CHECK(_read_hex_string(
itr, end, sixteen_bytes_buf, sizeof(sixteen_bytes_buf)));
parsed_info->attributes_flags_mask = read_uint64(sixteen_bytes_buf);
parsed_info->attributes_xfrm_mask = read_uint64(sixteen_bytes_buf + 8);
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading mrsigner");
OE_CHECK(_read_property_name_and_colon("mrsigner", itr, end));
OE_CHECK(_read_hex_string(
itr, end, parsed_info->mrsigner, sizeof(parsed_info->mrsigner)));
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading isvprodid");
OE_CHECK(_read_property_name_and_colon("isvprodid", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
parsed_info->isvprodid = (uint16_t)value;
OE_CHECK(_read(',', itr, end));
OE_TRACE_VERBOSE("Reading isvsvn");
OE_CHECK(_read_property_name_and_colon("isvsvn", itr, end));
OE_CHECK(_read_integer(itr, end, &value));
parsed_info->isvsvn = (uint16_t)value;
// itr is expected to point to the '}' that denotes the end of the qe
// identity object. The signature is generated over the entire object
// including the '}'.
parsed_info->info_size = (size_t)(*itr - parsed_info->info_start + 1);
OE_CHECK(_read('}', itr, end));
OE_TRACE_VERBOSE("Done with last read");
result = OE_OK;
done:
OE_TRACE_VERBOSE(
"Reading _read_qe_identity_info_v1 ended with [%s]\n",
oe_result_str(result));
return result;
}
/**
* Type: tcb in QE Identity Info tcbLevels
* Schema:
* {
* "isvsvn": uint32_t
* }
*/
static oe_result_t _read_qe_tcb(
const uint8_t** itr,
const uint8_t* end,
oe_qe_identity_info_tcb_level_t* tcb_level)
{
oe_result_t result = OE_JSON_INFO_PARSE_ERROR;
uint64_t value = 0;
static const char* _names[] = {"isvsvn"};
OE_STATIC_ASSERT(OE_COUNTOF(_names) == OE_COUNTOF(tcb_level->isvsvn));
OE_CHECK(_read('{', itr, end));
for (size_t i = 0; i < OE_COUNTOF(_names); ++i)
{
OE_TRACE_VERBOSE("Reading %s", _names[i]);
OE_CHECK(_read_property_name_and_colon(_names[i], itr, end));
OE_CHECK(_read_integer(itr, end, &value));
OE_TRACE_VERBOSE("value = %lu", value);
if (i != (OE_COUNTOF(_names) - 1))