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