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(data []byte) (ret UEFIVariableData, err error) {
240 r := &io.LimitedReader{R: bytes.NewReader(data), N: int64(len(data))}
241 err = binary.Read(r, binary.LittleEndian, &ret.Header)
242 if err != nil {
243 return
244 }
245 if ret.Header.UnicodeNameLength > maxNameLen {
246 return UEFIVariableData{}, fmt.Errorf("unicode name too long: %d > %d", ret.Header.UnicodeNameLength, maxNameLen)
247 }
248 if int64(ret.Header.UnicodeNameLength)*2 > r.N {
249 return UEFIVariableData{}, fmt.Errorf("unicode name length (%d bytes) larger than remaining capacity (%d bytes)", ret.Header.UnicodeNameLength*2, r.N)
250 }
251 ret.UnicodeName = make([]uint16, ret.Header.UnicodeNameLength)
252 for i := 0; uint64(i) < ret.Header.UnicodeNameLength; i++ {
253 err = binary.Read(r, binary.LittleEndian, &ret.UnicodeName[i])
254 if err != nil {
255 return
256 }
257 }
258 if ret.Header.VariableDataLength > maxDataLen {
259 return UEFIVariableData{}, fmt.Errorf("variable data too long: %d > %d", ret.Header.VariableDataLength, maxDataLen)
260 }
261 if int64(ret.Header.VariableDataLength) > r.N {
262 return UEFIVariableData{}, fmt.Errorf("variable data length (%d bytes) larger than remaining capacity (%d bytes)", ret.Header.VariableDataLength, r.N)
263 }
264 ret.VariableData = make([]byte, ret.Header.VariableDataLength)
265 _, err = io.ReadFull(r, ret.VariableData)
266 return
267 }
268
269 // VarName returns the variable name from the UEFI variable data.
270 func (v *UEFIVariableData) VarName() string {
271 return string(utf16.Decode(v.UnicodeName))
272 }
273
274 // SignatureData returns the signature data from the UEFI variable data.
275 func (v *UEFIVariableData) SignatureData() (certs []x509.Certificate, hashes [][]byte, err error) {
276 return parseEfiSignatureList(v.VariableData)
277 }
278
279 // UEFIVariableAuthority describes the contents of a UEFI variable authority
280 // event.
281 type UEFIVariableAuthority struct {
282 Certs []x509.Certificate
283 }
284
285 // ParseUEFIVariableAuthority parses the data section of an event structured as
286 // a UEFI variable authority.
287 //
288 // https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=1789
289 func ParseUEFIVariableAuthority(v UEFIVariableData) (UEFIVariableAuthority, error) {
290 if v.Header.VariableName == shimLockGUID && (
291 // Skip parsing new SBAT section logged by shim.
292 // See https://github.com/rhboot/shim/blob/main/SBAT.md for more.
293 unicodeNameEquals(v, shimSbatVarName) || // https://github.com/rhboot/shim/blob/20e4d9486fcae54ee44d2323ae342ffe68c920e6/include/sbat.h#L9-L12
294 // Skip parsing new MokListTrusted section logged by shim.
295 // See https://github.com/rhboot/shim/blob/main/MokVars.txt for more.
296 unicodeNameEquals(v, shimMokListTrustedVarName)) { // https://github.com/rhboot/shim/blob/4e513405b4f1641710115780d19dcec130c5208f/mok.c#L169-L182
297 return UEFIVariableAuthority{}, nil
298 }
299 certs, err := parseEfiSignature(v.VariableData)
300 return UEFIVariableAuthority{Certs: certs}, err
301 }
302
303 func unicodeNameEquals(v UEFIVariableData, comp []uint16) bool {
304 if len(v.UnicodeName) != len(comp) {
305 return false
306 }
307 for i, v := range v.UnicodeName {
308 if v != comp[i] {
309 return false
310 }
311 }
312 return true
313 }
314
315 // efiSignatureData represents the EFI_SIGNATURE_DATA type.
316 // See section "31.4.1 Signature Database" in the specification for more information.
317 type efiSignatureData struct {
318 SignatureOwner efiGUID
319 SignatureData []byte // []int8
320 }
321
322 // efiSignatureList represents the EFI_SIGNATURE_LIST type.
323 // See section "31.4.1 Signature Database" in the specification for more information.
324 type efiSignatureListHeader struct {
325 SignatureType efiGUID
326 SignatureListSize uint32
327 SignatureHeaderSize uint32
328 SignatureSize uint32
329 }
330
331 type efiSignatureList struct {
332 Header efiSignatureListHeader
333 SignatureData []byte
334 Signatures []byte
335 }
336
337 const (
338 // efiGUIDSize is the binary size of efiGUID:
339 // uint32(4) + uint16(2) + uint16(2) + [8]byte(8) = 16 bytes.
340 efiGUIDSize = 16
341
342 // efiSignatureListHeaderSize is the binary size of efiSignatureListHeader:
343 // efiGUID(16) + SignatureListSize uint32(4) + SignatureHeaderSize uint32(4) + SignatureSize uint32(4) = 28 bytes.
344 efiSignatureListHeaderSize = efiGUIDSize + 4 + 4 + 4
345 )
346
347 // parseEfiSignatureList parses a EFI_SIGNATURE_LIST structure.
348 // The structure and related GUIDs are defined at:
349 // https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_final.pdf#page=1790
350 func parseEfiSignatureList(b []byte) ([]x509.Certificate, [][]byte, error) {
351 if len(b) < efiSignatureListHeaderSize {
352 // Being passed an empty signature list here appears to be valid
353 return nil, nil, nil
354 }
355 signatures := efiSignatureList{}
356 buf := bytes.NewReader(b)
357 var certificates []x509.Certificate
358 var hashes [][]byte
359
360 for buf.Len() > 0 {
361 err := binary.Read(buf, binary.LittleEndian, &signatures.Header)
362 if err != nil {
363 return nil, nil, err
364 }
365
366 if signatures.Header.SignatureHeaderSize > maxDataLen {
367 return nil, nil, fmt.Errorf("signature header too large: %d > %d", signatures.Header.SignatureHeaderSize, maxDataLen)
368 }
369 if signatures.Header.SignatureListSize > maxDataLen {
370 return nil, nil, fmt.Errorf("signature list too large: %d > %d", signatures.Header.SignatureListSize, maxDataLen)
371 }
372 // Guard against uint32 underflow and OOM: SignatureListSize and SignatureSize
373 // are attacker-controlled fields. Without these checks, the subtractions
374 // below (SignatureListSize-efiSignatureListHeaderSize, SignatureSize-efiGUIDSize) would wrap around,
375 // causing an infinite loop or an OOM panic via make([]byte, ~4 GiB).
376 if signatures.Header.SignatureListSize < efiSignatureListHeaderSize {
377 return nil, nil, fmt.Errorf("SignatureListSize %d is smaller than the minimum header size of %d", signatures.Header.SignatureListSize, efiSignatureListHeaderSize)
378 }
379 if signatures.Header.SignatureSize < efiGUIDSize {
380 return nil, nil, fmt.Errorf("SignatureSize %d is smaller than the minimum entry size of %d", signatures.Header.SignatureSize, efiGUIDSize)
381 }
382 // Guard against OOM: SignatureSize is unbounded by the prior checks, allowing
383 // a crafted event log to set SignatureSize=0xFFFFFFFF while providing only a
384 // few bytes of actual data, causing make([]byte, SignatureSize-efiGUIDSize) to
385 // attempt a ~4 GiB allocation before binary.Read fails.
386 // SignatureSize must not exceed the remaining space within the signature list.
387 remainingListSize := signatures.Header.SignatureListSize - efiSignatureListHeaderSize
388 if signatures.Header.SignatureSize > remainingListSize {
389 return nil, nil, fmt.Errorf("SignatureSize %d exceeds remaining signature list space %d", signatures.Header.SignatureSize, remainingListSize)
390 }
391 // Guard against hash injection via oversized SignatureHeaderSize.
392 // Per UEFI spec section 31.4.1, SignatureHeaderSize bytes of vendor data
393 // appear between the fixed header and the actual signature entries.
394 // SignatureHeaderSize must not consume the entire remaining list space.
395 if signatures.Header.SignatureHeaderSize >= remainingListSize {
396 return nil, nil, fmt.Errorf("SignatureHeaderSize %d exceeds remaining signature list space %d", signatures.Header.SignatureHeaderSize, remainingListSize)
397 }
398 // Skip the vendor-specific SignatureHeader bytes per UEFI spec section 31.4.1.
399 // Without this, vendor bytes are misread as signature entries, allowing a
400 // crafted event log to inject arbitrary hashes into the trusted hash list.
401 if signatures.Header.SignatureHeaderSize > 0 {
402 if _, err := buf.Seek(int64(signatures.Header.SignatureHeaderSize), io.SeekCurrent); err != nil {
403 return nil, nil, fmt.Errorf("seeking past signature vendor header: %w", err)
404 }
405 }
406
407 signatureType := signatures.Header.SignatureType
408 switch signatureType {
409 case certX509SigGUID: // X509 certificate
410 for sigOffset := int(signatures.Header.SignatureHeaderSize); uint32(sigOffset) < signatures.Header.SignatureListSize-efiSignatureListHeaderSize; {
411 signature := efiSignatureData{}
412 signature.SignatureData = make([]byte, signatures.Header.SignatureSize-efiGUIDSize)
413 err := binary.Read(buf, binary.LittleEndian, &signature.SignatureOwner)
414 if err != nil {
415 return nil, nil, err
416 }
417 err = binary.Read(buf, binary.LittleEndian, &signature.SignatureData)
418 if err != nil {
419 return nil, nil, err
420 }
421 cert, err := x509.ParseCertificate(signature.SignatureData)
422 if err != nil {
423 return nil, nil, err
424 }
425 sigOffset += int(signatures.Header.SignatureSize)
426 certificates = append(certificates, *cert)
427 }
428 case hashSHA256SigGUID: // SHA256
429 for sigOffset := int(signatures.Header.SignatureHeaderSize); uint32(sigOffset) < signatures.Header.SignatureListSize-efiSignatureListHeaderSize; {
430 signature := efiSignatureData{}
431 signature.SignatureData = make([]byte, signatures.Header.SignatureSize-efiGUIDSize)
432 err := binary.Read(buf, binary.LittleEndian, &signature.SignatureOwner)
433 if err != nil {
434 return nil, nil, err
435 }
436 err = binary.Read(buf, binary.LittleEndian, &signature.SignatureData)
437 if err != nil {
438 return nil, nil, err
439 }
440 hashes = append(hashes, signature.SignatureData)
441 sigOffset += int(signatures.Header.SignatureSize)
442 }
443 case keyRSA2048SigGUID:
444 err = errors.New("unhandled RSA2048 key")
445 case certRSA2048SHA256SigGUID:
446 err = errors.New("unhandled RSA2048-SHA256 key")
447 case hashSHA1SigGUID:
448 err = errors.New("unhandled SHA1 hash")
449 case certRSA2048SHA1SigGUID:
450 err = errors.New("unhandled RSA2048-SHA1 key")
451 case hashSHA224SigGUID:
452 err = errors.New("unhandled SHA224 hash")
453 case hashSHA384SigGUID:
454 err = errors.New("unhandled SHA384 hash")
455 case hashSHA512SigGUID:
456 err = errors.New("unhandled SHA512 hash")
457 case certHashSHA256SigGUID:
458 err = errors.New("unhandled X509-SHA256 hash metadata")
459 case certHashSHA384SigGUID:
460 err = errors.New("unhandled X509-SHA384 hash metadata")
461 case certHashSHA512SigGUID:
462 err = errors.New("unhandled X509-SHA512 hash metadata")
463 default:
464 err = fmt.Errorf("unhandled signature type %s", signatureType)
465 }
466 if err != nil {
467 return nil, nil, err
468 }
469 }
470 return certificates, hashes, nil
471 }
472
473 // EFISignatureData represents the EFI_SIGNATURE_DATA type.
474 // See section "31.4.1 Signature Database" in the specification
475 // for more information.
476 type EFISignatureData struct {
477 SignatureOwner efiGUID
478 SignatureData []byte // []int8
479 }
480
481 func parseEfiSignature(b []byte) ([]x509.Certificate, error) {
482 var certificates []x509.Certificate
483
484 if len(b) < efiGUIDSize {
485 return nil, fmt.Errorf("invalid signature: buffer smaller than header (%d < %d)", len(b), efiGUIDSize)
486 }
487
488 buf := bytes.NewReader(b)
489 signature := EFISignatureData{}
490 signature.SignatureData = make([]byte, len(b)-efiGUIDSize)
491
492 if err := binary.Read(buf, binary.LittleEndian, &signature.SignatureOwner); err != nil {
493 return certificates, err
494 }
495 if err := binary.Read(buf, binary.LittleEndian, &signature.SignatureData); err != nil {
496 return certificates, err
497 }
498
499 cert, err := x509.ParseCertificate(signature.SignatureData)
500 if err == nil {
501 certificates = append(certificates, *cert)
502 } else {
503 // A bug in shim may cause an event to be missing the SignatureOwner GUID.
504 // We handle this, but signal back to the caller using ErrSigMissingGUID.
505 var err2 error
506 cert, err2 = x509.ParseCertificate(b)
507 if err2 == nil {
508 certificates = append(certificates, *cert)
509 err = ErrSigMissingGUID
510 }
511 }
512 return certificates, err
513 }
514
515 // EFIDevicePathElement represents an EFI_DEVICE_PATH_ELEMENT structure.
516 type EFIDevicePathElement struct {
517 Type EFIDeviceType
518 Subtype uint8
519 Data []byte
520 }
521
522 // EFIImageLoad describes an EFI_IMAGE_LOAD_EVENT structure.
523 type EFIImageLoad struct {
524 Header EFIImageLoadHeader
525 DevPathData []byte
526 }
527
528 // EFIImageLoadHeader represents the EFI_IMAGE_LOAD_EVENT structure.
529 type EFIImageLoadHeader struct {
530 LoadAddr uint64
531 Length uint64
532 LinkAddr uint64
533 DevicePathLen uint64
534 }
535
536 func parseDevicePathElement(r *io.LimitedReader) (EFIDevicePathElement, error) {
537 var (
538 out EFIDevicePathElement
539 dataLen uint16
540 )
541
542 if err := binary.Read(r, binary.LittleEndian, &out.Type); err != nil {
543 return EFIDevicePathElement{}, fmt.Errorf("reading type: %v", err)
544 }
545 if err := binary.Read(r, binary.LittleEndian, &out.Subtype); err != nil {
546 return EFIDevicePathElement{}, fmt.Errorf("reading subtype: %v", err)
547 }
548 if err := binary.Read(r, binary.LittleEndian, &dataLen); err != nil {
549 return EFIDevicePathElement{}, fmt.Errorf("reading data len: %v", err)
550 }
551 if dataLen > maxNameLen {
552 return EFIDevicePathElement{}, fmt.Errorf("device path data too long: %d > %d", dataLen, maxNameLen)
553 }
554 if dataLen < 4 {
555 return EFIDevicePathElement{}, fmt.Errorf("device path data too short: %d < %d", dataLen, 4)
556 }
557 if int64(dataLen-4) > r.N {
558 return EFIDevicePathElement{}, fmt.Errorf("device path element data (%d bytes) larger than remaining capacity (%d bytes)", dataLen-4, r.N)
559 }
560 out.Data = make([]byte, dataLen-4)
561 if err := binary.Read(r, binary.LittleEndian, &out.Data); err != nil {
562 return EFIDevicePathElement{}, fmt.Errorf("reading data: %v", err)
563 }
564 return out, nil
565 }
566
567 // DevicePath returns the device path elements from the EFI_IMAGE_LOAD_EVENT structure.
568 func (h *EFIImageLoad) DevicePath() ([]EFIDevicePathElement, error) {
569 var (
570 r = &io.LimitedReader{R: bytes.NewReader(h.DevPathData), N: int64(len(h.DevPathData))}
571 out []EFIDevicePathElement
572 )
573
574 for r.N > 0 {
575 e, err := parseDevicePathElement(r)
576 if err != nil {
577 return nil, err
578 }
579 if e.Type == EndDeviceArrayMarker {
580 return out, nil
581 }
582
583 out = append(out, e)
584 }
585
586 return out, nil
587 }
588
589 // ParseEFIImageLoad parses an EFI_IMAGE_LOAD_EVENT structure.
590 //
591 // https://trustedcomputinggroup.org/wp-content/uploads/TCG_EFI_Platform_1_22_Final_-v15.pdf#page=17
592 func ParseEFIImageLoad(data []byte) (ret EFIImageLoad, err error) {
593 r := &io.LimitedReader{R: bytes.NewReader(data), N: int64(len(data))}
594 err = binary.Read(r, binary.LittleEndian, &ret.Header)
595 if err != nil {
596 return
597 }
598 if ret.Header.DevicePathLen > maxNameLen {
599 return EFIImageLoad{}, fmt.Errorf("device path structure too long: %d > %d", ret.Header.DevicePathLen, maxNameLen)
600 }
601 if int64(ret.Header.DevicePathLen) > r.N {
602 return EFIImageLoad{}, fmt.Errorf("device path structure size (%d bytes) larger than remaining capacity (%d bytes)", ret.Header.DevicePathLen, r.N)
603 }
604 ret.DevPathData = make([]byte, ret.Header.DevicePathLen)
605 err = binary.Read(r, binary.LittleEndian, &ret.DevPathData)
606 return
607 }