]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyEku.c
40cc39afe7dd529d5bfd12855b053ba89905c7a4
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptPkcs7VerifyEku.c
1 /** @file
2 This module verifies that Enhanced Key Usages (EKU's) are present within
3 a PKCS7 signature blob using OpenSSL.
4
5 Copyright (C) Microsoft Corporation. All Rights Reserved.
6 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <Base.h>
13 #include "InternalCryptLib.h"
14 #include <openssl/x509v3.h>
15 #include <openssl/asn1.h>
16 #include <openssl/x509.h>
17 #include <openssl/bio.h>
18 #include <crypto/x509.h>
19 #include <openssl/pkcs7.h>
20 #include <openssl/bn.h>
21 #include <openssl/x509_vfy.h>
22 #include <openssl/pem.h>
23 #include <openssl/evp.h>
24 #include <crypto/asn1.h>
25
26 /**
27 This function will return the leaf signer certificate in a chain. This is
28 required because certificate chains are not guaranteed to have the
29 certificates in the order that they were issued.
30
31 A typical certificate chain looks like this:
32
33
34 ----------------------------
35 | Root |
36 ----------------------------
37 ^
38 |
39 ----------------------------
40 | Policy CA | <-- Typical Trust Anchor.
41 ----------------------------
42 ^
43 |
44 ----------------------------
45 | Issuing CA |
46 ----------------------------
47 ^
48 |
49 -----------------------------
50 / End-Entity (leaf) signer / <-- Bottom certificate.
51 ----------------------------- EKU: "1.3.6.1.4.1.311.76.9.21.1"
52 (Firmware Signing)
53
54
55 @param[in] CertChain Certificate chain.
56
57 @param[out] SignerCert Last certificate in the chain. For PKCS7 signatures,
58 this will be the end-entity (leaf) signer cert.
59
60 @retval EFI_SUCCESS The required EKUs were found in the signature.
61 @retval EFI_INVALID_PARAMETER A parameter was invalid.
62 @retval EFI_NOT_FOUND The number of signers found was not 1.
63
64 **/
65 EFI_STATUS
66 GetSignerCertificate (
67 IN CONST PKCS7 *CertChain,
68 OUT X509 **SignerCert
69 )
70 {
71 EFI_STATUS Status;
72 STACK_OF(X509) *Signers;
73 INT32 NumberSigners;
74
75 Status = EFI_SUCCESS;
76 Signers = NULL;
77 NumberSigners = 0;
78
79 if (CertChain == NULL || SignerCert == NULL) {
80 Status = EFI_INVALID_PARAMETER;
81 goto Exit;
82 }
83
84 //
85 // Get the signers from the chain.
86 //
87 Signers = PKCS7_get0_signers ((PKCS7*) CertChain, NULL, PKCS7_BINARY);
88 if (Signers == NULL) {
89 //
90 // Fail to get signers form PKCS7
91 //
92 Status = EFI_INVALID_PARAMETER;
93 goto Exit;
94 }
95
96 //
97 // There should only be one signer in the PKCS7 stack.
98 //
99 NumberSigners = sk_X509_num (Signers);
100 if (NumberSigners != 1) {
101 //
102 // The number of singers should have been 1
103 //
104 Status = EFI_NOT_FOUND;
105 goto Exit;
106 }
107
108 *SignerCert = sk_X509_value (Signers, 0);
109
110 Exit:
111 //
112 // Release Resources
113 //
114 if (Signers != NULL) {
115 sk_X509_free (Signers);
116 }
117
118 return Status;
119 }
120
121
122 /**
123 Determines if the specified EKU represented in ASN1 form is present
124 in a given certificate.
125
126 @param[in] Cert The certificate to check.
127
128 @param[in] Asn1ToFind The EKU to look for.
129
130 @retval EFI_SUCCESS We successfully identified the signing type.
131 @retval EFI_INVALID_PARAMETER A parameter was invalid.
132 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
133
134 **/
135 EFI_STATUS
136 IsEkuInCertificate (
137 IN CONST X509 *Cert,
138 IN ASN1_OBJECT *Asn1ToFind
139 )
140 {
141 EFI_STATUS Status;
142 X509 *ClonedCert;
143 X509_EXTENSION *Extension;
144 EXTENDED_KEY_USAGE *Eku;
145 INT32 ExtensionIndex;
146 INTN NumExtensions;
147 ASN1_OBJECT *Asn1InCert;
148 INTN Index;
149
150 Status = EFI_NOT_FOUND;
151 ClonedCert = NULL;
152 Extension = NULL;
153 Eku = NULL;
154 ExtensionIndex = -1;
155 NumExtensions = 0;
156 Asn1InCert = NULL;
157
158 if (Cert == NULL || Asn1ToFind == NULL) {
159 Status = EFI_INVALID_PARAMETER;
160 goto Exit;
161 }
162
163 //
164 // Clone the certificate. This is required because the Extension API's
165 // only work once per instance of an X509 object.
166 //
167 ClonedCert = X509_dup ((X509*)Cert);
168 if (ClonedCert == NULL) {
169 //
170 // Fail to duplicate cert.
171 //
172 Status = EFI_INVALID_PARAMETER;
173 goto Exit;
174 }
175
176 //
177 // Look for the extended key usage.
178 //
179 ExtensionIndex = X509_get_ext_by_NID (ClonedCert, NID_ext_key_usage, -1);
180
181 if (ExtensionIndex < 0) {
182 //
183 // Fail to find 'NID_ext_key_usage' in Cert.
184 //
185 goto Exit;
186 }
187
188 Extension = X509_get_ext (ClonedCert, ExtensionIndex);
189 if (Extension == NULL) {
190 //
191 // Fail to get Extension form cert.
192 //
193 goto Exit;
194 }
195
196 Eku = (EXTENDED_KEY_USAGE*)X509V3_EXT_d2i (Extension);
197 if (Eku == NULL) {
198 //
199 // Fail to get Eku from extension.
200 //
201 goto Exit;
202 }
203
204 NumExtensions = sk_ASN1_OBJECT_num (Eku);
205
206 //
207 // Now loop through the extensions, looking for the specified Eku.
208 //
209 for (Index = 0; Index < NumExtensions; Index++) {
210 Asn1InCert = sk_ASN1_OBJECT_value (Eku, (INT32)Index);
211 if (Asn1InCert == NULL) {
212 //
213 // Fail to get ASN object from Eku.
214 //
215 goto Exit;
216 }
217
218 if (Asn1InCert->length == Asn1ToFind->length &&
219 CompareMem (Asn1InCert->data, Asn1ToFind->data, Asn1InCert->length) == 0) {
220 //
221 // Found Eku in certificate.
222 //
223 Status = EFI_SUCCESS;
224 goto Exit;
225 }
226 }
227
228 Exit:
229
230 //
231 // Release Resources
232 //
233 if (ClonedCert != NULL) {
234 X509_free (ClonedCert);
235 }
236
237 if (Eku != NULL) {
238 sk_ASN1_OBJECT_pop_free (Eku, ASN1_OBJECT_free);
239 }
240
241 return Status;
242 }
243
244
245 /**
246 Determines if the specified EKUs are present in a signing certificate.
247
248 @param[in] SignerCert The certificate to check.
249 @param[in] RequiredEKUs The EKUs to look for.
250 @param[in] RequiredEKUsSize The number of EKUs
251 @param[in] RequireAllPresent If TRUE, then all the specified EKUs
252 must be present in the certificate.
253
254 @retval EFI_SUCCESS We successfully identified the signing type.
255 @retval EFI_INVALID_PARAMETER A parameter was invalid.
256 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
257 **/
258 EFI_STATUS
259 CheckEKUs(
260 IN CONST X509 *SignerCert,
261 IN CONST CHAR8 *RequiredEKUs[],
262 IN CONST UINT32 RequiredEKUsSize,
263 IN BOOLEAN RequireAllPresent
264 )
265 {
266 EFI_STATUS Status;
267 ASN1_OBJECT *Asn1ToFind;
268 UINT32 NumEkusFound;
269 UINT32 Index;
270
271 Status = EFI_SUCCESS;
272 Asn1ToFind = NULL;
273 NumEkusFound = 0;
274
275 if (SignerCert == NULL || RequiredEKUs == NULL || RequiredEKUsSize == 0) {
276 Status = EFI_INVALID_PARAMETER;
277 goto Exit;
278 }
279
280 for (Index = 0; Index < RequiredEKUsSize; Index++) {
281 //
282 // Finding required EKU in cert.
283 //
284 if (Asn1ToFind != NULL) {
285 ASN1_OBJECT_free(Asn1ToFind);
286 Asn1ToFind = NULL;
287 }
288
289 Asn1ToFind = OBJ_txt2obj (RequiredEKUs[Index], 0);
290 if (Asn1ToFind == NULL) {
291 //
292 // Fail to convert required EKU to ASN1.
293 //
294 Status = EFI_INVALID_PARAMETER;
295 goto Exit;
296 }
297
298 Status = IsEkuInCertificate (SignerCert, Asn1ToFind);
299 if (Status == EFI_SUCCESS) {
300 NumEkusFound++;
301 if (!RequireAllPresent) {
302 //
303 // Found at least one, so we are done.
304 //
305 goto Exit;
306 }
307 } else {
308 //
309 // Fail to find Eku in cert
310 break;
311 }
312 }
313
314 Exit:
315
316 if (Asn1ToFind != NULL) {
317 ASN1_OBJECT_free(Asn1ToFind);
318 }
319
320 if (RequireAllPresent &&
321 NumEkusFound == RequiredEKUsSize) {
322 //
323 // Found all required EKUs in certificate.
324 //
325 Status = EFI_SUCCESS;
326 }
327
328 return Status;
329 }
330
331 /**
332 This function receives a PKCS#7 formatted signature blob,
333 looks for the EKU SEQUENCE blob, and if found then looks
334 for all the required EKUs. This function was created so that
335 the Surface team can cut down on the number of Certificate
336 Authorities (CA's) by checking EKU's on leaf signers for
337 a specific product. This prevents one product's certificate
338 from signing another product's firmware or unlock blobs.
339
340 Note that this function does not validate the certificate chain.
341 That needs to be done before using this function.
342
343 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
344 containing the content block with both the signature,
345 the signer's certificate, and any necessary intermediate
346 certificates.
347 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
348 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
349 required EKUs that must be present in the signature.
350 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
351 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
352 must be present in the leaf signer. If it is
353 FALSE, then we will succeed if we find any
354 of the specified EKU's.
355
356 @retval EFI_SUCCESS The required EKUs were found in the signature.
357 @retval EFI_INVALID_PARAMETER A parameter was invalid.
358 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
359
360 **/
361 EFI_STATUS
362 EFIAPI
363 VerifyEKUsInPkcs7Signature (
364 IN CONST UINT8 *Pkcs7Signature,
365 IN CONST UINT32 SignatureSize,
366 IN CONST CHAR8 *RequiredEKUs[],
367 IN CONST UINT32 RequiredEKUsSize,
368 IN BOOLEAN RequireAllPresent
369 )
370 {
371 EFI_STATUS Status;
372 PKCS7 *Pkcs7;
373 STACK_OF(X509) *CertChain;
374 INT32 SignatureType;
375 INT32 NumberCertsInSignature;
376 X509 *SignerCert;
377 UINT8 *SignedData;
378 UINT8 *Temp;
379 UINTN SignedDataSize;
380 BOOLEAN IsWrapped;
381 BOOLEAN Ok;
382
383 Status = EFI_SUCCESS;
384 Pkcs7 = NULL;
385 CertChain = NULL;
386 SignatureType = 0;
387 NumberCertsInSignature = 0;
388 SignerCert = NULL;
389 SignedData = NULL;
390 SignedDataSize = 0;
391 IsWrapped = FALSE;
392 Ok = FALSE;
393
394 //
395 //Validate the input parameters.
396 //
397 if (Pkcs7Signature == NULL ||
398 SignatureSize == 0 ||
399 RequiredEKUs == NULL ||
400 RequiredEKUsSize == 0) {
401 Status = EFI_INVALID_PARAMETER;
402 goto Exit;
403 }
404
405 if (RequiredEKUsSize == 1) {
406 RequireAllPresent = TRUE;
407 }
408
409 //
410 // Wrap the PKCS7 data if needed.
411 //
412 Ok = WrapPkcs7Data (Pkcs7Signature,
413 SignatureSize,
414 &IsWrapped,
415 &SignedData,
416 &SignedDataSize);
417 if (!Ok) {
418 //
419 // Fail to Wrap the PKCS7 data.
420 //
421 Status = EFI_INVALID_PARAMETER;
422 goto Exit;
423 }
424
425 Temp = SignedData;
426
427 //
428 // Create the PKCS7 object.
429 //
430 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (INT32)SignedDataSize);
431 if (Pkcs7 == NULL) {
432 //
433 // Fail to read PKCS7 data.
434 //
435 Status = EFI_INVALID_PARAMETER;
436 goto Exit;
437 }
438
439 //
440 // Get the certificate chain.
441 //
442 SignatureType = OBJ_obj2nid (Pkcs7->type);
443 switch (SignatureType) {
444 case NID_pkcs7_signed:
445 if (Pkcs7->d.sign != NULL) {
446 CertChain = Pkcs7->d.sign->cert;
447 }
448 break;
449 case NID_pkcs7_signedAndEnveloped:
450 if (Pkcs7->d.signed_and_enveloped != NULL) {
451 CertChain = Pkcs7->d.signed_and_enveloped->cert;
452 }
453 break;
454 default:
455 break;
456 }
457
458 //
459 // Ensure we have a certificate stack
460 //
461 if (CertChain == NULL) {
462 //
463 // Fail to get the certificate stack from signature.
464 //
465 Status = EFI_INVALID_PARAMETER;
466 goto Exit;
467 }
468
469 //
470 // Find out how many certificates were in the PKCS7 signature.
471 //
472 NumberCertsInSignature = sk_X509_num (CertChain);
473
474 if (NumberCertsInSignature == 0) {
475 //
476 // Fail to find any certificates in signature.
477 //
478 Status = EFI_INVALID_PARAMETER;
479 goto Exit;
480 }
481
482 //
483 // Get the leaf signer.
484 //
485 Status = GetSignerCertificate (Pkcs7, &SignerCert);
486 if (Status != EFI_SUCCESS || SignerCert == NULL) {
487 //
488 // Fail to get the end-entity leaf signer certificate.
489 //
490 Status = EFI_INVALID_PARAMETER;
491 goto Exit;
492 }
493
494 Status = CheckEKUs (SignerCert, RequiredEKUs, RequiredEKUsSize, RequireAllPresent);
495 if (Status != EFI_SUCCESS) {
496 goto Exit;
497 }
498
499 Exit:
500
501 //
502 // Release Resources
503 //
504 // If the signature was not wrapped, then the call to WrapData() will allocate
505 // the data and add a header to it
506 //
507 if (!IsWrapped && SignedData) {
508 free (SignedData);
509 }
510
511 if (Pkcs7 != NULL) {
512 PKCS7_free (Pkcs7);
513 }
514
515 return Status;
516 }
517