2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "InternalCryptLib.h"
10 #include <openssl/x509.h>
11 #include <openssl/rsa.h>
14 Construct a X509 object from DER-encoded certificate data.
16 If Cert is NULL, then return FALSE.
17 If SingleX509Cert is NULL, then return FALSE.
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.
23 @retval TRUE The X509 object generation succeeded.
24 @retval FALSE The operation failed.
29 X509ConstructCertificate (
32 OUT UINT8
**SingleX509Cert
39 // Check input parameters.
41 if ((Cert
== NULL
) || (SingleX509Cert
== NULL
) || (CertSize
> INT_MAX
)) {
46 // Read DER-encoded X509 Certificate and Construct X509 object.
49 X509Cert
= d2i_X509 (NULL
, &Temp
, (long)CertSize
);
50 if (X509Cert
== NULL
) {
54 *SingleX509Cert
= (UINT8
*)X509Cert
;
60 Construct a X509 stack object from a list of DER-encoded certificate data.
62 If X509Stack is NULL, then return FALSE.
63 If this interface is not supported, then return FALSE.
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().
73 @retval TRUE The X509 stack construction succeeded.
74 @retval FALSE The construction operation failed.
75 @retval FALSE This interface is not supported.
80 X509ConstructCertificateStackV (
81 IN OUT UINT8
**X509Stack
,
89 STACK_OF (X509
) *CertStack
;
94 // Check input parameters.
96 if (X509Stack
== NULL
) {
103 // Initialize X509 stack object.
105 CertStack
= (STACK_OF (X509
) *)(*X509Stack
);
106 if (CertStack
== NULL
) {
107 CertStack
= sk_X509_new_null ();
108 if (CertStack
== NULL
) {
113 for (Index
= 0; ; Index
++) {
115 // If Cert is NULL, then it is the end of the list.
117 Cert
= VA_ARG (Args
, UINT8
*);
122 CertSize
= VA_ARG (Args
, UINTN
);
128 // Construct X509 Object from the given DER-encoded certificate data.
131 Status
= X509ConstructCertificate (
137 if (X509Cert
!= NULL
) {
138 X509_free (X509Cert
);
145 // Insert the new X509 object into X509 stack object.
147 sk_X509_push (CertStack
, X509Cert
);
151 sk_X509_pop_free (CertStack
, X509_free
);
153 *X509Stack
= (UINT8
*)CertStack
;
160 Construct a X509 stack object from a list of DER-encoded certificate data.
162 If X509Stack is NULL, then return FALSE.
164 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
165 On output, pointer to the X509 stack object with new
166 inserted X509 certificate.
167 @param ... A list of DER-encoded single certificate data followed
168 by certificate size. A NULL terminates the list. The
169 pairs are the arguments to X509ConstructCertificate().
171 @retval TRUE The X509 stack construction succeeded.
172 @retval FALSE The construction operation failed.
177 X509ConstructCertificateStack (
178 IN OUT UINT8
**X509Stack
,
185 VA_START (Args
, X509Stack
);
186 Result
= X509ConstructCertificateStackV (X509Stack
, Args
);
192 Release the specified X509 object.
194 If X509Cert is NULL, then return FALSE.
196 @param[in] X509Cert Pointer to the X509 object to be released.
206 // Check input parameters.
208 if (X509Cert
== NULL
) {
213 // Free OpenSSL X509 object.
215 X509_free ((X509
*)X509Cert
);
219 Release the specified X509 stack object.
221 If X509Stack is NULL, then return FALSE.
223 @param[in] X509Stack Pointer to the X509 stack object to be released.
233 // Check input parameters.
235 if (X509Stack
== NULL
) {
240 // Free OpenSSL X509 stack object.
242 sk_X509_pop_free ((STACK_OF (X509
) *) X509Stack
, X509_free
);
246 Retrieve the subject bytes from one X.509 certificate.
248 @param[in] Cert Pointer to the DER-encoded X509 certificate.
249 @param[in] CertSize Size of the X509 certificate in bytes.
250 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
251 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
252 and the size of buffer returned CertSubject on output.
254 If Cert is NULL, then return FALSE.
255 If SubjectSize is NULL, then return FALSE.
257 @retval TRUE The certificate subject retrieved successfully.
258 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
259 The SubjectSize will be updated with the required size.
265 IN CONST UINT8
*Cert
,
267 OUT UINT8
*CertSubject
,
268 IN OUT UINTN
*SubjectSize
277 // Check input parameters.
279 if ((Cert
== NULL
) || (SubjectSize
== NULL
)) {
286 // Read DER-encoded X509 Certificate and Construct X509 object.
288 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**)&X509Cert
);
289 if ((X509Cert
== NULL
) || (!Status
)) {
297 // Retrieve subject name from certificate object.
299 X509Name
= X509_get_subject_name (X509Cert
);
300 if (X509Name
== NULL
) {
304 X509NameSize
= i2d_X509_NAME (X509Name
, NULL
);
305 if (*SubjectSize
< X509NameSize
) {
306 *SubjectSize
= X509NameSize
;
310 *SubjectSize
= X509NameSize
;
311 if (CertSubject
!= NULL
) {
312 i2d_X509_NAME (X509Name
, &CertSubject
);
318 // Release Resources.
320 if (X509Cert
!= NULL
) {
321 X509_free (X509Cert
);
328 Retrieve a string from one X.509 certificate base on the Request_NID.
330 @param[in] Cert Pointer to the DER-encoded X509 certificate.
331 @param[in] CertSize Size of the X509 certificate in bytes.
332 @param[in] Request_NID NID of string to obtain
333 @param[out] CommonName Buffer to contain the retrieved certificate common
334 name string (UTF8). At most CommonNameSize bytes will be
335 written and the string will be null terminated. May be
336 NULL in order to determine the size buffer needed.
337 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
338 and the size of buffer returned CommonName on output.
339 If CommonName is NULL then the amount of space needed
340 in buffer (including the final null) is returned.
342 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
343 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
344 If CommonNameSize is NULL.
345 If CommonName is not NULL and *CommonNameSize is 0.
346 If Certificate is invalid.
347 @retval RETURN_NOT_FOUND If no NID Name entry exists.
348 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
349 (including the final null) is returned in the
350 CommonNameSize parameter.
351 @retval RETURN_UNSUPPORTED The operation is not supported.
356 InternalX509GetNIDName (
357 IN CONST UINT8
*Cert
,
359 IN INT32 Request_NID
,
360 OUT CHAR8
*CommonName OPTIONAL
,
361 IN OUT UINTN
*CommonNameSize
364 RETURN_STATUS ReturnStatus
;
370 X509_NAME_ENTRY
*Entry
;
371 ASN1_STRING
*EntryData
;
374 ReturnStatus
= RETURN_INVALID_PARAMETER
;
378 // Check input parameters.
380 if ((Cert
== NULL
) || (CertSize
> INT_MAX
) || (CommonNameSize
== NULL
)) {
384 if ((CommonName
!= NULL
) && (*CommonNameSize
== 0)) {
390 // Read DER-encoded X509 Certificate and Construct X509 object.
392 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**)&X509Cert
);
393 if ((X509Cert
== NULL
) || (!Status
)) {
395 // Invalid X.509 Certificate
403 // Retrieve subject name from certificate object.
405 X509Name
= X509_get_subject_name (X509Cert
);
406 if (X509Name
== NULL
) {
408 // Fail to retrieve subject name content
414 // Retrive the string from X.509 Subject base on the Request_NID
416 Index
= X509_NAME_get_index_by_NID (X509Name
, Request_NID
, -1);
419 // No Request_NID name entry exists in X509_NAME object
422 ReturnStatus
= RETURN_NOT_FOUND
;
426 Entry
= X509_NAME_get_entry (X509Name
, Index
);
429 // Fail to retrieve name entry data
432 ReturnStatus
= RETURN_NOT_FOUND
;
436 EntryData
= X509_NAME_ENTRY_get_data (Entry
);
438 Length
= ASN1_STRING_to_UTF8 (&UTF8Name
, EntryData
);
441 // Fail to convert the Name string
444 ReturnStatus
= RETURN_INVALID_PARAMETER
;
448 if (CommonName
== NULL
) {
449 *CommonNameSize
= Length
+ 1;
450 ReturnStatus
= RETURN_BUFFER_TOO_SMALL
;
452 *CommonNameSize
= MIN ((UINTN
)Length
, *CommonNameSize
- 1) + 1;
453 CopyMem (CommonName
, UTF8Name
, *CommonNameSize
- 1);
454 CommonName
[*CommonNameSize
- 1] = '\0';
455 ReturnStatus
= RETURN_SUCCESS
;
460 // Release Resources.
462 if (X509Cert
!= NULL
) {
463 X509_free (X509Cert
);
466 if (UTF8Name
!= NULL
) {
467 OPENSSL_free (UTF8Name
);
474 Retrieve the common name (CN) string from one X.509 certificate.
476 @param[in] Cert Pointer to the DER-encoded X509 certificate.
477 @param[in] CertSize Size of the X509 certificate in bytes.
478 @param[out] CommonName Buffer to contain the retrieved certificate common
479 name string. At most CommonNameSize bytes will be
480 written and the string will be null terminated. May be
481 NULL in order to determine the size buffer needed.
482 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
483 and the size of buffer returned CommonName on output.
484 If CommonName is NULL then the amount of space needed
485 in buffer (including the final null) is returned.
487 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
488 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
489 If CommonNameSize is NULL.
490 If CommonName is not NULL and *CommonNameSize is 0.
491 If Certificate is invalid.
492 @retval RETURN_NOT_FOUND If no CommonName entry exists.
493 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
494 (including the final null) is returned in the
495 CommonNameSize parameter.
496 @retval RETURN_UNSUPPORTED The operation is not supported.
502 IN CONST UINT8
*Cert
,
504 OUT CHAR8
*CommonName OPTIONAL
,
505 IN OUT UINTN
*CommonNameSize
508 return InternalX509GetNIDName (Cert
, CertSize
, NID_commonName
, CommonName
, CommonNameSize
);
512 Retrieve the organization name (O) string from one X.509 certificate.
514 @param[in] Cert Pointer to the DER-encoded X509 certificate.
515 @param[in] CertSize Size of the X509 certificate in bytes.
516 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
517 name string. At most NameBufferSize bytes will be
518 written and the string will be null terminated. May be
519 NULL in order to determine the size buffer needed.
520 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
521 and the size of buffer returned Name on output.
522 If NameBuffer is NULL then the amount of space needed
523 in buffer (including the final null) is returned.
525 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
526 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
527 If NameBufferSize is NULL.
528 If NameBuffer is not NULL and *CommonNameSize is 0.
529 If Certificate is invalid.
530 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
531 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
532 (including the final null) is returned in the
533 CommonNameSize parameter.
534 @retval RETURN_UNSUPPORTED The operation is not supported.
539 X509GetOrganizationName (
540 IN CONST UINT8
*Cert
,
542 OUT CHAR8
*NameBuffer OPTIONAL
,
543 IN OUT UINTN
*NameBufferSize
546 return InternalX509GetNIDName (Cert
, CertSize
, NID_organizationName
, NameBuffer
, NameBufferSize
);
550 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
552 @param[in] Cert Pointer to the DER-encoded X509 certificate.
553 @param[in] CertSize Size of the X509 certificate in bytes.
554 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
555 RSA public key component. Use RsaFree() function to free the
558 If Cert is NULL, then return FALSE.
559 If RsaContext is NULL, then return FALSE.
561 @retval TRUE RSA Public Key was retrieved successfully.
562 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
567 RsaGetPublicKeyFromX509 (
568 IN CONST UINT8
*Cert
,
570 OUT VOID
**RsaContext
578 // Check input parameters.
580 if ((Cert
== NULL
) || (RsaContext
== NULL
)) {
588 // Read DER-encoded X509 Certificate and Construct X509 object.
590 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**)&X509Cert
);
591 if ((X509Cert
== NULL
) || (!Status
)) {
599 // Retrieve and check EVP_PKEY data from X509 Certificate.
601 Pkey
= X509_get_pubkey (X509Cert
);
602 if ((Pkey
== NULL
) || (EVP_PKEY_id (Pkey
) != EVP_PKEY_RSA
)) {
607 // Duplicate RSA Context from the retrieved EVP_PKEY.
609 if ((*RsaContext
= RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey
))) != NULL
) {
615 // Release Resources.
617 if (X509Cert
!= NULL
) {
618 X509_free (X509Cert
);
622 EVP_PKEY_free (Pkey
);
629 Verify one X509 certificate was issued by the trusted CA.
631 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
632 @param[in] CertSize Size of the X509 certificate in bytes.
633 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
634 @param[in] CACertSize Size of the CA Certificate in bytes.
636 If Cert is NULL, then return FALSE.
637 If CACert is NULL, then return FALSE.
639 @retval TRUE The certificate was issued by the trusted CA.
640 @retval FALSE Invalid certificate or the certificate was not issued by the given
647 IN CONST UINT8
*Cert
,
649 IN CONST UINT8
*CACert
,
656 X509_STORE
*CertStore
;
657 X509_STORE_CTX
*CertCtx
;
660 // Check input parameters.
662 if ((Cert
== NULL
) || (CACert
== NULL
)) {
673 // Register & Initialize necessary digest algorithms for certificate verification.
675 if (EVP_add_digest (EVP_md5 ()) == 0) {
679 if (EVP_add_digest (EVP_sha1 ()) == 0) {
683 if (EVP_add_digest (EVP_sha256 ()) == 0) {
688 // Read DER-encoded certificate to be verified and Construct X509 object.
690 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**)&X509Cert
);
691 if ((X509Cert
== NULL
) || (!Status
)) {
697 // Read DER-encoded root certificate and Construct X509 object.
699 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**)&X509CACert
);
700 if ((X509CACert
== NULL
) || (!Status
)) {
708 // Set up X509 Store for trusted certificate.
710 CertStore
= X509_STORE_new ();
711 if (CertStore
== NULL
) {
715 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
720 // Allow partial certificate chains, terminated by a non-self-signed but
721 // still trusted intermediate certificate. Also disable time checks.
723 X509_STORE_set_flags (
725 X509_V_FLAG_PARTIAL_CHAIN
| X509_V_FLAG_NO_CHECK_TIME
729 // Set up X509_STORE_CTX for the subsequent verification operation.
731 CertCtx
= X509_STORE_CTX_new ();
732 if (CertCtx
== NULL
) {
736 if (!X509_STORE_CTX_init (CertCtx
, CertStore
, X509Cert
, NULL
)) {
741 // X509 Certificate Verification.
743 Status
= (BOOLEAN
)X509_verify_cert (CertCtx
);
744 X509_STORE_CTX_cleanup (CertCtx
);
748 // Release Resources.
750 if (X509Cert
!= NULL
) {
751 X509_free (X509Cert
);
754 if (X509CACert
!= NULL
) {
755 X509_free (X509CACert
);
758 if (CertStore
!= NULL
) {
759 X509_STORE_free (CertStore
);
762 X509_STORE_CTX_free (CertCtx
);
768 Retrieve the TBSCertificate from one given X.509 certificate.
770 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
771 @param[in] CertSize Size of the X509 certificate in bytes.
772 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
773 @param[out] TBSCertSize Size of the TBS certificate in bytes.
775 If Cert is NULL, then return FALSE.
776 If TBSCert is NULL, then return FALSE.
777 If TBSCertSize is NULL, then return FALSE.
779 @retval TRUE The TBSCertificate was retrieved successfully.
780 @retval FALSE Invalid X.509 certificate.
786 IN CONST UINT8
*Cert
,
789 OUT UINTN
*TBSCertSize
798 // Check input parameters.
800 if ((Cert
== NULL
) || (TBSCert
== NULL
) ||
801 (TBSCertSize
== NULL
) || (CertSize
> INT_MAX
))
807 // An X.509 Certificate is: (defined in RFC3280)
808 // Certificate ::= SEQUENCE {
809 // tbsCertificate TBSCertificate,
810 // signatureAlgorithm AlgorithmIdentifier,
811 // signature BIT STRING }
815 // TBSCertificate ::= SEQUENCE {
816 // version [0] Version DEFAULT v1,
820 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
821 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
825 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
827 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
831 *TBSCert
= (UINT8
*)Temp
;
833 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
835 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
837 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
841 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);
847 Retrieve the EC Public Key from one DER-encoded X509 certificate.
849 @param[in] Cert Pointer to the DER-encoded X509 certificate.
850 @param[in] CertSize Size of the X509 certificate in bytes.
851 @param[out] EcContext Pointer to new-generated EC DSA context which contain the retrieved
852 EC public key component. Use EcFree() function to free the
855 If Cert is NULL, then return FALSE.
856 If EcContext is NULL, then return FALSE.
858 @retval TRUE EC Public Key was retrieved successfully.
859 @retval FALSE Fail to retrieve EC public key from X509 certificate.
864 EcGetPublicKeyFromX509 (
865 IN CONST UINT8
*Cert
,
870 #if FixedPcdGetBool (PcdOpensslEcEnabled)
876 // Check input parameters.
878 if ((Cert
== NULL
) || (EcContext
== NULL
)) {
886 // Read DER-encoded X509 Certificate and Construct X509 object.
888 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**)&X509Cert
);
889 if ((X509Cert
== NULL
) || (!Status
)) {
897 // Retrieve and check EVP_PKEY data from X509 Certificate.
899 Pkey
= X509_get_pubkey (X509Cert
);
900 if ((Pkey
== NULL
) || (EVP_PKEY_id (Pkey
) != EVP_PKEY_EC
)) {
905 // Duplicate EC Context from the retrieved EVP_PKEY.
907 if ((*EcContext
= EC_KEY_dup (EVP_PKEY_get0_EC_KEY (Pkey
))) != NULL
) {
913 // Release Resources.
915 if (X509Cert
!= NULL
) {
916 X509_free (X509Cert
);
920 EVP_PKEY_free (Pkey
);