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