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