]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7.c
433175bbf1e1490eab7ce5ced1203caf32066949
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7.c
1 /** @file
2 PKCS#7 SignedData Verification Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalCryptLib.h"
16
17 #include <openssl/objects.h>
18 #include <openssl/x509.h>
19 #include <openssl/pkcs7.h>
20
21 UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };
22
23 /**
24 Verification callback function to override any existing callbacks in OpenSSL
25 for intermediate certificate supports.
26
27 @param[in] Status Original status before calling this callback.
28 @param[in] Context X509 store context.
29
30 @retval 1 Current X509 certificate is verified successfully.
31 @retval 0 Verification failed.
32
33 **/
34 int
35 X509VerifyCb (
36 IN int Status,
37 IN X509_STORE_CTX *Context
38 )
39 {
40 X509_OBJECT *Obj;
41 INTN Error;
42 INTN Index;
43 INTN Count;
44
45 Obj = NULL;
46 Error = (INTN) X509_STORE_CTX_get_error (Context);
47
48 //
49 // X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT and X509_V_ERR_UNABLE_TO_GET_ISSUER_
50 // CERT_LOCALLY mean a X509 certificate is not self signed and its issuer
51 // can not be found in X509_verify_cert of X509_vfy.c.
52 // In order to support intermediate certificate node, we override the
53 // errors if the certification is obtained from X509 store, i.e. it is
54 // a trusted ceritifcate node that is enrolled by user.
55 // Besides,X509_V_ERR_CERT_UNTRUSTED and X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
56 // are also ignored to enable such feature.
57 //
58 if ((Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT) ||
59 (Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)) {
60 Obj = (X509_OBJECT *) OPENSSL_malloc (sizeof (X509_OBJECT));
61 if (Obj == NULL) {
62 return 0;
63 }
64
65 Obj->type = X509_LU_X509;
66 Obj->data.x509 = Context->current_cert;
67
68 CRYPTO_w_lock (CRYPTO_LOCK_X509_STORE);
69
70 if (X509_OBJECT_retrieve_match (Context->ctx->objs, Obj)) {
71 Status = 1;
72 } else {
73 //
74 // If any certificate in the chain is enrolled as trusted certificate,
75 // pass the certificate verification.
76 //
77 if (Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) {
78 Count = (INTN) sk_X509_num (Context->chain);
79 for (Index = 0; Index < Count; Index++) {
80 Obj->data.x509 = sk_X509_value (Context->chain, (int) Index);
81 if (X509_OBJECT_retrieve_match (Context->ctx->objs, Obj)) {
82 Status = 1;
83 break;
84 }
85 }
86 }
87 }
88
89 CRYPTO_w_unlock (CRYPTO_LOCK_X509_STORE);
90 }
91
92 if ((Error == X509_V_ERR_CERT_UNTRUSTED) ||
93 (Error == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE)) {
94 Status = 1;
95 }
96
97 if (Obj != NULL) {
98 OPENSSL_free (Obj);
99 }
100
101 return Status;
102 }
103
104 /**
105 Creates a PKCS#7 signedData as described in "PKCS #7: Cryptographic Message
106 Syntax Standard, version 1.5". This interface is only intended to be used for
107 application to perform PKCS#7 functionality validation.
108
109 @param[in] PrivateKey Pointer to the PEM-formatted private key data for
110 data signing.
111 @param[in] PrivateKeySize Size of the PEM private key data in bytes.
112 @param[in] KeyPassword NULL-terminated passphrase used for encrypted PEM
113 key data.
114 @param[in] InData Pointer to the content to be signed.
115 @param[in] InDataSize Size of InData in bytes.
116 @param[in] SignCert Pointer to signer's DER-encoded certificate to sign with.
117 @param[in] OtherCerts Pointer to an optional additional set of certificates to
118 include in the PKCS#7 signedData (e.g. any intermediate
119 CAs in the chain).
120 @param[out] SignedData Pointer to output PKCS#7 signedData.
121 @param[out] SignedDataSize Size of SignedData in bytes.
122
123 @retval TRUE PKCS#7 data signing succeeded.
124 @retval FALSE PKCS#7 data signing failed.
125
126 **/
127 BOOLEAN
128 EFIAPI
129 Pkcs7Sign (
130 IN CONST UINT8 *PrivateKey,
131 IN UINTN PrivateKeySize,
132 IN CONST UINT8 *KeyPassword,
133 IN UINT8 *InData,
134 IN UINTN InDataSize,
135 IN UINT8 *SignCert,
136 IN UINT8 *OtherCerts OPTIONAL,
137 OUT UINT8 **SignedData,
138 OUT UINTN *SignedDataSize
139 )
140 {
141 BOOLEAN Status;
142 EVP_PKEY *Key;
143 BIO *DataBio;
144 PKCS7 *Pkcs7;
145 UINT8 *RsaContext;
146 UINT8 *P7Data;
147 UINTN P7DataSize;
148 UINT8 *Tmp;
149
150 //
151 // Check input parameters.
152 //
153 if ((PrivateKey == NULL) || (KeyPassword == NULL) || (InData == NULL)) {
154 return FALSE;
155 }
156
157 if ((SignCert == NULL) || (SignedData == NULL) || (SignedDataSize == NULL)) {
158 return FALSE;
159 }
160
161 if (InDataSize > INT_MAX) {
162 return FALSE;
163 }
164
165 RsaContext = NULL;
166 Key = NULL;
167 Pkcs7 = NULL;
168 DataBio = NULL;
169 Status = FALSE;
170
171 //
172 // Retrieve RSA private key from PEM data.
173 //
174 Status = RsaGetPrivateKeyFromPem (
175 PrivateKey,
176 PrivateKeySize,
177 (CONST CHAR8 *) KeyPassword,
178 (VOID **) &RsaContext
179 );
180 if (!Status) {
181 return Status;
182 }
183
184 //
185 // Register & Initialize necessary digest algorithms and PRNG for PKCS#7 Handling
186 //
187 EVP_add_digest (EVP_md5());
188 EVP_add_digest (EVP_sha1());
189 EVP_add_digest (EVP_sha256());
190 RandomSeed (NULL, 0);
191
192 //
193 // Construct OpenSSL EVP_PKEY for private key.
194 //
195 Key = EVP_PKEY_new ();
196 if (Key == NULL) {
197 goto _Exit;
198 }
199 Key->save_type = EVP_PKEY_RSA;
200 Key->type = EVP_PKEY_type (EVP_PKEY_RSA);
201 Key->pkey.rsa = (RSA *) RsaContext;
202
203 //
204 // Convert the data to be signed to BIO format.
205 //
206 DataBio = BIO_new (BIO_s_mem ());
207 BIO_write (DataBio, InData, (int) InDataSize);
208
209 //
210 // Create the PKCS#7 signedData structure.
211 //
212 Pkcs7 = PKCS7_sign (
213 (X509 *) SignCert,
214 Key,
215 (STACK_OF(X509) *) OtherCerts,
216 DataBio,
217 PKCS7_BINARY | PKCS7_NOATTR | PKCS7_DETACHED
218 );
219 if (Pkcs7 == NULL) {
220 goto _Exit;
221 }
222
223 //
224 // Convert PKCS#7 signedData structure into DER-encoded buffer.
225 //
226 P7DataSize = i2d_PKCS7 (Pkcs7, NULL);
227 if (P7DataSize <= 19) {
228 goto _Exit;
229 }
230 P7Data = OPENSSL_malloc (P7DataSize);
231 Tmp = P7Data;
232 P7DataSize = i2d_PKCS7 (Pkcs7, (unsigned char **) &Tmp);
233
234 //
235 // Strip ContentInfo to content only for signeddata. The data be trimmed off
236 // is totally 19 bytes.
237 //
238 *SignedDataSize = P7DataSize - 19;
239 *SignedData = OPENSSL_malloc (*SignedDataSize);
240 CopyMem (*SignedData, P7Data + 19, *SignedDataSize);
241
242 OPENSSL_free (P7Data);
243
244 Status = TRUE;
245
246 _Exit:
247 //
248 // Release Resources
249 //
250 if (RsaContext != NULL) {
251 RsaFree (RsaContext);
252 if (Key != NULL) {
253 Key->pkey.rsa = NULL;
254 }
255 }
256
257 if (Key != NULL) {
258 EVP_PKEY_free (Key);
259 }
260
261 if (DataBio != NULL) {
262 BIO_free (DataBio);
263 }
264
265 if (Pkcs7 != NULL) {
266 PKCS7_free (Pkcs7);
267 }
268
269 return Status;
270 }
271
272 /**
273 Verifies the validility of a PKCS#7 signed data as described in "PKCS #7:
274 Cryptographic Message Syntax Standard". The input signed data could be wrapped
275 in a ContentInfo structure.
276
277 If P7Data is NULL, then ASSERT().
278
279 @param[in] P7Data Pointer to the PKCS#7 message to verify.
280 @param[in] P7Length Length of the PKCS#7 message in bytes.
281 @param[in] TrustedCert Pointer to a trusted/root certificate encoded in DER, which
282 is used for certificate chain verification.
283 @param[in] CertLength Length of the trusted certificate in bytes.
284 @param[in] InData Pointer to the content to be verified.
285 @param[in] DataLength Length of InData in bytes.
286
287 @retval TRUE The specified PKCS#7 signed data is valid.
288 @retval FALSE Invalid PKCS#7 signed data.
289
290 **/
291 BOOLEAN
292 EFIAPI
293 Pkcs7Verify (
294 IN CONST UINT8 *P7Data,
295 IN UINTN P7Length,
296 IN CONST UINT8 *TrustedCert,
297 IN UINTN CertLength,
298 IN CONST UINT8 *InData,
299 IN UINTN DataLength
300 )
301 {
302 PKCS7 *Pkcs7;
303 BIO *CertBio;
304 BIO *DataBio;
305 BOOLEAN Status;
306 X509 *Cert;
307 X509_STORE *CertStore;
308 UINT8 *SignedData;
309 UINT8 *Temp;
310 UINTN SignedDataSize;
311 BOOLEAN Wrapped;
312
313 //
314 // ASSERT if P7Data is NULL or P7Length is not larger than 19 bytes.
315 //
316 ASSERT ((P7Data != NULL) || (P7Length <= 19));
317
318 if ((CertLength > INT_MAX) || (DataLength > INT_MAX)) {
319 return FALSE;
320 }
321
322 Status = FALSE;
323 Pkcs7 = NULL;
324 CertBio = NULL;
325 DataBio = NULL;
326 Cert = NULL;
327 CertStore = NULL;
328
329 //
330 // Register & Initialize necessary digest algorithms for PKCS#7 Handling
331 //
332 EVP_add_digest (EVP_md5());
333 EVP_add_digest (EVP_sha1());
334 EVP_add_digest_alias (SN_sha1WithRSAEncryption, SN_sha1WithRSA);
335 EVP_add_digest (EVP_sha256());
336
337 //
338 // Check whether input P7Data is a wrapped ContentInfo structure or not.
339 //
340 Wrapped = FALSE;
341 if ((P7Data[4] == 0x06) && (P7Data[5] == 0x09)) {
342 if (CompareMem (P7Data + 6, mOidValue, sizeof (mOidValue)) == 0) {
343 if ((P7Data[15] == 0xA0) && (P7Data[16] == 0x82)) {
344 Wrapped = TRUE;
345 }
346 }
347 }
348
349 if (Wrapped) {
350 SignedData = (UINT8 *) P7Data;
351 SignedDataSize = P7Length;
352 } else {
353 //
354 // Wrap PKCS#7 signeddata to a ContentInfo structure - add a header in 19 bytes.
355 //
356 SignedDataSize = P7Length + 19;
357 SignedData = OPENSSL_malloc (SignedDataSize);
358 if (SignedData == NULL) {
359 return FALSE;
360 }
361
362 //
363 // Part1: 0x30, 0x82.
364 //
365 SignedData[0] = 0x30;
366 SignedData[1] = 0x82;
367
368 //
369 // Part2: Length1 = P7Length + 19 - 4, in big endian.
370 //
371 SignedData[2] = (UINT8) (((UINT16) (SignedDataSize - 4)) >> 8);
372 SignedData[3] = (UINT8) (((UINT16) (SignedDataSize - 4)) & 0xff);
373
374 //
375 // Part3: 0x06, 0x09.
376 //
377 SignedData[4] = 0x06;
378 SignedData[5] = 0x09;
379
380 //
381 // Part4: OID value -- 0x2A 0x86 0x48 0x86 0xF7 0x0D 0x01 0x07 0x02.
382 //
383 CopyMem (SignedData + 6, mOidValue, sizeof (mOidValue));
384
385 //
386 // Part5: 0xA0, 0x82.
387 //
388 SignedData[15] = 0xA0;
389 SignedData[16] = 0x82;
390
391 //
392 // Part6: Length2 = P7Length, in big endian.
393 //
394 SignedData[17] = (UINT8) (((UINT16) P7Length) >> 8);
395 SignedData[18] = (UINT8) (((UINT16) P7Length) & 0xff);
396
397 //
398 // Part7: P7Data.
399 //
400 CopyMem (SignedData + 19, P7Data, P7Length);
401 }
402
403 //
404 // Retrieve PKCS#7 Data (DER encoding)
405 //
406 if (SignedDataSize > INT_MAX) {
407 goto _Exit;
408 }
409
410 Temp = SignedData;
411 Pkcs7 = d2i_PKCS7 (NULL, &Temp, (int) SignedDataSize);
412 if (Pkcs7 == NULL) {
413 goto _Exit;
414 }
415
416 //
417 // Check if it's PKCS#7 Signed Data (for Authenticode Scenario)
418 //
419 if (!PKCS7_type_is_signed (Pkcs7)) {
420 goto _Exit;
421 }
422
423 //
424 // Read DER-encoded root certificate and Construct X509 Certificate
425 //
426 CertBio = BIO_new (BIO_s_mem ());
427 BIO_write (CertBio, TrustedCert, (int)CertLength);
428 if (CertBio == NULL) {
429 goto _Exit;
430 }
431 Cert = d2i_X509_bio (CertBio, NULL);
432 if (Cert == NULL) {
433 goto _Exit;
434 }
435
436 //
437 // Setup X509 Store for trusted certificate
438 //
439 CertStore = X509_STORE_new ();
440 if (CertStore == NULL) {
441 goto _Exit;
442 }
443 if (!(X509_STORE_add_cert (CertStore, Cert))) {
444 goto _Exit;
445 }
446
447 //
448 // Register customized X509 verification callback function to support
449 // trusted intermediate certificate anchor.
450 //
451 CertStore->verify_cb = X509VerifyCb;
452
453 //
454 // For generic PKCS#7 handling, InData may be NULL if the content is present
455 // in PKCS#7 structure. So ignore NULL checking here.
456 //
457 DataBio = BIO_new (BIO_s_mem ());
458 BIO_write (DataBio, InData, (int)DataLength);
459
460 //
461 // Verifies the PKCS#7 signedData structure
462 //
463 Status = (BOOLEAN) PKCS7_verify (Pkcs7, NULL, CertStore, DataBio, NULL, PKCS7_BINARY);
464
465 _Exit:
466 //
467 // Release Resources
468 //
469 BIO_free (DataBio);
470 BIO_free (CertBio);
471 X509_free (Cert);
472 X509_STORE_free (CertStore);
473 PKCS7_free (Pkcs7);
474
475 if (!Wrapped) {
476 OPENSSL_free (SignedData);
477 }
478
479 return Status;
480 }