]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7.c
1. Remove conducting ASSERT in BaseCryptLib.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7.c
CommitLineData
97f98500
HT
1/** @file\r
2 PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.\r
3\r
16d2c32c 4Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT 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 21UINT8 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 34int\r
35X509VerifyCb (\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
60 Obj = (X509_OBJECT *) OPENSSL_malloc (sizeof (X509_OBJECT));\r
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
127BOOLEAN\r
128EFIAPI\r
129Pkcs7Sign (\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
da9e7418 227 P7Data = OPENSSL_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
b7d320f8 241 *SignedData = OPENSSL_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
da9e7418 281 Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:\r
282 Cryptographic Message Syntax Standard". The input signed data could be wrapped\r
283 in a ContentInfo structure.\r
97f98500 284\r
16d2c32c 285 If P7Data, TrustedCert or InData is NULL, then return FALSE.\r
286 If P7Length, CertLength or DataLength overflow, then return FAlSE.\r
97f98500
HT
287\r
288 @param[in] P7Data Pointer to the PKCS#7 message to verify.\r
289 @param[in] P7Length Length of the PKCS#7 message in bytes.\r
290 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r
291 is used for certificate chain verification.\r
292 @param[in] CertLength Length of the trusted certificate in bytes.\r
293 @param[in] InData Pointer to the content to be verified.\r
294 @param[in] DataLength Length of InData in bytes.\r
295\r
a8c44645 296 @retval TRUE The specified PKCS#7 signed data is valid.\r
297 @retval FALSE Invalid PKCS#7 signed data.\r
97f98500
HT
298\r
299**/\r
300BOOLEAN\r
301EFIAPI\r
302Pkcs7Verify (\r
303 IN CONST UINT8 *P7Data,\r
304 IN UINTN P7Length,\r
305 IN CONST UINT8 *TrustedCert,\r
306 IN UINTN CertLength,\r
307 IN CONST UINT8 *InData,\r
308 IN UINTN DataLength\r
309 )\r
310{\r
311 PKCS7 *Pkcs7;\r
97f98500
HT
312 BIO *CertBio;\r
313 BIO *DataBio;\r
314 BOOLEAN Status;\r
315 X509 *Cert;\r
316 X509_STORE *CertStore;\r
da9e7418 317 UINT8 *SignedData;\r
318 UINT8 *Temp;\r
319 UINTN SignedDataSize;\r
320 BOOLEAN Wrapped;\r
97f98500
HT
321\r
322 //\r
16d2c32c 323 // Check input parameters.\r
97f98500 324 //\r
16d2c32c 325 if (P7Data == NULL || TrustedCert == NULL || InData == NULL || \r
326 P7Length > INT_MAX || CertLength > INT_MAX || DataLength > INT_MAX) {\r
327 return FALSE;\r
328 }\r
329 \r
97f98500
HT
330 Status = FALSE;\r
331 Pkcs7 = NULL;\r
332 CertBio = NULL;\r
333 DataBio = NULL;\r
334 Cert = NULL;\r
335 CertStore = NULL;\r
336\r
337 //\r
338 // Register & Initialize necessary digest algorithms for PKCS#7 Handling\r
339 //\r
340 EVP_add_digest (EVP_md5());\r
341 EVP_add_digest (EVP_sha1());\r
b7d320f8 342 EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA);\r
97f98500
HT
343 EVP_add_digest (EVP_sha256());\r
344\r
da9e7418 345 //\r
346 // Check whether input P7Data is a wrapped ContentInfo structure or not.\r
347 //\r
348 Wrapped = FALSE;\r
349 if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {\r
350 if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {\r
351 if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {\r
352 Wrapped = TRUE;\r
353 }\r
354 }\r
355 }\r
356\r
357 if (Wrapped) {\r
358 SignedData = (UINT8 *) P7Data;\r
359 SignedDataSize = P7Length;\r
360 } else {\r
361 //\r
362 // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.\r
363 //\r
364 SignedDataSize = P7Length + 19;\r
365 SignedData = OPENSSL_malloc (SignedDataSize);\r
366 if (SignedData == NULL) {\r
367 return FALSE;\r
368 }\r
369\r
370 //\r
371 // Part1: 0x30, 0x82.\r
372 //\r
373 SignedData[0] = 0x30;\r
374 SignedData[1] = 0x82;\r
375\r
376 //\r
377 // Part2: Length1 = P7Length + 19 - 4, in big endian.\r
378 //\r
379 SignedData[2] = (UINT8) (((UINT16) (SignedDataSize - 4)) >> 8);\r
380 SignedData[3] = (UINT8) (((UINT16) (SignedDataSize - 4)) & 0xff);\r
381\r
382 //\r
383 // Part3: 0x06, 0x09.\r
384 //\r
385 SignedData[4] = 0x06;\r
386 SignedData[5] = 0x09;\r
387\r
388 //\r
389 // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.\r
390 //\r
391 CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));\r
392\r
393 //\r
394 // Part5: 0xA0, 0x82.\r
395 //\r
396 SignedData[15] = 0xA0;\r
397 SignedData[16] = 0x82;\r
398\r
399 //\r
400 // Part6: Length2 = P7Length, in big endian.\r
401 //\r
402 SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);\r
403 SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);\r
404\r
405 //\r
406 // Part7: P7Data.\r
407 //\r
408 CopyMem (SignedData + 19, P7Data, P7Length);\r
409 }\r
410 \r
97f98500
HT
411 //\r
412 // Retrieve PKCS#7 Data (DER encoding)\r
413 //\r
da9e7418 414 if (SignedDataSize > INT_MAX) {\r
415 goto _Exit;\r
416 }\r
417\r
418 Temp = SignedData;\r
421fb3b5 419 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);\r
97f98500
HT
420 if (Pkcs7 == NULL) {\r
421 goto _Exit;\r
422 }\r
423\r
424 //\r
425 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r
426 //\r
427 if (!PKCS7_type_is_signed (Pkcs7)) {\r
428 goto _Exit;\r
429 }\r
430\r
97f98500
HT
431 //\r
432 // Read DER-encoded root certificate and Construct X509 Certificate\r
433 //\r
434 CertBio = BIO_new (BIO_s_mem ());\r
435 BIO_write (CertBio, TrustedCert, (int)CertLength);\r
436 if (CertBio == NULL) {\r
437 goto _Exit;\r
438 }\r
439 Cert = d2i_X509_bio (CertBio, NULL);\r
440 if (Cert == NULL) {\r
441 goto _Exit;\r
442 }\r
443\r
444 //\r
445 // Setup X509 Store for trusted certificate\r
446 //\r
447 CertStore = X509_STORE_new ();\r
448 if (CertStore == NULL) {\r
449 goto _Exit;\r
450 }\r
451 if (!(X509_STORE_add_cert (CertStore, Cert))) {\r
452 goto _Exit;\r
453 }\r
454\r
b7d320f8 455 //\r
456 // Register customized X509 verification callback function to support\r
457 // trusted intermediate certificate anchor.\r
458 //\r
459 CertStore->verify_cb = X509VerifyCb;\r
460\r
97f98500
HT
461 //\r
462 // For generic PKCS#7 handling, InData may be NULL if the content is present\r
463 // in PKCS#7 structure. So ignore NULL checking here.\r
464 //\r
465 DataBio = BIO_new (BIO_s_mem ());\r
466 BIO_write (DataBio, InData, (int)DataLength);\r
467\r
468 //\r
469 // Verifies the PKCS#7 signedData structure\r
470 //\r
b7d320f8 471 Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);\r
97f98500
HT
472\r
473_Exit:\r
474 //\r
475 // Release Resources\r
476 //\r
477 BIO_free (DataBio);\r
478 BIO_free (CertBio);\r
479 X509_free (Cert);\r
480 X509_STORE_free (CertStore);\r
481 PKCS7_free (Pkcs7);\r
482\r
da9e7418 483 if (!Wrapped) {\r
484 OPENSSL_free (SignedData);\r
485 }\r
486\r
97f98500
HT
487 return Status;\r
488}\r