-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathabi.go
More file actions
1302 lines (1161 loc) · 52.4 KB
/
abi.go
File metadata and controls
1302 lines (1161 loc) · 52.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
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 2023 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
//
// 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.
// Package abi provides Go representations and conversions for TDX attestation
// data structures
package abi
import (
"encoding/binary"
"errors"
"fmt"
"math/big"
pb "github.com/google/go-tdx-guest/proto/tdx"
"golang.org/x/crypto/cryptobyte"
"golang.org/x/crypto/cryptobyte/asn1"
)
const (
// QuoteMinSize is the minimum specified size of TDX generated quote
QuoteMinSize = 0x3FC
// QuoteVersionV4 is the version4 of the quote currently in support
QuoteVersionV4 = 4
// AttestationKeyType supported value
AttestationKeyType = 2 // (ECDSA-256-with-P-256 curve)
// TeeTDX for Attestation
TeeTDX = 0x00000081
// TeeTcbSvnSize is the size of TEE_TCB_SVN field in TdQuoteBody
TeeTcbSvnSize = 0x10
// MrSeamSize is the size of MR_SEAM field in TdQuoteBody
MrSeamSize = 0x30
// TdAttributesSize is the size of TD_ATTRIBUTES field in TdQuoteBody
TdAttributesSize = 0x08
// XfamSize is the size of XFAM field in TdQuoteBody
XfamSize = 0x08
// MrTdSize is the size of MR_TD field in TdQuoteBody
MrTdSize = 0x30
// MrConfigIDSize is the size of MR_CONFIG_ID field in TdQuoteBody
MrConfigIDSize = 0x30
// MrOwnerSize is the size of MR_OWNER field in TdQuoteBody
MrOwnerSize = 0x30
// MrOwnerConfigSize is the size of MR_OWNER_CONFIG field in TdQuoteBody
MrOwnerConfigSize = 0x30
// RtmrSize is the size of Runtime extendable measurement register
RtmrSize = 0x30
// ReportDataSize is the size of ReportData field in TdQuoteBody
ReportDataSize = 0x40
// QeVendorIDSize is the size of QeVendorID field in Header
QeVendorIDSize = 0x10
userDataSize = 0x14
qeReportCertificationDataType = 0x6
pckReportCertificationDataType = 0x5
qeReportSize = 0x180
headerSize = 0x30
tdQuoteBodySize = 0x248
qeSvnSize = 0x2
pceSvnSize = 0x2
mrSignerSeamSize = 0x30
seamAttributesSize = 0x08
cpuSvnSize = 0x10
reserved1Size = 0x1C
attributesSize = 0x10
mrEnclaveSize = 0x20
reserved2Size = 0x20
mrSignerSize = 0x20
reserved3Size = 0x60
reserved4Size = 0x3C
signatureSize = 0x40
attestationKeySize = 0x40
pckCertificateChainKnownSize = 0x06
qeAuthDataKnownSize = 0x02
certificationDataKnownSize = 0x06
quoteV4AuthDataKnownSize = 0x80
quoteHeaderStart = 0x00
quoteHeaderEnd = 0x30
quoteBodyStart = 0x30
quoteBodyEnd = 0x278
quoteSignedDataSizeStart = 0x278
quoteSignedDataSizeEnd = 0x27C
quoteSignedDataStart = quoteSignedDataSizeEnd
headerVersionStart = 0x00
headerVersionEnd = 0x02
headerAttestationKeyTypeStart = headerVersionEnd
headerAttestationKeyTypeEnd = 0x04
headerTeeTypeStart = headerAttestationKeyTypeEnd
headerTeeTypeEnd = 0x08
headerPceSvnStart = headerTeeTypeEnd
headerPceSvnEnd = 0xA
headerQeSvnStart = headerPceSvnEnd
headerQeSvnEnd = 0xC
headerQeVendorIDStart = headerQeSvnEnd
headerQeVendorIDEnd = 0x1C
headerUserDataStart = headerQeVendorIDEnd
headerUserDataEnd = 0x30
intelQuoteV4Version = 4
tdTeeTcbSvnStart = 0x00
tdTeeTcbSvnEnd = 0x10
tdMrSeamStart = tdTeeTcbSvnEnd
tdMrSeamEnd = 0x40
tdMrSignerSeamStart = tdMrSeamEnd
tdMrSignerSeamEnd = 0x70
tdSeamAttributesStart = tdMrSignerSeamEnd
tdSeamAttributesEnd = 0x78
tdAttributesStart = tdSeamAttributesEnd
tdAttributesEnd = 0x80
tdXfamStart = tdAttributesEnd
tdXfamEnd = 0x88
tdMrTdStart = tdXfamEnd
tdMrTdEnd = 0xB8
tdMrConfigIDStart = tdMrTdEnd
tdMrConfigIDEnd = 0xE8
tdMrOwnerStart = tdMrConfigIDEnd
tdMrOwnerEnd = 0x118
tdMrOwnerConfigStart = tdMrOwnerEnd
tdMrOwnerConfigEnd = 0x148
tdRtmrsStart = tdMrOwnerConfigEnd
tdRtmrsEnd = 0x208
tdReportDataStart = tdRtmrsEnd
tdReportDataEnd = 0x248
signedDataSignatureStart = 0x00
signedDataSignatureEnd = 0x40
signedDataAttestationKeyStart = signedDataSignatureEnd
signedDataAttestationKeyEnd = 0x80
signedDataCertificationDataStart = signedDataAttestationKeyEnd
certificateDataTypeStart = 0x00
certificateDataTypeEnd = 0x02
certificateSizeStart = certificateDataTypeEnd
certificateSizeEnd = 0x06
certificateDataStart = certificateSizeEnd
enclaveReportStart = 0x00
enclaveReportEnd = 0x180
qeReportCertificationDataSignatureStart = enclaveReportEnd
qeReportCertificationDataSignatureEnd = 0x1C0
qeReportCertificationDataAuthDataStart = qeReportCertificationDataSignatureEnd
qeCPUSvnStart = 0x00
qeCPUSvnEnd = 0x10
qeMiscSelectStart = qeCPUSvnEnd
qeMiscSelectEnd = 0x14
qeReserved1Start = qeMiscSelectEnd
qeReserved1End = 0x30
qeAttributesStart = qeReserved1End
qeAttributesEnd = 0x40
qeMrEnclaveStart = qeAttributesEnd
qeMrEnclaveEnd = 0x60
qeReserved2Start = qeMrEnclaveEnd
qeReserved2End = 0x80
qeMrSignerStart = qeReserved2End
qeMrSignerEnd = 0xA0
qeReserved3Start = qeMrSignerEnd
qeReserved3End = 0x100
qeIsvProdIDStart = qeReserved3End
qeIsvProdIDEnd = 0x102
qeIsvSvnStart = qeIsvProdIDEnd
qeIsvSvnEnd = 0x104
qeReserved4Start = qeIsvSvnEnd
qeReserved4End = 0x140
qeReportDataStart = qeReserved4End
qeReportDataEnd = 0x180
authDataParsedDataSizeStart = 0x00
authDataParsedDataSizeEnd = 0x02
authDataStart = authDataParsedDataSizeEnd
pckCertChainCertificationDataTypeStart = 0x00
pckCertChainCertificationDataTypeEnd = 0x02
pckCertChainSizeStart = pckCertChainCertificationDataTypeEnd
pckCertChainSizeEnd = 0x06
pckCertChainDataStart = pckCertChainSizeEnd
rtmrsCount = 4
// V5 constants
intelQuoteV5Version = 5
quoteHeaderSizeV5 = 48
quoteBodyTypeSizeV5 = 2
quoteBodySizeFieldLengthV5 = 4
teeTcbSvnSizeV5 = 16
mrSeamSizeV5 = 48
mrSignerSeamSizeV5 = 48
seamAttributesSizeV5 = 8
tdAttributesSizeV5 = 8
xFamSizeV5 = 8
mrTdSizeV5 = 48
mrConfigIDSizeV5 = 48
mrOwnerSizeV5 = 48
mrOwnerConfigSizeV5 = 48
rtmrsTotalSizeV5 = 192
rtmrsCountV5 = 4
rtmrsSizeV5 = 48
reportDataSizeV5 = 64
teeTcbSvn2SizeV5 = 16
mrServiceTdSizeV5 = 48
tdQuoteBodySizeV5TDX10 = 584
tdQuoteBodySizeV5TDX15 = 648
tdxVersion10BodyType = 2
tdxVersion15BodyType = 3
signedDataSizeFieldLengthV5 = 4
// QuoteMinSizeV5 is the minimum specified size of TDX generated quote in case of version 5
QuoteMinSizeV5 = 1026
// TeeTcbSvn2SizeV5 is the size of TeeTcbSvn2 field in TDQuoteBodyV5
TeeTcbSvn2SizeV5 = 16
// MrServiceTdSizeV5 is the size of MrServiceTd field in TDQuoteBodyV5
MrServiceTdSizeV5 = 48
// QuoteVersionV5 is the version of the quote in case of version 5 of quote format
QuoteVersionV5 = 5
)
var (
// ErrQuoteNil error returned when Quote is nil
ErrQuoteNil = errors.New("quote is nil")
// ErrQuoteV4Nil error returned when QuoteV4 is nil
ErrQuoteV4Nil = errors.New("QuoteV4 is nil")
// ErrQuoteV4AuthDataNil error returned when QuoteV4 Auth Data is nil
ErrQuoteV4AuthDataNil = errors.New("QuoteV4 authData is nil")
// ErrCertificationDataNil error returned when Certification Data is nil
ErrCertificationDataNil = errors.New("certification data is nil")
// ErrQeReportCertificationDataNil error returned when QE report certification data is nil
ErrQeReportCertificationDataNil = errors.New("QE Report certification data is nil")
// ErrQeAuthDataNil error returned when QE Auth Data is nil
ErrQeAuthDataNil = errors.New("QE AuthData is nil")
// ErrQeReportNil error returned when QE Report is nil
ErrQeReportNil = errors.New("QE Report is nil")
// ErrPckCertChainNil error returned when PCK Certificate Chain is nil
ErrPckCertChainNil = errors.New("PCK certificate chain is nil")
// ErrTDQuoteBodyNil error returned when TD quote body is nil
ErrTDQuoteBodyNil = errors.New("TD quote body is nil")
// ErrTeeType error returned when TEE type is not TDX
ErrTeeType = errors.New("TEE type is not TDX")
// ErrAttestationKeyType error returned when attestation key is not of expected type
ErrAttestationKeyType = errors.New("attestation key type not supported")
// ErrHeaderNil error returned when header is nil
ErrHeaderNil = errors.New("header is nil")
// ErrQuoteV5Nil error returned when QuoteV5 is nil
ErrQuoteV5Nil = errors.New("QuoteV5 is nil")
// ErrTDQuoteBodyDescriptorNil error returned when TD quote body descriptor is nil
ErrTDQuoteBodyDescriptorNil = errors.New("TD quote body descriptor is nil")
// ErrRawQuoteEmpty error returned when raw quote is empty
ErrRawQuoteEmpty = errors.New("raw quote is empty")
)
func clone(b []byte) []byte {
result := make([]byte, len(b))
copy(result, b)
return result
}
// determineQuoteFormat returns the quote format version from the header.
func determineQuoteFormat(b []uint8) (uint32, error) {
if len(b) < headerVersionEnd {
return 0, fmt.Errorf("unable to determine quote format since bytes length is less than %d bytes", headerVersionEnd)
}
data := clone(b)
header := &pb.Header{}
header.Version = uint32(binary.LittleEndian.Uint16(data[headerVersionStart:headerVersionEnd]))
return header.Version, nil
}
// QuoteToProto creates a Quote from the Intel's attestation quote byte array in Intel's ABI format.
// Supported quote formats - QuoteV4.
func QuoteToProto(b []uint8) (any, error) {
if len(b) == 0 {
return nil, ErrRawQuoteEmpty
}
quoteFormat, err := determineQuoteFormat(b)
if err != nil {
return nil, err
}
switch quoteFormat {
case intelQuoteV4Version:
return quoteToProtoV4(b)
case intelQuoteV5Version:
return quoteToProtoV5(b)
default:
return nil, fmt.Errorf("quote format not supported")
}
}
// quoteToProtoV4 creates a pb.QuoteV4 from the Intel's attestation quote byte array in Intel's ABI format.
func quoteToProtoV4(b []uint8) (*pb.QuoteV4, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
if len(data) < QuoteMinSize {
return nil, fmt.Errorf("raw quote size is 0x%x, a TDX quote should have size a minimum size of 0x%x", len(data), QuoteMinSize)
}
quote := &pb.QuoteV4{}
header, err := headerToProto(data[quoteHeaderStart:quoteHeaderEnd])
if err != nil {
return nil, err
}
tdQuoteBody, err := tdQuoteBodyToProto(data[quoteBodyStart:quoteBodyEnd])
if err != nil {
return nil, err
}
quote.SignedDataSize = binary.LittleEndian.Uint32(data[quoteSignedDataSizeStart:quoteSignedDataSizeEnd])
additionalData := data[quoteSignedDataStart:]
if uint32(len(additionalData)) < quote.GetSignedDataSize() {
return nil, fmt.Errorf("size of signed data is 0x%x. Expected minimum size of 0x%x", len(additionalData), quote.GetSignedDataSize())
}
quoteSignedDataEnd := quoteSignedDataStart + quote.GetSignedDataSize()
rawSignedData := data[quoteSignedDataStart:quoteSignedDataEnd]
extraBytes := data[quoteSignedDataEnd:]
signedData, err := signedDataToProto(rawSignedData)
if err != nil {
return nil, err
}
quote.Header = header
quote.TdQuoteBody = tdQuoteBody
quote.SignedData = signedData
if len(extraBytes) > 0 {
quote.ExtraBytes = extraBytes
}
if err := CheckQuote(quote); err != nil {
return nil, fmt.Errorf("parsing QuoteV4 failed: %v", err)
}
return quote, nil
}
func quoteToProtoV5(b []uint8) (*pb.QuoteV5, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
if len(data) < QuoteMinSizeV5 {
return nil, fmt.Errorf("raw quote size is %d bytes, a V5 TDX quote should have size a minimum size of %d bytes", len(data), QuoteMinSizeV5)
}
quote := &pb.QuoteV5{}
// maintains current parsed length of quote
var offset int32
header, err := headerToProto(data[offset:quoteHeaderSizeV5])
if err != nil {
return nil, err
}
offset = quoteHeaderSizeV5
tdQuoteBodyDescriptor, bodyDescriptorSizeV5, err := tdQuoteBodyDescriptorToProto(data[offset:])
if err != nil {
return nil, err
}
offset += bodyDescriptorSizeV5
signedDataSizeV5 := int32(binary.LittleEndian.Uint32(data[offset : offset+signedDataSizeFieldLengthV5]))
offset += signedDataSizeFieldLengthV5
additionalData := data[offset:]
if int32(len(additionalData)) < signedDataSizeV5 {
return nil, fmt.Errorf("size of signed data is 0x%x. Expected minimum size of 0x%x", len(additionalData), signedDataSizeV5)
}
signedDataV5, err := signedDataToProto(data[offset : offset+signedDataSizeV5])
if err != nil {
return nil, err
}
offset += signedDataSizeV5
extraBytes := data[offset:]
quote.Header = header
quote.TdQuoteBodyDescriptor = tdQuoteBodyDescriptor
quote.SignedDataSize = signedDataSizeV5
quote.SignedData = signedDataV5
if len(extraBytes) > 0 {
quote.ExtraBytes = extraBytes
}
if err := CheckQuote(quote); err != nil {
return nil, fmt.Errorf("parsing QuoteV5 failed: %v", err)
}
return quote, nil
}
func headerToProto(b []uint8) (*pb.Header, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
header := &pb.Header{}
quoteVersion := uint32(binary.LittleEndian.Uint16(data[headerVersionStart:headerVersionEnd]))
header.Version = quoteVersion
if quoteVersion != QuoteVersionV4 && quoteVersion != QuoteVersionV5 {
return nil, fmt.Errorf("quote version %d not supported", quoteVersion)
}
header.AttestationKeyType = uint32(binary.LittleEndian.Uint16(data[headerAttestationKeyTypeStart:headerAttestationKeyTypeEnd]))
header.TeeType = binary.LittleEndian.Uint32(data[headerTeeTypeStart:headerTeeTypeEnd])
header.PceSvn = data[headerPceSvnStart:headerPceSvnEnd]
header.QeSvn = data[headerQeSvnStart:headerQeSvnEnd]
header.QeVendorId = data[headerQeVendorIDStart:headerQeVendorIDEnd]
header.UserData = data[headerUserDataStart:headerUserDataEnd]
if err := checkHeader(header, quoteVersion); err != nil {
return nil, fmt.Errorf("parsing header failed: %v", err)
}
return header, nil
}
func tdQuoteBodyToProto(b []uint8) (*pb.TDQuoteBody, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
report := &pb.TDQuoteBody{}
report.TeeTcbSvn = data[tdTeeTcbSvnStart:tdTeeTcbSvnEnd]
report.MrSeam = data[tdMrSeamStart:tdMrSeamEnd]
report.MrSignerSeam = data[tdMrSignerSeamStart:tdMrSignerSeamEnd]
report.SeamAttributes = data[tdSeamAttributesStart:tdSeamAttributesEnd]
report.TdAttributes = data[tdAttributesStart:tdAttributesEnd]
report.Xfam = data[tdXfamStart:tdXfamEnd]
report.MrTd = data[tdMrTdStart:tdMrTdEnd]
report.MrConfigId = data[tdMrConfigIDStart:tdMrConfigIDEnd]
report.MrOwner = data[tdMrOwnerStart:tdMrOwnerEnd]
report.MrOwnerConfig = data[tdMrOwnerConfigStart:tdMrOwnerConfigEnd]
report.ReportData = data[tdReportDataStart:tdReportDataEnd]
rtmrsStart := tdRtmrsStart
for i := 0; i < rtmrsCount; i++ {
rtmrsEnd := rtmrsStart + RtmrSize
arr := data[rtmrsStart:rtmrsEnd]
report.Rtmrs = append(report.Rtmrs, arr)
rtmrsStart += RtmrSize
}
if err := checkTDQuoteBody(report); err != nil {
return nil, fmt.Errorf("parsing TD Quote Body failed: %v", err)
}
return report, nil
}
func tdQuoteBodyDescriptorToProto(b []uint8) (*pb.TDQuoteBodyDescriptor, int32, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
var offset int32
tdQuoteBodyDescriptor := &pb.TDQuoteBodyDescriptor{}
tdQuoteBodyV5 := &pb.TDQuoteBodyV5{}
tdQuoteBodyDescriptor.TdQuoteBodyType = int32(binary.LittleEndian.Uint16(data[offset : offset+quoteBodyTypeSizeV5]))
offset += quoteBodyTypeSizeV5
tdQuoteBodyDescriptor.TdQuoteBodySize = int32(binary.LittleEndian.Uint32(data[offset : offset+quoteBodySizeFieldLengthV5]))
if tdQuoteBodyDescriptor.TdQuoteBodyType == tdxVersion10BodyType && tdQuoteBodyDescriptor.TdQuoteBodySize != tdQuoteBodySizeV5TDX10 {
return nil, 0, fmt.Errorf("TD quote body size is %d, a V5 TDX1.0 quote should have size %d", tdQuoteBodyDescriptor.TdQuoteBodySize, tdQuoteBodySizeV5TDX10)
}
if tdQuoteBodyDescriptor.TdQuoteBodyType == tdxVersion15BodyType && tdQuoteBodyDescriptor.TdQuoteBodySize != tdQuoteBodySizeV5TDX15 {
return nil, 0, fmt.Errorf("TD quote body size is %d, a V5 TDX1.5 quote should have size %d", tdQuoteBodyDescriptor.TdQuoteBodySize, tdQuoteBodySizeV5TDX15)
}
offset += quoteBodySizeFieldLengthV5
tdQuoteBodyV5.TeeTcbSvn = data[offset : offset+teeTcbSvnSizeV5]
offset += teeTcbSvnSizeV5
tdQuoteBodyV5.MrSeam = data[offset : offset+mrSeamSizeV5]
offset += mrSeamSizeV5
tdQuoteBodyV5.MrSignerSeam = data[offset : offset+mrSignerSeamSizeV5]
offset += mrSignerSeamSizeV5
tdQuoteBodyV5.SeamAttributes = data[offset : offset+seamAttributesSizeV5]
offset += seamAttributesSizeV5
tdQuoteBodyV5.TdAttributes = data[offset : offset+tdAttributesSizeV5]
offset += tdAttributesSizeV5
tdQuoteBodyV5.Xfam = data[offset : offset+xFamSizeV5]
offset += xFamSizeV5
tdQuoteBodyV5.MrTd = data[offset : offset+mrTdSizeV5]
offset += mrTdSizeV5
tdQuoteBodyV5.MrConfigId = data[offset : offset+mrConfigIDSizeV5]
offset += mrConfigIDSizeV5
tdQuoteBodyV5.MrOwner = data[offset : offset+mrOwnerSizeV5]
offset += mrOwnerSizeV5
tdQuoteBodyV5.MrOwnerConfig = data[offset : offset+mrOwnerConfigSizeV5]
offset += mrOwnerConfigSizeV5
for i := 0; i < rtmrsCountV5; i++ {
arr := data[offset : offset+rtmrsSizeV5]
offset += rtmrsSizeV5
tdQuoteBodyV5.Rtmrs = append(tdQuoteBodyV5.Rtmrs, arr)
}
tdQuoteBodyV5.ReportData = data[offset : offset+reportDataSizeV5]
offset += reportDataSizeV5
switch tdQuoteBodyDescriptor.TdQuoteBodyType {
// 3 is the body type when TDX1.5 is present, and 2 when TDX1.0.
case tdxVersion15BodyType:
tdQuoteBodyV5.TeeTcbSvn2 = data[offset : offset+teeTcbSvn2SizeV5]
offset += teeTcbSvn2SizeV5
tdQuoteBodyV5.MrServiceTd = data[offset : offset+mrServiceTdSizeV5]
offset += mrServiceTdSizeV5
default:
tdQuoteBodyV5.TeeTcbSvn2 = nil
tdQuoteBodyV5.MrServiceTd = nil
}
tdQuoteBodyDescriptor.TdQuoteBodyV5 = tdQuoteBodyV5
err := checkTDQuoteBodyDescriptor(tdQuoteBodyDescriptor)
if err != nil {
return nil, 0, fmt.Errorf("parsing TD Quote Body Descriptor failed: %v", err)
}
return tdQuoteBodyDescriptor, offset, nil
}
func signedDataToProto(b []uint8) (*pb.Ecdsa256BitQuoteV4AuthData, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
signedData := &pb.Ecdsa256BitQuoteV4AuthData{}
signedData.Signature = data[signedDataSignatureStart:signedDataSignatureEnd]
signedData.EcdsaAttestationKey = data[signedDataAttestationKeyStart:signedDataAttestationKeyEnd]
certificationData, err := certificationDataToProto(data[signedDataCertificationDataStart:])
if err != nil {
return nil, err
}
signedData.CertificationData = certificationData
if err := checkEcdsa256BitQuoteV4AuthData(signedData); err != nil {
return nil, fmt.Errorf("parsing QuoteV4 AuthData failed: %v", err)
}
return signedData, nil
}
func certificationDataToProto(b []uint8) (*pb.CertificationData, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
certification := &pb.CertificationData{}
certification.CertificateDataType = uint32(binary.LittleEndian.Uint16(data[certificateDataTypeStart:certificateDataTypeEnd]))
certification.Size = binary.LittleEndian.Uint32(data[certificateSizeStart:certificateSizeEnd])
rawCertificateData := data[certificateDataStart:]
if uint32(len(rawCertificateData)) != certification.GetSize() {
return nil, fmt.Errorf("size of certificate data is 0x%x. Expected size 0x%x", len(rawCertificateData), certification.GetSize())
}
qeReportCertificationData, err := qeReportCertificationDataToProto(rawCertificateData)
if err != nil {
return nil, err
}
certification.QeReportCertificationData = qeReportCertificationData
if err := checkCertificationData(certification); err != nil {
return nil, fmt.Errorf("parsing certification data failed: %v", err)
}
return certification, nil
}
func qeReportCertificationDataToProto(b []uint8) (*pb.QEReportCertificationData, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
qeReportCertificationData := &pb.QEReportCertificationData{}
enclaveReport, err := enclaveReportToProto(data[enclaveReportStart:enclaveReportEnd])
if err != nil {
return nil, err
}
qeReportCertificationData.QeReport = enclaveReport
qeReportCertificationData.QeReportSignature = data[qeReportCertificationDataSignatureStart:qeReportCertificationDataSignatureEnd]
authData, authDataSize, err := qeAuthDataToProto(data[qeReportCertificationDataAuthDataStart:])
if err != nil {
return nil, err
}
qeReportCertificationData.QeAuthData = authData
pckCertificateStart := qeReportCertificationDataAuthDataStart + authDataSize
pckCertificateChain, err := pckCertificateChainToProto(data[pckCertificateStart:])
if err != nil {
return nil, err
}
qeReportCertificationData.PckCertificateChainData = pckCertificateChain
if err := checkQeReportCertificationData(qeReportCertificationData); err != nil {
return nil, fmt.Errorf("parsing QE Report Certification Data failed: %v", err)
}
return qeReportCertificationData, nil
}
func enclaveReportToProto(b []uint8) (*pb.EnclaveReport, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
enclaveReport := &pb.EnclaveReport{}
enclaveReport.CpuSvn = data[qeCPUSvnStart:qeCPUSvnEnd]
enclaveReport.MiscSelect = binary.LittleEndian.Uint32(data[qeMiscSelectStart:qeMiscSelectEnd])
enclaveReport.Reserved1 = data[qeReserved1Start:qeReserved1End]
enclaveReport.Attributes = data[qeAttributesStart:qeAttributesEnd]
enclaveReport.MrEnclave = data[qeMrEnclaveStart:qeMrEnclaveEnd]
enclaveReport.Reserved2 = data[qeReserved2Start:qeReserved2End]
enclaveReport.MrSigner = data[qeMrSignerStart:qeMrSignerEnd]
enclaveReport.Reserved3 = data[qeReserved3Start:qeReserved3End]
enclaveReport.IsvProdId = uint32(binary.LittleEndian.Uint16(data[qeIsvProdIDStart:qeIsvProdIDEnd]))
enclaveReport.IsvSvn = uint32(binary.LittleEndian.Uint16(data[qeIsvSvnStart:qeIsvSvnEnd]))
enclaveReport.Reserved4 = data[qeReserved4Start:qeReserved4End]
enclaveReport.ReportData = data[qeReportDataStart:qeReportDataEnd]
if err := checkQeReport(enclaveReport); err != nil {
return nil, fmt.Errorf("parsing QE Report failed: %v", err)
}
return enclaveReport, nil
}
func qeAuthDataToProto(b []uint8) (*pb.QeAuthData, uint32, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
authData := &pb.QeAuthData{}
authData.ParsedDataSize = uint32(binary.LittleEndian.Uint16(data[authDataParsedDataSizeStart:authDataParsedDataSizeEnd]))
authDataEnd := authDataParsedDataSizeEnd + authData.GetParsedDataSize()
authData.Data = data[authDataStart:authDataEnd]
if err := checkQeAuthData(authData); err != nil {
return nil, 0, fmt.Errorf("parsing QE AuthData failed: %v", err)
}
return authData, authDataEnd, nil
}
func pckCertificateChainToProto(b []uint8) (*pb.PCKCertificateChainData, error) {
data := clone(b) // Created an independent copy to make the interface less error-prone
pckCertificateChain := &pb.PCKCertificateChainData{}
pckCertificateChain.CertificateDataType = uint32(binary.LittleEndian.Uint16(data[pckCertChainCertificationDataTypeStart:pckCertChainCertificationDataTypeEnd]))
pckCertificateChain.Size = binary.LittleEndian.Uint32(data[pckCertChainSizeStart:pckCertChainSizeEnd])
pckCertificateChain.PckCertChain = data[pckCertChainDataStart:]
if err := checkPCKCertificateChain(pckCertificateChain); err != nil {
return nil, fmt.Errorf("parsing PCK certification chain failed: %v", err)
}
return pckCertificateChain, nil
}
func checkHeader(header *pb.Header, quoteVersion uint32) error {
if header == nil {
return ErrHeaderNil
}
if header.GetVersion() >= (1 << 16) {
return fmt.Errorf("version field size must fit in 2 bytes , got %d", header.GetVersion())
}
if header.GetVersion() != quoteVersion {
return fmt.Errorf("version %d not supported", header.GetVersion())
}
if header.GetAttestationKeyType() >= (1 << 16) {
return fmt.Errorf("attestation key type field size must fit in 2 bytes , got %d", header.GetAttestationKeyType())
}
if header.GetAttestationKeyType() != AttestationKeyType {
return ErrAttestationKeyType
}
if header.GetTeeType() != TeeTDX {
return ErrTeeType
}
if len(header.GetQeSvn()) != qeSvnSize {
return fmt.Errorf("qeSvn size is %d bytes. Expected %d bytes", len(header.GetQeSvn()), qeSvnSize)
}
if len(header.GetPceSvn()) != pceSvnSize {
return fmt.Errorf("pceSvn size is %d bytes. Expected %d bytes", len(header.GetPceSvn()), pceSvnSize)
}
if len(header.GetQeVendorId()) != QeVendorIDSize {
return fmt.Errorf("qeVendorId size is %d bytes. Expected %d bytes", len(header.GetQeVendorId()), QeVendorIDSize)
}
if len(header.GetUserData()) != userDataSize {
return fmt.Errorf("user data size is %d bytes. Expected %d bytes", len(header.GetUserData()), userDataSize)
}
return nil
}
func checkTDQuoteBody(tdQuoteBody *pb.TDQuoteBody) error {
if tdQuoteBody == nil {
return ErrTDQuoteBodyNil
}
if len(tdQuoteBody.GetTeeTcbSvn()) != TeeTcbSvnSize {
return fmt.Errorf("teeTcbSvn size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetTeeTcbSvn()), TeeTcbSvnSize)
}
if len(tdQuoteBody.GetMrSeam()) != MrSeamSize {
return fmt.Errorf("mrSeam size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetMrSeam()), MrSeamSize)
}
if len(tdQuoteBody.GetMrSignerSeam()) != mrSignerSeamSize {
return fmt.Errorf("mrSignerSeam size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetMrSignerSeam()), mrSignerSeamSize)
}
if len(tdQuoteBody.GetSeamAttributes()) != seamAttributesSize {
return fmt.Errorf("seamAttributes size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetSeamAttributes()), seamAttributesSize)
}
if len(tdQuoteBody.GetTdAttributes()) != TdAttributesSize {
return fmt.Errorf("tdAttributes size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetTdAttributes()), TdAttributesSize)
}
if len(tdQuoteBody.GetXfam()) != XfamSize {
return fmt.Errorf("xfam size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetXfam()), XfamSize)
}
if len(tdQuoteBody.GetMrTd()) != MrTdSize {
return fmt.Errorf("mrTd size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetMrTd()), MrTdSize)
}
if len(tdQuoteBody.GetMrConfigId()) != MrConfigIDSize {
return fmt.Errorf("mrConfigId size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetMrConfigId()), MrConfigIDSize)
}
if len(tdQuoteBody.GetMrOwner()) != MrOwnerSize {
return fmt.Errorf("mrOwner size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetMrOwner()), MrOwnerSize)
}
if len(tdQuoteBody.GetMrOwnerConfig()) != MrOwnerConfigSize {
return fmt.Errorf("mrOwnerConfig size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetMrOwnerConfig()), MrOwnerConfigSize)
}
if len(tdQuoteBody.GetRtmrs()) != rtmrsCount {
return fmt.Errorf("rtmrs count is %d. Expected %d", len(tdQuoteBody.GetRtmrs()), rtmrsCount)
}
for i := 0; i < rtmrsCount; i++ {
if len(tdQuoteBody.GetRtmrs()[i]) != RtmrSize {
return fmt.Errorf("rtmr%d size is %d bytes. Expected %d bytes", i, len(tdQuoteBody.GetRtmrs()[i]), RtmrSize)
}
}
if len(tdQuoteBody.GetReportData()) != ReportDataSize {
return fmt.Errorf("report data size is %d bytes. Expected %d bytes", len(tdQuoteBody.GetReportData()), ReportDataSize)
}
return nil
}
// getCommonFieldsFromV5 retrieves common fields from quote V5 and V4 and places in quoteBodyV4
func getCommonFieldsFromV5(tdQuoteBodyV5 *pb.TDQuoteBodyV5) *pb.TDQuoteBody {
tdQuoteBody := &pb.TDQuoteBody{
TeeTcbSvn: tdQuoteBodyV5.GetTeeTcbSvn(),
MrSeam: tdQuoteBodyV5.GetMrSeam(),
MrSignerSeam: tdQuoteBodyV5.GetMrSignerSeam(),
SeamAttributes: tdQuoteBodyV5.GetSeamAttributes(),
TdAttributes: tdQuoteBodyV5.GetTdAttributes(),
Xfam: tdQuoteBodyV5.GetXfam(),
MrTd: tdQuoteBodyV5.GetMrTd(),
MrConfigId: tdQuoteBodyV5.GetMrConfigId(),
MrOwner: tdQuoteBodyV5.GetMrOwner(),
MrOwnerConfig: tdQuoteBodyV5.GetMrOwnerConfig(),
Rtmrs: tdQuoteBodyV5.GetRtmrs(),
ReportData: tdQuoteBodyV5.GetReportData(),
}
return tdQuoteBody
}
func checkTDQuoteBodyV5(tdQuoteBodyV5 *pb.TDQuoteBodyV5, tdxVersion int32) error {
if tdxVersion != tdxVersion10BodyType && tdxVersion != tdxVersion15BodyType {
return fmt.Errorf("tdx version %d is not supported", tdxVersion)
}
// V4 quote body and V5 quote body have same TD quote body fields.
// So, we can copy V5 quote body to V4 quote body and check the V4 quote body.
if err := checkTDQuoteBody(getCommonFieldsFromV5(tdQuoteBodyV5)); err != nil {
return err
}
// TDX1.0 does not have teeTcbSvn2 and mrServiceTd fields.
if tdxVersion == tdxVersion10BodyType {
if len(tdQuoteBodyV5.GetTeeTcbSvn2()) != 0 {
return fmt.Errorf("teeTcbSvn2 is not expected to be set for TDX version 1.0")
}
if len(tdQuoteBodyV5.GetMrServiceTd()) != 0 {
return fmt.Errorf("mrServiceTd are not expected to be set for TDX version 1.0")
}
return nil
}
// TDX1.5 has teeTcbSvn2 and mrServiceTd fields.
if len(tdQuoteBodyV5.GetTeeTcbSvn2()) != TeeTcbSvn2SizeV5 {
return fmt.Errorf("teeTcbSvn2 size is %d bytes. Expected %d bytes", len(tdQuoteBodyV5.GetTeeTcbSvn2()), TeeTcbSvn2SizeV5)
}
if len(tdQuoteBodyV5.GetMrServiceTd()) != MrServiceTdSizeV5 {
return fmt.Errorf("mrServiceTd size is %d bytes. Expected %d bytes", len(tdQuoteBodyV5.GetMrServiceTd()), MrServiceTdSizeV5)
}
return nil
}
func checkPCKCertificateChain(chain *pb.PCKCertificateChainData) error {
if chain == nil {
return ErrPckCertChainNil
}
if chain.GetCertificateDataType() >= (1 << 16) {
return fmt.Errorf("certification data type expected to be of 2 bytes, got %d", chain.GetCertificateDataType())
}
if chain.GetCertificateDataType() != pckReportCertificationDataType {
return fmt.Errorf("PCK certificate chain data type invalid, got %d, expected %d", chain.GetCertificateDataType(), pckReportCertificationDataType)
}
if chain.GetSize() != uint32(len(chain.GetPckCertChain())) {
return fmt.Errorf("PCK certificate chain size is %d. Expected size %d", len(chain.GetPckCertChain()), chain.GetSize())
}
return nil
}
func checkQeReport(report *pb.EnclaveReport) error {
if report == nil {
return ErrQeReportNil
}
if len(report.GetCpuSvn()) != cpuSvnSize {
return fmt.Errorf("cpuSvn size is %d bytes. Expected %d bytes", len(report.GetCpuSvn()), cpuSvnSize)
}
if len(report.GetReserved1()) != reserved1Size {
return fmt.Errorf("reserved1 size is %d bytes. Expected %d bytes", len(report.GetReserved1()), reserved1Size)
}
if len(report.GetAttributes()) != attributesSize {
return fmt.Errorf("attributes size is %d bytes. Expected %d bytes", len(report.GetAttributes()), attributesSize)
}
if len(report.GetMrEnclave()) != mrEnclaveSize {
return fmt.Errorf("mrEnclave size is %d bytes. Expected %d bytes", len(report.GetMrEnclave()), mrEnclaveSize)
}
if len(report.GetReserved2()) != reserved2Size {
return fmt.Errorf("reserved2 size is %d bytes. Expected %d bytes", len(report.GetReserved2()), reserved2Size)
}
if len(report.GetMrSigner()) != mrSignerSize {
return fmt.Errorf("mrSigner size is %d bytes. Expected %d bytes", len(report.GetMrSigner()), mrSignerSize)
}
if len(report.GetReserved3()) != reserved3Size {
return fmt.Errorf("reserved3 size is %d bytes. Expected %d bytes", len(report.GetReserved3()), reserved3Size)
}
if report.GetIsvProdId() >= (1 << 16) {
return fmt.Errorf("isVProdId field size must fit in 2 bytes , got %d", report.GetIsvProdId())
}
if report.GetIsvSvn() >= (1 << 16) {
return fmt.Errorf("isVSvn field size must fit in 2 bytes , got %d", report.GetIsvSvn())
}
if len(report.GetReserved4()) != reserved4Size {
return fmt.Errorf("reserved4 size is %d bytes. Expected %d bytes", len(report.GetReserved4()), reserved4Size)
}
if len(report.GetReportData()) != ReportDataSize {
return fmt.Errorf("report data size is %d bytes. Expected %d bytes", len(report.GetReportData()), ReportDataSize)
}
return nil
}
func checkQeAuthData(authData *pb.QeAuthData) error {
if authData == nil {
return ErrQeAuthDataNil
}
if authData.GetParsedDataSize() >= (1 << 16) {
return fmt.Errorf("parsed data size field size must fit in 2 bytes , got %d", authData.GetParsedDataSize())
}
if authData.GetParsedDataSize() != uint32(len(authData.GetData())) {
return fmt.Errorf("parsed data size is %d bytes. Expected %d bytes", len(authData.GetData()), authData.GetParsedDataSize())
}
return nil
}
func checkQeReportCertificationData(qeReport *pb.QEReportCertificationData) error {
if qeReport == nil {
return ErrQeReportCertificationDataNil
}
if err := checkQeReport(qeReport.GetQeReport()); err != nil {
return fmt.Errorf("QE Report error: %v", err)
}
if len(qeReport.GetQeReportSignature()) != signatureSize {
return fmt.Errorf("signature size is %d bytes. Expected %d bytes", len(qeReport.GetQeReportSignature()), signatureSize)
}
if err := checkQeAuthData(qeReport.GetQeAuthData()); err != nil {
return fmt.Errorf("QE AuthData error: %v", err)
}
if err := checkPCKCertificateChain(qeReport.GetPckCertificateChainData()); err != nil {
return fmt.Errorf("PCK certificate chain error: %v", err)
}
return nil
}
func checkCertificationData(certification *pb.CertificationData) error {
if certification == nil {
return ErrCertificationDataNil
}
if certification.GetCertificateDataType() >= (1 << 16) {
return fmt.Errorf("certification data type field size must fit in 2 bytes , got %d", certification.GetCertificateDataType())
}
if certification.GetCertificateDataType() != qeReportCertificationDataType {
return fmt.Errorf("certification data type invalid, got %d, expected %d", certification.GetCertificateDataType(), qeReportCertificationDataType)
}
if err := checkQeReportCertificationData(certification.GetQeReportCertificationData()); err != nil {
return fmt.Errorf("QE Report certification data error: %v", err)
}
return nil
}
func checkEcdsa256BitQuoteV4AuthData(signedData *pb.Ecdsa256BitQuoteV4AuthData) error {
if signedData == nil {
return ErrQuoteV4AuthDataNil
}
if len(signedData.GetSignature()) != signatureSize {
return fmt.Errorf("signature size is %d bytes. Expected %d bytes", len(signedData.GetSignature()), signatureSize)
}
if len(signedData.GetEcdsaAttestationKey()) != attestationKeySize {
return fmt.Errorf("ecdsa attestation key size is %d bytes. Expected %d bytes", len(signedData.GetEcdsaAttestationKey()), attestationKeySize)
}
if err := checkCertificationData(signedData.GetCertificationData()); err != nil {
return fmt.Errorf("certification data error: %v", err)
}
return nil
}
// CheckQuote validates a quote protobuf by ensuring all parameters meet their required size
func CheckQuote(quote any) error {
switch q := quote.(type) {
case *pb.QuoteV4:
return checkQuoteV4(q)
case *pb.QuoteV5:
return checkQuoteV5(q)
default:
return fmt.Errorf("unsupported quote type: %T", quote)
}
}
func checkQuoteV4(quote *pb.QuoteV4) error {
if quote == nil {
return ErrQuoteV4Nil
}
if err := checkHeader(quote.GetHeader(), QuoteVersionV4); err != nil {
return fmt.Errorf("QuoteV4 Header error: %v", err)
}
if err := checkTDQuoteBody(quote.GetTdQuoteBody()); err != nil {
return fmt.Errorf("QuoteV4 TD Quote Body error: %v", err)
}
if err := checkEcdsa256BitQuoteV4AuthData(quote.GetSignedData()); err != nil {
return fmt.Errorf("QuoteV4 AuthData error: %v", err)
}
return nil
}
func checkQuoteV5(quote *pb.QuoteV5) error {
if quote == nil {
return ErrQuoteV5Nil
}
if err := checkHeader(quote.GetHeader(), QuoteVersionV5); err != nil {
return fmt.Errorf("quoteV5 Header error: %v", err)
}
if err := checkTDQuoteBodyDescriptor(quote.GetTdQuoteBodyDescriptor()); err != nil {
return fmt.Errorf("quoteV5 TD Quote Body Descriptor error: %v", err)
}
if quote.GetSignedDataSize() == 0 {
return fmt.Errorf("quoteV5 Signed Data Size is 0 bytes. Expected non-zero value")
}
if err := checkEcdsa256BitQuoteV4AuthData(quote.GetSignedData()); err != nil {
return fmt.Errorf("quoteV5 AuthData error: %v", err)
}
return nil
}
func checkTDQuoteBodyDescriptor(tdQuoteBodyDescriptor *pb.TDQuoteBodyDescriptor) error {
if tdQuoteBodyDescriptor == nil {
return ErrTDQuoteBodyDescriptorNil
}
if tdQuoteBodyDescriptor.GetTdQuoteBodyType() >= (1 << 16) {
return fmt.Errorf("td quote body type field size must fit in 2 bytes , got %d", tdQuoteBodyDescriptor.GetTdQuoteBodyType())
}
// TD quote body type 2(TDX1.0) and 3(TDX1.5) are supported.
switch tdQuoteBodyDescriptor.GetTdQuoteBodyType() {
case tdxVersion10BodyType:
if tdQuoteBodyDescriptor.GetTdQuoteBodySize() < tdQuoteBodySizeV5TDX10 {
return fmt.Errorf("td quote body size is %d bytes. Expected minimum %d bytes", tdQuoteBodyDescriptor.GetTdQuoteBodySize(), 584)
}
case tdxVersion15BodyType:
if tdQuoteBodyDescriptor.GetTdQuoteBodySize() < tdQuoteBodySizeV5TDX15 {
return fmt.Errorf("td quote body size is %d bytes. Expected minimum %d bytes", tdQuoteBodyDescriptor.GetTdQuoteBodySize(), 648)
}
default:
return fmt.Errorf("unsupported TD quote body type , got %d", tdQuoteBodyDescriptor.GetTdQuoteBodyType())
}
if err := checkTDQuoteBodyV5(tdQuoteBodyDescriptor.GetTdQuoteBodyV5(), tdQuoteBodyDescriptor.GetTdQuoteBodyType()); err != nil {
return fmt.Errorf("td quote body V5 error: %v", err)
}
return nil
}
// EnclaveReportToAbiBytes translates the EnclaveReport back into its little-endian ABI format
func EnclaveReportToAbiBytes(report *pb.EnclaveReport) ([]byte, error) {
if report == nil {
return nil, ErrQeReportNil
}
if err := checkQeReport(report); err != nil {
return nil, fmt.Errorf("QE Report invalid: %v", err)
}
data := make([]byte, qeReportSize)
copy(data[qeCPUSvnStart:qeCPUSvnEnd], report.GetCpuSvn())
binary.LittleEndian.PutUint32(data[qeMiscSelectStart:qeMiscSelectEnd], report.GetMiscSelect())
copy(data[qeReserved1Start:qeReserved1End], report.GetReserved1())
copy(data[qeAttributesStart:qeAttributesEnd], report.GetAttributes())
copy(data[qeMrEnclaveStart:qeMrEnclaveEnd], report.GetMrEnclave())
copy(data[qeReserved2Start:qeReserved2End], report.GetReserved2())
copy(data[qeMrSignerStart:qeMrSignerEnd], report.GetMrSigner())
copy(data[qeReserved3Start:qeReserved3End], report.GetReserved3())
binary.LittleEndian.PutUint16(data[qeIsvProdIDStart:qeIsvProdIDEnd], uint16(report.GetIsvProdId()))
binary.LittleEndian.PutUint16(data[qeIsvSvnStart:qeIsvSvnEnd], uint16(report.GetIsvSvn()))
copy(data[qeReserved4Start:qeReserved4End], report.GetReserved4())
copy(data[qeReportDataStart:qeReportDataEnd], report.GetReportData())
return data, nil
}
// HeaderToAbiBytes translates the Header back into its little-endian ABI format
func HeaderToAbiBytes(header *pb.Header) ([]byte, error) {
if header == nil {
return nil, ErrHeaderNil
}