2 This module verifies that Enhanced Key Usages (EKU's) are present within
3 a PKCS7 signature blob using OpenSSL.
5 Copyright (C) Microsoft Corporation. All Rights Reserved.
6 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
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>
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.
31 A typical certificate chain looks like this:
34 ----------------------------
36 ----------------------------
39 ----------------------------
40 | Policy CA | <-- Typical Trust Anchor.
41 ----------------------------
44 ----------------------------
46 ----------------------------
49 -----------------------------
50 / End-Entity (leaf) signer / <-- Bottom certificate.
51 ----------------------------- EKU: "1.3.6.1.4.1.311.76.9.21.1"
55 @param[in] CertChain Certificate chain.
57 @param[out] SignerCert Last certificate in the chain. For PKCS7 signatures,
58 this will be the end-entity (leaf) signer cert.
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.
67 GetSignerCertificate (
68 IN CONST PKCS7
*CertChain
,
74 STACK_OF (X509
) *Signers
;
81 if ((CertChain
== NULL
) || (SignerCert
== NULL
)) {
82 Status
= EFI_INVALID_PARAMETER
;
87 // Get the signers from the chain.
89 Signers
= PKCS7_get0_signers ((PKCS7
*)CertChain
, NULL
, PKCS7_BINARY
);
90 if (Signers
== NULL
) {
92 // Fail to get signers form PKCS7
94 Status
= EFI_INVALID_PARAMETER
;
99 // There should only be one signer in the PKCS7 stack.
101 NumberSigners
= sk_X509_num (Signers
);
102 if (NumberSigners
!= 1) {
104 // The number of singers should have been 1
106 Status
= EFI_NOT_FOUND
;
110 *SignerCert
= sk_X509_value (Signers
, 0);
116 if (Signers
!= NULL
) {
117 sk_X509_free (Signers
);
124 Determines if the specified EKU represented in ASN1 form is present
125 in a given certificate.
127 @param[in] Cert The certificate to check.
129 @param[in] Asn1ToFind The EKU to look for.
131 @retval EFI_SUCCESS We successfully identified the signing type.
132 @retval EFI_INVALID_PARAMETER A parameter was invalid.
133 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
140 IN ASN1_OBJECT
*Asn1ToFind
145 X509_EXTENSION
*Extension
;
146 EXTENDED_KEY_USAGE
*Eku
;
147 INT32 ExtensionIndex
;
149 ASN1_OBJECT
*Asn1InCert
;
152 Status
= EFI_NOT_FOUND
;
160 if ((Cert
== NULL
) || (Asn1ToFind
== NULL
)) {
161 Status
= EFI_INVALID_PARAMETER
;
166 // Clone the certificate. This is required because the Extension API's
167 // only work once per instance of an X509 object.
169 ClonedCert
= X509_dup ((X509
*)Cert
);
170 if (ClonedCert
== NULL
) {
172 // Fail to duplicate cert.
174 Status
= EFI_INVALID_PARAMETER
;
179 // Look for the extended key usage.
181 ExtensionIndex
= X509_get_ext_by_NID (ClonedCert
, NID_ext_key_usage
, -1);
183 if (ExtensionIndex
< 0) {
185 // Fail to find 'NID_ext_key_usage' in Cert.
190 Extension
= X509_get_ext (ClonedCert
, ExtensionIndex
);
191 if (Extension
== NULL
) {
193 // Fail to get Extension form cert.
198 Eku
= (EXTENDED_KEY_USAGE
*)X509V3_EXT_d2i (Extension
);
201 // Fail to get Eku from extension.
206 NumExtensions
= sk_ASN1_OBJECT_num (Eku
);
209 // Now loop through the extensions, looking for the specified Eku.
211 for (Index
= 0; Index
< NumExtensions
; Index
++) {
212 Asn1InCert
= sk_ASN1_OBJECT_value (Eku
, (INT32
)Index
);
213 if (Asn1InCert
== NULL
) {
215 // Fail to get ASN object from Eku.
220 if ((Asn1InCert
->length
== Asn1ToFind
->length
) &&
221 (CompareMem (Asn1InCert
->data
, Asn1ToFind
->data
, Asn1InCert
->length
) == 0))
224 // Found Eku in certificate.
226 Status
= EFI_SUCCESS
;
236 if (ClonedCert
!= NULL
) {
237 X509_free (ClonedCert
);
241 sk_ASN1_OBJECT_pop_free (Eku
, ASN1_OBJECT_free
);
248 Determines if the specified EKUs are present in a signing certificate.
250 @param[in] SignerCert The certificate to check.
251 @param[in] RequiredEKUs The EKUs to look for.
252 @param[in] RequiredEKUsSize The number of EKUs
253 @param[in] RequireAllPresent If TRUE, then all the specified EKUs
254 must be present in the certificate.
256 @retval EFI_SUCCESS We successfully identified the signing type.
257 @retval EFI_INVALID_PARAMETER A parameter was invalid.
258 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
263 IN CONST X509
*SignerCert
,
264 IN CONST CHAR8
*RequiredEKUs
[],
265 IN CONST UINT32 RequiredEKUsSize
,
266 IN BOOLEAN RequireAllPresent
270 ASN1_OBJECT
*Asn1ToFind
;
274 Status
= EFI_SUCCESS
;
278 if ((SignerCert
== NULL
) || (RequiredEKUs
== NULL
) || (RequiredEKUsSize
== 0)) {
279 Status
= EFI_INVALID_PARAMETER
;
283 for (Index
= 0; Index
< RequiredEKUsSize
; Index
++) {
285 // Finding required EKU in cert.
287 if (Asn1ToFind
!= NULL
) {
288 ASN1_OBJECT_free (Asn1ToFind
);
292 Asn1ToFind
= OBJ_txt2obj (RequiredEKUs
[Index
], 0);
293 if (Asn1ToFind
== NULL
) {
295 // Fail to convert required EKU to ASN1.
297 Status
= EFI_INVALID_PARAMETER
;
301 Status
= IsEkuInCertificate (SignerCert
, Asn1ToFind
);
302 if (Status
== EFI_SUCCESS
) {
304 if (!RequireAllPresent
) {
306 // Found at least one, so we are done.
312 // Fail to find Eku in cert
319 if (Asn1ToFind
!= NULL
) {
320 ASN1_OBJECT_free (Asn1ToFind
);
323 if (RequireAllPresent
&&
324 (NumEkusFound
== RequiredEKUsSize
))
327 // Found all required EKUs in certificate.
329 Status
= EFI_SUCCESS
;
336 This function receives a PKCS#7 formatted signature blob,
337 looks for the EKU SEQUENCE blob, and if found then looks
338 for all the required EKUs. This function was created so that
339 the Surface team can cut down on the number of Certificate
340 Authorities (CA's) by checking EKU's on leaf signers for
341 a specific product. This prevents one product's certificate
342 from signing another product's firmware or unlock blobs.
344 Note that this function does not validate the certificate chain.
345 That needs to be done before using this function.
347 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
348 containing the content block with both the signature,
349 the signer's certificate, and any necessary intermediate
351 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
352 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
353 required EKUs that must be present in the signature.
354 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
355 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
356 must be present in the leaf signer. If it is
357 FALSE, then we will succeed if we find any
358 of the specified EKU's.
360 @retval EFI_SUCCESS The required EKUs were found in the signature.
361 @retval EFI_INVALID_PARAMETER A parameter was invalid.
362 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
367 VerifyEKUsInPkcs7Signature (
368 IN CONST UINT8
*Pkcs7Signature
,
369 IN CONST UINT32 SignatureSize
,
370 IN CONST CHAR8
*RequiredEKUs
[],
371 IN CONST UINT32 RequiredEKUsSize
,
372 IN BOOLEAN RequireAllPresent
378 STACK_OF (X509
) *CertChain
;
380 INT32 NumberCertsInSignature
;
384 UINTN SignedDataSize
;
388 Status
= EFI_SUCCESS
;
392 NumberCertsInSignature
= 0;
400 // Validate the input parameters.
402 if ((Pkcs7Signature
== NULL
) ||
403 (SignatureSize
== 0) ||
404 (RequiredEKUs
== NULL
) ||
405 (RequiredEKUsSize
== 0))
407 Status
= EFI_INVALID_PARAMETER
;
411 if (RequiredEKUsSize
== 1) {
412 RequireAllPresent
= TRUE
;
416 // Wrap the PKCS7 data if needed.
427 // Fail to Wrap the PKCS7 data.
429 Status
= EFI_INVALID_PARAMETER
;
436 // Create the PKCS7 object.
438 Pkcs7
= d2i_PKCS7 (NULL
, (const unsigned char **)&Temp
, (INT32
)SignedDataSize
);
441 // Fail to read PKCS7 data.
443 Status
= EFI_INVALID_PARAMETER
;
448 // Get the certificate chain.
450 SignatureType
= OBJ_obj2nid (Pkcs7
->type
);
451 switch (SignatureType
) {
452 case NID_pkcs7_signed
:
453 if (Pkcs7
->d
.sign
!= NULL
) {
454 CertChain
= Pkcs7
->d
.sign
->cert
;
458 case NID_pkcs7_signedAndEnveloped
:
459 if (Pkcs7
->d
.signed_and_enveloped
!= NULL
) {
460 CertChain
= Pkcs7
->d
.signed_and_enveloped
->cert
;
469 // Ensure we have a certificate stack
471 if (CertChain
== NULL
) {
473 // Fail to get the certificate stack from signature.
475 Status
= EFI_INVALID_PARAMETER
;
480 // Find out how many certificates were in the PKCS7 signature.
482 NumberCertsInSignature
= sk_X509_num (CertChain
);
484 if (NumberCertsInSignature
== 0) {
486 // Fail to find any certificates in signature.
488 Status
= EFI_INVALID_PARAMETER
;
493 // Get the leaf signer.
495 Status
= GetSignerCertificate (Pkcs7
, &SignerCert
);
496 if ((Status
!= EFI_SUCCESS
) || (SignerCert
== NULL
)) {
498 // Fail to get the end-entity leaf signer certificate.
500 Status
= EFI_INVALID_PARAMETER
;
504 Status
= CheckEKUs (SignerCert
, RequiredEKUs
, RequiredEKUsSize
, RequireAllPresent
);
505 if (Status
!= EFI_SUCCESS
) {
514 // If the signature was not wrapped, then the call to WrapData() will allocate
515 // the data and add a header to it
517 if (!IsWrapped
&& SignedData
) {