Debian Codegraph @

Cryptlib/Pk/CryptPkcs7VerifyEku.c

raw
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 <openssl/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 <openssl/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 (OBJ_cmp(Asn1InCert, Asn1ToFind) == 0) {
219 //
220 // Found Eku in certificate.
221 //
222 Status = EFI_SUCCESS;
223 goto Exit;
224 }
225 }
226
227 Exit:
228
229 //
230 // Release Resources
231 //
232 if (ClonedCert != NULL) {
233 X509_free (ClonedCert);
234 }
235
236 if (Eku != NULL) {
237 sk_ASN1_OBJECT_pop_free (Eku, ASN1_OBJECT_free);
238 }
239
240 return Status;
241 }
242
243
244 /**
245 Determines if the specified EKUs are present in a signing certificate.
246
247 @param[in] SignerCert The certificate to check.
248 @param[in] RequiredEKUs The EKUs to look for.
249 @param[in] RequiredEKUsSize The number of EKUs
250 @param[in] RequireAllPresent If TRUE, then all the specified EKUs
251 must be present in the certificate.
252
253 @retval EFI_SUCCESS We successfully identified the signing type.
254 @retval EFI_INVALID_PARAMETER A parameter was invalid.
255 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
256 **/
257 EFI_STATUS
258 CheckEKUs(
259 IN CONST X509 *SignerCert,
260 IN CONST CHAR8 *RequiredEKUs[],
261 IN CONST UINT32 RequiredEKUsSize,
262 IN BOOLEAN RequireAllPresent
263 )
264 {
265 EFI_STATUS Status;
266 ASN1_OBJECT *Asn1ToFind;
267 UINT32 NumEkusFound;
268 UINT32 Index;
269
270 Status = EFI_NOT_FOUND;
271 Asn1ToFind = NULL;
272 NumEkusFound = 0;
273
274 if (SignerCert == NULL || RequiredEKUs == NULL || RequiredEKUsSize == 0) {
275 Status = EFI_INVALID_PARAMETER;
276 goto Exit;
277 }
278
279 for (Index = 0; Index < RequiredEKUsSize; Index++) {
280 //
281 // Finding required EKU in cert.
282 //
283 if (Asn1ToFind != NULL) {
284 ASN1_OBJECT_free(Asn1ToFind);
285 Asn1ToFind = NULL;
286 }
287
288 Asn1ToFind = OBJ_txt2obj (RequiredEKUs[Index], 0);
289 if (Asn1ToFind == NULL) {
290 //
291 // Fail to convert required EKU to ASN1.
292 //
293 Status = EFI_INVALID_PARAMETER;
294 goto Exit;
295 }
296
297 Status = IsEkuInCertificate (SignerCert, Asn1ToFind);
298 if (Status == EFI_SUCCESS) {
299 NumEkusFound++;
300 if (!RequireAllPresent) {
301 //
302 // Found at least one, so we are done.
303 //
304 goto Exit;
305 }
306 } else {
307 //
308 // Fail to find Eku in cert
309 break;
310 }
311 }
312
313 Exit:
314
315 if (Asn1ToFind != NULL) {
316 ASN1_OBJECT_free(Asn1ToFind);
317 }
318
319 if (RequireAllPresent &&
320 NumEkusFound == RequiredEKUsSize) {
321 //
322 // Found all required EKUs in certificate.
323 //
324 Status = EFI_SUCCESS;
325 }
326
327 return Status;
328 }
329
330 /**
331 This function receives a PKCS#7 formatted signature blob,
332 looks for the EKU SEQUENCE blob, and if found then looks
333 for all the required EKUs. This function was created so that
334 the Surface team can cut down on the number of Certificate
335 Authorities (CA's) by checking EKU's on leaf signers for
336 a specific product. This prevents one product's certificate
337 from signing another product's firmware or unlock blobs.
338
339 Note that this function does not validate the certificate chain.
340 That needs to be done before using this function.
341
342 @param[in] Pkcs7Signature The PKCS#7 signed information content block. An array
343 containing the content block with both the signature,
344 the signer's certificate, and any necessary intermediate
345 certificates.
346 @param[in] Pkcs7SignatureSize Number of bytes in Pkcs7Signature.
347 @param[in] RequiredEKUs Array of null-terminated strings listing OIDs of
348 required EKUs that must be present in the signature.
349 @param[in] RequiredEKUsSize Number of elements in the RequiredEKUs string array.
350 @param[in] RequireAllPresent If this is TRUE, then all of the specified EKU's
351 must be present in the leaf signer. If it is
352 FALSE, then we will succeed if we find any
353 of the specified EKU's.
354
355 @retval EFI_SUCCESS The required EKUs were found in the signature.
356 @retval EFI_INVALID_PARAMETER A parameter was invalid.
357 @retval EFI_NOT_FOUND One or more EKU's were not found in the signature.
358
359 **/
360 EFI_STATUS
361 EFIAPI
362 VerifyEKUsInPkcs7Signature (
363 IN CONST UINT8 *Pkcs7Signature,
364 IN CONST UINT32 SignatureSize,
365 IN CONST CHAR8 *RequiredEKUs[],
366 IN CONST UINT32 RequiredEKUsSize,
367 IN BOOLEAN RequireAllPresent
368 )
369 {
370 EFI_STATUS Status;
371 PKCS7 *Pkcs7;
372 STACK_OF(X509) *CertChain;
373 INT32 SignatureType;
374 INT32 NumberCertsInSignature;
375 X509 *SignerCert;
376 UINT8 *SignedData;
377 UINT8 *Temp;
378 UINTN SignedDataSize;
379 BOOLEAN IsWrapped;
380 BOOLEAN Ok;
381
382 Status = EFI_SUCCESS;
383 Pkcs7 = NULL;
384 CertChain = NULL;
385 SignatureType = 0;
386 NumberCertsInSignature = 0;
387 SignerCert = NULL;
388 SignedData = NULL;
389 SignedDataSize = 0;
390 IsWrapped = FALSE;
391 Ok = FALSE;
392
393 //
394 //Validate the input parameters.
395 //
396 if (Pkcs7Signature == NULL ||
397 SignatureSize == 0 ||
398 RequiredEKUs == NULL ||
399 RequiredEKUsSize == 0) {
400 Status = EFI_INVALID_PARAMETER;
401 goto Exit;
402 }
403
404 if (RequiredEKUsSize == 1) {
405 RequireAllPresent = TRUE;
406 }
407
408 //
409 // Wrap the PKCS7 data if needed.
410 //
411 Ok = WrapPkcs7Data (Pkcs7Signature,
412 SignatureSize,
413 &IsWrapped,
414 &SignedData,
415 &SignedDataSize);
416 if (!Ok) {
417 //
418 // Fail to Wrap the PKCS7 data.
419 //
420 Status = EFI_INVALID_PARAMETER;
421 goto Exit;
422 }
423
424 Temp = SignedData;
425
426 //
427 // Create the PKCS7 object.
428 //
429 Pkcs7 = d2i_PKCS7 (NULL, (const unsigned char **)&Temp, (INT32)SignedDataSize);
430 if (Pkcs7 == NULL) {
431 //
432 // Fail to read PKCS7 data.
433 //
434 Status = EFI_INVALID_PARAMETER;
435 goto Exit;
436 }
437
438 //
439 // Get the certificate chain.
440 //
441 SignatureType = OBJ_obj2nid (Pkcs7->type);
442 switch (SignatureType) {
443 case NID_pkcs7_signed:
444 if (Pkcs7->d.sign != NULL) {
445 CertChain = Pkcs7->d.sign->cert;
446 }
447 break;
448 case NID_pkcs7_signedAndEnveloped:
449 if (Pkcs7->d.signed_and_enveloped != NULL) {
450 CertChain = Pkcs7->d.signed_and_enveloped->cert;
451 }
452 break;
453 default:
454 break;
455 }
456
457 //
458 // Ensure we have a certificate stack
459 //
460 if (CertChain == NULL) {
461 //
462 // Fail to get the certificate stack from signature.
463 //
464 Status = EFI_INVALID_PARAMETER;
465 goto Exit;
466 }
467
468 //
469 // Find out how many certificates were in the PKCS7 signature.
470 //
471 NumberCertsInSignature = sk_X509_num (CertChain);
472
473 if (NumberCertsInSignature == 0) {
474 //
475 // Fail to find any certificates in signature.
476 //
477 Status = EFI_INVALID_PARAMETER;
478 goto Exit;
479 }
480
481 //
482 // Get the leaf signer.
483 //
484 Status = GetSignerCertificate (Pkcs7, &SignerCert);
485 if (Status != EFI_SUCCESS || SignerCert == NULL) {
486 //
487 // Fail to get the end-entity leaf signer certificate.
488 //
489 Status = EFI_INVALID_PARAMETER;
490 goto Exit;
491 }
492
493 Status = CheckEKUs (SignerCert, RequiredEKUs, RequiredEKUsSize, RequireAllPresent);
494 if (Status != EFI_SUCCESS) {
495 goto Exit;
496 }
497
498 Exit:
499
500 //
501 // Release Resources
502 //
503 // If the signature was not wrapped, then the call to WrapData() will allocate
504 // the data and add a header to it
505 //
506 if (!IsWrapped && SignedData) {
507 free (SignedData);
508 }
509
510 if (Pkcs7 != NULL) {
511 PKCS7_free (Pkcs7);
512 }
513
514 return Status;
515 }
516