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