]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
b1393a89c5abb8de7f219737c3c948bc5bc63645
[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/rsa.h>
12
13 /**
14 Construct a X509 object from DER-encoded certificate data.
15
16 If Cert is NULL, then return FALSE.
17 If SingleX509Cert is NULL, then return FALSE.
18
19 @param[in] Cert Pointer to the DER-encoded certificate data.
20 @param[in] CertSize The size of certificate data in bytes.
21 @param[out] SingleX509Cert The generated X509 object.
22
23 @retval TRUE The X509 object generation succeeded.
24 @retval FALSE The operation failed.
25
26 **/
27 BOOLEAN
28 EFIAPI
29 X509ConstructCertificate (
30 IN CONST UINT8 *Cert,
31 IN UINTN CertSize,
32 OUT UINT8 **SingleX509Cert
33 )
34 {
35 X509 *X509Cert;
36 CONST UINT8 *Temp;
37
38 //
39 // Check input parameters.
40 //
41 if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {
42 return FALSE;
43 }
44
45 //
46 // Read DER-encoded X509 Certificate and Construct X509 object.
47 //
48 Temp = Cert;
49 X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize);
50 if (X509Cert == NULL) {
51 return FALSE;
52 }
53
54 *SingleX509Cert = (UINT8 *) X509Cert;
55
56 return TRUE;
57 }
58
59 /**
60 Construct a X509 stack object from a list of DER-encoded certificate data.
61
62 If X509Stack is NULL, then return FALSE.
63 If this interface is not supported, then return FALSE.
64
65 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
66 On output, pointer to the X509 stack object with new
67 inserted X509 certificate.
68 @param[in] Args VA_LIST marker for the variable argument list.
69 A list of DER-encoded single certificate data followed
70 by certificate size. A NULL terminates the list. The
71 pairs are the arguments to X509ConstructCertificate().
72
73 @retval TRUE The X509 stack construction succeeded.
74 @retval FALSE The construction operation failed.
75 @retval FALSE This interface is not supported.
76
77 **/
78 BOOLEAN
79 EFIAPI
80 X509ConstructCertificateStackV (
81 IN OUT UINT8 **X509Stack,
82 IN VA_LIST Args
83 )
84 {
85 UINT8 *Cert;
86 UINTN CertSize;
87 X509 *X509Cert;
88 STACK_OF(X509) *CertStack;
89 BOOLEAN Status;
90 UINTN Index;
91
92 //
93 // Check input parameters.
94 //
95 if (X509Stack == NULL) {
96 return FALSE;
97 }
98
99 Status = FALSE;
100
101 //
102 // Initialize X509 stack object.
103 //
104 CertStack = (STACK_OF(X509) *) (*X509Stack);
105 if (CertStack == NULL) {
106 CertStack = sk_X509_new_null ();
107 if (CertStack == NULL) {
108 return Status;
109 }
110 }
111
112 for (Index = 0; ; Index++) {
113 //
114 // If Cert is NULL, then it is the end of the list.
115 //
116 Cert = VA_ARG (Args, UINT8 *);
117 if (Cert == NULL) {
118 break;
119 }
120
121 CertSize = VA_ARG (Args, UINTN);
122 if (CertSize == 0) {
123 break;
124 }
125
126 //
127 // Construct X509 Object from the given DER-encoded certificate data.
128 //
129 X509Cert = NULL;
130 Status = X509ConstructCertificate (
131 (CONST UINT8 *) Cert,
132 CertSize,
133 (UINT8 **) &X509Cert
134 );
135 if (!Status) {
136 if (X509Cert != NULL) {
137 X509_free (X509Cert);
138 }
139 break;
140 }
141
142 //
143 // Insert the new X509 object into X509 stack object.
144 //
145 sk_X509_push (CertStack, X509Cert);
146 }
147
148 if (!Status) {
149 sk_X509_pop_free (CertStack, X509_free);
150 } else {
151 *X509Stack = (UINT8 *) CertStack;
152 }
153
154 return Status;
155 }
156
157 /**
158 Construct a X509 stack object from a list of DER-encoded certificate data.
159
160 If X509Stack is NULL, then return FALSE.
161
162 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
163 On output, pointer to the X509 stack object with new
164 inserted X509 certificate.
165 @param ... A list of DER-encoded single certificate data followed
166 by certificate size. A NULL terminates the list. The
167 pairs are the arguments to X509ConstructCertificate().
168
169 @retval TRUE The X509 stack construction succeeded.
170 @retval FALSE The construction operation failed.
171
172 **/
173 BOOLEAN
174 EFIAPI
175 X509ConstructCertificateStack (
176 IN OUT UINT8 **X509Stack,
177 ...
178 )
179 {
180 VA_LIST Args;
181 BOOLEAN Result;
182
183 VA_START (Args, X509Stack);
184 Result = X509ConstructCertificateStackV (X509Stack, Args);
185 VA_END (Args);
186 return Result;
187 }
188
189 /**
190 Release the specified X509 object.
191
192 If X509Cert is NULL, then return FALSE.
193
194 @param[in] X509Cert Pointer to the X509 object to be released.
195
196 **/
197 VOID
198 EFIAPI
199 X509Free (
200 IN VOID *X509Cert
201 )
202 {
203 //
204 // Check input parameters.
205 //
206 if (X509Cert == NULL) {
207 return;
208 }
209
210 //
211 // Free OpenSSL X509 object.
212 //
213 X509_free ((X509 *) X509Cert);
214 }
215
216 /**
217 Release the specified X509 stack object.
218
219 If X509Stack is NULL, then return FALSE.
220
221 @param[in] X509Stack Pointer to the X509 stack object to be released.
222
223 **/
224 VOID
225 EFIAPI
226 X509StackFree (
227 IN VOID *X509Stack
228 )
229 {
230 //
231 // Check input parameters.
232 //
233 if (X509Stack == NULL) {
234 return;
235 }
236
237 //
238 // Free OpenSSL X509 stack object.
239 //
240 sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);
241 }
242
243 /**
244 Retrieve the subject bytes from one X.509 certificate.
245
246 @param[in] Cert Pointer to the DER-encoded X509 certificate.
247 @param[in] CertSize Size of the X509 certificate in bytes.
248 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
249 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
250 and the size of buffer returned CertSubject on output.
251
252 If Cert is NULL, then return FALSE.
253 If SubjectSize is NULL, then return FALSE.
254
255 @retval TRUE The certificate subject retrieved successfully.
256 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
257 The SubjectSize will be updated with the required size.
258
259 **/
260 BOOLEAN
261 EFIAPI
262 X509GetSubjectName (
263 IN CONST UINT8 *Cert,
264 IN UINTN CertSize,
265 OUT UINT8 *CertSubject,
266 IN OUT UINTN *SubjectSize
267 )
268 {
269 BOOLEAN Status;
270 X509 *X509Cert;
271 X509_NAME *X509Name;
272 UINTN X509NameSize;
273
274 //
275 // Check input parameters.
276 //
277 if (Cert == NULL || SubjectSize == NULL) {
278 return FALSE;
279 }
280
281 X509Cert = NULL;
282
283 //
284 // Read DER-encoded X509 Certificate and Construct X509 object.
285 //
286 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
287 if ((X509Cert == NULL) || (!Status)) {
288 Status = FALSE;
289 goto _Exit;
290 }
291
292 Status = FALSE;
293
294 //
295 // Retrieve subject name from certificate object.
296 //
297 X509Name = X509_get_subject_name (X509Cert);
298 if (X509Name == NULL) {
299 goto _Exit;
300 }
301
302 X509NameSize = i2d_X509_NAME(X509Name, NULL);
303 if (*SubjectSize < X509NameSize) {
304 *SubjectSize = X509NameSize;
305 goto _Exit;
306 }
307 *SubjectSize = X509NameSize;
308 if (CertSubject != NULL) {
309 i2d_X509_NAME(X509Name, &CertSubject);
310 Status = TRUE;
311 }
312
313 _Exit:
314 //
315 // Release Resources.
316 //
317 if (X509Cert != NULL) {
318 X509_free (X509Cert);
319 }
320
321 return Status;
322 }
323
324 /**
325 Retrieve a string from one X.509 certificate base on the Request_NID.
326
327 @param[in] Cert Pointer to the DER-encoded X509 certificate.
328 @param[in] CertSize Size of the X509 certificate in bytes.
329 @param[in] Request_NID NID of string to obtain
330 @param[out] CommonName Buffer to contain the retrieved certificate common
331 name string (UTF8). At most CommonNameSize bytes will be
332 written and the string will be null terminated. May be
333 NULL in order to determine the size buffer needed.
334 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
335 and the size of buffer returned CommonName on output.
336 If CommonName is NULL then the amount of space needed
337 in buffer (including the final null) is returned.
338
339 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
340 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
341 If CommonNameSize is NULL.
342 If CommonName is not NULL and *CommonNameSize is 0.
343 If Certificate is invalid.
344 @retval RETURN_NOT_FOUND If no NID Name entry exists.
345 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
346 (including the final null) is returned in the
347 CommonNameSize parameter.
348 @retval RETURN_UNSUPPORTED The operation is not supported.
349
350 **/
351 STATIC
352 RETURN_STATUS
353 InternalX509GetNIDName (
354 IN CONST UINT8 *Cert,
355 IN UINTN CertSize,
356 IN INT32 Request_NID,
357 OUT CHAR8 *CommonName, OPTIONAL
358 IN OUT UINTN *CommonNameSize
359 )
360 {
361 RETURN_STATUS ReturnStatus;
362 BOOLEAN Status;
363 X509 *X509Cert;
364 X509_NAME *X509Name;
365 INT32 Index;
366 INTN Length;
367 X509_NAME_ENTRY *Entry;
368 ASN1_STRING *EntryData;
369 UINT8 *UTF8Name;
370
371 ReturnStatus = RETURN_INVALID_PARAMETER;
372 UTF8Name = NULL;
373
374 //
375 // Check input parameters.
376 //
377 if ((Cert == NULL) || (CertSize > INT_MAX) || (CommonNameSize == NULL)) {
378 return ReturnStatus;
379 }
380 if ((CommonName != NULL) && (*CommonNameSize == 0)) {
381 return ReturnStatus;
382 }
383
384 X509Cert = NULL;
385 //
386 // Read DER-encoded X509 Certificate and Construct X509 object.
387 //
388 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
389 if ((X509Cert == NULL) || (!Status)) {
390 //
391 // Invalid X.509 Certificate
392 //
393 goto _Exit;
394 }
395
396 Status = FALSE;
397
398 //
399 // Retrieve subject name from certificate object.
400 //
401 X509Name = X509_get_subject_name (X509Cert);
402 if (X509Name == NULL) {
403 //
404 // Fail to retrieve subject name content
405 //
406 goto _Exit;
407 }
408
409 //
410 // Retrive the string from X.509 Subject base on the Request_NID
411 //
412 Index = X509_NAME_get_index_by_NID (X509Name, Request_NID, -1);
413 if (Index < 0) {
414 //
415 // No Request_NID name entry exists in X509_NAME object
416 //
417 *CommonNameSize = 0;
418 ReturnStatus = RETURN_NOT_FOUND;
419 goto _Exit;
420 }
421
422 Entry = X509_NAME_get_entry (X509Name, Index);
423 if (Entry == NULL) {
424 //
425 // Fail to retrieve name entry data
426 //
427 *CommonNameSize = 0;
428 ReturnStatus = RETURN_NOT_FOUND;
429 goto _Exit;
430 }
431
432 EntryData = X509_NAME_ENTRY_get_data (Entry);
433
434 Length = ASN1_STRING_to_UTF8 (&UTF8Name, EntryData);
435 if (Length < 0) {
436 //
437 // Fail to convert the Name string
438 //
439 *CommonNameSize = 0;
440 ReturnStatus = RETURN_INVALID_PARAMETER;
441 goto _Exit;
442 }
443
444 if (CommonName == NULL) {
445 *CommonNameSize = Length + 1;
446 ReturnStatus = RETURN_BUFFER_TOO_SMALL;
447 } else {
448 *CommonNameSize = MIN ((UINTN)Length, *CommonNameSize - 1) + 1;
449 CopyMem (CommonName, UTF8Name, *CommonNameSize - 1);
450 CommonName[*CommonNameSize - 1] = '\0';
451 ReturnStatus = RETURN_SUCCESS;
452 }
453
454 _Exit:
455 //
456 // Release Resources.
457 //
458 if (X509Cert != NULL) {
459 X509_free (X509Cert);
460 }
461 if (UTF8Name != NULL) {
462 OPENSSL_free (UTF8Name);
463 }
464
465 return ReturnStatus;
466 }
467
468 /**
469 Retrieve the common name (CN) string from one X.509 certificate.
470
471 @param[in] Cert Pointer to the DER-encoded X509 certificate.
472 @param[in] CertSize Size of the X509 certificate in bytes.
473 @param[out] CommonName Buffer to contain the retrieved certificate common
474 name string. At most CommonNameSize bytes will be
475 written and the string will be null terminated. May be
476 NULL in order to determine the size buffer needed.
477 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
478 and the size of buffer returned CommonName on output.
479 If CommonName is NULL then the amount of space needed
480 in buffer (including the final null) is returned.
481
482 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
483 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
484 If CommonNameSize is NULL.
485 If CommonName is not NULL and *CommonNameSize is 0.
486 If Certificate is invalid.
487 @retval RETURN_NOT_FOUND If no CommonName entry exists.
488 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
489 (including the final null) is returned in the
490 CommonNameSize parameter.
491 @retval RETURN_UNSUPPORTED The operation is not supported.
492
493 **/
494 RETURN_STATUS
495 EFIAPI
496 X509GetCommonName (
497 IN CONST UINT8 *Cert,
498 IN UINTN CertSize,
499 OUT CHAR8 *CommonName, OPTIONAL
500 IN OUT UINTN *CommonNameSize
501 )
502 {
503 return InternalX509GetNIDName (Cert, CertSize, NID_commonName, CommonName, CommonNameSize);
504 }
505
506 /**
507 Retrieve the organization name (O) string from one X.509 certificate.
508
509 @param[in] Cert Pointer to the DER-encoded X509 certificate.
510 @param[in] CertSize Size of the X509 certificate in bytes.
511 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
512 name string. At most NameBufferSize bytes will be
513 written and the string will be null terminated. May be
514 NULL in order to determine the size buffer needed.
515 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
516 and the size of buffer returned Name on output.
517 If NameBuffer is NULL then the amount of space needed
518 in buffer (including the final null) is returned.
519
520 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
521 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
522 If NameBufferSize is NULL.
523 If NameBuffer is not NULL and *CommonNameSize is 0.
524 If Certificate is invalid.
525 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
526 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
527 (including the final null) is returned in the
528 CommonNameSize parameter.
529 @retval RETURN_UNSUPPORTED The operation is not supported.
530
531 **/
532 RETURN_STATUS
533 EFIAPI
534 X509GetOrganizationName (
535 IN CONST UINT8 *Cert,
536 IN UINTN CertSize,
537 OUT CHAR8 *NameBuffer, OPTIONAL
538 IN OUT UINTN *NameBufferSize
539 )
540 {
541 return InternalX509GetNIDName (Cert, CertSize, NID_organizationName, NameBuffer, NameBufferSize);
542 }
543
544 /**
545 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
546
547 @param[in] Cert Pointer to the DER-encoded X509 certificate.
548 @param[in] CertSize Size of the X509 certificate in bytes.
549 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
550 RSA public key component. Use RsaFree() function to free the
551 resource.
552
553 If Cert is NULL, then return FALSE.
554 If RsaContext is NULL, then return FALSE.
555
556 @retval TRUE RSA Public Key was retrieved successfully.
557 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
558
559 **/
560 BOOLEAN
561 EFIAPI
562 RsaGetPublicKeyFromX509 (
563 IN CONST UINT8 *Cert,
564 IN UINTN CertSize,
565 OUT VOID **RsaContext
566 )
567 {
568 BOOLEAN Status;
569 EVP_PKEY *Pkey;
570 X509 *X509Cert;
571
572 //
573 // Check input parameters.
574 //
575 if (Cert == NULL || RsaContext == NULL) {
576 return FALSE;
577 }
578
579 Pkey = NULL;
580 X509Cert = NULL;
581
582 //
583 // Read DER-encoded X509 Certificate and Construct X509 object.
584 //
585 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
586 if ((X509Cert == NULL) || (!Status)) {
587 Status = FALSE;
588 goto _Exit;
589 }
590
591 Status = FALSE;
592
593 //
594 // Retrieve and check EVP_PKEY data from X509 Certificate.
595 //
596 Pkey = X509_get_pubkey (X509Cert);
597 if ((Pkey == NULL) || (EVP_PKEY_id (Pkey) != EVP_PKEY_RSA)) {
598 goto _Exit;
599 }
600
601 //
602 // Duplicate RSA Context from the retrieved EVP_PKEY.
603 //
604 if ((*RsaContext = RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey))) != NULL) {
605 Status = TRUE;
606 }
607
608 _Exit:
609 //
610 // Release Resources.
611 //
612 if (X509Cert != NULL) {
613 X509_free (X509Cert);
614 }
615
616 if (Pkey != NULL) {
617 EVP_PKEY_free (Pkey);
618 }
619
620 return Status;
621 }
622
623 /**
624 Verify one X509 certificate was issued by the trusted CA.
625
626 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
627 @param[in] CertSize Size of the X509 certificate in bytes.
628 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
629 @param[in] CACertSize Size of the CA Certificate in bytes.
630
631 If Cert is NULL, then return FALSE.
632 If CACert is NULL, then return FALSE.
633
634 @retval TRUE The certificate was issued by the trusted CA.
635 @retval FALSE Invalid certificate or the certificate was not issued by the given
636 trusted CA.
637
638 **/
639 BOOLEAN
640 EFIAPI
641 X509VerifyCert (
642 IN CONST UINT8 *Cert,
643 IN UINTN CertSize,
644 IN CONST UINT8 *CACert,
645 IN UINTN CACertSize
646 )
647 {
648 BOOLEAN Status;
649 X509 *X509Cert;
650 X509 *X509CACert;
651 X509_STORE *CertStore;
652 X509_STORE_CTX *CertCtx;
653
654 //
655 // Check input parameters.
656 //
657 if (Cert == NULL || CACert == NULL) {
658 return FALSE;
659 }
660
661 Status = FALSE;
662 X509Cert = NULL;
663 X509CACert = NULL;
664 CertStore = NULL;
665 CertCtx = NULL;
666
667 //
668 // Register & Initialize necessary digest algorithms for certificate verification.
669 //
670 if (EVP_add_digest (EVP_md5 ()) == 0) {
671 goto _Exit;
672 }
673 if (EVP_add_digest (EVP_sha1 ()) == 0) {
674 goto _Exit;
675 }
676 if (EVP_add_digest (EVP_sha256 ()) == 0) {
677 goto _Exit;
678 }
679
680 //
681 // Read DER-encoded certificate to be verified and Construct X509 object.
682 //
683 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
684 if ((X509Cert == NULL) || (!Status)) {
685 Status = FALSE;
686 goto _Exit;
687 }
688
689 //
690 // Read DER-encoded root certificate and Construct X509 object.
691 //
692 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);
693 if ((X509CACert == NULL) || (!Status)) {
694 Status = FALSE;
695 goto _Exit;
696 }
697
698 Status = FALSE;
699
700 //
701 // Set up X509 Store for trusted certificate.
702 //
703 CertStore = X509_STORE_new ();
704 if (CertStore == NULL) {
705 goto _Exit;
706 }
707 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {
708 goto _Exit;
709 }
710
711 //
712 // Allow partial certificate chains, terminated by a non-self-signed but
713 // still trusted intermediate certificate. Also disable time checks.
714 //
715 X509_STORE_set_flags (CertStore,
716 X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);
717
718 //
719 // Set up X509_STORE_CTX for the subsequent verification operation.
720 //
721 CertCtx = X509_STORE_CTX_new ();
722 if (CertCtx == NULL) {
723 goto _Exit;
724 }
725 if (!X509_STORE_CTX_init (CertCtx, CertStore, X509Cert, NULL)) {
726 goto _Exit;
727 }
728
729 //
730 // X509 Certificate Verification.
731 //
732 Status = (BOOLEAN) X509_verify_cert (CertCtx);
733 X509_STORE_CTX_cleanup (CertCtx);
734
735 _Exit:
736 //
737 // Release Resources.
738 //
739 if (X509Cert != NULL) {
740 X509_free (X509Cert);
741 }
742
743 if (X509CACert != NULL) {
744 X509_free (X509CACert);
745 }
746
747 if (CertStore != NULL) {
748 X509_STORE_free (CertStore);
749 }
750
751 X509_STORE_CTX_free (CertCtx);
752
753 return Status;
754 }
755
756 /**
757 Retrieve the TBSCertificate from one given X.509 certificate.
758
759 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
760 @param[in] CertSize Size of the X509 certificate in bytes.
761 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
762 @param[out] TBSCertSize Size of the TBS certificate in bytes.
763
764 If Cert is NULL, then return FALSE.
765 If TBSCert is NULL, then return FALSE.
766 If TBSCertSize is NULL, then return FALSE.
767
768 @retval TRUE The TBSCertificate was retrieved successfully.
769 @retval FALSE Invalid X.509 certificate.
770
771 **/
772 BOOLEAN
773 EFIAPI
774 X509GetTBSCert (
775 IN CONST UINT8 *Cert,
776 IN UINTN CertSize,
777 OUT UINT8 **TBSCert,
778 OUT UINTN *TBSCertSize
779 )
780 {
781 CONST UINT8 *Temp;
782 UINT32 Asn1Tag;
783 UINT32 ObjClass;
784 UINTN Length;
785
786 //
787 // Check input parameters.
788 //
789 if ((Cert == NULL) || (TBSCert == NULL) ||
790 (TBSCertSize == NULL) || (CertSize > INT_MAX)) {
791 return FALSE;
792 }
793
794 //
795 // An X.509 Certificate is: (defined in RFC3280)
796 // Certificate ::= SEQUENCE {
797 // tbsCertificate TBSCertificate,
798 // signatureAlgorithm AlgorithmIdentifier,
799 // signature BIT STRING }
800 //
801 // and
802 //
803 // TBSCertificate ::= SEQUENCE {
804 // version [0] Version DEFAULT v1,
805 // ...
806 // }
807 //
808 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
809 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
810 //
811 Temp = Cert;
812 Length = 0;
813 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)CertSize);
814
815 if (Asn1Tag != V_ASN1_SEQUENCE) {
816 return FALSE;
817 }
818
819 *TBSCert = (UINT8 *)Temp;
820
821 ASN1_get_object (&Temp, (long *)&Length, (int *)&Asn1Tag, (int *)&ObjClass, (long)Length);
822 //
823 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
824 //
825 if (Asn1Tag != V_ASN1_SEQUENCE) {
826 return FALSE;
827 }
828
829 *TBSCertSize = Length + (Temp - *TBSCert);
830
831 return TRUE;
832 }