-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtcgeventdata_efi.go
1328 lines (1148 loc) · 38.6 KB
/
tcgeventdata_efi.go
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 2019 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package tcglog
import (
"bytes"
"crypto"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"math"
"strings"
"unicode/utf8"
efi "github.com/canonical/go-efilib"
"github.com/canonical/go-tpm2"
"golang.org/x/xerrors"
"github.com/canonical/tcglog-parser/internal/ioerr"
)
// GUIDEventData corresponds to event data that is a EFI_GUID.
type GUIDEventData efi.GUID
func (d GUIDEventData) Bytes() []byte {
return d[:]
}
func (d GUIDEventData) String() string {
return efi.GUID(d).String()
}
func (d GUIDEventData) Write(w io.Writer) error {
_, err := w.Write(d[:])
return err
}
// ComputeGUIDEventDataDigest computes the digest for the specified EFI_GUID.
func ComputeGUIDEventDataDigest(alg crypto.Hash, guid efi.GUID) []byte {
h := alg.New()
h.Write(guid[:])
return h.Sum(nil)
}
// SpecIdEvent02 corresponds to the TCG_EfiSpecIdEventStruct type and is the
// event data for a Specification ID Version EV_NO_ACTION event on EFI platforms
// for TPM family 1.2.
type SpecIdEvent02 struct {
PlatformClass uint32
SpecVersionMinor uint8
SpecVersionMajor uint8
SpecErrata uint8
UintnSize uint8
VendorInfo []byte
}
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf
//
// (section 7.4 "EV_NO_ACTION Event Types")
func decodeSpecIdEvent02(data []byte, r io.Reader) (out *SpecIdEvent02, err error) {
d := new(SpecIdEvent02)
if err := binary.Read(r, binary.LittleEndian, &d.PlatformClass); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.SpecVersionMinor); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.SpecVersionMajor); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.SpecErrata); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.UintnSize); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
vendorInfo, err := readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.VendorInfo = vendorInfo
return d, nil
}
func (e *SpecIdEvent02) String() string {
return fmt.Sprintf("EfiSpecIdEvent{ platformClass=%d, specVersionMinor=%d, specVersionMajor=%d, specErrata=%d, uintnSize=%d }",
e.PlatformClass, e.SpecVersionMinor, e.SpecVersionMajor, e.SpecErrata, e.UintnSize)
}
func (e *SpecIdEvent02) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *SpecIdEvent02) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("Spec ID Event02"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.PlatformClass); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.SpecVersionMinor); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.SpecVersionMajor); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.SpecErrata); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.UintnSize); err != nil {
return err
}
return writeLengthPrefixed[uint8](w, e.VendorInfo)
}
// EFISpecIdEventAlgorithmSize represents a digest algorithm and its length and corresponds to the
// TCG_EfiSpecIdEventAlgorithmSize type.
type EFISpecIdEventAlgorithmSize struct {
AlgorithmId tpm2.HashAlgorithmId
DigestSize uint16
}
// SpecIdEvent03 corresponds to the TCG_EfiSpecIdEvent type and is the
// event data for a Specification ID Version EV_NO_ACTION event on EFI platforms
// for TPM family 2.0.
type SpecIdEvent03 struct {
PlatformClass uint32
SpecVersionMinor uint8
SpecVersionMajor uint8
SpecErrata uint8
UintnSize uint8
DigestSizes []EFISpecIdEventAlgorithmSize // The digest algorithms contained within this log
VendorInfo []byte
}
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf
//
// (secion 9.4.5.1 "Specification ID Version Event")
func decodeSpecIdEvent03(data []byte, r io.Reader) (out *SpecIdEvent03, err error) {
d := new(SpecIdEvent03)
if err := binary.Read(r, binary.LittleEndian, &d.PlatformClass); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.SpecVersionMinor); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.SpecVersionMajor); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.SpecErrata); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if err := binary.Read(r, binary.LittleEndian, &d.UintnSize); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
digestSizes, err := readLengthPrefixed[uint32, EFISpecIdEventAlgorithmSize](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.DigestSizes = digestSizes
vendorInfo, err := readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.VendorInfo = vendorInfo
return d, nil
}
func (e *SpecIdEvent03) String() string {
var builder bytes.Buffer
fmt.Fprintf(&builder, "EfiSpecIdEvent{ platformClass=%d, specVersionMinor=%d, specVersionMajor=%d, specErrata=%d, uintnSize=%d, digestSizes=[",
e.PlatformClass, e.SpecVersionMinor, e.SpecVersionMajor, e.SpecErrata, e.UintnSize)
for i, algSize := range e.DigestSizes {
if i > 0 {
builder.WriteString(", ")
}
fmt.Fprintf(&builder, "{ algorithmId=0x%04x, digestSize=%d }",
uint16(algSize.AlgorithmId), algSize.DigestSize)
}
builder.WriteString("] }")
return builder.String()
}
func (e *SpecIdEvent03) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *SpecIdEvent03) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("Spec ID Event03"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.PlatformClass); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.SpecVersionMinor); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.SpecVersionMajor); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.SpecErrata); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.UintnSize); err != nil {
return err
}
if err := writeLengthPrefixed[uint32](w, e.DigestSizes); err != nil {
return err
}
return writeLengthPrefixed[uint8](w, e.VendorInfo)
}
// StartupLocalityEventData is the event data for a StartupLocality EV_NO_ACTION event.
type StartupLocalityEventData struct {
StartupLocality uint8
}
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf
//
// (section 9.4.5.3 "Startup Locality Event")
func decodeStartupLocalityEvent(data []byte, r io.Reader) (*StartupLocalityEventData, error) {
var locality uint8
if err := binary.Read(r, binary.LittleEndian, &locality); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
return &StartupLocalityEventData{StartupLocality: locality}, nil
}
func (e *StartupLocalityEventData) String() string {
return fmt.Sprintf("EfiStartupLocalityEvent{ StartupLocality: %d }", e.StartupLocality)
}
func (e *StartupLocalityEventData) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *StartupLocalityEventData) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("StartupLocality"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, e.StartupLocality)
}
type HCRTMMeasurementFormatType uint8
const (
HCRTMMeasurementFormatDigest HCRTMMeasurementFormatType = 0
HCRTMMeasurementFormatRawData HCRTMMeasurementFormatType = 0x80
)
// HCRTMComponentEventData corresponds to TCG_HCRTMComponentEvent
type HCRTMComponentEventData struct {
ComponentDescription string
MeasurementFormatType HCRTMMeasurementFormatType
ComponentMeasurement []byte // This will be a TPMT_HA structure if MeasurementFormatType == HCRTMMeasurementFormatDigest
}
func decodeHCRTMComponentEvent(data []byte, r io.Reader) (*HCRTMComponentEventData, error) {
d := new(HCRTMComponentEventData)
data, err := readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
// Make sure we have valid printable ASCII
if !isPrintableASCII(data, false) {
return nil, fmt.Errorf("ComponentDescription does not contain printable ASCII that is not NULL terminated")
}
d.ComponentDescription = string(data)
if err := binary.Read(r, binary.LittleEndian, &d.MeasurementFormatType); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
data, err = readLengthPrefixed[uint16, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.ComponentMeasurement = data
return d, nil
}
func (d *HCRTMComponentEventData) String() string {
return fmt.Sprintf("TCG_HCRTMComponentEvent{ComponentDescription: %s, MeasurementFormatType: %x}", d.ComponentDescription, d.MeasurementFormatType)
}
func (e *HCRTMComponentEventData) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (d *HCRTMComponentEventData) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("H-CRTM CompMeas"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
if err := writeLengthPrefixed[uint8, byte](w, []byte(d.ComponentDescription)); err != nil {
return err
}
if _, err := w.Write([]byte{byte(d.MeasurementFormatType)}); err != nil {
return err
}
return writeLengthPrefixed[uint16, byte](w, d.ComponentMeasurement)
}
func decodeEventDataEFIHCRTMEvent(data []byte) (StringEventData, error) {
// The spec says this should just be the string "HCRTM"
if !bytes.Equal(data, HCRTM.Bytes()) {
return "", errors.New("data contains unexpected contents")
}
return StringEventData(data), nil
}
// SP800_155_PlatformIdEventData corresponds to the event data for a SP800-155 Event
// EV_NO_ACTION event
type SP800_155_PlatformIdEventData struct {
VendorId uint32
ReferenceManifestGuid efi.GUID
}
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf
//
// (section 9.4.5.2 "BIOS Integrity Measurement Reference Manifest Event")
//
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf
//
// (section 7.4 "EV_NO_ACTION Event Types")
func decodeBIMReferenceManifestEvent(data []byte, r io.Reader) (*SP800_155_PlatformIdEventData, error) {
var d struct {
VendorId uint32
Guid efi.GUID
}
if err := binary.Read(r, binary.LittleEndian, &d); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
return &SP800_155_PlatformIdEventData{VendorId: d.VendorId, ReferenceManifestGuid: d.Guid}, nil
}
func (e *SP800_155_PlatformIdEventData) String() string {
return fmt.Sprintf("Sp800_155_PlatformId_Event{ VendorId: %d, ReferenceManifestGuid: %s }", e.VendorId, e.ReferenceManifestGuid)
}
func (e *SP800_155_PlatformIdEventData) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *SP800_155_PlatformIdEventData) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("SP800-155 Event"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, e.VendorId); err != nil {
return err
}
_, err := w.Write(e.ReferenceManifestGuid[:])
return err
}
// SP800_155_PlatformIdEventData2 corresponds to the event data for a SP800-155 Event2
// EV_NO_ACTION event
type SP800_155_PlatformIdEventData2 struct {
PlatformManufacturerId uint32
ReferenceManifestGuid efi.GUID
PlatformManufacturer string
PlatformModel string
PlatformVersion string
FirmwareManufacturer string
FirmwareManufacturerId uint32
FirmwareVersion string
}
func decodeBIMReferenceManifestEvent2(data []byte, r io.Reader) (*SP800_155_PlatformIdEventData2, error) {
d := new(SP800_155_PlatformIdEventData2)
if err := binary.Read(r, binary.LittleEndian, &d.PlatformManufacturerId); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
guid, err := efi.ReadGUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.ReferenceManifestGuid = guid
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err := decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode PlatformManufacturer: %w", err)
}
d.PlatformManufacturer = string(str)
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode PlatformModel: %w", err)
}
d.PlatformModel = string(str)
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode PlatformVersion: %w", err)
}
d.PlatformVersion = string(data)
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode FirmwareManufacturer: %w", err)
}
d.FirmwareManufacturer = string(str)
if err := binary.Read(r, binary.LittleEndian, &d.FirmwareManufacturerId); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode FirmwareVersion: %w", err)
}
d.FirmwareVersion = string(str)
return d, nil
}
func (d *SP800_155_PlatformIdEventData2) String() string {
w := new(bytes.Buffer)
fmt.Fprintf(w, "SP800_155_PlatformId_Event2{")
fmt.Fprintf(w, "\tPlatformManufacturerId: %d\n", d.PlatformManufacturerId)
fmt.Fprintf(w, "\tReferenceManifestGUID: %v\n", d.ReferenceManifestGuid)
fmt.Fprintf(w, "\tPlatformManufacturer: %s\n", d.PlatformManufacturer)
fmt.Fprintf(w, "\tPlatformModel: %s\n", d.PlatformModel)
fmt.Fprintf(w, "\tPlatformVersion: %s\n", d.PlatformVersion)
fmt.Fprintf(w, "\tFirmwareManufacturer: %s\n", d.FirmwareManufacturer)
fmt.Fprintf(w, "\tFirmwareManufacturerId: %d\n", d.FirmwareManufacturerId)
fmt.Fprintf(w, "\tFirmwareVersion: %s\n", d.FirmwareVersion)
fmt.Fprintf(w, "}")
return w.String()
}
func (e *SP800_155_PlatformIdEventData2) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (d *SP800_155_PlatformIdEventData2) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("SP800-155 Event2"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, d.PlatformManufacturerId); err != nil {
return err
}
if _, err := w.Write(d.ReferenceManifestGuid[:]); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.PlatformManufacturer).Bytes()); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.PlatformModel).Bytes()); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.PlatformVersion).Bytes()); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.FirmwareManufacturer).Bytes()); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, d.FirmwareManufacturerId); err != nil {
return err
}
return writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.FirmwareVersion).Bytes())
}
type LocatorType uint32
const (
LocatorTypeRaw LocatorType = 0
LocatorTypeURI LocatorType = 1
LocatorTypeDevicePath LocatorType = 2
LocatorTypeVariable LocatorType = 3
)
// SP800_155_PlatformIdEventData3 corresponds to the event data for a SP800-155 Event3
// EV_NO_ACTION event
type SP800_155_PlatformIdEventData3 struct {
PlatformManufacturerId uint32
ReferenceManifestGuid efi.GUID
PlatformManufacturer string
PlatformModel string
PlatformVersion string
FirmwareManufacturer string
FirmwareManufacturerId uint32
FirmwareVersion string
RIMLocatorType LocatorType
RIMLocator []byte
PlatformCertLocatorType LocatorType
PlatformCertLocator []byte
}
func decodeBIMReferenceManifestEvent3(data []byte, r io.Reader) (*SP800_155_PlatformIdEventData3, error) {
d := new(SP800_155_PlatformIdEventData3)
if err := binary.Read(r, binary.LittleEndian, &d.PlatformManufacturerId); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
guid, err := efi.ReadGUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.ReferenceManifestGuid = guid
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err := decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode PlatformManufacturer: %w", err)
}
d.PlatformManufacturer = string(str)
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode PlatformModel: %w", err)
}
d.PlatformModel = string(str)
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode PlatformVersion: %w", err)
}
d.PlatformVersion = string(data)
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode FirmwareManufacturer: %w", err)
}
d.FirmwareManufacturer = string(str)
if err := binary.Read(r, binary.LittleEndian, &d.FirmwareManufacturerId); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
data, err = readLengthPrefixed[uint8, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
str, err = decodeNullTerminatedStringEventData(data)
if err != nil {
return nil, fmt.Errorf("cannot decode FirmwareVersion: %w", err)
}
d.FirmwareVersion = string(str)
if err := binary.Read(r, binary.LittleEndian, &d.RIMLocatorType); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
data, err = readLengthPrefixed[uint32, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.RIMLocator = data
if err := binary.Read(r, binary.LittleEndian, &d.PlatformCertLocatorType); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
data, err = readLengthPrefixed[uint32, byte](r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.PlatformCertLocator = data
return d, nil
}
func (d *SP800_155_PlatformIdEventData3) String() string {
w := new(bytes.Buffer)
fmt.Fprintf(w, "SP800_155_PlatformId_Event3{")
fmt.Fprintf(w, "\tPlatformManufacturerId: %d\n", d.PlatformManufacturerId)
fmt.Fprintf(w, "\tReferenceManifestGUID: %v\n", d.ReferenceManifestGuid)
fmt.Fprintf(w, "\tPlatformManufacturer: %s\n", d.PlatformManufacturer)
fmt.Fprintf(w, "\tPlatformModel: %s\n", d.PlatformModel)
fmt.Fprintf(w, "\tPlatformVersion: %s\n", d.PlatformVersion)
fmt.Fprintf(w, "\tFirmwareManufacturer: %s\n", d.FirmwareManufacturer)
fmt.Fprintf(w, "\tFirmwareManufacturerId: %d\n", d.FirmwareManufacturerId)
fmt.Fprintf(w, "\tFirmwareVersion: %s\n", d.FirmwareVersion)
printLocator := func(name string, t LocatorType, data []byte) {
switch t {
case LocatorTypeRaw:
fmt.Fprintf(w, "\t%s:<raw>\n", name)
case LocatorTypeURI:
fmt.Fprintf(w, "\t%s:uri:%s\n", name, string(bytes.TrimSuffix(data, []byte{0x00})))
case LocatorTypeDevicePath:
path, err := efi.ReadDevicePath(bytes.NewReader(data))
if err != nil {
fmt.Fprintf(w, "\t%s:devicepath:%v\n", name, err)
} else {
fmt.Fprintf(w, "\t%s:devicepath:%s\n", name, path)
}
case LocatorTypeVariable:
r := bytes.NewReader(data)
guid, err := efi.ReadGUID(r)
if err != nil {
fmt.Fprintf(w, "\t%s:variable:%v\n", name, err)
}
name, err := io.ReadAll(r)
if err != nil {
fmt.Fprintf(w, "\t%s:variable:%v\n", name, err)
}
name = bytes.TrimSuffix(name, []byte{0x00})
fmt.Fprintf(w, "\t%s:variable:%v-%s\n", name, guid, string(name))
}
}
printLocator("RIMLocator", d.RIMLocatorType, d.RIMLocator)
printLocator("PlatformCertLocator", d.PlatformCertLocatorType, d.PlatformCertLocator)
fmt.Fprintf(w, "}")
return w.String()
}
func (e *SP800_155_PlatformIdEventData3) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (d *SP800_155_PlatformIdEventData3) Write(w io.Writer) error {
var signature [16]byte
copy(signature[:], []byte("SP800-155 Event3"))
if _, err := w.Write(signature[:]); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, d.PlatformManufacturerId); err != nil {
return err
}
if _, err := w.Write(d.ReferenceManifestGuid[:]); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.PlatformManufacturer).Bytes()); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.PlatformModel).Bytes()); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.PlatformVersion).Bytes()); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.FirmwareManufacturer).Bytes()); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, d.FirmwareManufacturerId); err != nil {
return err
}
if err := writeLengthPrefixed[uint8](w, NullTerminatedStringEventData(d.FirmwareVersion).Bytes()); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, d.RIMLocatorType); err != nil {
return err
}
if err := writeLengthPrefixed[uint32](w, d.RIMLocator); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, d.PlatformCertLocatorType); err != nil {
return err
}
return writeLengthPrefixed[uint32](w, d.PlatformCertLocator)
}
// EFIVariableData corresponds to the EFI_VARIABLE_DATA type and is the event data associated with the measurement of an
// EFI variable. This is not informative.
type EFIVariableData struct {
VariableName efi.GUID
UnicodeName string
VariableData []byte
}
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf (section 7.8 "Measuring EFI Variables")
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf (section 9.2.6 "Measuring UEFI Variables")
func decodeEventDataEFIVariable(data []byte) (*EFIVariableData, error) {
r := bytes.NewReader(data)
d := new(EFIVariableData)
variableName, err := efi.ReadGUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.VariableName = variableName
var unicodeNameLength uint64
if err := binary.Read(r, binary.LittleEndian, &unicodeNameLength); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
var variableDataLength uint64
if err := binary.Read(r, binary.LittleEndian, &variableDataLength); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
ucs2Name := make([]uint16, unicodeNameLength)
if err := binary.Read(r, binary.LittleEndian, &ucs2Name); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.UnicodeName = efi.ConvertUTF16ToUTF8(ucs2Name)
d.VariableData = make([]byte, variableDataLength)
if _, err := io.ReadFull(r, d.VariableData); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
return d, nil
}
func (e *EFIVariableData) String() string {
return fmt.Sprintf("UEFI_VARIABLE_DATA{ VariableName: %s, UnicodeName: \"%s\", VariableData:\n\t%s}",
e.VariableName, e.UnicodeName, strings.Replace(hex.Dump(e.VariableData), "\n", "\n\t", -1))
}
func (e *EFIVariableData) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *EFIVariableData) Write(w io.Writer) error {
if _, err := w.Write(e.VariableName[:]); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(utf8.RuneCount([]byte(e.UnicodeName)))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(len(e.VariableData))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, efi.ConvertUTF8ToUCS2(e.UnicodeName)); err != nil {
return err
}
_, err := w.Write(e.VariableData)
return err
}
// ComputeEFIVariableDataDigest computes the EFI_VARIABLE_DATA digest associated with the supplied
// parameters
func ComputeEFIVariableDataDigest(alg crypto.Hash, name string, guid efi.GUID, data []byte) []byte {
h := alg.New()
varData := EFIVariableData{VariableName: guid, UnicodeName: name, VariableData: data}
varData.Write(h)
return h.Sum(nil)
}
type rawEFIImageLoadEventHdr struct {
LocationInMemory efi.PhysicalAddress
LengthInMemory uint64
LinkTimeAddress uint64
LengthOfDevicePath uint64
}
// EFIImageLoadEvent corresponds to UEFI_IMAGE_LOAD_EVENT and is informative.
type EFIImageLoadEvent struct {
LocationInMemory efi.PhysicalAddress
LengthInMemory uint64
LinkTimeAddress uint64
DevicePath efi.DevicePath
}
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf (section 4 "Measuring PE/COFF Image Files")
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf (section 9.2.3 "UEFI_IMAGE_LOAD_EVENT Structure")
func decodeEventDataEFIImageLoad(data []byte) (*EFIImageLoadEvent, error) {
r := bytes.NewReader(data)
var e rawEFIImageLoadEventHdr
if err := binary.Read(r, binary.LittleEndian, &e); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
lr := io.LimitReader(r, int64(e.LengthOfDevicePath))
path, err := efi.ReadDevicePath(lr)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
return &EFIImageLoadEvent{
LocationInMemory: e.LocationInMemory,
LengthInMemory: e.LengthInMemory,
LinkTimeAddress: e.LinkTimeAddress,
DevicePath: path}, nil
}
func (e *EFIImageLoadEvent) String() string {
return fmt.Sprintf("UEFI_IMAGE_LOAD_EVENT{ ImageLocationInMemory: 0x%016x, ImageLengthInMemory: %d, "+
"ImageLinkTimeAddress: 0x%016x, DevicePath: %s }", e.LocationInMemory, e.LengthInMemory, e.LinkTimeAddress, e.DevicePath)
}
func (e *EFIImageLoadEvent) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *EFIImageLoadEvent) Write(w io.Writer) error {
dpw := new(bytes.Buffer)
if err := e.DevicePath.Write(dpw); err != nil {
return xerrors.Errorf("cannot write device path: %w", err)
}
ev := rawEFIImageLoadEventHdr{
LocationInMemory: e.LocationInMemory,
LengthInMemory: e.LengthInMemory,
LinkTimeAddress: e.LinkTimeAddress,
LengthOfDevicePath: uint64(dpw.Len())}
if err := binary.Write(w, binary.LittleEndian, &ev); err != nil {
return err
}
_, err := dpw.WriteTo(w)
return err
}
// EFIGPTData corresponds to UEFI_GPT_DATA and is the event data for EV_EFI_GPT_EVENT and
// EV_EFI_GPT_EVENT2 events. When used for EV_EFI_GPT_EVENT2 events, the platform firmware
// zeroes out the DiskGUID field in the header and the UniquePartitionGUID field in each
// partition entry.
type EFIGPTData struct {
Hdr efi.PartitionTableHeader
Partitions []*efi.PartitionEntry
}
func decodeEventDataEFIGPT(data []byte) (*EFIGPTData, error) {
r := bytes.NewReader(data)
d := new(EFIGPTData)
// UEFI_GPT_DATA.UEFIPartitionHeader
hdr, err := efi.ReadPartitionTableHeader(r, false)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.Hdr = *hdr
// UEFI_GPT_DATA.NumberOfPartitions
var numberOfParts uint64
if err := binary.Read(r, binary.LittleEndian, &numberOfParts); err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
if numberOfParts > math.MaxUint32 {
return nil, errors.New("invalid EFI_GPT_DATA.NumberOfPartitons")
}
// UEFI_GPT_DATA.Partitions
partitions, err := efi.ReadPartitionEntries(r, uint32(numberOfParts), hdr.SizeOfPartitionEntry)
if err != nil {
return nil, ioerr.EOFIsUnexpected(err)
}
d.Partitions = partitions
return d, nil
}
func (e *EFIGPTData) String() string {
var builder bytes.Buffer
fmt.Fprintf(&builder, "UEFI_GPT_DATA{\n\tHdr: %s,\n\tPartitions: [", &e.Hdr)
for _, part := range e.Partitions {
fmt.Fprintf(&builder, "\n\t\t%s", part)
}
fmt.Fprintf(&builder, "\n\t]\n}")
return builder.String()
}
func (e *EFIGPTData) Bytes() []byte {
w := new(bytes.Buffer)
if err := e.Write(w); err != nil {
panic(err)
}
return w.Bytes()
}
func (e *EFIGPTData) Write(w io.Writer) error {
if err := e.Hdr.Write(w); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(len(e.Partitions))); err != nil {
return err
}
for _, entry := range e.Partitions {
w2 := new(bytes.Buffer)
if err := entry.Write(w2); err != nil {
return err
}
if w2.Len() > int(e.Hdr.SizeOfPartitionEntry) {
return errors.New("SizeOfPartitionEntry too small")
}
b := make([]byte, e.Hdr.SizeOfPartitionEntry)
copy(b, w2.Bytes())
if _, err := w.Write(b); err != nil {
return err
}
}
return nil
}
// ComputeEFIGPTDataDigest computes a UEFI_GPT_DATA digest from the supplied data.
func ComputeEFIGPTDataDigest(alg crypto.Hash, data *EFIGPTData) ([]byte, error) {
h := alg.New()
if err := data.Write(h); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// ComputeEFIGPT2DataDigest computes a UEFI_GPT_DATA digest from the supplied data,
// for the new EV_EFI_GPT_EVENT2 event type, which zeroes out personally identifiable
// information (the disk GUID and each individual partition unique partition GUID).