blob: 573784ecfe2ec6d18aef8659bcb9ce89f9188983 [file] [log] [blame]
[email protected]09581d12012-02-27 05:12:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]3b073b22009-01-16 03:29:035#include "chrome/browser/ssl/ssl_error_info.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]09581d12012-02-27 05:12:477#include "base/i18n/time_formatting.h"
[email protected]903df2b2014-07-23 06:38:198#include "base/strings/string_number_conversions.h"
[email protected]e309f312013-06-07 21:50:089#include "base/strings/utf_string_conversions.h"
[email protected]b59c6cf02012-03-12 20:51:4210#include "content/public/browser/cert_store.h"
[email protected]34ac8f32009-02-22 23:03:2711#include "grit/chromium_strings.h"
12#include "grit/generated_resources.h"
[email protected]68b65022012-08-18 01:58:4213#include "net/base/escape.h"
initial.commit09911bf2008-07-26 23:55:2914#include "net/base/net_errors.h"
[email protected]6e7845ae2013-03-29 21:48:1115#include "net/cert/cert_status_flags.h"
[email protected]536fd0b2013-03-14 17:41:5716#include "net/ssl/ssl_info.h"
[email protected]c051a1b2011-01-21 23:30:1717#include "ui/base/l10n/l10n_util.h"
[email protected]761fa4702013-07-02 15:25:1518#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]f911df52013-12-24 23:24:2320using base::UTF8ToUTF16;
21
[email protected]4c5f2b92014-08-09 06:32:2422SSLErrorInfo::SSLErrorInfo(const base::string16& details,
23 const base::string16& short_description)
24 : details_(details),
25 short_description_(short_description) {
initial.commit09911bf2008-07-26 23:55:2926}
27
28// static
29SSLErrorInfo SSLErrorInfo::CreateError(ErrorType error_type,
[email protected]8ac1a752008-07-31 19:40:3730 net::X509Certificate* cert,
initial.commit09911bf2008-07-26 23:55:2931 const GURL& request_url) {
[email protected]4c5f2b92014-08-09 06:32:2432 base::string16 details, short_description;
initial.commit09911bf2008-07-26 23:55:2933 switch (error_type) {
34 case CERT_COMMON_NAME_INVALID: {
initial.commit09911bf2008-07-26 23:55:2935 // 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;
48 }
49 if (i == dns_names.size())
50 i = 0;
51 details =
[email protected]42197a22010-12-28 23:29:4252 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_COMMON_NAME_INVALID_DETAILS,
53 UTF8ToUTF16(request_url.host()),
[email protected]68b65022012-08-18 01:58:4254 net::EscapeForHTML(
[email protected]903df2b2014-07-23 06:38:1955 UTF8ToUTF16(dns_names[i])));
[email protected]42197a22010-12-28 23:29:4256 short_description = l10n_util::GetStringUTF16(
57 IDS_CERT_ERROR_COMMON_NAME_INVALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2958 break;
59 }
60 case CERT_DATE_INVALID:
initial.commit09911bf2008-07-26 23:55:2961 if (cert->HasExpired()) {
[email protected]09581d12012-02-27 05:12:4762 details = l10n_util::GetStringFUTF16(
63 IDS_CERT_ERROR_EXPIRED_DETAILS,
64 UTF8ToUTF16(request_url.host()),
[email protected]903df2b2014-07-23 06:38:1965 base::IntToString16(
66 (base::Time::Now() - cert->valid_expiry()).InDays()),
67 base::TimeFormatFriendlyDate(base::Time::Now()));
initial.commit09911bf2008-07-26 23:55:2968 short_description =
[email protected]42197a22010-12-28 23:29:4269 l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXPIRED_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2970 } else {
71 // Then it must be not yet valid. We don't check that it is not yet
72 // valid as there is still a very unlikely chance that the cert might
73 // have become valid since the error occurred.
[email protected]42197a22010-12-28 23:29:4274 details = l10n_util::GetStringFUTF16(
75 IDS_CERT_ERROR_NOT_YET_VALID_DETAILS,
76 UTF8ToUTF16(request_url.host()),
[email protected]903df2b2014-07-23 06:38:1977 base::IntToString16(
78 (cert->valid_start() - base::Time::Now()).InDays()));
initial.commit09911bf2008-07-26 23:55:2979 short_description =
[email protected]42197a22010-12-28 23:29:4280 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2981 }
82 break;
83 case CERT_AUTHORITY_INVALID:
[email protected]42197a22010-12-28 23:29:4284 details = l10n_util::GetStringFUTF16(
85 IDS_CERT_ERROR_AUTHORITY_INVALID_DETAILS,
86 UTF8ToUTF16(request_url.host()));
87 short_description = l10n_util::GetStringUTF16(
88 IDS_CERT_ERROR_AUTHORITY_INVALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2989 break;
90 case CERT_CONTAINS_ERRORS:
[email protected]42197a22010-12-28 23:29:4291 details = l10n_util::GetStringFUTF16(
92 IDS_CERT_ERROR_CONTAINS_ERRORS_DETAILS,
93 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:2994 short_description =
[email protected]42197a22010-12-28 23:29:4295 l10n_util::GetStringUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2996 break;
97 case CERT_NO_REVOCATION_MECHANISM:
[email protected]42197a22010-12-28 23:29:4298 details = l10n_util::GetStringUTF16(
99 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DETAILS);
100 short_description = l10n_util::GetStringUTF16(
initial.commit09911bf2008-07-26 23:55:29101 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DESCRIPTION);
102 break;
initial.commit09911bf2008-07-26 23:55:29103 case CERT_REVOKED:
[email protected]42197a22010-12-28 23:29:42104 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_REVOKED_CERT_DETAILS,
105 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29106 short_description =
[email protected]42197a22010-12-28 23:29:42107 l10n_util::GetStringUTF16(IDS_CERT_ERROR_REVOKED_CERT_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29108 break;
109 case CERT_INVALID:
[email protected]eb8a06d2011-03-04 04:40:38110 details = l10n_util::GetStringFUTF16(
111 IDS_CERT_ERROR_INVALID_CERT_DETAILS,
112 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29113 short_description =
[email protected]42197a22010-12-28 23:29:42114 l10n_util::GetStringUTF16(IDS_CERT_ERROR_INVALID_CERT_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29115 break;
[email protected]0374b292009-08-14 23:49:19116 case CERT_WEAK_SIGNATURE_ALGORITHM:
[email protected]42197a22010-12-28 23:29:42117 details = l10n_util::GetStringFUTF16(
[email protected]0374b292009-08-14 23:49:19118 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DETAILS,
[email protected]42197a22010-12-28 23:29:42119 UTF8ToUTF16(request_url.host()));
120 short_description = l10n_util::GetStringUTF16(
[email protected]0374b292009-08-14 23:49:19121 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DESCRIPTION);
[email protected]0374b292009-08-14 23:49:19122 break;
[email protected]aaa20bde2011-12-16 23:27:35123 case CERT_WEAK_KEY:
[email protected]aaa20bde2011-12-16 23:27:35124 details = l10n_util::GetStringFUTF16(
125 IDS_CERT_ERROR_WEAK_KEY_DETAILS, UTF8ToUTF16(request_url.host()));
126 short_description = l10n_util::GetStringUTF16(
127 IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
[email protected]aaa20bde2011-12-16 23:27:35128 break;
[email protected]6061c142013-10-21 15:13:34129 case CERT_WEAK_KEY_DH:
[email protected]6061c142013-10-21 15:13:34130 details = l10n_util::GetStringFUTF16(
131 IDS_CERT_ERROR_WEAK_KEY_DETAILS, UTF8ToUTF16(request_url.host()));
132 short_description = l10n_util::GetStringUTF16(
133 IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
[email protected]f5a393db2013-12-16 18:41:01134 case CERT_NAME_CONSTRAINT_VIOLATION:
[email protected]f5a393db2013-12-16 18:41:01135 details = l10n_util::GetStringFUTF16(
136 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DETAILS,
137 UTF8ToUTF16(request_url.host()));
138 short_description = l10n_util::GetStringUTF16(
139 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DESCRIPTION);
140 break;
[email protected]6061c142013-10-21 15:13:34141 case CERT_PINNED_KEY_MISSING:
[email protected]6061c142013-10-21 15:13:34142 details = l10n_util::GetStringUTF16(
143 IDS_ERRORPAGES_SUMMARY_PINNING_FAILURE);
144 short_description = l10n_util::GetStringUTF16(
145 IDS_ERRORPAGES_DETAILS_PINNING_FAILURE);
initial.commit09911bf2008-07-26 23:55:29146 case UNKNOWN:
[email protected]42197a22010-12-28 23:29:42147 details = l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DETAILS);
initial.commit09911bf2008-07-26 23:55:29148 short_description =
[email protected]42197a22010-12-28 23:29:42149 l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29150 break;
[email protected]4c5f2b92014-08-09 06:32:24151 case CERT_UNABLE_TO_CHECK_REVOCATION: // Deprecated.
initial.commit09911bf2008-07-26 23:55:29152 default:
153 NOTREACHED();
154 }
[email protected]4c5f2b92014-08-09 06:32:24155 return SSLErrorInfo(details, short_description);
initial.commit09911bf2008-07-26 23:55:29156}
157
158SSLErrorInfo::~SSLErrorInfo() {
159}
160
161// static
162SSLErrorInfo::ErrorType SSLErrorInfo::NetErrorToErrorType(int net_error) {
163 switch (net_error) {
164 case net::ERR_CERT_COMMON_NAME_INVALID:
165 return CERT_COMMON_NAME_INVALID;
166 case net::ERR_CERT_DATE_INVALID:
167 return CERT_DATE_INVALID;
168 case net::ERR_CERT_AUTHORITY_INVALID:
169 return CERT_AUTHORITY_INVALID;
170 case net::ERR_CERT_CONTAINS_ERRORS:
171 return CERT_CONTAINS_ERRORS;
172 case net::ERR_CERT_NO_REVOCATION_MECHANISM:
173 return CERT_NO_REVOCATION_MECHANISM;
174 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
175 return CERT_UNABLE_TO_CHECK_REVOCATION;
176 case net::ERR_CERT_REVOKED:
177 return CERT_REVOKED;
178 case net::ERR_CERT_INVALID:
179 return CERT_INVALID;
[email protected]0374b292009-08-14 23:49:19180 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
181 return CERT_WEAK_SIGNATURE_ALGORITHM;
[email protected]aaa20bde2011-12-16 23:27:35182 case net::ERR_CERT_WEAK_KEY:
183 return CERT_WEAK_KEY;
[email protected]f5a393db2013-12-16 18:41:01184 case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION:
185 return CERT_NAME_CONSTRAINT_VIOLATION;
[email protected]6061c142013-10-21 15:13:34186 case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY:
187 return CERT_WEAK_KEY_DH;
188 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
189 return CERT_PINNED_KEY_MISSING;
initial.commit09911bf2008-07-26 23:55:29190 default:
191 NOTREACHED();
192 return UNKNOWN;
193 }
194}
195
196// static
197int SSLErrorInfo::GetErrorsForCertStatus(int cert_id,
[email protected]70d66502011-09-23 00:55:08198 net::CertStatus cert_status,
initial.commit09911bf2008-07-26 23:55:29199 const GURL& url,
200 std::vector<SSLErrorInfo>* errors) {
[email protected]70d66502011-09-23 00:55:08201 const net::CertStatus kErrorFlags[] = {
initial.commit09911bf2008-07-26 23:55:29202 net::CERT_STATUS_COMMON_NAME_INVALID,
203 net::CERT_STATUS_DATE_INVALID,
204 net::CERT_STATUS_AUTHORITY_INVALID,
205 net::CERT_STATUS_NO_REVOCATION_MECHANISM,
206 net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION,
207 net::CERT_STATUS_REVOKED,
[email protected]0374b292009-08-14 23:49:19208 net::CERT_STATUS_INVALID,
[email protected]aaa20bde2011-12-16 23:27:35209 net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM,
[email protected]f5a393db2013-12-16 18:41:01210 net::CERT_STATUS_WEAK_KEY,
211 net::CERT_STATUS_NAME_CONSTRAINT_VIOLATION,
initial.commit09911bf2008-07-26 23:55:29212 };
213
214 const ErrorType kErrorTypes[] = {
215 CERT_COMMON_NAME_INVALID,
216 CERT_DATE_INVALID,
217 CERT_AUTHORITY_INVALID,
218 CERT_NO_REVOCATION_MECHANISM,
219 CERT_UNABLE_TO_CHECK_REVOCATION,
220 CERT_REVOKED,
[email protected]0374b292009-08-14 23:49:19221 CERT_INVALID,
[email protected]aaa20bde2011-12-16 23:27:35222 CERT_WEAK_SIGNATURE_ALGORITHM,
[email protected]f5a393db2013-12-16 18:41:01223 CERT_WEAK_KEY,
224 CERT_NAME_CONSTRAINT_VIOLATION,
initial.commit09911bf2008-07-26 23:55:29225 };
226 DCHECK(arraysize(kErrorFlags) == arraysize(kErrorTypes));
227
[email protected]8ac1a752008-07-31 19:40:37228 scoped_refptr<net::X509Certificate> cert = NULL;
initial.commit09911bf2008-07-26 23:55:29229 int count = 0;
[email protected]85e0f1f2008-12-17 18:30:28230 for (size_t i = 0; i < arraysize(kErrorFlags); ++i) {
initial.commit09911bf2008-07-26 23:55:29231 if (cert_status & kErrorFlags[i]) {
232 count++;
233 if (!cert.get()) {
[email protected]b59c6cf02012-03-12 20:51:42234 bool r = content::CertStore::GetInstance()->RetrieveCert(
235 cert_id, &cert);
initial.commit09911bf2008-07-26 23:55:29236 DCHECK(r);
237 }
238 if (errors)
[email protected]39790b7f2013-06-03 00:10:59239 errors->push_back(
240 SSLErrorInfo::CreateError(kErrorTypes[i], cert.get(), url));
initial.commit09911bf2008-07-26 23:55:29241 }
242 }
243 return count;
244}