]>
Commit | Line | Data |
---|---|---|
532616bb | 1 | /** @file\r |
2 | PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.\r | |
3 | \r | |
4 | Caution: This module requires additional review when modified.\r | |
5 | This library will have external input - signature (e.g. UEFI Authenticated\r | |
6 | Variable). It may by input in SMM mode.\r | |
7 | This external input must be validated carefully to avoid security issue like\r | |
8 | buffer overflow, integer overflow.\r | |
9 | \r | |
10 | WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated\r | |
11 | Variable and will do basic check for data structure.\r | |
12 | \r | |
1463ce18 | 13 | Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r |
532616bb | 14 | This program and the accompanying materials\r |
15 | are licensed and made available under the terms and conditions of the BSD License\r | |
16 | which accompanies this distribution. The full text of the license may be found at\r | |
17 | http://opensource.org/licenses/bsd-license.php\r | |
18 | \r | |
19 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
20 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
21 | \r | |
22 | **/\r | |
23 | \r | |
24 | #include "InternalCryptLib.h"\r | |
25 | \r | |
26 | #include <openssl/objects.h>\r | |
27 | #include <openssl/x509.h>\r | |
02ee8d3b | 28 | #include <openssl/x509v3.h>\r |
532616bb | 29 | #include <openssl/pkcs7.h>\r |
30 | \r | |
31 | UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };\r | |
32 | \r | |
532616bb | 33 | /**\r |
34 | Check input P7Data is a wrapped ContentInfo structure or not. If not construct\r | |
35 | a new structure to wrap P7Data.\r | |
36 | \r | |
37 | Caution: This function may receive untrusted input.\r | |
38 | UEFI Authenticated Variable is external input, so this function will do basic\r | |
39 | check for PKCS#7 data structure.\r | |
40 | \r | |
41 | @param[in] P7Data Pointer to the PKCS#7 message to verify.\r | |
42 | @param[in] P7Length Length of the PKCS#7 message in bytes.\r | |
43 | @param[out] WrapFlag If TRUE P7Data is a ContentInfo structure, otherwise\r | |
44 | return FALSE.\r | |
2ac68e8b | 45 | @param[out] WrapData If return status of this function is TRUE:\r |
532616bb | 46 | 1) when WrapFlag is TRUE, pointer to P7Data.\r |
47 | 2) when WrapFlag is FALSE, pointer to a new ContentInfo\r | |
48 | structure. It's caller's responsibility to free this\r | |
49 | buffer.\r | |
50 | @param[out] WrapDataSize Length of ContentInfo structure in bytes.\r | |
51 | \r | |
52 | @retval TRUE The operation is finished successfully.\r | |
53 | @retval FALSE The operation is failed due to lack of resources.\r | |
54 | \r | |
55 | **/\r | |
56 | BOOLEAN\r | |
57 | WrapPkcs7Data (\r | |
58 | IN CONST UINT8 *P7Data,\r | |
59 | IN UINTN P7Length,\r | |
60 | OUT BOOLEAN *WrapFlag,\r | |
61 | OUT UINT8 **WrapData,\r | |
62 | OUT UINTN *WrapDataSize\r | |
63 | )\r | |
64 | {\r | |
65 | BOOLEAN Wrapped;\r | |
66 | UINT8 *SignedData;\r | |
67 | \r | |
68 | //\r | |
69 | // Check whether input P7Data is a wrapped ContentInfo structure or not.\r | |
70 | //\r | |
71 | Wrapped = FALSE;\r | |
72 | if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {\r | |
73 | if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {\r | |
74 | if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {\r | |
75 | Wrapped = TRUE;\r | |
76 | }\r | |
77 | }\r | |
78 | }\r | |
79 | \r | |
80 | if (Wrapped) {\r | |
81 | *WrapData = (UINT8 *) P7Data;\r | |
82 | *WrapDataSize = P7Length;\r | |
83 | } else {\r | |
84 | //\r | |
85 | // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.\r | |
86 | //\r | |
87 | *WrapDataSize = P7Length + 19;\r | |
88 | *WrapData = malloc (*WrapDataSize);\r | |
89 | if (*WrapData == NULL) {\r | |
90 | *WrapFlag = Wrapped;\r | |
91 | return FALSE;\r | |
92 | }\r | |
93 | \r | |
94 | SignedData = *WrapData;\r | |
95 | \r | |
96 | //\r | |
97 | // Part1: 0x30, 0x82.\r | |
98 | //\r | |
99 | SignedData[0] = 0x30;\r | |
100 | SignedData[1] = 0x82;\r | |
101 | \r | |
102 | //\r | |
103 | // Part2: Length1 = P7Length + 19 - 4, in big endian.\r | |
104 | //\r | |
105 | SignedData[2] = (UINT8) (((UINT16) (*WrapDataSize - 4)) >> 8);\r | |
106 | SignedData[3] = (UINT8) (((UINT16) (*WrapDataSize - 4)) & 0xff);\r | |
107 | \r | |
108 | //\r | |
109 | // Part3: 0x06, 0x09.\r | |
110 | //\r | |
111 | SignedData[4] = 0x06;\r | |
112 | SignedData[5] = 0x09;\r | |
113 | \r | |
114 | //\r | |
115 | // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.\r | |
116 | //\r | |
117 | CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));\r | |
118 | \r | |
119 | //\r | |
120 | // Part5: 0xA0, 0x82.\r | |
121 | //\r | |
122 | SignedData[15] = 0xA0;\r | |
123 | SignedData[16] = 0x82;\r | |
124 | \r | |
125 | //\r | |
126 | // Part6: Length2 = P7Length, in big endian.\r | |
127 | //\r | |
128 | SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);\r | |
129 | SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);\r | |
130 | \r | |
131 | //\r | |
132 | // Part7: P7Data.\r | |
133 | //\r | |
134 | CopyMem (SignedData + 19, P7Data, P7Length);\r | |
135 | }\r | |
136 | \r | |
137 | *WrapFlag = Wrapped;\r | |
138 | return TRUE;\r | |
139 | }\r | |
140 | \r | |
efad60c5 | 141 | /**\r |
142 | Pop single certificate from STACK_OF(X509).\r | |
143 | \r | |
144 | If X509Stack, Cert, or CertSize is NULL, then return FALSE.\r | |
145 | \r | |
146 | @param[in] X509Stack Pointer to a X509 stack object.\r | |
147 | @param[out] Cert Pointer to a X509 certificate.\r | |
148 | @param[out] CertSize Length of output X509 certificate in bytes.\r | |
2ac68e8b | 149 | \r |
efad60c5 | 150 | @retval TRUE The X509 stack pop succeeded.\r |
151 | @retval FALSE The pop operation failed.\r | |
152 | \r | |
153 | **/\r | |
154 | BOOLEAN\r | |
155 | X509PopCertificate (\r | |
156 | IN VOID *X509Stack,\r | |
157 | OUT UINT8 **Cert,\r | |
158 | OUT UINTN *CertSize\r | |
159 | )\r | |
160 | {\r | |
161 | BIO *CertBio;\r | |
162 | X509 *X509Cert;\r | |
163 | STACK_OF(X509) *CertStack;\r | |
164 | BOOLEAN Status;\r | |
eb58f559 | 165 | INT32 Result;\r |
166 | INT32 Length;\r | |
efad60c5 | 167 | VOID *Buffer;\r |
168 | \r | |
169 | Status = FALSE;\r | |
170 | \r | |
171 | if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) {\r | |
172 | return Status;\r | |
173 | }\r | |
174 | \r | |
175 | CertStack = (STACK_OF(X509) *) X509Stack;\r | |
176 | \r | |
177 | X509Cert = sk_X509_pop (CertStack);\r | |
178 | \r | |
179 | if (X509Cert == NULL) {\r | |
180 | return Status;\r | |
181 | }\r | |
182 | \r | |
183 | Buffer = NULL;\r | |
184 | \r | |
185 | CertBio = BIO_new (BIO_s_mem ());\r | |
186 | if (CertBio == NULL) {\r | |
187 | return Status;\r | |
188 | }\r | |
189 | \r | |
190 | Result = i2d_X509_bio (CertBio, X509Cert);\r | |
191 | if (Result == 0) {\r | |
192 | goto _Exit;\r | |
193 | }\r | |
194 | \r | |
1463ce18 | 195 | Length = (INT32)(((BUF_MEM *) CertBio->ptr)->length);\r |
efad60c5 | 196 | if (Length <= 0) {\r |
197 | goto _Exit;\r | |
198 | }\r | |
199 | \r | |
200 | Buffer = malloc (Length);\r | |
201 | if (Buffer == NULL) {\r | |
202 | goto _Exit;\r | |
203 | }\r | |
204 | \r | |
205 | Result = BIO_read (CertBio, Buffer, Length);\r | |
206 | if (Result != Length) {\r | |
207 | goto _Exit;\r | |
208 | }\r | |
209 | \r | |
210 | *Cert = Buffer;\r | |
211 | *CertSize = Length;\r | |
212 | \r | |
213 | Status = TRUE;\r | |
214 | \r | |
215 | _Exit:\r | |
216 | \r | |
217 | BIO_free (CertBio);\r | |
218 | \r | |
219 | if (!Status && (Buffer != NULL)) {\r | |
220 | free (Buffer);\r | |
221 | }\r | |
222 | \r | |
223 | return Status;\r | |
224 | }\r | |
225 | \r | |
532616bb | 226 | /**\r |
227 | Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7:\r | |
228 | Cryptographic Message Syntax Standard". The input signed data could be wrapped\r | |
229 | in a ContentInfo structure.\r | |
230 | \r | |
231 | If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then\r | |
232 | return FALSE. If P7Length overflow, then return FAlSE.\r | |
233 | \r | |
234 | Caution: This function may receive untrusted input.\r | |
235 | UEFI Authenticated Variable is external input, so this function will do basic\r | |
236 | check for PKCS#7 data structure.\r | |
237 | \r | |
238 | @param[in] P7Data Pointer to the PKCS#7 message to verify.\r | |
239 | @param[in] P7Length Length of the PKCS#7 message in bytes.\r | |
240 | @param[out] CertStack Pointer to Signer's certificates retrieved from P7Data.\r | |
241 | It's caller's responsiblity to free the buffer.\r | |
242 | @param[out] StackLength Length of signer's certificates in bytes.\r | |
243 | @param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates.\r | |
244 | It's caller's responsiblity to free the buffer.\r | |
245 | @param[out] CertLength Length of the trusted certificate in bytes.\r | |
246 | \r | |
247 | @retval TRUE The operation is finished successfully.\r | |
248 | @retval FALSE Error occurs during the operation.\r | |
249 | \r | |
250 | **/\r | |
251 | BOOLEAN\r | |
252 | EFIAPI\r | |
253 | Pkcs7GetSigners (\r | |
254 | IN CONST UINT8 *P7Data,\r | |
255 | IN UINTN P7Length,\r | |
256 | OUT UINT8 **CertStack,\r | |
257 | OUT UINTN *StackLength,\r | |
258 | OUT UINT8 **TrustedCert,\r | |
259 | OUT UINTN *CertLength\r | |
260 | )\r | |
261 | {\r | |
262 | PKCS7 *Pkcs7;\r | |
263 | BOOLEAN Status;\r | |
264 | UINT8 *SignedData;\r | |
1463ce18 | 265 | CONST UINT8 *Temp;\r |
532616bb | 266 | UINTN SignedDataSize;\r |
267 | BOOLEAN Wrapped;\r | |
268 | STACK_OF(X509) *Stack;\r | |
269 | UINT8 Index;\r | |
270 | UINT8 *CertBuf;\r | |
271 | UINT8 *OldBuf;\r | |
272 | UINTN BufferSize;\r | |
273 | UINTN OldSize;\r | |
274 | UINT8 *SingleCert;\r | |
275 | UINTN SingleCertSize;\r | |
276 | \r | |
277 | if ((P7Data == NULL) || (CertStack == NULL) || (StackLength == NULL) ||\r | |
278 | (TrustedCert == NULL) || (CertLength == NULL) || (P7Length > INT_MAX)) {\r | |
279 | return FALSE;\r | |
280 | }\r | |
2ac68e8b | 281 | \r |
532616bb | 282 | Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);\r |
283 | if (!Status) {\r | |
284 | return Status;\r | |
285 | }\r | |
286 | \r | |
287 | Status = FALSE;\r | |
288 | Pkcs7 = NULL;\r | |
289 | Stack = NULL;\r | |
290 | CertBuf = NULL;\r | |
291 | OldBuf = NULL;\r | |
292 | SingleCert = NULL;\r | |
293 | \r | |
294 | //\r | |
295 | // Retrieve PKCS#7 Data (DER encoding)\r | |
296 | //\r | |
297 | if (SignedDataSize > INT_MAX) {\r | |
298 | goto _Exit;\r | |
299 | }\r | |
300 | \r | |
301 | Temp = SignedData;\r | |
302 | Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);\r | |
303 | if (Pkcs7 == NULL) {\r | |
304 | goto _Exit;\r | |
305 | }\r | |
306 | \r | |
307 | //\r | |
308 | // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r | |
309 | //\r | |
310 | if (!PKCS7_type_is_signed (Pkcs7)) {\r | |
311 | goto _Exit;\r | |
312 | }\r | |
313 | \r | |
314 | Stack = PKCS7_get0_signers(Pkcs7, NULL, PKCS7_BINARY);\r | |
315 | if (Stack == NULL) {\r | |
316 | goto _Exit;\r | |
317 | }\r | |
318 | \r | |
319 | //\r | |
320 | // Convert CertStack to buffer in following format:\r | |
321 | // UINT8 CertNumber;\r | |
322 | // UINT32 Cert1Length;\r | |
323 | // UINT8 Cert1[];\r | |
324 | // UINT32 Cert2Length;\r | |
325 | // UINT8 Cert2[];\r | |
326 | // ...\r | |
327 | // UINT32 CertnLength;\r | |
328 | // UINT8 Certn[];\r | |
329 | //\r | |
330 | BufferSize = sizeof (UINT8);\r | |
331 | OldSize = BufferSize;\r | |
2ac68e8b | 332 | \r |
532616bb | 333 | for (Index = 0; ; Index++) {\r |
334 | Status = X509PopCertificate (Stack, &SingleCert, &SingleCertSize);\r | |
335 | if (!Status) {\r | |
336 | break;\r | |
337 | }\r | |
338 | \r | |
339 | OldSize = BufferSize;\r | |
340 | OldBuf = CertBuf;\r | |
341 | BufferSize = OldSize + SingleCertSize + sizeof (UINT32);\r | |
342 | CertBuf = malloc (BufferSize);\r | |
343 | \r | |
344 | if (CertBuf == NULL) {\r | |
345 | goto _Exit;\r | |
346 | }\r | |
347 | \r | |
348 | if (OldBuf != NULL) {\r | |
349 | CopyMem (CertBuf, OldBuf, OldSize);\r | |
350 | free (OldBuf);\r | |
351 | OldBuf = NULL;\r | |
352 | }\r | |
353 | \r | |
354 | WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) SingleCertSize);\r | |
355 | CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, SingleCertSize);\r | |
356 | \r | |
357 | free (SingleCert);\r | |
358 | SingleCert = NULL;\r | |
359 | }\r | |
360 | \r | |
361 | if (CertBuf != NULL) {\r | |
362 | //\r | |
363 | // Update CertNumber.\r | |
364 | //\r | |
365 | CertBuf[0] = Index;\r | |
366 | \r | |
367 | *CertLength = BufferSize - OldSize - sizeof (UINT32);\r | |
368 | *TrustedCert = malloc (*CertLength);\r | |
369 | if (*TrustedCert == NULL) {\r | |
370 | goto _Exit;\r | |
371 | }\r | |
372 | \r | |
373 | CopyMem (*TrustedCert, CertBuf + OldSize + sizeof (UINT32), *CertLength);\r | |
374 | *CertStack = CertBuf;\r | |
375 | *StackLength = BufferSize;\r | |
376 | Status = TRUE;\r | |
2ac68e8b | 377 | }\r |
532616bb | 378 | \r |
379 | _Exit:\r | |
380 | //\r | |
381 | // Release Resources\r | |
382 | //\r | |
383 | if (!Wrapped) {\r | |
384 | free (SignedData);\r | |
385 | }\r | |
386 | \r | |
387 | if (Pkcs7 != NULL) {\r | |
388 | PKCS7_free (Pkcs7);\r | |
389 | }\r | |
390 | \r | |
391 | if (Stack != NULL) {\r | |
392 | sk_X509_pop_free(Stack, X509_free);\r | |
393 | }\r | |
394 | \r | |
395 | if (SingleCert != NULL) {\r | |
396 | free (SingleCert);\r | |
397 | }\r | |
398 | \r | |
399 | if (!Status && (CertBuf != NULL)) {\r | |
400 | free (CertBuf);\r | |
401 | *CertStack = NULL;\r | |
402 | }\r | |
403 | \r | |
404 | if (OldBuf != NULL) {\r | |
405 | free (OldBuf);\r | |
406 | }\r | |
2ac68e8b | 407 | \r |
532616bb | 408 | return Status;\r |
409 | }\r | |
410 | \r | |
411 | /**\r | |
412 | Wrap function to use free() to free allocated memory for certificates.\r | |
413 | \r | |
414 | @param[in] Certs Pointer to the certificates to be freed.\r | |
415 | \r | |
416 | **/\r | |
417 | VOID\r | |
418 | EFIAPI\r | |
419 | Pkcs7FreeSigners (\r | |
420 | IN UINT8 *Certs\r | |
421 | )\r | |
422 | {\r | |
423 | if (Certs == NULL) {\r | |
424 | return;\r | |
425 | }\r | |
426 | \r | |
427 | free (Certs);\r | |
428 | }\r | |
429 | \r | |
430 | /**\r | |
431 | Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:\r | |
432 | Cryptographic Message Syntax Standard". The input signed data could be wrapped\r | |
433 | in a ContentInfo structure.\r | |
434 | \r | |
435 | If P7Data, TrustedCert or InData is NULL, then return FALSE.\r | |
436 | If P7Length, CertLength or DataLength overflow, then return FAlSE.\r | |
437 | \r | |
438 | Caution: This function may receive untrusted input.\r | |
439 | UEFI Authenticated Variable is external input, so this function will do basic\r | |
440 | check for PKCS#7 data structure.\r | |
441 | \r | |
442 | @param[in] P7Data Pointer to the PKCS#7 message to verify.\r | |
443 | @param[in] P7Length Length of the PKCS#7 message in bytes.\r | |
444 | @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r | |
445 | is used for certificate chain verification.\r | |
446 | @param[in] CertLength Length of the trusted certificate in bytes.\r | |
447 | @param[in] InData Pointer to the content to be verified.\r | |
448 | @param[in] DataLength Length of InData in bytes.\r | |
449 | \r | |
450 | @retval TRUE The specified PKCS#7 signed data is valid.\r | |
451 | @retval FALSE Invalid PKCS#7 signed data.\r | |
452 | \r | |
453 | **/\r | |
454 | BOOLEAN\r | |
455 | EFIAPI\r | |
456 | Pkcs7Verify (\r | |
457 | IN CONST UINT8 *P7Data,\r | |
458 | IN UINTN P7Length,\r | |
459 | IN CONST UINT8 *TrustedCert,\r | |
460 | IN UINTN CertLength,\r | |
461 | IN CONST UINT8 *InData,\r | |
462 | IN UINTN DataLength\r | |
463 | )\r | |
464 | {\r | |
465 | PKCS7 *Pkcs7;\r | |
532616bb | 466 | BIO *DataBio;\r |
467 | BOOLEAN Status;\r | |
468 | X509 *Cert;\r | |
469 | X509_STORE *CertStore;\r | |
470 | UINT8 *SignedData;\r | |
1463ce18 | 471 | CONST UINT8 *Temp;\r |
532616bb | 472 | UINTN SignedDataSize;\r |
473 | BOOLEAN Wrapped;\r | |
474 | \r | |
475 | //\r | |
476 | // Check input parameters.\r | |
477 | //\r | |
2ac68e8b | 478 | if (P7Data == NULL || TrustedCert == NULL || InData == NULL ||\r |
532616bb | 479 | P7Length > INT_MAX || CertLength > INT_MAX || DataLength > INT_MAX) {\r |
480 | return FALSE;\r | |
481 | }\r | |
2ac68e8b | 482 | \r |
532616bb | 483 | Pkcs7 = NULL;\r |
532616bb | 484 | DataBio = NULL;\r |
485 | Cert = NULL;\r | |
486 | CertStore = NULL;\r | |
487 | \r | |
488 | //\r | |
489 | // Register & Initialize necessary digest algorithms for PKCS#7 Handling\r | |
490 | //\r | |
dda39f3a | 491 | if (EVP_add_digest (EVP_md5 ()) == 0) {\r |
492 | return FALSE;\r | |
493 | }\r | |
494 | if (EVP_add_digest (EVP_sha1 ()) == 0) {\r | |
495 | return FALSE;\r | |
496 | }\r | |
497 | if (EVP_add_digest (EVP_sha256 ()) == 0) {\r | |
498 | return FALSE;\r | |
499 | }\r | |
2ac68e8b QL |
500 | if (EVP_add_digest (EVP_sha384 ()) == 0) {\r |
501 | return FALSE;\r | |
502 | }\r | |
503 | if (EVP_add_digest (EVP_sha512 ()) == 0) {\r | |
504 | return FALSE;\r | |
505 | }\r | |
dda39f3a | 506 | if (EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA) == 0) {\r |
507 | return FALSE;\r | |
508 | }\r | |
509 | \r | |
532616bb | 510 | Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);\r |
511 | if (!Status) {\r | |
512 | return Status;\r | |
513 | }\r | |
514 | \r | |
515 | Status = FALSE;\r | |
2ac68e8b | 516 | \r |
532616bb | 517 | //\r |
518 | // Retrieve PKCS#7 Data (DER encoding)\r | |
519 | //\r | |
520 | if (SignedDataSize > INT_MAX) {\r | |
521 | goto _Exit;\r | |
522 | }\r | |
523 | \r | |
524 | Temp = SignedData;\r | |
525 | Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);\r | |
526 | if (Pkcs7 == NULL) {\r | |
527 | goto _Exit;\r | |
528 | }\r | |
529 | \r | |
530 | //\r | |
531 | // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r | |
532 | //\r | |
533 | if (!PKCS7_type_is_signed (Pkcs7)) {\r | |
534 | goto _Exit;\r | |
535 | }\r | |
536 | \r | |
537 | //\r | |
538 | // Read DER-encoded root certificate and Construct X509 Certificate\r | |
539 | //\r | |
1463ce18 QL |
540 | Temp = TrustedCert;\r |
541 | Cert = d2i_X509 (NULL, &Temp, (long) CertLength);\r | |
532616bb | 542 | if (Cert == NULL) {\r |
543 | goto _Exit;\r | |
544 | }\r | |
545 | \r | |
546 | //\r | |
547 | // Setup X509 Store for trusted certificate\r | |
548 | //\r | |
549 | CertStore = X509_STORE_new ();\r | |
550 | if (CertStore == NULL) {\r | |
551 | goto _Exit;\r | |
552 | }\r | |
553 | if (!(X509_STORE_add_cert (CertStore, Cert))) {\r | |
554 | goto _Exit;\r | |
555 | }\r | |
556 | \r | |
532616bb | 557 | //\r |
558 | // For generic PKCS#7 handling, InData may be NULL if the content is present\r | |
559 | // in PKCS#7 structure. So ignore NULL checking here.\r | |
560 | //\r | |
561 | DataBio = BIO_new (BIO_s_mem ());\r | |
5b2956ea YT |
562 | if (DataBio == NULL) {\r |
563 | goto _Exit;\r | |
564 | }\r | |
565 | \r | |
566 | if (BIO_write (DataBio, InData, (int) DataLength) <= 0) {\r | |
567 | goto _Exit;\r | |
568 | }\r | |
532616bb | 569 | \r |
68547181 DW |
570 | //\r |
571 | // Allow partial certificate chains, terminated by a non-self-signed but\r | |
de0408be | 572 | // still trusted intermediate certificate. Also disable time checks.\r |
68547181 | 573 | //\r |
de0408be DW |
574 | X509_STORE_set_flags (CertStore,\r |
575 | X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);\r | |
68547181 | 576 | \r |
02ee8d3b | 577 | //\r |
578 | // OpenSSL PKCS7 Verification by default checks for SMIME (email signing) and\r | |
579 | // doesn't support the extended key usage for Authenticode Code Signing.\r | |
580 | // Bypass the certificate purpose checking by enabling any purposes setting.\r | |
581 | //\r | |
582 | X509_STORE_set_purpose (CertStore, X509_PURPOSE_ANY);\r | |
583 | \r | |
532616bb | 584 | //\r |
585 | // Verifies the PKCS#7 signedData structure\r | |
586 | //\r | |
587 | Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);\r | |
588 | \r | |
589 | _Exit:\r | |
590 | //\r | |
591 | // Release Resources\r | |
592 | //\r | |
593 | BIO_free (DataBio);\r | |
532616bb | 594 | X509_free (Cert);\r |
595 | X509_STORE_free (CertStore);\r | |
596 | PKCS7_free (Pkcs7);\r | |
597 | \r | |
598 | if (!Wrapped) {\r | |
599 | OPENSSL_free (SignedData);\r | |
600 | }\r | |
601 | \r | |
602 | return Status;\r | |
afeb55e4 QL |
603 | }\r |
604 | \r | |
605 | /**\r | |
606 | Extracts the attached content from a PKCS#7 signed data if existed. The input signed\r | |
607 | data could be wrapped in a ContentInfo structure.\r | |
608 | \r | |
609 | If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow,\r | |
610 | then return FAlSE. If the P7Data is not correctly formatted, then return FALSE.\r | |
611 | \r | |
612 | Caution: This function may receive untrusted input. So this function will do\r | |
613 | basic check for PKCS#7 data structure.\r | |
614 | \r | |
615 | @param[in] P7Data Pointer to the PKCS#7 signed data to process.\r | |
616 | @param[in] P7Length Length of the PKCS#7 signed data in bytes.\r | |
617 | @param[out] Content Pointer to the extracted content from the PKCS#7 signedData.\r | |
618 | It's caller's responsiblity to free the buffer.\r | |
619 | @param[out] ContentSize The size of the extracted content in bytes.\r | |
620 | \r | |
621 | @retval TRUE The P7Data was correctly formatted for processing.\r | |
622 | @retval FALSE The P7Data was not correctly formatted for processing.\r | |
623 | \r | |
624 | */\r | |
625 | BOOLEAN\r | |
626 | EFIAPI\r | |
627 | Pkcs7GetAttachedContent (\r | |
628 | IN CONST UINT8 *P7Data,\r | |
629 | IN UINTN P7Length,\r | |
630 | OUT VOID **Content,\r | |
631 | OUT UINTN *ContentSize\r | |
632 | )\r | |
633 | {\r | |
634 | BOOLEAN Status;\r | |
635 | PKCS7 *Pkcs7;\r | |
636 | UINT8 *SignedData;\r | |
637 | UINTN SignedDataSize;\r | |
638 | BOOLEAN Wrapped;\r | |
639 | CONST UINT8 *Temp;\r | |
640 | ASN1_OCTET_STRING *OctStr;\r | |
641 | \r | |
afeb55e4 QL |
642 | //\r |
643 | // Check input parameter.\r | |
644 | //\r | |
645 | if ((P7Data == NULL) || (P7Length > INT_MAX) || (Content == NULL) || (ContentSize == NULL)) {\r | |
646 | return FALSE;\r | |
647 | }\r | |
648 | \r | |
2aabd146 QL |
649 | *Content = NULL;\r |
650 | Pkcs7 = NULL;\r | |
651 | SignedData = NULL;\r | |
652 | OctStr = NULL;\r | |
653 | \r | |
afeb55e4 QL |
654 | Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize);\r |
655 | if (!Status || (SignedDataSize > INT_MAX)) {\r | |
656 | goto _Exit;\r | |
657 | }\r | |
658 | \r | |
659 | Status = FALSE;\r | |
660 | \r | |
661 | //\r | |
662 | // Decoding PKCS#7 SignedData\r | |
663 | //\r | |
664 | Temp = SignedData;\r | |
665 | Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (int)SignedDataSize);\r | |
666 | if (Pkcs7 == NULL) {\r | |
667 | goto _Exit;\r | |
668 | }\r | |
669 | \r | |
670 | //\r | |
671 | // The type of Pkcs7 must be signedData\r | |
672 | //\r | |
673 | if (!PKCS7_type_is_signed (Pkcs7)) {\r | |
674 | goto _Exit;\r | |
675 | }\r | |
676 | \r | |
677 | //\r | |
678 | // Check for detached or attached content\r | |
679 | //\r | |
680 | if (PKCS7_get_detached (Pkcs7)) {\r | |
681 | //\r | |
682 | // No Content supplied for PKCS7 detached signedData\r | |
683 | //\r | |
684 | *Content = NULL;\r | |
685 | *ContentSize = 0;\r | |
686 | } else {\r | |
687 | //\r | |
688 | // Retrieve the attached content in PKCS7 signedData\r | |
689 | //\r | |
690 | OctStr = Pkcs7->d.sign->contents->d.data;\r | |
691 | if ((OctStr->length > 0) && (OctStr->data != NULL)) {\r | |
692 | *ContentSize = OctStr->length;\r | |
693 | *Content = malloc (*ContentSize);\r | |
2aabd146 QL |
694 | if (*Content == NULL) {\r |
695 | *ContentSize = 0;\r | |
696 | goto _Exit;\r | |
697 | }\r | |
afeb55e4 QL |
698 | CopyMem (*Content, OctStr->data, *ContentSize);\r |
699 | }\r | |
700 | }\r | |
701 | Status = TRUE;\r | |
702 | \r | |
703 | _Exit:\r | |
704 | //\r | |
705 | // Release Resources\r | |
706 | //\r | |
707 | PKCS7_free (Pkcs7);\r | |
708 | \r | |
709 | if (!Wrapped) {\r | |
710 | OPENSSL_free (SignedData);\r | |
711 | }\r | |
712 | \r | |
713 | return Status;\r | |
714 | }\r |