-
Notifications
You must be signed in to change notification settings - Fork 1
/
grubeventdata.go
73 lines (62 loc) · 1.5 KB
/
grubeventdata.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
package tcglog
import (
"fmt"
"io"
"strings"
)
var (
kernelCmdlinePrefix = "kernel_cmdline: "
grubCmdPrefix = "grub_cmd: "
)
type GrubStringEventType int
const (
GrubCmd GrubStringEventType = iota
KernelCmdline
)
func grubEventTypeString(t GrubStringEventType) string {
switch t {
case GrubCmd:
return "grub_cmd"
case KernelCmdline:
return "kernel_cmdline"
}
panic("invalid value")
}
type GrubStringEventData struct {
data []byte
Type GrubStringEventType
Str string
}
func (e *GrubStringEventData) String() string {
return fmt.Sprintf("%s{ %s }", grubEventTypeString(e.Type), e.Str)
}
func (e *GrubStringEventData) Bytes() []byte {
return e.data
}
func (e *GrubStringEventData) EncodeMeasuredBytes(buf io.Writer) error {
if _, err := io.WriteString(buf, e.Str); err != nil {
return err
}
return nil
}
func decodeEventDataGRUB(pcrIndex PCRIndex, eventType EventType, data []byte) (EventData, int) {
if eventType != EventTypeIPL {
return nil, 0
}
switch pcrIndex {
case 8:
str := string(data)
switch {
case strings.HasPrefix(str, kernelCmdlinePrefix):
return &GrubStringEventData{data, KernelCmdline, strings.TrimSuffix(strings.TrimPrefix(str, kernelCmdlinePrefix), "\x00")}, 0
case strings.HasPrefix(str, grubCmdPrefix):
return &GrubStringEventData{data, GrubCmd, strings.TrimSuffix(strings.TrimPrefix(str, grubCmdPrefix), "\x00")}, 0
default:
return nil, 0
}
case 9:
return &asciiStringEventData{data: data}, 0
default:
panic("unhandled PCR index")
}
}