5#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
8#if defined(DART_HOST_OS_WINDOWS)
12#include <openssl/bio.h>
13#include <openssl/ssl.h>
14#include <openssl/x509.h>
23#ifndef DART_TARGET_OS_WINDOWS_UWP
24#pragma comment(lib, "crypt32.lib")
37static void PrintSSLErr(
const char* str) {
38 int error = ERR_get_error();
40 ERR_error_string_n(
error, error_string,
45#ifndef DART_TARGET_OS_WINDOWS_UWP
46static bool AddCertificatesFromNamedSystemStore(
const wchar_t*
name,
49 ASSERT(store_type == CERT_SYSTEM_STORE_CURRENT_USER ||
50 store_type == CERT_SYSTEM_STORE_LOCAL_MACHINE);
54 store_type == CERT_SYSTEM_STORE_CURRENT_USER
59 HCERTSTORE cert_store =
60 CertOpenStore(CERT_STORE_PROV_SYSTEM,
63 store_type | CERT_STORE_READONLY_FLAG,
name);
65 if (cert_store ==
nullptr) {
69 "Failed to open Windows root store %ls type %d due to %d\n",
name,
77 PCCERT_CONTEXT cert_context =
nullptr;
79 cert_context = CertEnumCertificatesInStore(cert_store, cert_context);
80 if (cert_context ==
nullptr) {
85 BIO_new_mem_buf(
const_cast<unsigned char*
>(cert_context->pbCertEncoded),
86 cert_context->cbCertEncoded);
89 X509* root_cert =
nullptr;
90 if (d2i_X509_bio(root_cert_bio, &root_cert) ==
nullptr) {
92 PrintSSLErr(
"Fail to read certificate");
94 BIO_free(root_cert_bio);
97 BIO_free(root_cert_bio);
100 auto s_name = X509_get_subject_name(root_cert);
101 auto s_issuer_name = X509_get_issuer_name(root_cert);
102 auto serial_number = X509_get_serialNumber(root_cert);
103 BIGNUM* bn = ASN1_INTEGER_to_BN(serial_number,
nullptr);
104 char* hex = BN_bn2hex(bn);
105 Syslog::Print(
"Considering root certificate serial: %s subject name: ",
108 X509_NAME_print_ex_fp(stdout, s_name, 4, 0);
110 X509_NAME_print_ex_fp(stdout, s_issuer_name, 4, 0);
116 Syslog::Print(
"...certificate is outside of its valid date range\n");
118 X509_free(root_cert);
122 int status = X509_STORE_add_cert(
store, root_cert);
124 X509_free(root_cert);
126 int error = ERR_get_error();
127 if (ERR_GET_REASON(
error) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
134 PrintSSLErr(
"Failed to add certificate to x509 trust store");
136 CertFreeCertificateContext(cert_context);
137 CertCloseStore(cert_store, 0);
140 }
while (cert_context !=
nullptr);
143 CertFreeCertificateContext(cert_context);
144 if (!CertCloseStore(cert_store, 0)) {
146 PrintSSLErr(
"Fail to close system root store");
153static bool AddCertificatesFromSystemStore(
DWORD store_type,
155 if (!AddCertificatesFromNamedSystemStore(
L"ROOT", store_type,
store)) {
158 if (!AddCertificatesFromNamedSystemStore(
L"CA", store_type,
store)) {
161 if (!AddCertificatesFromNamedSystemStore(
L"TRUST", store_type,
store)) {
164 if (!AddCertificatesFromNamedSystemStore(
L"MY", store_type,
store)) {
172static bool AddCertificatesFromRootStore(X509_STORE*
store) {
175#ifdef DART_TARGET_OS_WINDOWS_UWP
178 if (!AddCertificatesFromSystemStore(CERT_SYSTEM_STORE_CURRENT_USER,
store)) {
182 if (!AddCertificatesFromSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE,
store)) {
210 if (AddCertificatesFromRootStore(
store)) {
216 SSL_CTX_set_cert_store(
context(), X509_STORE_new());
222 AddCompiledInCerts();
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static void Print(const char *format,...) PRINTF_ATTRIBUTE(1
void RegisterCallbacks(SSL *ssl)
SSL_CTX * context() const
static const char * root_certs_file()
TrustEvaluateHandlerFunc GetTrustEvaluateHandler() const
static const intptr_t kApproximateSize
static const char * root_certs_cache()
static bool bypass_trusting_system_roots()
static constexpr int SSL_ERROR_MESSAGE_BUFFER_SIZE
static bool IsCurrentTimeInsideCertValidDateRange(X509 *root_cert)
const uint8_t uint32_t uint32_t GError ** error
const bool SSL_LOG_STATUS
unsigned int root_certificates_pem_length
void(* TrustEvaluateHandlerFunc)(Dart_Port dest_port_id, Dart_CObject *message)
WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI GetLastError(VOID)