-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlog.go
103 lines (87 loc) · 2.93 KB
/
log.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
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package tcglog
import (
"github.com/canonical/go-tpm2"
)
type PlatformType int
const (
PlatformTypeUnknown PlatformType = iota
PlatformTypeBIOS
PlatformTypeEFI
)
// Spec corresponds to the TCG specification that an event log conforms to.
type Spec struct {
PlatformType PlatformType
Major uint8
Minor uint8
Errata uint8
}
// IsBIOS indicates that a log conforms to "TCG PC Client Specific Implementation Specification
// for Conventional BIOS".
// See https://www.trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientImplementation_1-21_1_00.pdf
func (s Spec) IsBIOS() bool { return s.PlatformType == PlatformTypeBIOS }
// IsEFI_1_2 indicates that a log conforms to "TCG EFI Platform Specification For TPM Family 1.1 or
// 1.2".
// See https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf
func (s Spec) IsEFI_1_2() bool {
return s.PlatformType == PlatformTypeEFI && s.Major == 1 && s.Minor == 2
}
// IsEFI_2 indicates that a log conforms to "TCG PC Client Platform Firmware Profile Specification"
// See https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf
func (s Spec) IsEFI_2() bool {
return s.PlatformType == PlatformTypeEFI && s.Major == 2
}
// Log corresponds to a parsed event log.
type Log struct {
Spec Spec // The specification to which this log conforms
Algorithms AlgorithmIdList // The digest algorithms that appear in the log
Events []*Event // The list of events in the log
}
func newLog(event0 *Event) (*Log, []EFISpecIdEventAlgorithmSize) {
var spec Spec
var digestSizes []EFISpecIdEventAlgorithmSize
switch d := event0.Data.(type) {
case *SpecIdEvent00:
spec = Spec{
PlatformType: PlatformTypeBIOS,
Major: d.SpecVersionMajor,
Minor: d.SpecVersionMinor,
Errata: d.SpecErrata}
case *SpecIdEvent02:
spec = Spec{
PlatformType: PlatformTypeEFI,
Major: d.SpecVersionMajor,
Minor: d.SpecVersionMinor,
Errata: d.SpecErrata}
case *SpecIdEvent03:
spec = Spec{
PlatformType: PlatformTypeEFI,
Major: d.SpecVersionMajor,
Minor: d.SpecVersionMinor,
Errata: d.SpecErrata}
digestSizes = d.DigestSizes
}
var algorithms AlgorithmIdList
if spec.IsEFI_2() {
for _, s := range digestSizes {
if s.AlgorithmId.IsValid() {
algorithms = append(algorithms, s.AlgorithmId)
}
}
} else {
algorithms = AlgorithmIdList{tpm2.HashAlgorithmSHA1}
}
return &Log{Spec: spec, Algorithms: algorithms, Events: []*Event{event0}}, digestSizes
}
// NewLogForTesting creates a new log instance from the supplied list of
// events.
func NewLogForTesting(events []*Event) *Log {
if len(events) == 0 {
return new(Log)
}
log, _ := newLog(events[0])
log.Events = append(log.Events, events[1:]...)
return log
}