2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
4 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalCryptLib.h"
16 #include <openssl/x509.h>
17 #include <openssl/rsa.h>
20 Construct a X509 object from DER-encoded certificate data.
22 If Cert is NULL, then return FALSE.
23 If SingleX509Cert is NULL, then return FALSE.
25 @param[in] Cert Pointer to the DER-encoded certificate data.
26 @param[in] CertSize The size of certificate data in bytes.
27 @param[out] SingleX509Cert The generated X509 object.
29 @retval TRUE The X509 object generation succeeded.
30 @retval FALSE The operation failed.
35 X509ConstructCertificate (
38 OUT UINT8
**SingleX509Cert
45 // Check input parameters.
47 if (Cert
== NULL
|| SingleX509Cert
== NULL
|| CertSize
> INT_MAX
) {
52 // Read DER-encoded X509 Certificate and Construct X509 object.
55 X509Cert
= d2i_X509 (NULL
, &Temp
, (long) CertSize
);
56 if (X509Cert
== NULL
) {
60 *SingleX509Cert
= (UINT8
*) X509Cert
;
66 Construct a X509 stack object from a list of DER-encoded certificate data.
68 If X509Stack is NULL, then return FALSE.
70 @param[in, out] X509Stack On input, pointer to an existing or NULL X509 stack object.
71 On output, pointer to the X509 stack object with new
72 inserted X509 certificate.
73 @param ... A list of DER-encoded single certificate data followed
74 by certificate size. A NULL terminates the list. The
75 pairs are the arguments to X509ConstructCertificate().
77 @retval TRUE The X509 stack construction succeeded.
78 @retval FALSE The construction operation failed.
83 X509ConstructCertificateStack (
84 IN OUT UINT8
**X509Stack
,
91 STACK_OF(X509
) *CertStack
;
97 // Check input parameters.
99 if (X509Stack
== NULL
) {
106 // Initialize X509 stack object.
108 CertStack
= (STACK_OF(X509
) *) (*X509Stack
);
109 if (CertStack
== NULL
) {
110 CertStack
= sk_X509_new_null ();
111 if (CertStack
== NULL
) {
116 VA_START (Args
, X509Stack
);
118 for (Index
= 0; ; Index
++) {
120 // If Cert is NULL, then it is the end of the list.
122 Cert
= VA_ARG (Args
, UINT8
*);
127 CertSize
= VA_ARG (Args
, UINTN
);
133 // Construct X509 Object from the given DER-encoded certificate data.
136 Status
= X509ConstructCertificate (
137 (CONST UINT8
*) Cert
,
142 if (X509Cert
!= NULL
) {
143 X509_free (X509Cert
);
149 // Insert the new X509 object into X509 stack object.
151 sk_X509_push (CertStack
, X509Cert
);
157 sk_X509_pop_free (CertStack
, X509_free
);
159 *X509Stack
= (UINT8
*) CertStack
;
166 Release the specified X509 object.
168 If X509Cert is NULL, then return FALSE.
170 @param[in] X509Cert Pointer to the X509 object to be released.
180 // Check input parameters.
182 if (X509Cert
== NULL
) {
187 // Free OpenSSL X509 object.
189 X509_free ((X509
*) X509Cert
);
193 Release the specified X509 stack object.
195 If X509Stack is NULL, then return FALSE.
197 @param[in] X509Stack Pointer to the X509 stack object to be released.
207 // Check input parameters.
209 if (X509Stack
== NULL
) {
214 // Free OpenSSL X509 stack object.
216 sk_X509_pop_free ((STACK_OF(X509
) *) X509Stack
, X509_free
);
220 Retrieve the subject bytes from one X.509 certificate.
222 @param[in] Cert Pointer to the DER-encoded X509 certificate.
223 @param[in] CertSize Size of the X509 certificate in bytes.
224 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
225 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
226 and the size of buffer returned CertSubject on output.
228 If Cert is NULL, then return FALSE.
229 If SubjectSize is NULL, then return FALSE.
231 @retval TRUE The certificate subject retrieved successfully.
232 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
233 The SubjectSize will be updated with the required size.
239 IN CONST UINT8
*Cert
,
241 OUT UINT8
*CertSubject
,
242 IN OUT UINTN
*SubjectSize
251 // Check input parameters.
253 if (Cert
== NULL
|| SubjectSize
== NULL
) {
260 // Read DER-encoded X509 Certificate and Construct X509 object.
262 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
263 if ((X509Cert
== NULL
) || (!Status
)) {
271 // Retrieve subject name from certificate object.
273 X509Name
= X509_get_subject_name (X509Cert
);
274 if (X509Name
== NULL
) {
278 X509NameSize
= i2d_X509_NAME(X509Name
, NULL
);
279 if (*SubjectSize
< X509NameSize
) {
280 *SubjectSize
= X509NameSize
;
283 *SubjectSize
= X509NameSize
;
284 if (CertSubject
!= NULL
) {
285 i2d_X509_NAME(X509Name
, &CertSubject
);
291 // Release Resources.
293 if (X509Cert
!= NULL
) {
294 X509_free (X509Cert
);
301 Retrieve the common name (CN) string from one X.509 certificate.
303 @param[in] Cert Pointer to the DER-encoded X509 certificate.
304 @param[in] CertSize Size of the X509 certificate in bytes.
305 @param[out] CommonName Buffer to contain the retrieved certificate common
306 name string. At most CommonNameSize bytes will be
307 written and the string will be null terminated. May be
308 NULL in order to determine the size buffer needed.
309 @param[in,out] CommonNameSize The size in bytes of the CommonName buffer on input,
310 and the size of buffer returned CommonName on output.
311 If CommonName is NULL then the amount of space needed
312 in buffer (including the final null) is returned.
314 @retval RETURN_SUCCESS The certificate CommonName retrieved successfully.
315 @retval RETURN_INVALID_PARAMETER If Cert is NULL.
316 If CommonNameSize is NULL.
317 If CommonName is not NULL and *CommonNameSize is 0.
318 If Certificate is invalid.
319 @retval RETURN_NOT_FOUND If no CommonName entry exists.
320 @retval RETURN_BUFFER_TOO_SMALL If the CommonName is NULL. The required buffer size
321 (including the final null) is returned in the
322 CommonNameSize parameter.
323 @retval RETURN_UNSUPPORTED The operation is not supported.
329 IN CONST UINT8
*Cert
,
331 OUT CHAR8
*CommonName
, OPTIONAL
332 IN OUT UINTN
*CommonNameSize
335 RETURN_STATUS ReturnStatus
;
341 ReturnStatus
= RETURN_INVALID_PARAMETER
;
344 // Check input parameters.
346 if ((Cert
== NULL
) || (CertSize
> INT_MAX
) || (CommonNameSize
== NULL
)) {
349 if ((CommonName
!= NULL
) && (*CommonNameSize
== 0)) {
355 // Read DER-encoded X509 Certificate and Construct X509 object.
357 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
358 if ((X509Cert
== NULL
) || (!Status
)) {
360 // Invalid X.509 Certificate
368 // Retrieve subject name from certificate object.
370 X509Name
= X509_get_subject_name (X509Cert
);
371 if (X509Name
== NULL
) {
373 // Fail to retrieve subject name content
379 // Retrieve the CommonName information from X.509 Subject
381 Length
= (INTN
) X509_NAME_get_text_by_NID (X509Name
, NID_commonName
, CommonName
, (int)(*CommonNameSize
));
384 // No CommonName entry exists in X509_NAME object
387 ReturnStatus
= RETURN_NOT_FOUND
;
391 *CommonNameSize
= (UINTN
)(Length
+ 1);
392 if (CommonName
== NULL
) {
393 ReturnStatus
= RETURN_BUFFER_TOO_SMALL
;
395 ReturnStatus
= RETURN_SUCCESS
;
400 // Release Resources.
402 if (X509Cert
!= NULL
) {
403 X509_free (X509Cert
);
410 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
412 @param[in] Cert Pointer to the DER-encoded X509 certificate.
413 @param[in] CertSize Size of the X509 certificate in bytes.
414 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
415 RSA public key component. Use RsaFree() function to free the
418 If Cert is NULL, then return FALSE.
419 If RsaContext is NULL, then return FALSE.
421 @retval TRUE RSA Public Key was retrieved successfully.
422 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
427 RsaGetPublicKeyFromX509 (
428 IN CONST UINT8
*Cert
,
430 OUT VOID
**RsaContext
438 // Check input parameters.
440 if (Cert
== NULL
|| RsaContext
== NULL
) {
448 // Read DER-encoded X509 Certificate and Construct X509 object.
450 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
451 if ((X509Cert
== NULL
) || (!Status
)) {
459 // Retrieve and check EVP_PKEY data from X509 Certificate.
461 Pkey
= X509_get_pubkey (X509Cert
);
462 if ((Pkey
== NULL
) || (EVP_PKEY_id (Pkey
) != EVP_PKEY_RSA
)) {
467 // Duplicate RSA Context from the retrieved EVP_PKEY.
469 if ((*RsaContext
= RSAPublicKey_dup (EVP_PKEY_get0_RSA (Pkey
))) != NULL
) {
475 // Release Resources.
477 if (X509Cert
!= NULL
) {
478 X509_free (X509Cert
);
482 EVP_PKEY_free (Pkey
);
489 Verify one X509 certificate was issued by the trusted CA.
491 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
492 @param[in] CertSize Size of the X509 certificate in bytes.
493 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
494 @param[in] CACertSize Size of the CA Certificate in bytes.
496 If Cert is NULL, then return FALSE.
497 If CACert is NULL, then return FALSE.
499 @retval TRUE The certificate was issued by the trusted CA.
500 @retval FALSE Invalid certificate or the certificate was not issued by the given
507 IN CONST UINT8
*Cert
,
509 IN CONST UINT8
*CACert
,
516 X509_STORE
*CertStore
;
517 X509_STORE_CTX
*CertCtx
;
520 // Check input parameters.
522 if (Cert
== NULL
|| CACert
== NULL
) {
533 // Register & Initialize necessary digest algorithms for certificate verification.
535 if (EVP_add_digest (EVP_md5 ()) == 0) {
538 if (EVP_add_digest (EVP_sha1 ()) == 0) {
541 if (EVP_add_digest (EVP_sha256 ()) == 0) {
546 // Read DER-encoded certificate to be verified and Construct X509 object.
548 Status
= X509ConstructCertificate (Cert
, CertSize
, (UINT8
**) &X509Cert
);
549 if ((X509Cert
== NULL
) || (!Status
)) {
555 // Read DER-encoded root certificate and Construct X509 object.
557 Status
= X509ConstructCertificate (CACert
, CACertSize
, (UINT8
**) &X509CACert
);
558 if ((X509CACert
== NULL
) || (!Status
)) {
566 // Set up X509 Store for trusted certificate.
568 CertStore
= X509_STORE_new ();
569 if (CertStore
== NULL
) {
572 if (!(X509_STORE_add_cert (CertStore
, X509CACert
))) {
577 // Allow partial certificate chains, terminated by a non-self-signed but
578 // still trusted intermediate certificate. Also disable time checks.
580 X509_STORE_set_flags (CertStore
,
581 X509_V_FLAG_PARTIAL_CHAIN
| X509_V_FLAG_NO_CHECK_TIME
);
584 // Set up X509_STORE_CTX for the subsequent verification operation.
586 CertCtx
= X509_STORE_CTX_new ();
587 if (CertCtx
== NULL
) {
590 if (!X509_STORE_CTX_init (CertCtx
, CertStore
, X509Cert
, NULL
)) {
595 // X509 Certificate Verification.
597 Status
= (BOOLEAN
) X509_verify_cert (CertCtx
);
598 X509_STORE_CTX_cleanup (CertCtx
);
602 // Release Resources.
604 if (X509Cert
!= NULL
) {
605 X509_free (X509Cert
);
608 if (X509CACert
!= NULL
) {
609 X509_free (X509CACert
);
612 if (CertStore
!= NULL
) {
613 X509_STORE_free (CertStore
);
616 X509_STORE_CTX_free (CertCtx
);
622 Retrieve the TBSCertificate from one given X.509 certificate.
624 @param[in] Cert Pointer to the given DER-encoded X509 certificate.
625 @param[in] CertSize Size of the X509 certificate in bytes.
626 @param[out] TBSCert DER-Encoded To-Be-Signed certificate.
627 @param[out] TBSCertSize Size of the TBS certificate in bytes.
629 If Cert is NULL, then return FALSE.
630 If TBSCert is NULL, then return FALSE.
631 If TBSCertSize is NULL, then return FALSE.
633 @retval TRUE The TBSCertificate was retrieved successfully.
634 @retval FALSE Invalid X.509 certificate.
640 IN CONST UINT8
*Cert
,
643 OUT UINTN
*TBSCertSize
652 // Check input parameters.
654 if ((Cert
== NULL
) || (TBSCert
== NULL
) ||
655 (TBSCertSize
== NULL
) || (CertSize
> INT_MAX
)) {
660 // An X.509 Certificate is: (defined in RFC3280)
661 // Certificate ::= SEQUENCE {
662 // tbsCertificate TBSCertificate,
663 // signatureAlgorithm AlgorithmIdentifier,
664 // signature BIT STRING }
668 // TBSCertificate ::= SEQUENCE {
669 // version [0] Version DEFAULT v1,
673 // So we can just ASN1-parse the x.509 DER-encoded data. If we strip
674 // the first SEQUENCE, the second SEQUENCE is the TBSCertificate.
677 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)CertSize
);
679 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
683 *TBSCert
= (UINT8
*)Temp
;
685 ASN1_get_object (&Temp
, (long *)&Length
, (int *)&Asn1Tag
, (int *)&ObjClass
, (long)Length
);
687 // Verify the parsed TBSCertificate is one correct SEQUENCE data.
689 if (Asn1Tag
!= V_ASN1_SEQUENCE
) {
693 *TBSCertSize
= Length
+ (Temp
- *TBSCert
);