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
))
72 return P7
->d
.other
->value
.octet_string
;
79 Extracts the attached content from a PKCS#7 signed data if existed. The input signed
80 data could be wrapped in a ContentInfo structure.
82 If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,
83 then return FALSE. If the P7Data is not correctly formatted, then return FALSE.
85 Caution: This function may receive untrusted input. So this function will do
86 basic check for PKCS#7 data structure.
88 @param[in] P7Data Pointer to the PKCS#7 signed data to process.
89 @param[in] P7Length Length of the PKCS#7 signed data in bytes.
90 @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.
91 It's caller's responsibility to free the buffer with FreePool().
92 @param[out] ContentSize The size of the extracted content in bytes.
94 @retval TRUE The P7Data was correctly formatted for processing.
95 @retval FALSE The P7Data was not correctly formatted for processing.
100 Pkcs7GetAttachedContent (
101 IN CONST UINT8
*P7Data
,
104 OUT UINTN
*ContentSize
110 UINTN SignedDataSize
;
113 ASN1_OCTET_STRING
*OctStr
;
116 // Check input parameter.
118 if ((P7Data
== NULL
) || (P7Length
> INT_MAX
) || (Content
== NULL
) || (ContentSize
== NULL
)) {
127 Status
= WrapPkcs7Data (P7Data
, P7Length
, &Wrapped
, &SignedData
, &SignedDataSize
);
128 if (!Status
|| (SignedDataSize
> INT_MAX
)) {
135 // Decoding PKCS#7 SignedData
138 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **)&Temp
, (int)SignedDataSize
);
144 // The type of Pkcs7 must be signedData
146 if (!PKCS7_type_is_signed (Pkcs7
)) {
151 // Check for detached or attached content
153 if (PKCS7_get_detached (Pkcs7
)) {
155 // No Content supplied for PKCS7 detached signedData
161 // Retrieve the attached content in PKCS7 signedData
163 OctStr
= Pkcs7GetOctetString (Pkcs7
->d
.sign
->contents
);
164 if (OctStr
== NULL
) {
168 if ((OctStr
->length
> 0) && (OctStr
->data
!= NULL
)) {
169 *ContentSize
= OctStr
->length
;
170 *Content
= AllocatePool (*ContentSize
);
171 if (*Content
== NULL
) {
176 CopyMem (*Content
, OctStr
->data
, *ContentSize
);
189 OPENSSL_free (SignedData
);