]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7Verify.c
edk2: Add .DS_Store to .gitignore for macOS
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7Verify.c
1 /** @file
2 PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.
3
4 Caution: This module requires additional review when modified.
5 This library will have external input - signature (e.g. UEFI Authenticated
6 Variable). It may by input in SMM mode.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
9
10 WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated
11 Variable and will do basic check for data structure.
12
13 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
14 This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
18
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21
22 **/
23
24 #include "InternalCryptLib.h"
25
26 #include <openssl/objects.h>
27 #include <openssl/x509.h>
28 #include <openssl/x509v3.h>
29 #include <openssl/pkcs7.h>
30
31 UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };
32
33 /**
34 Check input P7Data is a wrapped ContentInfo structure or not. If not construct
35 a new structure to wrap P7Data.
36
37 Caution: This function may receive untrusted input.
38 UEFI Authenticated Variable is external input, so this function will do basic
39 check for PKCS#7 data structure.
40
41 @param[in] P7Data Pointer to the PKCS#7 message to verify.
42 @param[in] P7Length Length of the PKCS#7 message in bytes.
43 @param[out] WrapFlag If TRUE P7Data is a ContentInfo structure, otherwise
44 return FALSE.
45 @param[out] WrapData If return status of this function is TRUE:
46 1) when WrapFlag is TRUE, pointer to P7Data.
47 2) when WrapFlag is FALSE, pointer to a new ContentInfo
48 structure. It's caller's responsibility to free this
49 buffer.
50 @param[out] WrapDataSize Length of ContentInfo structure in bytes.
51
52 @retval TRUE The operation is finished successfully.
53 @retval FALSE The operation is failed due to lack of resources.
54
55 **/
56 BOOLEAN
57 WrapPkcs7Data (
58 IN CONST UINT8 *P7Data,
59 IN UINTN P7Length,
60 OUT BOOLEAN *WrapFlag,
61 OUT UINT8 **WrapData,
62 OUT UINTN *WrapDataSize
63 )
64 {
65 BOOLEAN Wrapped;
66 UINT8 *SignedData;
67
68 //
69 // Check whether input P7Data is a wrapped ContentInfo structure or not.
70 //
71 Wrapped = FALSE;
72 if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {
73 if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {
74 if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {
75 Wrapped = TRUE;
76 }
77 }
78 }
79
80 if (Wrapped) {
81 *WrapData = (UINT8 *) P7Data;
82 *WrapDataSize = P7Length;
83 } else {
84 //
85 // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.
86 //
87 *WrapDataSize = P7Length + 19;
88 *WrapData = malloc (*WrapDataSize);
89 if (*WrapData == NULL) {
90 *WrapFlag = Wrapped;
91 return FALSE;
92 }
93
94 SignedData = *WrapData;
95
96 //
97 // Part1: 0x30, 0x82.
98 //
99 SignedData[0] = 0x30;
100 SignedData[1] = 0x82;
101
102 //
103 // Part2: Length1 = P7Length + 19 - 4, in big endian.
104 //
105 SignedData[2] = (UINT8) (((UINT16) (*WrapDataSize - 4)) >> 8);
106 SignedData[3] = (UINT8) (((UINT16) (*WrapDataSize - 4)) & 0xff);
107
108 //
109 // Part3: 0x06, 0x09.
110 //
111 SignedData[4] = 0x06;
112 SignedData[5] = 0x09;
113
114 //
115 // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.
116 //
117 CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));
118
119 //
120 // Part5: 0xA0, 0x82.
121 //
122 SignedData[15] = 0xA0;
123 SignedData[16] = 0x82;
124
125 //
126 // Part6: Length2 = P7Length, in big endian.
127 //
128 SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);
129 SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);
130
131 //
132 // Part7: P7Data.
133 //
134 CopyMem (SignedData + 19, P7Data, P7Length);
135 }
136
137 *WrapFlag = Wrapped;
138 return TRUE;
139 }
140
141 /**
142 Pop single certificate from STACK_OF(X509).
143
144 If X509Stack, Cert, or CertSize is NULL, then return FALSE.
145
146 @param[in] X509Stack Pointer to a X509 stack object.
147 @param[out] Cert Pointer to a X509 certificate.
148 @param[out] CertSize Length of output X509 certificate in bytes.
149
150 @retval TRUE The X509 stack pop succeeded.
151 @retval FALSE The pop operation failed.
152
153 **/
154 BOOLEAN
155 X509PopCertificate (
156 IN VOID *X509Stack,
157 OUT UINT8 **Cert,
158 OUT UINTN *CertSize
159 )
160 {
161 BIO *CertBio;
162 X509 *X509Cert;
163 STACK_OF(X509) *CertStack;
164 BOOLEAN Status;
165 INT32 Result;
166 BUF_MEM *Ptr;
167 INT32 Length;
168 VOID *Buffer;
169
170 Status = FALSE;
171
172 if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) {
173 return Status;
174 }
175
176 CertStack = (STACK_OF(X509) *) X509Stack;
177
178 X509Cert = sk_X509_pop (CertStack);
179
180 if (X509Cert == NULL) {
181 return Status;
182 }
183
184 Buffer = NULL;
185
186 CertBio = BIO_new (BIO_s_mem ());
187 if (CertBio == NULL) {
188 return Status;
189 }
190
191 Result = i2d_X509_bio (CertBio, X509Cert);
192 if (Result == 0) {
193 goto _Exit;
194 }
195
196 BIO_get_mem_ptr (CertBio, &Ptr);
197 Length = (INT32)(Ptr->length);
198 if (Length <= 0) {
199 goto _Exit;
200 }
201
202 Buffer = malloc (Length);
203 if (Buffer == NULL) {
204 goto _Exit;
205 }
206
207 Result = BIO_read (CertBio, Buffer, Length);
208 if (Result != Length) {
209 goto _Exit;
210 }
211
212 *Cert = Buffer;
213 *CertSize = Length;
214
215 Status = TRUE;
216
217 _Exit:
218
219 BIO_free (CertBio);
220
221 if (!Status && (Buffer != NULL)) {
222 free (Buffer);
223 }
224
225 return Status;
226 }
227
228 /**
229 Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:
230 Cryptographic Message Syntax Standard". The input signed data could be wrapped
231 in a ContentInfo structure.
232
233 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
234 return FALSE. If P7Length overflow, then return FALSE.
235
236 Caution: This function may receive untrusted input.
237 UEFI Authenticated Variable is external input, so this function will do basic
238 check for PKCS#7 data structure.
239
240 @param[in] P7Data Pointer to the PKCS#7 message to verify.
241 @param[in] P7Length Length of the PKCS#7 message in bytes.
242 @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.
243 It's caller's responsibility to free the buffer.
244 @param[out] StackLength Length of signer's certificates in bytes.
245 @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.
246 It's caller's responsibility to free the buffer.
247 @param[out] CertLength Length of the trusted certificate in bytes.
248
249 @retval TRUE The operation is finished successfully.
250 @retval FALSE Error occurs during the operation.
251
252 **/
253 BOOLEAN
254 EFIAPI
255 Pkcs7GetSigners (
256 IN CONST UINT8 *P7Data,
257 IN UINTN P7Length,
258 OUT UINT8 **CertStack,
259 OUT UINTN *StackLength,
260 OUT UINT8 **TrustedCert,
261 OUT UINTN *CertLength
262 )
263 {
264 PKCS7 *Pkcs7;
265 BOOLEAN Status;
266 UINT8 *SignedData;
267 CONST UINT8 *Temp;
268 UINTN SignedDataSize;
269 BOOLEAN Wrapped;
270 STACK_OF(X509) *Stack;
271 UINT8 Index;
272 UINT8 *CertBuf;
273 UINT8 *OldBuf;
274 UINTN BufferSize;
275 UINTN OldSize;
276 UINT8 *SingleCert;
277 UINTN SingleCertSize;
278
279 if ((P7Data == NULL) || (CertStack == NULL) || (StackLength == NULL) ||
280 (TrustedCert == NULL) || (CertLength == NULL) || (P7Length > INT_MAX)) {
281 return FALSE;
282 }
283
284 Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);
285 if (!Status) {
286 return Status;
287 }
288
289 Status = FALSE;
290 Pkcs7 = NULL;
291 Stack = NULL;
292 CertBuf = NULL;
293 OldBuf = NULL;
294 SingleCert = NULL;
295
296 //
297 // Retrieve PKCS#7 Data (DER encoding)
298 //
299 if (SignedDataSize > INT_MAX) {
300 goto _Exit;
301 }
302
303 Temp = SignedData;
304 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);
305 if (Pkcs7 == NULL) {
306 goto _Exit;
307 }
308
309 //
310 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
311 //
312 if (!PKCS7_type_is_signed (Pkcs7)) {
313 goto _Exit;
314 }
315
316 Stack = PKCS7_get0_signers(Pkcs7, NULL, PKCS7_BINARY);
317 if (Stack == NULL) {
318 goto _Exit;
319 }
320
321 //
322 // Convert CertStack to buffer in following format:
323 // UINT8 CertNumber;
324 // UINT32 Cert1Length;
325 // UINT8 Cert1[];
326 // UINT32 Cert2Length;
327 // UINT8 Cert2[];
328 // ...
329 // UINT32 CertnLength;
330 // UINT8 Certn[];
331 //
332 BufferSize = sizeof (UINT8);
333 OldSize = BufferSize;
334
335 for (Index = 0; ; Index++) {
336 Status = X509PopCertificate (Stack, &SingleCert, &SingleCertSize);
337 if (!Status) {
338 break;
339 }
340
341 OldSize = BufferSize;
342 OldBuf = CertBuf;
343 BufferSize = OldSize + SingleCertSize + sizeof (UINT32);
344 CertBuf = malloc (BufferSize);
345
346 if (CertBuf == NULL) {
347 goto _Exit;
348 }
349
350 if (OldBuf != NULL) {
351 CopyMem (CertBuf, OldBuf, OldSize);
352 free (OldBuf);
353 OldBuf = NULL;
354 }
355
356 WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) SingleCertSize);
357 CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, SingleCertSize);
358
359 free (SingleCert);
360 SingleCert = NULL;
361 }
362
363 if (CertBuf != NULL) {
364 //
365 // Update CertNumber.
366 //
367 CertBuf[0] = Index;
368
369 *CertLength = BufferSize - OldSize - sizeof (UINT32);
370 *TrustedCert = malloc (*CertLength);
371 if (*TrustedCert == NULL) {
372 goto _Exit;
373 }
374
375 CopyMem (*TrustedCert, CertBuf + OldSize + sizeof (UINT32), *CertLength);
376 *CertStack = CertBuf;
377 *StackLength = BufferSize;
378 Status = TRUE;
379 }
380
381 _Exit:
382 //
383 // Release Resources
384 //
385 if (!Wrapped) {
386 free (SignedData);
387 }
388
389 if (Pkcs7 != NULL) {
390 PKCS7_free (Pkcs7);
391 }
392
393 if (Stack != NULL) {
394 sk_X509_pop_free(Stack, X509_free);
395 }
396
397 if (SingleCert != NULL) {
398 free (SingleCert);
399 }
400
401 if (!Status && (CertBuf != NULL)) {
402 free (CertBuf);
403 *CertStack = NULL;
404 }
405
406 if (OldBuf != NULL) {
407 free (OldBuf);
408 }
409
410 return Status;
411 }
412
413 /**
414 Wrap function to use free() to free allocated memory for certificates.
415
416 @param[in] Certs Pointer to the certificates to be freed.
417
418 **/
419 VOID
420 EFIAPI
421 Pkcs7FreeSigners (
422 IN UINT8 *Certs
423 )
424 {
425 if (Certs == NULL) {
426 return;
427 }
428
429 free (Certs);
430 }
431
432 /**
433 Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7:
434 Cryptographic Message Syntax Standard", and outputs two certificate lists chained and
435 unchained to the signer's certificates.
436 The input signed data could be wrapped in a ContentInfo structure.
437
438 @param[in] P7Data Pointer to the PKCS#7 message.
439 @param[in] P7Length Length of the PKCS#7 message in bytes.
440 @param[out] SignerChainCerts Pointer to the certificates list chained to signer's
441 certificate. It's caller's responsibility to free the buffer.
442 @param[out] ChainLength Length of the chained certificates list buffer in bytes.
443 @param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's
444 responsibility to free the buffer.
445 @param[out] UnchainLength Length of the unchained certificates list buffer in bytes.
446
447 @retval TRUE The operation is finished successfully.
448 @retval FALSE Error occurs during the operation.
449
450 **/
451 BOOLEAN
452 EFIAPI
453 Pkcs7GetCertificatesList (
454 IN CONST UINT8 *P7Data,
455 IN UINTN P7Length,
456 OUT UINT8 **SignerChainCerts,
457 OUT UINTN *ChainLength,
458 OUT UINT8 **UnchainCerts,
459 OUT UINTN *UnchainLength
460 )
461 {
462 BOOLEAN Status;
463 UINT8 *NewP7Data;
464 UINTN NewP7Length;
465 BOOLEAN Wrapped;
466 UINT8 Index;
467 PKCS7 *Pkcs7;
468 X509_STORE_CTX *CertCtx;
469 STACK_OF(X509) *CtxChain;
470 STACK_OF(X509) *CtxUntrusted;
471 X509 *CtxCert;
472 STACK_OF(X509) *Signers;
473 X509 *Signer;
474 X509 *Cert;
475 X509 *Issuer;
476 X509_NAME *IssuerName;
477 UINT8 *CertBuf;
478 UINT8 *OldBuf;
479 UINTN BufferSize;
480 UINTN OldSize;
481 UINT8 *SingleCert;
482 UINTN CertSize;
483
484 //
485 // Initializations
486 //
487 Status = FALSE;
488 NewP7Data = NULL;
489 Pkcs7 = NULL;
490 CertCtx = NULL;
491 CtxChain = NULL;
492 CtxCert = NULL;
493 CtxUntrusted = NULL;
494 Cert = NULL;
495 SingleCert = NULL;
496 CertBuf = NULL;
497 OldBuf = NULL;
498 Signers = NULL;
499
500 ZeroMem (&CertCtx, sizeof (CertCtx));
501
502 //
503 // Parameter Checking
504 //
505 if ((P7Data == NULL) || (SignerChainCerts == NULL) || (ChainLength == NULL) ||
506 (UnchainCerts == NULL) || (UnchainLength == NULL) || (P7Length > INT_MAX)) {
507 return Status;
508 }
509
510 *SignerChainCerts = NULL;
511 *ChainLength = 0;
512 *UnchainCerts = NULL;
513 *UnchainLength = 0;
514
515 //
516 // Construct a new PKCS#7 data wrapping with ContentInfo structure if needed.
517 //
518 Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &NewP7Data, &NewP7Length);
519 if (!Status || (NewP7Length > INT_MAX)) {
520 goto _Error;
521 }
522
523 //
524 // Decodes PKCS#7 SignedData
525 //
526 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &NewP7Data, (int) NewP7Length);
527 if ((Pkcs7 == NULL) || (!PKCS7_type_is_signed (Pkcs7))) {
528 goto _Error;
529 }
530
531 //
532 // Obtains Signer's Certificate from PKCS#7 data
533 // NOTE: Only one signer case will be handled in this function, which means SignerInfos
534 // should include only one signer's certificate.
535 //
536 Signers = PKCS7_get0_signers (Pkcs7, NULL, PKCS7_BINARY);
537 if ((Signers == NULL) || (sk_X509_num (Signers) != 1)) {
538 goto _Error;
539 }
540 Signer = sk_X509_value (Signers, 0);
541
542 CertCtx = X509_STORE_CTX_new ();
543 if (CertCtx == NULL) {
544 goto _Error;
545 }
546 if (!X509_STORE_CTX_init (CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) {
547 goto _Error;
548 }
549 //
550 // Initialize Chained & Untrusted stack
551 //
552 CtxChain = X509_STORE_CTX_get0_chain (CertCtx);
553 CtxCert = X509_STORE_CTX_get0_cert (CertCtx);
554 if (CtxChain == NULL) {
555 if (((CtxChain = sk_X509_new_null ()) == NULL) ||
556 (!sk_X509_push (CtxChain, CtxCert))) {
557 goto _Error;
558 }
559 }
560 CtxUntrusted = X509_STORE_CTX_get0_untrusted (CertCtx);
561 (VOID)sk_X509_delete_ptr (CtxUntrusted, Signer);
562
563 //
564 // Build certificates stack chained from Signer's certificate.
565 //
566 Cert = Signer;
567 for (; ;) {
568 //
569 // Self-Issue checking
570 //
571 Issuer = NULL;
572 if (X509_STORE_CTX_get1_issuer (&Issuer, CertCtx, Cert) == 1) {
573 if (X509_cmp (Issuer, Cert) == 0) {
574 break;
575 }
576 }
577
578 //
579 // Found the issuer of the current certificate
580 //
581 if (CtxUntrusted != NULL) {
582 Issuer = NULL;
583 IssuerName = X509_get_issuer_name (Cert);
584 Issuer = X509_find_by_subject (CtxUntrusted, IssuerName);
585 if (Issuer != NULL) {
586 if (!sk_X509_push (CtxChain, Issuer)) {
587 goto _Error;
588 }
589 (VOID)sk_X509_delete_ptr (CtxUntrusted, Issuer);
590
591 Cert = Issuer;
592 continue;
593 }
594 }
595
596 break;
597 }
598
599 //
600 // Converts Chained and Untrusted Certificate to Certificate Buffer in following format:
601 // UINT8 CertNumber;
602 // UINT32 Cert1Length;
603 // UINT8 Cert1[];
604 // UINT32 Cert2Length;
605 // UINT8 Cert2[];
606 // ...
607 // UINT32 CertnLength;
608 // UINT8 Certn[];
609 //
610
611 if (CtxChain != NULL) {
612 BufferSize = sizeof (UINT8);
613 OldSize = BufferSize;
614 CertBuf = NULL;
615
616 for (Index = 0; ; Index++) {
617 Status = X509PopCertificate (CtxChain, &SingleCert, &CertSize);
618 if (!Status) {
619 break;
620 }
621
622 OldSize = BufferSize;
623 OldBuf = CertBuf;
624 BufferSize = OldSize + CertSize + sizeof (UINT32);
625 CertBuf = malloc (BufferSize);
626
627 if (CertBuf == NULL) {
628 Status = FALSE;
629 goto _Error;
630 }
631 if (OldBuf != NULL) {
632 CopyMem (CertBuf, OldBuf, OldSize);
633 free (OldBuf);
634 OldBuf = NULL;
635 }
636
637 WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize);
638 CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize);
639
640 free (SingleCert);
641 SingleCert = NULL;
642 }
643
644 if (CertBuf != NULL) {
645 //
646 // Update CertNumber.
647 //
648 CertBuf[0] = Index;
649
650 *SignerChainCerts = CertBuf;
651 *ChainLength = BufferSize;
652 }
653 }
654
655 if (CtxUntrusted != NULL) {
656 BufferSize = sizeof (UINT8);
657 OldSize = BufferSize;
658 CertBuf = NULL;
659
660 for (Index = 0; ; Index++) {
661 Status = X509PopCertificate (CtxUntrusted, &SingleCert, &CertSize);
662 if (!Status) {
663 break;
664 }
665
666 OldSize = BufferSize;
667 OldBuf = CertBuf;
668 BufferSize = OldSize + CertSize + sizeof (UINT32);
669 CertBuf = malloc (BufferSize);
670
671 if (CertBuf == NULL) {
672 Status = FALSE;
673 goto _Error;
674 }
675 if (OldBuf != NULL) {
676 CopyMem (CertBuf, OldBuf, OldSize);
677 free (OldBuf);
678 OldBuf = NULL;
679 }
680
681 WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize);
682 CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize);
683
684 free (SingleCert);
685 SingleCert = NULL;
686 }
687
688 if (CertBuf != NULL) {
689 //
690 // Update CertNumber.
691 //
692 CertBuf[0] = Index;
693
694 *UnchainCerts = CertBuf;
695 *UnchainLength = BufferSize;
696 }
697 }
698
699 Status = TRUE;
700
701 _Error:
702 //
703 // Release Resources.
704 //
705 if (!Wrapped && (NewP7Data != NULL)) {
706 free (NewP7Data);
707 }
708
709 if (Pkcs7 != NULL) {
710 PKCS7_free (Pkcs7);
711 }
712 sk_X509_free (Signers);
713
714 X509_STORE_CTX_cleanup (CertCtx);
715 X509_STORE_CTX_free (CertCtx);
716
717 if (SingleCert != NULL) {
718 free (SingleCert);
719 }
720
721 if (OldBuf != NULL) {
722 free (OldBuf);
723 }
724
725 if (!Status && (CertBuf != NULL)) {
726 free (CertBuf);
727 *SignerChainCerts = NULL;
728 *UnchainCerts = NULL;
729 }
730
731 return Status;
732 }
733
734 /**
735 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
736 Cryptographic Message Syntax Standard". The input signed data could be wrapped
737 in a ContentInfo structure.
738
739 If P7Data, TrustedCert or InData is NULL, then return FALSE.
740 If P7Length, CertLength or DataLength overflow, then return FALSE.
741
742 Caution: This function may receive untrusted input.
743 UEFI Authenticated Variable is external input, so this function will do basic
744 check for PKCS#7 data structure.
745
746 @param[in] P7Data Pointer to the PKCS#7 message to verify.
747 @param[in] P7Length Length of the PKCS#7 message in bytes.
748 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
749 is used for certificate chain verification.
750 @param[in] CertLength Length of the trusted certificate in bytes.
751 @param[in] InData Pointer to the content to be verified.
752 @param[in] DataLength Length of InData in bytes.
753
754 @retval TRUE The specified PKCS#7 signed data is valid.
755 @retval FALSE Invalid PKCS#7 signed data.
756
757 **/
758 BOOLEAN
759 EFIAPI
760 Pkcs7Verify (
761 IN CONST UINT8 *P7Data,
762 IN UINTN P7Length,
763 IN CONST UINT8 *TrustedCert,
764 IN UINTN CertLength,
765 IN CONST UINT8 *InData,
766 IN UINTN DataLength
767 )
768 {
769 PKCS7 *Pkcs7;
770 BIO *DataBio;
771 BOOLEAN Status;
772 X509 *Cert;
773 X509_STORE *CertStore;
774 UINT8 *SignedData;
775 CONST UINT8 *Temp;
776 UINTN SignedDataSize;
777 BOOLEAN Wrapped;
778
779 //
780 // Check input parameters.
781 //
782 if (P7Data == NULL || TrustedCert == NULL || InData == NULL ||
783 P7Length > INT_MAX || CertLength > INT_MAX || DataLength > INT_MAX) {
784 return FALSE;
785 }
786
787 Pkcs7 = NULL;
788 DataBio = NULL;
789 Cert = NULL;
790 CertStore = NULL;
791
792 //
793 // Register & Initialize necessary digest algorithms for PKCS#7 Handling
794 //
795 if (EVP_add_digest (EVP_md5 ()) == 0) {
796 return FALSE;
797 }
798 if (EVP_add_digest (EVP_sha1 ()) == 0) {
799 return FALSE;
800 }
801 if (EVP_add_digest (EVP_sha256 ()) == 0) {
802 return FALSE;
803 }
804 if (EVP_add_digest (EVP_sha384 ()) == 0) {
805 return FALSE;
806 }
807 if (EVP_add_digest (EVP_sha512 ()) == 0) {
808 return FALSE;
809 }
810 if (EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA) == 0) {
811 return FALSE;
812 }
813
814 Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);
815 if (!Status) {
816 return Status;
817 }
818
819 Status = FALSE;
820
821 //
822 // Retrieve PKCS#7 Data (DER encoding)
823 //
824 if (SignedDataSize > INT_MAX) {
825 goto _Exit;
826 }
827
828 Temp = SignedData;
829 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);
830 if (Pkcs7 == NULL) {
831 goto _Exit;
832 }
833
834 //
835 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
836 //
837 if (!PKCS7_type_is_signed (Pkcs7)) {
838 goto _Exit;
839 }
840
841 //
842 // Read DER-encoded root certificate and Construct X509 Certificate
843 //
844 Temp = TrustedCert;
845 Cert = d2i_X509 (NULL, &Temp, (long) CertLength);
846 if (Cert == NULL) {
847 goto _Exit;
848 }
849
850 //
851 // Setup X509 Store for trusted certificate
852 //
853 CertStore = X509_STORE_new ();
854 if (CertStore == NULL) {
855 goto _Exit;
856 }
857 if (!(X509_STORE_add_cert (CertStore, Cert))) {
858 goto _Exit;
859 }
860
861 //
862 // For generic PKCS#7 handling, InData may be NULL if the content is present
863 // in PKCS#7 structure. So ignore NULL checking here.
864 //
865 DataBio = BIO_new (BIO_s_mem ());
866 if (DataBio == NULL) {
867 goto _Exit;
868 }
869
870 if (BIO_write (DataBio, InData, (int) DataLength) <= 0) {
871 goto _Exit;
872 }
873
874 //
875 // Allow partial certificate chains, terminated by a non-self-signed but
876 // still trusted intermediate certificate. Also disable time checks.
877 //
878 X509_STORE_set_flags (CertStore,
879 X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);
880
881 //
882 // OpenSSL PKCS7 Verification by default checks for SMIME (email signing) and
883 // doesn't support the extended key usage for Authenticode Code Signing.
884 // Bypass the certificate purpose checking by enabling any purposes setting.
885 //
886 X509_STORE_set_purpose (CertStore, X509_PURPOSE_ANY);
887
888 //
889 // Verifies the PKCS#7 signedData structure
890 //
891 Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);
892
893 _Exit:
894 //
895 // Release Resources
896 //
897 BIO_free (DataBio);
898 X509_free (Cert);
899 X509_STORE_free (CertStore);
900 PKCS7_free (Pkcs7);
901
902 if (!Wrapped) {
903 OPENSSL_free (SignedData);
904 }
905
906 return Status;
907 }
908
909 /**
910 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
911 data could be wrapped in a ContentInfo structure.
912
913 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
914 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
915
916 Caution: This function may receive untrusted input. So this function will do
917 basic check for PKCS#7 data structure.
918
919 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
920 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
921 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
922 It's caller's responsibility to free the buffer.
923 @param[out] ContentSize The size of the extracted content in bytes.
924
925 @retval TRUE The P7Data was correctly formatted for processing.
926 @retval FALSE The P7Data was not correctly formatted for processing.
927
928 **/
929 BOOLEAN
930 EFIAPI
931 Pkcs7GetAttachedContent (
932 IN CONST UINT8 *P7Data,
933 IN UINTN P7Length,
934 OUT VOID **Content,
935 OUT UINTN *ContentSize
936 )
937 {
938 BOOLEAN Status;
939 PKCS7 *Pkcs7;
940 UINT8 *SignedData;
941 UINTN SignedDataSize;
942 BOOLEAN Wrapped;
943 CONST UINT8 *Temp;
944 ASN1_OCTET_STRING *OctStr;
945
946 //
947 // Check input parameter.
948 //
949 if ((P7Data == NULL) || (P7Length > INT_MAX) || (Content == NULL) || (ContentSize == NULL)) {
950 return FALSE;
951 }
952
953 *Content = NULL;
954 Pkcs7 = NULL;
955 SignedData = NULL;
956 OctStr = NULL;
957
958 Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);
959 if (!Status || (SignedDataSize > INT_MAX)) {
960 goto _Exit;
961 }
962
963 Status = FALSE;
964
965 //
966 // Decoding PKCS#7 SignedData
967 //
968 Temp = SignedData;
969 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (int)SignedDataSize);
970 if (Pkcs7 == NULL) {
971 goto _Exit;
972 }
973
974 //
975 // The type of Pkcs7 must be signedData
976 //
977 if (!PKCS7_type_is_signed (Pkcs7)) {
978 goto _Exit;
979 }
980
981 //
982 // Check for detached or attached content
983 //
984 if (PKCS7_get_detached (Pkcs7)) {
985 //
986 // No Content supplied for PKCS7 detached signedData
987 //
988 *Content = NULL;
989 *ContentSize = 0;
990 } else {
991 //
992 // Retrieve the attached content in PKCS7 signedData
993 //
994 OctStr = Pkcs7->d.sign->contents->d.data;
995 if ((OctStr->length > 0) && (OctStr->data != NULL)) {
996 *ContentSize = OctStr->length;
997 *Content = malloc (*ContentSize);
998 if (*Content == NULL) {
999 *ContentSize = 0;
1000 goto _Exit;
1001 }
1002 CopyMem (*Content, OctStr->data, *ContentSize);
1003 }
1004 }
1005 Status = TRUE;
1006
1007 _Exit:
1008 //
1009 // Release Resources
1010 //
1011 PKCS7_free (Pkcs7);
1012
1013 if (!Wrapped) {
1014 OPENSSL_free (SignedData);
1015 }
1016
1017 return Status;
1018 }