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