Flutter Engine
The Flutter Engine
Static Public Member Functions | List of all members
dart::bin::X509Helper Class Reference

#include <security_context.h>

Inheritance diagram for dart::bin::X509Helper:
dart::AllStatic

Static Public Member Functions

static Dart_Handle GetDer (Dart_NativeArguments args)
 
static Dart_Handle GetPem (Dart_NativeArguments args)
 
static Dart_Handle GetSha1 (Dart_NativeArguments args)
 
static Dart_Handle GetSubject (Dart_NativeArguments args)
 
static Dart_Handle GetIssuer (Dart_NativeArguments args)
 
static Dart_Handle GetStartValidity (Dart_NativeArguments args)
 
static Dart_Handle GetEndValidity (Dart_NativeArguments args)
 
static Dart_Handle WrappedX509Certificate (X509 *certificate)
 

Detailed Description

Definition at line 128 of file security_context.h.

Member Function Documentation

◆ GetDer()

Dart_Handle dart::bin::X509Helper::GetDer ( Dart_NativeArguments  args)
static

Definition at line 649 of file security_context.cc.

649 {
650 X509* certificate = GetX509Certificate(args);
651 // When the second argument is nullptr, i2d_X509() returns the length of the
652 // DER encoded cert in bytes.
653 intptr_t length = i2d_X509(certificate, nullptr);
655 if (Dart_IsError(cert_handle)) {
656 Dart_PropagateError(cert_handle);
657 }
659 void* dart_cert_bytes = nullptr;
660 Dart_Handle status =
661 Dart_TypedDataAcquireData(cert_handle, &typ, &dart_cert_bytes, &length);
662 if (Dart_IsError(status)) {
663 Dart_PropagateError(status);
664 }
665
666 // When the second argument points to a non-nullptr buffer address,
667 // i2d_X509 fills that buffer with the DER encoded cert data and increments
668 // the buffer pointer.
669 unsigned char* tmp = static_cast<unsigned char*>(dart_cert_bytes);
670 const intptr_t written_length = i2d_X509(certificate, &tmp);
671 ASSERT(written_length <= length);
672 if (written_length < 0) {
673 Dart_TypedDataReleaseData(cert_handle);
675 -1, "TlsException", "Failed to get certificate bytes", nullptr);
676 // SecureSocketUtils::ThrowIOException() does not return.
677 }
678
679 status = Dart_TypedDataReleaseData(cert_handle);
680 if (Dart_IsError(status)) {
681 Dart_PropagateError(status);
682 }
683 return cert_handle;
684}
static void ThrowIOException(int status, const char *exception_type, const char *message, const SSL *ssl)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
Dart_TypedData_Type
Definition: dart_api.h:2612
@ Dart_TypedData_kUint8
Definition: dart_api.h:2615
#define ASSERT(E)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
size_t length
static X509 * GetX509Certificate(Dart_NativeArguments args)
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, intptr_t length)
DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, Dart_TypedData_Type *type, void **data, intptr_t *len)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object)

◆ GetEndValidity()

Dart_Handle dart::bin::X509Helper::GetEndValidity ( Dart_NativeArguments  args)
static

Definition at line 791 of file security_context.cc.

791 {
792 X509* certificate = GetX509Certificate(args);
793 ASN1_TIME* not_after = X509_get_notAfter(certificate);
794 return ASN1TimeToMilliseconds(not_after);
795}
static Dart_Handle ASN1TimeToMilliseconds(ASN1_TIME *aTime)

◆ GetIssuer()

Dart_Handle dart::bin::X509Helper::GetIssuer ( Dart_NativeArguments  args)
static

Definition at line 758 of file security_context.cc.

758 {
759 X509* certificate = GetX509Certificate(args);
760 X509_NAME* issuer = X509_get_issuer_name(certificate);
761 char* issuer_string = X509_NAME_oneline(issuer, nullptr, 0);
762 if (issuer_string == nullptr) {
764 "X509.issuer failed to find issuer's common name."));
765 }
766 Dart_Handle issuer_handle = Dart_NewStringFromCString(issuer_string);
767 OPENSSL_free(issuer_string);
768 return issuer_handle;
769}
static Dart_Handle NewDartArgumentError(const char *message)
Definition: dartutils.cc:746
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)

◆ GetPem()

Dart_Handle dart::bin::X509Helper::GetPem ( Dart_NativeArguments  args)
static

Definition at line 686 of file security_context.cc.

686 {
687 X509* certificate = GetX509Certificate(args);
688 BIO* cert_bio = BIO_new(BIO_s_mem());
689 intptr_t status = PEM_write_bio_X509(cert_bio, certificate);
690 if (status == 0) {
691 BIO_free(cert_bio);
693 -1, "TlsException", "Failed to write certificate to PEM", nullptr);
694 // SecureSocketUtils::ThrowIOException() does not return.
695 }
696
697 BUF_MEM* mem = nullptr;
698 BIO_get_mem_ptr(cert_bio, &mem);
700 reinterpret_cast<const uint8_t*>(mem->data), mem->length);
701 BIO_free(cert_bio);
702 if (Dart_IsError(pem_string)) {
703 Dart_PropagateError(pem_string);
704 }
705
706 return pem_string;
707}
DART_EXPORT Dart_Handle Dart_NewStringFromUTF8(const uint8_t *utf8_array, intptr_t length)

◆ GetSha1()

Dart_Handle dart::bin::X509Helper::GetSha1 ( Dart_NativeArguments  args)
static

Definition at line 709 of file security_context.cc.

709 {
710 unsigned char sha1_bytes[EVP_MAX_MD_SIZE];
711 X509* certificate = GetX509Certificate(args);
712 const EVP_MD* hash_type = EVP_sha1();
713
714 unsigned int sha1_size;
715 intptr_t status = X509_digest(certificate, hash_type, sha1_bytes, &sha1_size);
716 if (status == 0) {
718 -1, "TlsException", "Failed to compute certificate's sha1", nullptr);
719 // SecureSocketUtils::ThrowIOException() does not return.
720 }
721
722 Dart_Handle sha1_handle = Dart_NewTypedData(Dart_TypedData_kUint8, sha1_size);
723 if (Dart_IsError(sha1_handle)) {
724 Dart_PropagateError(sha1_handle);
725 }
726
728 void* dart_sha1_bytes;
729 intptr_t length;
731 Dart_TypedDataAcquireData(sha1_handle, &typ, &dart_sha1_bytes, &length);
732 if (Dart_IsError(result)) {
734 }
735
736 memmove(dart_sha1_bytes, sha1_bytes, length);
737
738 result = Dart_TypedDataReleaseData(sha1_handle);
739 if (Dart_IsError(result)) {
741 }
742 return sha1_handle;
743}
GAsyncResult * result

◆ GetStartValidity()

Dart_Handle dart::bin::X509Helper::GetStartValidity ( Dart_NativeArguments  args)
static

Definition at line 785 of file security_context.cc.

785 {
786 X509* certificate = GetX509Certificate(args);
787 ASN1_TIME* not_before = X509_get_notBefore(certificate);
788 return ASN1TimeToMilliseconds(not_before);
789}

◆ GetSubject()

Dart_Handle dart::bin::X509Helper::GetSubject ( Dart_NativeArguments  args)
static

Definition at line 745 of file security_context.cc.

745 {
746 X509* certificate = GetX509Certificate(args);
747 X509_NAME* subject = X509_get_subject_name(certificate);
748 char* subject_string = X509_NAME_oneline(subject, nullptr, 0);
749 if (subject_string == nullptr) {
751 "X509.subject failed to find subject's common name."));
752 }
753 Dart_Handle subject_handle = Dart_NewStringFromCString(subject_string);
754 OPENSSL_free(subject_string);
755 return subject_handle;
756}

◆ WrappedX509Certificate()

Dart_Handle dart::bin::X509Helper::WrappedX509Certificate ( X509 *  certificate)
static

Definition at line 146 of file security_context.cc.

146 {
147 if (certificate == nullptr) {
148 return Dart_Null();
149 }
150 Dart_Handle x509_type =
152 if (Dart_IsError(x509_type)) {
153 X509_free(certificate);
154 return x509_type;
155 }
156 Dart_Handle arguments[] = {nullptr};
158 Dart_New(x509_type, DartUtils::NewString("_"), 0, arguments);
159 if (Dart_IsError(result)) {
160 X509_free(certificate);
161 return result;
162 }
164 Dart_Handle status =
166 reinterpret_cast<intptr_t>(certificate));
167 if (Dart_IsError(status)) {
168 X509_free(certificate);
169 return status;
170 }
171 const intptr_t approximate_size_of_certificate =
172 EstimateX509Size(certificate);
173 ASSERT(approximate_size_of_certificate > 0);
174 Dart_NewFinalizableHandle(result, reinterpret_cast<void*>(certificate),
175 approximate_size_of_certificate,
177 return result;
178}
static constexpr const char * kIOLibURL
Definition: dartutils.h:297
static Dart_Handle NewString(const char *str)
Definition: dartutils.h:214
static Dart_Handle GetDartType(const char *library_url, const char *class_name)
Definition: dartutils.cc:696
static constexpr int kX509NativeFieldIndex
static intptr_t EstimateX509Size(X509 *certificate)
static void ReleaseCertificate(void *isolate_data, void *context_pointer)
DART_EXPORT bool Dart_IsInstance(Dart_Handle object)
DART_EXPORT Dart_FinalizableHandle Dart_NewFinalizableHandle(Dart_Handle object, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj, int index, intptr_t value)
DART_EXPORT Dart_Handle Dart_Null()
DART_EXPORT Dart_Handle Dart_New(Dart_Handle type, Dart_Handle constructor_name, int number_of_arguments, Dart_Handle *arguments)

The documentation for this class was generated from the following files: