TianoCore EDK2 master
Loading...
Searching...
No Matches
CryptPkcs7VerifyBase.c
Go to the documentation of this file.
1
9#include "InternalCryptLib.h"
10
11#include <openssl/objects.h>
12#include <openssl/x509.h>
13#include <openssl/x509v3.h>
14#include <openssl/pkcs7.h>
15
27BOOLEAN
29 IN PKCS7 *P7
30 )
31{
32 BOOLEAN Others;
33 INTN Nid = OBJ_obj2nid (P7->type);
34
35 switch (Nid) {
36 case NID_pkcs7_data:
37 case NID_pkcs7_signed:
38 case NID_pkcs7_enveloped:
39 case NID_pkcs7_signedAndEnveloped:
40 case NID_pkcs7_encrypted:
41 Others = FALSE;
42 break;
43 default:
44 Others = TRUE;
45 }
46
47 return Others;
48}
49
60ASN1_OCTET_STRING *
62 IN PKCS7 *P7
63 )
64{
65 if (PKCS7_type_is_data (P7)) {
66 return P7->d.data;
67 }
68
69 if (Pkcs7TypeIsOther (P7) && (P7->d.other != NULL) &&
70 (P7->d.other->type == V_ASN1_OCTET_STRING))
71 {
72 return P7->d.other->value.octet_string;
73 }
74
75 return NULL;
76}
77
98BOOLEAN
99EFIAPI
101 IN CONST UINT8 *P7Data,
102 IN UINTN P7Length,
103 OUT VOID **Content,
104 OUT UINTN *ContentSize
105 )
106{
107 BOOLEAN Status;
108 PKCS7 *Pkcs7;
109 UINT8 *SignedData;
110 UINTN SignedDataSize;
111 BOOLEAN Wrapped;
112 CONST UINT8 *Temp;
113 ASN1_OCTET_STRING *OctStr;
114
115 //
116 // Check input parameter.
117 //
118 if ((P7Data == NULL) || (P7Length > INT_MAX) || (Content == NULL) || (ContentSize == NULL)) {
119 return FALSE;
120 }
121
122 *Content = NULL;
123 Pkcs7 = NULL;
124 SignedData = NULL;
125 OctStr = NULL;
126
127 Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);
128 if (!Status || (SignedDataSize > INT_MAX)) {
129 goto _Exit;
130 }
131
132 Status = FALSE;
133
134 //
135 // Decoding PKCS#7 SignedData
136 //
137 Temp = SignedData;
138 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (int)SignedDataSize);
139 if (Pkcs7 == NULL) {
140 goto _Exit;
141 }
142
143 //
144 // The type of Pkcs7 must be signedData
145 //
146 if (!PKCS7_type_is_signed (Pkcs7)) {
147 goto _Exit;
148 }
149
150 //
151 // Check for detached or attached content
152 //
153 if (PKCS7_get_detached (Pkcs7)) {
154 //
155 // No Content supplied for PKCS7 detached signedData
156 //
157 *Content = NULL;
158 *ContentSize = 0;
159 } else {
160 //
161 // Retrieve the attached content in PKCS7 signedData
162 //
163 OctStr = Pkcs7GetOctetString (Pkcs7->d.sign->contents);
164 if (OctStr == NULL) {
165 goto _Exit;
166 }
167
168 if ((OctStr->length > 0) && (OctStr->data != NULL)) {
169 *ContentSize = OctStr->length;
170 *Content = AllocatePool (*ContentSize);
171 if (*Content == NULL) {
172 *ContentSize = 0;
173 goto _Exit;
174 }
175
176 CopyMem (*Content, OctStr->data, *ContentSize);
177 }
178 }
179
180 Status = TRUE;
181
182_Exit:
183 //
184 // Release Resources
185 //
186 PKCS7_free (Pkcs7);
187
188 if (!Wrapped) {
189 OPENSSL_free (SignedData);
190 }
191
192 return Status;
193}
UINT64 UINTN
INT64 INTN
BOOLEAN WrapPkcs7Data(IN CONST UINT8 *P7Data, IN UINTN P7Length, OUT BOOLEAN *WrapFlag, OUT UINT8 **WrapData, OUT UINTN *WrapDataSize)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
STATIC ASN1_OCTET_STRING * Pkcs7GetOctetString(IN PKCS7 *P7)
BOOLEAN EFIAPI Pkcs7GetAttachedContent(IN CONST UINT8 *P7Data, IN UINTN P7Length, OUT VOID **Content, OUT UINTN *ContentSize)
STATIC BOOLEAN Pkcs7TypeIsOther(IN PKCS7 *P7)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)