2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2018, 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.
64 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
65 On output, pointer to the X509 stack object with new
66 inserted X509 certificate.
67 @param ... A list of DER-encoded single certificate data followed
68 by certificate size. A NULL terminates the list. The
69 pairs are the arguments to X509ConstructCertificate().
71 @retval TRUE The X509 stack construction succeeded.
72 @retval FALSE The construction operation failed.
77 X509ConstructCertificateStack (
78 IN OUT UINT8
**X509Stack
,
85 STACK_OF(X509
) *CertStack
;
91 // Check input parameters.
93 if (X509Stack
== NULL
) {
100 // Initialize X509 stack object.
102 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
103 if (CertStack
== NULL
) {
104 CertStack
= sk_X509_new_null ();
105 if (CertStack
== NULL
) {
110 VA_START (Args
, X509Stack
);
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
);
151 sk_X509_pop_free (CertStack
, X509_free
);
153 *X509Stack
= (UINT8
*) CertStack
;
160 Release the specified X509 object.
162 If X509Cert is NULL, then return FALSE.
164 @param[in] X509Cert Pointer to the X509 object to be released.
174 // Check input parameters.
176 if (X509Cert
== NULL
) {
181 // Free OpenSSL X509 object.
183 X509_free ((X509
*) X509Cert
);
187 Release the specified X509 stack object.
189 If X509Stack is NULL, then return FALSE.
191 @param[in] X509Stack Pointer to the X509 stack object to be released.
201 // Check input parameters.
203 if (X509Stack
== NULL
) {
208 // Free OpenSSL X509 stack object.
210 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
214 Retrieve the subject bytes from one X.509 certificate.
216 @param[in] Cert Pointer to the DER-encoded X509 certificate.
217 @param[in] CertSize Size of the X509 certificate in bytes.
218 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
219 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
220 and the size of buffer returned CertSubject on output.
222 If Cert is NULL, then return FALSE.
223 If SubjectSize is NULL, then return FALSE.
225 @retval TRUE The certificate subject retrieved successfully.
226 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
227 The SubjectSize will be updated with the required size.
233 IN CONST UINT8
*Cert
,
235 OUT UINT8
*CertSubject
,
236 IN OUT UINTN
*SubjectSize
245 // Check input parameters.
247 if (Cert
== NULL
|| SubjectSize
== NULL
) {
254 // Read DER-encoded X509 Certificate and Construct X509 object.
256 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
257 if ((X509Cert
== NULL
) || (!Status
)) {
265 // Retrieve subject name from certificate object.
267 X509Name
= X509_get_subject_name (X509Cert
);
268 if (X509Name
== NULL
) {
272 X509NameSize
= i2d_X509_NAME(X509Name
, NULL
);
273 if (*SubjectSize
< X509NameSize
) {
274 *SubjectSize
= X509NameSize
;
277 *SubjectSize
= X509NameSize
;
278 if (CertSubject
!= NULL
) {
279 i2d_X509_NAME(X509Name
, &CertSubject
);
285 // Release Resources.
287 if (X509Cert
!= NULL
) {
288 X509_free (X509Cert
);
295 Retrieve a string from one X.509 certificate base on the Request_NID.
297 @param[in] Cert Pointer to the DER-encoded X509 certificate.
298 @param[in] CertSize Size of the X509 certificate in bytes.
299 @param[in] Request_NID NID of string to obtain
300 @param[out] CommonName Buffer to contain the retrieved certificate common
301 name string (UTF8). At most CommonNameSize bytes will be
302 written and the string will be null terminated. May be
303 NULL in order to determine the size buffer needed.
304 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
305 and the size of buffer returned CommonName on output.
306 If CommonName is NULL then the amount of space needed
307 in buffer (including the final null) is returned.
309 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
310 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
311 If CommonNameSize is NULL.
312 If CommonName is not NULL and *CommonNameSize is 0.
313 If Certificate is invalid.
314 @retval RETURN_NOT_FOUND If no NID Name entry exists.
315 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
316 (including the final null) is returned in the
317 CommonNameSize parameter.
318 @retval RETURN_UNSUPPORTED The operation is not supported.
323 InternalX509GetNIDName (
324 IN CONST UINT8
*Cert
,
326 IN INT32 Request_NID
,
327 OUT CHAR8
*CommonName
, OPTIONAL
328 IN OUT UINTN
*CommonNameSize
331 RETURN_STATUS ReturnStatus
;
337 X509_NAME_ENTRY
*Entry
;
338 ASN1_STRING
*EntryData
;
341 ReturnStatus
= RETURN_INVALID_PARAMETER
;
345 // Check input parameters.
347 if ((Cert
== NULL
) || (CertSize
> INT_MAX
) || (CommonNameSize
== NULL
)) {
350 if ((CommonName
!= NULL
) && (*CommonNameSize
== 0)) {
356 // Read DER-encoded X509 Certificate and Construct X509 object.
358 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
359 if ((X509Cert
== NULL
) || (!Status
)) {
361 // Invalid X.509 Certificate
369 // Retrieve subject name from certificate object.
371 X509Name
= X509_get_subject_name (X509Cert
);
372 if (X509Name
== NULL
) {
374 // Fail to retrieve subject name content
380 // Retrive the string from X.509 Subject base on the Request_NID
382 Index
= X509_NAME_get_index_by_NID (X509Name
, Request_NID
, -1);
385 // No Request_NID name entry exists in X509_NAME object
388 ReturnStatus
= RETURN_NOT_FOUND
;
392 Entry
= X509_NAME_get_entry (X509Name
, Index
);
395 // Fail to retrieve name entry data
398 ReturnStatus
= RETURN_NOT_FOUND
;
402 EntryData
= X509_NAME_ENTRY_get_data (Entry
);
404 Length
= ASN1_STRING_to_UTF8 (&UTF8Name
, EntryData
);
407 // Fail to convert the Name string
410 ReturnStatus
= RETURN_INVALID_PARAMETER
;
414 if (CommonName
== NULL
) {
415 *CommonNameSize
= Length
+ 1;
416 ReturnStatus
= RETURN_BUFFER_TOO_SMALL
;
418 *CommonNameSize
= MIN ((UINTN
)Length
, *CommonNameSize
- 1) + 1;
419 CopyMem (CommonName
, UTF8Name
, *CommonNameSize
- 1);
420 CommonName
[*CommonNameSize
- 1] = '\0';
421 ReturnStatus
= RETURN_SUCCESS
;
426 // Release Resources.
428 if (X509Cert
!= NULL
) {
429 X509_free (X509Cert
);
431 if (UTF8Name
!= NULL
) {
432 OPENSSL_free (UTF8Name
);
439 Retrieve the common name (CN) string from one X.509 certificate.
441 @param[in] Cert Pointer to the DER-encoded X509 certificate.
442 @param[in] CertSize Size of the X509 certificate in bytes.
443 @param[out] CommonName Buffer to contain the retrieved certificate common
444 name string. At most CommonNameSize bytes will be
445 written and the string will be null terminated. May be
446 NULL in order to determine the size buffer needed.
447 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
448 and the size of buffer returned CommonName on output.
449 If CommonName is NULL then the amount of space needed
450 in buffer (including the final null) is returned.
452 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
453 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
454 If CommonNameSize is NULL.
455 If CommonName is not NULL and *CommonNameSize is 0.
456 If Certificate is invalid.
457 @retval RETURN_NOT_FOUND If no CommonName entry exists.
458 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
459 (including the final null) is returned in the
460 CommonNameSize parameter.
461 @retval RETURN_UNSUPPORTED The operation is not supported.
467 IN CONST UINT8
*Cert
,
469 OUT CHAR8
*CommonName
, OPTIONAL
470 IN OUT UINTN
*CommonNameSize
473 return InternalX509GetNIDName (Cert
, CertSize
, NID_commonName
, CommonName
, CommonNameSize
);
477 Retrieve the organization name (O) string from one X.509 certificate.
479 @param[in] Cert Pointer to the DER-encoded X509 certificate.
480 @param[in] CertSize Size of the X509 certificate in bytes.
481 @param[out] NameBuffer Buffer to contain the retrieved certificate organization
482 name string. At most NameBufferSize bytes will be
483 written and the string will be null terminated. May be
484 NULL in order to determine the size buffer needed.
485 @param[in,out] NameBufferSize The size in bytes of the Name buffer on input,
486 and the size of buffer returned Name on output.
487 If NameBuffer is NULL then the amount of space needed
488 in buffer (including the final null) is returned.
490 @retval RETURN_SUCCESS The certificate Organization Name retrieved successfully.
491 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
492 If NameBufferSize is NULL.
493 If NameBuffer is not NULL and *CommonNameSize is 0.
494 If Certificate is invalid.
495 @retval RETURN_NOT_FOUND If no Organization Name entry exists.
496 @retval RETURN_BUFFER_TOO_SMALL If the NameBuffer is NULL. The required buffer size
497 (including the final null) is returned in the
498 CommonNameSize parameter.
499 @retval RETURN_UNSUPPORTED The operation is not supported.
504 X509GetOrganizationName (
505 IN CONST UINT8
*Cert
,
507 OUT CHAR8
*NameBuffer
, OPTIONAL
508 IN OUT UINTN
*NameBufferSize
511 return InternalX509GetNIDName (Cert
, CertSize
, NID_organizationName
, NameBuffer
, NameBufferSize
);
515 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
517 @param[in] Cert Pointer to the DER-encoded X509 certificate.
518 @param[in] CertSize Size of the X509 certificate in bytes.
519 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
520 RSA public key component. Use RsaFree() function to free the
523 If Cert is NULL, then return FALSE.
524 If RsaContext is NULL, then return FALSE.
526 @retval TRUE RSA Public Key was retrieved successfully.
527 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
532 RsaGetPublicKeyFromX509 (
533 IN CONST UINT8
*Cert
,
535 OUT VOID
**RsaContext
543 // Check input parameters.
545 if (Cert
== NULL
|| RsaContext
== NULL
) {
553 // Read DER-encoded X509 Certificate and Construct X509 object.
555 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
556 if ((X509Cert
== NULL
) || (!Status
)) {
564 // Retrieve and check EVP_PKEY data from X509 Certificate.
566 Pkey
= X509_get_pubkey (X509Cert
);
567 if ((Pkey
== NULL
) || (EVP_PKEY_id (Pkey
) != EVP_PKEY_RSA
)) {
572 // Duplicate RSA Context from the retrieved EVP_PKEY.
574 if ((*RsaContext
= RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey
))) != NULL
) {
580 // Release Resources.
582 if (X509Cert
!= NULL
) {
583 X509_free (X509Cert
);
587 EVP_PKEY_free (Pkey
);
594 Verify one X509 certificate was issued by the trusted CA.
596 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
597 @param[in] CertSize Size of the X509 certificate in bytes.
598 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
599 @param[in] CACertSize Size of the CA Certificate in bytes.
601 If Cert is NULL, then return FALSE.
602 If CACert is NULL, then return FALSE.
604 @retval TRUE The certificate was issued by the trusted CA.
605 @retval FALSE Invalid certificate or the certificate was not issued by the given
612 IN CONST UINT8
*Cert
,
614 IN CONST UINT8
*CACert
,
621 X509_STORE
*CertStore
;
622 X509_STORE_CTX
*CertCtx
;
625 // Check input parameters.
627 if (Cert
== NULL
|| CACert
== NULL
) {
638 // Register & Initialize necessary digest algorithms for certificate verification.
640 if (EVP_add_digest (EVP_md5 ()) == 0) {
643 if (EVP_add_digest (EVP_sha1 ()) == 0) {
646 if (EVP_add_digest (EVP_sha256 ()) == 0) {
651 // Read DER-encoded certificate to be verified and Construct X509 object.
653 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
654 if ((X509Cert
== NULL
) || (!Status
)) {
660 // Read DER-encoded root certificate and Construct X509 object.
662 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
663 if ((X509CACert
== NULL
) || (!Status
)) {
671 // Set up X509 Store for trusted certificate.
673 CertStore
= X509_STORE_new ();
674 if (CertStore
== NULL
) {
677 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
682 // Allow partial certificate chains, terminated by a non-self-signed but
683 // still trusted intermediate certificate. Also disable time checks.
685 X509_STORE_set_flags (CertStore
,
686 X509_V_FLAG_PARTIAL_CHAIN
| X509_V_FLAG_NO_CHECK_TIME
);
689 // Set up X509_STORE_CTX for the subsequent verification operation.
691 CertCtx
= X509_STORE_CTX_new ();
692 if (CertCtx
== NULL
) {
695 if (!X509_STORE_CTX_init (CertCtx
, CertStore
, X509Cert
, NULL
)) {
700 // X509 Certificate Verification.
702 Status
= (BOOLEAN
) X509_verify_cert (CertCtx
);
703 X509_STORE_CTX_cleanup (CertCtx
);
707 // Release Resources.
709 if (X509Cert
!= NULL
) {
710 X509_free (X509Cert
);
713 if (X509CACert
!= NULL
) {
714 X509_free (X509CACert
);
717 if (CertStore
!= NULL
) {
718 X509_STORE_free (CertStore
);
721 X509_STORE_CTX_free (CertCtx
);
727 Retrieve the TBSCertificate from one given X.509 certificate.
729 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
730 @param[in] CertSize Size of the X509 certificate in bytes.
731 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
732 @param[out] TBSCertSize Size of the TBS certificate in bytes.
734 If Cert is NULL, then return FALSE.
735 If TBSCert is NULL, then return FALSE.
736 If TBSCertSize is NULL, then return FALSE.
738 @retval TRUE The TBSCertificate was retrieved successfully.
739 @retval FALSE Invalid X.509 certificate.
745 IN CONST UINT8
*Cert
,
748 OUT UINTN
*TBSCertSize
757 // Check input parameters.
759 if ((Cert
== NULL
) || (TBSCert
== NULL
) ||
760 (TBSCertSize
== NULL
) || (CertSize
> INT_MAX
)) {
765 // An X.509 Certificate is: (defined in RFC3280)
766 // Certificate ::= SEQUENCE {
767 // tbsCertificate TBSCertificate,
768 // signatureAlgorithm AlgorithmIdentifier,
769 // signature BIT STRING }
773 // TBSCertificate ::= SEQUENCE {
774 // version [0] Version DEFAULT v1,
778 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
779 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
783 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
785 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
789 *TBSCert
= (UINT8
*)Temp
;
791 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
793 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
795 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
799 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);