2 Non-runtime specific implementation of PKCS#7 SignedData Verification Wrapper.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
11 #include <openssl/objects.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/pkcs7.h>
17 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
18 data could be wrapped in a ContentInfo structure.
20 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
21 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
23 Caution: This function may receive untrusted input. So this function will do
24 basic check for PKCS#7 data structure.
26 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
27 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
28 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
29 It's caller's responsibility to free the buffer with FreePool().
30 @param[out] ContentSize The size of the extracted content in bytes.
32 @retval TRUE The P7Data was correctly formatted for processing.
33 @retval FALSE The P7Data was not correctly formatted for processing.
38 Pkcs7GetAttachedContent (
39 IN CONST UINT8
*P7Data
,
42 OUT UINTN
*ContentSize
51 ASN1_OCTET_STRING
*OctStr
;
54 // Check input parameter.
56 if ((P7Data
== NULL
) || (P7Length
> INT_MAX
) || (Content
== NULL
) || (ContentSize
== NULL
)) {
65 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &SignedData
, &SignedDataSize
);
66 if (!Status
|| (SignedDataSize
> INT_MAX
)) {
73 // Decoding PKCS#7 SignedData
76 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **)&Temp
, (int)SignedDataSize
);
82 // The type of Pkcs7 must be signedData
84 if (!PKCS7_type_is_signed (Pkcs7
)) {
89 // Check for detached or attached content
91 if (PKCS7_get_detached (Pkcs7
)) {
93 // No Content supplied for PKCS7 detached signedData
99 // Retrieve the attached content in PKCS7 signedData
101 OctStr
= Pkcs7
->d
.sign
->contents
->d
.data
;
102 if ((OctStr
->length
> 0) && (OctStr
->data
!= NULL
)) {
103 *ContentSize
= OctStr
->length
;
104 *Content
= AllocatePool (*ContentSize
);
105 if (*Content
== NULL
) {
109 CopyMem (*Content
, OctStr
->data
, *ContentSize
);
121 OPENSSL_free (SignedData
);