]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c
Add security package to repository.
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasureBootLib / DxeTpmMeasureBootLib.c
... / ...
CommitLineData
1/** @file\r
2 The library instance provides security service of TPM measure boot. \r
3\r
4Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/TcgService.h>\r
18#include <Protocol/FirmwareVolume2.h>\r
19#include <Protocol/BlockIo.h>\r
20#include <Protocol/DiskIo.h>\r
21#include <Protocol/DevicePathToText.h>\r
22\r
23#include <Library/BaseLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
26#include <Library/MemoryAllocationLib.h>\r
27#include <Library/DevicePathLib.h>\r
28#include <Library/UefiBootServicesTableLib.h>\r
29#include <Library/BaseCryptLib.h>\r
30#include <Library/PeCoffLib.h>\r
31#include <Library/SecurityManagementLib.h>\r
32\r
33//\r
34// Flag to check GPT partition. It only need be measured once.\r
35//\r
36BOOLEAN mMeasureGptTableFlag = FALSE;\r
37EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};\r
38UINTN mMeasureGptCount = 0;\r
39\r
40/**\r
41 Reads contents of a PE/COFF image in memory buffer.\r
42\r
43 @param FileHandle Pointer to the file handle to read the PE/COFF image.\r
44 @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
45 @param ReadSize On input, the size in bytes of the requested read operation. \r
46 On output, the number of bytes actually read.\r
47 @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
48 \r
49 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size \r
50**/\r
51EFI_STATUS\r
52EFIAPI\r
53ImageRead (\r
54 IN VOID *FileHandle,\r
55 IN UINTN FileOffset,\r
56 IN OUT UINTN *ReadSize,\r
57 OUT VOID *Buffer\r
58 )\r
59{\r
60 CopyMem (Buffer, (UINT8 *)((UINTN) FileHandle + FileOffset), *ReadSize);\r
61 return EFI_SUCCESS;\r
62}\r
63\r
64/**\r
65 Measure GPT table data into TPM log.\r
66\r
67 @param TcgProtocol Pointer to the located TCG protocol instance.\r
68 @param GptHandle Handle that GPT partition was installed.\r
69\r
70 @retval EFI_SUCCESS Successfully measure GPT table.\r
71 @retval EFI_UNSUPPORTED Not support GPT table on the given handle.\r
72 @retval EFI_DEVICE_ERROR Can't get GPT table because device error.\r
73 @retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table.\r
74 @retval other error value\r
75**/\r
76EFI_STATUS\r
77EFIAPI\r
78TcgMeasureGptTable (\r
79 IN EFI_TCG_PROTOCOL *TcgProtocol,\r
80 IN EFI_HANDLE GptHandle\r
81 )\r
82{\r
83 EFI_STATUS Status;\r
84 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
85 EFI_DISK_IO_PROTOCOL *DiskIo;\r
86 EFI_PARTITION_TABLE_HEADER *PrimaryHeader;\r
87 EFI_PARTITION_ENTRY *PartitionEntry;\r
88 UINT8 *EntryPtr;\r
89 UINTN NumberOfPartition;\r
90 UINT32 Index;\r
91 TCG_PCR_EVENT *TcgEvent;\r
92 EFI_GPT_DATA *GptData;\r
93 UINT32 EventSize;\r
94 UINT32 EventNumber;\r
95 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
96\r
97 if (mMeasureGptCount > 0) {\r
98 return EFI_SUCCESS;\r
99 }\r
100\r
101 Status = gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo);\r
102 if (EFI_ERROR (Status)) {\r
103 return EFI_UNSUPPORTED;\r
104 }\r
105 Status = gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID**)&DiskIo);\r
106 if (EFI_ERROR (Status)) {\r
107 return EFI_UNSUPPORTED;\r
108 }\r
109 //\r
110 // Read the EFI Partition Table Header\r
111 // \r
112 PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *) AllocatePool (BlockIo->Media->BlockSize);\r
113 if (PrimaryHeader == NULL) {\r
114 return EFI_OUT_OF_RESOURCES;\r
115 } \r
116 Status = DiskIo->ReadDisk (\r
117 DiskIo,\r
118 BlockIo->Media->MediaId,\r
119 1 * BlockIo->Media->BlockSize,\r
120 BlockIo->Media->BlockSize,\r
121 (UINT8 *)PrimaryHeader\r
122 );\r
123 if (EFI_ERROR (Status)) {\r
124 DEBUG ((EFI_D_ERROR, "Failed to Read Partition Table Header!\n"));\r
125 FreePool (PrimaryHeader);\r
126 return EFI_DEVICE_ERROR;\r
127 } \r
128 //\r
129 // Read the partition entry.\r
130 //\r
131 EntryPtr = (UINT8 *)AllocatePool (PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry);\r
132 if (EntryPtr == NULL) {\r
133 FreePool (PrimaryHeader);\r
134 return EFI_OUT_OF_RESOURCES;\r
135 }\r
136 Status = DiskIo->ReadDisk (\r
137 DiskIo,\r
138 BlockIo->Media->MediaId,\r
139 MultU64x32(PrimaryHeader->PartitionEntryLBA, BlockIo->Media->BlockSize),\r
140 PrimaryHeader->NumberOfPartitionEntries * PrimaryHeader->SizeOfPartitionEntry,\r
141 EntryPtr\r
142 );\r
143 if (EFI_ERROR (Status)) {\r
144 FreePool (PrimaryHeader);\r
145 FreePool (EntryPtr);\r
146 return EFI_DEVICE_ERROR;\r
147 }\r
148 \r
149 //\r
150 // Count the valid partition\r
151 //\r
152 PartitionEntry = (EFI_PARTITION_ENTRY *)EntryPtr;\r
153 NumberOfPartition = 0;\r
154 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
155 if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mZeroGuid)) {\r
156 NumberOfPartition++; \r
157 }\r
158 PartitionEntry++;\r
159 }\r
160\r
161 //\r
162 // Parepare Data for Measurement\r
163 // \r
164 EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions) \r
165 + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r
166 TcgEvent = (TCG_PCR_EVENT *) AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT));\r
167 if (TcgEvent == NULL) {\r
168 FreePool (PrimaryHeader);\r
169 FreePool (EntryPtr);\r
170 return EFI_OUT_OF_RESOURCES;\r
171 }\r
172\r
173 TcgEvent->PCRIndex = 5;\r
174 TcgEvent->EventType = EV_EFI_GPT_EVENT;\r
175 TcgEvent->EventSize = EventSize;\r
176 GptData = (EFI_GPT_DATA *) TcgEvent->Event; \r
177\r
178 //\r
179 // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r
180 // \r
181 CopyMem ((UINT8 *)GptData, (UINT8*)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER));\r
182 GptData->NumberOfPartitions = NumberOfPartition;\r
183 //\r
184 // Copy the valid partition entry\r
185 //\r
186 PartitionEntry = (EFI_PARTITION_ENTRY*)EntryPtr;\r
187 NumberOfPartition = 0;\r
188 for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
189 if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mZeroGuid)) {\r
190 CopyMem (\r
191 (UINT8 *)&GptData->Partitions + NumberOfPartition * sizeof (EFI_PARTITION_ENTRY),\r
192 (UINT8 *)PartitionEntry,\r
193 sizeof (EFI_PARTITION_ENTRY)\r
194 );\r
195 NumberOfPartition++;\r
196 }\r
197 PartitionEntry++;\r
198 }\r
199\r
200 //\r
201 // Measure the GPT data\r
202 //\r
203 EventNumber = 1;\r
204 Status = TcgProtocol->HashLogExtendEvent (\r
205 TcgProtocol,\r
206 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) GptData,\r
207 (UINT64) TcgEvent->EventSize,\r
208 TPM_ALG_SHA,\r
209 TcgEvent,\r
210 &EventNumber,\r
211 &EventLogLastEntry\r
212 );\r
213 if (!EFI_ERROR (Status)) {\r
214 mMeasureGptCount++;\r
215 }\r
216\r
217 FreePool (PrimaryHeader);\r
218 FreePool (EntryPtr);\r
219 FreePool (TcgEvent);\r
220\r
221 return Status;\r
222}\r
223\r
224/**\r
225 Measure PE image into TPM log based on the authenticode image hashing in\r
226 PE/COFF Specification 8.0 Appendix A.\r
227\r
228 @param[in] TcgProtocol Pointer to the located TCG protocol instance.\r
229 @param[in] ImageAddress Start address of image buffer.\r
230 @param[in] ImageSize Image size\r
231 @param[in] LinkTimeBase Address that the image is loaded into memory.\r
232 @param[in] ImageType Image subsystem type.\r
233 @param[in] FilePath File path is corresponding to the input image.\r
234\r
235 @retval EFI_SUCCESS Successfully measure image.\r
236 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.\r
237 @retval other error value\r
238**/\r
239EFI_STATUS\r
240EFIAPI\r
241TcgMeasurePeImage (\r
242 IN EFI_TCG_PROTOCOL *TcgProtocol,\r
243 IN EFI_PHYSICAL_ADDRESS ImageAddress,\r
244 IN UINTN ImageSize,\r
245 IN UINTN LinkTimeBase,\r
246 IN UINT16 ImageType,\r
247 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
248 )\r
249{\r
250 EFI_STATUS Status;\r
251 TCG_PCR_EVENT *TcgEvent;\r
252 EFI_IMAGE_LOAD_EVENT *ImageLoad;\r
253 UINT32 FilePathSize;\r
254 VOID *Sha1Ctx;\r
255 UINTN CtxSize;\r
256 EFI_IMAGE_DOS_HEADER *DosHdr;\r
257 UINT32 PeCoffHeaderOffset;\r
258 EFI_IMAGE_SECTION_HEADER *Section;\r
259 UINT8 *HashBase;\r
260 UINTN HashSize;\r
261 UINTN SumOfBytesHashed;\r
262 EFI_IMAGE_SECTION_HEADER *SectionHeader;\r
263 UINTN Index, Pos;\r
264 UINT16 Magic;\r
265 UINT32 EventSize;\r
266 UINT32 EventNumber;\r
267 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
268 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
269\r
270 Status = EFI_SUCCESS;\r
271 ImageLoad = NULL;\r
272 SectionHeader = NULL;\r
273 Sha1Ctx = NULL;\r
274 FilePathSize = (UINT32) GetDevicePathSize (FilePath);\r
275\r
276 //\r
277 // Determine destination PCR by BootPolicy\r
278 //\r
279 EventSize = sizeof (*ImageLoad) - sizeof (ImageLoad->DevicePath) + FilePathSize;\r
280 TcgEvent = AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT));\r
281 if (TcgEvent == NULL) {\r
282 return EFI_OUT_OF_RESOURCES;\r
283 }\r
284\r
285 TcgEvent->EventSize = EventSize;\r
286 ImageLoad = (EFI_IMAGE_LOAD_EVENT *) TcgEvent->Event;\r
287\r
288 switch (ImageType) {\r
289 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
290 TcgEvent->EventType = EV_EFI_BOOT_SERVICES_APPLICATION;\r
291 TcgEvent->PCRIndex = 4;\r
292 break;\r
293 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
294 TcgEvent->EventType = EV_EFI_BOOT_SERVICES_DRIVER;\r
295 TcgEvent->PCRIndex = 2;\r
296 break;\r
297 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
298 TcgEvent->EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;\r
299 TcgEvent->PCRIndex = 2;\r
300 break;\r
301 default:\r
302 DEBUG ((\r
303 EFI_D_ERROR,\r
304 "TcgMeasurePeImage: Unknown subsystem type %d",\r
305 ImageType\r
306 ));\r
307 ASSERT (FALSE);\r
308 TcgEvent->EventType = ImageType;\r
309 Status = EFI_UNSUPPORTED;\r
310 goto Finish;\r
311 }\r
312\r
313 ImageLoad->ImageLocationInMemory = ImageAddress;\r
314 ImageLoad->ImageLengthInMemory = ImageSize;\r
315 ImageLoad->ImageLinkTimeAddress = LinkTimeBase;\r
316 ImageLoad->LengthOfDevicePath = FilePathSize;\r
317 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);\r
318\r
319 //\r
320 // Check PE/COFF image\r
321 //\r
322 DosHdr = (EFI_IMAGE_DOS_HEADER *) (UINTN) ImageAddress;\r
323 PeCoffHeaderOffset = 0;\r
324 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
325 PeCoffHeaderOffset = DosHdr->e_lfanew;\r
326 }\r
327 if (((EFI_TE_IMAGE_HEADER *)((UINT8 *) (UINTN) ImageAddress + PeCoffHeaderOffset))->Signature\r
328 == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
329 goto Finish;\r
330 }\r
331\r
332 //\r
333 // PE/COFF Image Measurement\r
334 //\r
335 // NOTE: The following codes/steps are based upon the authenticode image hashing in\r
336 // PE/COFF Specification 8.0 Appendix A.\r
337 //\r
338 //\r
339\r
340 // 1. Load the image header into memory.\r
341\r
342 // 2. Initialize a SHA hash context.\r
343 CtxSize = Sha1GetContextSize ();\r
344 Sha1Ctx = AllocatePool (CtxSize);\r
345 if (Sha1Ctx == NULL) {\r
346 Status = EFI_OUT_OF_RESOURCES;\r
347 goto Finish;\r
348 }\r
349\r
350 Sha1Init (Sha1Ctx);\r
351\r
352 //\r
353 // Measuring PE/COFF Image Header;\r
354 // But CheckSum field and SECURITY data directory (certificate) are excluded\r
355 //\r
356 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *) (UINTN) ImageAddress + PeCoffHeaderOffset);\r
357 Magic = Hdr.Pe32->OptionalHeader.Magic;\r
358 \r
359 //\r
360 // 3. Calculate the distance from the base of the image header to the image checksum address.\r
361 // 4. Hash the image header from its base to beginning of the image checksum.\r
362 //\r
363 HashBase = (UINT8 *) (UINTN) ImageAddress;\r
364 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
365 //\r
366 // Use PE32 offset\r
367 //\r
368 HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32->OptionalHeader.CheckSum) - HashBase);\r
369 } else {\r
370 //\r
371 // Use PE32+ offset\r
372 //\r
373 HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32Plus->OptionalHeader.CheckSum) - HashBase);\r
374 }\r
375\r
376 Sha1Update (Sha1Ctx, HashBase, HashSize);\r
377\r
378 //\r
379 // 5. Skip over the image checksum (it occupies a single ULONG).\r
380 // 6. Get the address of the beginning of the Cert Directory.\r
381 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.\r
382 //\r
383 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
384 //\r
385 // Use PE32 offset\r
386 //\r
387 HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
388 HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);\r
389 } else {\r
390 //\r
391 // Use PE32+ offset\r
392 // \r
393 HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
394 HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);\r
395 }\r
396\r
397 Sha1Update (Sha1Ctx, HashBase, HashSize);\r
398\r
399 //\r
400 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)\r
401 // 9. Hash everything from the end of the Cert Directory to the end of image header.\r
402 //\r
403 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
404 //\r
405 // Use PE32 offset\r
406 //\r
407 HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
408 HashSize = Hdr.Pe32->OptionalHeader.SizeOfHeaders -\r
409 (UINTN) ((UINT8 *)(&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1]) - (UINT8 *) (UINTN) ImageAddress);\r
410 } else {\r
411 //\r
412 // Use PE32+ offset\r
413 //\r
414 HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
415 HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders -\r
416 (UINTN) ((UINT8 *)(&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1]) - (UINT8 *) (UINTN) ImageAddress);\r
417 }\r
418 \r
419 Sha1Update (Sha1Ctx, HashBase, HashSize);\r
420\r
421 //\r
422 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header\r
423 //\r
424 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
425 //\r
426 // Use PE32 offset\r
427 //\r
428 SumOfBytesHashed = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r
429 } else {\r
430 //\r
431 // Use PE32+ offset\r
432 //\r
433 SumOfBytesHashed = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;\r
434 }\r
435\r
436 //\r
437 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER\r
438 // structures in the image. The 'NumberOfSections' field of the image\r
439 // header indicates how big the table should be. Do not include any\r
440 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.\r
441 //\r
442 SectionHeader = (EFI_IMAGE_SECTION_HEADER *)AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER) * Hdr.Pe32->FileHeader.NumberOfSections);\r
443 if (SectionHeader == NULL) {\r
444 Status = EFI_OUT_OF_RESOURCES;\r
445 goto Finish;\r
446 }\r
447\r
448 //\r
449 // 12. Using the 'PointerToRawData' in the referenced section headers as\r
450 // a key, arrange the elements in the table in ascending order. In other\r
451 // words, sort the section headers according to the disk-file offset of\r
452 // the section.\r
453 //\r
454 Section = (EFI_IMAGE_SECTION_HEADER *) (\r
455 (UINT8 *) (UINTN) ImageAddress +\r
456 PeCoffHeaderOffset +\r
457 sizeof(UINT32) +\r
458 sizeof(EFI_IMAGE_FILE_HEADER) +\r
459 Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r
460 );\r
461 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {\r
462 Pos = Index;\r
463 while ((Pos > 0) && (Section->PointerToRawData < SectionHeader[Pos - 1].PointerToRawData)) {\r
464 CopyMem (&SectionHeader[Pos], &SectionHeader[Pos - 1], sizeof(EFI_IMAGE_SECTION_HEADER));\r
465 Pos--;\r
466 }\r
467 CopyMem (&SectionHeader[Pos], Section, sizeof(EFI_IMAGE_SECTION_HEADER));\r
468 Section += 1;\r
469 }\r
470\r
471 //\r
472 // 13. Walk through the sorted table, bring the corresponding section\r
473 // into memory, and hash the entire section (using the 'SizeOfRawData'\r
474 // field in the section header to determine the amount of data to hash).\r
475 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .\r
476 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.\r
477 //\r
478 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {\r
479 Section = (EFI_IMAGE_SECTION_HEADER *) &SectionHeader[Index];\r
480 if (Section->SizeOfRawData == 0) {\r
481 continue;\r
482 }\r
483 HashBase = (UINT8 *) (UINTN) ImageAddress + Section->PointerToRawData;\r
484 HashSize = (UINTN) Section->SizeOfRawData;\r
485\r
486 Sha1Update (Sha1Ctx, HashBase, HashSize);\r
487\r
488 SumOfBytesHashed += HashSize;\r
489 }\r
490\r
491 //\r
492 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra\r
493 // data in the file that needs to be added to the hash. This data begins\r
494 // at file offset SUM_OF_BYTES_HASHED and its length is:\r
495 // FileSize - (CertDirectory->Size)\r
496 //\r
497 if (ImageSize > SumOfBytesHashed) {\r
498 HashBase = (UINT8 *) (UINTN) ImageAddress + SumOfBytesHashed;\r
499 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
500 //\r
501 // Use PE32 offset\r
502 //\r
503 HashSize = (UINTN)(ImageSize -\r
504 Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size -\r
505 SumOfBytesHashed);\r
506 } else {\r
507 //\r
508 // Use PE32+ offset\r
509 //\r
510 HashSize = (UINTN)(ImageSize -\r
511 Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size -\r
512 SumOfBytesHashed); \r
513 }\r
514\r
515 Sha1Update (Sha1Ctx, HashBase, HashSize);\r
516 }\r
517\r
518 //\r
519 // 17. Finalize the SHA hash.\r
520 //\r
521 Sha1Final (Sha1Ctx, (UINT8 *)&TcgEvent->Digest);\r
522\r
523 //\r
524 // Log the PE data\r
525 //\r
526 EventNumber = 1;\r
527 Status = TcgProtocol->HashLogExtendEvent (\r
528 TcgProtocol,\r
529 (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) NULL,\r
530 0,\r
531 TPM_ALG_SHA,\r
532 TcgEvent,\r
533 &EventNumber,\r
534 &EventLogLastEntry\r
535 );\r
536\r
537Finish:\r
538 FreePool (TcgEvent);\r
539\r
540 if (SectionHeader != NULL) {\r
541 FreePool (SectionHeader);\r
542 }\r
543\r
544 if (Sha1Ctx != NULL ) {\r
545 FreePool (Sha1Ctx);\r
546 }\r
547 return Status;\r
548}\r
549\r
550/**\r
551 The security handler is used to abstract platform-specific policy \r
552 from the DXE core response to an attempt to use a file that returns a \r
553 given status for the authentication check from the section extraction protocol. \r
554\r
555 The possible responses in a given SAP implementation may include locking \r
556 flash upon failure to authenticate, attestation logging for all signed drivers, \r
557 and other exception operations. The File parameter allows for possible logging \r
558 within the SAP of the driver.\r
559\r
560 If File is NULL, then EFI_INVALID_PARAMETER is returned.\r
561\r
562 If the file specified by File with an authentication status specified by \r
563 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.\r
564\r
565 If the file specified by File with an authentication status specified by \r
566 AuthenticationStatus is not safe for the DXE Core to use under any circumstances, \r
567 then EFI_ACCESS_DENIED is returned.\r
568\r
569 If the file specified by File with an authentication status specified by \r
570 AuthenticationStatus is not safe for the DXE Core to use right now, but it \r
571 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is \r
572 returned.\r
573\r
574 @param[in, out] AuthenticationStatus This is the authentication status returned\r
575 from the securitymeasurement services for the\r
576 input file.\r
577 @param[in] File This is a pointer to the device path of the file that is\r
578 being dispatched. This will optionally be used for logging.\r
579 @param[in] FileBuffer File buffer matches the input file device path.\r
580 @param[in] FileSize Size of File buffer matches the input file device path.\r
581\r
582 @retval EFI_SUCCESS The file specified by File did authenticate, and the\r
583 platform policy dictates that the DXE Core may use File.\r
584 @retval EFI_INVALID_PARAMETER File is NULL.\r
585 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
586 the platform policy dictates that File should be placed\r
587 in the untrusted state. A file may be promoted from\r
588 the untrusted to the trusted state at a future time\r
589 with a call to the Trust() DXE Service.\r
590 @retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and\r
591 the platform policy dictates that File should not be\r
592 used for any purpose.\r
593\r
594**/\r
595EFI_STATUS\r
596EFIAPI\r
597DxeTpmMeasureBootHandler (\r
598 IN OUT UINT32 AuthenticationStatus,\r
599 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
600 IN VOID *FileBuffer OPTIONAL,\r
601 IN UINTN FileSize OPTIONAL\r
602 )\r
603{\r
604 EFI_TCG_PROTOCOL *TcgProtocol;\r
605 EFI_STATUS Status;\r
606 TCG_EFI_BOOT_SERVICE_CAPABILITY ProtocolCapability;\r
607 UINT32 TCGFeatureFlags;\r
608 EFI_PHYSICAL_ADDRESS EventLogLocation;\r
609 EFI_PHYSICAL_ADDRESS EventLogLastEntry;\r
610 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
611 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
612 EFI_HANDLE Handle;\r
613 BOOLEAN ApplicationRequired;\r
614 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
615\r
616 if (File == NULL) {\r
617 return EFI_INVALID_PARAMETER;\r
618 }\r
619\r
620 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
621 if (EFI_ERROR (Status)) {\r
622 //\r
623 // TCG protocol is not installed. So, TPM is not present.\r
624 // Don't do any measurement, and directly return EFI_SUCCESS.\r
625 //\r
626 return EFI_SUCCESS;\r
627 }\r
628\r
629 ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);\r
630 Status = TcgProtocol->StatusCheck (\r
631 TcgProtocol, \r
632 &ProtocolCapability,\r
633 &TCGFeatureFlags,\r
634 &EventLogLocation,\r
635 &EventLogLastEntry\r
636 );\r
637 if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag) {\r
638 //\r
639 // TPM device doesn't work or activate.\r
640 //\r
641 return EFI_SUCCESS;\r
642 }\r
643\r
644 //\r
645 // Copy File Device Path\r
646 //\r
647 OrigDevicePathNode = DuplicateDevicePath (File);\r
648 ASSERT (OrigDevicePathNode != NULL);\r
649 \r
650 //\r
651 // 1. Check whether this device path support BlockIo protocol.\r
652 // Is so, this device path may be a GPT device path.\r
653 //\r
654 DevicePathNode = OrigDevicePathNode;\r
655 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);\r
656 if (!EFI_ERROR (Status) && !mMeasureGptTableFlag) {\r
657 //\r
658 // Find the gpt partion on the given devicepath\r
659 //\r
660 DevicePathNode = OrigDevicePathNode;\r
661 while (!IsDevicePathEnd (DevicePathNode)) {\r
662 //\r
663 // Find the Gpt partition\r
664 //\r
665 if (DevicePathType (DevicePathNode) == MEDIA_DEVICE_PATH &&\r
666 DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) {\r
667 //\r
668 // Check whether it is a gpt partition or not\r
669 // \r
670 if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER && \r
671 ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) {\r
672\r
673 //\r
674 // Change the partition device path to its parent device path (disk) and get the handle.\r
675 //\r
676 DevicePathNode->Type = END_DEVICE_PATH_TYPE;\r
677 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
678 DevicePathNode = OrigDevicePathNode;\r
679 Status = gBS->LocateDevicePath (\r
680 &gEfiDiskIoProtocolGuid,\r
681 &DevicePathNode,\r
682 &Handle\r
683 );\r
684 if (!EFI_ERROR (Status)) {\r
685 //\r
686 // Measure GPT disk.\r
687 //\r
688 Status = TcgMeasureGptTable (TcgProtocol, Handle);\r
689 if (!EFI_ERROR (Status)) {\r
690 //\r
691 // GPT disk check done.\r
692 //\r
693 mMeasureGptTableFlag = TRUE;\r
694 }\r
695 }\r
696 FreePool (OrigDevicePathNode);\r
697 OrigDevicePathNode = DuplicateDevicePath (File);\r
698 ASSERT (OrigDevicePathNode != NULL);\r
699 break;\r
700 }\r
701 }\r
702 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
703 }\r
704 }\r
705 \r
706 //\r
707 // 2. Measure PE image.\r
708 //\r
709 ApplicationRequired = FALSE;\r
710\r
711 //\r
712 // Check whether this device path support FV2 protocol.\r
713 //\r
714 DevicePathNode = OrigDevicePathNode;\r
715 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
716 if (!EFI_ERROR (Status)) {\r
717 //\r
718 // Don't check FV image, and directly return EFI_SUCCESS.\r
719 // It can be extended to the specific FV authentication according to the different requirement.\r
720 //\r
721 if (IsDevicePathEnd (DevicePathNode)) {\r
722 return EFI_SUCCESS;\r
723 }\r
724 //\r
725 // The image from Firmware image will not be mearsured.\r
726 // Current policy doesn't measure PeImage from Firmware if it is driver\r
727 // If the got PeImage is application, it will be still be measured.\r
728 //\r
729 ApplicationRequired = TRUE;\r
730 }\r
731 \r
732 //\r
733 // File is not found.\r
734 //\r
735 if (FileBuffer == NULL) {\r
736 Status = EFI_SECURITY_VIOLATION;\r
737 goto Finish;\r
738 }\r
739\r
740 //\r
741 // Measure PE Image\r
742 //\r
743 DevicePathNode = OrigDevicePathNode;\r
744 ZeroMem (&ImageContext, sizeof (ImageContext));\r
745 ImageContext.Handle = (VOID *) FileBuffer;\r
746 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) ImageRead;\r
747\r
748 //\r
749 // Get information about the image being loaded\r
750 //\r
751 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
752 if (EFI_ERROR (Status)) {\r
753 //\r
754 // The information can't be got from the invalid PeImage\r
755 //\r
756 goto Finish;\r
757 }\r
758 \r
759 //\r
760 // Measure only application if Application flag is set\r
761 // Measure drivers and applications if Application flag is not set\r
762 //\r
763 if ((!ApplicationRequired) || \r
764 (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) { \r
765 //\r
766 // Print the image path to be measured.\r
767 // \r
768 DEBUG_CODE_BEGIN ();\r
769 CHAR16 *ToText;\r
770 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevPathToText;\r
771 Status = gBS->LocateProtocol (\r
772 &gEfiDevicePathToTextProtocolGuid,\r
773 NULL,\r
774 (VOID **) &DevPathToText\r
775 );\r
776 if (!EFI_ERROR (Status)) {\r
777 ToText = DevPathToText->ConvertDevicePathToText (\r
778 DevicePathNode,\r
779 FALSE,\r
780 TRUE\r
781 );\r
782 if (ToText != NULL) {\r
783 DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));\r
784 }\r
785 }\r
786 DEBUG_CODE_END ();\r
787\r
788 //\r
789 // Measure PE image into TPM log.\r
790 //\r
791 Status = TcgMeasurePeImage (\r
792 TcgProtocol,\r
793 (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer, \r
794 FileSize, \r
795 (UINTN) ImageContext.ImageAddress, \r
796 ImageContext.ImageType, \r
797 DevicePathNode\r
798 );\r
799 }\r
800\r
801 //\r
802 // Done, free the allocated resource.\r
803 //\r
804Finish:\r
805 FreePool (OrigDevicePathNode);\r
806\r
807 return Status;\r
808}\r
809\r
810/**\r
811 Register the security handler to provide TPM measure boot service.\r
812\r
813 @param ImageHandle ImageHandle of the loaded driver.\r
814 @param SystemTable Pointer to the EFI System Table.\r
815\r
816 @retval EFI_SUCCESS Register successfully.\r
817 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r
818**/\r
819EFI_STATUS\r
820EFIAPI\r
821DxeTpmMeasureBootLibConstructor (\r
822 IN EFI_HANDLE ImageHandle,\r
823 IN EFI_SYSTEM_TABLE *SystemTable\r
824 )\r
825{\r
826 return RegisterSecurityHandler (\r
827 DxeTpmMeasureBootHandler,\r
828 EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED\r
829 );\r
830}\r