blob: a405e1496f1b4af2c99e633d846ab20ff4e136d6 [file] [log] [blame]
felt2493b4452015-09-17 20:33:591// Copyright 2015 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
felt2493b4452015-09-17 20:33:595#include "components/ssl_errors/error_info.h"
initial.commit09911bf2008-07-26 23:55:296
avif57136c12015-12-25 23:27:457#include <stddef.h>
8
jshin8b581d82015-08-07 10:11:099#include "base/i18n/message_formatter.h"
avif57136c12015-12-25 23:27:4510#include "base/macros.h"
[email protected]e309f312013-06-07 21:50:0811#include "base/strings/utf_string_conversions.h"
felt2493b4452015-09-17 20:33:5912#include "grit/components_strings.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
felt2493b4452015-09-17 20:33:5922namespace ssl_errors {
23
24ErrorInfo::ErrorInfo(const base::string16& details,
25 const base::string16& short_description)
26 : details_(details), short_description_(short_description) {}
initial.commit09911bf2008-07-26 23:55:2927
28// static
felt2493b4452015-09-17 20:33:5929ErrorInfo ErrorInfo::CreateError(ErrorType error_type,
30 net::X509Certificate* cert,
31 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;
felt2493b4452015-09-17 20:33:5951 details = l10n_util::GetStringFUTF16(
52 IDS_CERT_ERROR_COMMON_NAME_INVALID_DETAILS,
53 UTF8ToUTF16(request_url.host()),
54 net::EscapeForHTML(UTF8ToUTF16(dns_names[i])));
[email protected]42197a22010-12-28 23:29:4255 short_description = l10n_util::GetStringUTF16(
56 IDS_CERT_ERROR_COMMON_NAME_INVALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2957 break;
58 }
59 case CERT_DATE_INVALID:
initial.commit09911bf2008-07-26 23:55:2960 if (cert->HasExpired()) {
fahl0757a442015-04-24 01:43:0961 // Make sure to round up to the smallest integer value not less than
62 // the expiration value (https://crbug.com/476758).
63 int expiration_value =
64 (base::Time::Now() - cert->valid_expiry()).InDays() + 1;
jshin8b581d82015-08-07 10:11:0965 details = base::i18n::MessageFormatter::FormatWithNumberedArgs(
66 l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXPIRED_DETAILS),
67 request_url.host(), expiration_value, 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);
[email protected]1511b0d2014-08-19 04:02:1070 } else if (base::Time::Now() < cert->valid_start()) {
jshin8b581d82015-08-07 10:11:0971 details = base::i18n::MessageFormatter::FormatWithNumberedArgs(
72 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DETAILS),
73 request_url.host(),
74 (cert->valid_start() - base::Time::Now()).InDays());
initial.commit09911bf2008-07-26 23:55:2975 short_description =
[email protected]42197a22010-12-28 23:29:4276 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DESCRIPTION);
[email protected]1511b0d2014-08-19 04:02:1077 } else {
78 // Two possibilities: (1) an intermediate or root certificate has
79 // expired, or (2) the certificate has become valid since the error
fahl9d6f4e942015-04-21 03:38:0880 // occurred. Both are probably rare cases. To avoid giving the wrong
81 // date, remove the information.
[email protected]1511b0d2014-08-19 04:02:1082 details = l10n_util::GetStringFUTF16(
fahl9d6f4e942015-04-21 03:38:0883 IDS_CERT_ERROR_NOT_VALID_AT_THIS_TIME_DETAILS,
84 UTF8ToUTF16(request_url.host()));
85 short_description = l10n_util::GetStringUTF16(
86 IDS_CERT_ERROR_NOT_VALID_AT_THIS_TIME_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2987 }
88 break;
89 case CERT_AUTHORITY_INVALID:
felt2493b4452015-09-17 20:33:5990 details =
91 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_AUTHORITY_INVALID_DETAILS,
92 UTF8ToUTF16(request_url.host()));
[email protected]42197a22010-12-28 23:29:4293 short_description = l10n_util::GetStringUTF16(
94 IDS_CERT_ERROR_AUTHORITY_INVALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2995 break;
96 case CERT_CONTAINS_ERRORS:
felt2493b4452015-09-17 20:33:5997 details =
98 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DETAILS,
99 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29100 short_description =
[email protected]42197a22010-12-28 23:29:42101 l10n_util::GetStringUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29102 break;
103 case CERT_NO_REVOCATION_MECHANISM:
[email protected]42197a22010-12-28 23:29:42104 details = l10n_util::GetStringUTF16(
105 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DETAILS);
106 short_description = l10n_util::GetStringUTF16(
initial.commit09911bf2008-07-26 23:55:29107 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DESCRIPTION);
108 break;
initial.commit09911bf2008-07-26 23:55:29109 case CERT_REVOKED:
[email protected]42197a22010-12-28 23:29:42110 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_REVOKED_CERT_DETAILS,
111 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29112 short_description =
[email protected]42197a22010-12-28 23:29:42113 l10n_util::GetStringUTF16(IDS_CERT_ERROR_REVOKED_CERT_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29114 break;
115 case CERT_INVALID:
felt2493b4452015-09-17 20:33:59116 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_INVALID_CERT_DETAILS,
117 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29118 short_description =
[email protected]42197a22010-12-28 23:29:42119 l10n_util::GetStringUTF16(IDS_CERT_ERROR_INVALID_CERT_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29120 break;
[email protected]0374b292009-08-14 23:49:19121 case CERT_WEAK_SIGNATURE_ALGORITHM:
[email protected]42197a22010-12-28 23:29:42122 details = l10n_util::GetStringFUTF16(
[email protected]0374b292009-08-14 23:49:19123 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DETAILS,
[email protected]42197a22010-12-28 23:29:42124 UTF8ToUTF16(request_url.host()));
125 short_description = l10n_util::GetStringUTF16(
[email protected]0374b292009-08-14 23:49:19126 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DESCRIPTION);
[email protected]0374b292009-08-14 23:49:19127 break;
[email protected]aaa20bde2011-12-16 23:27:35128 case CERT_WEAK_KEY:
felt2493b4452015-09-17 20:33:59129 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_WEAK_KEY_DETAILS,
130 UTF8ToUTF16(request_url.host()));
131 short_description =
132 l10n_util::GetStringUTF16(IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
[email protected]aaa20bde2011-12-16 23:27:35133 break;
[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;
palmer4867d6cc2015-01-28 23:05:09141 case CERT_VALIDITY_TOO_LONG:
142 details =
143 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_VALIDITY_TOO_LONG_DETAILS,
144 UTF8ToUTF16(request_url.host()));
145 short_description = l10n_util::GetStringUTF16(
146 IDS_CERT_ERROR_VALIDITY_TOO_LONG_DESCRIPTION);
147 break;
[email protected]6061c142013-10-21 15:13:34148 case CERT_PINNED_KEY_MISSING:
[email protected]6061c142013-10-21 15:13:34149 details = l10n_util::GetStringUTF16(
felt2493b4452015-09-17 20:33:59150 IDS_CERT_ERROR_SUMMARY_PINNING_FAILURE_DETAILS);
[email protected]6061c142013-10-21 15:13:34151 short_description = l10n_util::GetStringUTF16(
felt2493b4452015-09-17 20:33:59152 IDS_CERT_ERROR_SUMMARY_PINNING_FAILURE_DESCRIPTION);
rsleevi4f8012722014-09-30 01:28:01153 break;
feltf752492a2015-09-14 16:17:35154 case CERT_UNABLE_TO_CHECK_REVOCATION:
155 details = l10n_util::GetStringUTF16(
156 IDS_CERT_ERROR_UNABLE_TO_CHECK_REVOCATION_DETAILS);
157 short_description = l10n_util::GetStringUTF16(
158 IDS_CERT_ERROR_UNABLE_TO_CHECK_REVOCATION_DESCRIPTION);
159 break;
rsleevi41b34612016-06-30 00:18:25160 case CERTIFICATE_TRANSPARENCY_REQUIRED:
161 details = l10n_util::GetStringUTF16(
162 IDS_CERT_ERROR_CERTIFICATE_TRANSPARENCY_REQUIRED_DETAILS);
163 short_description = l10n_util::GetStringUTF16(
164 IDS_CERT_ERROR_CERTIFICATE_TRANSPARENCY_REQUIRED_DESCRIPTION);
165 break;
initial.commit09911bf2008-07-26 23:55:29166 case UNKNOWN:
[email protected]42197a22010-12-28 23:29:42167 details = l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DETAILS);
initial.commit09911bf2008-07-26 23:55:29168 short_description =
[email protected]42197a22010-12-28 23:29:42169 l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29170 break;
171 default:
172 NOTREACHED();
173 }
felt2493b4452015-09-17 20:33:59174 return ErrorInfo(details, short_description);
initial.commit09911bf2008-07-26 23:55:29175}
176
felt2493b4452015-09-17 20:33:59177ErrorInfo::~ErrorInfo() {}
initial.commit09911bf2008-07-26 23:55:29178
179// static
felt2493b4452015-09-17 20:33:59180ErrorInfo::ErrorType ErrorInfo::NetErrorToErrorType(int net_error) {
initial.commit09911bf2008-07-26 23:55:29181 switch (net_error) {
182 case net::ERR_CERT_COMMON_NAME_INVALID:
183 return CERT_COMMON_NAME_INVALID;
184 case net::ERR_CERT_DATE_INVALID:
185 return CERT_DATE_INVALID;
186 case net::ERR_CERT_AUTHORITY_INVALID:
187 return CERT_AUTHORITY_INVALID;
188 case net::ERR_CERT_CONTAINS_ERRORS:
189 return CERT_CONTAINS_ERRORS;
190 case net::ERR_CERT_NO_REVOCATION_MECHANISM:
191 return CERT_NO_REVOCATION_MECHANISM;
192 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
193 return CERT_UNABLE_TO_CHECK_REVOCATION;
194 case net::ERR_CERT_REVOKED:
195 return CERT_REVOKED;
196 case net::ERR_CERT_INVALID:
197 return CERT_INVALID;
[email protected]0374b292009-08-14 23:49:19198 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
199 return CERT_WEAK_SIGNATURE_ALGORITHM;
[email protected]aaa20bde2011-12-16 23:27:35200 case net::ERR_CERT_WEAK_KEY:
201 return CERT_WEAK_KEY;
[email protected]f5a393db2013-12-16 18:41:01202 case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION:
203 return CERT_NAME_CONSTRAINT_VIOLATION;
palmer4867d6cc2015-01-28 23:05:09204 case net::ERR_CERT_VALIDITY_TOO_LONG:
205 return CERT_VALIDITY_TOO_LONG;
[email protected]6061c142013-10-21 15:13:34206 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
207 return CERT_PINNED_KEY_MISSING;
rsleevi41b34612016-06-30 00:18:25208 case net::ERR_CERTIFICATE_TRANSPARENCY_REQUIRED:
209 return CERTIFICATE_TRANSPARENCY_REQUIRED;
initial.commit09911bf2008-07-26 23:55:29210 default:
211 NOTREACHED();
212 return UNKNOWN;
felt2493b4452015-09-17 20:33:59213 }
initial.commit09911bf2008-07-26 23:55:29214}
215
216// static
felt2493b4452015-09-17 20:33:59217void ErrorInfo::GetErrorsForCertStatus(
218 const scoped_refptr<net::X509Certificate>& cert,
219 net::CertStatus cert_status,
220 const GURL& url,
221 std::vector<ErrorInfo>* errors) {
[email protected]70d66502011-09-23 00:55:08222 const net::CertStatus kErrorFlags[] = {
palmer4867d6cc2015-01-28 23:05:09223 net::CERT_STATUS_COMMON_NAME_INVALID,
224 net::CERT_STATUS_DATE_INVALID,
225 net::CERT_STATUS_AUTHORITY_INVALID,
226 net::CERT_STATUS_NO_REVOCATION_MECHANISM,
227 net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION,
228 net::CERT_STATUS_REVOKED,
229 net::CERT_STATUS_INVALID,
230 net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM,
231 net::CERT_STATUS_WEAK_KEY,
232 net::CERT_STATUS_NAME_CONSTRAINT_VIOLATION,
233 net::CERT_STATUS_VALIDITY_TOO_LONG,
rsleevi41b34612016-06-30 00:18:25234 net::CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED,
initial.commit09911bf2008-07-26 23:55:29235 };
236
237 const ErrorType kErrorTypes[] = {
palmer4867d6cc2015-01-28 23:05:09238 CERT_COMMON_NAME_INVALID,
239 CERT_DATE_INVALID,
240 CERT_AUTHORITY_INVALID,
241 CERT_NO_REVOCATION_MECHANISM,
242 CERT_UNABLE_TO_CHECK_REVOCATION,
243 CERT_REVOKED,
244 CERT_INVALID,
245 CERT_WEAK_SIGNATURE_ALGORITHM,
246 CERT_WEAK_KEY,
247 CERT_NAME_CONSTRAINT_VIOLATION,
248 CERT_VALIDITY_TOO_LONG,
rsleevi41b34612016-06-30 00:18:25249 CERTIFICATE_TRANSPARENCY_REQUIRED,
initial.commit09911bf2008-07-26 23:55:29250 };
251 DCHECK(arraysize(kErrorFlags) == arraysize(kErrorTypes));
252
[email protected]85e0f1f2008-12-17 18:30:28253 for (size_t i = 0; i < arraysize(kErrorFlags); ++i) {
felt2493b4452015-09-17 20:33:59254 if ((cert_status & kErrorFlags[i]) && errors) {
255 errors->push_back(
256 ErrorInfo::CreateError(kErrorTypes[i], cert.get(), url));
initial.commit09911bf2008-07-26 23:55:29257 }
258 }
initial.commit09911bf2008-07-26 23:55:29259}
felt2493b4452015-09-17 20:33:59260
261} // namespace ssl_errors