Flutter Engine
The Flutter Engine
security_context_win.cc
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#if !defined(DART_IO_SECURE_SOCKET_DISABLED)
6
7#include "platform/globals.h"
8#if defined(DART_HOST_OS_WINDOWS)
9
11
12#include <openssl/bio.h>
13#include <openssl/ssl.h>
14#include <openssl/x509.h>
15#include <wincrypt.h>
16
17#include "bin/directory.h"
18#include "bin/file.h"
21#include "platform/syslog.h"
22
23#ifndef DART_TARGET_OS_WINDOWS_UWP
24#pragma comment(lib, "crypt32.lib")
25#endif
26
27namespace dart {
28namespace bin {
29
30// The security context won't necessarily use the compiled-in root certificates,
31// but since there is no way to update the size of the allocation after creating
32// the weak persistent handle, we assume that it will. Note that when the
33// root certs aren't compiled in, |root_certificates_pem_length| is 0.
35 sizeof(SSLCertContext) + root_certificates_pem_length;
36
37static void PrintSSLErr(const char* str) {
38 int error = ERR_get_error();
40 ERR_error_string_n(error, error_string,
42 Syslog::PrintErr("%s %s\n", str, error_string);
43}
44
45#ifndef DART_TARGET_OS_WINDOWS_UWP
46static bool AddCertificatesFromNamedSystemStore(const wchar_t* name,
47 DWORD store_type,
48 X509_STORE* store) {
49 ASSERT(store_type == CERT_SYSTEM_STORE_CURRENT_USER ||
50 store_type == CERT_SYSTEM_STORE_LOCAL_MACHINE);
51
52 if (SSL_LOG_STATUS) {
53 Syslog::Print("AddCertificatesFromNamedSystemStore %ls type: %s\n", name,
54 store_type == CERT_SYSTEM_STORE_CURRENT_USER
55 ? "Current User"
56 : "Local Machine");
57 }
58
59 HCERTSTORE cert_store =
60 CertOpenStore(CERT_STORE_PROV_SYSTEM,
61 0, // the encoding type is not needed
62 NULL, // use the default HCRYPTPROV
63 store_type | CERT_STORE_READONLY_FLAG, name);
64
65 if (cert_store == nullptr) {
66 if (SSL_LOG_STATUS) {
69 "Failed to open Windows root store %ls type %d due to %d\n", name,
70 store_type, error);
71 }
72 return false;
73 }
74
75 // Iterating through all certificates in the store. A nullptr is required to
76 // start iteration.
77 PCCERT_CONTEXT cert_context = nullptr;
78 do {
79 cert_context = CertEnumCertificatesInStore(cert_store, cert_context);
80 if (cert_context == nullptr) {
81 // reach the end of store.
82 break;
83 }
84 BIO* root_cert_bio =
85 BIO_new_mem_buf(const_cast<unsigned char*>(cert_context->pbCertEncoded),
86 cert_context->cbCertEncoded);
87 // `root_cert` has to be initialized to nullptr, otherwise, it will be
88 // considered as an existing X509 and cause segmentation fault.
89 X509* root_cert = nullptr;
90 if (d2i_X509_bio(root_cert_bio, &root_cert) == nullptr) {
91 if (SSL_LOG_STATUS) {
92 PrintSSLErr("Fail to read certificate");
93 }
94 BIO_free(root_cert_bio);
95 continue;
96 }
97 BIO_free(root_cert_bio);
98
99 if (SSL_LOG_STATUS) {
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: ",
106 hex);
107 OPENSSL_free(hex);
108 X509_NAME_print_ex_fp(stdout, s_name, 4, 0);
109 Syslog::Print(" issuer:");
110 X509_NAME_print_ex_fp(stdout, s_issuer_name, 4, 0);
111 Syslog::Print("\n");
112 }
113
115 if (SSL_LOG_STATUS) {
116 Syslog::Print("...certificate is outside of its valid date range\n");
117 }
118 X509_free(root_cert);
119 continue;
120 }
121
122 int status = X509_STORE_add_cert(store, root_cert);
123 // Always free the certificate after use.
124 X509_free(root_cert);
125 if (status == 0) {
126 int error = ERR_get_error();
127 if (ERR_GET_REASON(error) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
128 if (SSL_LOG_STATUS) {
129 Syslog::Print("...duplicate\n");
130 }
131 continue;
132 }
133 if (SSL_LOG_STATUS) {
134 PrintSSLErr("Failed to add certificate to x509 trust store");
135 }
136 CertFreeCertificateContext(cert_context);
137 CertCloseStore(cert_store, 0);
138 return false;
139 }
140 } while (cert_context != nullptr);
141
142 // It always returns non-zero.
143 CertFreeCertificateContext(cert_context);
144 if (!CertCloseStore(cert_store, 0)) {
145 if (SSL_LOG_STATUS) {
146 PrintSSLErr("Fail to close system root store");
147 }
148 return false;
149 }
150 return true;
151}
152
153static bool AddCertificatesFromSystemStore(DWORD store_type,
154 X509_STORE* store) {
155 if (!AddCertificatesFromNamedSystemStore(L"ROOT", store_type, store)) {
156 return false;
157 }
158 if (!AddCertificatesFromNamedSystemStore(L"CA", store_type, store)) {
159 return false;
160 }
161 if (!AddCertificatesFromNamedSystemStore(L"TRUST", store_type, store)) {
162 return false;
163 }
164 if (!AddCertificatesFromNamedSystemStore(L"MY", store_type, store)) {
165 return false;
166 }
167 return true;
168}
169#endif // ifdef DART_TARGET_OS_WINDOWS_UWP
170
171// Add certificates from Windows trusted root store.
172static bool AddCertificatesFromRootStore(X509_STORE* store) {
173// The UWP platform doesn't support CertEnumCertificatesInStore hence
174// this function cannot work when compiled in UWP mode.
175#ifdef DART_TARGET_OS_WINDOWS_UWP
176 return false;
177#else
178 if (!AddCertificatesFromSystemStore(CERT_SYSTEM_STORE_CURRENT_USER, store)) {
179 return false;
180 }
181
182 if (!AddCertificatesFromSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, store)) {
183 return false;
184 }
185
186 return true;
187#endif // ifdef DART_TARGET_OS_WINDOWS_UWP
188}
189
191 // First, try to use locations specified on the command line.
192 if (root_certs_file() != nullptr) {
193 LoadRootCertFile(root_certs_file());
194 return;
195 }
196 if (root_certs_cache() != nullptr) {
197 LoadRootCertCache(root_certs_cache());
198 return;
199 }
200
202 if (SSL_LOG_STATUS) {
203 Syslog::Print("Bypass trusting Windows built-in roots\n");
204 }
205 } else {
206 if (SSL_LOG_STATUS) {
207 Syslog::Print("Trusting Windows built-in roots\n");
208 }
209 X509_STORE* store = SSL_CTX_get_cert_store(context());
210 if (AddCertificatesFromRootStore(store)) {
211 return;
212 }
213 }
214 // Reset store. SSL_CTX_set_cert_store will take ownership of store. A manual
215 // free is not needed.
216 SSL_CTX_set_cert_store(context(), X509_STORE_new());
217 // Fall back on the compiled-in certs if the standard locations don't exist,
218 // or fail to load certificates from Windows root store.
219 if (SSL_LOG_STATUS) {
220 Syslog::Print("Trusting compiled-in roots\n");
221 }
222 AddCompiledInCerts();
223}
224
226 // No callbacks to register for implementations using BoringSSL's built-in
227 // verification mechanism.
228}
229
231 return nullptr;
232}
233
234} // namespace bin
235} // namespace dart
236
237#endif // defined(DART_HOST_OS_WINDOWS)
238
239#endif // !defined(DART_IO_SECURE_SOCKET_DISABLED)
SI void store(P *ptr, const T &val)
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)
#define ASSERT(E)
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)
Definition: dart_vm.cc:33
const char *const name
WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI GetLastError(VOID)
unsigned long DWORD
Definition: windows_types.h:22