Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ssl / ssl_error_info.cc
blob31874150eae112d9efd6424458b3a4fbaf6fed62
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ssl/ssl_error_info.h"
7 #include "base/i18n/time_formatting.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/grit/chromium_strings.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "content/public/browser/cert_store.h"
13 #include "net/base/escape.h"
14 #include "net/base/net_errors.h"
15 #include "net/cert/cert_status_flags.h"
16 #include "net/ssl/ssl_info.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "url/gurl.h"
20 using base::UTF8ToUTF16;
22 SSLErrorInfo::SSLErrorInfo(const base::string16& details,
23 const base::string16& short_description)
24 : details_(details),
25 short_description_(short_description) {
28 // static
29 SSLErrorInfo SSLErrorInfo::CreateError(ErrorType error_type,
30 net::X509Certificate* cert,
31 const GURL& request_url) {
32 base::string16 details, short_description;
33 switch (error_type) {
34 case CERT_COMMON_NAME_INVALID: {
35 // If the certificate contains multiple DNS names, we choose the most
36 // representative one -- either the DNS name that's also in the subject
37 // field, or the first one. If this heuristic turns out to be
38 // inadequate, we can consider choosing the DNS name that is the
39 // "closest match" to the host name in the request URL, or listing all
40 // the DNS names with an HTML <ul>.
41 std::vector<std::string> dns_names;
42 cert->GetDNSNames(&dns_names);
43 DCHECK(!dns_names.empty());
44 size_t i = 0;
45 for (; i < dns_names.size(); ++i) {
46 if (dns_names[i] == cert->subject().common_name)
47 break;
49 if (i == dns_names.size())
50 i = 0;
51 details =
52 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_COMMON_NAME_INVALID_DETAILS,
53 UTF8ToUTF16(request_url.host()),
54 net::EscapeForHTML(
55 UTF8ToUTF16(dns_names[i])));
56 short_description = l10n_util::GetStringUTF16(
57 IDS_CERT_ERROR_COMMON_NAME_INVALID_DESCRIPTION);
58 break;
60 case CERT_DATE_INVALID:
61 if (cert->HasExpired()) {
62 details = l10n_util::GetStringFUTF16(
63 IDS_CERT_ERROR_EXPIRED_DETAILS,
64 UTF8ToUTF16(request_url.host()),
65 base::IntToString16(
66 (base::Time::Now() - cert->valid_expiry()).InDays()),
67 base::TimeFormatFriendlyDate(base::Time::Now()));
68 short_description =
69 l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXPIRED_DESCRIPTION);
70 } else if (base::Time::Now() < cert->valid_start()) {
71 details = l10n_util::GetStringFUTF16(
72 IDS_CERT_ERROR_NOT_YET_VALID_DETAILS,
73 UTF8ToUTF16(request_url.host()),
74 base::IntToString16(
75 (cert->valid_start() - base::Time::Now()).InDays()));
76 short_description =
77 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DESCRIPTION);
78 } else {
79 // Two possibilities: (1) an intermediate or root certificate has
80 // expired, or (2) the certificate has become valid since the error
81 // occurred. Since (1) is more likely, assume that's the case.
82 details = l10n_util::GetStringFUTF16(
83 IDS_CERT_ERROR_CHAIN_EXPIRED_DETAILS,
84 UTF8ToUTF16(request_url.host()),
85 base::TimeFormatFriendlyDate(base::Time::Now()));
86 short_description =
87 l10n_util::GetStringUTF16(IDS_CERT_ERROR_CHAIN_EXPIRED_DESCRIPTION);
89 break;
90 case CERT_AUTHORITY_INVALID:
91 details = l10n_util::GetStringFUTF16(
92 IDS_CERT_ERROR_AUTHORITY_INVALID_DETAILS,
93 UTF8ToUTF16(request_url.host()));
94 short_description = l10n_util::GetStringUTF16(
95 IDS_CERT_ERROR_AUTHORITY_INVALID_DESCRIPTION);
96 break;
97 case CERT_CONTAINS_ERRORS:
98 details = l10n_util::GetStringFUTF16(
99 IDS_CERT_ERROR_CONTAINS_ERRORS_DETAILS,
100 UTF8ToUTF16(request_url.host()));
101 short_description =
102 l10n_util::GetStringUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DESCRIPTION);
103 break;
104 case CERT_NO_REVOCATION_MECHANISM:
105 details = l10n_util::GetStringUTF16(
106 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DETAILS);
107 short_description = l10n_util::GetStringUTF16(
108 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DESCRIPTION);
109 break;
110 case CERT_REVOKED:
111 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_REVOKED_CERT_DETAILS,
112 UTF8ToUTF16(request_url.host()));
113 short_description =
114 l10n_util::GetStringUTF16(IDS_CERT_ERROR_REVOKED_CERT_DESCRIPTION);
115 break;
116 case CERT_INVALID:
117 details = l10n_util::GetStringFUTF16(
118 IDS_CERT_ERROR_INVALID_CERT_DETAILS,
119 UTF8ToUTF16(request_url.host()));
120 short_description =
121 l10n_util::GetStringUTF16(IDS_CERT_ERROR_INVALID_CERT_DESCRIPTION);
122 break;
123 case CERT_WEAK_SIGNATURE_ALGORITHM:
124 details = l10n_util::GetStringFUTF16(
125 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DETAILS,
126 UTF8ToUTF16(request_url.host()));
127 short_description = l10n_util::GetStringUTF16(
128 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DESCRIPTION);
129 break;
130 case CERT_WEAK_KEY:
131 details = l10n_util::GetStringFUTF16(
132 IDS_CERT_ERROR_WEAK_KEY_DETAILS, UTF8ToUTF16(request_url.host()));
133 short_description = l10n_util::GetStringUTF16(
134 IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
135 break;
136 case CERT_WEAK_KEY_DH:
137 details = l10n_util::GetStringFUTF16(
138 IDS_CERT_ERROR_WEAK_KEY_DETAILS, UTF8ToUTF16(request_url.host()));
139 short_description = l10n_util::GetStringUTF16(
140 IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
141 case CERT_NAME_CONSTRAINT_VIOLATION:
142 details = l10n_util::GetStringFUTF16(
143 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DETAILS,
144 UTF8ToUTF16(request_url.host()));
145 short_description = l10n_util::GetStringUTF16(
146 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DESCRIPTION);
147 break;
148 case CERT_VALIDITY_TOO_LONG:
149 details =
150 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_VALIDITY_TOO_LONG_DETAILS,
151 UTF8ToUTF16(request_url.host()));
152 short_description = l10n_util::GetStringUTF16(
153 IDS_CERT_ERROR_VALIDITY_TOO_LONG_DESCRIPTION);
154 break;
155 case CERT_PINNED_KEY_MISSING:
156 details = l10n_util::GetStringUTF16(
157 IDS_ERRORPAGES_SUMMARY_PINNING_FAILURE);
158 short_description = l10n_util::GetStringUTF16(
159 IDS_ERRORPAGES_DETAILS_PINNING_FAILURE);
160 break;
161 case UNKNOWN:
162 details = l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DETAILS);
163 short_description =
164 l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DESCRIPTION);
165 break;
166 case CERT_UNABLE_TO_CHECK_REVOCATION: // Deprecated.
167 default:
168 NOTREACHED();
170 return SSLErrorInfo(details, short_description);
173 SSLErrorInfo::~SSLErrorInfo() {
176 // static
177 SSLErrorInfo::ErrorType SSLErrorInfo::NetErrorToErrorType(int net_error) {
178 switch (net_error) {
179 case net::ERR_CERT_COMMON_NAME_INVALID:
180 return CERT_COMMON_NAME_INVALID;
181 case net::ERR_CERT_DATE_INVALID:
182 return CERT_DATE_INVALID;
183 case net::ERR_CERT_AUTHORITY_INVALID:
184 return CERT_AUTHORITY_INVALID;
185 case net::ERR_CERT_CONTAINS_ERRORS:
186 return CERT_CONTAINS_ERRORS;
187 case net::ERR_CERT_NO_REVOCATION_MECHANISM:
188 return CERT_NO_REVOCATION_MECHANISM;
189 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
190 return CERT_UNABLE_TO_CHECK_REVOCATION;
191 case net::ERR_CERT_REVOKED:
192 return CERT_REVOKED;
193 case net::ERR_CERT_INVALID:
194 return CERT_INVALID;
195 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
196 return CERT_WEAK_SIGNATURE_ALGORITHM;
197 case net::ERR_CERT_WEAK_KEY:
198 return CERT_WEAK_KEY;
199 case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION:
200 return CERT_NAME_CONSTRAINT_VIOLATION;
201 case net::ERR_CERT_VALIDITY_TOO_LONG:
202 return CERT_VALIDITY_TOO_LONG;
203 case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY:
204 return CERT_WEAK_KEY_DH;
205 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
206 return CERT_PINNED_KEY_MISSING;
207 default:
208 NOTREACHED();
209 return UNKNOWN;
213 // static
214 void SSLErrorInfo::GetErrorsForCertStatus(int cert_id,
215 net::CertStatus cert_status,
216 const GURL& url,
217 std::vector<SSLErrorInfo>* errors) {
218 const net::CertStatus kErrorFlags[] = {
219 net::CERT_STATUS_COMMON_NAME_INVALID,
220 net::CERT_STATUS_DATE_INVALID,
221 net::CERT_STATUS_AUTHORITY_INVALID,
222 net::CERT_STATUS_NO_REVOCATION_MECHANISM,
223 net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION,
224 net::CERT_STATUS_REVOKED,
225 net::CERT_STATUS_INVALID,
226 net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM,
227 net::CERT_STATUS_WEAK_KEY,
228 net::CERT_STATUS_NAME_CONSTRAINT_VIOLATION,
229 net::CERT_STATUS_VALIDITY_TOO_LONG,
232 const ErrorType kErrorTypes[] = {
233 CERT_COMMON_NAME_INVALID,
234 CERT_DATE_INVALID,
235 CERT_AUTHORITY_INVALID,
236 CERT_NO_REVOCATION_MECHANISM,
237 CERT_UNABLE_TO_CHECK_REVOCATION,
238 CERT_REVOKED,
239 CERT_INVALID,
240 CERT_WEAK_SIGNATURE_ALGORITHM,
241 CERT_WEAK_KEY,
242 CERT_NAME_CONSTRAINT_VIOLATION,
243 CERT_VALIDITY_TOO_LONG,
245 DCHECK(arraysize(kErrorFlags) == arraysize(kErrorTypes));
247 scoped_refptr<net::X509Certificate> cert = NULL;
248 for (size_t i = 0; i < arraysize(kErrorFlags); ++i) {
249 if (cert_status & kErrorFlags[i]) {
250 if (!cert.get()) {
251 bool r = content::CertStore::GetInstance()->RetrieveCert(
252 cert_id, &cert);
253 DCHECK(r);
255 if (errors) {
256 errors->push_back(
257 SSLErrorInfo::CreateError(kErrorTypes[i], cert.get(), url));