]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7.c
Update return FALSE to ASSERT() for code consistent.
[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
b7d320f8 4Copyright (c) 2009 - 2011, 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
d3945da6 153 ASSERT (PrivateKey != NULL);\r
154 ASSERT (KeyPassword != NULL);\r
155 ASSERT (InData != NULL);\r
156 ASSERT (SignCert != NULL);\r
157 ASSERT (SignedData != NULL);\r
158 ASSERT (SignedDataSize != NULL);\r
159 ASSERT (InDataSize <= INT_MAX);\r
da9e7418 160\r
b7d320f8 161 RsaContext = NULL;\r
162 Key = NULL;\r
163 Pkcs7 = NULL;\r
164 DataBio = NULL;\r
165 Status = FALSE;\r
166\r
167 //\r
168 // Retrieve RSA private key from PEM data.\r
169 //\r
170 Status = RsaGetPrivateKeyFromPem (\r
171 PrivateKey,\r
172 PrivateKeySize,\r
173 (CONST CHAR8 *) KeyPassword,\r
174 (VOID **) &RsaContext\r
175 );\r
176 if (!Status) {\r
177 return Status;\r
178 }\r
179\r
180 //\r
181 // Register & Initialize necessary digest algorithms and PRNG for PKCS#7 Handling\r
182 //\r
183 EVP_add_digest (EVP_md5());\r
184 EVP_add_digest (EVP_sha1());\r
185 EVP_add_digest (EVP_sha256());\r
186 RandomSeed (NULL, 0);\r
187\r
188 //\r
189 // Construct OpenSSL EVP_PKEY for private key.\r
190 //\r
191 Key = EVP_PKEY_new ();\r
192 if (Key == NULL) {\r
193 goto _Exit;\r
194 }\r
195 Key->save_type = EVP_PKEY_RSA;\r
196 Key->type = EVP_PKEY_type (EVP_PKEY_RSA);\r
197 Key->pkey.rsa = (RSA *) RsaContext;\r
198\r
199 //\r
200 // Convert the data to be signed to BIO format. \r
201 //\r
202 DataBio = BIO_new (BIO_s_mem ());\r
203 BIO_write (DataBio, InData, (int) InDataSize);\r
204\r
205 //\r
206 // Create the PKCS#7 signedData structure.\r
207 //\r
208 Pkcs7 = PKCS7_sign (\r
209 (X509 *) SignCert,\r
210 Key,\r
211 (STACK_OF(X509) *) OtherCerts,\r
212 DataBio,\r
55581f95 213 PKCS7_BINARY | PKCS7_NOATTR | PKCS7_DETACHED\r
b7d320f8 214 );\r
215 if (Pkcs7 == NULL) {\r
216 goto _Exit;\r
217 }\r
218\r
219 //\r
220 // Convert PKCS#7 signedData structure into DER-encoded buffer.\r
221 //\r
da9e7418 222 P7DataSize = i2d_PKCS7 (Pkcs7, NULL);\r
223 if (P7DataSize <= 19) {\r
b7d320f8 224 goto _Exit;\r
225 }\r
da9e7418 226 P7Data = OPENSSL_malloc (P7DataSize);\r
227 Tmp = P7Data;\r
228 P7DataSize = i2d_PKCS7 (Pkcs7, (unsigned char **) &Tmp);\r
229\r
230 //\r
231 // Strip ContentInfo to content only for signeddata. The data be trimmed off\r
232 // is totally 19 bytes.\r
233 //\r
234 *SignedDataSize = P7DataSize - 19;\r
b7d320f8 235 *SignedData = OPENSSL_malloc (*SignedDataSize);\r
da9e7418 236 CopyMem (*SignedData, P7Data + 19, *SignedDataSize);\r
237 \r
238 OPENSSL_free (P7Data);\r
b7d320f8 239\r
240 Status = TRUE;\r
241\r
242_Exit:\r
243 //\r
244 // Release Resources\r
245 //\r
246 if (RsaContext != NULL) {\r
247 RsaFree (RsaContext);\r
248 if (Key != NULL) {\r
249 Key->pkey.rsa = NULL;\r
250 }\r
251 }\r
252\r
253 if (Key != NULL) {\r
254 EVP_PKEY_free (Key);\r
255 }\r
256\r
257 if (DataBio != NULL) {\r
258 BIO_free (DataBio);\r
259 }\r
260\r
261 if (Pkcs7 != NULL) {\r
262 PKCS7_free (Pkcs7);\r
263 }\r
264\r
265 return Status;\r
266}\r
267\r
97f98500 268/**\r
da9e7418 269 Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:\r
270 Cryptographic Message Syntax Standard". The input signed data could be wrapped\r
271 in a ContentInfo structure.\r
97f98500
HT
272\r
273 If P7Data is NULL, then ASSERT().\r
274\r
275 @param[in] P7Data Pointer to the PKCS#7 message to verify.\r
276 @param[in] P7Length Length of the PKCS#7 message in bytes.\r
277 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r
278 is used for certificate chain verification.\r
279 @param[in] CertLength Length of the trusted certificate in bytes.\r
280 @param[in] InData Pointer to the content to be verified.\r
281 @param[in] DataLength Length of InData in bytes.\r
282\r
a8c44645 283 @retval TRUE The specified PKCS#7 signed data is valid.\r
284 @retval FALSE Invalid PKCS#7 signed data.\r
97f98500
HT
285\r
286**/\r
287BOOLEAN\r
288EFIAPI\r
289Pkcs7Verify (\r
290 IN CONST UINT8 *P7Data,\r
291 IN UINTN P7Length,\r
292 IN CONST UINT8 *TrustedCert,\r
293 IN UINTN CertLength,\r
294 IN CONST UINT8 *InData,\r
295 IN UINTN DataLength\r
296 )\r
297{\r
298 PKCS7 *Pkcs7;\r
97f98500
HT
299 BIO *CertBio;\r
300 BIO *DataBio;\r
301 BOOLEAN Status;\r
302 X509 *Cert;\r
303 X509_STORE *CertStore;\r
da9e7418 304 UINT8 *SignedData;\r
305 UINT8 *Temp;\r
306 UINTN SignedDataSize;\r
307 BOOLEAN Wrapped;\r
97f98500
HT
308\r
309 //\r
d3945da6 310 // ASSERT if any input parameter is invalid.\r
97f98500 311 //\r
d3945da6 312 ASSERT (P7Data != NULL);\r
313 ASSERT (TrustedCert != NULL);\r
314 ASSERT (InData != NULL);\r
315 ASSERT (P7Length <= INT_MAX);\r
316 ASSERT (CertLength <= INT_MAX);\r
317 ASSERT (DataLength <= INT_MAX);\r
97f98500
HT
318\r
319 Status = FALSE;\r
320 Pkcs7 = NULL;\r
321 CertBio = NULL;\r
322 DataBio = NULL;\r
323 Cert = NULL;\r
324 CertStore = NULL;\r
325\r
326 //\r
327 // Register & Initialize necessary digest algorithms for PKCS#7 Handling\r
328 //\r
329 EVP_add_digest (EVP_md5());\r
330 EVP_add_digest (EVP_sha1());\r
b7d320f8 331 EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA);\r
97f98500
HT
332 EVP_add_digest (EVP_sha256());\r
333\r
da9e7418 334 //\r
335 // Check whether input P7Data is a wrapped ContentInfo structure or not.\r
336 //\r
337 Wrapped = FALSE;\r
338 if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {\r
339 if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {\r
340 if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {\r
341 Wrapped = TRUE;\r
342 }\r
343 }\r
344 }\r
345\r
346 if (Wrapped) {\r
347 SignedData = (UINT8 *) P7Data;\r
348 SignedDataSize = P7Length;\r
349 } else {\r
350 //\r
351 // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.\r
352 //\r
353 SignedDataSize = P7Length + 19;\r
354 SignedData = OPENSSL_malloc (SignedDataSize);\r
355 if (SignedData == NULL) {\r
356 return FALSE;\r
357 }\r
358\r
359 //\r
360 // Part1: 0x30, 0x82.\r
361 //\r
362 SignedData[0] = 0x30;\r
363 SignedData[1] = 0x82;\r
364\r
365 //\r
366 // Part2: Length1 = P7Length + 19 - 4, in big endian.\r
367 //\r
368 SignedData[2] = (UINT8) (((UINT16) (SignedDataSize - 4)) >> 8);\r
369 SignedData[3] = (UINT8) (((UINT16) (SignedDataSize - 4)) & 0xff);\r
370\r
371 //\r
372 // Part3: 0x06, 0x09.\r
373 //\r
374 SignedData[4] = 0x06;\r
375 SignedData[5] = 0x09;\r
376\r
377 //\r
378 // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.\r
379 //\r
380 CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));\r
381\r
382 //\r
383 // Part5: 0xA0, 0x82.\r
384 //\r
385 SignedData[15] = 0xA0;\r
386 SignedData[16] = 0x82;\r
387\r
388 //\r
389 // Part6: Length2 = P7Length, in big endian.\r
390 //\r
391 SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);\r
392 SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);\r
393\r
394 //\r
395 // Part7: P7Data.\r
396 //\r
397 CopyMem (SignedData + 19, P7Data, P7Length);\r
398 }\r
399 \r
97f98500
HT
400 //\r
401 // Retrieve PKCS#7 Data (DER encoding)\r
402 //\r
da9e7418 403 if (SignedDataSize > INT_MAX) {\r
404 goto _Exit;\r
405 }\r
406\r
407 Temp = SignedData;\r
421fb3b5 408 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);\r
97f98500
HT
409 if (Pkcs7 == NULL) {\r
410 goto _Exit;\r
411 }\r
412\r
413 //\r
414 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r
415 //\r
416 if (!PKCS7_type_is_signed (Pkcs7)) {\r
417 goto _Exit;\r
418 }\r
419\r
97f98500
HT
420 //\r
421 // Read DER-encoded root certificate and Construct X509 Certificate\r
422 //\r
423 CertBio = BIO_new (BIO_s_mem ());\r
424 BIO_write (CertBio, TrustedCert, (int)CertLength);\r
425 if (CertBio == NULL) {\r
426 goto _Exit;\r
427 }\r
428 Cert = d2i_X509_bio (CertBio, NULL);\r
429 if (Cert == NULL) {\r
430 goto _Exit;\r
431 }\r
432\r
433 //\r
434 // Setup X509 Store for trusted certificate\r
435 //\r
436 CertStore = X509_STORE_new ();\r
437 if (CertStore == NULL) {\r
438 goto _Exit;\r
439 }\r
440 if (!(X509_STORE_add_cert (CertStore, Cert))) {\r
441 goto _Exit;\r
442 }\r
443\r
b7d320f8 444 //\r
445 // Register customized X509 verification callback function to support\r
446 // trusted intermediate certificate anchor.\r
447 //\r
448 CertStore->verify_cb = X509VerifyCb;\r
449\r
97f98500
HT
450 //\r
451 // For generic PKCS#7 handling, InData may be NULL if the content is present\r
452 // in PKCS#7 structure. So ignore NULL checking here.\r
453 //\r
454 DataBio = BIO_new (BIO_s_mem ());\r
455 BIO_write (DataBio, InData, (int)DataLength);\r
456\r
457 //\r
458 // Verifies the PKCS#7 signedData structure\r
459 //\r
b7d320f8 460 Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);\r
97f98500
HT
461\r
462_Exit:\r
463 //\r
464 // Release Resources\r
465 //\r
466 BIO_free (DataBio);\r
467 BIO_free (CertBio);\r
468 X509_free (Cert);\r
469 X509_STORE_free (CertStore);\r
470 PKCS7_free (Pkcs7);\r
471\r
da9e7418 472 if (!Wrapped) {\r
473 OPENSSL_free (SignedData);\r
474 }\r
475\r
97f98500
HT
476 return Status;\r
477}\r