2 PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.
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.
10 WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated
11 Variable and will do basic check for data structure.
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
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.
24 #include "InternalCryptLib.h"
26 #include <openssl/objects.h>
27 #include <openssl/x509.h>
28 #include <openssl/x509v3.h>
29 #include <openssl/pkcs7.h>
31 UINT8 mOidValue
[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };
34 Check input P7Data is a wrapped ContentInfo structure or not. If not construct
35 a new structure to wrap P7Data.
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.
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
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
50 @param[out] WrapDataSize Length of ContentInfo structure in bytes.
52 @retval TRUE The operation is finished successfully.
53 @retval FALSE The operation is failed due to lack of resources.
58 IN CONST UINT8
*P7Data
,
60 OUT BOOLEAN
*WrapFlag
,
62 OUT UINTN
*WrapDataSize
69 // Check whether input P7Data is a wrapped ContentInfo structure or not.
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)) {
81 *WrapData
= (UINT8
*) P7Data
;
82 *WrapDataSize
= P7Length
;
85 // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.
87 *WrapDataSize
= P7Length
+ 19;
88 *WrapData
= malloc (*WrapDataSize
);
89 if (*WrapData
== NULL
) {
94 SignedData
= *WrapData
;
100 SignedData
[1] = 0x82;
103 // Part2: Length1 = P7Length + 19 - 4, in big endian.
105 SignedData
[2] = (UINT8
) (((UINT16
) (*WrapDataSize
- 4)) >> 8);
106 SignedData
[3] = (UINT8
) (((UINT16
) (*WrapDataSize
- 4)) & 0xff);
109 // Part3: 0x06, 0x09.
111 SignedData
[4] = 0x06;
112 SignedData
[5] = 0x09;
115 // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.
117 CopyMem (SignedData
+ 6, mOidValue
, sizeof (mOidValue
));
120 // Part5: 0xA0, 0x82.
122 SignedData
[15] = 0xA0;
123 SignedData
[16] = 0x82;
126 // Part6: Length2 = P7Length, in big endian.
128 SignedData
[17] = (UINT8
) (((UINT16
) P7Length
) >> 8);
129 SignedData
[18] = (UINT8
) (((UINT16
) P7Length
) & 0xff);
134 CopyMem (SignedData
+ 19, P7Data
, P7Length
);
142 Pop single certificate from STACK_OF(X509).
144 If X509Stack, Cert, or CertSize is NULL, then return FALSE.
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.
150 @retval TRUE The X509 stack pop succeeded.
151 @retval FALSE The pop operation failed.
163 STACK_OF(X509
) *CertStack
;
172 if ((X509Stack
== NULL
) || (Cert
== NULL
) || (CertSize
== NULL
)) {
176 CertStack
= (STACK_OF(X509
) *) X509Stack
;
178 X509Cert
= sk_X509_pop (CertStack
);
180 if (X509Cert
== NULL
) {
186 CertBio
= BIO_new (BIO_s_mem ());
187 if (CertBio
== NULL
) {
191 Result
= i2d_X509_bio (CertBio
, X509Cert
);
196 BIO_get_mem_ptr (CertBio
, &Ptr
);
197 Length
= (INT32
)(Ptr
->length
);
202 Buffer
= malloc (Length
);
203 if (Buffer
== NULL
) {
207 Result
= BIO_read (CertBio
, Buffer
, Length
);
208 if (Result
!= Length
) {
221 if (!Status
&& (Buffer
!= NULL
)) {
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.
233 If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then
234 return FALSE. If P7Length overflow, then return FALSE.
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.
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.
249 @retval TRUE The operation is finished successfully.
250 @retval FALSE Error occurs during the operation.
256 IN CONST UINT8
*P7Data
,
258 OUT UINT8
**CertStack
,
259 OUT UINTN
*StackLength
,
260 OUT UINT8
**TrustedCert
,
261 OUT UINTN
*CertLength
268 UINTN SignedDataSize
;
270 STACK_OF(X509
) *Stack
;
277 UINTN SingleCertSize
;
279 if ((P7Data
== NULL
) || (CertStack
== NULL
) || (StackLength
== NULL
) ||
280 (TrustedCert
== NULL
) || (CertLength
== NULL
) || (P7Length
> INT_MAX
)) {
284 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &SignedData
, &SignedDataSize
);
297 // Retrieve PKCS#7 Data (DER encoding)
299 if (SignedDataSize
> INT_MAX
) {
304 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **) &Temp
, (int) SignedDataSize
);
310 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
312 if (!PKCS7_type_is_signed (Pkcs7
)) {
316 Stack
= PKCS7_get0_signers(Pkcs7
, NULL
, PKCS7_BINARY
);
322 // Convert CertStack to buffer in following format:
324 // UINT32 Cert1Length;
326 // UINT32 Cert2Length;
329 // UINT32 CertnLength;
332 BufferSize
= sizeof (UINT8
);
333 OldSize
= BufferSize
;
335 for (Index
= 0; ; Index
++) {
336 Status
= X509PopCertificate (Stack
, &SingleCert
, &SingleCertSize
);
341 OldSize
= BufferSize
;
343 BufferSize
= OldSize
+ SingleCertSize
+ sizeof (UINT32
);
344 CertBuf
= malloc (BufferSize
);
346 if (CertBuf
== NULL
) {
350 if (OldBuf
!= NULL
) {
351 CopyMem (CertBuf
, OldBuf
, OldSize
);
356 WriteUnaligned32 ((UINT32
*) (CertBuf
+ OldSize
), (UINT32
) SingleCertSize
);
357 CopyMem (CertBuf
+ OldSize
+ sizeof (UINT32
), SingleCert
, SingleCertSize
);
363 if (CertBuf
!= NULL
) {
365 // Update CertNumber.
369 *CertLength
= BufferSize
- OldSize
- sizeof (UINT32
);
370 *TrustedCert
= malloc (*CertLength
);
371 if (*TrustedCert
== NULL
) {
375 CopyMem (*TrustedCert
, CertBuf
+ OldSize
+ sizeof (UINT32
), *CertLength
);
376 *CertStack
= CertBuf
;
377 *StackLength
= BufferSize
;
394 sk_X509_pop_free(Stack
, X509_free
);
397 if (SingleCert
!= NULL
) {
401 if (!Status
&& (CertBuf
!= NULL
)) {
406 if (OldBuf
!= NULL
) {
414 Wrap function to use free() to free allocated memory for certificates.
416 @param[in] Certs Pointer to the certificates to be freed.
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.
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.
447 @retval TRUE The operation is finished successfully.
448 @retval FALSE Error occurs during the operation.
453 Pkcs7GetCertificatesList (
454 IN CONST UINT8
*P7Data
,
456 OUT UINT8
**SignerChainCerts
,
457 OUT UINTN
*ChainLength
,
458 OUT UINT8
**UnchainCerts
,
459 OUT UINTN
*UnchainLength
468 X509_STORE_CTX
*CertCtx
;
469 STACK_OF(X509
) *CtxChain
;
470 STACK_OF(X509
) *CtxUntrusted
;
472 STACK_OF(X509
) *Signers
;
476 X509_NAME
*IssuerName
;
500 ZeroMem (&CertCtx
, sizeof (CertCtx
));
503 // Parameter Checking
505 if ((P7Data
== NULL
) || (SignerChainCerts
== NULL
) || (ChainLength
== NULL
) ||
506 (UnchainCerts
== NULL
) || (UnchainLength
== NULL
) || (P7Length
> INT_MAX
)) {
510 *SignerChainCerts
= NULL
;
512 *UnchainCerts
= NULL
;
516 // Construct a new PKCS#7 data wrapping with ContentInfo structure if needed.
518 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &NewP7Data
, &NewP7Length
);
519 if (!Status
|| (NewP7Length
> INT_MAX
)) {
524 // Decodes PKCS#7 SignedData
526 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **) &NewP7Data
, (int) NewP7Length
);
527 if ((Pkcs7
== NULL
) || (!PKCS7_type_is_signed (Pkcs7
))) {
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.
536 Signers
= PKCS7_get0_signers (Pkcs7
, NULL
, PKCS7_BINARY
);
537 if ((Signers
== NULL
) || (sk_X509_num (Signers
) != 1)) {
540 Signer
= sk_X509_value (Signers
, 0);
542 CertCtx
= X509_STORE_CTX_new ();
543 if (CertCtx
== NULL
) {
546 if (!X509_STORE_CTX_init (CertCtx
, NULL
, Signer
, Pkcs7
->d
.sign
->cert
)) {
550 // Initialize Chained & Untrusted stack
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
))) {
560 CtxUntrusted
= X509_STORE_CTX_get0_untrusted (CertCtx
);
561 if (CtxUntrusted
!= NULL
) {
562 (VOID
)sk_X509_delete_ptr (CtxUntrusted
, Signer
);
566 // Build certificates stack chained from Signer's certificate.
571 // Self-Issue checking
574 if (X509_STORE_CTX_get1_issuer (&Issuer
, CertCtx
, Cert
) == 1) {
575 if (X509_cmp (Issuer
, Cert
) == 0) {
581 // Found the issuer of the current certificate
583 if (CtxUntrusted
!= NULL
) {
585 IssuerName
= X509_get_issuer_name (Cert
);
586 Issuer
= X509_find_by_subject (CtxUntrusted
, IssuerName
);
587 if (Issuer
!= NULL
) {
588 if (!sk_X509_push (CtxChain
, Issuer
)) {
591 (VOID
)sk_X509_delete_ptr (CtxUntrusted
, Issuer
);
602 // Converts Chained and Untrusted Certificate to Certificate Buffer in following format:
604 // UINT32 Cert1Length;
606 // UINT32 Cert2Length;
609 // UINT32 CertnLength;
613 if (CtxChain
!= NULL
) {
614 BufferSize
= sizeof (UINT8
);
617 for (Index
= 0; ; Index
++) {
618 Status
= X509PopCertificate (CtxChain
, &SingleCert
, &CertSize
);
623 OldSize
= BufferSize
;
625 BufferSize
= OldSize
+ CertSize
+ sizeof (UINT32
);
626 CertBuf
= malloc (BufferSize
);
628 if (CertBuf
== NULL
) {
632 if (OldBuf
!= NULL
) {
633 CopyMem (CertBuf
, OldBuf
, OldSize
);
638 WriteUnaligned32 ((UINT32
*) (CertBuf
+ OldSize
), (UINT32
) CertSize
);
639 CopyMem (CertBuf
+ OldSize
+ sizeof (UINT32
), SingleCert
, CertSize
);
645 if (CertBuf
!= NULL
) {
647 // Update CertNumber.
651 *SignerChainCerts
= CertBuf
;
652 *ChainLength
= BufferSize
;
656 if (CtxUntrusted
!= NULL
) {
657 BufferSize
= sizeof (UINT8
);
660 for (Index
= 0; ; Index
++) {
661 Status
= X509PopCertificate (CtxUntrusted
, &SingleCert
, &CertSize
);
666 OldSize
= BufferSize
;
668 BufferSize
= OldSize
+ CertSize
+ sizeof (UINT32
);
669 CertBuf
= malloc (BufferSize
);
671 if (CertBuf
== NULL
) {
675 if (OldBuf
!= NULL
) {
676 CopyMem (CertBuf
, OldBuf
, OldSize
);
681 WriteUnaligned32 ((UINT32
*) (CertBuf
+ OldSize
), (UINT32
) CertSize
);
682 CopyMem (CertBuf
+ OldSize
+ sizeof (UINT32
), SingleCert
, CertSize
);
688 if (CertBuf
!= NULL
) {
690 // Update CertNumber.
694 *UnchainCerts
= CertBuf
;
695 *UnchainLength
= BufferSize
;
703 // Release Resources.
705 if (!Wrapped
&& (NewP7Data
!= NULL
)) {
712 sk_X509_free (Signers
);
714 if (CertCtx
!= NULL
) {
715 X509_STORE_CTX_cleanup (CertCtx
);
716 X509_STORE_CTX_free (CertCtx
);
719 if (SingleCert
!= NULL
) {
723 if (OldBuf
!= NULL
) {
727 if (!Status
&& (CertBuf
!= NULL
)) {
729 *SignerChainCerts
= NULL
;
730 *UnchainCerts
= NULL
;
737 Verifies the validity of a PKCS#7 signed data as described in "PKCS #7:
738 Cryptographic Message Syntax Standard". The input signed data could be wrapped
739 in a ContentInfo structure.
741 If P7Data, TrustedCert or InData is NULL, then return FALSE.
742 If P7Length, CertLength or DataLength overflow, then return FALSE.
744 Caution: This function may receive untrusted input.
745 UEFI Authenticated Variable is external input, so this function will do basic
746 check for PKCS#7 data structure.
748 @param[in] P7Data Pointer to the PKCS#7 message to verify.
749 @param[in] P7Length Length of the PKCS#7 message in bytes.
750 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
751 is used for certificate chain verification.
752 @param[in] CertLength Length of the trusted certificate in bytes.
753 @param[in] InData Pointer to the content to be verified.
754 @param[in] DataLength Length of InData in bytes.
756 @retval TRUE The specified PKCS#7 signed data is valid.
757 @retval FALSE Invalid PKCS#7 signed data.
763 IN CONST UINT8
*P7Data
,
765 IN CONST UINT8
*TrustedCert
,
767 IN CONST UINT8
*InData
,
775 X509_STORE
*CertStore
;
778 UINTN SignedDataSize
;
782 // Check input parameters.
784 if (P7Data
== NULL
|| TrustedCert
== NULL
|| InData
== NULL
||
785 P7Length
> INT_MAX
|| CertLength
> INT_MAX
|| DataLength
> INT_MAX
) {
795 // Register & Initialize necessary digest algorithms for PKCS#7 Handling
797 if (EVP_add_digest (EVP_md5 ()) == 0) {
800 if (EVP_add_digest (EVP_sha1 ()) == 0) {
803 if (EVP_add_digest (EVP_sha256 ()) == 0) {
806 if (EVP_add_digest (EVP_sha384 ()) == 0) {
809 if (EVP_add_digest (EVP_sha512 ()) == 0) {
812 if (EVP_add_digest_alias (SN_sha1WithRSAEncryption
, SN_sha1WithRSA
) == 0) {
816 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &SignedData
, &SignedDataSize
);
824 // Retrieve PKCS#7 Data (DER encoding)
826 if (SignedDataSize
> INT_MAX
) {
831 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **) &Temp
, (int) SignedDataSize
);
837 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
839 if (!PKCS7_type_is_signed (Pkcs7
)) {
844 // Read DER-encoded root certificate and Construct X509 Certificate
847 Cert
= d2i_X509 (NULL
, &Temp
, (long) CertLength
);
853 // Setup X509 Store for trusted certificate
855 CertStore
= X509_STORE_new ();
856 if (CertStore
== NULL
) {
859 if (!(X509_STORE_add_cert (CertStore
, Cert
))) {
864 // For generic PKCS#7 handling, InData may be NULL if the content is present
865 // in PKCS#7 structure. So ignore NULL checking here.
867 DataBio
= BIO_new (BIO_s_mem ());
868 if (DataBio
== NULL
) {
872 if (BIO_write (DataBio
, InData
, (int) DataLength
) <= 0) {
877 // Allow partial certificate chains, terminated by a non-self-signed but
878 // still trusted intermediate certificate. Also disable time checks.
880 X509_STORE_set_flags (CertStore
,
881 X509_V_FLAG_PARTIAL_CHAIN
| X509_V_FLAG_NO_CHECK_TIME
);
884 // OpenSSL PKCS7 Verification by default checks for SMIME (email signing) and
885 // doesn't support the extended key usage for Authenticode Code Signing.
886 // Bypass the certificate purpose checking by enabling any purposes setting.
888 X509_STORE_set_purpose (CertStore
, X509_PURPOSE_ANY
);
891 // Verifies the PKCS#7 signedData structure
893 Status
= (BOOLEAN
) PKCS7_verify (Pkcs7
, NULL
, CertStore
, DataBio
, NULL
, PKCS7_BINARY
);
901 X509_STORE_free (CertStore
);
905 OPENSSL_free (SignedData
);
912 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
913 data could be wrapped in a ContentInfo structure.
915 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
916 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
918 Caution: This function may receive untrusted input. So this function will do
919 basic check for PKCS#7 data structure.
921 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
922 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
923 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
924 It's caller's responsibility to free the buffer.
925 @param[out] ContentSize The size of the extracted content in bytes.
927 @retval TRUE The P7Data was correctly formatted for processing.
928 @retval FALSE The P7Data was not correctly formatted for processing.
933 Pkcs7GetAttachedContent (
934 IN CONST UINT8
*P7Data
,
937 OUT UINTN
*ContentSize
943 UINTN SignedDataSize
;
946 ASN1_OCTET_STRING
*OctStr
;
949 // Check input parameter.
951 if ((P7Data
== NULL
) || (P7Length
> INT_MAX
) || (Content
== NULL
) || (ContentSize
== NULL
)) {
960 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &SignedData
, &SignedDataSize
);
961 if (!Status
|| (SignedDataSize
> INT_MAX
)) {
968 // Decoding PKCS#7 SignedData
971 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **)&Temp
, (int)SignedDataSize
);
977 // The type of Pkcs7 must be signedData
979 if (!PKCS7_type_is_signed (Pkcs7
)) {
984 // Check for detached or attached content
986 if (PKCS7_get_detached (Pkcs7
)) {
988 // No Content supplied for PKCS7 detached signedData
994 // Retrieve the attached content in PKCS7 signedData
996 OctStr
= Pkcs7
->d
.sign
->contents
->d
.data
;
997 if ((OctStr
->length
> 0) && (OctStr
->data
!= NULL
)) {
998 *ContentSize
= OctStr
->length
;
999 *Content
= malloc (*ContentSize
);
1000 if (*Content
== NULL
) {
1004 CopyMem (*Content
, OctStr
->data
, *ContentSize
);
1011 // Release Resources
1016 OPENSSL_free (SignedData
);