| 1 |
/** @file |
| 2 |
PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL. |
| 3 |
|
| 4 |
Caution: This module requires additional review when modified. |
| 5 |
This library will have external input - signature (e.g. UEFI Authenticated |
| 6 |
Variable). It may by input in SMM mode. |
| 7 |
This external input must be validated carefully to avoid security issue like |
| 8 |
buffer overflow, integer overflow. |
| 9 |
|
| 10 |
WrapPkcs7Data(), Pkcs7GetSigners(), Pkcs7Verify() will get UEFI Authenticated |
| 11 |
Variable and will do basic check for data structure. |
| 12 |
|
| 13 |
Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR> |
| 14 |
This program and the accompanying materials |
| 15 |
are licensed and made available under the terms and conditions of the BSD License |
| 16 |
which accompanies this distribution. The full text of the license may be found at |
| 17 |
http://opensource.org/licenses/bsd-license.php |
| 18 |
|
| 19 |
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, |
| 20 |
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. |
| 21 |
|
| 22 |
**/ |
| 23 |
|
| 24 |
#include "InternalCryptLib.h" |
| 25 |
|
| 26 |
#include <openssl/objects.h> |
| 27 |
#include <openssl/x509.h> |
| 28 |
#include <openssl/x509v3.h> |
| 29 |
#include <openssl/pkcs7.h> |
| 30 |
|
| 31 |
UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 }; |
| 32 |
#if defined(ENABLE_CODESIGN_EKU) |
| 33 |
/* EKU CodeSign */ |
| 34 |
CHAR8 mOidCodeSign[] = "1.3.6.1.5.5.7.3.3"; |
| 35 |
#endif |
| 36 |
|
| 37 |
#if 1 |
| 38 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 39 |
#define X509_OBJECT_get0_X509(obj) ((obj)->data.x509) |
| 40 |
#define X509_OBJECT_get_type(obj) ((obj)->type) |
| 41 |
#define X509_STORE_CTX_get0_cert(ctx) ((ctx)->cert) |
| 42 |
#define X509_STORE_get0_objects(certs) ((certs)->objs) |
| 43 |
#define X509_get_extended_key_usage(cert) ((cert)->ex_xkusage) |
| 44 |
#if OPENSSL_VERSION_NUMBER < 0x10020000L |
| 45 |
#define X509_STORE_CTX_get0_store(ctx) ((ctx)->ctx) |
| 46 |
#endif |
| 47 |
#endif |
| 48 |
|
| 49 |
static int cert_in_store(X509 *cert, X509_STORE_CTX *ctx) |
| 50 |
{ |
| 51 |
X509_OBJECT obj; |
| 52 |
obj.type = X509_LU_X509; |
| 53 |
obj.data.x509 = cert; |
| 54 |
return X509_OBJECT_retrieve_match(ctx->ctx->objs, &obj) != NULL; |
| 55 |
} |
| 56 |
#else |
| 57 |
/* |
| 58 |
* Later versions of openssl will need this instead. |
| 59 |
*/ |
| 60 |
static int cert_in_store(X509 *cert, X509_STORE_CTX *ctx) |
| 61 |
{ |
| 62 |
STACK_OF(X509_OBJECT) *objs; |
| 63 |
X509_OBJECT *obj; |
| 64 |
int i; |
| 65 |
|
| 66 |
objs = X509_STORE_get0_objects(X509_STORE_CTX_get0_store(ctx)); |
| 67 |
|
| 68 |
for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 69 |
obj = sk_X509_OBJECT_value(objs, i); |
| 70 |
|
| 71 |
if (X509_OBJECT_get_type(obj) == X509_LU_X509 && |
| 72 |
!X509_cmp(X509_OBJECT_get0_X509(obj), cert)) |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
int |
| 81 |
X509VerifyCb ( |
| 82 |
IN int Status, |
| 83 |
IN X509_STORE_CTX *Context |
| 84 |
) |
| 85 |
{ |
| 86 |
INTN Error; |
| 87 |
|
| 88 |
Error = (INTN) X509_STORE_CTX_get_error (Context); |
| 89 |
|
| 90 |
/* Accept code-signing keys */ |
| 91 |
if (Error == X509_V_ERR_INVALID_PURPOSE && |
| 92 |
X509_get_extended_key_usage(X509_STORE_CTX_get0_cert(Context)) == XKU_CODE_SIGN) { |
| 93 |
Status = 1; |
| 94 |
} else if (Error == X509_V_ERR_CERT_UNTRUSTED || |
| 95 |
Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT || |
| 96 |
Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY || |
| 97 |
Error == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE) { |
| 98 |
/* all certs in our cert database are explicitly trusted */ |
| 99 |
|
| 100 |
if (cert_in_store(X509_STORE_CTX_get_current_cert(Context), Context)) |
| 101 |
Status = 1; |
| 102 |
} else if (Error == X509_V_ERR_CERT_HAS_EXPIRED || |
| 103 |
Error == X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD || |
| 104 |
Error == X509_V_ERR_CERT_NOT_YET_VALID || |
| 105 |
Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY || |
| 106 |
Error == X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD) { |
| 107 |
/* UEFI explicitly allows expired certificates */ |
| 108 |
Status = 1; |
| 109 |
#if 0 |
| 110 |
} else if (Error == X509_V_ERR_INVALID_CA) { |
| 111 |
/* Due to the historical reason, we have to relax the the x509 v3 extension |
| 112 |
* check to allow the CA certificates without the CA flag in the basic |
| 113 |
* constraints or KeyCertSign in the key usage to be loaded. In the future, |
| 114 |
* this callback should be removed to enforce the proper check. */ |
| 115 |
Status = 1; |
| 116 |
#endif |
| 117 |
} |
| 118 |
|
| 119 |
return Status; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
Check input P7Data is a wrapped ContentInfo structure or not. If not construct |
| 124 |
a new structure to wrap P7Data. |
| 125 |
|
| 126 |
Caution: This function may receive untrusted input. |
| 127 |
UEFI Authenticated Variable is external input, so this function will do basic |
| 128 |
check for PKCS#7 data structure. |
| 129 |
|
| 130 |
@param[in] P7Data Pointer to the PKCS#7 message to verify. |
| 131 |
@param[in] P7Length Length of the PKCS#7 message in bytes. |
| 132 |
@param[out] WrapFlag If TRUE P7Data is a ContentInfo structure, otherwise |
| 133 |
return FALSE. |
| 134 |
@param[out] WrapData If return status of this function is TRUE: |
| 135 |
1) when WrapFlag is TRUE, pointer to P7Data. |
| 136 |
2) when WrapFlag is FALSE, pointer to a new ContentInfo |
| 137 |
structure. It's caller's responsibility to free this |
| 138 |
buffer. |
| 139 |
@param[out] WrapDataSize Length of ContentInfo structure in bytes. |
| 140 |
|
| 141 |
@retval TRUE The operation is finished successfully. |
| 142 |
@retval FALSE The operation is failed due to lack of resources. |
| 143 |
|
| 144 |
**/ |
| 145 |
BOOLEAN |
| 146 |
WrapPkcs7Data ( |
| 147 |
IN CONST UINT8 *P7Data, |
| 148 |
IN UINTN P7Length, |
| 149 |
OUT BOOLEAN *WrapFlag, |
| 150 |
OUT UINT8 **WrapData, |
| 151 |
OUT UINTN *WrapDataSize |
| 152 |
) |
| 153 |
{ |
| 154 |
BOOLEAN Wrapped; |
| 155 |
UINT8 *SignedData; |
| 156 |
|
| 157 |
// |
| 158 |
// Check whether input P7Data is a wrapped ContentInfo structure or not. |
| 159 |
// |
| 160 |
Wrapped = FALSE; |
| 161 |
if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) { |
| 162 |
if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) { |
| 163 |
if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) { |
| 164 |
Wrapped = TRUE; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (Wrapped) { |
| 170 |
*WrapData = (UINT8 *) P7Data; |
| 171 |
*WrapDataSize = P7Length; |
| 172 |
} else { |
| 173 |
// |
| 174 |
// Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes. |
| 175 |
// |
| 176 |
*WrapDataSize = P7Length + 19; |
| 177 |
*WrapData = malloc (*WrapDataSize); |
| 178 |
if (*WrapData == NULL) { |
| 179 |
*WrapFlag = Wrapped; |
| 180 |
return FALSE; |
| 181 |
} |
| 182 |
|
| 183 |
SignedData = *WrapData; |
| 184 |
|
| 185 |
// |
| 186 |
// Part1: 0x30, 0x82. |
| 187 |
// |
| 188 |
SignedData[0] = 0x30; |
| 189 |
SignedData[1] = 0x82; |
| 190 |
|
| 191 |
// |
| 192 |
// Part2: Length1 = P7Length + 19 - 4, in big endian. |
| 193 |
// |
| 194 |
SignedData[2] = (UINT8) (((UINT16) (*WrapDataSize - 4)) >> 8); |
| 195 |
SignedData[3] = (UINT8) (((UINT16) (*WrapDataSize - 4)) & 0xff); |
| 196 |
|
| 197 |
// |
| 198 |
// Part3: 0x06, 0x09. |
| 199 |
// |
| 200 |
SignedData[4] = 0x06; |
| 201 |
SignedData[5] = 0x09; |
| 202 |
|
| 203 |
// |
| 204 |
// Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02. |
| 205 |
// |
| 206 |
CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue)); |
| 207 |
|
| 208 |
// |
| 209 |
// Part5: 0xA0, 0x82. |
| 210 |
// |
| 211 |
SignedData[15] = 0xA0; |
| 212 |
SignedData[16] = 0x82; |
| 213 |
|
| 214 |
// |
| 215 |
// Part6: Length2 = P7Length, in big endian. |
| 216 |
// |
| 217 |
SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8); |
| 218 |
SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff); |
| 219 |
|
| 220 |
// |
| 221 |
// Part7: P7Data. |
| 222 |
// |
| 223 |
CopyMem (SignedData + 19, (void *)P7Data, P7Length); |
| 224 |
} |
| 225 |
|
| 226 |
*WrapFlag = Wrapped; |
| 227 |
return TRUE; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
Pop single certificate from STACK_OF(X509). |
| 232 |
|
| 233 |
If X509Stack, Cert, or CertSize is NULL, then return FALSE. |
| 234 |
|
| 235 |
@param[in] X509Stack Pointer to a X509 stack object. |
| 236 |
@param[out] Cert Pointer to a X509 certificate. |
| 237 |
@param[out] CertSize Length of output X509 certificate in bytes. |
| 238 |
|
| 239 |
@retval TRUE The X509 stack pop succeeded. |
| 240 |
@retval FALSE The pop operation failed. |
| 241 |
|
| 242 |
**/ |
| 243 |
BOOLEAN |
| 244 |
X509PopCertificate ( |
| 245 |
IN VOID *X509Stack, |
| 246 |
OUT UINT8 **Cert, |
| 247 |
OUT UINTN *CertSize |
| 248 |
) |
| 249 |
{ |
| 250 |
BIO *CertBio; |
| 251 |
X509 *X509Cert; |
| 252 |
STACK_OF(X509) *CertStack; |
| 253 |
BOOLEAN Status; |
| 254 |
INT32 Result; |
| 255 |
INT32 Length; |
| 256 |
VOID *Buffer; |
| 257 |
|
| 258 |
Status = FALSE; |
| 259 |
|
| 260 |
if ((X509Stack == NULL) || (Cert == NULL) || (CertSize == NULL)) { |
| 261 |
return Status; |
| 262 |
} |
| 263 |
|
| 264 |
CertStack = (STACK_OF(X509) *) X509Stack; |
| 265 |
|
| 266 |
X509Cert = sk_X509_pop (CertStack); |
| 267 |
|
| 268 |
if (X509Cert == NULL) { |
| 269 |
return Status; |
| 270 |
} |
| 271 |
|
| 272 |
Buffer = NULL; |
| 273 |
|
| 274 |
CertBio = BIO_new (BIO_s_mem ()); |
| 275 |
if (CertBio == NULL) { |
| 276 |
return Status; |
| 277 |
} |
| 278 |
|
| 279 |
Result = i2d_X509_bio (CertBio, X509Cert); |
| 280 |
if (Result == 0) { |
| 281 |
goto _Exit; |
| 282 |
} |
| 283 |
|
| 284 |
Length = (INT32)(((BUF_MEM *) CertBio->ptr)->length); |
| 285 |
if (Length <= 0) { |
| 286 |
goto _Exit; |
| 287 |
} |
| 288 |
|
| 289 |
Buffer = malloc (Length); |
| 290 |
if (Buffer == NULL) { |
| 291 |
goto _Exit; |
| 292 |
} |
| 293 |
|
| 294 |
Result = BIO_read (CertBio, Buffer, Length); |
| 295 |
if (Result != Length) { |
| 296 |
goto _Exit; |
| 297 |
} |
| 298 |
|
| 299 |
*Cert = Buffer; |
| 300 |
*CertSize = Length; |
| 301 |
|
| 302 |
Status = TRUE; |
| 303 |
|
| 304 |
_Exit: |
| 305 |
|
| 306 |
BIO_free (CertBio); |
| 307 |
|
| 308 |
if (!Status && (Buffer != NULL)) { |
| 309 |
free (Buffer); |
| 310 |
} |
| 311 |
|
| 312 |
return Status; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
Get the signer's certificates from PKCS#7 signed data as described in "PKCS #7: |
| 317 |
Cryptographic Message Syntax Standard". The input signed data could be wrapped |
| 318 |
in a ContentInfo structure. |
| 319 |
|
| 320 |
If P7Data, CertStack, StackLength, TrustedCert or CertLength is NULL, then |
| 321 |
return FALSE. If P7Length overflow, then return FALSE. |
| 322 |
|
| 323 |
Caution: This function may receive untrusted input. |
| 324 |
UEFI Authenticated Variable is external input, so this function will do basic |
| 325 |
check for PKCS#7 data structure. |
| 326 |
|
| 327 |
@param[in] P7Data Pointer to the PKCS#7 message to verify. |
| 328 |
@param[in] P7Length Length of the PKCS#7 message in bytes. |
| 329 |
@param[out] CertStack Pointer to Signer's certificates retrieved from P7Data. |
| 330 |
It's caller's responsibility to free the buffer. |
| 331 |
@param[out] StackLength Length of signer's certificates in bytes. |
| 332 |
@param[out] TrustedCert Pointer to a trusted certificate from Signer's certificates. |
| 333 |
It's caller's responsibility to free the buffer. |
| 334 |
@param[out] CertLength Length of the trusted certificate in bytes. |
| 335 |
|
| 336 |
@retval TRUE The operation is finished successfully. |
| 337 |
@retval FALSE Error occurs during the operation. |
| 338 |
|
| 339 |
**/ |
| 340 |
BOOLEAN |
| 341 |
EFIAPI |
| 342 |
Pkcs7GetSigners ( |
| 343 |
IN CONST UINT8 *P7Data, |
| 344 |
IN UINTN P7Length, |
| 345 |
OUT UINT8 **CertStack, |
| 346 |
OUT UINTN *StackLength, |
| 347 |
OUT UINT8 **TrustedCert, |
| 348 |
OUT UINTN *CertLength |
| 349 |
) |
| 350 |
{ |
| 351 |
PKCS7 *Pkcs7; |
| 352 |
BOOLEAN Status; |
| 353 |
UINT8 *SignedData; |
| 354 |
CONST UINT8 *Temp; |
| 355 |
UINTN SignedDataSize; |
| 356 |
BOOLEAN Wrapped; |
| 357 |
STACK_OF(X509) *Stack; |
| 358 |
UINT8 Index; |
| 359 |
UINT8 *CertBuf; |
| 360 |
UINT8 *OldBuf; |
| 361 |
UINTN BufferSize; |
| 362 |
UINTN OldSize; |
| 363 |
UINT8 *SingleCert; |
| 364 |
UINTN SingleCertSize; |
| 365 |
|
| 366 |
if ((P7Data == NULL) || (CertStack == NULL) || (StackLength == NULL) || |
| 367 |
(TrustedCert == NULL) || (CertLength == NULL) || (P7Length > INT_MAX)) { |
| 368 |
return FALSE; |
| 369 |
} |
| 370 |
|
| 371 |
Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize); |
| 372 |
if (!Status) { |
| 373 |
return Status; |
| 374 |
} |
| 375 |
|
| 376 |
Status = FALSE; |
| 377 |
Pkcs7 = NULL; |
| 378 |
Stack = NULL; |
| 379 |
CertBuf = NULL; |
| 380 |
OldBuf = NULL; |
| 381 |
SingleCert = NULL; |
| 382 |
|
| 383 |
// |
| 384 |
// Retrieve PKCS#7 Data (DER encoding) |
| 385 |
// |
| 386 |
if (SignedDataSize > INT_MAX) { |
| 387 |
goto _Exit; |
| 388 |
} |
| 389 |
|
| 390 |
Temp = SignedData; |
| 391 |
Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize); |
| 392 |
if (Pkcs7 == NULL) { |
| 393 |
goto _Exit; |
| 394 |
} |
| 395 |
|
| 396 |
// |
| 397 |
// Check if it's PKCS#7 Signed Data (for Authenticode Scenario) |
| 398 |
// |
| 399 |
if (!PKCS7_type_is_signed (Pkcs7)) { |
| 400 |
goto _Exit; |
| 401 |
} |
| 402 |
|
| 403 |
Stack = PKCS7_get0_signers(Pkcs7, NULL, PKCS7_BINARY); |
| 404 |
if (Stack == NULL) { |
| 405 |
goto _Exit; |
| 406 |
} |
| 407 |
|
| 408 |
// |
| 409 |
// Convert CertStack to buffer in following format: |
| 410 |
// UINT8 CertNumber; |
| 411 |
// UINT32 Cert1Length; |
| 412 |
// UINT8 Cert1[]; |
| 413 |
// UINT32 Cert2Length; |
| 414 |
// UINT8 Cert2[]; |
| 415 |
// ... |
| 416 |
// UINT32 CertnLength; |
| 417 |
// UINT8 Certn[]; |
| 418 |
// |
| 419 |
BufferSize = sizeof (UINT8); |
| 420 |
OldSize = BufferSize; |
| 421 |
|
| 422 |
for (Index = 0; ; Index++) { |
| 423 |
Status = X509PopCertificate (Stack, &SingleCert, &SingleCertSize); |
| 424 |
if (!Status) { |
| 425 |
break; |
| 426 |
} |
| 427 |
|
| 428 |
OldSize = BufferSize; |
| 429 |
OldBuf = CertBuf; |
| 430 |
BufferSize = OldSize + SingleCertSize + sizeof (UINT32); |
| 431 |
CertBuf = malloc (BufferSize); |
| 432 |
|
| 433 |
if (CertBuf == NULL) { |
| 434 |
goto _Exit; |
| 435 |
} |
| 436 |
|
| 437 |
if (OldBuf != NULL) { |
| 438 |
CopyMem (CertBuf, OldBuf, OldSize); |
| 439 |
free (OldBuf); |
| 440 |
OldBuf = NULL; |
| 441 |
} |
| 442 |
|
| 443 |
WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) SingleCertSize); |
| 444 |
CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, SingleCertSize); |
| 445 |
|
| 446 |
free (SingleCert); |
| 447 |
SingleCert = NULL; |
| 448 |
} |
| 449 |
|
| 450 |
if (CertBuf != NULL) { |
| 451 |
// |
| 452 |
// Update CertNumber. |
| 453 |
// |
| 454 |
CertBuf[0] = Index; |
| 455 |
|
| 456 |
*CertLength = BufferSize - OldSize - sizeof (UINT32); |
| 457 |
*TrustedCert = malloc (*CertLength); |
| 458 |
if (*TrustedCert == NULL) { |
| 459 |
goto _Exit; |
| 460 |
} |
| 461 |
|
| 462 |
CopyMem (*TrustedCert, CertBuf + OldSize + sizeof (UINT32), *CertLength); |
| 463 |
*CertStack = CertBuf; |
| 464 |
*StackLength = BufferSize; |
| 465 |
Status = TRUE; |
| 466 |
} |
| 467 |
|
| 468 |
_Exit: |
| 469 |
// |
| 470 |
// Release Resources |
| 471 |
// |
| 472 |
if (!Wrapped) { |
| 473 |
free (SignedData); |
| 474 |
} |
| 475 |
|
| 476 |
if (Pkcs7 != NULL) { |
| 477 |
PKCS7_free (Pkcs7); |
| 478 |
} |
| 479 |
|
| 480 |
if (Stack != NULL) { |
| 481 |
sk_X509_pop_free(Stack, X509_free); |
| 482 |
} |
| 483 |
|
| 484 |
if (SingleCert != NULL) { |
| 485 |
free (SingleCert); |
| 486 |
} |
| 487 |
|
| 488 |
if (!Status && (CertBuf != NULL)) { |
| 489 |
free (CertBuf); |
| 490 |
*CertStack = NULL; |
| 491 |
} |
| 492 |
|
| 493 |
if (OldBuf != NULL) { |
| 494 |
free (OldBuf); |
| 495 |
} |
| 496 |
|
| 497 |
return Status; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
Wrap function to use free() to free allocated memory for certificates. |
| 502 |
|
| 503 |
@param[in] Certs Pointer to the certificates to be freed. |
| 504 |
|
| 505 |
**/ |
| 506 |
VOID |
| 507 |
EFIAPI |
| 508 |
Pkcs7FreeSigners ( |
| 509 |
IN UINT8 *Certs |
| 510 |
) |
| 511 |
{ |
| 512 |
if (Certs == NULL) { |
| 513 |
return; |
| 514 |
} |
| 515 |
|
| 516 |
free (Certs); |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
Retrieves all embedded certificates from PKCS#7 signed data as described in "PKCS #7: |
| 521 |
Cryptographic Message Syntax Standard", and outputs two certificate lists chained and |
| 522 |
unchained to the signer's certificates. |
| 523 |
The input signed data could be wrapped in a ContentInfo structure. |
| 524 |
|
| 525 |
@param[in] P7Data Pointer to the PKCS#7 message. |
| 526 |
@param[in] P7Length Length of the PKCS#7 message in bytes. |
| 527 |
@param[out] SignerChainCerts Pointer to the certificates list chained to signer's |
| 528 |
certificate. It's caller's responsibility to free the buffer. |
| 529 |
@param[out] ChainLength Length of the chained certificates list buffer in bytes. |
| 530 |
@param[out] UnchainCerts Pointer to the unchained certificates lists. It's caller's |
| 531 |
responsibility to free the buffer. |
| 532 |
@param[out] UnchainLength Length of the unchained certificates list buffer in bytes. |
| 533 |
|
| 534 |
@retval TRUE The operation is finished successfully. |
| 535 |
@retval FALSE Error occurs during the operation. |
| 536 |
|
| 537 |
**/ |
| 538 |
BOOLEAN |
| 539 |
EFIAPI |
| 540 |
Pkcs7GetCertificatesList ( |
| 541 |
IN CONST UINT8 *P7Data, |
| 542 |
IN UINTN P7Length, |
| 543 |
OUT UINT8 **SignerChainCerts, |
| 544 |
OUT UINTN *ChainLength, |
| 545 |
OUT UINT8 **UnchainCerts, |
| 546 |
OUT UINTN *UnchainLength |
| 547 |
) |
| 548 |
{ |
| 549 |
BOOLEAN Status; |
| 550 |
UINT8 *NewP7Data; |
| 551 |
UINTN NewP7Length; |
| 552 |
BOOLEAN Wrapped; |
| 553 |
UINT8 Index; |
| 554 |
PKCS7 *Pkcs7; |
| 555 |
X509_STORE_CTX CertCtx; |
| 556 |
STACK_OF(X509) *Signers; |
| 557 |
X509 *Signer; |
| 558 |
X509 *Cert; |
| 559 |
X509 *TempCert; |
| 560 |
X509 *Issuer; |
| 561 |
UINT8 *CertBuf; |
| 562 |
UINT8 *OldBuf; |
| 563 |
UINTN BufferSize; |
| 564 |
UINTN OldSize; |
| 565 |
UINT8 *SingleCert; |
| 566 |
UINTN CertSize; |
| 567 |
|
| 568 |
// |
| 569 |
// Initializations |
| 570 |
// |
| 571 |
Status = FALSE; |
| 572 |
NewP7Data = NULL; |
| 573 |
Pkcs7 = NULL; |
| 574 |
Cert = NULL; |
| 575 |
TempCert = NULL; |
| 576 |
SingleCert = NULL; |
| 577 |
CertBuf = NULL; |
| 578 |
OldBuf = NULL; |
| 579 |
Signers = NULL; |
| 580 |
|
| 581 |
ZeroMem (&CertCtx, sizeof (CertCtx)); |
| 582 |
|
| 583 |
// |
| 584 |
// Parameter Checking |
| 585 |
// |
| 586 |
if ((P7Data == NULL) || (SignerChainCerts == NULL) || (ChainLength == NULL) || |
| 587 |
(UnchainCerts == NULL) || (UnchainLength == NULL) || (P7Length > INT_MAX)) { |
| 588 |
return Status; |
| 589 |
} |
| 590 |
|
| 591 |
*SignerChainCerts = NULL; |
| 592 |
*ChainLength = 0; |
| 593 |
*UnchainCerts = NULL; |
| 594 |
*UnchainLength = 0; |
| 595 |
|
| 596 |
// |
| 597 |
// Construct a new PKCS#7 data wrapping with ContentInfo structure if needed. |
| 598 |
// |
| 599 |
Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &NewP7Data, &NewP7Length); |
| 600 |
if (!Status || (NewP7Length > INT_MAX)) { |
| 601 |
goto _Error; |
| 602 |
} |
| 603 |
|
| 604 |
// |
| 605 |
// Decodes PKCS#7 SignedData |
| 606 |
// |
| 607 |
Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &NewP7Data, (int) NewP7Length); |
| 608 |
if ((Pkcs7 == NULL) || (!PKCS7_type_is_signed (Pkcs7))) { |
| 609 |
goto _Error; |
| 610 |
} |
| 611 |
|
| 612 |
// |
| 613 |
// Obtains Signer's Certificate from PKCS#7 data |
| 614 |
// NOTE: Only one signer case will be handled in this function, which means SignerInfos |
| 615 |
// should include only one signer's certificate. |
| 616 |
// |
| 617 |
Signers = PKCS7_get0_signers (Pkcs7, NULL, PKCS7_BINARY); |
| 618 |
if ((Signers == NULL) || (sk_X509_num (Signers) != 1)) { |
| 619 |
goto _Error; |
| 620 |
} |
| 621 |
Signer = sk_X509_value (Signers, 0); |
| 622 |
|
| 623 |
if (!X509_STORE_CTX_init (&CertCtx, NULL, Signer, Pkcs7->d.sign->cert)) { |
| 624 |
goto _Error; |
| 625 |
} |
| 626 |
// |
| 627 |
// Initialize Chained & Untrusted stack |
| 628 |
// |
| 629 |
if (CertCtx.chain == NULL) { |
| 630 |
if (((CertCtx.chain = sk_X509_new_null ()) == NULL) || |
| 631 |
(!sk_X509_push (CertCtx.chain, CertCtx.cert))) { |
| 632 |
goto _Error; |
| 633 |
} |
| 634 |
} |
| 635 |
(VOID)sk_X509_delete_ptr (CertCtx.untrusted, Signer); |
| 636 |
|
| 637 |
// |
| 638 |
// Build certificates stack chained from Signer's certificate. |
| 639 |
// |
| 640 |
Cert = Signer; |
| 641 |
for (; ;) { |
| 642 |
// |
| 643 |
// Self-Issue checking |
| 644 |
// |
| 645 |
if (CertCtx.check_issued (&CertCtx, Cert, Cert)) { |
| 646 |
break; |
| 647 |
} |
| 648 |
|
| 649 |
// |
| 650 |
// Found the issuer of the current certificate |
| 651 |
// |
| 652 |
if (CertCtx.untrusted != NULL) { |
| 653 |
Issuer = NULL; |
| 654 |
for (Index = 0; Index < sk_X509_num (CertCtx.untrusted); Index++) { |
| 655 |
TempCert = sk_X509_value (CertCtx.untrusted, Index); |
| 656 |
if (CertCtx.check_issued (&CertCtx, Cert, TempCert)) { |
| 657 |
Issuer = TempCert; |
| 658 |
break; |
| 659 |
} |
| 660 |
} |
| 661 |
if (Issuer != NULL) { |
| 662 |
if (!sk_X509_push (CertCtx.chain, Issuer)) { |
| 663 |
goto _Error; |
| 664 |
} |
| 665 |
(VOID)sk_X509_delete_ptr (CertCtx.untrusted, Issuer); |
| 666 |
|
| 667 |
Cert = Issuer; |
| 668 |
continue; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
break; |
| 673 |
} |
| 674 |
|
| 675 |
// |
| 676 |
// Converts Chained and Untrusted Certificate to Certificate Buffer in following format: |
| 677 |
// UINT8 CertNumber; |
| 678 |
// UINT32 Cert1Length; |
| 679 |
// UINT8 Cert1[]; |
| 680 |
// UINT32 Cert2Length; |
| 681 |
// UINT8 Cert2[]; |
| 682 |
// ... |
| 683 |
// UINT32 CertnLength; |
| 684 |
// UINT8 Certn[]; |
| 685 |
// |
| 686 |
|
| 687 |
if (CertCtx.chain != NULL) { |
| 688 |
BufferSize = sizeof (UINT8); |
| 689 |
OldSize = BufferSize; |
| 690 |
CertBuf = NULL; |
| 691 |
|
| 692 |
for (Index = 0; ; Index++) { |
| 693 |
Status = X509PopCertificate (CertCtx.chain, &SingleCert, &CertSize); |
| 694 |
if (!Status) { |
| 695 |
break; |
| 696 |
} |
| 697 |
|
| 698 |
OldSize = BufferSize; |
| 699 |
OldBuf = CertBuf; |
| 700 |
BufferSize = OldSize + CertSize + sizeof (UINT32); |
| 701 |
CertBuf = malloc (BufferSize); |
| 702 |
|
| 703 |
if (CertBuf == NULL) { |
| 704 |
Status = FALSE; |
| 705 |
goto _Error; |
| 706 |
} |
| 707 |
if (OldBuf != NULL) { |
| 708 |
CopyMem (CertBuf, OldBuf, OldSize); |
| 709 |
free (OldBuf); |
| 710 |
OldBuf = NULL; |
| 711 |
} |
| 712 |
|
| 713 |
WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize); |
| 714 |
CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize); |
| 715 |
|
| 716 |
free (SingleCert); |
| 717 |
SingleCert = NULL; |
| 718 |
} |
| 719 |
|
| 720 |
if (CertBuf != NULL) { |
| 721 |
// |
| 722 |
// Update CertNumber. |
| 723 |
// |
| 724 |
CertBuf[0] = Index; |
| 725 |
|
| 726 |
*SignerChainCerts = CertBuf; |
| 727 |
*ChainLength = BufferSize; |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
if (CertCtx.untrusted != NULL) { |
| 732 |
BufferSize = sizeof (UINT8); |
| 733 |
OldSize = BufferSize; |
| 734 |
CertBuf = NULL; |
| 735 |
|
| 736 |
for (Index = 0; ; Index++) { |
| 737 |
Status = X509PopCertificate (CertCtx.untrusted, &SingleCert, &CertSize); |
| 738 |
if (!Status) { |
| 739 |
break; |
| 740 |
} |
| 741 |
|
| 742 |
OldSize = BufferSize; |
| 743 |
OldBuf = CertBuf; |
| 744 |
BufferSize = OldSize + CertSize + sizeof (UINT32); |
| 745 |
CertBuf = malloc (BufferSize); |
| 746 |
|
| 747 |
if (CertBuf == NULL) { |
| 748 |
Status = FALSE; |
| 749 |
goto _Error; |
| 750 |
} |
| 751 |
if (OldBuf != NULL) { |
| 752 |
CopyMem (CertBuf, OldBuf, OldSize); |
| 753 |
free (OldBuf); |
| 754 |
OldBuf = NULL; |
| 755 |
} |
| 756 |
|
| 757 |
WriteUnaligned32 ((UINT32 *) (CertBuf + OldSize), (UINT32) CertSize); |
| 758 |
CopyMem (CertBuf + OldSize + sizeof (UINT32), SingleCert, CertSize); |
| 759 |
|
| 760 |
free (SingleCert); |
| 761 |
SingleCert = NULL; |
| 762 |
} |
| 763 |
|
| 764 |
if (CertBuf != NULL) { |
| 765 |
// |
| 766 |
// Update CertNumber. |
| 767 |
// |
| 768 |
CertBuf[0] = Index; |
| 769 |
|
| 770 |
*UnchainCerts = CertBuf; |
| 771 |
*UnchainLength = BufferSize; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
Status = TRUE; |
| 776 |
|
| 777 |
_Error: |
| 778 |
// |
| 779 |
// Release Resources. |
| 780 |
// |
| 781 |
if (!Wrapped && (NewP7Data != NULL)) { |
| 782 |
free (NewP7Data); |
| 783 |
} |
| 784 |
|
| 785 |
if (Pkcs7 != NULL) { |
| 786 |
PKCS7_free (Pkcs7); |
| 787 |
} |
| 788 |
sk_X509_free (Signers); |
| 789 |
|
| 790 |
X509_STORE_CTX_cleanup (&CertCtx); |
| 791 |
|
| 792 |
if (SingleCert != NULL) { |
| 793 |
free (SingleCert); |
| 794 |
} |
| 795 |
|
| 796 |
if (OldBuf != NULL) { |
| 797 |
free (OldBuf); |
| 798 |
} |
| 799 |
|
| 800 |
if (!Status && (CertBuf != NULL)) { |
| 801 |
free (CertBuf); |
| 802 |
*SignerChainCerts = NULL; |
| 803 |
*UnchainCerts = NULL; |
| 804 |
} |
| 805 |
|
| 806 |
return Status; |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
Verifies the validity of a PKCS#7 signed data as described in "PKCS #7: |
| 811 |
Cryptographic Message Syntax Standard". The input signed data could be wrapped |
| 812 |
in a ContentInfo structure. |
| 813 |
|
| 814 |
If P7Data, TrustedCert or InData is NULL, then return FALSE. |
| 815 |
If P7Length, CertLength or DataLength overflow, then return FALSE. |
| 816 |
|
| 817 |
Caution: This function may receive untrusted input. |
| 818 |
UEFI Authenticated Variable is external input, so this function will do basic |
| 819 |
check for PKCS#7 data structure. |
| 820 |
|
| 821 |
@param[in] P7Data Pointer to the PKCS#7 message to verify. |
| 822 |
@param[in] P7Length Length of the PKCS#7 message in bytes. |
| 823 |
@param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which |
| 824 |
is used for certificate chain verification. |
| 825 |
@param[in] CertLength Length of the trusted certificate in bytes. |
| 826 |
@param[in] InData Pointer to the content to be verified. |
| 827 |
@param[in] DataLength Length of InData in bytes. |
| 828 |
|
| 829 |
@retval TRUE The specified PKCS#7 signed data is valid. |
| 830 |
@retval FALSE Invalid PKCS#7 signed data. |
| 831 |
|
| 832 |
**/ |
| 833 |
BOOLEAN |
| 834 |
EFIAPI |
| 835 |
Pkcs7Verify ( |
| 836 |
IN CONST UINT8 *P7Data, |
| 837 |
IN UINTN P7Length, |
| 838 |
IN CONST UINT8 *TrustedCert, |
| 839 |
IN UINTN CertLength, |
| 840 |
IN CONST UINT8 *InData, |
| 841 |
IN UINTN DataLength |
| 842 |
) |
| 843 |
{ |
| 844 |
PKCS7 *Pkcs7; |
| 845 |
BIO *DataBio; |
| 846 |
BOOLEAN Status; |
| 847 |
X509 *Cert; |
| 848 |
X509_STORE *CertStore; |
| 849 |
UINT8 *SignedData; |
| 850 |
CONST UINT8 *Temp; |
| 851 |
UINTN SignedDataSize; |
| 852 |
BOOLEAN Wrapped; |
| 853 |
#if defined(ENABLE_CODESIGN_EKU) |
| 854 |
CONST CHAR8 *Ekus[1]; |
| 855 |
EFI_STATUS EFI_Status; |
| 856 |
#endif |
| 857 |
|
| 858 |
// |
| 859 |
// Check input parameters. |
| 860 |
// |
| 861 |
if (P7Data == NULL || TrustedCert == NULL || InData == NULL || |
| 862 |
P7Length > INT_MAX || CertLength > INT_MAX || DataLength > INT_MAX) { |
| 863 |
return FALSE; |
| 864 |
} |
| 865 |
|
| 866 |
Pkcs7 = NULL; |
| 867 |
DataBio = NULL; |
| 868 |
Cert = NULL; |
| 869 |
CertStore = NULL; |
| 870 |
#if defined(ENABLE_CODESIGN_EKU) |
| 871 |
Ekus[0] = mOidCodeSign; |
| 872 |
#endif |
| 873 |
|
| 874 |
// |
| 875 |
// Register & Initialize necessary digest algorithms for PKCS#7 Handling |
| 876 |
// |
| 877 |
if (EVP_add_digest (EVP_md5 ()) == 0) { |
| 878 |
return FALSE; |
| 879 |
} |
| 880 |
if (EVP_add_digest (EVP_sha1 ()) == 0) { |
| 881 |
return FALSE; |
| 882 |
} |
| 883 |
if (EVP_add_digest (EVP_sha256 ()) == 0) { |
| 884 |
return FALSE; |
| 885 |
} |
| 886 |
if (EVP_add_digest (EVP_sha384 ()) == 0) { |
| 887 |
return FALSE; |
| 888 |
} |
| 889 |
if (EVP_add_digest (EVP_sha512 ()) == 0) { |
| 890 |
return FALSE; |
| 891 |
} |
| 892 |
if (EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA) == 0) { |
| 893 |
return FALSE; |
| 894 |
} |
| 895 |
|
| 896 |
Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize); |
| 897 |
if (!Status) { |
| 898 |
return Status; |
| 899 |
} |
| 900 |
|
| 901 |
Status = FALSE; |
| 902 |
|
| 903 |
// |
| 904 |
// Retrieve PKCS#7 Data (DER encoding) |
| 905 |
// |
| 906 |
if (SignedDataSize > INT_MAX) { |
| 907 |
goto _Exit; |
| 908 |
} |
| 909 |
|
| 910 |
Temp = SignedData; |
| 911 |
Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize); |
| 912 |
if (Pkcs7 == NULL) { |
| 913 |
goto _Exit; |
| 914 |
} |
| 915 |
|
| 916 |
// |
| 917 |
// Check if it's PKCS#7 Signed Data (for Authenticode Scenario) |
| 918 |
// |
| 919 |
if (!PKCS7_type_is_signed (Pkcs7)) { |
| 920 |
goto _Exit; |
| 921 |
} |
| 922 |
|
| 923 |
// |
| 924 |
// Read DER-encoded root certificate and Construct X509 Certificate |
| 925 |
// |
| 926 |
Temp = TrustedCert; |
| 927 |
Cert = d2i_X509 (NULL, &Temp, (long) CertLength); |
| 928 |
if (Cert == NULL) { |
| 929 |
goto _Exit; |
| 930 |
} |
| 931 |
|
| 932 |
// |
| 933 |
// Setup X509 Store for trusted certificate |
| 934 |
// |
| 935 |
CertStore = X509_STORE_new (); |
| 936 |
if (CertStore == NULL) { |
| 937 |
goto _Exit; |
| 938 |
} |
| 939 |
if (!(X509_STORE_add_cert (CertStore, Cert))) { |
| 940 |
goto _Exit; |
| 941 |
} |
| 942 |
|
| 943 |
X509_STORE_set_verify_cb (CertStore, X509VerifyCb); |
| 944 |
|
| 945 |
// |
| 946 |
// For generic PKCS#7 handling, InData may be NULL if the content is present |
| 947 |
// in PKCS#7 structure. So ignore NULL checking here. |
| 948 |
// |
| 949 |
DataBio = BIO_new (BIO_s_mem ()); |
| 950 |
if (DataBio == NULL) { |
| 951 |
goto _Exit; |
| 952 |
} |
| 953 |
|
| 954 |
if (BIO_write (DataBio, InData, (int) DataLength) <= 0) { |
| 955 |
goto _Exit; |
| 956 |
} |
| 957 |
|
| 958 |
// |
| 959 |
// Allow partial certificate chains, terminated by a non-self-signed but |
| 960 |
// still trusted intermediate certificate. Also disable time checks. |
| 961 |
// |
| 962 |
X509_STORE_set_flags (CertStore, |
| 963 |
X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME); |
| 964 |
|
| 965 |
// |
| 966 |
// OpenSSL PKCS7 Verification by default checks for SMIME (email signing) and |
| 967 |
// doesn't support the extended key usage for Authenticode Code Signing. |
| 968 |
// Bypass the certificate purpose checking by enabling any purposes setting. |
| 969 |
// |
| 970 |
X509_STORE_set_purpose (CertStore, X509_PURPOSE_ANY); |
| 971 |
|
| 972 |
#if defined(ENABLE_CODESIGN_EKU) |
| 973 |
EFI_Status = VerifyEKUsInPkcs7Signature(P7Data, P7Length, Ekus, 1, TRUE); |
| 974 |
if (EFI_Status != EFI_SUCCESS) { |
| 975 |
goto _Exit; |
| 976 |
} |
| 977 |
#endif |
| 978 |
|
| 979 |
// |
| 980 |
// Verifies the PKCS#7 signedData structure |
| 981 |
// |
| 982 |
Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY); |
| 983 |
|
| 984 |
_Exit: |
| 985 |
// |
| 986 |
// Release Resources |
| 987 |
// |
| 988 |
BIO_free (DataBio); |
| 989 |
X509_free (Cert); |
| 990 |
X509_STORE_free (CertStore); |
| 991 |
PKCS7_free (Pkcs7); |
| 992 |
|
| 993 |
if (!Wrapped) { |
| 994 |
OPENSSL_free (SignedData); |
| 995 |
} |
| 996 |
|
| 997 |
return Status; |
| 998 |
} |
| 999 |
|
| 1000 |
/** |
| 1001 |
Extracts the attached content from a PKCS#7 signed data if existed. The input signed |
| 1002 |
data could be wrapped in a ContentInfo structure. |
| 1003 |
|
| 1004 |
If P7Data, Content, or ContentSize is NULL, then return FALSE. If P7Length overflow, |
| 1005 |
then return FALSE. If the P7Data is not correctly formatted, then return FALSE. |
| 1006 |
|
| 1007 |
Caution: This function may receive untrusted input. So this function will do |
| 1008 |
basic check for PKCS#7 data structure. |
| 1009 |
|
| 1010 |
@param[in] P7Data Pointer to the PKCS#7 signed data to process. |
| 1011 |
@param[in] P7Length Length of the PKCS#7 signed data in bytes. |
| 1012 |
@param[out] Content Pointer to the extracted content from the PKCS#7 signedData. |
| 1013 |
It's caller's responsibility to free the buffer. |
| 1014 |
@param[out] ContentSize The size of the extracted content in bytes. |
| 1015 |
|
| 1016 |
@retval TRUE The P7Data was correctly formatted for processing. |
| 1017 |
@retval FALSE The P7Data was not correctly formatted for processing. |
| 1018 |
|
| 1019 |
*/ |
| 1020 |
BOOLEAN |
| 1021 |
EFIAPI |
| 1022 |
Pkcs7GetAttachedContent ( |
| 1023 |
IN CONST UINT8 *P7Data, |
| 1024 |
IN UINTN P7Length, |
| 1025 |
OUT VOID **Content, |
| 1026 |
OUT UINTN *ContentSize |
| 1027 |
) |
| 1028 |
{ |
| 1029 |
BOOLEAN Status; |
| 1030 |
PKCS7 *Pkcs7; |
| 1031 |
UINT8 *SignedData; |
| 1032 |
UINTN SignedDataSize; |
| 1033 |
BOOLEAN Wrapped; |
| 1034 |
CONST UINT8 *Temp; |
| 1035 |
ASN1_OCTET_STRING *OctStr; |
| 1036 |
|
| 1037 |
// |
| 1038 |
// Check input parameter. |
| 1039 |
// |
| 1040 |
if ((P7Data == NULL) || (P7Length > INT_MAX) || (Content == NULL) || (ContentSize == NULL)) { |
| 1041 |
return FALSE; |
| 1042 |
} |
| 1043 |
|
| 1044 |
*Content = NULL; |
| 1045 |
Pkcs7 = NULL; |
| 1046 |
SignedData = NULL; |
| 1047 |
OctStr = NULL; |
| 1048 |
|
| 1049 |
Status = WrapPkcs7Data (P7Data, P7Length, &Wrapped, &SignedData, &SignedDataSize); |
| 1050 |
if (!Status || (SignedDataSize > INT_MAX)) { |
| 1051 |
goto _Exit; |
| 1052 |
} |
| 1053 |
|
| 1054 |
Status = FALSE; |
| 1055 |
|
| 1056 |
// |
| 1057 |
// Decoding PKCS#7 SignedData |
| 1058 |
// |
| 1059 |
Temp = SignedData; |
| 1060 |
Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (int)SignedDataSize); |
| 1061 |
if (Pkcs7 == NULL) { |
| 1062 |
goto _Exit; |
| 1063 |
} |
| 1064 |
|
| 1065 |
// |
| 1066 |
// The type of Pkcs7 must be signedData |
| 1067 |
// |
| 1068 |
if (!PKCS7_type_is_signed (Pkcs7)) { |
| 1069 |
goto _Exit; |
| 1070 |
} |
| 1071 |
|
| 1072 |
// |
| 1073 |
// Check for detached or attached content |
| 1074 |
// |
| 1075 |
if (PKCS7_get_detached (Pkcs7)) { |
| 1076 |
// |
| 1077 |
// No Content supplied for PKCS7 detached signedData |
| 1078 |
// |
| 1079 |
*Content = NULL; |
| 1080 |
*ContentSize = 0; |
| 1081 |
} else { |
| 1082 |
// |
| 1083 |
// Retrieve the attached content in PKCS7 signedData |
| 1084 |
// |
| 1085 |
OctStr = Pkcs7->d.sign->contents->d.data; |
| 1086 |
if ((OctStr->length > 0) && (OctStr->data != NULL)) { |
| 1087 |
*ContentSize = OctStr->length; |
| 1088 |
*Content = malloc (*ContentSize); |
| 1089 |
if (*Content == NULL) { |
| 1090 |
*ContentSize = 0; |
| 1091 |
goto _Exit; |
| 1092 |
} |
| 1093 |
CopyMem (*Content, OctStr->data, *ContentSize); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
Status = TRUE; |
| 1097 |
|
| 1098 |
_Exit: |
| 1099 |
// |
| 1100 |
// Release Resources |
| 1101 |
// |
| 1102 |
PKCS7_free (Pkcs7); |
| 1103 |
|
| 1104 |
if (!Wrapped) { |
| 1105 |
OPENSSL_free (SignedData); |
| 1106 |
} |
| 1107 |
|
| 1108 |
return Status; |
| 1109 |
} |