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 Check the contents of PKCS7 is not data.
19 It is copied from PKCS7_type_is_other() in pk7_doit.c.
21 @param[in] P7 Pointer to the location at which the PKCS7 is located.
23 @retval TRUE If the type is others.
24 @retval FALSE If the type is expected.
33 INTN Nid
= OBJ_obj2nid (P7
->type
);
37 case NID_pkcs7_signed
:
38 case NID_pkcs7_enveloped
:
39 case NID_pkcs7_signedAndEnveloped
:
40 case NID_pkcs7_encrypted
:
51 Get the ASN.1 string for the PKCS7.
53 It is copied from PKCS7_get_octet_string() in pk7_doit.c.
55 @param[in] P7 Pointer to the location at which the PKCS7 is located.
57 @return ASN1_OCTET_STRING ASN.1 string.
65 if (PKCS7_type_is_data (P7
)) {
69 if (Pkcs7TypeIsOther(P7
) && (P7
->d
.other
!= NULL
) &&
70 (P7
->d
.other
->type
== V_ASN1_OCTET_STRING
)) {
71 return P7
->d
.other
->value
.octet_string
;
78 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
79 data could be wrapped in a ContentInfo structure.
81 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
82 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
84 Caution: This function may receive untrusted input. So this function will do
85 basic check for PKCS#7 data structure.
87 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
88 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
89 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
90 It's caller's responsibility to free the buffer with FreePool().
91 @param[out] ContentSize The size of the extracted content in bytes.
93 @retval TRUE The P7Data was correctly formatted for processing.
94 @retval FALSE The P7Data was not correctly formatted for processing.
99 Pkcs7GetAttachedContent (
100 IN CONST UINT8
*P7Data
,
103 OUT UINTN
*ContentSize
109 UINTN SignedDataSize
;
112 ASN1_OCTET_STRING
*OctStr
;
115 // Check input parameter.
117 if ((P7Data
== NULL
) || (P7Length
> INT_MAX
) || (Content
== NULL
) || (ContentSize
== NULL
)) {
126 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &SignedData
, &SignedDataSize
);
127 if (!Status
|| (SignedDataSize
> INT_MAX
)) {
134 // Decoding PKCS#7 SignedData
137 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **)&Temp
, (int)SignedDataSize
);
143 // The type of Pkcs7 must be signedData
145 if (!PKCS7_type_is_signed (Pkcs7
)) {
150 // Check for detached or attached content
152 if (PKCS7_get_detached (Pkcs7
)) {
154 // No Content supplied for PKCS7 detached signedData
160 // Retrieve the attached content in PKCS7 signedData
162 OctStr
= Pkcs7GetOctetString (Pkcs7
->d
.sign
->contents
);
163 if (OctStr
== NULL
) {
167 if ((OctStr
->length
> 0) && (OctStr
->data
!= NULL
)) {
168 *ContentSize
= OctStr
->length
;
169 *Content
= AllocatePool (*ContentSize
);
170 if (*Content
== NULL
) {
174 CopyMem (*Content
, OctStr
->data
, *ContentSize
);
186 OPENSSL_free (SignedData
);