Skip to content

Commit 8564fd0

Browse files
aamcommit-bot@chromium.org
authored andcommitted
[io/ssl/win] Look for certificates in local machine store as well current user store.
Add certificates from "ca", "trust" and "my" stores in addition to "root". Improve certificate tracing: print certificates as they are added. Clean up error logging. Fixes #45909 TEST=remove certs from current user store, see requests fail to connect, add cert to local machine store, confirm connection works. Change-Id: Ied234098d56b406c9868602a2b806786ae3740be Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/198241 Commit-Queue: Alexander Aprelev <[email protected]> Reviewed-by: Zach Anderson <[email protected]>
1 parent 1f55b7c commit 8564fd0

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

runtime/bin/secure_socket_utils.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ void SecureSocketUtils::CheckStatusSSL(int status,
7575
}
7676
if (SSL_LOG_STATUS) {
7777
int error = ERR_get_error();
78-
Syslog::PrintErr("Failed: %s status %d", message, status);
78+
Syslog::PrintErr("Failed: %s status: %d ", message, status);
7979
char error_string[SSL_ERROR_MESSAGE_BUFFER_SIZE];
8080
ERR_error_string_n(error, error_string, SSL_ERROR_MESSAGE_BUFFER_SIZE);
81-
Syslog::PrintErr("ERROR: %d %s\n", error, error_string);
81+
Syslog::PrintErr("%s\n", error_string);
8282
}
8383
SecureSocketUtils::ThrowIOException(status, type, message, ssl);
8484
}

runtime/bin/security_context_win.cc

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,35 @@ static void PrintSSLErr(const char* str) {
3939
char error_string[SecureSocketUtils::SSL_ERROR_MESSAGE_BUFFER_SIZE];
4040
ERR_error_string_n(error, error_string,
4141
SecureSocketUtils::SSL_ERROR_MESSAGE_BUFFER_SIZE);
42-
Syslog::PrintErr("%s ERROR: %d %s\n", str, error, error_string);
42+
Syslog::PrintErr("%s %s\n", str, error_string);
4343
}
4444

45-
// Add certificates from Windows trusted root store.
46-
static bool AddCertificatesFromRootStore(X509_STORE* store) {
47-
// The UWP platform doesn't support CertEnumCertificatesInStore hence
48-
// this function cannot work when compiled in UWP mode.
49-
#ifdef TARGET_OS_WINDOWS_UWP
50-
return false;
51-
#else
52-
// Open root system store.
53-
// Note that only current user certificates are accessible using this method,
54-
// not the local machine store.
55-
HCERTSTORE cert_store = CertOpenSystemStore(NULL, L"ROOT");
45+
#ifndef TARGET_OS_WINDOWS_UWP
46+
static 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+
5665
if (cert_store == NULL) {
5766
if (SSL_LOG_STATUS) {
5867
DWORD error = GetLastError();
59-
Syslog::PrintErr("Failed to open Windows root store due to %d\n", error);
68+
Syslog::PrintErr(
69+
"Failed to open Windows root store %ls type %d due to %d\n", name,
70+
store_type, error);
6071
}
6172
return false;
6273
}
@@ -84,10 +95,34 @@ static bool AddCertificatesFromRootStore(X509_STORE* store) {
8495
continue;
8596
}
8697
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+
87114
int status = X509_STORE_add_cert(store, root_cert);
88115
if (status == 0) {
116+
int error = ERR_get_error();
117+
if (ERR_GET_REASON(error) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
118+
if (SSL_LOG_STATUS) {
119+
Syslog::Print("...duplicate\n");
120+
}
121+
X509_free(root_cert);
122+
continue;
123+
}
89124
if (SSL_LOG_STATUS) {
90-
PrintSSLErr("Fail to add certificate to trust store");
125+
PrintSSLErr("Failed to add certificate to x509 trust store");
91126
}
92127
X509_free(root_cert);
93128
CertFreeCertificateContext(cert_context);
@@ -104,6 +139,42 @@ static bool AddCertificatesFromRootStore(X509_STORE* store) {
104139
}
105140
return false;
106141
}
142+
return true;
143+
}
144+
145+
static bool AddCertificatesFromSystemStore(DWORD store_type,
146+
X509_STORE* store) {
147+
if (!AddCertificatesFromNamedSystemStore(L"ROOT", store_type, store)) {
148+
return false;
149+
}
150+
if (!AddCertificatesFromNamedSystemStore(L"CA", store_type, store)) {
151+
return false;
152+
}
153+
if (!AddCertificatesFromNamedSystemStore(L"TRUST", store_type, store)) {
154+
return false;
155+
}
156+
if (!AddCertificatesFromNamedSystemStore(L"MY", store_type, store)) {
157+
return false;
158+
}
159+
return true;
160+
}
161+
#endif // ifdef TARGET_OS_WINDOWS_UWP
162+
163+
// Add certificates from Windows trusted root store.
164+
static bool AddCertificatesFromRootStore(X509_STORE* store) {
165+
// The UWP platform doesn't support CertEnumCertificatesInStore hence
166+
// this function cannot work when compiled in UWP mode.
167+
#ifdef TARGET_OS_WINDOWS_UWP
168+
return false;
169+
#else
170+
if (!AddCertificatesFromSystemStore(CERT_SYSTEM_STORE_CURRENT_USER, store)) {
171+
return false;
172+
}
173+
174+
if (!AddCertificatesFromSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, store)) {
175+
return false;
176+
}
177+
107178
return true;
108179
#endif // ifdef TARGET_OS_WINDOWS_UWP
109180
}

0 commit comments

Comments
 (0)