Debian Codegraph @

attest/internal/events.go

raw
1 // Package internal contains internal structures and functions for parsing
2 // TCG event logs.
3 package internal
4
5 import (
6 "bytes"
7 "crypto/x509"
8 "encoding/binary"
9 "errors"
10 "fmt"
11 "io"
12 "unicode/utf16"
13 )
14
15 const (
16 // maxNameLen is the maximum accepted byte length for a name field.
17 // This value should be larger than any reasonable value.
18 maxNameLen = 2048
19 // maxDataLen is the maximum size in bytes of a variable data field.
20 // This value should be larger than any reasonable value.
21 maxDataLen = 1024 * 1024 // 1 Megabyte.
22 )
23
24 // GUIDs representing the contents of an UEFI_SIGNATURE_LIST.
25 var (
26 hashSHA256SigGUID = efiGUID{0xc1c41626, 0x504c, 0x4092, [8]byte{0xac, 0xa9, 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28}}
27 hashSHA1SigGUID = efiGUID{0x826ca512, 0xcf10, 0x4ac9, [8]byte{0xb1, 0x87, 0xbe, 0x01, 0x49, 0x66, 0x31, 0xbd}}
28 hashSHA224SigGUID = efiGUID{0x0b6e5233, 0xa65c, 0x44c9, [8]byte{0x94, 0x07, 0xd9, 0xab, 0x83, 0xbf, 0xc8, 0xbd}}
29 hashSHA384SigGUID = efiGUID{0xff3e5307, 0x9fd0, 0x48c9, [8]byte{0x85, 0xf1, 0x8a, 0xd5, 0x6c, 0x70, 0x1e, 0x01}}
30 hashSHA512SigGUID = efiGUID{0x093e0fae, 0xa6c4, 0x4f50, [8]byte{0x9f, 0x1b, 0xd4, 0x1e, 0x2b, 0x89, 0xc1, 0x9a}}
31 keyRSA2048SigGUID = efiGUID{0x3c5766e8, 0x269c, 0x4e34, [8]byte{0xaa, 0x14, 0xed, 0x77, 0x6e, 0x85, 0xb3, 0xb6}}
32 certRSA2048SHA256SigGUID = efiGUID{0xe2b36190, 0x879b, 0x4a3d, [8]byte{0xad, 0x8d, 0xf2, 0xe7, 0xbb, 0xa3, 0x27, 0x84}}
33 certRSA2048SHA1SigGUID = efiGUID{0x67f8444f, 0x8743, 0x48f1, [8]byte{0xa3, 0x28, 0x1e, 0xaa, 0xb8, 0x73, 0x60, 0x80}}
34 certX509SigGUID = efiGUID{0xa5c059a1, 0x94e4, 0x4aa7, [8]byte{0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72}}
35 certHashSHA256SigGUID = efiGUID{0x3bd2a492, 0x96c0, 0x4079, [8]byte{0xb4, 0x20, 0xfc, 0xf9, 0x8e, 0xf1, 0x03, 0xed}}
36 certHashSHA384SigGUID = efiGUID{0x7076876e, 0x80c2, 0x4ee6, [8]byte{0xaa, 0xd2, 0x28, 0xb3, 0x49, 0xa6, 0x86, 0x5b}}
37 certHashSHA512SigGUID = efiGUID{0x446dbf63, 0x2502, 0x4cda, [8]byte{0xbc, 0xfa, 0x24, 0x65, 0xd2, 0xb0, 0xfe, 0x9d}}
38 )
39
40 var (
41 // https://github.com/rhboot/shim/blob/20e4d9486fcae54ee44d2323ae342ffe68c920e6/lib/guid.c#L36
42 // GUID used by the shim.
43 shimLockGUID = efiGUID{0x605dab50, 0xe046, 0x4300, [8]byte{0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23}}
44 // "SbatLevel" encoded as UCS-2.
45 shimSbatVarName = []uint16{0x53, 0x62, 0x61, 0x74, 0x4c, 0x65, 0x76, 0x65, 0x6c}
46 // "MokListTrusted" encoded as UCS-2.
47 shimMokListTrustedVarName = []uint16{0x4d, 0x6f, 0x6b, 0x4c, 0x69, 0x73, 0x74, 0x54, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64}
48 )
49
50 // EventType describes the type of event signalled in the event log.
51 type EventType uint32
52
53 // BIOS Events (TCG PC Client Specific Implementation Specification for Conventional BIOS 1.21)
54 const (
55 PrebootCert EventType = 0x00000000
56 PostCode EventType = 0x00000001
57 unused EventType = 0x00000002
58 NoAction EventType = 0x00000003
59 Separator EventType = 0x00000004
60 Action EventType = 0x00000005
61 EventTag EventType = 0x00000006
62 SCRTMContents EventType = 0x00000007
63 SCRTMVersion EventType = 0x00000008
64 CPUMicrocode EventType = 0x00000009
65 PlatformConfigFlags EventType = 0x0000000A
66 TableOfDevices EventType = 0x0000000B
67 CompactHash EventType = 0x0000000C
68 Ipl EventType = 0x0000000D
69 IplPartitionData EventType = 0x0000000E
70 NonhostCode EventType = 0x0000000F
71 NonhostConfig EventType = 0x00000010
72 NonhostInfo EventType = 0x00000011
73 OmitBootDeviceEvents EventType = 0x00000012
74 )
75
76 // EFI Events (TCG EFI Platform Specification Version 1.22)
77 const (
78 EFIEventBase EventType = 0x80000000
79 EFIVariableDriverConfig EventType = 0x80000001
80 EFIVariableBoot EventType = 0x80000002
81 EFIBootServicesApplication EventType = 0x80000003
82 EFIBootServicesDriver EventType = 0x80000004
83 EFIRuntimeServicesDriver EventType = 0x80000005
84 EFIGPTEvent EventType = 0x80000006
85 EFIAction EventType = 0x80000007
86 EFIPlatformFirmwareBlob EventType = 0x80000008
87 EFIHandoffTables EventType = 0x80000009
88 EFIHCRTMEvent EventType = 0x80000010
89 EFIVariableAuthority EventType = 0x800000e0
90 )
91
92 // EFIDeviceType describes the type of a device specified by a device path.
93 type EFIDeviceType uint8
94
95 // "Device Path Protocol" type values.
96 //
97 // Section 9.3.2 of the UEFI specification, accessible at:
98 // https://uefi.org/sites/default/files/resources/UEFI%20Spec%202_6.pdf
99 const (
100 HardwareDevice EFIDeviceType = 0x01
101 ACPIDevice EFIDeviceType = 0x02
102 MessagingDevice EFIDeviceType = 0x03
103 MediaDevice EFIDeviceType = 0x04
104 BBSDevice EFIDeviceType = 0x05
105
106 EndDeviceArrayMarker EFIDeviceType = 0x7f
107 )
108
109 // ErrSigMissingGUID is returned if an EFI_SIGNATURE_DATA structure was parsed
110 // successfully, however was missing the SignatureOwner GUID. This case is
111 // handled specially as a workaround for a bug relating to authority events.
112 var ErrSigMissingGUID = errors.New("signature data was missing owner GUID")
113
114 var eventTypeNames = map[EventType]string{
115 PrebootCert: "Preboot Cert",
116 PostCode: "POST Code",
117 unused: "Unused",
118 NoAction: "No Action",
119 Separator: "Separator",
120 Action: "Action",
121 EventTag: "Event Tag",
122 SCRTMContents: "S-CRTM Contents",
123 SCRTMVersion: "S-CRTM Version",
124 CPUMicrocode: "CPU Microcode",
125 PlatformConfigFlags: "Platform Config Flags",
126 TableOfDevices: "Table of Devices",
127 CompactHash: "Compact Hash",
128 Ipl: "IPL",
129 IplPartitionData: "IPL Partition Data",
130 NonhostCode: "Non-Host Code",
131 NonhostConfig: "Non-HostConfig",
132 NonhostInfo: "Non-Host Info",
133 OmitBootDeviceEvents: "Omit Boot Device Events",
134
135 EFIEventBase: "EFI Event Base",
136 EFIVariableDriverConfig: "EFI Variable Driver Config",
137 EFIVariableBoot: "EFI Variable Boot",
138 EFIBootServicesApplication: "EFI Boot Services Application",
139 EFIBootServicesDriver: "EFI Boot Services Driver",
140 EFIRuntimeServicesDriver: "EFI Runtime Services Driver",
141 EFIGPTEvent: "EFI GPT Event",
142 EFIAction: "EFI Action",
143 EFIPlatformFirmwareBlob: "EFI Platform Firmware Blob",
144 EFIVariableAuthority: "EFI Variable Authority",
145 EFIHandoffTables: "EFI Handoff Tables",
146 EFIHCRTMEvent: "EFI H-CRTM Event",
147 }
148
149 // TaggedEventData represents the TCG_PCClientTaggedEventStruct structure,
150 // as defined by 11.3.2.1 in the "TCG PC Client Specific Implementation
151 // Specification for Conventional BIOS", version 1.21.
152 type TaggedEventData struct {
153 ID uint32
154 Data []byte
155 }
156
157 // ParseTaggedEventData parses a TCG_PCClientTaggedEventStruct structure.
158 func ParseTaggedEventData(d []byte) (*TaggedEventData, error) {
159 var (
160 r = bytes.NewReader(d)
161 header struct {
162 ID uint32
163 DataLen uint32
164 }
165 )
166 if err := binary.Read(r, binary.LittleEndian, &header); err != nil {
167 return nil, fmt.Errorf("reading header: %w", err)
168 }
169
170 if int(header.DataLen) > len(d) {
171 return nil, fmt.Errorf("tagged event len (%d bytes) larger than data length (%d bytes)", header.DataLen, len(d))
172 }
173
174 out := TaggedEventData{
175 ID: header.ID,
176 Data: make([]byte, header.DataLen),
177 }
178 return &out, binary.Read(r, binary.LittleEndian, &out.Data)
179 }
180
181 func (e EventType) String() string {
182 if s, ok := eventTypeNames[e]; ok {
183 return s
184 }
185 return fmt.Sprintf("EventType(0x%x)", uint32(e))
186 }
187
188 // UntrustedParseEventType returns the event type indicated by
189 // the provided value.
190 func UntrustedParseEventType(et uint32) (EventType, error) {
191 // "The value associated with a UEFI specific platform event type MUST be in
192 // the range between 0x80000000 and 0x800000FF, inclusive."
193 if (et < 0x80000000 && et > 0x800000FF) || (et <= 0x0 && et > 0x12) {
194 return EventType(0), fmt.Errorf("event type not between [0x0, 0x12] or [0x80000000, 0x800000FF]: got %#x", et)
195 }
196 if _, ok := eventTypeNames[EventType(et)]; !ok {
197 return EventType(0), fmt.Errorf("unknown event type %#x", et)
198 }
199 return EventType(et), nil
200 }
201
202 // efiGUID represents the EFI_GUID type.
203 // See section "2.3.1 Data Types" in the specification for more information.
204 // type efiGUID [16]byte
205 type efiGUID struct {
206 Data1 uint32
207 Data2 uint16
208 Data3 uint16
209 Data4 [8]byte
210 }
211
212 func (d efiGUID) String() string {
213 var u [8]byte
214 binary.BigEndian.PutUint32(u[:4], d.Data1)
215 binary.BigEndian.PutUint16(u[4:6], d.Data2)
216 binary.BigEndian.PutUint16(u[6:8], d.Data3)
217 return fmt.Sprintf("%x-%x-%x-%x-%x", u[:4], u[4:6], u[6:8], d.Data4[:2], d.Data4[2:])
218 }
219
220 // UEFIVariableDataHeader represents the leading fixed-size fields
221 // within UEFI_VARIABLE_DATA.
222 type UEFIVariableDataHeader struct {
223 VariableName efiGUID
224 UnicodeNameLength uint64 // uintN
225 VariableDataLength uint64 // uintN
226 }
227
228 // UEFIVariableData represents the UEFI_VARIABLE_DATA structure.
229 type UEFIVariableData struct {
230 Header UEFIVariableDataHeader
231 UnicodeName []uint16
232 VariableData []byte // []int8
233 }
234
235 // ParseUEFIVariableData parses the data section of an event structured as
236 // a UEFI variable.
237 //
238 // https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClient_Specific_Platform_Profile_for_TPM_2p0_1p04_PUBLIC.pdf#page=100
239 func ParseUEFIVariableData(r io.Reader) (ret UEFIVariableData, err error) {
240 err = binary.Read(r, binary.LittleEndian, &ret.Header)
241 if err != nil {
242 return
243 }
244 if ret.Header.UnicodeNameLength > maxNameLen {
245 return UEFIVariableData{}, fmt.Errorf("unicode name too long: %d > %d", ret.Header.UnicodeNameLength, maxNameLen)
246 }
247 ret.UnicodeName = make([]uint16, ret.Header.UnicodeNameLength)
248 for i := 0; uint64(i) < ret.Header.UnicodeNameLength; i++ {
249 err = binary.Read(r, binary.LittleEndian, &ret.UnicodeName[i])
250 if err != nil {
251 return
252 }
253 }
254 if ret.Header.VariableDataLength > maxDataLen {
255 return UEFIVariableData{}, fmt.Errorf("variable data too long: %d > %d", ret.Header.VariableDataLength, maxDataLen)
256 }
257 ret.VariableData = make([]byte, ret.Header.VariableDataLength)
258 _, err = io.ReadFull(r, ret.VariableData)
259 return
260 }
261
262 // VarName returns the variable name from the UEFI variable data.
263 func (v *UEFIVariableData) VarName() string {
264 return string(utf16.Decode(v.UnicodeName))
265 }
266
267 // SignatureData returns the signature data from the UEFI variable data.
268 func (v *UEFIVariableData) SignatureData() (certs []x509.Certificate, hashes [][]byte, err error) {
269 return parseEfiSignatureList(v.VariableData)
270 }
271
272 // UEFIVariableAuthority describes the contents of a UEFI variable authority
273 // event.
274 type UEFIVariableAuthority struct {
275 Certs []x509.Certificate
276 }
277
278 // ParseUEFIVariableAuthority parses the data section of an event structured as
279 // a UEFI variable authority.
280 //
281 // https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=1789
282 func ParseUEFIVariableAuthority(v UEFIVariableData) (UEFIVariableAuthority, error) {
283 if v.Header.VariableName == shimLockGUID && (
284 // Skip parsing new SBAT section logged by shim.
285 // See https://github.com/rhboot/shim/blob/main/SBAT.md for more.
286 unicodeNameEquals(v, shimSbatVarName) || // https://github.com/rhboot/shim/blob/20e4d9486fcae54ee44d2323ae342ffe68c920e6/include/sbat.h#L9-L12
287 // Skip parsing new MokListTrusted section logged by shim.
288 // See https://github.com/rhboot/shim/blob/main/MokVars.txt for more.
289 unicodeNameEquals(v, shimMokListTrustedVarName)) { // https://github.com/rhboot/shim/blob/4e513405b4f1641710115780d19dcec130c5208f/mok.c#L169-L182
290 return UEFIVariableAuthority{}, nil
291 }
292 certs, err := parseEfiSignature(v.VariableData)
293 return UEFIVariableAuthority{Certs: certs}, err
294 }
295
296 func unicodeNameEquals(v UEFIVariableData, comp []uint16) bool {
297 if len(v.UnicodeName) != len(comp) {
298 return false
299 }
300 for i, v := range v.UnicodeName {
301 if v != comp[i] {
302 return false
303 }
304 }
305 return true
306 }
307
308 // efiSignatureData represents the EFI_SIGNATURE_DATA type.
309 // See section "31.4.1 Signature Database" in the specification for more information.
310 type efiSignatureData struct {
311 SignatureOwner efiGUID
312 SignatureData []byte // []int8
313 }
314
315 // efiSignatureList represents the EFI_SIGNATURE_LIST type.
316 // See section "31.4.1 Signature Database" in the specification for more information.
317 type efiSignatureListHeader struct {
318 SignatureType efiGUID
319 SignatureListSize uint32
320 SignatureHeaderSize uint32
321 SignatureSize uint32
322 }
323
324 type efiSignatureList struct {
325 Header efiSignatureListHeader
326 SignatureData []byte
327 Signatures []byte
328 }
329
330 const (
331 // efiGUIDSize is the binary size of efiGUID:
332 // uint32(4) + uint16(2) + uint16(2) + [8]byte(8) = 16 bytes.
333 efiGUIDSize = 16
334
335 // efiSignatureListHeaderSize is the binary size of efiSignatureListHeader:
336 // efiGUID(16) + SignatureListSize uint32(4) + SignatureHeaderSize uint32(4) + SignatureSize uint32(4) = 28 bytes.
337 efiSignatureListHeaderSize = efiGUIDSize + 4 + 4 + 4
338 )
339
340 // parseEfiSignatureList parses a EFI_SIGNATURE_LIST structure.
341 // The structure and related GUIDs are defined at:
342 // https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=1790
343 func parseEfiSignatureList(b []byte) ([]x509.Certificate, [][]byte, error) {
344 if len(b) < efiSignatureListHeaderSize {
345 // Being passed an empty signature list here appears to be valid
346 return nil, nil, nil
347 }
348 signatures := efiSignatureList{}
349 buf := bytes.NewReader(b)
350 var certificates []x509.Certificate
351 var hashes [][]byte
352
353 for buf.Len() > 0 {
354 err := binary.Read(buf, binary.LittleEndian, &signatures.Header)
355 if err != nil {
356 return nil, nil, err
357 }
358
359 if signatures.Header.SignatureHeaderSize > maxDataLen {
360 return nil, nil, fmt.Errorf("signature header too large: %d > %d", signatures.Header.SignatureHeaderSize, maxDataLen)
361 }
362 if signatures.Header.SignatureListSize > maxDataLen {
363 return nil, nil, fmt.Errorf("signature list too large: %d > %d", signatures.Header.SignatureListSize, maxDataLen)
364 }
365 // Guard against uint32 underflow and OOM: SignatureListSize and SignatureSize
366 // are attacker-controlled fields. Without these checks, the subtractions
367 // below (SignatureListSize-efiSignatureListHeaderSize, SignatureSize-efiGUIDSize) would wrap around,
368 // causing an infinite loop or an OOM panic via make([]byte, ~4 GiB).
369 if signatures.Header.SignatureListSize < efiSignatureListHeaderSize {
370 return nil, nil, fmt.Errorf("SignatureListSize %d is smaller than the minimum header size of %d", signatures.Header.SignatureListSize, efiSignatureListHeaderSize)
371 }
372 if signatures.Header.SignatureSize < efiGUIDSize {
373 return nil, nil, fmt.Errorf("SignatureSize %d is smaller than the minimum entry size of %d", signatures.Header.SignatureSize, efiGUIDSize)
374 }
375 // Guard against OOM: SignatureSize is unbounded by the prior checks, allowing
376 // a crafted event log to set SignatureSize=0xFFFFFFFF while providing only a
377 // few bytes of actual data, causing make([]byte, SignatureSize-efiGUIDSize) to
378 // attempt a ~4 GiB allocation before binary.Read fails.
379 // SignatureSize must not exceed the remaining space within the signature list.
380 remainingListSize := signatures.Header.SignatureListSize - efiSignatureListHeaderSize
381 if signatures.Header.SignatureSize > remainingListSize {
382 return nil, nil, fmt.Errorf("SignatureSize %d exceeds remaining signature list space %d", signatures.Header.SignatureSize, remainingListSize)
383 }
384 // Guard against hash injection via oversized SignatureHeaderSize.
385 // Per UEFI spec section 31.4.1, SignatureHeaderSize bytes of vendor data
386 // appear between the fixed header and the actual signature entries.
387 // SignatureHeaderSize must not consume the entire remaining list space.
388 if signatures.Header.SignatureHeaderSize >= remainingListSize {
389 return nil, nil, fmt.Errorf("SignatureHeaderSize %d exceeds remaining signature list space %d", signatures.Header.SignatureHeaderSize, remainingListSize)
390 }
391 // Skip the vendor-specific SignatureHeader bytes per UEFI spec section 31.4.1.
392 // Without this, vendor bytes are misread as signature entries, allowing a
393 // crafted event log to inject arbitrary hashes into the trusted hash list.
394 if signatures.Header.SignatureHeaderSize > 0 {
395 if _, err := buf.Seek(int64(signatures.Header.SignatureHeaderSize), io.SeekCurrent); err != nil {
396 return nil, nil, fmt.Errorf("seeking past signature vendor header: %w", err)
397 }
398 }
399
400 signatureType := signatures.Header.SignatureType
401 switch signatureType {
402 case certX509SigGUID: // X509 certificate
403 for sigOffset := int(signatures.Header.SignatureHeaderSize); uint32(sigOffset) < signatures.Header.SignatureListSize-efiSignatureListHeaderSize; {
404 signature := efiSignatureData{}
405 signature.SignatureData = make([]byte, signatures.Header.SignatureSize-efiGUIDSize)
406 err := binary.Read(buf, binary.LittleEndian, &signature.SignatureOwner)
407 if err != nil {
408 return nil, nil, err
409 }
410 err = binary.Read(buf, binary.LittleEndian, &signature.SignatureData)
411 if err != nil {
412 return nil, nil, err
413 }
414 cert, err := x509.ParseCertificate(signature.SignatureData)
415 if err != nil {
416 return nil, nil, err
417 }
418 sigOffset += int(signatures.Header.SignatureSize)
419 certificates = append(certificates, *cert)
420 }
421 case hashSHA256SigGUID: // SHA256
422 for sigOffset := int(signatures.Header.SignatureHeaderSize); uint32(sigOffset) < signatures.Header.SignatureListSize-efiSignatureListHeaderSize; {
423 signature := efiSignatureData{}
424 signature.SignatureData = make([]byte, signatures.Header.SignatureSize-efiGUIDSize)
425 err := binary.Read(buf, binary.LittleEndian, &signature.SignatureOwner)
426 if err != nil {
427 return nil, nil, err
428 }
429 err = binary.Read(buf, binary.LittleEndian, &signature.SignatureData)
430 if err != nil {
431 return nil, nil, err
432 }
433 hashes = append(hashes, signature.SignatureData)
434 sigOffset += int(signatures.Header.SignatureSize)
435 }
436 case keyRSA2048SigGUID:
437 err = errors.New("unhandled RSA2048 key")
438 case certRSA2048SHA256SigGUID:
439 err = errors.New("unhandled RSA2048-SHA256 key")
440 case hashSHA1SigGUID:
441 err = errors.New("unhandled SHA1 hash")
442 case certRSA2048SHA1SigGUID:
443 err = errors.New("unhandled RSA2048-SHA1 key")
444 case hashSHA224SigGUID:
445 err = errors.New("unhandled SHA224 hash")
446 case hashSHA384SigGUID:
447 err = errors.New("unhandled SHA384 hash")
448 case hashSHA512SigGUID:
449 err = errors.New("unhandled SHA512 hash")
450 case certHashSHA256SigGUID:
451 err = errors.New("unhandled X509-SHA256 hash metadata")
452 case certHashSHA384SigGUID:
453 err = errors.New("unhandled X509-SHA384 hash metadata")
454 case certHashSHA512SigGUID:
455 err = errors.New("unhandled X509-SHA512 hash metadata")
456 default:
457 err = fmt.Errorf("unhandled signature type %s", signatureType)
458 }
459 if err != nil {
460 return nil, nil, err
461 }
462 }
463 return certificates, hashes, nil
464 }
465
466 // EFISignatureData represents the EFI_SIGNATURE_DATA type.
467 // See section "31.4.1 Signature Database" in the specification
468 // for more information.
469 type EFISignatureData struct {
470 SignatureOwner efiGUID
471 SignatureData []byte // []int8
472 }
473
474 func parseEfiSignature(b []byte) ([]x509.Certificate, error) {
475 var certificates []x509.Certificate
476
477 if len(b) < efiGUIDSize {
478 return nil, fmt.Errorf("invalid signature: buffer smaller than header (%d < %d)", len(b), efiGUIDSize)
479 }
480
481 buf := bytes.NewReader(b)
482 signature := EFISignatureData{}
483 signature.SignatureData = make([]byte, len(b)-efiGUIDSize)
484
485 if err := binary.Read(buf, binary.LittleEndian, &signature.SignatureOwner); err != nil {
486 return certificates, err
487 }
488 if err := binary.Read(buf, binary.LittleEndian, &signature.SignatureData); err != nil {
489 return certificates, err
490 }
491
492 cert, err := x509.ParseCertificate(signature.SignatureData)
493 if err == nil {
494 certificates = append(certificates, *cert)
495 } else {
496 // A bug in shim may cause an event to be missing the SignatureOwner GUID.
497 // We handle this, but signal back to the caller using ErrSigMissingGUID.
498 var err2 error
499 cert, err2 = x509.ParseCertificate(b)
500 if err2 == nil {
501 certificates = append(certificates, *cert)
502 err = ErrSigMissingGUID
503 }
504 }
505 return certificates, err
506 }
507
508 // EFIDevicePathElement represents an EFI_DEVICE_PATH_ELEMENT structure.
509 type EFIDevicePathElement struct {
510 Type EFIDeviceType
511 Subtype uint8
512 Data []byte
513 }
514
515 // EFIImageLoad describes an EFI_IMAGE_LOAD_EVENT structure.
516 type EFIImageLoad struct {
517 Header EFIImageLoadHeader
518 DevPathData []byte
519 }
520
521 // EFIImageLoadHeader represents the EFI_IMAGE_LOAD_EVENT structure.
522 type EFIImageLoadHeader struct {
523 LoadAddr uint64
524 Length uint64
525 LinkAddr uint64
526 DevicePathLen uint64
527 }
528
529 func parseDevicePathElement(r io.Reader) (EFIDevicePathElement, error) {
530 var (
531 out EFIDevicePathElement
532 dataLen uint16
533 )
534
535 if err := binary.Read(r, binary.LittleEndian, &out.Type); err != nil {
536 return EFIDevicePathElement{}, fmt.Errorf("reading type: %v", err)
537 }
538 if err := binary.Read(r, binary.LittleEndian, &out.Subtype); err != nil {
539 return EFIDevicePathElement{}, fmt.Errorf("reading subtype: %v", err)
540 }
541 if err := binary.Read(r, binary.LittleEndian, &dataLen); err != nil {
542 return EFIDevicePathElement{}, fmt.Errorf("reading data len: %v", err)
543 }
544 if dataLen > maxNameLen {
545 return EFIDevicePathElement{}, fmt.Errorf("device path data too long: %d > %d", dataLen, maxNameLen)
546 }
547 if dataLen < 4 {
548 return EFIDevicePathElement{}, fmt.Errorf("device path data too short: %d < %d", dataLen, 4)
549 }
550 out.Data = make([]byte, dataLen-4)
551 if err := binary.Read(r, binary.LittleEndian, &out.Data); err != nil {
552 return EFIDevicePathElement{}, fmt.Errorf("reading data: %v", err)
553 }
554 return out, nil
555 }
556
557 // DevicePath returns the device path elements from the EFI_IMAGE_LOAD_EVENT structure.
558 func (h *EFIImageLoad) DevicePath() ([]EFIDevicePathElement, error) {
559 var (
560 r = bytes.NewReader(h.DevPathData)
561 out []EFIDevicePathElement
562 )
563
564 for r.Len() > 0 {
565 e, err := parseDevicePathElement(r)
566 if err != nil {
567 return nil, err
568 }
569 if e.Type == EndDeviceArrayMarker {
570 return out, nil
571 }
572
573 out = append(out, e)
574 }
575
576 return out, nil
577 }
578
579 // ParseEFIImageLoad parses an EFI_IMAGE_LOAD_EVENT structure.
580 //
581 // https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf#page=17
582 func ParseEFIImageLoad(r io.Reader) (ret EFIImageLoad, err error) {
583 err = binary.Read(r, binary.LittleEndian, &ret.Header)
584 if err != nil {
585 return
586 }
587 if ret.Header.DevicePathLen > maxNameLen {
588 return EFIImageLoad{}, fmt.Errorf("device path structure too long: %d > %d", ret.Header.DevicePathLen, maxNameLen)
589 }
590 ret.DevPathData = make([]byte, ret.Header.DevicePathLen)
591 err = binary.Read(r, binary.LittleEndian, &ret.DevPathData)
592 return
593 }