]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Pkcs7Verify/Pkcs7VerifyDxe/Pkcs7VerifyDxe.c
SecurityPkg/Pkcs7VerifyDxe: Add format check in DB list contents
[mirror_edk2.git] / SecurityPkg / Pkcs7Verify / Pkcs7VerifyDxe / Pkcs7VerifyDxe.c
CommitLineData
2d093499
QL
1/** @file\r
2 Pkcs7Verify Driver to produce the UEFI PKCS7 Verification Protocol.\r
3\r
4 The driver will produce the UEFI PKCS7 Verification Protocol which is used to\r
5 verify data signed using PKCS7 structure. The PKCS7 data to be verified must\r
6 be ASN.1 (DER) encoded.\r
7\r
76b35710 8Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
2d093499
QL
9This program and the accompanying materials\r
10are licensed and made available under the terms and conditions of the BSD License\r
11which accompanies this distribution. The full text of the license may be found at\r
12http://opensource.org/licenses/bsd-license.php\r
13\r
14THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include <Library/BaseLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Library/BaseCryptLib.h>\r
24#include <Protocol/Pkcs7Verify.h>\r
25\r
26#define MAX_DIGEST_SIZE SHA512_DIGEST_SIZE\r
27\r
28/**\r
29 Calculates the hash of the given data based on the specified hash GUID.\r
30\r
31 @param[in] Data Pointer to the data buffer to be hashed.\r
32 @param[in] DataSize The size of data buffer in bytes.\r
33 @param[in] CertGuid The GUID to identify the hash algorithm to be used.\r
34 @param[out] HashValue Pointer to a buffer that receives the hash result.\r
35\r
36 @retval TRUE Data hash calculation succeeded.\r
37 @retval FALSE Data hash calculation failed.\r
38\r
39**/\r
40BOOLEAN\r
41CalculateDataHash (\r
42 IN VOID *Data,\r
43 IN UINTN DataSize,\r
44 IN EFI_GUID *CertGuid,\r
45 OUT UINT8 *HashValue\r
46 )\r
47{\r
48 BOOLEAN Status;\r
49 VOID *HashCtx;\r
50 UINTN CtxSize;\r
51\r
52 Status = FALSE;\r
53 HashCtx = NULL;\r
54\r
55 if (CompareGuid (CertGuid, &gEfiCertSha1Guid)) {\r
56 //\r
57 // SHA-1 Hash\r
58 //\r
59 CtxSize = Sha1GetContextSize ();\r
60 HashCtx = AllocatePool (CtxSize);\r
61 if (HashCtx == NULL) {\r
62 goto _Exit;\r
63 }\r
64 Status = Sha1Init (HashCtx);\r
65 Status = Sha1Update (HashCtx, Data, DataSize);\r
66 Status = Sha1Final (HashCtx, HashValue);\r
67\r
68 } else if (CompareGuid (CertGuid, &gEfiCertSha256Guid)) {\r
69 //\r
70 // SHA256 Hash\r
71 //\r
72 CtxSize = Sha256GetContextSize ();\r
73 HashCtx = AllocatePool (CtxSize);\r
74 if (HashCtx == NULL) {\r
75 goto _Exit;\r
76 }\r
77 Status = Sha256Init (HashCtx);\r
78 Status = Sha256Update (HashCtx, Data, DataSize);\r
79 Status = Sha256Final (HashCtx, HashValue);\r
80\r
81 } else if (CompareGuid (CertGuid, &gEfiCertSha384Guid)) {\r
82 //\r
83 // SHA384 Hash\r
84 //\r
85 CtxSize = Sha384GetContextSize ();\r
86 HashCtx = AllocatePool (CtxSize);\r
87 if (HashCtx == NULL) {\r
88 goto _Exit;\r
89 }\r
90 Status = Sha384Init (HashCtx);\r
91 Status = Sha384Update (HashCtx, Data, DataSize);\r
92 Status = Sha384Final (HashCtx, HashValue);\r
93\r
94 } else if (CompareGuid (CertGuid, &gEfiCertSha512Guid)) {\r
95 //\r
96 // SHA512 Hash\r
97 //\r
98 CtxSize = Sha512GetContextSize ();\r
99 HashCtx = AllocatePool (CtxSize);\r
100 if (HashCtx == NULL) {\r
101 goto _Exit;\r
102 }\r
103 Status = Sha512Init (HashCtx);\r
104 Status = Sha512Update (HashCtx, Data, DataSize);\r
105 Status = Sha512Final (HashCtx, HashValue);\r
106 }\r
107\r
108_Exit:\r
109 if (HashCtx != NULL) {\r
110 FreePool (HashCtx);\r
111 }\r
112\r
113 return Status;\r
114}\r
115\r
116/**\r
117 Check whether the hash of data content is revoked by the revocation database.\r
118\r
119 @param[in] Content Pointer to the content buffer that is searched for.\r
120 @param[in] ContentSize The size of data content in bytes.\r
121 @param[in] RevokedDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
122 structure which contains list of X.509 certificates\r
123 of revoked signers and revoked content hashes.\r
124\r
125 @return TRUE The matched content hash is found in the revocation database.\r
126 @return FALSE The matched content hash is not found in the revocation database.\r
127\r
128**/\r
129BOOLEAN\r
130IsContentHashRevoked (\r
131 IN UINT8 *Content,\r
132 IN UINTN ContentSize,\r
133 IN EFI_SIGNATURE_LIST **RevokedDb\r
134 )\r
135{\r
136 EFI_SIGNATURE_LIST *SigList;\r
137 EFI_SIGNATURE_DATA *SigData;\r
138 UINTN Index;\r
139 UINT8 HashVal[MAX_DIGEST_SIZE];\r
140 UINTN EntryIndex;\r
141 UINTN EntryCount;\r
142 BOOLEAN Status;\r
143\r
144 if (RevokedDb == NULL) {\r
145 return FALSE;\r
146 }\r
147\r
148 Status = FALSE;\r
149 //\r
150 // Check if any hash matching content hash can be found in RevokedDB\r
151 //\r
152 for (Index = 0; ; Index++) {\r
153 SigList = (EFI_SIGNATURE_LIST *)(RevokedDb[Index]);\r
154\r
155 //\r
156 // The list is terminated by a NULL pointer.\r
157 //\r
158 if (SigList == NULL) {\r
159 break;\r
160 }\r
161\r
162 //\r
163 // Calculate the digest of supplied data based on the signature hash type.\r
164 //\r
165 if (!CalculateDataHash (Content, ContentSize, &SigList->SignatureType, HashVal)) {\r
166 //\r
167 // Un-matched Hash GUID or other failure.\r
168 //\r
169 continue;\r
170 }\r
171\r
172 //\r
173 // Search the signature database to search the revoked content hash\r
174 //\r
175 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) +\r
176 SigList->SignatureHeaderSize);\r
177 EntryCount = (SigList->SignatureListSize - SigList->SignatureHeaderSize -\r
178 sizeof (EFI_SIGNATURE_LIST)) / SigList->SignatureSize;\r
179 for (EntryIndex = 0; EntryIndex < EntryCount; EntryIndex++) {\r
180 //\r
181 // Compare Data Hash with Signature Data\r
182 //\r
183 if (CompareMem (SigData->SignatureData, HashVal, (SigList->SignatureSize - sizeof (EFI_GUID))) == 0) {\r
184 Status = TRUE;\r
185 goto _Exit;\r
186 }\r
187\r
188 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigData + SigList->SignatureSize);\r
189 }\r
190 }\r
191\r
192_Exit:\r
193 return Status;\r
194}\r
195\r
196/**\r
197 Check whether the hash of an given certificate is revoked by the revocation database.\r
198\r
199 @param[in] Certificate Pointer to the certificate that is searched for.\r
200 @param[in] CertSize Size of certificate in bytes.\r
201 @param[in] RevokedDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
202 structures which contains list of X.509 certificate\r
203 of revoked signers and revoked content hashes.\r
204 @param[out] RevocationTime Return the time that the certificate was revoked.\r
205\r
206 @return TRUE The certificate hash is found in the revocation database.\r
207 @return FALSE The certificate hash is not found in the revocation database.\r
208\r
209**/\r
210BOOLEAN\r
211IsCertHashRevoked (\r
212 IN UINT8 *Certificate,\r
213 IN UINTN CertSize,\r
214 IN EFI_SIGNATURE_LIST **RevokedDb,\r
215 OUT EFI_TIME *RevocationTime\r
216 )\r
217{\r
218 BOOLEAN Status;\r
219 EFI_SIGNATURE_LIST *SigList;\r
220 EFI_SIGNATURE_DATA *SigData;\r
221 UINT8 *TBSCert;\r
222 UINTN TBSCertSize;\r
223 UINTN Index;\r
224 UINTN EntryIndex;\r
225 UINTN EntryCount;\r
226 UINT8 CertHashVal[MAX_DIGEST_SIZE];\r
227\r
228 if ((RevocationTime == NULL) || (RevokedDb == NULL)) {\r
229 return FALSE;\r
230 }\r
231\r
232 //\r
233 // Retrieve the TBSCertificate from the X.509 Certificate for hash calculation\r
234 //\r
235 if (!X509GetTBSCert (Certificate, CertSize, &TBSCert, &TBSCertSize)) {\r
236 return FALSE;\r
237 }\r
238\r
239 Status = FALSE;\r
240 for (Index = 0; ; Index++) {\r
241\r
242 SigList = (EFI_SIGNATURE_LIST *)(RevokedDb[Index]);\r
243 //\r
244 // The list is terminated by a NULL pointer.\r
245 //\r
246 if (SigList == NULL) {\r
247 break;\r
248 }\r
249\r
250 //\r
251 // Determine Hash Algorithm based on the entry type in revocation database, and\r
252 // calculate the certificate hash.\r
253 //\r
254 if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Sha256Guid)) {\r
255 Status = CalculateDataHash (TBSCert, TBSCertSize, &gEfiCertSha256Guid, CertHashVal);\r
256\r
257 } else if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Sha384Guid)) {\r
258 Status = CalculateDataHash (TBSCert, TBSCertSize, &gEfiCertSha384Guid, CertHashVal);\r
259\r
260 } else if (CompareGuid (&SigList->SignatureType, &gEfiCertX509Sha512Guid)) {\r
261 Status = CalculateDataHash (TBSCert, TBSCertSize, &gEfiCertSha512Guid, CertHashVal);\r
262\r
263 } else {\r
264 //\r
265 // Un-matched Cert Hash GUID\r
266 //\r
267 continue;\r
268 }\r
269\r
270 if (!Status) {\r
271 continue;\r
272 }\r
273\r
274 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) +\r
275 SigList->SignatureHeaderSize);\r
276 EntryCount = (SigList->SignatureListSize - SigList->SignatureHeaderSize -\r
277 sizeof (EFI_SIGNATURE_LIST)) / SigList->SignatureSize;\r
278 for (EntryIndex = 0; EntryIndex < EntryCount; Index++) {\r
279 //\r
280 // Check if the Certificate Hash is revoked.\r
281 //\r
282 if (CompareMem (SigData->SignatureData, CertHashVal,\r
283 SigList->SignatureSize - sizeof (EFI_GUID) - sizeof (EFI_TIME)) == 0) {\r
284 Status = TRUE;\r
285 //\r
286 // Return the revocation time of this revoked certificate.\r
287 //\r
288 CopyMem (\r
289 RevocationTime,\r
290 (EFI_TIME *)((UINT8 *)SigData + SigList->SignatureSize - sizeof (EFI_TIME)),\r
291 sizeof (EFI_TIME)\r
292 );\r
293 goto _Exit;\r
294 }\r
295\r
296 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigData + SigList->SignatureSize);\r
297 }\r
298 }\r
299\r
300_Exit:\r
301 return Status;\r
302}\r
303\r
304/**\r
305 Check if the given time value is zero.\r
306\r
307 @param[in] Time Pointer of a time value.\r
308\r
309 @retval TRUE The Time is Zero.\r
310 @retval FALSE The Time is not Zero.\r
311\r
312**/\r
313BOOLEAN\r
314IsTimeZero (\r
315 IN EFI_TIME *Time\r
316 )\r
317{\r
318 if ((Time->Year == 0) && (Time->Month == 0) && (Time->Day == 0) &&\r
319 (Time->Hour == 0) && (Time->Minute == 0) && (Time->Second == 0)) {\r
320 return TRUE;\r
321 }\r
322\r
323 return FALSE;\r
324}\r
325\r
326/**\r
327 Check whether the timestamp is valid by comparing the signing time and the revocation time.\r
328\r
329 @param SigningTime Pointer to the signing time.\r
330 @param RevocationTime Pointer to the revocation time.\r
331\r
332 @retval TRUE The SigningTime is not later than the RevocationTime.\r
333 @retval FALSE The SigningTime is later than the RevocationTime.\r
334\r
335**/\r
336BOOLEAN\r
337CompareTimestamp (\r
338 IN EFI_TIME *SigningTime,\r
339 IN EFI_TIME *RevocationTime\r
340 )\r
341{\r
342 if (SigningTime->Year != RevocationTime->Year) {\r
343 return (BOOLEAN) (SigningTime->Year < RevocationTime->Year);\r
344 } else if (SigningTime->Month != RevocationTime->Month) {\r
345 return (BOOLEAN) (SigningTime->Month < RevocationTime->Month);\r
346 } else if (SigningTime->Day != RevocationTime->Day) {\r
347 return (BOOLEAN) (SigningTime->Day < RevocationTime->Day);\r
348 } else if (SigningTime->Hour != RevocationTime->Hour) {\r
349 return (BOOLEAN) (SigningTime->Hour < RevocationTime->Hour);\r
350 } else if (SigningTime->Minute != RevocationTime->Minute) {\r
351 return (BOOLEAN) (SigningTime->Minute < RevocationTime->Minute);\r
352 }\r
353\r
354 return (BOOLEAN) (SigningTime->Second <= RevocationTime->Second);\r
355}\r
356\r
357/**\r
358 Check whether the timestamp signature embedded in PKCS7 signedData is valid and\r
359 the signing time is also earlier than the revocation time.\r
360\r
361 @param[in] SignedData Pointer to the PKCS#7 signedData.\r
362 @param[in] SignedDataSize Size of SignedData in bytes.\r
363 @param[in] TimeStampDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
364 structures which is used to pass a list of X.509\r
365 certificates of trusted timestamp signers.\r
366 @param[in] RevocationTime The time that the certificate was revoked.\r
367\r
368 @retval TRUE Timestamp signature is valid and the signing time is no later\r
369 than the revocation time.\r
370 @retval FALSE Timestamp signature is not valid or the signing time is later\r
371 than the revocation time.\r
372\r
373**/\r
374BOOLEAN\r
375IsValidTimestamp (\r
376 IN UINT8 *SignedData,\r
377 IN UINTN SignedDataSize,\r
378 IN EFI_SIGNATURE_LIST **TimeStampDb,\r
379 IN EFI_TIME *RevocationTime\r
380 )\r
381{\r
382 BOOLEAN Status;\r
383 EFI_SIGNATURE_LIST *SigList;\r
384 EFI_SIGNATURE_DATA *SigData;\r
385 UINT8 *TsaCert;\r
386 UINTN TsaCertSize;\r
387 UINTN Index;\r
388 EFI_TIME SigningTime;\r
389\r
390 //\r
391 // If no supplied database for verification or RevocationTime is zero,\r
392 // the certificate shall be considered to always be revoked.\r
393 //\r
394 if ((TimeStampDb == NULL) || (IsTimeZero (RevocationTime))) {\r
395 return FALSE;\r
396 }\r
397\r
398 Status = FALSE;\r
399 //\r
400 // RevocationTime is non-zero, the certificate should be considered to be revoked\r
401 // from that time and onwards.\r
402 //\r
403 for (Index = 0; ; Index++) {\r
404 SigList = (EFI_SIGNATURE_LIST *) (TimeStampDb[Index]);\r
405\r
406 //\r
407 // The list is terminated by a NULL pointer.\r
408 //\r
409 if (SigList == NULL) {\r
410 break;\r
411 }\r
412\r
413 //\r
414 // Ignore any non-X509-format entry in the list\r
415 //\r
416 if (!CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) {\r
417 continue;\r
418 }\r
419\r
420\r
421 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) +\r
422 SigList->SignatureHeaderSize);\r
423 TsaCert = SigData->SignatureData;\r
424 TsaCertSize = SigList->SignatureSize - sizeof (EFI_GUID);\r
425\r
426 //\r
427 // Each TSA Certificate will normally be in a seperate EFI_SIGNATURE_LIST\r
428 // Leverage ImageTimestampVerify interface for Timestamp counterSignature Verification\r
429 //\r
430 if (ImageTimestampVerify (SignedData, SignedDataSize, TsaCert, TsaCertSize, &SigningTime)) {\r
431 //\r
432 // The signer signature is valid only when the signing time is earlier than revocation time.\r
433 //\r
434 if (CompareTimestamp (&SigningTime, RevocationTime)) {\r
435 Status = TRUE;\r
436 break;\r
437 }\r
438 }\r
439 }\r
440\r
441 return Status;\r
442}\r
443\r
444/**\r
445 Check whether the PKCS7 signedData is revoked by verifying with the revoked\r
446 certificates database, and if the signedData is timestamped, the embedded timestamp\r
447 couterSignature will be checked with the supplied timestamp database.\r
448\r
449 @param[in] SignedData Pointer to buffer containing ASN.1 DER-encoded PKCS7\r
450 signature.\r
451 @param[in] SignedDataSize The size of SignedData buffer in bytes.\r
452 @param[in] InData Pointer to the buffer containing the raw message data\r
453 previously signed and to be verified.\r
454 @param[in] InDataSize The size of InData buffer in bytes.\r
455 @param[in] RevokedDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
456 structure which contains list of X.509 certificates\r
457 of revoked signers and revoked content hashes.\r
458 @param[in] TimeStampDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
459 structures which is used to pass a list of X.509\r
460 certificates of trusted timestamp signers.\r
461\r
462 @retval EFI_SUCCESS The PKCS7 signedData is revoked.\r
463 @retval EFI_SECURITY_VIOLATION Fail to verify the signature in PKCS7 signedData.\r
464 @retval EFI_INVALID_PARAMETER SignedData is NULL or SignedDataSize is zero.\r
465 AllowedDb is NULL.\r
466 Content is not NULL and ContentSize is NULL.\r
467 @retval EFI_NOT_FOUND Content not found because InData is NULL and no\r
468 content embedded in PKCS7 signedData.\r
469 @retval EFI_UNSUPPORTED The PKCS7 signedData was not correctly formatted.\r
470\r
471**/\r
472EFI_STATUS\r
473P7CheckRevocation (\r
474 IN UINT8 *SignedData,\r
475 IN UINTN SignedDataSize,\r
476 IN UINT8 *InData,\r
477 IN UINTN InDataSize,\r
478 IN EFI_SIGNATURE_LIST **RevokedDb,\r
479 IN EFI_SIGNATURE_LIST **TimeStampDb\r
480 )\r
481{\r
482 EFI_STATUS Status;\r
483 EFI_SIGNATURE_LIST *SigList;\r
484 EFI_SIGNATURE_DATA *SigData;\r
485 UINT8 *RevokedCert;\r
486 UINTN RevokedCertSize;\r
487 UINTN Index;\r
488 UINT8 *CertBuffer;\r
489 UINTN BufferLength;\r
490 UINT8 *TrustedCert;\r
491 UINTN TrustedCertLength;\r
492 UINT8 CertNumber;\r
493 UINT8 *CertPtr;\r
494 UINT8 *Cert;\r
495 UINTN CertSize;\r
496 EFI_TIME RevocationTime;\r
497\r
498 Status = EFI_UNSUPPORTED;\r
499 SigData = NULL;\r
500 RevokedCert = NULL;\r
501 RevokedCertSize = 0;\r
502 CertBuffer = NULL;\r
503 TrustedCert = NULL;\r
504\r
505 //\r
506 // The signedData is revoked if the hash of content existed in RevokedDb\r
507 //\r
508 if (IsContentHashRevoked (InData, InDataSize, RevokedDb)) {\r
509 Status = EFI_SUCCESS;\r
510 goto _Exit;\r
511 }\r
512\r
513 //\r
514 // Check if the signer's certificate can be found in Revoked database\r
515 //\r
516 for (Index = 0; ; Index++) {\r
517 SigList = (EFI_SIGNATURE_LIST *)(RevokedDb[Index]);\r
518\r
519 //\r
520 // The list is terminated by a NULL pointer.\r
521 //\r
522 if (SigList == NULL) {\r
523 break;\r
524 }\r
525\r
526 //\r
527 // Ignore any non-X509-format entry in the list.\r
528 //\r
529 if (!CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) {\r
530 continue;\r
531 }\r
532\r
533 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) +\r
534 SigList->SignatureHeaderSize);\r
535\r
536 RevokedCert = SigData->SignatureData;\r
537 RevokedCertSize = SigList->SignatureSize - sizeof (EFI_GUID);\r
538\r
539 //\r
540 // Verifying the PKCS#7 SignedData with the revoked certificate in RevokedDb\r
541 //\r
542 if (Pkcs7Verify (SignedData, SignedDataSize, RevokedCert, RevokedCertSize, InData, InDataSize)) {\r
543 //\r
544 // The signedData was verified by one entry in Revoked Database\r
545 //\r
546 Status = EFI_SUCCESS;\r
547 break;\r
548 }\r
549 }\r
550\r
551 if (!EFI_ERROR (Status)) {\r
552 //\r
553 // The signedData was revoked, since it was hit by RevokedDb\r
554 //\r
555 goto _Exit;\r
556 }\r
557\r
558 //\r
559 // Now we will continue to check the X.509 Certificate Hash & Possible Timestamp\r
560 //\r
561 if ((TimeStampDb == NULL) || (*TimeStampDb == NULL)) {\r
562 goto _Exit;\r
563 }\r
564\r
565 Pkcs7GetSigners (SignedData, SignedDataSize, &CertBuffer, &BufferLength, &TrustedCert, &TrustedCertLength);\r
566 if ((BufferLength == 0) || (CertBuffer == NULL)) {\r
567 Status = EFI_SUCCESS;\r
568 goto _Exit;\r
569 }\r
570\r
571 //\r
572 // Check if any hash of certificates embedded in P7 data is in the revoked database.\r
573 //\r
574 CertNumber = (UINT8) (*CertBuffer);\r
575 CertPtr = CertBuffer + 1;\r
576 for (Index = 0; Index < CertNumber; Index++) {\r
577 //\r
578 // Retrieve the Certificate data\r
579 //\r
580 CertSize = (UINTN) ReadUnaligned32 ((UINT32 *) CertPtr);\r
581 Cert = (UINT8 *)CertPtr + sizeof (UINT32);\r
582\r
583 if (IsCertHashRevoked (Cert, CertSize, RevokedDb, &RevocationTime)) {\r
584 //\r
585 // Check the timestamp signature and signing time to determine if p7 data can be trusted.\r
586 //\r
587 Status = EFI_SUCCESS;\r
588 if (IsValidTimestamp (SignedData, SignedDataSize, TimeStampDb, &RevocationTime)) {\r
589 //\r
590 // Use EFI_NOT_READY to identify the P7Data is not reovked, because the timestamping\r
591 // occured prior to the time of certificate revocation.\r
592 //\r
593 Status = EFI_NOT_READY;\r
594 }\r
595\r
596 goto _Exit;\r
597 }\r
598\r
599 CertPtr = CertPtr + sizeof (UINT32) + CertSize;\r
600 }\r
601\r
602_Exit:\r
603 Pkcs7FreeSigners (CertBuffer);\r
604 Pkcs7FreeSigners (TrustedCert);\r
605\r
606 return Status;\r
607}\r
608\r
609/**\r
610 Check whether the PKCS7 signedData can be verified by the trusted certificates\r
611 database, and return the content of the signedData if requested.\r
612\r
613 @param[in] SignedData Pointer to buffer containing ASN.1 DER-encoded PKCS7\r
614 signature.\r
615 @param[in] SignedDataSize The size of SignedData buffer in bytes.\r
616 @param[in] InData Pointer to the buffer containing the raw message data\r
617 previously signed and to be verified.\r
618 @param[in] InDataSize The size of InData buffer in bytes.\r
619 @param[in] AllowedDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
620 structures which contains lists of X.509 certificates\r
621 of approved signers.\r
2d093499
QL
622\r
623 @retval EFI_SUCCESS The PKCS7 signedData is trusted.\r
624 @retval EFI_SECURITY_VIOLATION Fail to verify the signature in PKCS7 signedData.\r
625 @retval EFI_INVALID_PARAMETER SignedData is NULL or SignedDataSize is zero.\r
626 AllowedDb is NULL.\r
627 Content is not NULL and ContentSize is NULL.\r
628 @retval EFI_NOT_FOUND Content not found because InData is NULL and no\r
629 content embedded in PKCS7 signedData.\r
630 @retval EFI_UNSUPPORTED The PKCS7 signedData was not correctly formatted.\r
631 @retval EFI_BUFFER_TOO_SMALL The size of buffer indicated by ContentSize is too\r
632 small to hold the content. ContentSize updated to\r
633 the required size.\r
634\r
635**/\r
636EFI_STATUS\r
637P7CheckTrust (\r
638 IN UINT8 *SignedData,\r
639 IN UINTN SignedDataSize,\r
640 IN UINT8 *InData,\r
641 IN UINTN InDataSize,\r
642 IN EFI_SIGNATURE_LIST **AllowedDb\r
643 )\r
644{\r
645 EFI_STATUS Status;\r
646 EFI_SIGNATURE_LIST *SigList;\r
647 EFI_SIGNATURE_DATA *SigData;\r
648 UINT8 *TrustCert;\r
649 UINTN TrustCertSize;\r
650 UINTN Index;\r
651\r
652 Status = EFI_SECURITY_VIOLATION;\r
653 SigData = NULL;\r
654 TrustCert = NULL;\r
655 TrustCertSize = 0;\r
656\r
657 if (AllowedDb == NULL) {\r
658 return EFI_INVALID_PARAMETER;\r
659 }\r
660\r
661 //\r
662 // Build Certificate Stack with all valid X509 certificates in the supplied\r
663 // Signature List for PKCS7 Verification.\r
664 //\r
665 for (Index = 0; ; Index++) {\r
666 SigList = (EFI_SIGNATURE_LIST *)(AllowedDb[Index]);\r
667\r
668 //\r
669 // The list is terminated by a NULL pointer.\r
670 //\r
671 if (SigList == NULL) {\r
672 break;\r
673 }\r
674\r
675 //\r
676 // Ignore any non-X509-format entry in the list.\r
677 //\r
678 if (!CompareGuid (&SigList->SignatureType, &gEfiCertX509Guid)) {\r
679 continue;\r
680 }\r
681\r
682 SigData = (EFI_SIGNATURE_DATA *) ((UINT8 *) SigList + sizeof (EFI_SIGNATURE_LIST) +\r
683 SigList->SignatureHeaderSize);\r
684\r
685 TrustCert = SigData->SignatureData;\r
686 TrustCertSize = SigList->SignatureSize - sizeof (EFI_GUID);\r
687\r
688 //\r
689 // Verifying the PKCS#7 SignedData with the trusted certificate from AllowedDb\r
690 //\r
691 if (Pkcs7Verify (SignedData, SignedDataSize, TrustCert, TrustCertSize, InData, InDataSize)) {\r
692 //\r
693 // The SignedData was verified successfully by one entry in Trusted Database\r
694 //\r
695 Status = EFI_SUCCESS;\r
696 break;\r
697 }\r
698 }\r
699\r
700 return Status;\r
701}\r
702\r
703/**\r
704 Processes a buffer containing binary DER-encoded PKCS7 signature.\r
705 The signed data content may be embedded within the buffer or separated. Function\r
706 verifies the signature of the content is valid and signing certificate was not\r
707 revoked and is contained within a list of trusted signers.\r
708\r
709 @param[in] This Pointer to EFI_PKCS7_VERIFY_PROTOCOL instance.\r
710 @param[in] SignedData Points to buffer containing ASN.1 DER-encoded PKCS7\r
711 signature.\r
712 @param[in] SignedDataSize The size of SignedData buffer in bytes.\r
713 @param[in] InData In case of detached signature, InData points to\r
714 buffer containing the raw message data previously\r
715 signed and to be verified by function. In case of\r
716 SignedData containing embedded data, InData must be\r
717 NULL.\r
718 @param[in] InDataSize When InData is used, the size of InData buffer in\r
719 bytes. When InData is NULL. This parameter must be\r
720 0.\r
721 @param[in] AllowedDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
722 structures. The list is terminated by a null\r
723 pointer. The EFI_SIGNATURE_LIST structures contain\r
724 lists of X.509 certificates of approved signers.\r
725 Function recognizes signer certificates of type\r
726 EFI_CERT_X509_GUID. Any hash certificate in AllowedDb\r
727 list is ignored by this function. Function returns\r
728 success if signer of the buffer is within this list\r
729 (and not within RevokedDb). This parameter is\r
730 required.\r
731 @param[in] RevokedDb Optional pointer to a list of pointers to\r
732 EFI_SIGNATURE_LIST structures. The list is terminated\r
733 by a null pointer. List of X.509 certificates of\r
734 revoked signers and revoked file hashes. Except as\r
735 noted in description of TimeStampDb signature\r
736 verification will always fail if the signer of the\r
737 file or the hash of the data component of the buffer\r
738 is in RevokedDb list. This list is optional and\r
739 caller may pass Null or pointer to NULL if not\r
740 required.\r
741 @param[in] TimeStampDb Optional pointer to a list of pointers to\r
742 EFI_SIGNATURE_LIST structures. The list is terminated\r
743 by a null pointer. This parameter can be used to pass\r
744 a list of X.509 certificates of trusted time stamp\r
745 signers. This list is optional and caller must pass\r
746 Null or pointer to NULL if not required.\r
747 @param[out] Content On input, points to an optional caller-allocated\r
748 buffer into which the function will copy the content\r
749 portion of the file after verification succeeds.\r
750 This parameter is optional and if NULL, no copy of\r
751 content from file is performed.\r
752 @param[in,out] ContentSize On input, points to the size in bytes of the optional\r
753 buffer Content previously allocated by caller. On\r
754 output, if the verification succeeds, the value\r
755 referenced by ContentSize will contain the actual\r
756 size of the content from signed file. If ContentSize\r
757 indicates the caller-allocated buffer is too small\r
758 to contain content, an error is returned, and\r
759 ContentSize will be updated with the required size.\r
760 This parameter must be 0 if Content is Null.\r
761\r
762 @retval EFI_SUCCESS Content signature was verified against hash of\r
763 content, the signer's certificate was not found in\r
764 RevokedDb, and was found in AllowedDb or if in signer\r
765 is found in both AllowedDb and RevokedDb, the\r
766 signing was allowed by reference to TimeStampDb as\r
767 described above, and no hash matching content hash\r
768 was found in RevokedDb.\r
769 @retval EFI_SECURITY_VIOLATION The SignedData buffer was correctly formatted but\r
770 signer was in RevokedDb or not in AllowedDb. Also\r
771 returned if matching content hash found in RevokedDb.\r
772 @retval EFI_COMPROMISED_DATA Calculated hash differs from signed hash.\r
773 @retval EFI_INVALID_PARAMETER SignedData is NULL or SignedDataSize is zero.\r
774 AllowedDb is NULL.\r
775 @retval EFI_INVALID_PARAMETER Content is not NULL and ContentSize is NULL.\r
776 @retval EFI_ABORTED Unsupported or invalid format in TimeStampDb,\r
777 RevokedDb or AllowedDb list contents was detected.\r
778 @retval EFI_NOT_FOUND Content not found because InData is NULL and no\r
779 content embedded in SignedData.\r
780 @retval EFI_UNSUPPORTED The SignedData buffer was not correctly formatted\r
781 for processing by the function.\r
782 @retval EFI_UNSUPPORTED Signed data embedded in SignedData but InData is not\r
783 NULL.\r
784 @retval EFI_BUFFER_TOO_SMALL The size of buffer indicated by ContentSize is too\r
785 small to hold the content. ContentSize updated to\r
786 required size.\r
787\r
788**/\r
789EFI_STATUS\r
790EFIAPI\r
791VerifyBuffer (\r
792 IN EFI_PKCS7_VERIFY_PROTOCOL *This,\r
793 IN VOID *SignedData,\r
794 IN UINTN SignedDataSize,\r
795 IN VOID *InData OPTIONAL,\r
796 IN UINTN InDataSize,\r
797 IN EFI_SIGNATURE_LIST **AllowedDb,\r
798 IN EFI_SIGNATURE_LIST **RevokedDb OPTIONAL,\r
799 IN EFI_SIGNATURE_LIST **TimeStampDb OPTIONAL,\r
800 OUT VOID *Content OPTIONAL,\r
801 IN OUT UINTN *ContentSize\r
802 )\r
803{\r
76b35710
LQ
804 EFI_STATUS Status;\r
805 EFI_SIGNATURE_LIST *SigList;\r
806 UINTN Index;\r
807 UINT8 *AttachedData;\r
808 UINTN AttachedDataSize;\r
809 UINT8 *DataPtr;\r
810 UINTN DataSize;\r
2d093499
QL
811\r
812 //\r
813 // Parameters Checking\r
814 //\r
815 if ((SignedData == NULL) || (SignedDataSize == 0) || (AllowedDb == NULL)) {\r
816 return EFI_INVALID_PARAMETER;\r
817 }\r
818 if ((Content != NULL) && (ContentSize == NULL)) {\r
819 return EFI_INVALID_PARAMETER;\r
820 }\r
821\r
76b35710
LQ
822 //\r
823 // Check if any invalid entry format in AllowedDb list contents\r
824 //\r
825 for (Index = 0; ; Index++) {\r
826 SigList = (EFI_SIGNATURE_LIST *)(AllowedDb[Index]);\r
827\r
828 if (SigList == NULL) {\r
829 break;\r
830 }\r
831 if (SigList->SignatureListSize < sizeof (EFI_SIGNATURE_LIST) +\r
832 SigList->SignatureHeaderSize +\r
833 SigList->SignatureSize) {\r
834 return EFI_ABORTED;\r
835 }\r
836 }\r
837\r
838 //\r
839 // Check if any invalid entry format in RevokedDb list contents\r
840 //\r
841 if (RevokedDb != NULL) {\r
842 for (Index = 0; ; Index++) {\r
843 SigList = (EFI_SIGNATURE_LIST *)(RevokedDb[Index]);\r
844\r
845 if (SigList == NULL) {\r
846 break;\r
847 }\r
848 if (SigList->SignatureListSize < sizeof (EFI_SIGNATURE_LIST) +\r
849 SigList->SignatureHeaderSize +\r
850 SigList->SignatureSize) {\r
851 return EFI_ABORTED;\r
852 }\r
853 }\r
854 }\r
855\r
856 //\r
857 // Check if any invalid entry format in TimeStampDb list contents\r
858 //\r
859 if (TimeStampDb != NULL) {\r
860 for (Index = 0; ; Index++) {\r
861 SigList = (EFI_SIGNATURE_LIST *)(TimeStampDb[Index]);\r
862\r
863 if (SigList == NULL) {\r
864 break;\r
865 }\r
866 if (SigList->SignatureListSize < sizeof (EFI_SIGNATURE_LIST) +\r
867 SigList->SignatureHeaderSize +\r
868 SigList->SignatureSize) {\r
869 return EFI_ABORTED;\r
870 }\r
871 }\r
872 }\r
873\r
2d093499
QL
874 //\r
875 // Try to retrieve the attached content from PKCS7 signedData\r
876 //\r
877 AttachedData = NULL;\r
878 AttachedDataSize = 0;\r
879 if (!Pkcs7GetAttachedContent (\r
880 SignedData,\r
881 SignedDataSize,\r
882 (VOID **)&AttachedData,\r
883 &AttachedDataSize)) {\r
884 //\r
885 // The SignedData buffer was not correctly formatted for processing\r
886 //\r
887 return EFI_UNSUPPORTED;\r
888 }\r
889 if (AttachedData != NULL) {\r
173a1e68
QL
890 if (InData != NULL) {\r
891 //\r
892 // The embedded content is found in SignedData but InData is not NULL\r
893 //\r
894 Status = EFI_UNSUPPORTED;\r
895 goto _Exit;\r
896 }\r
2d093499
QL
897 //\r
898 // PKCS7-formatted signedData with attached content; Use the embedded\r
899 // content for verification\r
900 //\r
901 DataPtr = AttachedData;\r
902 DataSize = AttachedDataSize;\r
903\r
904 } else if (InData != NULL) {\r
905 //\r
906 // PKCS7-formatted signedData with detached content; Use the user-supplied\r
907 // input data for verification\r
908 //\r
909 DataPtr = (UINT8 *)InData;\r
910 DataSize = InDataSize;\r
911 } else {\r
912 //\r
913 // Content not found because InData is NULL and no content attached in SignedData\r
914 //\r
915 Status = EFI_NOT_FOUND;\r
916 goto _Exit;\r
917 }\r
918\r
919 Status = EFI_UNSUPPORTED;\r
920\r
921 //\r
922 // Verify PKCS7 SignedData with Revoked database\r
923 //\r
924 if (RevokedDb != NULL) {\r
925 Status = P7CheckRevocation (\r
926 SignedData,\r
927 SignedDataSize,\r
928 DataPtr,\r
929 DataSize,\r
930 RevokedDb,\r
931 TimeStampDb\r
932 );\r
933 if (!EFI_ERROR (Status)) {\r
934 //\r
935 // The PKCS7 SignedData is reovked\r
936 //\r
937 Status = EFI_SECURITY_VIOLATION;\r
938 goto _Exit;\r
939 }\r
940 }\r
941\r
942 //\r
943 // Verify PKCS7 SignedData with AllowedDB\r
944 //\r
945 Status = P7CheckTrust (\r
946 SignedData,\r
947 SignedDataSize,\r
948 DataPtr,\r
949 DataSize,\r
950 AllowedDb\r
951 );\r
952 if (EFI_ERROR (Status)) {\r
953 //\r
954 // Verification failed with AllowedDb\r
955 //\r
956 goto _Exit;\r
957 }\r
958\r
959 //\r
960 // Copy the content portion after verification succeeds\r
961 //\r
962 if (Content != NULL) {\r
963 if (*ContentSize < DataSize) {\r
964 //\r
965 // Caller-allocated buffer is too small to contain content\r
966 //\r
967 *ContentSize = DataSize;\r
968 Status = EFI_BUFFER_TOO_SMALL;\r
969 } else {\r
970 *ContentSize = DataSize;\r
971 CopyMem (Content, DataPtr, DataSize);\r
972 }\r
973 }\r
974\r
975_Exit:\r
976 if (AttachedData != NULL) {\r
977 FreePool (AttachedData);\r
978 }\r
979\r
980 return Status;\r
981}\r
982\r
983/**\r
984 Processes a buffer containing binary DER-encoded detached PKCS7 signature.\r
985 The hash of the signed data content is calculated and passed by the caller. Function\r
986 verifies the signature of the content is valid and signing certificate was not revoked\r
987 and is contained within a list of trusted signers.\r
988\r
989 @param[in] This Pointer to EFI_PKCS7_VERIFY_PROTOCOL instance.\r
990 @param[in] Signature Points to buffer containing ASN.1 DER-encoded PKCS\r
991 detached signature.\r
992 @param[in] SignatureSize The size of Signature buffer in bytes.\r
993 @param[in] InHash InHash points to buffer containing the caller\r
994 calculated hash of the data. The parameter may not\r
995 be NULL.\r
996 @param[in] InHashSize The size in bytes of InHash buffer.\r
997 @param[in] AllowedDb Pointer to a list of pointers to EFI_SIGNATURE_LIST\r
998 structures. The list is terminated by a null\r
999 pointer. The EFI_SIGNATURE_LIST structures contain\r
1000 lists of X.509 certificates of approved signers.\r
1001 Function recognizes signer certificates of type\r
1002 EFI_CERT_X509_GUID. Any hash certificate in AllowedDb\r
1003 list is ignored by this function. Function returns\r
1004 success if signer of the buffer is within this list\r
1005 (and not within RevokedDb). This parameter is\r
1006 required.\r
1007 @param[in] RevokedDb Optional pointer to a list of pointers to\r
1008 EFI_SIGNATURE_LIST structures. The list is terminated\r
1009 by a null pointer. List of X.509 certificates of\r
1010 revoked signers and revoked file hashes. Signature\r
1011 verification will always fail if the signer of the\r
1012 file or the hash of the data component of the buffer\r
1013 is in RevokedDb list. This parameter is optional\r
1014 and caller may pass Null if not required.\r
1015 @param[in] TimeStampDb Optional pointer to a list of pointers to\r
1016 EFI_SIGNATURE_LIST structures. The list is terminated\r
1017 by a null pointer. This parameter can be used to pass\r
1018 a list of X.509 certificates of trusted time stamp\r
1019 counter-signers.\r
1020\r
1021 @retval EFI_SUCCESS Signed hash was verified against caller-provided\r
1022 hash of content, the signer's certificate was not\r
1023 found in RevokedDb, and was found in AllowedDb or\r
1024 if in signer is found in both AllowedDb and\r
1025 RevokedDb, the signing was allowed by reference to\r
1026 TimeStampDb as described above, and no hash matching\r
1027 content hash was found in RevokedDb.\r
1028 @retval EFI_SECURITY_VIOLATION The SignedData buffer was correctly formatted but\r
1029 signer was in RevokedDb or not in AllowedDb. Also\r
1030 returned if matching content hash found in RevokedDb.\r
1031 @retval EFI_COMPROMISED_DATA Caller provided hash differs from signed hash. Or,\r
1032 caller and encrypted hash are different sizes.\r
1033 @retval EFI_INVALID_PARAMETER Signature is NULL or SignatureSize is zero. InHash\r
1034 is NULL or InHashSize is zero. AllowedDb is NULL.\r
1035 @retval EFI_ABORTED Unsupported or invalid format in TimeStampDb,\r
1036 RevokedDb or AllowedDb list contents was detected.\r
1037 @retval EFI_UNSUPPORTED The Signature buffer was not correctly formatted\r
1038 for processing by the function.\r
1039\r
1040**/\r
1041EFI_STATUS\r
1042EFIAPI\r
1043VerifySignature (\r
1044 IN EFI_PKCS7_VERIFY_PROTOCOL *This,\r
1045 IN VOID *Signature,\r
1046 IN UINTN SignatureSize,\r
1047 IN VOID *InHash,\r
1048 IN UINTN InHashSize,\r
1049 IN EFI_SIGNATURE_LIST **AllowedDb,\r
1050 IN EFI_SIGNATURE_LIST **RevokedDb OPTIONAL,\r
1051 IN EFI_SIGNATURE_LIST **TimeStampDb OPTIONAL\r
1052 )\r
1053{\r
1054 //\r
1055 // NOTE: Current EDKII-OpenSSL interface cannot support VerifySignature\r
1056 // directly. EFI_UNSUPPORTED is returned in this version.\r
1057 //\r
1058 return EFI_UNSUPPORTED;\r
1059}\r
1060\r
1061//\r
1062// The PKCS7 Verification Protocol\r
1063//\r
1064EFI_PKCS7_VERIFY_PROTOCOL mPkcs7Verify = {\r
1065 VerifyBuffer,\r
1066 VerifySignature\r
1067};\r
1068\r
1069/**\r
1070 The user Entry Point for the PKCS7 Verification driver.\r
1071\r
1072 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
1073 @param[in] SystemTable A pointer to the EFI System Table.\r
1074\r
1075 @retval EFI_SUCCESS The entry point is executed successfully.\r
1076 @retval EFI_NOT_SUPPORTED Platform does not support PKCS7 Verification.\r
1077 @retval Other Some error occurs when executing this entry point.\r
1078\r
1079**/\r
1080EFI_STATUS\r
1081EFIAPI\r
1082Pkcs7VerifyDriverEntry (\r
1083 IN EFI_HANDLE ImageHandle,\r
1084 IN EFI_SYSTEM_TABLE *SystemTable\r
1085 )\r
1086{\r
1087 EFI_STATUS Status;\r
1088 EFI_HANDLE Handle;\r
1089\r
1090 //\r
1091 // Install UEFI Pkcs7 Verification Protocol\r
1092 //\r
1093 Handle = NULL;\r
1094 Status = gBS->InstallMultipleProtocolInterfaces (\r
1095 &Handle,\r
1096 &gEfiPkcs7VerifyProtocolGuid,\r
1097 &mPkcs7Verify,\r
1098 NULL\r
1099 );\r
1100\r
1101 return Status;\r
1102}\r