]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
2333157e0d17e13d3ae7f1146e73d691c68a5d2f
[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 BOOLEAN Status;
885 EVP_PKEY *Pkey;
886 X509 *X509Cert;
887
888 //
889 // Check input parameters.
890 //
891 if ((Cert == NULL) || (EcContext == NULL)) {
892 return FALSE;
893 }
894
895 Pkey = NULL;
896 X509Cert = NULL;
897
898 //
899 // Read DER-encoded X509 Certificate and Construct X509 object.
900 //
901 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
902 if ((X509Cert == NULL) || (!Status)) {
903 Status = FALSE;
904 goto _Exit;
905 }
906
907 Status = FALSE;
908
909 //
910 // Retrieve and check EVP_PKEY data from X509 Certificate.
911 //
912 Pkey = X509_get_pubkey (X509Cert);
913 if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_EC)) {
914 goto _Exit;
915 }
916
917 //
918 // Duplicate EC Context from the retrieved EVP_PKEY.
919 //
920 if ((*EcContext = EC_KEY_dup (EVP_PKEY_get0_EC_KEY (Pkey))) != NULL) {
921 Status = TRUE;
922 }
923
924 _Exit:
925 //
926 // Release Resources.
927 //
928 if (X509Cert != NULL) {
929 X509_free (X509Cert);
930 }
931
932 if (Pkey != NULL) {
933 EVP_PKEY_free (Pkey);
934 }
935
936 return Status;
937 }
938
939 /**
940 Retrieve the version from one X.509 certificate.
941
942 If Cert is NULL, then return FALSE.
943 If CertSize is 0, then return FALSE.
944 If this interface is not supported, then return FALSE.
945
946 @param[in] Cert Pointer to the DER-encoded X509 certificate.
947 @param[in] CertSize Size of the X509 certificate in bytes.
948 @param[out] Version Pointer to the retrieved version integer.
949
950 @retval TRUE The certificate version retrieved successfully.
951 @retval FALSE If Cert is NULL or CertSize is Zero.
952 @retval FALSE The operation is not supported.
953
954 **/
955 BOOLEAN
956 EFIAPI
957 X509GetVersion (
958 IN CONST UINT8 *Cert,
959 IN UINTN CertSize,
960 OUT UINTN *Version
961 )
962 {
963 BOOLEAN Status;
964 X509 *X509Cert;
965
966 X509Cert = NULL;
967 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
968 if ((X509Cert == NULL) || (!Status)) {
969 //
970 // Invalid X.509 Certificate
971 //
972 Status = FALSE;
973 }
974
975 if (Status) {
976 *Version = X509_get_version (X509Cert);
977 }
978
979 if (X509Cert != NULL) {
980 X509_free (X509Cert);
981 }
982
983 return Status;
984 }
985
986 /**
987 Retrieve the serialNumber from one X.509 certificate.
988
989 If Cert is NULL, then return FALSE.
990 If CertSize is 0, then return FALSE.
991 If this interface is not supported, then return FALSE.
992
993 @param[in] Cert Pointer to the DER-encoded X509 certificate.
994 @param[in] CertSize Size of the X509 certificate in bytes.
995 @param[out] SerialNumber Pointer to the retrieved certificate SerialNumber bytes.
996 @param[in, out] SerialNumberSize The size in bytes of the SerialNumber buffer on input,
997 and the size of buffer returned SerialNumber on output.
998
999 @retval TRUE The certificate serialNumber retrieved successfully.
1000 @retval FALSE If Cert is NULL or CertSize is Zero.
1001 If SerialNumberSize is NULL.
1002 If Certificate is invalid.
1003 @retval FALSE If no SerialNumber exists.
1004 @retval FALSE If the SerialNumber is NULL. The required buffer size
1005 (including the final null) is returned in the
1006 SerialNumberSize parameter.
1007 @retval FALSE The operation is not supported.
1008 **/
1009 BOOLEAN
1010 EFIAPI
1011 X509GetSerialNumber (
1012 IN CONST UINT8 *Cert,
1013 IN UINTN CertSize,
1014 OUT UINT8 *SerialNumber, OPTIONAL
1015 IN OUT UINTN *SerialNumberSize
1016 )
1017 {
1018 BOOLEAN Status;
1019 X509 *X509Cert;
1020 ASN1_INTEGER *Asn1Integer;
1021
1022 Status = FALSE;
1023 //
1024 // Check input parameters.
1025 //
1026 if ((Cert == NULL) || (SerialNumberSize == NULL)) {
1027 return Status;
1028 }
1029
1030 X509Cert = NULL;
1031
1032 //
1033 // Read DER-encoded X509 Certificate and Construct X509 object.
1034 //
1035 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1036 if ((X509Cert == NULL) || (!Status)) {
1037 *SerialNumberSize = 0;
1038 Status = FALSE;
1039 goto _Exit;
1040 }
1041
1042 //
1043 // Retrieve subject name from certificate object.
1044 //
1045 Asn1Integer = X509_get_serialNumber (X509Cert);
1046 if (Asn1Integer == NULL) {
1047 *SerialNumberSize = 0;
1048 Status = FALSE;
1049 goto _Exit;
1050 }
1051
1052 if (*SerialNumberSize < (UINTN)Asn1Integer->length) {
1053 *SerialNumberSize = (UINTN)Asn1Integer->length;
1054 Status = FALSE;
1055 goto _Exit;
1056 }
1057
1058 if (SerialNumber != NULL) {
1059 CopyMem (SerialNumber, Asn1Integer->data, *SerialNumberSize);
1060 Status = TRUE;
1061 }
1062
1063 *SerialNumberSize = (UINTN)Asn1Integer->length;
1064
1065 _Exit:
1066 //
1067 // Release Resources.
1068 //
1069 if (X509Cert != NULL) {
1070 X509_free (X509Cert);
1071 }
1072
1073 return Status;
1074 }
1075
1076 /**
1077 Retrieve the issuer bytes from one X.509 certificate.
1078
1079 If Cert is NULL, then return FALSE.
1080 If CertIssuerSize is NULL, then return FALSE.
1081 If this interface is not supported, then return FALSE.
1082
1083 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1084 @param[in] CertSize Size of the X509 certificate in bytes.
1085 @param[out] CertIssuer Pointer to the retrieved certificate subject bytes.
1086 @param[in, out] CertIssuerSize The size in bytes of the CertIssuer buffer on input,
1087 and the size of buffer returned CertSubject on output.
1088
1089 @retval TRUE The certificate issuer retrieved successfully.
1090 @retval FALSE Invalid certificate, or the CertIssuerSize is too small for the result.
1091 The CertIssuerSize will be updated with the required size.
1092 @retval FALSE This interface is not supported.
1093
1094 **/
1095 BOOLEAN
1096 EFIAPI
1097 X509GetIssuerName (
1098 IN CONST UINT8 *Cert,
1099 IN UINTN CertSize,
1100 OUT UINT8 *CertIssuer,
1101 IN OUT UINTN *CertIssuerSize
1102 )
1103 {
1104 BOOLEAN Status;
1105 X509 *X509Cert;
1106 X509_NAME *X509Name;
1107 UINTN X509NameSize;
1108
1109 //
1110 // Check input parameters.
1111 //
1112 if ((Cert == NULL) || (CertIssuerSize == NULL)) {
1113 return FALSE;
1114 }
1115
1116 X509Cert = NULL;
1117
1118 //
1119 // Read DER-encoded X509 Certificate and Construct X509 object.
1120 //
1121 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1122 if ((X509Cert == NULL) || (!Status)) {
1123 Status = FALSE;
1124 goto _Exit;
1125 }
1126
1127 Status = FALSE;
1128
1129 //
1130 // Retrieve subject name from certificate object.
1131 //
1132 X509Name = X509_get_subject_name (X509Cert);
1133 if (X509Name == NULL) {
1134 goto _Exit;
1135 }
1136
1137 X509NameSize = i2d_X509_NAME (X509Name, NULL);
1138 if (*CertIssuerSize < X509NameSize) {
1139 *CertIssuerSize = X509NameSize;
1140 goto _Exit;
1141 }
1142
1143 *CertIssuerSize = X509NameSize;
1144 if (CertIssuer != NULL) {
1145 i2d_X509_NAME (X509Name, &CertIssuer);
1146 Status = TRUE;
1147 }
1148
1149 _Exit:
1150 //
1151 // Release Resources.
1152 //
1153 if (X509Cert != NULL) {
1154 X509_free (X509Cert);
1155 }
1156
1157 return Status;
1158 }
1159
1160 /**
1161 Retrieve the Signature Algorithm from one X.509 certificate.
1162
1163 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1164 @param[in] CertSize Size of the X509 certificate in bytes.
1165 @param[out] Oid Signature Algorithm Object identifier buffer.
1166 @param[in,out] OidSize Signature Algorithm Object identifier buffer size
1167
1168 @retval TRUE The certificate Extension data retrieved successfully.
1169 @retval FALSE If Cert is NULL.
1170 If OidSize is NULL.
1171 If Oid is not NULL and *OidSize is 0.
1172 If Certificate is invalid.
1173 @retval FALSE If no SignatureType.
1174 @retval FALSE If the Oid is NULL. The required buffer size
1175 is returned in the OidSize.
1176 @retval FALSE The operation is not supported.
1177 **/
1178 BOOLEAN
1179 EFIAPI
1180 X509GetSignatureAlgorithm (
1181 IN CONST UINT8 *Cert,
1182 IN UINTN CertSize,
1183 OUT UINT8 *Oid, OPTIONAL
1184 IN OUT UINTN *OidSize
1185 )
1186 {
1187 BOOLEAN Status;
1188 X509 *X509Cert;
1189 int Nid;
1190 ASN1_OBJECT *Asn1Obj;
1191
1192 //
1193 // Check input parameters.
1194 //
1195 if ((Cert == NULL) || (OidSize == NULL) || (CertSize == 0)) {
1196 return FALSE;
1197 }
1198
1199 X509Cert = NULL;
1200 Status = FALSE;
1201
1202 //
1203 // Read DER-encoded X509 Certificate and Construct X509 object.
1204 //
1205 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1206 if ((X509Cert == NULL) || (!Status)) {
1207 Status = FALSE;
1208 goto _Exit;
1209 }
1210
1211 //
1212 // Retrieve subject name from certificate object.
1213 //
1214 Nid = X509_get_signature_nid (X509Cert);
1215 if (Nid == NID_undef) {
1216 *OidSize = 0;
1217 Status = FALSE;
1218 goto _Exit;
1219 }
1220
1221 Asn1Obj = OBJ_nid2obj (Nid);
1222 if (Asn1Obj == NULL) {
1223 *OidSize = 0;
1224 Status = FALSE;
1225 goto _Exit;
1226 }
1227
1228 if (*OidSize < (UINTN)Asn1Obj->length) {
1229 *OidSize = Asn1Obj->length;
1230 Status = FALSE;
1231 goto _Exit;
1232 }
1233
1234 if (Oid != NULL) {
1235 CopyMem (Oid, Asn1Obj->data, Asn1Obj->length);
1236 }
1237
1238 *OidSize = Asn1Obj->length;
1239 Status = TRUE;
1240
1241 _Exit:
1242 //
1243 // Release Resources.
1244 //
1245 if (X509Cert != NULL) {
1246 X509_free (X509Cert);
1247 }
1248
1249 return Status;
1250 }
1251
1252 /**
1253 Retrieve Extension data from one X.509 certificate.
1254
1255 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1256 @param[in] CertSize Size of the X509 certificate in bytes.
1257 @param[in] Oid Object identifier buffer
1258 @param[in] OidSize Object identifier buffer size
1259 @param[out] ExtensionData Extension bytes.
1260 @param[in, out] ExtensionDataSize Extension bytes size.
1261
1262 @retval TRUE The certificate Extension data retrieved successfully.
1263 @retval FALSE If Cert is NULL.
1264 If ExtensionDataSize is NULL.
1265 If ExtensionData is not NULL and *ExtensionDataSize is 0.
1266 If Certificate is invalid.
1267 @retval FALSE If no Extension entry match Oid.
1268 @retval FALSE If the ExtensionData is NULL. The required buffer size
1269 is returned in the ExtensionDataSize parameter.
1270 @retval FALSE The operation is not supported.
1271 **/
1272 BOOLEAN
1273 EFIAPI
1274 X509GetExtensionData (
1275 IN CONST UINT8 *Cert,
1276 IN UINTN CertSize,
1277 IN CONST UINT8 *Oid,
1278 IN UINTN OidSize,
1279 OUT UINT8 *ExtensionData,
1280 IN OUT UINTN *ExtensionDataSize
1281 )
1282 {
1283 BOOLEAN Status;
1284 INTN i;
1285 X509 *X509Cert;
1286
1287 CONST STACK_OF (X509_EXTENSION) *Extensions;
1288 ASN1_OBJECT *Asn1Obj;
1289 ASN1_OCTET_STRING *Asn1Oct;
1290 X509_EXTENSION *Ext;
1291 UINTN ObjLength;
1292 UINTN OctLength;
1293
1294 //
1295 // Check input parameters.
1296 //
1297 if ((Cert == NULL) || (CertSize == 0) || (Oid == NULL) || (OidSize == 0) || (ExtensionDataSize == NULL)) {
1298 return FALSE;
1299 }
1300
1301 X509Cert = NULL;
1302 Status = FALSE;
1303
1304 //
1305 // Read DER-encoded X509 Certificate and Construct X509 object.
1306 //
1307 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1308 if ((X509Cert == NULL) || (!Status)) {
1309 *ExtensionDataSize = 0;
1310 goto Cleanup;
1311 }
1312
1313 //
1314 // Retrieve Extensions from certificate object.
1315 //
1316 Extensions = X509_get0_extensions (X509Cert);
1317 if (sk_X509_EXTENSION_num (Extensions) <= 0) {
1318 *ExtensionDataSize = 0;
1319 goto Cleanup;
1320 }
1321
1322 //
1323 // Traverse Extensions
1324 //
1325 Status = FALSE;
1326 Asn1Oct = NULL;
1327 OctLength = 0;
1328 for (i = 0; i < sk_X509_EXTENSION_num (Extensions); i++) {
1329 Ext = sk_X509_EXTENSION_value (Extensions, (int)i);
1330 if (Ext == NULL) {
1331 continue;
1332 }
1333
1334 Asn1Obj = X509_EXTENSION_get_object (Ext);
1335 if (Asn1Obj == NULL) {
1336 continue;
1337 }
1338
1339 Asn1Oct = X509_EXTENSION_get_data (Ext);
1340 if (Asn1Oct == NULL) {
1341 continue;
1342 }
1343
1344 ObjLength = OBJ_length (Asn1Obj);
1345 OctLength = ASN1_STRING_length (Asn1Oct);
1346 if ((OidSize == ObjLength) && (CompareMem (OBJ_get0_data (Asn1Obj), Oid, OidSize) == 0)) {
1347 //
1348 // Extension Found
1349 //
1350 Status = TRUE;
1351 break;
1352 }
1353
1354 //
1355 // reset to 0 if not found
1356 //
1357 OctLength = 0;
1358 }
1359
1360 if (Status) {
1361 if (*ExtensionDataSize < OctLength) {
1362 *ExtensionDataSize = OctLength;
1363 Status = FALSE;
1364 goto Cleanup;
1365 }
1366
1367 if (Asn1Oct != NULL) {
1368 CopyMem (ExtensionData, ASN1_STRING_get0_data (Asn1Oct), OctLength);
1369 }
1370
1371 *ExtensionDataSize = OctLength;
1372 } else {
1373 *ExtensionDataSize = 0;
1374 }
1375
1376 Cleanup:
1377 //
1378 // Release Resources.
1379 //
1380 if (X509Cert != NULL) {
1381 X509_free (X509Cert);
1382 }
1383
1384 return Status;
1385 }
1386
1387 /**
1388 Retrieve the Extended Key Usage from one X.509 certificate.
1389
1390 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1391 @param[in] CertSize Size of the X509 certificate in bytes.
1392 @param[out] Usage Key Usage bytes.
1393 @param[in, out] UsageSize Key Usage buffer sizs in bytes.
1394
1395 @retval TRUE The Usage bytes retrieve successfully.
1396 @retval FALSE If Cert is NULL.
1397 If CertSize is NULL.
1398 If Usage is not NULL and *UsageSize is 0.
1399 If Cert is invalid.
1400 @retval FALSE If the Usage is NULL. The required buffer size
1401 is returned in the UsageSize parameter.
1402 @retval FALSE The operation is not supported.
1403 **/
1404 BOOLEAN
1405 EFIAPI
1406 X509GetExtendedKeyUsage (
1407 IN CONST UINT8 *Cert,
1408 IN UINTN CertSize,
1409 OUT UINT8 *Usage,
1410 IN OUT UINTN *UsageSize
1411 )
1412 {
1413 BOOLEAN Status;
1414
1415 Status = X509GetExtensionData (Cert, CertSize, mOidExtKeyUsage, sizeof (mOidExtKeyUsage), Usage, UsageSize);
1416 return Status;
1417 }
1418
1419 /**
1420 Retrieve the Validity from one X.509 certificate
1421
1422 If Cert is NULL, then return FALSE.
1423 If CertIssuerSize is NULL, then return FALSE.
1424 If this interface is not supported, then return FALSE.
1425
1426 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1427 @param[in] CertSize Size of the X509 certificate in bytes.
1428 @param[out] From notBefore Pointer to DateTime object.
1429 @param[in,out] FromSize notBefore DateTime object size.
1430 @param[out] To notAfter Pointer to DateTime object.
1431 @param[in,out] ToSize notAfter DateTime object size.
1432
1433 Note: X509CompareDateTime to compare DateTime oject
1434 x509SetDateTime to get a DateTime object from a DateTimeStr
1435
1436 @retval TRUE The certificate Validity retrieved successfully.
1437 @retval FALSE Invalid certificate, or Validity retrieve failed.
1438 @retval FALSE This interface is not supported.
1439 **/
1440 BOOLEAN
1441 EFIAPI
1442 X509GetValidity (
1443 IN CONST UINT8 *Cert,
1444 IN UINTN CertSize,
1445 IN UINT8 *From,
1446 IN OUT UINTN *FromSize,
1447 IN UINT8 *To,
1448 IN OUT UINTN *ToSize
1449 )
1450 {
1451 BOOLEAN Status;
1452 X509 *X509Cert;
1453 CONST ASN1_TIME *F;
1454 CONST ASN1_TIME *T;
1455 UINTN TSize;
1456 UINTN FSize;
1457
1458 //
1459 // Check input parameters.
1460 //
1461 if ((Cert == NULL) || (FromSize == NULL) || (ToSize == NULL) || (CertSize == 0)) {
1462 return FALSE;
1463 }
1464
1465 X509Cert = NULL;
1466 Status = FALSE;
1467
1468 //
1469 // Read DER-encoded X509 Certificate and Construct X509 object.
1470 //
1471 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1472 if ((X509Cert == NULL) || (!Status)) {
1473 goto _Exit;
1474 }
1475
1476 //
1477 // Retrieve Validity from/to from certificate object.
1478 //
1479 F = X509_get0_notBefore (X509Cert);
1480 T = X509_get0_notAfter (X509Cert);
1481
1482 if ((F == NULL) || (T == NULL)) {
1483 goto _Exit;
1484 }
1485
1486 FSize = sizeof (ASN1_TIME) + F->length;
1487 if (*FromSize < FSize) {
1488 *FromSize = FSize;
1489 goto _Exit;
1490 }
1491
1492 *FromSize = FSize;
1493 if (From != NULL) {
1494 CopyMem (From, F, sizeof (ASN1_TIME));
1495 ((ASN1_TIME *)From)->data = From + sizeof (ASN1_TIME);
1496 CopyMem (From + sizeof (ASN1_TIME), F->data, F->length);
1497 }
1498
1499 TSize = sizeof (ASN1_TIME) + T->length;
1500 if (*ToSize < TSize) {
1501 *ToSize = TSize;
1502 goto _Exit;
1503 }
1504
1505 *ToSize = TSize;
1506 if (To != NULL) {
1507 CopyMem (To, T, sizeof (ASN1_TIME));
1508 ((ASN1_TIME *)To)->data = To + sizeof (ASN1_TIME);
1509 CopyMem (To + sizeof (ASN1_TIME), T->data, T->length);
1510 }
1511
1512 Status = TRUE;
1513
1514 _Exit:
1515 //
1516 // Release Resources.
1517 //
1518 if (X509Cert != NULL) {
1519 X509_free (X509Cert);
1520 }
1521
1522 return Status;
1523 }
1524
1525 /**
1526 Format a DateTimeStr to DataTime object in DataTime Buffer
1527
1528 If DateTimeStr is NULL, then return FALSE.
1529 If DateTimeSize is NULL, then return FALSE.
1530 If this interface is not supported, then return FALSE.
1531
1532 @param[in] DateTimeStr DateTime string like YYYYMMDDhhmmssZ
1533 Ref: https://www.w3.org/TR/NOTE-datetime
1534 Z stand for UTC time
1535 @param[out] DateTime Pointer to a DateTime object.
1536 @param[in,out] DateTimeSize DateTime object buffer size.
1537
1538 @retval TRUE The DateTime object create successfully.
1539 @retval FALSE If DateTimeStr is NULL.
1540 If DateTimeSize is NULL.
1541 If DateTime is not NULL and *DateTimeSize is 0.
1542 If Year Month Day Hour Minute Second combination is invalid datetime.
1543 @retval FALSE If the DateTime is NULL. The required buffer size
1544 (including the final null) is returned in the
1545 DateTimeSize parameter.
1546 @retval FALSE The operation is not supported.
1547 **/
1548 BOOLEAN
1549 EFIAPI
1550 X509FormatDateTime (
1551 IN CONST CHAR8 *DateTimeStr,
1552 OUT VOID *DateTime,
1553 IN OUT UINTN *DateTimeSize
1554 )
1555 {
1556 BOOLEAN Status;
1557 INT32 Ret;
1558 ASN1_TIME *Dt;
1559 UINTN DSize;
1560
1561 Dt = NULL;
1562 Status = FALSE;
1563
1564 Dt = ASN1_TIME_new ();
1565 if (Dt == NULL) {
1566 Status = FALSE;
1567 goto Cleanup;
1568 }
1569
1570 Ret = ASN1_TIME_set_string_X509 (Dt, DateTimeStr);
1571 if (Ret != 1) {
1572 Status = FALSE;
1573 goto Cleanup;
1574 }
1575
1576 DSize = sizeof (ASN1_TIME) + Dt->length;
1577 if (*DateTimeSize < DSize) {
1578 *DateTimeSize = DSize;
1579 Status = FALSE;
1580 goto Cleanup;
1581 }
1582
1583 *DateTimeSize = DSize;
1584 if (DateTime != NULL) {
1585 CopyMem (DateTime, Dt, sizeof (ASN1_TIME));
1586 ((ASN1_TIME *)DateTime)->data = (UINT8 *)DateTime + sizeof (ASN1_TIME);
1587 CopyMem ((UINT8 *)DateTime + sizeof (ASN1_TIME), Dt->data, Dt->length);
1588 }
1589
1590 Status = TRUE;
1591
1592 Cleanup:
1593 if (Dt != NULL) {
1594 ASN1_TIME_free (Dt);
1595 }
1596
1597 return Status;
1598 }
1599
1600 /**
1601 Compare DateTime1 object and DateTime2 object.
1602
1603 If DateTime1 is NULL, then return -2.
1604 If DateTime2 is NULL, then return -2.
1605 If DateTime1 == DateTime2, then return 0
1606 If DateTime1 > DateTime2, then return 1
1607 If DateTime1 < DateTime2, then return -1
1608
1609 @param[in] DateTime1 Pointer to a DateTime Ojbect
1610 @param[in] DateTime2 Pointer to a DateTime Object
1611
1612 @retval 0 If DateTime1 == DateTime2
1613 @retval 1 If DateTime1 > DateTime2
1614 @retval -1 If DateTime1 < DateTime2
1615 **/
1616 INT32
1617 EFIAPI
1618 X509CompareDateTime (
1619 IN CONST VOID *DateTime1,
1620 IN CONST VOID *DateTime2
1621 )
1622 {
1623 return (INT32)ASN1_TIME_compare (DateTime1, DateTime2);
1624 }
1625
1626 /**
1627 Retrieve the Key Usage from one X.509 certificate.
1628
1629 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1630 @param[in] CertSize Size of the X509 certificate in bytes.
1631 @param[out] Usage Key Usage (CRYPTO_X509_KU_*)
1632
1633 @retval TRUE The certificate Key Usage retrieved successfully.
1634 @retval FALSE Invalid certificate, or Usage is NULL
1635 @retval FALSE This interface is not supported.
1636 **/
1637 BOOLEAN
1638 EFIAPI
1639 X509GetKeyUsage (
1640 IN CONST UINT8 *Cert,
1641 IN UINTN CertSize,
1642 OUT UINTN *Usage
1643 )
1644 {
1645 BOOLEAN Status;
1646 X509 *X509Cert;
1647
1648 //
1649 // Check input parameters.
1650 //
1651 if ((Cert == NULL) || (Usage == NULL)) {
1652 return FALSE;
1653 }
1654
1655 X509Cert = NULL;
1656 Status = FALSE;
1657
1658 //
1659 // Read DER-encoded X509 Certificate and Construct X509 object.
1660 //
1661 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **)&X509Cert);
1662 if ((X509Cert == NULL) || (!Status)) {
1663 goto _Exit;
1664 }
1665
1666 //
1667 // Retrieve subject name from certificate object.
1668 //
1669 *Usage = X509_get_key_usage (X509Cert);
1670 if (*Usage == NID_undef) {
1671 goto _Exit;
1672 }
1673
1674 Status = TRUE;
1675
1676 _Exit:
1677 //
1678 // Release Resources.
1679 //
1680 if (X509Cert != NULL) {
1681 X509_free (X509Cert);
1682 }
1683
1684 return Status;
1685 }
1686
1687 /**
1688 Verify one X509 certificate was issued by the trusted CA.
1689 @param[in] RootCert Trusted Root Certificate buffer
1690
1691 @param[in] RootCertLength Trusted Root Certificate buffer length
1692 @param[in] CertChain One or more ASN.1 DER-encoded X.509 certificates
1693 where the first certificate is signed by the Root
1694 Certificate or is the Root Cerificate itself. and
1695 subsequent cerificate is signed by the preceding
1696 cerificate.
1697 @param[in] CertChainLength Total length of the certificate chain, in bytes.
1698
1699 @retval TRUE All cerificates was issued by the first certificate in X509Certchain.
1700 @retval FALSE Invalid certificate or the certificate was not issued by the given
1701 trusted CA.
1702 **/
1703 BOOLEAN
1704 EFIAPI
1705 X509VerifyCertChain (
1706 IN CONST UINT8 *RootCert,
1707 IN UINTN RootCertLength,
1708 IN CONST UINT8 *CertChain,
1709 IN UINTN CertChainLength
1710 )
1711 {
1712 CONST UINT8 *TmpPtr;
1713 UINTN Length;
1714 UINT32 Asn1Tag;
1715 UINT32 ObjClass;
1716 CONST UINT8 *CurrentCert;
1717 UINTN CurrentCertLen;
1718 CONST UINT8 *PrecedingCert;
1719 UINTN PrecedingCertLen;
1720 BOOLEAN VerifyFlag;
1721 INT32 Ret;
1722
1723 PrecedingCert = RootCert;
1724 PrecedingCertLen = RootCertLength;
1725
1726 CurrentCert = CertChain;
1727 Length = 0;
1728 CurrentCertLen = 0;
1729
1730 VerifyFlag = FALSE;
1731 while (TRUE) {
1732 TmpPtr = CurrentCert;
1733 Ret = ASN1_get_object (
1734 (CONST UINT8 **)&TmpPtr,
1735 (long *)&Length,
1736 (int *)&Asn1Tag,
1737 (int *)&ObjClass,
1738 (long)(CertChainLength + CertChain - TmpPtr)
1739 );
1740 if ((Asn1Tag != V_ASN1_SEQUENCE) || (Ret == 0x80)) {
1741 break;
1742 }
1743
1744 //
1745 // Calculate CurrentCert length;
1746 //
1747 CurrentCertLen = TmpPtr - CurrentCert + Length;
1748
1749 //
1750 // Verify CurrentCert with preceding cert;
1751 //
1752 VerifyFlag = X509VerifyCert (CurrentCert, CurrentCertLen, PrecedingCert, PrecedingCertLen);
1753 if (VerifyFlag == FALSE) {
1754 break;
1755 }
1756
1757 //
1758 // move Current cert to Preceding cert
1759 //
1760 PrecedingCertLen = CurrentCertLen;
1761 PrecedingCert = CurrentCert;
1762
1763 //
1764 // Move to next
1765 //
1766 CurrentCert = CurrentCert + CurrentCertLen;
1767 }
1768
1769 return VerifyFlag;
1770 }
1771
1772 /**
1773 Get one X509 certificate from CertChain.
1774
1775 @param[in] CertChain One or more ASN.1 DER-encoded X.509 certificates
1776 where the first certificate is signed by the Root
1777 Certificate or is the Root Cerificate itself. and
1778 subsequent cerificate is signed by the preceding
1779 cerificate.
1780 @param[in] CertChainLength Total length of the certificate chain, in bytes.
1781
1782 @param[in] CertIndex Index of certificate.
1783
1784 @param[out] Cert The certificate at the index of CertChain.
1785 @param[out] CertLength The length certificate at the index of CertChain.
1786
1787 @retval TRUE Success.
1788 @retval FALSE Failed to get certificate from certificate chain.
1789 **/
1790 BOOLEAN
1791 EFIAPI
1792 X509GetCertFromCertChain (
1793 IN CONST UINT8 *CertChain,
1794 IN UINTN CertChainLength,
1795 IN CONST INT32 CertIndex,
1796 OUT CONST UINT8 **Cert,
1797 OUT UINTN *CertLength
1798 )
1799 {
1800 UINTN Asn1Len;
1801 INT32 CurrentIndex;
1802 UINTN CurrentCertLen;
1803 CONST UINT8 *CurrentCert;
1804 CONST UINT8 *TmpPtr;
1805 INT32 Ret;
1806 UINT32 Asn1Tag;
1807 UINT32 ObjClass;
1808
1809 //
1810 // Check input parameters.
1811 //
1812 if ((CertChain == NULL) || (Cert == NULL) ||
1813 (CertIndex < -1) || (CertLength == NULL))
1814 {
1815 return FALSE;
1816 }
1817
1818 Asn1Len = 0;
1819 CurrentCertLen = 0;
1820 CurrentCert = CertChain;
1821 CurrentIndex = -1;
1822
1823 //
1824 // Traverse the certificate chain
1825 //
1826 while (TRUE) {
1827 TmpPtr = CurrentCert;
1828
1829 // Get asn1 object and taglen
1830 Ret = ASN1_get_object (
1831 (CONST UINT8 **)&TmpPtr,
1832 (long *)&Asn1Len,
1833 (int *)&Asn1Tag,
1834 (int *)&ObjClass,
1835 (long)(CertChainLength + CertChain - TmpPtr)
1836 );
1837 if ((Asn1Tag != V_ASN1_SEQUENCE) || (Ret == 0x80)) {
1838 break;
1839 }
1840
1841 //
1842 // Calculate CurrentCert length;
1843 //
1844 CurrentCertLen = TmpPtr - CurrentCert + Asn1Len;
1845 CurrentIndex++;
1846
1847 if (CurrentIndex == CertIndex) {
1848 *Cert = CurrentCert;
1849 *CertLength = CurrentCertLen;
1850 return TRUE;
1851 }
1852
1853 //
1854 // Move to next
1855 //
1856 CurrentCert = CurrentCert + CurrentCertLen;
1857 }
1858
1859 //
1860 // If CertIndex is -1, Return the last certificate
1861 //
1862 if ((CertIndex == -1) && (CurrentIndex >= 0)) {
1863 *Cert = CurrentCert - CurrentCertLen;
1864 *CertLength = CurrentCertLen;
1865 return TRUE;
1866 }
1867
1868 return FALSE;
1869 }
1870
1871 /**
1872 Retrieve the tag and length of the tag.
1873
1874 @param Ptr The position in the ASN.1 data
1875 @param End End of data
1876 @param Length The variable that will receive the length
1877 @param Tag The expected tag
1878
1879 @retval TRUE Get tag successful
1880 @retval FALSe Failed to get tag or tag not match
1881 **/
1882 BOOLEAN
1883 EFIAPI
1884 Asn1GetTag (
1885 IN OUT UINT8 **Ptr,
1886 IN CONST UINT8 *End,
1887 OUT UINTN *Length,
1888 IN UINT32 Tag
1889 )
1890 {
1891 UINT8 *PtrOld;
1892 INT32 ObjTag;
1893 INT32 ObjCls;
1894 long ObjLength;
1895
1896 //
1897 // Save Ptr position
1898 //
1899 PtrOld = *Ptr;
1900
1901 ASN1_get_object ((CONST UINT8 **)Ptr, &ObjLength, &ObjTag, &ObjCls, (INT32)(End - (*Ptr)));
1902 if ((ObjTag == (INT32)(Tag & CRYPTO_ASN1_TAG_VALUE_MASK)) &&
1903 (ObjCls == (INT32)(Tag & CRYPTO_ASN1_TAG_CLASS_MASK)))
1904 {
1905 *Length = (UINTN)ObjLength;
1906 return TRUE;
1907 } else {
1908 //
1909 // if doesn't match Tag, restore Ptr to origin Ptr
1910 //
1911 *Ptr = PtrOld;
1912 return FALSE;
1913 }
1914 }
1915
1916 /**
1917 Retrieve the basic constraints from one X.509 certificate.
1918
1919 @param[in] Cert Pointer to the DER-encoded X509 certificate.
1920 @param[in] CertSize size of the X509 certificate in bytes.
1921 @param[out] BasicConstraints basic constraints bytes.
1922 @param[in, out] BasicConstraintsSize basic constraints buffer sizs in bytes.
1923
1924 @retval TRUE The basic constraints retrieve successfully.
1925 @retval FALSE If cert is NULL.
1926 If cert_size is NULL.
1927 If basic_constraints is not NULL and *basic_constraints_size is 0.
1928 If cert is invalid.
1929 @retval FALSE The required buffer size is small.
1930 The return buffer size is basic_constraints_size parameter.
1931 @retval FALSE If no Extension entry match oid.
1932 @retval FALSE The operation is not supported.
1933 **/
1934 BOOLEAN
1935 EFIAPI
1936 X509GetExtendedBasicConstraints (
1937 CONST UINT8 *Cert,
1938 UINTN CertSize,
1939 UINT8 *BasicConstraints,
1940 UINTN *BasicConstraintsSize
1941 )
1942 {
1943 BOOLEAN Status;
1944
1945 if ((Cert == NULL) || (CertSize == 0) || (BasicConstraintsSize == NULL)) {
1946 return FALSE;
1947 }
1948
1949 Status = X509GetExtensionData (
1950 (UINT8 *)Cert,
1951 CertSize,
1952 mOidBasicConstraints,
1953 sizeof (mOidBasicConstraints),
1954 BasicConstraints,
1955 BasicConstraintsSize
1956 );
1957
1958 return Status;
1959 }