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