Debian Codegraph @

attest/eventlog_workarounds.go

raw
1 // Copyright 2020 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 package attest
16
17 import "fmt"
18
19 type elWorkaround struct {
20 id string
21 affectedPCR int
22 apply func(e *EventLog) error
23 }
24
25 // inject3 appends two new events into the event log.
26 func inject3(e *EventLog, pcr int, data1, data2, data3 string) error {
27 if err := inject(e, pcr, data1); err != nil {
28 return err
29 }
30 if err := inject(e, pcr, data2); err != nil {
31 return err
32 }
33 return inject(e, pcr, data3)
34 }
35
36 // inject2 appends two new events into the event log.
37 func inject2(e *EventLog, pcr int, data1, data2 string) error {
38 if err := inject(e, pcr, data1); err != nil {
39 return err
40 }
41 return inject(e, pcr, data2)
42 }
43
44 // inject appends a new event into the event log.
45 func inject(e *EventLog, pcr int, data string) error {
46 evt := rawEvent{
47 data: []byte(data),
48 index: pcr,
49 sequence: e.rawEvents[len(e.rawEvents)-1].sequence + 1,
50 }
51 for _, alg := range e.Algs {
52 hash, err := alg.cryptoHash()
53 if err != nil {
54 return fmt.Errorf("unknown algorithm ID %x: %v", alg, err)
55 }
56 h := hash.New()
57 h.Write([]byte(data))
58 evt.digests = append(evt.digests, digest{hash: hash, data: h.Sum(nil)})
59 }
60 e.rawEvents = append(e.rawEvents, evt)
61 return nil
62 }
63
64 const (
65 ebsInvocation = "Exit Boot Services Invocation"
66 ebsSuccess = "Exit Boot Services Returned with Success"
67 ebsFailure = "Exit Boot Services Returned with Failure"
68 )
69
70 var eventlogWorkarounds = []elWorkaround{
71 {
72 id: "EBS Invocation + Success",
73 affectedPCR: 5,
74 apply: func(e *EventLog) error {
75 return inject2(e, 5, ebsInvocation, ebsSuccess)
76 },
77 },
78 {
79 id: "EBS Invocation + Failure",
80 affectedPCR: 5,
81 apply: func(e *EventLog) error {
82 return inject2(e, 5, ebsInvocation, ebsFailure)
83 },
84 },
85 {
86 id: "EBS Invocation + Failure + Success",
87 affectedPCR: 5,
88 apply: func(e *EventLog) error {
89 return inject3(e, 5, ebsInvocation, ebsFailure, ebsSuccess)
90 },
91 },
92 }