| 1 |
// Copyright 2019 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 eventlog |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto" |
| 20 |
"encoding/binary" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"unicode/utf16" |
| 24 |
|
| 25 |
"github.com/google/go-attestation/attest" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
// https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=153 |
| 30 |
efiGlobalVariable = efiGUID{ |
| 31 |
0x8BE4DF61, 0x93CA, 0x11d2, [8]uint8{0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}} |
| 32 |
|
| 33 |
efiGlobalVariableSecureBoot = "SecureBoot" |
| 34 |
efiGlobalVariablePlatformKey = "PK" |
| 35 |
efiGlobalVariableKeyExchangeKey = "KEK" |
| 36 |
|
| 37 |
// https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=1804 |
| 38 |
efiImageSecurityDatabaseGUID = efiGUID{ |
| 39 |
0xd719b2cb, 0x3d3a, 0x4596, [8]uint8{0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f}} |
| 40 |
|
| 41 |
efiImageSecurityDatabase = "db" |
| 42 |
efiImageSecurityDatabase1 = "dbx" |
| 43 |
efiImageSecurityDatabase2 = "dbt" |
| 44 |
efiImageSecurityDatabase3 = "dbr" |
| 45 |
) |
| 46 |
|
| 47 |
type efiGUID struct { |
| 48 |
Data1 uint32 |
| 49 |
Data2 uint16 |
| 50 |
Data3 uint16 |
| 51 |
Data4 [8]uint8 |
| 52 |
} |
| 53 |
|
| 54 |
func (e efiGUID) String() string { |
| 55 |
if s, ok := efiGUIDString[e]; ok { |
| 56 |
return s |
| 57 |
} |
| 58 |
return fmt.Sprintf("{0x%x,0x%x,0x%x,{%x}}", e.Data1, e.Data2, e.Data3, e.Data4) |
| 59 |
} |
| 60 |
|
| 61 |
var efiGUIDString = map[efiGUID]string{ |
| 62 |
efiGlobalVariable: "EFI_GLOBAL_VARIABLE", |
| 63 |
efiImageSecurityDatabaseGUID: "EFI_IMAGE_SECURITY_DATABASE_GUID", |
| 64 |
} |
| 65 |
|
| 66 |
type uefiVariableData struct { |
| 67 |
id efiGUID |
| 68 |
name string |
| 69 |
data []byte |
| 70 |
} |
| 71 |
|
| 72 |
func (d *uefiVariableData) String() string { |
| 73 |
return fmt.Sprintf("%s %s data length %d", d.id, d.name, len(d.data)) |
| 74 |
} |
| 75 |
|
| 76 |
// SecureBoot holds parsed PCR 7 values representing secure boot settings for |
| 77 |
// the device. |
| 78 |
type SecureBoot struct { |
| 79 |
Enabled bool |
| 80 |
|
| 81 |
// TODO(ericchiang): parse these as EFI_SIGNATURE_LIST |
| 82 |
// |
| 83 |
// https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=1788 |
| 84 |
|
| 85 |
PK []byte |
| 86 |
KEK []byte |
| 87 |
|
| 88 |
DB []byte |
| 89 |
DBX []byte |
| 90 |
|
| 91 |
DBT []byte |
| 92 |
DBR []byte |
| 93 |
|
| 94 |
// Authority is the set of certificate that were used during secure boot |
| 95 |
// validation. This will be a subset of the certificates in DB. |
| 96 |
Authority []byte |
| 97 |
} |
| 98 |
|
| 99 |
// ParseSecureBoot parses UEFI secure boot variables (PCR[7) from a verified event log. |
| 100 |
// |
| 101 |
// See https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClient_Specific_Platform_Profile_for_TPM_2p0_1p04_PUBLIC.pdf#page=56 |
| 102 |
func ParseSecureBoot(events []attest.Event) (*SecureBoot, error) { |
| 103 |
var sb SecureBoot |
| 104 |
seenSep := false |
| 105 |
for i, e := range events { |
| 106 |
if e.Index != 7 { |
| 107 |
continue |
| 108 |
} |
| 109 |
t := eventType(e.Type) |
| 110 |
switch t { |
| 111 |
case evEFIVariableDriverConfig: |
| 112 |
if seenSep { |
| 113 |
return nil, fmt.Errorf("event %d %s after %s", i, t, evSeparator) |
| 114 |
} |
| 115 |
data, err := parseUEFIVariableData(e.Data, e.Digest) |
| 116 |
if err != nil { |
| 117 |
return nil, fmt.Errorf("parsing event %d, PCR[%02d] %s: %v", i, e.Index, t, err) |
| 118 |
} |
| 119 |
|
| 120 |
switch data.id { |
| 121 |
case efiGlobalVariable: |
| 122 |
switch data.name { |
| 123 |
case efiGlobalVariableSecureBoot: |
| 124 |
if len(data.data) != 1 { |
| 125 |
return nil, fmt.Errorf("%s/%s was %d bytes", data.id, data.name, len(data.data)) |
| 126 |
} |
| 127 |
switch data.data[0] { |
| 128 |
case 0x0: |
| 129 |
sb.Enabled = false |
| 130 |
case 0x1: |
| 131 |
sb.Enabled = true |
| 132 |
default: |
| 133 |
return nil, fmt.Errorf("invalid %s/%s value 0x%x", data.id, data.name, data.data) |
| 134 |
} |
| 135 |
case efiGlobalVariablePlatformKey: |
| 136 |
sb.PK = data.data |
| 137 |
case efiGlobalVariableKeyExchangeKey: |
| 138 |
sb.KEK = data.data |
| 139 |
} |
| 140 |
case efiImageSecurityDatabaseGUID: |
| 141 |
switch data.name { |
| 142 |
case efiImageSecurityDatabase: |
| 143 |
sb.DB = data.data |
| 144 |
case efiImageSecurityDatabase1: |
| 145 |
sb.DBX = data.data |
| 146 |
case efiImageSecurityDatabase2: |
| 147 |
sb.DBT = data.data |
| 148 |
case efiImageSecurityDatabase3: |
| 149 |
sb.DBR = data.data |
| 150 |
} |
| 151 |
} |
| 152 |
case evEFIVariableAuthority: |
| 153 |
if !seenSep { |
| 154 |
return nil, fmt.Errorf("event %d %s before %s", i, t, evSeparator) |
| 155 |
} |
| 156 |
data, err := parseUEFIVariableData(e.Data, e.Digest) |
| 157 |
if err != nil { |
| 158 |
return nil, fmt.Errorf("parsing event %d, PCR[%02d] %s: %v", i, e.Index, t, err) |
| 159 |
} |
| 160 |
switch data.id { |
| 161 |
case efiImageSecurityDatabaseGUID: |
| 162 |
switch data.name { |
| 163 |
case efiImageSecurityDatabase: |
| 164 |
if !sb.Enabled { |
| 165 |
return nil, fmt.Errorf("%s/%s present when secure boot wasn't enabled", t, data.name) |
| 166 |
} |
| 167 |
if len(sb.Authority) != 0 { |
| 168 |
// If a malicious value is appended to the eventlog, |
| 169 |
// ensure we only trust the first value written by |
| 170 |
// the UEFI firmware. |
| 171 |
return nil, fmt.Errorf("%s/%s was already present earlier in the event log", t, data.name) |
| 172 |
} |
| 173 |
sb.Authority = data.data |
| 174 |
} |
| 175 |
} |
| 176 |
case evSeparator: |
| 177 |
seenSep = true |
| 178 |
} |
| 179 |
} |
| 180 |
return &sb, nil |
| 181 |
} |
| 182 |
|
| 183 |
func binaryRead(r io.Reader, i any) error { |
| 184 |
return binary.Read(r, binary.LittleEndian, i) |
| 185 |
} |
| 186 |
|
| 187 |
var hashBySize = map[int]crypto.Hash{ |
| 188 |
crypto.SHA1.Size(): crypto.SHA1, |
| 189 |
crypto.SHA256.Size(): crypto.SHA256, |
| 190 |
} |
| 191 |
|
| 192 |
func verifyDigest(digest, data []byte) bool { |
| 193 |
h, ok := hashBySize[len(digest)] |
| 194 |
if !ok { |
| 195 |
return false |
| 196 |
} |
| 197 |
hash := h.New() |
| 198 |
hash.Write(data) |
| 199 |
return bytes.Equal(digest, hash.Sum(nil)) |
| 200 |
} |
| 201 |
|
| 202 |
// parseUEFIVariableData parses a UEFI_VARIABLE_DATA struct and validates the |
| 203 |
// digest of an event entry. |
| 204 |
// |
| 205 |
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClient_Specific_Platform_Profile_for_TPM_2p0_1p04_PUBLIC.pdf#page=100 |
| 206 |
func parseUEFIVariableData(b, digest []byte) (*uefiVariableData, error) { |
| 207 |
lr := &io.LimitedReader{R: bytes.NewReader(b), N: int64(len(b))} |
| 208 |
var hdr struct { |
| 209 |
ID efiGUID |
| 210 |
NameLength uint64 |
| 211 |
DataLength uint64 |
| 212 |
} |
| 213 |
if err := binaryRead(lr, &hdr); err != nil { |
| 214 |
return nil, err |
| 215 |
} |
| 216 |
if hdr.NameLength > uint64(lr.N)/2 { |
| 217 |
return nil, fmt.Errorf("requested NameLength (%d uint16s) exceeds message size (%d uint16s)", hdr.NameLength, lr.N/2) |
| 218 |
} |
| 219 |
name := make([]uint16, hdr.NameLength) |
| 220 |
if err := binaryRead(lr, &name); err != nil { |
| 221 |
return nil, fmt.Errorf("parsing name: %v", err) |
| 222 |
} |
| 223 |
if lr.N != int64(hdr.DataLength) { |
| 224 |
return nil, fmt.Errorf("remaining bytes %d doesn't match data length %d", lr.N, hdr.DataLength) |
| 225 |
} |
| 226 |
data := make([]byte, hdr.DataLength) |
| 227 |
if _, err := io.ReadFull(lr, data); err != nil { |
| 228 |
return nil, fmt.Errorf("reading data: %v", err) |
| 229 |
} |
| 230 |
// TODO(ericchiang): older UEFI firmware (Lenovo Bios version 1.17) logs the |
| 231 |
// digest of the data, which doesn't encapsulate the ID or name. This lets |
| 232 |
// attackers alter keys and we should determine if this is an acceptable risk. |
| 233 |
if !verifyDigest(digest, b) && !verifyDigest(digest, data) { |
| 234 |
return nil, fmt.Errorf("digest didn't match data") |
| 235 |
} |
| 236 |
return &uefiVariableData{id: hdr.ID, name: string(utf16.Decode(name)), data: data}, nil |
| 237 |
} |