]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7.c
Fix CrytoPkg issue in GCC X64 tip.
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7.c
... / ...
CommitLineData
1/** @file\r
2 PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.\r
3\r
4Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
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
15#include "InternalCryptLib.h"\r
16\r
17#include <openssl/objects.h>\r
18#include <openssl/x509.h>\r
19#include <openssl/pkcs7.h>\r
20\r
21UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };\r
22\r
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
34int\r
35X509VerifyCb (\r
36 IN int Status,\r
37 IN X509_STORE_CTX *Context\r
38 )\r
39{\r
40 X509_OBJECT *Obj;\r
41 INTN Error;\r
42 INTN Index;\r
43 INTN Count;\r
44\r
45 Obj = NULL;\r
46 Error = (INTN) X509_STORE_CTX_get_error (Context);\r
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
78 Count = (INTN) sk_X509_num (Context->chain);\r
79 for (Index = 0; Index < Count; Index++) {\r
80 Obj->data.x509 = sk_X509_value (Context->chain, (int) Index);\r
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
147 UINTN P7DataSize;\r
148 UINT8 *Tmp;\r
149\r
150 //\r
151 // Check input parameters.\r
152 //\r
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
160\r
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 Status = FALSE;\r
194 goto _Exit;\r
195 }\r
196 Key->save_type = EVP_PKEY_RSA;\r
197 Key->type = EVP_PKEY_type (EVP_PKEY_RSA);\r
198 Key->pkey.rsa = (RSA *) RsaContext;\r
199\r
200 //\r
201 // Convert the data to be signed to BIO format. \r
202 //\r
203 DataBio = BIO_new (BIO_s_mem ());\r
204 BIO_write (DataBio, InData, (int) InDataSize);\r
205\r
206 //\r
207 // Create the PKCS#7 signedData structure.\r
208 //\r
209 Pkcs7 = PKCS7_sign (\r
210 (X509 *) SignCert,\r
211 Key,\r
212 (STACK_OF(X509) *) OtherCerts,\r
213 DataBio,\r
214 PKCS7_BINARY | PKCS7_NOATTR | PKCS7_DETACHED\r
215 );\r
216 if (Pkcs7 == NULL) {\r
217 Status = FALSE;\r
218 goto _Exit;\r
219 }\r
220\r
221 //\r
222 // Convert PKCS#7 signedData structure into DER-encoded buffer.\r
223 //\r
224 P7DataSize = i2d_PKCS7 (Pkcs7, NULL);\r
225 if (P7DataSize <= 19) {\r
226 Status = FALSE;\r
227 goto _Exit;\r
228 }\r
229\r
230 P7Data = OPENSSL_malloc (P7DataSize);\r
231 if (P7Data == NULL) {\r
232 Status = FALSE;\r
233 goto _Exit;\r
234 }\r
235\r
236 Tmp = P7Data;\r
237 P7DataSize = i2d_PKCS7 (Pkcs7, (unsigned char **) &Tmp);\r
238\r
239 //\r
240 // Strip ContentInfo to content only for signeddata. The data be trimmed off\r
241 // is totally 19 bytes.\r
242 //\r
243 *SignedDataSize = P7DataSize - 19;\r
244 *SignedData = OPENSSL_malloc (*SignedDataSize);\r
245 if (*SignedData == NULL) {\r
246 Status = FALSE;\r
247 OPENSSL_free (P7Data);\r
248 goto _Exit;\r
249 }\r
250\r
251 CopyMem (*SignedData, P7Data + 19, *SignedDataSize);\r
252 \r
253 OPENSSL_free (P7Data);\r
254\r
255 Status = TRUE;\r
256\r
257_Exit:\r
258 //\r
259 // Release Resources\r
260 //\r
261 if (RsaContext != NULL) {\r
262 RsaFree (RsaContext);\r
263 if (Key != NULL) {\r
264 Key->pkey.rsa = NULL;\r
265 }\r
266 }\r
267\r
268 if (Key != NULL) {\r
269 EVP_PKEY_free (Key);\r
270 }\r
271\r
272 if (DataBio != NULL) {\r
273 BIO_free (DataBio);\r
274 }\r
275\r
276 if (Pkcs7 != NULL) {\r
277 PKCS7_free (Pkcs7);\r
278 }\r
279\r
280 return Status;\r
281}\r
282\r
283/**\r
284 Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:\r
285 Cryptographic Message Syntax Standard". The input signed data could be wrapped\r
286 in a ContentInfo structure.\r
287\r
288 If P7Data is NULL, then ASSERT().\r
289\r
290 @param[in] P7Data Pointer to the PKCS#7 message to verify.\r
291 @param[in] P7Length Length of the PKCS#7 message in bytes.\r
292 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which\r
293 is used for certificate chain verification.\r
294 @param[in] CertLength Length of the trusted certificate in bytes.\r
295 @param[in] InData Pointer to the content to be verified.\r
296 @param[in] DataLength Length of InData in bytes.\r
297\r
298 @retval TRUE The specified PKCS#7 signed data is valid.\r
299 @retval FALSE Invalid PKCS#7 signed data.\r
300\r
301**/\r
302BOOLEAN\r
303EFIAPI\r
304Pkcs7Verify (\r
305 IN CONST UINT8 *P7Data,\r
306 IN UINTN P7Length,\r
307 IN CONST UINT8 *TrustedCert,\r
308 IN UINTN CertLength,\r
309 IN CONST UINT8 *InData,\r
310 IN UINTN DataLength\r
311 )\r
312{\r
313 PKCS7 *Pkcs7;\r
314 BIO *CertBio;\r
315 BIO *DataBio;\r
316 BOOLEAN Status;\r
317 X509 *Cert;\r
318 X509_STORE *CertStore;\r
319 UINT8 *SignedData;\r
320 UINT8 *Temp;\r
321 UINTN SignedDataSize;\r
322 BOOLEAN Wrapped;\r
323\r
324 //\r
325 // ASSERT if any input parameter is invalid.\r
326 //\r
327 ASSERT (P7Data != NULL);\r
328 ASSERT (TrustedCert != NULL);\r
329 ASSERT (InData != NULL);\r
330 ASSERT (P7Length <= INT_MAX);\r
331 ASSERT (CertLength <= INT_MAX);\r
332 ASSERT (DataLength <= INT_MAX);\r
333\r
334 Status = FALSE;\r
335 Pkcs7 = NULL;\r
336 CertBio = NULL;\r
337 DataBio = NULL;\r
338 Cert = NULL;\r
339 CertStore = NULL;\r
340\r
341 //\r
342 // Register & Initialize necessary digest algorithms for PKCS#7 Handling\r
343 //\r
344 EVP_add_digest (EVP_md5());\r
345 EVP_add_digest (EVP_sha1());\r
346 EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA);\r
347 EVP_add_digest (EVP_sha256());\r
348\r
349 //\r
350 // Check whether input P7Data is a wrapped ContentInfo structure or not.\r
351 //\r
352 Wrapped = FALSE;\r
353 if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {\r
354 if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {\r
355 if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {\r
356 Wrapped = TRUE;\r
357 }\r
358 }\r
359 }\r
360\r
361 if (Wrapped) {\r
362 SignedData = (UINT8 *) P7Data;\r
363 SignedDataSize = P7Length;\r
364 } else {\r
365 //\r
366 // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.\r
367 //\r
368 SignedDataSize = P7Length + 19;\r
369 SignedData = OPENSSL_malloc (SignedDataSize);\r
370 if (SignedData == NULL) {\r
371 return FALSE;\r
372 }\r
373\r
374 //\r
375 // Part1: 0x30, 0x82.\r
376 //\r
377 SignedData[0] = 0x30;\r
378 SignedData[1] = 0x82;\r
379\r
380 //\r
381 // Part2: Length1 = P7Length + 19 - 4, in big endian.\r
382 //\r
383 SignedData[2] = (UINT8) (((UINT16) (SignedDataSize - 4)) >> 8);\r
384 SignedData[3] = (UINT8) (((UINT16) (SignedDataSize - 4)) & 0xff);\r
385\r
386 //\r
387 // Part3: 0x06, 0x09.\r
388 //\r
389 SignedData[4] = 0x06;\r
390 SignedData[5] = 0x09;\r
391\r
392 //\r
393 // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.\r
394 //\r
395 CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));\r
396\r
397 //\r
398 // Part5: 0xA0, 0x82.\r
399 //\r
400 SignedData[15] = 0xA0;\r
401 SignedData[16] = 0x82;\r
402\r
403 //\r
404 // Part6: Length2 = P7Length, in big endian.\r
405 //\r
406 SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);\r
407 SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);\r
408\r
409 //\r
410 // Part7: P7Data.\r
411 //\r
412 CopyMem (SignedData + 19, P7Data, P7Length);\r
413 }\r
414 \r
415 //\r
416 // Retrieve PKCS#7 Data (DER encoding)\r
417 //\r
418 if (SignedDataSize > INT_MAX) {\r
419 goto _Exit;\r
420 }\r
421\r
422 Temp = SignedData;\r
423 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **) &Temp, (int) SignedDataSize);\r
424 if (Pkcs7 == NULL) {\r
425 goto _Exit;\r
426 }\r
427\r
428 //\r
429 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)\r
430 //\r
431 if (!PKCS7_type_is_signed (Pkcs7)) {\r
432 goto _Exit;\r
433 }\r
434\r
435 //\r
436 // Read DER-encoded root certificate and Construct X509 Certificate\r
437 //\r
438 CertBio = BIO_new (BIO_s_mem ());\r
439 BIO_write (CertBio, TrustedCert, (int)CertLength);\r
440 if (CertBio == NULL) {\r
441 goto _Exit;\r
442 }\r
443 Cert = d2i_X509_bio (CertBio, NULL);\r
444 if (Cert == NULL) {\r
445 goto _Exit;\r
446 }\r
447\r
448 //\r
449 // Setup X509 Store for trusted certificate\r
450 //\r
451 CertStore = X509_STORE_new ();\r
452 if (CertStore == NULL) {\r
453 goto _Exit;\r
454 }\r
455 if (!(X509_STORE_add_cert (CertStore, Cert))) {\r
456 goto _Exit;\r
457 }\r
458\r
459 //\r
460 // Register customized X509 verification callback function to support\r
461 // trusted intermediate certificate anchor.\r
462 //\r
463 CertStore->verify_cb = X509VerifyCb;\r
464\r
465 //\r
466 // For generic PKCS#7 handling, InData may be NULL if the content is present\r
467 // in PKCS#7 structure. So ignore NULL checking here.\r
468 //\r
469 DataBio = BIO_new (BIO_s_mem ());\r
470 BIO_write (DataBio, InData, (int)DataLength);\r
471\r
472 //\r
473 // Verifies the PKCS#7 signedData structure\r
474 //\r
475 Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);\r
476\r
477_Exit:\r
478 //\r
479 // Release Resources\r
480 //\r
481 BIO_free (DataBio);\r
482 BIO_free (CertBio);\r
483 X509_free (Cert);\r
484 X509_STORE_free (CertStore);\r
485 PKCS7_free (Pkcs7);\r
486\r
487 if (!Wrapped) {\r
488 OPENSSL_free (SignedData);\r
489 }\r
490\r
491 return Status;\r
492}\r