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 the common name (CN) string from one X.509 certificate.
297 @param[in] Cert Pointer to the DER-encoded X509 certificate.
298 @param[in] CertSize Size of the X509 certificate in bytes.
299 @param[out] CommonName Buffer to contain the retrieved certificate common
300 name string (UTF8). At most CommonNameSize bytes will be
301 written and the string will be null terminated. May be
302 NULL in order to determine the size buffer needed.
303 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
304 and the size of buffer returned CommonName on output.
305 If CommonName is NULL then the amount of space needed
306 in buffer (including the final null) is returned.
308 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
309 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
310 If CommonNameSize is NULL.
311 If CommonName is not NULL and *CommonNameSize is 0.
312 If Certificate is invalid.
313 @retval RETURN_NOT_FOUND If no CommonName entry exists.
314 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
315 (including the final null) is returned in the
316 CommonNameSize parameter.
317 @retval RETURN_UNSUPPORTED The operation is not supported.
323 IN CONST UINT8
*Cert
,
325 OUT CHAR8
*CommonName
, OPTIONAL
326 IN OUT UINTN
*CommonNameSize
329 RETURN_STATUS ReturnStatus
;
335 X509_NAME_ENTRY
*Entry
;
336 ASN1_STRING
*EntryData
;
339 ReturnStatus
= RETURN_INVALID_PARAMETER
;
343 // Check input parameters.
345 if ((Cert
== NULL
) || (CertSize
> INT_MAX
) || (CommonNameSize
== NULL
)) {
348 if ((CommonName
!= NULL
) && (*CommonNameSize
== 0)) {
354 // Read DER-encoded X509 Certificate and Construct X509 object.
356 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
357 if ((X509Cert
== NULL
) || (!Status
)) {
359 // Invalid X.509 Certificate
367 // Retrieve subject name from certificate object.
369 X509Name
= X509_get_subject_name (X509Cert
);
370 if (X509Name
== NULL
) {
372 // Fail to retrieve subject name content
378 // Retrieve the CommonName information from X.509 Subject
380 Index
= X509_NAME_get_index_by_NID (X509Name
, NID_commonName
, -1);
383 // No CommonName entry exists in X509_NAME object
386 ReturnStatus
= RETURN_NOT_FOUND
;
390 Entry
= X509_NAME_get_entry (X509Name
, Index
);
393 // Fail to retrieve name entry data
396 ReturnStatus
= RETURN_NOT_FOUND
;
400 EntryData
= X509_NAME_ENTRY_get_data (Entry
);
402 Length
= ASN1_STRING_to_UTF8 (&UTF8Name
, EntryData
);
405 // Fail to convert the commonName string
408 ReturnStatus
= RETURN_INVALID_PARAMETER
;
412 if (CommonName
== NULL
) {
413 *CommonNameSize
= Length
+ 1;
414 ReturnStatus
= RETURN_BUFFER_TOO_SMALL
;
416 *CommonNameSize
= MIN ((UINTN
)Length
, *CommonNameSize
- 1) + 1;
417 CopyMem (CommonName
, UTF8Name
, *CommonNameSize
- 1);
418 CommonName
[*CommonNameSize
- 1] = '\0';
419 ReturnStatus
= RETURN_SUCCESS
;
424 // Release Resources.
426 if (X509Cert
!= NULL
) {
427 X509_free (X509Cert
);
429 if (UTF8Name
!= NULL
) {
430 OPENSSL_free (UTF8Name
);
437 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
439 @param[in] Cert Pointer to the DER-encoded X509 certificate.
440 @param[in] CertSize Size of the X509 certificate in bytes.
441 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
442 RSA public key component. Use RsaFree() function to free the
445 If Cert is NULL, then return FALSE.
446 If RsaContext is NULL, then return FALSE.
448 @retval TRUE RSA Public Key was retrieved successfully.
449 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
454 RsaGetPublicKeyFromX509 (
455 IN CONST UINT8
*Cert
,
457 OUT VOID
**RsaContext
465 // Check input parameters.
467 if (Cert
== NULL
|| RsaContext
== NULL
) {
475 // Read DER-encoded X509 Certificate and Construct X509 object.
477 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
478 if ((X509Cert
== NULL
) || (!Status
)) {
486 // Retrieve and check EVP_PKEY data from X509 Certificate.
488 Pkey
= X509_get_pubkey (X509Cert
);
489 if ((Pkey
== NULL
) || (EVP_PKEY_id (Pkey
) != EVP_PKEY_RSA
)) {
494 // Duplicate RSA Context from the retrieved EVP_PKEY.
496 if ((*RsaContext
= RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey
))) != NULL
) {
502 // Release Resources.
504 if (X509Cert
!= NULL
) {
505 X509_free (X509Cert
);
509 EVP_PKEY_free (Pkey
);
516 Verify one X509 certificate was issued by the trusted CA.
518 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
519 @param[in] CertSize Size of the X509 certificate in bytes.
520 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
521 @param[in] CACertSize Size of the CA Certificate in bytes.
523 If Cert is NULL, then return FALSE.
524 If CACert is NULL, then return FALSE.
526 @retval TRUE The certificate was issued by the trusted CA.
527 @retval FALSE Invalid certificate or the certificate was not issued by the given
534 IN CONST UINT8
*Cert
,
536 IN CONST UINT8
*CACert
,
543 X509_STORE
*CertStore
;
544 X509_STORE_CTX
*CertCtx
;
547 // Check input parameters.
549 if (Cert
== NULL
|| CACert
== NULL
) {
560 // Register & Initialize necessary digest algorithms for certificate verification.
562 if (EVP_add_digest (EVP_md5 ()) == 0) {
565 if (EVP_add_digest (EVP_sha1 ()) == 0) {
568 if (EVP_add_digest (EVP_sha256 ()) == 0) {
573 // Read DER-encoded certificate to be verified and Construct X509 object.
575 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
576 if ((X509Cert
== NULL
) || (!Status
)) {
582 // Read DER-encoded root certificate and Construct X509 object.
584 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
585 if ((X509CACert
== NULL
) || (!Status
)) {
593 // Set up X509 Store for trusted certificate.
595 CertStore
= X509_STORE_new ();
596 if (CertStore
== NULL
) {
599 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
604 // Allow partial certificate chains, terminated by a non-self-signed but
605 // still trusted intermediate certificate. Also disable time checks.
607 X509_STORE_set_flags (CertStore
,
608 X509_V_FLAG_PARTIAL_CHAIN
| X509_V_FLAG_NO_CHECK_TIME
);
611 // Set up X509_STORE_CTX for the subsequent verification operation.
613 CertCtx
= X509_STORE_CTX_new ();
614 if (CertCtx
== NULL
) {
617 if (!X509_STORE_CTX_init (CertCtx
, CertStore
, X509Cert
, NULL
)) {
622 // X509 Certificate Verification.
624 Status
= (BOOLEAN
) X509_verify_cert (CertCtx
);
625 X509_STORE_CTX_cleanup (CertCtx
);
629 // Release Resources.
631 if (X509Cert
!= NULL
) {
632 X509_free (X509Cert
);
635 if (X509CACert
!= NULL
) {
636 X509_free (X509CACert
);
639 if (CertStore
!= NULL
) {
640 X509_STORE_free (CertStore
);
643 X509_STORE_CTX_free (CertCtx
);
649 Retrieve the TBSCertificate from one given X.509 certificate.
651 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
652 @param[in] CertSize Size of the X509 certificate in bytes.
653 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
654 @param[out] TBSCertSize Size of the TBS certificate in bytes.
656 If Cert is NULL, then return FALSE.
657 If TBSCert is NULL, then return FALSE.
658 If TBSCertSize is NULL, then return FALSE.
660 @retval TRUE The TBSCertificate was retrieved successfully.
661 @retval FALSE Invalid X.509 certificate.
667 IN CONST UINT8
*Cert
,
670 OUT UINTN
*TBSCertSize
679 // Check input parameters.
681 if ((Cert
== NULL
) || (TBSCert
== NULL
) ||
682 (TBSCertSize
== NULL
) || (CertSize
> INT_MAX
)) {
687 // An X.509 Certificate is: (defined in RFC3280)
688 // Certificate ::= SEQUENCE {
689 // tbsCertificate TBSCertificate,
690 // signatureAlgorithm AlgorithmIdentifier,
691 // signature BIT STRING }
695 // TBSCertificate ::= SEQUENCE {
696 // version [0] Version DEFAULT v1,
700 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
701 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
705 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
707 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
711 *TBSCert
= (UINT8
*)Temp
;
713 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
715 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
717 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
721 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);