]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
1d91ac3b0f4433c20908484bbe6c8fb946394bfa
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
1 /** @file
2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "InternalCryptLib.h"
10 #include <openssl/x509.h>
11 #include <openssl/x509v3.h>
12 #include <crypto/asn1.h>
13 #include <openssl/asn1.h>
14 #include <openssl/rsa.h>
15
16 /* OID*/
17 #define OID_EXT_KEY_USAGE { 0x55, 0x1D, 0x25 }
18 #define OID_BASIC_CONSTRAINTS { 0x55, 0x1D, 0x13 }
19
20 static CONST UINT8 mOidExtKeyUsage[] = OID_EXT_KEY_USAGE;
21 static CONST UINT8 mOidBasicConstraints[] = OID_BASIC_CONSTRAINTS;
22
23 #define CRYPTO_ASN1_TAG_CLASS_MASK 0xC0
24 #define CRYPTO_ASN1_TAG_PC_MASK 0x20
25 #define CRYPTO_ASN1_TAG_VALUE_MASK 0x1F
26
27 /**
28 Construct a X509 object from DER-encoded certificate data.
29
30 If Cert is NULL, then return FALSE.
31 If SingleX509Cert is NULL, then return FALSE.
32
33 @param[in] Cert Pointer to the DER-encoded certificate data.
34 @param[in] CertSize The size of certificate data in bytes.
35 @param[out] SingleX509Cert The generated X509 object.
36
37 @retval TRUE The X509 object generation succeeded.
38 @retval FALSE The operation failed.
39
40 **/
41 BOOLEAN
42 EFIAPI
43 X509ConstructCertificate (
44 IN CONST UINT8 *Cert,
45 IN UINTN CertSize,
46 OUT UINT8 **SingleX509Cert
47 )
48 {
49 X509 *X509Cert;
50 CONST UINT8 *Temp;
51
52 //
53 // Check input parameters.
54 //
55 if ((Cert == NULL) || (SingleX509Cert == NULL) || (CertSize > INT_MAX)) {
56 return FALSE;
57 }
58
59 //
60 // Read DER-encoded X509 Certificate and Construct X509 object.
61 //
62 Temp = Cert;
63 X509Cert = d2i_X509 (NULL, &Temp, (long)CertSize);
64 if (X509Cert == NULL) {
65 return FALSE;
66 }
67
68 *SingleX509Cert = (UINT8 *)X509Cert;
69
70 return TRUE;
71 }
72
73 /**
74 Construct a X509 stack object from a list of DER-encoded certificate data.
75
76 If X509Stack is NULL, then return FALSE.
77 If this interface is not supported, then return FALSE.
78
79 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
80 On output, pointer to the X509 stack object with new
81 inserted X509 certificate.
82 @param[in] Args VA_LIST marker for the variable argument list.
83 A list of DER-encoded single certificate data followed
84 by certificate size. A NULL terminates the list. The
85 pairs are the arguments to X509ConstructCertificate().
86
87 @retval TRUE The X509 stack construction succeeded.
88 @retval FALSE The construction operation failed.
89 @retval FALSE This interface is not supported.
90
91 **/
92 BOOLEAN
93 EFIAPI
94 X509ConstructCertificateStackV (
95 IN OUT UINT8 **X509Stack,
96 IN VA_LIST Args
97 )
98 {
99 UINT8 *Cert;
100 UINTN CertSize;
101 X509 *X509Cert;
102
103 STACK_OF (X509) *CertStack;
104 BOOLEAN Status;
105 UINTN Index;
106
107 //
108 // Check input parameters.
109 //
110 if (X509Stack == NULL) {
111 return FALSE;
112 }
113
114 Status = FALSE;
115
116 //
117 // Initialize X509 stack object.
118 //
119 CertStack = (STACK_OF (X509) *)(*X509Stack);
120 if (CertStack == NULL) {
121 CertStack = sk_X509_new_null ();
122 if (CertStack == NULL) {
123 return Status;
124 }
125 }
126
127 for (Index = 0; ; Index++) {
128 //
129 // If Cert is NULL, then it is the end of the list.
130 //
131 Cert = VA_ARG (Args, UINT8 *);
132 if (Cert == NULL) {
133 break;
134 }
135
136 CertSize = VA_ARG (Args, UINTN);
137 if (CertSize == 0) {
138 break;
139 }
140
141 //
142 // Construct X509 Object from the given DER-encoded certificate data.
143 //
144 X509Cert = NULL;
145 Status = X509ConstructCertificate (
146 (CONST UINT8 *)Cert,
147 CertSize,
148 (UINT8 **)&X509Cert
149 );
150 if (!Status) {
151 if (X509Cert != NULL) {
152 X509_free (X509Cert);
153 }
154
155 break;
156 }
157
158 //
159 // Insert the new X509 object into X509 stack object.
160 //
161 sk_X509_push (CertStack, X509Cert);
162 }
163
164 if (!Status) {
165 sk_X509_pop_free (CertStack, X509_free);
166 } else {
167 *X509Stack = (UINT8 *)CertStack;
168 }
169
170 return Status;
171 }
172
173 /**
174 Construct a X509 stack object from a list of DER-encoded certificate data.
175
176 If X509Stack is NULL, then return FALSE.
177
178 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
179 On output, pointer to the X509 stack object with new
180 inserted X509 certificate.
181 @param ... A list of DER-encoded single certificate data followed
182 by certificate size. A NULL terminates the list. The
183 pairs are the arguments to X509ConstructCertificate().
184
185 @retval TRUE The X509 stack construction succeeded.
186 @retval FALSE The construction operation failed.
187
188 **/
189 BOOLEAN
190 EFIAPI
191 X509ConstructCertificateStack (
192 IN OUT UINT8 **X509Stack,
193 ...
194 )
195 {
196 VA_LIST Args;
197 BOOLEAN Result;
198
199 VA_START (Args, X509Stack);
200 Result = X509ConstructCertificateStackV (X509Stack, Args);
201 VA_END (Args);
202 return Result;
203 }
204
205 /**
206 Release the specified X509 object.
207
208 If X509Cert is NULL, then return FALSE.
209
210 @param[in] X509Cert Pointer to the X509 object to be released.
211
212 **/
213 VOID
214 EFIAPI
215 X509Free (
216 IN VOID *X509Cert
217 )
218 {
219 //
220 // Check input parameters.
221 //
222 if (X509Cert == NULL) {
223 return;
224 }
225
226 //
227 // Free OpenSSL X509 object.
228 //
229 X509_free ((X509 *)X509Cert);
230 }
231
232 /**
233 Release the specified X509 stack object.
234
235 If X509Stack is NULL, then return FALSE.
236
237 @param[in] X509Stack Pointer to the X509 stack object to be released.
238
239 **/
240 VOID
241 EFIAPI
242 X509StackFree (
243 IN VOID *X509Stack
244 )
245 {
246 //
247 // Check input parameters.
248 //
249 if (X509Stack == NULL) {
250 return;
251 }
252
253 //
254 // Free OpenSSL X509 stack object.
255 //
256 sk_X509_pop_free ((STACK_OF (X509) *) X509Stack, X509_free);
257 }
258
259 /**
260 Retrieve the subject bytes from one X.509 certificate.
261
262 @param[in] Cert Pointer to the DER-encoded X509 certificate.
263 @param[in] CertSize Size of the X509 certificate in bytes.
264 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
265 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
266 and the size of buffer returned CertSubject on output.
267
268 If Cert is NULL, then return FALSE.
269 If SubjectSize is NULL, then return FALSE.
270
271 @retval TRUE The certificate subject retrieved successfully.
272 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
273 The SubjectSize will be updated with the required size.
274
275 **/
276 BOOLEAN
277 EFIAPI
278 X509GetSubjectName (
279 IN CONST UINT8 *Cert,
280 IN UINTN CertSize,
281 OUT UINT8 *CertSubject,
282 IN OUT UINTN *SubjectSize
283 )
284 {
285 BOOLEAN Status;
286 X509 *X509Cert;
287 X509_NAME *X509Name;
288 UINTN X509NameSize;
289
290 //
291 // Check input parameters.
292 //
293 if ((Cert == NULL) || (SubjectSize == NULL)) {
294 return FALSE;
295 }
296
297 X509Cert = NULL;
298
299 //
300 // Read DER-encoded X509 Certificate and Construct X509 object.
301 //
302 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
303 if ((X509Cert == NULL) || (!Status)) {
304 Status = FALSE;
305 goto _Exit;
306 }
307
308 Status = FALSE;
309
310 //
311 // Retrieve subject name from certificate object.
312 //
313 X509Name = X509_get_subject_name (X509Cert);
314 if (X509Name == NULL) {
315 goto _Exit;
316 }
317
318 X509NameSize = i2d_X509_NAME (X509Name, NULL);
319 if (*SubjectSize < X509NameSize) {
320 *SubjectSize = X509NameSize;
321 goto _Exit;
322 }
323
324 *SubjectSize = X509NameSize;
325 if (CertSubject != NULL) {
326 i2d_X509_NAME (X509Name, &CertSubject);
327 Status = TRUE;
328 }
329
330 _Exit:
331 //
332 // Release Resources.
333 //
334 if (X509Cert != NULL) {
335 X509_free (X509Cert);
336 }
337
338 return Status;
339 }
340
341 /**
342 Retrieve a string from one X.509 certificate base on the Request_NID.
343
344 @param[in] Cert Pointer to the DER-encoded X509 certificate.
345 @param[in] CertSize Size of the X509 certificate in bytes.
346 @param[in] Request_NID NID of string to obtain
347 @param[out] CommonName Buffer to contain the retrieved certificate common
348 name string (UTF8). At most CommonNameSize bytes will be
349 written and the string will be null terminated. May be
350 NULL in order to determine the size buffer needed.
351 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
352 and the size of buffer returned CommonName on output.
353 If CommonName is NULL then the amount of space needed
354 in buffer (including the final null) is returned.
355
356 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
357 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
358 If CommonNameSize is NULL.
359 If CommonName is not NULL and *CommonNameSize is 0.
360 If Certificate is invalid.
361 @retval RETURN_NOT_FOUND If no NID Name entry exists.
362 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
363 (including the final null) is returned in the
364 CommonNameSize parameter.
365 @retval RETURN_UNSUPPORTED The operation is not supported.
366
367 **/
368 STATIC
369 RETURN_STATUS
370 InternalX509GetNIDName (
371 IN CONST UINT8 *Cert,
372 IN UINTN CertSize,
373 IN INT32 Request_NID,
374 OUT CHAR8 *CommonName OPTIONAL,
375 IN OUT UINTN *CommonNameSize
376 )
377 {
378 RETURN_STATUS ReturnStatus;
379 BOOLEAN Status;
380 X509 *X509Cert;
381 X509_NAME *X509Name;
382 INT32 Index;
383 INTN Length;
384 X509_NAME_ENTRY *Entry;
385 ASN1_STRING *EntryData;
386 UINT8 *UTF8Name;
387
388 ReturnStatus = RETURN_INVALID_PARAMETER;
389 UTF8Name = NULL;
390
391 //
392 // Check input parameters.
393 //
394 if ((Cert == NULL) || (CertSize > INT_MAX) || (CommonNameSize == NULL)) {
395 return ReturnStatus;
396 }
397
398 if ((CommonName != NULL) && (*CommonNameSize == 0)) {
399 return ReturnStatus;
400 }
401
402 X509Cert = NULL;
403 //
404 // Read DER-encoded X509 Certificate and Construct X509 object.
405 //
406 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
407 if ((X509Cert == NULL) || (!Status)) {
408 //
409 // Invalid X.509 Certificate
410 //
411 goto _Exit;
412 }
413
414 Status = FALSE;
415
416 //
417 // Retrieve subject name from certificate object.
418 //
419 X509Name = X509_get_subject_name (X509Cert);
420 if (X509Name == NULL) {
421 //
422 // Fail to retrieve subject name content
423 //
424 goto _Exit;
425 }
426
427 //
428 // Retrive the string from X.509 Subject base on the Request_NID
429 //
430 Index = X509_NAME_get_index_by_NID (X509Name, Request_NID, -1);
431 if (Index < 0) {
432 //
433 // No Request_NID name entry exists in X509_NAME object
434 //
435 *CommonNameSize = 0;
436 ReturnStatus = RETURN_NOT_FOUND;
437 goto _Exit;
438 }
439
440 Entry = X509_NAME_get_entry (X509Name, Index);
441 if (Entry == NULL) {
442 //
443 // Fail to retrieve name entry data
444 //
445 *CommonNameSize = 0;
446 ReturnStatus = RETURN_NOT_FOUND;
447 goto _Exit;
448 }
449
450 EntryData = X509_NAME_ENTRY_get_data (Entry);
451
452 Length = ASN1_STRING_to_UTF8 (&UTF8Name, EntryData);
453 if (Length < 0) {
454 //
455 // Fail to convert the Name string
456 //
457 *CommonNameSize = 0;
458 ReturnStatus = RETURN_INVALID_PARAMETER;
459 goto _Exit;
460 }
461
462 if (CommonName == NULL) {
463 *CommonNameSize = Length + 1;
464 ReturnStatus = RETURN_BUFFER_TOO_SMALL;
465 } else {
466 *CommonNameSize = MIN ((UINTN)Length, *CommonNameSize - 1) + 1;
467 CopyMem (CommonName, UTF8Name, *CommonNameSize - 1);
468 CommonName[*CommonNameSize - 1] = '\0';
469 ReturnStatus = RETURN_SUCCESS;
470 }
471
472 _Exit:
473 //
474 // Release Resources.
475 //
476 if (X509Cert != NULL) {
477 X509_free (X509Cert);
478 }
479
480 if (UTF8Name != NULL) {
481 OPENSSL_free (UTF8Name);
482 }
483
484 return ReturnStatus;
485 }
486
487 /**
488 Retrieve the common name (CN) string from one X.509 certificate.
489
490 @param[in] Cert Pointer to the DER-encoded X509 certificate.
491 @param[in] CertSize Size of the X509 certificate in bytes.
492 @param[out] CommonName Buffer to contain the retrieved certificate common
493 name string. At most CommonNameSize bytes will be
494 written and the string will be null terminated. May be
495 NULL in order to determine the size buffer needed.
496 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
497 and the size of buffer returned CommonName on output.
498 If CommonName is NULL then the amount of space needed
499 in buffer (including the final null) is returned.
500
501 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
502 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
503 If CommonNameSize is NULL.
504 If CommonName is not NULL and *CommonNameSize is 0.
505 If Certificate is invalid.
506 @retval RETURN_NOT_FOUND If no CommonName entry exists.
507 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
508 (including the final null) is returned in the
509 CommonNameSize parameter.
510 @retval RETURN_UNSUPPORTED The operation is not supported.
511
512 **/
513 RETURN_STATUS
514 EFIAPI
515 X509GetCommonName (
516 IN CONST UINT8 *Cert,
517 IN UINTN CertSize,
518 OUT CHAR8 *CommonName OPTIONAL,
519 IN OUT UINTN *CommonNameSize
520 )
521 {
522 return InternalX509GetNIDName (Cert, CertSize, NID_commonName, CommonName, CommonNameSize);
523 }
524
525 /**
526 Retrieve the organization name (O) string from one X.509 certificate.
527
528 @param[in] Cert Pointer to the DER-encoded X509 certificate.
529 @param[in] CertSize Size of the X509 certificate in bytes.
530 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
531 name string. At most NameBufferSize bytes will be
532 written and the string will be null terminated. May be
533 NULL in order to determine the size buffer needed.
534 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
535 and the size of buffer returned Name on output.
536 If NameBuffer is NULL then the amount of space needed
537 in buffer (including the final null) is returned.
538
539 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
540 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
541 If NameBufferSize is NULL.
542 If NameBuffer is not NULL and *CommonNameSize is 0.
543 If Certificate is invalid.
544 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
545 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
546 (including the final null) is returned in the
547 CommonNameSize parameter.
548 @retval RETURN_UNSUPPORTED The operation is not supported.
549
550 **/
551 RETURN_STATUS
552 EFIAPI
553 X509GetOrganizationName (
554 IN CONST UINT8 *Cert,
555 IN UINTN CertSize,
556 OUT CHAR8 *NameBuffer OPTIONAL,
557 IN OUT UINTN *NameBufferSize
558 )
559 {
560 return InternalX509GetNIDName (Cert, CertSize, NID_organizationName, NameBuffer, NameBufferSize);
561 }
562
563 /**
564 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
565
566 @param[in] Cert Pointer to the DER-encoded X509 certificate.
567 @param[in] CertSize Size of the X509 certificate in bytes.
568 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
569 RSA public key component. Use RsaFree() function to free the
570 resource.
571
572 If Cert is NULL, then return FALSE.
573 If RsaContext is NULL, then return FALSE.
574
575 @retval TRUE RSA Public Key was retrieved successfully.
576 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
577
578 **/
579 BOOLEAN
580 EFIAPI
581 RsaGetPublicKeyFromX509 (
582 IN CONST UINT8 *Cert,
583 IN UINTN CertSize,
584 OUT VOID **RsaContext
585 )
586 {
587 BOOLEAN Status;
588 EVP_PKEY *Pkey;
589 X509 *X509Cert;
590
591 //
592 // Check input parameters.
593 //
594 if ((Cert == NULL) || (RsaContext == NULL)) {
595 return FALSE;
596 }
597
598 Pkey = NULL;
599 X509Cert = NULL;
600
601 //
602 // Read DER-encoded X509 Certificate and Construct X509 object.
603 //
604 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
605 if ((X509Cert == NULL) || (!Status)) {
606 Status = FALSE;
607 goto _Exit;
608 }
609
610 Status = FALSE;
611
612 //
613 // Retrieve and check EVP_PKEY data from X509 Certificate.
614 //
615 Pkey = X509_get_pubkey (X509Cert);
616 if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_RSA)) {
617 goto _Exit;
618 }
619
620 //
621 // Duplicate RSA Context from the retrieved EVP_PKEY.
622 //
623 if ((*RsaContext = RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey))) != NULL) {
624 Status = TRUE;
625 }
626
627 _Exit:
628 //
629 // Release Resources.
630 //
631 if (X509Cert != NULL) {
632 X509_free (X509Cert);
633 }
634
635 if (Pkey != NULL) {
636 EVP_PKEY_free (Pkey);
637 }
638
639 return Status;
640 }
641
642 /**
643 Verify one X509 certificate was issued by the trusted CA.
644
645 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
646 @param[in] CertSize Size of the X509 certificate in bytes.
647 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
648 @param[in] CACertSize Size of the CA Certificate in bytes.
649
650 If Cert is NULL, then return FALSE.
651 If CACert is NULL, then return FALSE.
652
653 @retval TRUE The certificate was issued by the trusted CA.
654 @retval FALSE Invalid certificate or the certificate was not issued by the given
655 trusted CA.
656
657 **/
658 BOOLEAN
659 EFIAPI
660 X509VerifyCert (
661 IN CONST UINT8 *Cert,
662 IN UINTN CertSize,
663 IN CONST UINT8 *CACert,
664 IN UINTN CACertSize
665 )
666 {
667 BOOLEAN Status;
668 X509 *X509Cert;
669 X509 *X509CACert;
670 X509_STORE *CertStore;
671 X509_STORE_CTX *CertCtx;
672
673 //
674 // Check input parameters.
675 //
676 if ((Cert == NULL) || (CACert == NULL)) {
677 return FALSE;
678 }
679
680 Status = FALSE;
681 X509Cert = NULL;
682 X509CACert = NULL;
683 CertStore = NULL;
684 CertCtx = NULL;
685
686 //
687 // Register & Initialize necessary digest algorithms for certificate verification.
688 //
689 if (EVP_add_digest (EVP_md5 ()) == 0) {
690 goto _Exit;
691 }
692
693 if (EVP_add_digest (EVP_sha1 ()) == 0) {
694 goto _Exit;
695 }
696
697 if (EVP_add_digest (EVP_sha256 ()) == 0) {
698 goto _Exit;
699 }
700
701 //
702 // Read DER-encoded certificate to be verified and Construct X509 object.
703 //
704 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
705 if ((X509Cert == NULL) || (!Status)) {
706 Status = FALSE;
707 goto _Exit;
708 }
709
710 //
711 // Read DER-encoded root certificate and Construct X509 object.
712 //
713 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **)&X509CACert);
714 if ((X509CACert == NULL) || (!Status)) {
715 Status = FALSE;
716 goto _Exit;
717 }
718
719 Status = FALSE;
720
721 //
722 // Set up X509 Store for trusted certificate.
723 //
724 CertStore = X509_STORE_new ();
725 if (CertStore == NULL) {
726 goto _Exit;
727 }
728
729 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {
730 goto _Exit;
731 }
732
733 //
734 // Allow partial certificate chains, terminated by a non-self-signed but
735 // still trusted intermediate certificate. Also disable time checks.
736 //
737 X509_STORE_set_flags (
738 CertStore,
739 X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME
740 );
741
742 //
743 // Set up X509_STORE_CTX for the subsequent verification operation.
744 //
745 CertCtx = X509_STORE_CTX_new ();
746 if (CertCtx == NULL) {
747 goto _Exit;
748 }
749
750 if (!X509_STORE_CTX_init (CertCtx, CertStore, X509Cert, NULL)) {
751 goto _Exit;
752 }
753
754 //
755 // X509 Certificate Verification.
756 //
757 Status = (BOOLEAN)X509_verify_cert (CertCtx);
758 X509_STORE_CTX_cleanup (CertCtx);
759
760 _Exit:
761 //
762 // Release Resources.
763 //
764 if (X509Cert != NULL) {
765 X509_free (X509Cert);
766 }
767
768 if (X509CACert != NULL) {
769 X509_free (X509CACert);
770 }
771
772 if (CertStore != NULL) {
773 X509_STORE_free (CertStore);
774 }
775
776 X509_STORE_CTX_free (CertCtx);
777
778 return Status;
779 }
780
781 /**
782 Retrieve the TBSCertificate from one given X.509 certificate.
783
784 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
785 @param[in] CertSize Size of the X509 certificate in bytes.
786 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
787 @param[out] TBSCertSize Size of the TBS certificate in bytes.
788
789 If Cert is NULL, then return FALSE.
790 If TBSCert is NULL, then return FALSE.
791 If TBSCertSize is NULL, then return FALSE.
792
793 @retval TRUE The TBSCertificate was retrieved successfully.
794 @retval FALSE Invalid X.509 certificate.
795
796 **/
797 BOOLEAN
798 EFIAPI
799 X509GetTBSCert (
800 IN CONST UINT8 *Cert,
801 IN UINTN CertSize,
802 OUT UINT8 **TBSCert,
803 OUT UINTN *TBSCertSize
804 )
805 {
806 CONST UINT8 *Temp;
807 UINT32 Asn1Tag;
808 UINT32 ObjClass;
809 UINTN Length;
810
811 //
812 // Check input parameters.
813 //
814 if ((Cert == NULL) || (TBSCert == NULL) ||
815 (TBSCertSize == NULL) || (CertSize > INT_MAX))
816 {
817 return FALSE;
818 }
819
820 //
821 // An X.509 Certificate is: (defined in RFC3280)
822 // Certificate ::= SEQUENCE {
823 // tbsCertificate TBSCertificate,
824 // signatureAlgorithm AlgorithmIdentifier,
825 // signature BIT STRING }
826 //
827 // and
828 //
829 // TBSCertificate ::= SEQUENCE {
830 // version [0] Version DEFAULT v1,
831 // ...
832 // }
833 //
834 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
835 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
836 //
837 Temp = Cert;
838 Length = 0;
839 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);
840
841 if (Asn1Tag != V_ASN1_SEQUENCE) {
842 return FALSE;
843 }
844
845 *TBSCert = (UINT8 *)Temp;
846
847 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);
848 //
849 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
850 //
851 if (Asn1Tag != V_ASN1_SEQUENCE) {
852 return FALSE;
853 }
854
855 *TBSCertSize = Length + (Temp - *TBSCert);
856
857 return TRUE;
858 }
859
860 /**
861 Retrieve the EC Public Key from one DER-encoded X509 certificate.
862
863 @param[in] Cert Pointer to the DER-encoded X509 certificate.
864 @param[in] CertSize Size of the X509 certificate in bytes.
865 @param[out] EcContext Pointer to new-generated EC DSA context which contain the retrieved
866 EC public key component. Use EcFree() function to free the
867 resource.
868
869 If Cert is NULL, then return FALSE.
870 If EcContext is NULL, then return FALSE.
871
872 @retval TRUE EC Public Key was retrieved successfully.
873 @retval FALSE Fail to retrieve EC public key from X509 certificate.
874
875 **/
876 BOOLEAN
877 EFIAPI
878 EcGetPublicKeyFromX509 (
879 IN CONST UINT8 *Cert,
880 IN UINTN CertSize,
881 OUT VOID **EcContext
882 )
883 {
884 #if FixedPcdGetBool (PcdOpensslEcEnabled)
885 BOOLEAN Status;
886 EVP_PKEY *Pkey;
887 X509 *X509Cert;
888
889 //
890 // Check input parameters.
891 //
892 if ((Cert == NULL) || (EcContext == NULL)) {
893 return FALSE;
894 }
895
896 Pkey = NULL;
897 X509Cert = NULL;
898
899 //
900 // Read DER-encoded X509 Certificate and Construct X509 object.
901 //
902 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
903 if ((X509Cert == NULL) || (!Status)) {
904 Status = FALSE;
905 goto _Exit;
906 }
907
908 Status = FALSE;
909
910 //
911 // Retrieve and check EVP_PKEY data from X509 Certificate.
912 //
913 Pkey = X509_get_pubkey (X509Cert);
914 if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_EC)) {
915 goto _Exit;
916 }
917
918 //
919 // Duplicate EC Context from the retrieved EVP_PKEY.
920 //
921 if ((*EcContext = EC_KEY_dup (EVP_PKEY_get0_EC_KEY (Pkey))) != NULL) {
922 Status = TRUE;
923 }
924
925 _Exit:
926 //
927 // Release Resources.
928 //
929 if (X509Cert != NULL) {
930 X509_free (X509Cert);
931 }
932
933 if (Pkey != NULL) {
934 EVP_PKEY_free (Pkey);
935 }
936
937 return Status;
938 #else
939 return FALSE;
940 #endif
941 }
942
943 /**
944 Retrieve the version from one X.509 certificate.
945
946 If Cert is NULL, then return FALSE.
947 If CertSize is 0, then return FALSE.
948 If this interface is not supported, then return FALSE.
949
950 @param[in] Cert Pointer to the DER-encoded X509 certificate.
951 @param[in] CertSize Size of the X509 certificate in bytes.
952 @param[out] Version Pointer to the retrieved version integer.
953
954 @retval TRUE The certificate version retrieved successfully.
955 @retval FALSE If Cert is NULL or CertSize is Zero.
956 @retval FALSE The operation is not supported.
957
958 **/
959 BOOLEAN
960 EFIAPI
961 X509GetVersion (
962 IN CONST UINT8 *Cert,
963 IN UINTN CertSize,
964 OUT UINTN *Version
965 )
966 {
967 BOOLEAN Status;
968 X509 *X509Cert;
969
970 X509Cert = NULL;
971 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
972 if ((X509Cert == NULL) || (!Status)) {
973 //
974 // Invalid X.509 Certificate
975 //
976 Status = FALSE;
977 }
978
979 if (Status) {
980 *Version = X509_get_version (X509Cert);
981 }
982
983 if (X509Cert != NULL) {
984 X509_free (X509Cert);
985 }
986
987 return Status;
988 }
989
990 /**
991 Retrieve the serialNumber from one X.509 certificate.
992
993 If Cert is NULL, then return FALSE.
994 If CertSize is 0, then return FALSE.
995 If this interface is not supported, then return FALSE.
996
997 @param[in] Cert Pointer to the DER-encoded X509 certificate.
998 @param[in] CertSize Size of the X509 certificate in bytes.
999 @param[out] SerialNumber Pointer to the retrieved certificate SerialNumber bytes.
1000 @param[in, out] SerialNumberSize The size in bytes of the SerialNumber buffer on input,
1001 and the size of buffer returned SerialNumber on output.
1002
1003 @retval TRUE The certificate serialNumber retrieved successfully.
1004 @retval FALSE If Cert is NULL or CertSize is Zero.
1005 If SerialNumberSize is NULL.
1006 If Certificate is invalid.
1007 @retval FALSE If no SerialNumber exists.
1008 @retval FALSE If the SerialNumber is NULL. The required buffer size
1009 (including the final null) is returned in the
1010 SerialNumberSize parameter.
1011 @retval FALSE The operation is not supported.
1012 **/
1013 BOOLEAN
1014 EFIAPI
1015 X509GetSerialNumber (
1016 IN CONST UINT8 *Cert,
1017 IN UINTN CertSize,
1018 OUT UINT8 *SerialNumber, OPTIONAL
1019 IN OUT UINTN *SerialNumberSize
1020 )
1021 {
1022 BOOLEAN Status;
1023 X509 *X509Cert;
1024 ASN1_INTEGER *Asn1Integer;
1025
1026 Status = FALSE;
1027 //
1028 // Check input parameters.
1029 //
1030 if ((Cert == NULL) || (SerialNumberSize == NULL)) {
1031 return Status;
1032 }
1033
1034 X509Cert = NULL;
1035
1036 //
1037 // Read DER-encoded X509 Certificate and Construct X509 object.
1038 //
1039 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1040 if ((X509Cert == NULL) || (!Status)) {
1041 *SerialNumberSize = 0;
1042 Status = FALSE;
1043 goto _Exit;
1044 }
1045
1046 //
1047 // Retrieve subject name from certificate object.
1048 //
1049 Asn1Integer = X509_get_serialNumber (X509Cert);
1050 if (Asn1Integer == NULL) {
1051 *SerialNumberSize = 0;
1052 Status = FALSE;
1053 goto _Exit;
1054 }
1055
1056 if (*SerialNumberSize < (UINTN)Asn1Integer->length) {
1057 *SerialNumberSize = (UINTN)Asn1Integer->length;
1058 Status = FALSE;
1059 goto _Exit;
1060 }
1061
1062 if (SerialNumber != NULL) {
1063 CopyMem (SerialNumber, Asn1Integer->data, *SerialNumberSize);
1064 Status = TRUE;
1065 }
1066
1067 *SerialNumberSize = (UINTN)Asn1Integer->length;
1068
1069 _Exit:
1070 //
1071 // Release Resources.
1072 //
1073 if (X509Cert != NULL) {
1074 X509_free (X509Cert);
1075 }
1076
1077 return Status;
1078 }
1079
1080 /**
1081 Retrieve the issuer bytes from one X.509 certificate.
1082
1083 If Cert is NULL, then return FALSE.
1084 If CertIssuerSize is NULL, then return FALSE.
1085 If this interface is not supported, then return FALSE.
1086
1087 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1088 @param[in] CertSize Size of the X509 certificate in bytes.
1089 @param[out] CertIssuer Pointer to the retrieved certificate subject bytes.
1090 @param[in, out] CertIssuerSize The size in bytes of the CertIssuer buffer on input,
1091 and the size of buffer returned CertSubject on output.
1092
1093 @retval TRUE The certificate issuer retrieved successfully.
1094 @retval FALSE Invalid certificate, or the CertIssuerSize is too small for the result.
1095 The CertIssuerSize will be updated with the required size.
1096 @retval FALSE This interface is not supported.
1097
1098 **/
1099 BOOLEAN
1100 EFIAPI
1101 X509GetIssuerName (
1102 IN CONST UINT8 *Cert,
1103 IN UINTN CertSize,
1104 OUT UINT8 *CertIssuer,
1105 IN OUT UINTN *CertIssuerSize
1106 )
1107 {
1108 BOOLEAN Status;
1109 X509 *X509Cert;
1110 X509_NAME *X509Name;
1111 UINTN X509NameSize;
1112
1113 //
1114 // Check input parameters.
1115 //
1116 if ((Cert == NULL) || (CertIssuerSize == NULL)) {
1117 return FALSE;
1118 }
1119
1120 X509Cert = NULL;
1121
1122 //
1123 // Read DER-encoded X509 Certificate and Construct X509 object.
1124 //
1125 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1126 if ((X509Cert == NULL) || (!Status)) {
1127 Status = FALSE;
1128 goto _Exit;
1129 }
1130
1131 Status = FALSE;
1132
1133 //
1134 // Retrieve subject name from certificate object.
1135 //
1136 X509Name = X509_get_subject_name (X509Cert);
1137 if (X509Name == NULL) {
1138 goto _Exit;
1139 }
1140
1141 X509NameSize = i2d_X509_NAME (X509Name, NULL);
1142 if (*CertIssuerSize < X509NameSize) {
1143 *CertIssuerSize = X509NameSize;
1144 goto _Exit;
1145 }
1146
1147 *CertIssuerSize = X509NameSize;
1148 if (CertIssuer != NULL) {
1149 i2d_X509_NAME (X509Name, &CertIssuer);
1150 Status = TRUE;
1151 }
1152
1153 _Exit:
1154 //
1155 // Release Resources.
1156 //
1157 if (X509Cert != NULL) {
1158 X509_free (X509Cert);
1159 }
1160
1161 return Status;
1162 }
1163
1164 /**
1165 Retrieve the Signature Algorithm from one X.509 certificate.
1166
1167 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1168 @param[in] CertSize Size of the X509 certificate in bytes.
1169 @param[out] Oid Signature Algorithm Object identifier buffer.
1170 @param[in,out] OidSize Signature Algorithm Object identifier buffer size
1171
1172 @retval TRUE The certificate Extension data retrieved successfully.
1173 @retval FALSE If Cert is NULL.
1174 If OidSize is NULL.
1175 If Oid is not NULL and *OidSize is 0.
1176 If Certificate is invalid.
1177 @retval FALSE If no SignatureType.
1178 @retval FALSE If the Oid is NULL. The required buffer size
1179 is returned in the OidSize.
1180 @retval FALSE The operation is not supported.
1181 **/
1182 BOOLEAN
1183 EFIAPI
1184 X509GetSignatureAlgorithm (
1185 IN CONST UINT8 *Cert,
1186 IN UINTN CertSize,
1187 OUT UINT8 *Oid, OPTIONAL
1188 IN OUT UINTN *OidSize
1189 )
1190 {
1191 BOOLEAN Status;
1192 X509 *X509Cert;
1193 int Nid;
1194 ASN1_OBJECT *Asn1Obj;
1195
1196 //
1197 // Check input parameters.
1198 //
1199 if ((Cert == NULL) || (OidSize == NULL) || (CertSize == 0)) {
1200 return FALSE;
1201 }
1202
1203 X509Cert = NULL;
1204 Status = FALSE;
1205
1206 //
1207 // Read DER-encoded X509 Certificate and Construct X509 object.
1208 //
1209 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1210 if ((X509Cert == NULL) || (!Status)) {
1211 Status = FALSE;
1212 goto _Exit;
1213 }
1214
1215 //
1216 // Retrieve subject name from certificate object.
1217 //
1218 Nid = X509_get_signature_nid (X509Cert);
1219 if (Nid == NID_undef) {
1220 *OidSize = 0;
1221 Status = FALSE;
1222 goto _Exit;
1223 }
1224
1225 Asn1Obj = OBJ_nid2obj (Nid);
1226 if (Asn1Obj == NULL) {
1227 *OidSize = 0;
1228 Status = FALSE;
1229 goto _Exit;
1230 }
1231
1232 if (*OidSize < (UINTN)Asn1Obj->length) {
1233 *OidSize = Asn1Obj->length;
1234 Status = FALSE;
1235 goto _Exit;
1236 }
1237
1238 if (Oid != NULL) {
1239 CopyMem (Oid, Asn1Obj->data, Asn1Obj->length);
1240 }
1241
1242 *OidSize = Asn1Obj->length;
1243 Status = TRUE;
1244
1245 _Exit:
1246 //
1247 // Release Resources.
1248 //
1249 if (X509Cert != NULL) {
1250 X509_free (X509Cert);
1251 }
1252
1253 return Status;
1254 }
1255
1256 /**
1257 Retrieve Extension data from one X.509 certificate.
1258
1259 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1260 @param[in] CertSize Size of the X509 certificate in bytes.
1261 @param[in] Oid Object identifier buffer
1262 @param[in] OidSize Object identifier buffer size
1263 @param[out] ExtensionData Extension bytes.
1264 @param[in, out] ExtensionDataSize Extension bytes size.
1265
1266 @retval TRUE The certificate Extension data retrieved successfully.
1267 @retval FALSE If Cert is NULL.
1268 If ExtensionDataSize is NULL.
1269 If ExtensionData is not NULL and *ExtensionDataSize is 0.
1270 If Certificate is invalid.
1271 @retval FALSE If no Extension entry match Oid.
1272 @retval FALSE If the ExtensionData is NULL. The required buffer size
1273 is returned in the ExtensionDataSize parameter.
1274 @retval FALSE The operation is not supported.
1275 **/
1276 BOOLEAN
1277 EFIAPI
1278 X509GetExtensionData (
1279 IN CONST UINT8 *Cert,
1280 IN UINTN CertSize,
1281 IN CONST UINT8 *Oid,
1282 IN UINTN OidSize,
1283 OUT UINT8 *ExtensionData,
1284 IN OUT UINTN *ExtensionDataSize
1285 )
1286 {
1287 BOOLEAN Status;
1288 INTN i;
1289 X509 *X509Cert;
1290
1291 CONST STACK_OF (X509_EXTENSION) *Extensions;
1292 ASN1_OBJECT *Asn1Obj;
1293 ASN1_OCTET_STRING *Asn1Oct;
1294 X509_EXTENSION *Ext;
1295 UINTN ObjLength;
1296 UINTN OctLength;
1297
1298 //
1299 // Check input parameters.
1300 //
1301 if ((Cert == NULL) || (CertSize == 0) || (Oid == NULL) || (OidSize == 0) || (ExtensionDataSize == NULL)) {
1302 return FALSE;
1303 }
1304
1305 X509Cert = NULL;
1306 Status = FALSE;
1307
1308 //
1309 // Read DER-encoded X509 Certificate and Construct X509 object.
1310 //
1311 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1312 if ((X509Cert == NULL) || (!Status)) {
1313 *ExtensionDataSize = 0;
1314 goto Cleanup;
1315 }
1316
1317 //
1318 // Retrieve Extensions from certificate object.
1319 //
1320 Extensions = X509_get0_extensions (X509Cert);
1321 if (sk_X509_EXTENSION_num (Extensions) <= 0) {
1322 *ExtensionDataSize = 0;
1323 goto Cleanup;
1324 }
1325
1326 //
1327 // Traverse Extensions
1328 //
1329 Status = FALSE;
1330 Asn1Oct = NULL;
1331 OctLength = 0;
1332 for (i = 0; i < sk_X509_EXTENSION_num (Extensions); i++) {
1333 Ext = sk_X509_EXTENSION_value (Extensions, (int)i);
1334 if (Ext == NULL) {
1335 continue;
1336 }
1337
1338 Asn1Obj = X509_EXTENSION_get_object (Ext);
1339 if (Asn1Obj == NULL) {
1340 continue;
1341 }
1342
1343 Asn1Oct = X509_EXTENSION_get_data (Ext);
1344 if (Asn1Oct == NULL) {
1345 continue;
1346 }
1347
1348 ObjLength = OBJ_length (Asn1Obj);
1349 OctLength = ASN1_STRING_length (Asn1Oct);
1350 if ((OidSize == ObjLength) && (CompareMem (OBJ_get0_data (Asn1Obj), Oid, OidSize) == 0)) {
1351 //
1352 // Extension Found
1353 //
1354 Status = TRUE;
1355 break;
1356 }
1357
1358 //
1359 // reset to 0 if not found
1360 //
1361 OctLength = 0;
1362 }
1363
1364 if (Status) {
1365 if (*ExtensionDataSize < OctLength) {
1366 *ExtensionDataSize = OctLength;
1367 Status = FALSE;
1368 goto Cleanup;
1369 }
1370
1371 if (Asn1Oct != NULL) {
1372 CopyMem (ExtensionData, ASN1_STRING_get0_data (Asn1Oct), OctLength);
1373 }
1374
1375 *ExtensionDataSize = OctLength;
1376 } else {
1377 *ExtensionDataSize = 0;
1378 }
1379
1380 Cleanup:
1381 //
1382 // Release Resources.
1383 //
1384 if (X509Cert != NULL) {
1385 X509_free (X509Cert);
1386 }
1387
1388 return Status;
1389 }
1390
1391 /**
1392 Retrieve the Extended Key Usage from one X.509 certificate.
1393
1394 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1395 @param[in] CertSize Size of the X509 certificate in bytes.
1396 @param[out] Usage Key Usage bytes.
1397 @param[in, out] UsageSize Key Usage buffer sizs in bytes.
1398
1399 @retval TRUE The Usage bytes retrieve successfully.
1400 @retval FALSE If Cert is NULL.
1401 If CertSize is NULL.
1402 If Usage is not NULL and *UsageSize is 0.
1403 If Cert is invalid.
1404 @retval FALSE If the Usage is NULL. The required buffer size
1405 is returned in the UsageSize parameter.
1406 @retval FALSE The operation is not supported.
1407 **/
1408 BOOLEAN
1409 EFIAPI
1410 X509GetExtendedKeyUsage (
1411 IN CONST UINT8 *Cert,
1412 IN UINTN CertSize,
1413 OUT UINT8 *Usage,
1414 IN OUT UINTN *UsageSize
1415 )
1416 {
1417 BOOLEAN Status;
1418
1419 Status = X509GetExtensionData (Cert, CertSize, mOidExtKeyUsage, sizeof (mOidExtKeyUsage), Usage, UsageSize);
1420 return Status;
1421 }
1422
1423 /**
1424 Retrieve the Validity from one X.509 certificate
1425
1426 If Cert is NULL, then return FALSE.
1427 If CertIssuerSize is NULL, then return FALSE.
1428 If this interface is not supported, then return FALSE.
1429
1430 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1431 @param[in] CertSize Size of the X509 certificate in bytes.
1432 @param[out] From notBefore Pointer to DateTime object.
1433 @param[in,out] FromSize notBefore DateTime object size.
1434 @param[out] To notAfter Pointer to DateTime object.
1435 @param[in,out] ToSize notAfter DateTime object size.
1436
1437 Note: X509CompareDateTime to compare DateTime oject
1438 x509SetDateTime to get a DateTime object from a DateTimeStr
1439
1440 @retval TRUE The certificate Validity retrieved successfully.
1441 @retval FALSE Invalid certificate, or Validity retrieve failed.
1442 @retval FALSE This interface is not supported.
1443 **/
1444 BOOLEAN
1445 EFIAPI
1446 X509GetValidity (
1447 IN CONST UINT8 *Cert,
1448 IN UINTN CertSize,
1449 IN UINT8 *From,
1450 IN OUT UINTN *FromSize,
1451 IN UINT8 *To,
1452 IN OUT UINTN *ToSize
1453 )
1454 {
1455 BOOLEAN Status;
1456 X509 *X509Cert;
1457 CONST ASN1_TIME *F;
1458 CONST ASN1_TIME *T;
1459 UINTN TSize;
1460 UINTN FSize;
1461
1462 //
1463 // Check input parameters.
1464 //
1465 if ((Cert == NULL) || (FromSize == NULL) || (ToSize == NULL) || (CertSize == 0)) {
1466 return FALSE;
1467 }
1468
1469 X509Cert = NULL;
1470 Status = FALSE;
1471
1472 //
1473 // Read DER-encoded X509 Certificate and Construct X509 object.
1474 //
1475 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1476 if ((X509Cert == NULL) || (!Status)) {
1477 goto _Exit;
1478 }
1479
1480 //
1481 // Retrieve Validity from/to from certificate object.
1482 //
1483 F = X509_get0_notBefore (X509Cert);
1484 T = X509_get0_notAfter (X509Cert);
1485
1486 if ((F == NULL) || (T == NULL)) {
1487 goto _Exit;
1488 }
1489
1490 FSize = sizeof (ASN1_TIME) + F->length;
1491 if (*FromSize < FSize) {
1492 *FromSize = FSize;
1493 goto _Exit;
1494 }
1495
1496 *FromSize = FSize;
1497 if (From != NULL) {
1498 CopyMem (From, F, sizeof (ASN1_TIME));
1499 ((ASN1_TIME *)From)->data = From + sizeof (ASN1_TIME);
1500 CopyMem (From + sizeof (ASN1_TIME), F->data, F->length);
1501 }
1502
1503 TSize = sizeof (ASN1_TIME) + T->length;
1504 if (*ToSize < TSize) {
1505 *ToSize = TSize;
1506 goto _Exit;
1507 }
1508
1509 *ToSize = TSize;
1510 if (To != NULL) {
1511 CopyMem (To, T, sizeof (ASN1_TIME));
1512 ((ASN1_TIME *)To)->data = To + sizeof (ASN1_TIME);
1513 CopyMem (To + sizeof (ASN1_TIME), T->data, T->length);
1514 }
1515
1516 Status = TRUE;
1517
1518 _Exit:
1519 //
1520 // Release Resources.
1521 //
1522 if (X509Cert != NULL) {
1523 X509_free (X509Cert);
1524 }
1525
1526 return Status;
1527 }
1528
1529 /**
1530 Format a DateTimeStr to DataTime object in DataTime Buffer
1531
1532 If DateTimeStr is NULL, then return FALSE.
1533 If DateTimeSize is NULL, then return FALSE.
1534 If this interface is not supported, then return FALSE.
1535
1536 @param[in] DateTimeStr DateTime string like YYYYMMDDhhmmssZ
1537 Ref: https://www.w3.org/TR/NOTE-datetime
1538 Z stand for UTC time
1539 @param[out] DateTime Pointer to a DateTime object.
1540 @param[in,out] DateTimeSize DateTime object buffer size.
1541
1542 @retval TRUE The DateTime object create successfully.
1543 @retval FALSE If DateTimeStr is NULL.
1544 If DateTimeSize is NULL.
1545 If DateTime is not NULL and *DateTimeSize is 0.
1546 If Year Month Day Hour Minute Second combination is invalid datetime.
1547 @retval FALSE If the DateTime is NULL. The required buffer size
1548 (including the final null) is returned in the
1549 DateTimeSize parameter.
1550 @retval FALSE The operation is not supported.
1551 **/
1552 BOOLEAN
1553 EFIAPI
1554 X509FormatDateTime (
1555 IN CONST CHAR8 *DateTimeStr,
1556 OUT VOID *DateTime,
1557 IN OUT UINTN *DateTimeSize
1558 )
1559 {
1560 BOOLEAN Status;
1561 INT32 Ret;
1562 ASN1_TIME *Dt;
1563 UINTN DSize;
1564
1565 Dt = NULL;
1566 Status = FALSE;
1567
1568 Dt = ASN1_TIME_new ();
1569 if (Dt == NULL) {
1570 Status = FALSE;
1571 goto Cleanup;
1572 }
1573
1574 Ret = ASN1_TIME_set_string_X509 (Dt, DateTimeStr);
1575 if (Ret != 1) {
1576 Status = FALSE;
1577 goto Cleanup;
1578 }
1579
1580 DSize = sizeof (ASN1_TIME) + Dt->length;
1581 if (*DateTimeSize < DSize) {
1582 *DateTimeSize = DSize;
1583 Status = FALSE;
1584 goto Cleanup;
1585 }
1586
1587 *DateTimeSize = DSize;
1588 if (DateTime != NULL) {
1589 CopyMem (DateTime, Dt, sizeof (ASN1_TIME));
1590 ((ASN1_TIME *)DateTime)->data = (UINT8 *)DateTime + sizeof (ASN1_TIME);
1591 CopyMem ((UINT8 *)DateTime + sizeof (ASN1_TIME), Dt->data, Dt->length);
1592 }
1593
1594 Status = TRUE;
1595
1596 Cleanup:
1597 if (Dt != NULL) {
1598 ASN1_TIME_free (Dt);
1599 }
1600
1601 return Status;
1602 }
1603
1604 /**
1605 Compare DateTime1 object and DateTime2 object.
1606
1607 If DateTime1 is NULL, then return -2.
1608 If DateTime2 is NULL, then return -2.
1609 If DateTime1 == DateTime2, then return 0
1610 If DateTime1 > DateTime2, then return 1
1611 If DateTime1 < DateTime2, then return -1
1612
1613 @param[in] DateTime1 Pointer to a DateTime Ojbect
1614 @param[in] DateTime2 Pointer to a DateTime Object
1615
1616 @retval 0 If DateTime1 == DateTime2
1617 @retval 1 If DateTime1 > DateTime2
1618 @retval -1 If DateTime1 < DateTime2
1619 **/
1620 INT32
1621 EFIAPI
1622 X509CompareDateTime (
1623 IN CONST VOID *DateTime1,
1624 IN CONST VOID *DateTime2
1625 )
1626 {
1627 return (INT32)ASN1_TIME_compare (DateTime1, DateTime2);
1628 }
1629
1630 /**
1631 Retrieve the Key Usage from one X.509 certificate.
1632
1633 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1634 @param[in] CertSize Size of the X509 certificate in bytes.
1635 @param[out] Usage Key Usage (CRYPTO_X509_KU_*)
1636
1637 @retval TRUE The certificate Key Usage retrieved successfully.
1638 @retval FALSE Invalid certificate, or Usage is NULL
1639 @retval FALSE This interface is not supported.
1640 **/
1641 BOOLEAN
1642 EFIAPI
1643 X509GetKeyUsage (
1644 IN CONST UINT8 *Cert,
1645 IN UINTN CertSize,
1646 OUT UINTN *Usage
1647 )
1648 {
1649 BOOLEAN Status;
1650 X509 *X509Cert;
1651
1652 //
1653 // Check input parameters.
1654 //
1655 if ((Cert == NULL) || (Usage == NULL)) {
1656 return FALSE;
1657 }
1658
1659 X509Cert = NULL;
1660 Status = FALSE;
1661
1662 //
1663 // Read DER-encoded X509 Certificate and Construct X509 object.
1664 //
1665 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1666 if ((X509Cert == NULL) || (!Status)) {
1667 goto _Exit;
1668 }
1669
1670 //
1671 // Retrieve subject name from certificate object.
1672 //
1673 *Usage = X509_get_key_usage (X509Cert);
1674 if (*Usage == NID_undef) {
1675 goto _Exit;
1676 }
1677
1678 Status = TRUE;
1679
1680 _Exit:
1681 //
1682 // Release Resources.
1683 //
1684 if (X509Cert != NULL) {
1685 X509_free (X509Cert);
1686 }
1687
1688 return Status;
1689 }
1690
1691 /**
1692 Verify one X509 certificate was issued by the trusted CA.
1693 @param[in] RootCert Trusted Root Certificate buffer
1694
1695 @param[in] RootCertLength Trusted Root Certificate buffer length
1696 @param[in] CertChain One or more ASN.1 DER-encoded X.509 certificates
1697 where the first certificate is signed by the Root
1698 Certificate or is the Root Cerificate itself. and
1699 subsequent cerificate is signed by the preceding
1700 cerificate.
1701 @param[in] CertChainLength Total length of the certificate chain, in bytes.
1702
1703 @retval TRUE All cerificates was issued by the first certificate in X509Certchain.
1704 @retval FALSE Invalid certificate or the certificate was not issued by the given
1705 trusted CA.
1706 **/
1707 BOOLEAN
1708 EFIAPI
1709 X509VerifyCertChain (
1710 IN CONST UINT8 *RootCert,
1711 IN UINTN RootCertLength,
1712 IN CONST UINT8 *CertChain,
1713 IN UINTN CertChainLength
1714 )
1715 {
1716 CONST UINT8 *TmpPtr;
1717 UINTN Length;
1718 UINT32 Asn1Tag;
1719 UINT32 ObjClass;
1720 CONST UINT8 *CurrentCert;
1721 UINTN CurrentCertLen;
1722 CONST UINT8 *PrecedingCert;
1723 UINTN PrecedingCertLen;
1724 BOOLEAN VerifyFlag;
1725 INT32 Ret;
1726
1727 PrecedingCert = RootCert;
1728 PrecedingCertLen = RootCertLength;
1729
1730 CurrentCert = CertChain;
1731 Length = 0;
1732 CurrentCertLen = 0;
1733
1734 VerifyFlag = FALSE;
1735 while (TRUE) {
1736 TmpPtr = CurrentCert;
1737 Ret = ASN1_get_object (
1738 (CONST UINT8 **)&TmpPtr,
1739 (long *)&Length,
1740 (int *)&Asn1Tag,
1741 (int *)&ObjClass,
1742 (long)(CertChainLength + CertChain - TmpPtr)
1743 );
1744 if ((Asn1Tag != V_ASN1_SEQUENCE) || (Ret == 0x80)) {
1745 break;
1746 }
1747
1748 //
1749 // Calculate CurrentCert length;
1750 //
1751 CurrentCertLen = TmpPtr - CurrentCert + Length;
1752
1753 //
1754 // Verify CurrentCert with preceding cert;
1755 //
1756 VerifyFlag = X509VerifyCert (CurrentCert, CurrentCertLen, PrecedingCert, PrecedingCertLen);
1757 if (VerifyFlag == FALSE) {
1758 break;
1759 }
1760
1761 //
1762 // move Current cert to Preceding cert
1763 //
1764 PrecedingCertLen = CurrentCertLen;
1765 PrecedingCert = CurrentCert;
1766
1767 //
1768 // Move to next
1769 //
1770 CurrentCert = CurrentCert + CurrentCertLen;
1771 }
1772
1773 return VerifyFlag;
1774 }
1775
1776 /**
1777 Get one X509 certificate from CertChain.
1778
1779 @param[in] CertChain One or more ASN.1 DER-encoded X.509 certificates
1780 where the first certificate is signed by the Root
1781 Certificate or is the Root Cerificate itself. and
1782 subsequent cerificate is signed by the preceding
1783 cerificate.
1784 @param[in] CertChainLength Total length of the certificate chain, in bytes.
1785
1786 @param[in] CertIndex Index of certificate.
1787
1788 @param[out] Cert The certificate at the index of CertChain.
1789 @param[out] CertLength The length certificate at the index of CertChain.
1790
1791 @retval TRUE Success.
1792 @retval FALSE Failed to get certificate from certificate chain.
1793 **/
1794 BOOLEAN
1795 EFIAPI
1796 X509GetCertFromCertChain (
1797 IN CONST UINT8 *CertChain,
1798 IN UINTN CertChainLength,
1799 IN CONST INT32 CertIndex,
1800 OUT CONST UINT8 **Cert,
1801 OUT UINTN *CertLength
1802 )
1803 {
1804 UINTN Asn1Len;
1805 INT32 CurrentIndex;
1806 UINTN CurrentCertLen;
1807 CONST UINT8 *CurrentCert;
1808 CONST UINT8 *TmpPtr;
1809 INT32 Ret;
1810 UINT32 Asn1Tag;
1811 UINT32 ObjClass;
1812
1813 //
1814 // Check input parameters.
1815 //
1816 if ((CertChain == NULL) || (Cert == NULL) ||
1817 (CertIndex < -1) || (CertLength == NULL))
1818 {
1819 return FALSE;
1820 }
1821
1822 Asn1Len = 0;
1823 CurrentCertLen = 0;
1824 CurrentCert = CertChain;
1825 CurrentIndex = -1;
1826
1827 //
1828 // Traverse the certificate chain
1829 //
1830 while (TRUE) {
1831 TmpPtr = CurrentCert;
1832
1833 // Get asn1 object and taglen
1834 Ret = ASN1_get_object (
1835 (CONST UINT8 **)&TmpPtr,
1836 (long *)&Asn1Len,
1837 (int *)&Asn1Tag,
1838 (int *)&ObjClass,
1839 (long)(CertChainLength + CertChain - TmpPtr)
1840 );
1841 if ((Asn1Tag != V_ASN1_SEQUENCE) || (Ret == 0x80)) {
1842 break;
1843 }
1844
1845 //
1846 // Calculate CurrentCert length;
1847 //
1848 CurrentCertLen = TmpPtr - CurrentCert + Asn1Len;
1849 CurrentIndex++;
1850
1851 if (CurrentIndex == CertIndex) {
1852 *Cert = CurrentCert;
1853 *CertLength = CurrentCertLen;
1854 return TRUE;
1855 }
1856
1857 //
1858 // Move to next
1859 //
1860 CurrentCert = CurrentCert + CurrentCertLen;
1861 }
1862
1863 //
1864 // If CertIndex is -1, Return the last certificate
1865 //
1866 if ((CertIndex == -1) && (CurrentIndex >= 0)) {
1867 *Cert = CurrentCert - CurrentCertLen;
1868 *CertLength = CurrentCertLen;
1869 return TRUE;
1870 }
1871
1872 return FALSE;
1873 }
1874
1875 /**
1876 Retrieve the tag and length of the tag.
1877
1878 @param Ptr The position in the ASN.1 data
1879 @param End End of data
1880 @param Length The variable that will receive the length
1881 @param Tag The expected tag
1882
1883 @retval TRUE Get tag successful
1884 @retval FALSe Failed to get tag or tag not match
1885 **/
1886 BOOLEAN
1887 EFIAPI
1888 Asn1GetTag (
1889 IN OUT UINT8 **Ptr,
1890 IN CONST UINT8 *End,
1891 OUT UINTN *Length,
1892 IN UINT32 Tag
1893 )
1894 {
1895 UINT8 *PtrOld;
1896 INT32 ObjTag;
1897 INT32 ObjCls;
1898 long ObjLength;
1899
1900 //
1901 // Save Ptr position
1902 //
1903 PtrOld = *Ptr;
1904
1905 ASN1_get_object ((CONST UINT8 **)Ptr, &ObjLength, &ObjTag, &ObjCls, (INT32)(End - (*Ptr)));
1906 if ((ObjTag == (INT32)(Tag & CRYPTO_ASN1_TAG_VALUE_MASK)) &&
1907 (ObjCls == (INT32)(Tag & CRYPTO_ASN1_TAG_CLASS_MASK)))
1908 {
1909 *Length = (UINTN)ObjLength;
1910 return TRUE;
1911 } else {
1912 //
1913 // if doesn't match Tag, restore Ptr to origin Ptr
1914 //
1915 *Ptr = PtrOld;
1916 return FALSE;
1917 }
1918 }
1919
1920 /**
1921 Retrieve the basic constraints from one X.509 certificate.
1922
1923 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1924 @param[in] CertSize size of the X509 certificate in bytes.
1925 @param[out] BasicConstraints basic constraints bytes.
1926 @param[in, out] BasicConstraintsSize basic constraints buffer sizs in bytes.
1927
1928 @retval TRUE The basic constraints retrieve successfully.
1929 @retval FALSE If cert is NULL.
1930 If cert_size is NULL.
1931 If basic_constraints is not NULL and *basic_constraints_size is 0.
1932 If cert is invalid.
1933 @retval FALSE The required buffer size is small.
1934 The return buffer size is basic_constraints_size parameter.
1935 @retval FALSE If no Extension entry match oid.
1936 @retval FALSE The operation is not supported.
1937 **/
1938 BOOLEAN
1939 EFIAPI
1940 X509GetExtendedBasicConstraints (
1941 CONST UINT8 *Cert,
1942 UINTN CertSize,
1943 UINT8 *BasicConstraints,
1944 UINTN *BasicConstraintsSize
1945 )
1946 {
1947 BOOLEAN Status;
1948
1949 if ((Cert == NULL) || (CertSize == 0) || (BasicConstraintsSize == NULL)) {
1950 return FALSE;
1951 }
1952
1953 Status = X509GetExtensionData (
1954 (UINT8 *)Cert,
1955 CertSize,
1956 mOidBasicConstraints,
1957 sizeof (mOidBasicConstraints),
1958 BasicConstraints,
1959 BasicConstraintsSize
1960 );
1961
1962 return Status;
1963 }