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
,
88 STACK_OF(X509
) *CertStack
;
93 // Check input parameters.
95 if (X509Stack
== NULL
) {
102 // Initialize X509 stack object.
104 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
105 if (CertStack
== NULL
) {
106 CertStack
= sk_X509_new_null ();
107 if (CertStack
== NULL
) {
112 for (Index
= 0; ; Index
++) {
114 // If Cert is NULL, then it is the end of the list.
116 Cert
= VA_ARG (Args
, UINT8
*);
121 CertSize
= VA_ARG (Args
, UINTN
);
127 // Construct X509 Object from the given DER-encoded certificate data.
130 Status
= X509ConstructCertificate (
131 (CONST UINT8
*) Cert
,
136 if (X509Cert
!= NULL
) {
137 X509_free (X509Cert
);
143 // Insert the new X509 object into X509 stack object.
145 sk_X509_push (CertStack
, X509Cert
);
149 sk_X509_pop_free (CertStack
, X509_free
);
151 *X509Stack
= (UINT8
*) CertStack
;
158 Construct a X509 stack object from a list of DER-encoded certificate data.
160 If X509Stack is NULL, then return FALSE.
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().
169 @retval TRUE The X509 stack construction succeeded.
170 @retval FALSE The construction operation failed.
175 X509ConstructCertificateStack (
176 IN OUT UINT8
**X509Stack
,
183 VA_START (Args
, X509Stack
);
184 Result
= X509ConstructCertificateStackV (X509Stack
, Args
);
190 Release the specified X509 object.
192 If X509Cert is NULL, then return FALSE.
194 @param[in] X509Cert Pointer to the X509 object to be released.
204 // Check input parameters.
206 if (X509Cert
== NULL
) {
211 // Free OpenSSL X509 object.
213 X509_free ((X509
*) X509Cert
);
217 Release the specified X509 stack object.
219 If X509Stack is NULL, then return FALSE.
221 @param[in] X509Stack Pointer to the X509 stack object to be released.
231 // Check input parameters.
233 if (X509Stack
== NULL
) {
238 // Free OpenSSL X509 stack object.
240 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
244 Retrieve the subject bytes from one X.509 certificate.
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.
252 If Cert is NULL, then return FALSE.
253 If SubjectSize is NULL, then return FALSE.
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.
263 IN CONST UINT8
*Cert
,
265 OUT UINT8
*CertSubject
,
266 IN OUT UINTN
*SubjectSize
275 // Check input parameters.
277 if (Cert
== NULL
|| SubjectSize
== NULL
) {
284 // Read DER-encoded X509 Certificate and Construct X509 object.
286 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
287 if ((X509Cert
== NULL
) || (!Status
)) {
295 // Retrieve subject name from certificate object.
297 X509Name
= X509_get_subject_name (X509Cert
);
298 if (X509Name
== NULL
) {
302 X509NameSize
= i2d_X509_NAME(X509Name
, NULL
);
303 if (*SubjectSize
< X509NameSize
) {
304 *SubjectSize
= X509NameSize
;
307 *SubjectSize
= X509NameSize
;
308 if (CertSubject
!= NULL
) {
309 i2d_X509_NAME(X509Name
, &CertSubject
);
315 // Release Resources.
317 if (X509Cert
!= NULL
) {
318 X509_free (X509Cert
);
325 Retrieve a string from one X.509 certificate base on the Request_NID.
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.
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.
353 InternalX509GetNIDName (
354 IN CONST UINT8
*Cert
,
356 IN INT32 Request_NID
,
357 OUT CHAR8
*CommonName OPTIONAL
,
358 IN OUT UINTN
*CommonNameSize
361 RETURN_STATUS ReturnStatus
;
367 X509_NAME_ENTRY
*Entry
;
368 ASN1_STRING
*EntryData
;
371 ReturnStatus
= RETURN_INVALID_PARAMETER
;
375 // Check input parameters.
377 if ((Cert
== NULL
) || (CertSize
> INT_MAX
) || (CommonNameSize
== NULL
)) {
380 if ((CommonName
!= NULL
) && (*CommonNameSize
== 0)) {
386 // Read DER-encoded X509 Certificate and Construct X509 object.
388 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
389 if ((X509Cert
== NULL
) || (!Status
)) {
391 // Invalid X.509 Certificate
399 // Retrieve subject name from certificate object.
401 X509Name
= X509_get_subject_name (X509Cert
);
402 if (X509Name
== NULL
) {
404 // Fail to retrieve subject name content
410 // Retrive the string from X.509 Subject base on the Request_NID
412 Index
= X509_NAME_get_index_by_NID (X509Name
, Request_NID
, -1);
415 // No Request_NID name entry exists in X509_NAME object
418 ReturnStatus
= RETURN_NOT_FOUND
;
422 Entry
= X509_NAME_get_entry (X509Name
, Index
);
425 // Fail to retrieve name entry data
428 ReturnStatus
= RETURN_NOT_FOUND
;
432 EntryData
= X509_NAME_ENTRY_get_data (Entry
);
434 Length
= ASN1_STRING_to_UTF8 (&UTF8Name
, EntryData
);
437 // Fail to convert the Name string
440 ReturnStatus
= RETURN_INVALID_PARAMETER
;
444 if (CommonName
== NULL
) {
445 *CommonNameSize
= Length
+ 1;
446 ReturnStatus
= RETURN_BUFFER_TOO_SMALL
;
448 *CommonNameSize
= MIN ((UINTN
)Length
, *CommonNameSize
- 1) + 1;
449 CopyMem (CommonName
, UTF8Name
, *CommonNameSize
- 1);
450 CommonName
[*CommonNameSize
- 1] = '\0';
451 ReturnStatus
= RETURN_SUCCESS
;
456 // Release Resources.
458 if (X509Cert
!= NULL
) {
459 X509_free (X509Cert
);
461 if (UTF8Name
!= NULL
) {
462 OPENSSL_free (UTF8Name
);
469 Retrieve the common name (CN) string from one X.509 certificate.
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.
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.
497 IN CONST UINT8
*Cert
,
499 OUT CHAR8
*CommonName OPTIONAL
,
500 IN OUT UINTN
*CommonNameSize
503 return InternalX509GetNIDName (Cert
, CertSize
, NID_commonName
, CommonName
, CommonNameSize
);
507 Retrieve the organization name (O) string from one X.509 certificate.
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.
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.
534 X509GetOrganizationName (
535 IN CONST UINT8
*Cert
,
537 OUT CHAR8
*NameBuffer OPTIONAL
,
538 IN OUT UINTN
*NameBufferSize
541 return InternalX509GetNIDName (Cert
, CertSize
, NID_organizationName
, NameBuffer
, NameBufferSize
);
545 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
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
553 If Cert is NULL, then return FALSE.
554 If RsaContext is NULL, then return FALSE.
556 @retval TRUE RSA Public Key was retrieved successfully.
557 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
562 RsaGetPublicKeyFromX509 (
563 IN CONST UINT8
*Cert
,
565 OUT VOID
**RsaContext
573 // Check input parameters.
575 if (Cert
== NULL
|| RsaContext
== NULL
) {
583 // Read DER-encoded X509 Certificate and Construct X509 object.
585 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
586 if ((X509Cert
== NULL
) || (!Status
)) {
594 // Retrieve and check EVP_PKEY data from X509 Certificate.
596 Pkey
= X509_get_pubkey (X509Cert
);
597 if ((Pkey
== NULL
) || (EVP_PKEY_id (Pkey
) != EVP_PKEY_RSA
)) {
602 // Duplicate RSA Context from the retrieved EVP_PKEY.
604 if ((*RsaContext
= RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey
))) != NULL
) {
610 // Release Resources.
612 if (X509Cert
!= NULL
) {
613 X509_free (X509Cert
);
617 EVP_PKEY_free (Pkey
);
624 Verify one X509 certificate was issued by the trusted CA.
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.
631 If Cert is NULL, then return FALSE.
632 If CACert is NULL, then return FALSE.
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
642 IN CONST UINT8
*Cert
,
644 IN CONST UINT8
*CACert
,
651 X509_STORE
*CertStore
;
652 X509_STORE_CTX
*CertCtx
;
655 // Check input parameters.
657 if (Cert
== NULL
|| CACert
== NULL
) {
668 // Register & Initialize necessary digest algorithms for certificate verification.
670 if (EVP_add_digest (EVP_md5 ()) == 0) {
673 if (EVP_add_digest (EVP_sha1 ()) == 0) {
676 if (EVP_add_digest (EVP_sha256 ()) == 0) {
681 // Read DER-encoded certificate to be verified and Construct X509 object.
683 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
684 if ((X509Cert
== NULL
) || (!Status
)) {
690 // Read DER-encoded root certificate and Construct X509 object.
692 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
693 if ((X509CACert
== NULL
) || (!Status
)) {
701 // Set up X509 Store for trusted certificate.
703 CertStore
= X509_STORE_new ();
704 if (CertStore
== NULL
) {
707 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
712 // Allow partial certificate chains, terminated by a non-self-signed but
713 // still trusted intermediate certificate. Also disable time checks.
715 X509_STORE_set_flags (CertStore
,
716 X509_V_FLAG_PARTIAL_CHAIN
| X509_V_FLAG_NO_CHECK_TIME
);
719 // Set up X509_STORE_CTX for the subsequent verification operation.
721 CertCtx
= X509_STORE_CTX_new ();
722 if (CertCtx
== NULL
) {
725 if (!X509_STORE_CTX_init (CertCtx
, CertStore
, X509Cert
, NULL
)) {
730 // X509 Certificate Verification.
732 Status
= (BOOLEAN
) X509_verify_cert (CertCtx
);
733 X509_STORE_CTX_cleanup (CertCtx
);
737 // Release Resources.
739 if (X509Cert
!= NULL
) {
740 X509_free (X509Cert
);
743 if (X509CACert
!= NULL
) {
744 X509_free (X509CACert
);
747 if (CertStore
!= NULL
) {
748 X509_STORE_free (CertStore
);
751 X509_STORE_CTX_free (CertCtx
);
757 Retrieve the TBSCertificate from one given X.509 certificate.
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.
764 If Cert is NULL, then return FALSE.
765 If TBSCert is NULL, then return FALSE.
766 If TBSCertSize is NULL, then return FALSE.
768 @retval TRUE The TBSCertificate was retrieved successfully.
769 @retval FALSE Invalid X.509 certificate.
775 IN CONST UINT8
*Cert
,
778 OUT UINTN
*TBSCertSize
787 // Check input parameters.
789 if ((Cert
== NULL
) || (TBSCert
== NULL
) ||
790 (TBSCertSize
== NULL
) || (CertSize
> INT_MAX
)) {
795 // An X.509 Certificate is: (defined in RFC3280)
796 // Certificate ::= SEQUENCE {
797 // tbsCertificate TBSCertificate,
798 // signatureAlgorithm AlgorithmIdentifier,
799 // signature BIT STRING }
803 // TBSCertificate ::= SEQUENCE {
804 // version [0] Version DEFAULT v1,
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.
813 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
815 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
819 *TBSCert
= (UINT8
*)Temp
;
821 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
823 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
825 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
829 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);