blob: b39c030eee8032eadeea65bbb0e856e883ec4890 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2015 The Chromium Authors
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
Arthur Sonzognib948e672024-07-31 08:29:045#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
7#pragma allow_unsafe_buffers
8#endif
9
felt2493b4452015-09-17 20:33:5910#include "components/ssl_errors/error_info.h"
initial.commit09911bf2008-07-26 23:55:2911
avif57136c12015-12-25 23:27:4512#include <stddef.h>
13
jshin8b581d82015-08-07 10:11:0914#include "base/i18n/message_formatter.h"
Hans Wennborg5bafbb92020-06-18 09:13:5715#include "base/notreached.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3916#include "base/strings/escape.h"
[email protected]e309f312013-06-07 21:50:0817#include "base/strings/utf_string_conversions.h"
thakisfe8fa0a2017-02-23 19:46:3618#include "components/strings/grit/components_strings.h"
initial.commit09911bf2008-07-26 23:55:2919#include "net/base/net_errors.h"
[email protected]6e7845ae2013-03-29 21:48:1120#include "net/cert/cert_status_flags.h"
[email protected]536fd0b2013-03-14 17:41:5721#include "net/ssl/ssl_info.h"
[email protected]c051a1b2011-01-21 23:30:1722#include "ui/base/l10n/l10n_util.h"
[email protected]761fa4702013-07-02 15:25:1523#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2924
[email protected]f911df52013-12-24 23:24:2325using base::UTF8ToUTF16;
26
felt2493b4452015-09-17 20:33:5927namespace ssl_errors {
28
Jan Wilken Dörriefa241ba2021-03-11 17:57:0129ErrorInfo::ErrorInfo(const std::u16string& details,
30 const std::u16string& short_description)
felt2493b4452015-09-17 20:33:5931 : details_(details), short_description_(short_description) {}
initial.commit09911bf2008-07-26 23:55:2932
33// static
felt2493b4452015-09-17 20:33:5934ErrorInfo ErrorInfo::CreateError(ErrorType error_type,
35 net::X509Certificate* cert,
36 const GURL& request_url) {
Jan Wilken Dörriefa241ba2021-03-11 17:57:0137 std::u16string details, short_description;
initial.commit09911bf2008-07-26 23:55:2938 switch (error_type) {
39 case CERT_COMMON_NAME_INVALID: {
initial.commit09911bf2008-07-26 23:55:2940 std::vector<std::string> dns_names;
elawrencec7484f52017-04-05 21:46:4241 cert->GetSubjectAltName(&dns_names, nullptr);
42
initial.commit09911bf2008-07-26 23:55:2943 size_t i = 0;
elawrencec7484f52017-04-05 21:46:4244 if (dns_names.empty()) {
45 // The certificate had no DNS names, display an explanatory string.
elawrence71bba342017-05-17 16:19:4346 details = l10n_util::GetStringFUTF16(
47 IDS_CERT_ERROR_NO_SUBJECT_ALTERNATIVE_NAMES_DETAILS,
48 UTF8ToUTF16(request_url.host()));
elawrencec7484f52017-04-05 21:46:4249 } else {
50 // If the certificate contains multiple DNS names, we choose the most
51 // representative one -- either the DNS name that's also in the subject
52 // field, or the first one. If this heuristic turns out to be
53 // inadequate, we can consider choosing the DNS name that is the
54 // "closest match" to the host name in the request URL, or listing all
55 // the DNS names with an HTML <ul>.
56 for (; i < dns_names.size(); ++i) {
57 if (dns_names[i] == cert->subject().common_name)
58 break;
59 }
60 if (i == dns_names.size())
61 i = 0;
elawrence71bba342017-05-17 16:19:4362
63 details = l10n_util::GetStringFUTF16(
64 IDS_CERT_ERROR_COMMON_NAME_INVALID_DETAILS,
65 UTF8ToUTF16(request_url.host()),
Ryan Hamilton7f3bd3d2022-04-23 00:07:3966 base::EscapeForHTML(UTF8ToUTF16(dns_names[i])));
initial.commit09911bf2008-07-26 23:55:2967 }
elawrencec7484f52017-04-05 21:46:4268
[email protected]42197a22010-12-28 23:29:4269 short_description = l10n_util::GetStringUTF16(
70 IDS_CERT_ERROR_COMMON_NAME_INVALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:2971 break;
72 }
73 case CERT_DATE_INVALID:
initial.commit09911bf2008-07-26 23:55:2974 if (cert->HasExpired()) {
fahl0757a442015-04-24 01:43:0975 // Make sure to round up to the smallest integer value not less than
76 // the expiration value (https://crbug.com/476758).
77 int expiration_value =
78 (base::Time::Now() - cert->valid_expiry()).InDays() + 1;
jshin8b581d82015-08-07 10:11:0979 details = base::i18n::MessageFormatter::FormatWithNumberedArgs(
80 l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXPIRED_DETAILS),
81 request_url.host(), expiration_value, base::Time::Now());
initial.commit09911bf2008-07-26 23:55:2982 short_description =
[email protected]42197a22010-12-28 23:29:4283 l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXPIRED_DESCRIPTION);
[email protected]1511b0d2014-08-19 04:02:1084 } else if (base::Time::Now() < cert->valid_start()) {
jshin8b581d82015-08-07 10:11:0985 details = base::i18n::MessageFormatter::FormatWithNumberedArgs(
86 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DETAILS),
87 request_url.host(),
88 (cert->valid_start() - base::Time::Now()).InDays());
initial.commit09911bf2008-07-26 23:55:2989 short_description =
[email protected]42197a22010-12-28 23:29:4290 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DESCRIPTION);
[email protected]1511b0d2014-08-19 04:02:1091 } else {
92 // Two possibilities: (1) an intermediate or root certificate has
93 // expired, or (2) the certificate has become valid since the error
fahl9d6f4e942015-04-21 03:38:0894 // occurred. Both are probably rare cases. To avoid giving the wrong
95 // date, remove the information.
[email protected]1511b0d2014-08-19 04:02:1096 details = l10n_util::GetStringFUTF16(
fahl9d6f4e942015-04-21 03:38:0897 IDS_CERT_ERROR_NOT_VALID_AT_THIS_TIME_DETAILS,
98 UTF8ToUTF16(request_url.host()));
99 short_description = l10n_util::GetStringUTF16(
100 IDS_CERT_ERROR_NOT_VALID_AT_THIS_TIME_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29101 }
102 break;
Ryan Sleevi54fe7662019-11-21 01:31:58103 case CERT_KNOWN_INTERCEPTION_BLOCKED:
initial.commit09911bf2008-07-26 23:55:29104 case CERT_AUTHORITY_INVALID:
Emily Starkc6ee87482018-02-27 16:00:17105 case CERT_SYMANTEC_LEGACY:
felt2493b4452015-09-17 20:33:59106 details =
107 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_AUTHORITY_INVALID_DETAILS,
108 UTF8ToUTF16(request_url.host()));
[email protected]42197a22010-12-28 23:29:42109 short_description = l10n_util::GetStringUTF16(
110 IDS_CERT_ERROR_AUTHORITY_INVALID_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29111 break;
112 case CERT_CONTAINS_ERRORS:
felt2493b4452015-09-17 20:33:59113 details =
114 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DETAILS,
115 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29116 short_description =
[email protected]42197a22010-12-28 23:29:42117 l10n_util::GetStringUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29118 break;
119 case CERT_NO_REVOCATION_MECHANISM:
Matt Mueller936511442019-09-03 18:15:12120 details = l10n_util::GetStringFUTF16(
121 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DETAILS,
122 UTF8ToUTF16(request_url.host()));
[email protected]42197a22010-12-28 23:29:42123 short_description = l10n_util::GetStringUTF16(
initial.commit09911bf2008-07-26 23:55:29124 IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DESCRIPTION);
125 break;
initial.commit09911bf2008-07-26 23:55:29126 case CERT_REVOKED:
[email protected]42197a22010-12-28 23:29:42127 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_REVOKED_CERT_DETAILS,
128 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29129 short_description =
[email protected]42197a22010-12-28 23:29:42130 l10n_util::GetStringUTF16(IDS_CERT_ERROR_REVOKED_CERT_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29131 break;
132 case CERT_INVALID:
felt2493b4452015-09-17 20:33:59133 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_INVALID_CERT_DETAILS,
134 UTF8ToUTF16(request_url.host()));
initial.commit09911bf2008-07-26 23:55:29135 short_description =
[email protected]42197a22010-12-28 23:29:42136 l10n_util::GetStringUTF16(IDS_CERT_ERROR_INVALID_CERT_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29137 break;
[email protected]0374b292009-08-14 23:49:19138 case CERT_WEAK_SIGNATURE_ALGORITHM:
[email protected]42197a22010-12-28 23:29:42139 details = l10n_util::GetStringFUTF16(
[email protected]0374b292009-08-14 23:49:19140 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DETAILS,
[email protected]42197a22010-12-28 23:29:42141 UTF8ToUTF16(request_url.host()));
142 short_description = l10n_util::GetStringUTF16(
[email protected]0374b292009-08-14 23:49:19143 IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DESCRIPTION);
[email protected]0374b292009-08-14 23:49:19144 break;
[email protected]aaa20bde2011-12-16 23:27:35145 case CERT_WEAK_KEY:
felt2493b4452015-09-17 20:33:59146 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_WEAK_KEY_DETAILS,
147 UTF8ToUTF16(request_url.host()));
148 short_description =
149 l10n_util::GetStringUTF16(IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
[email protected]aaa20bde2011-12-16 23:27:35150 break;
[email protected]f5a393db2013-12-16 18:41:01151 case CERT_NAME_CONSTRAINT_VIOLATION:
[email protected]f5a393db2013-12-16 18:41:01152 details = l10n_util::GetStringFUTF16(
153 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DETAILS,
154 UTF8ToUTF16(request_url.host()));
155 short_description = l10n_util::GetStringUTF16(
156 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DESCRIPTION);
157 break;
palmer4867d6cc2015-01-28 23:05:09158 case CERT_VALIDITY_TOO_LONG:
159 details =
160 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_VALIDITY_TOO_LONG_DETAILS,
161 UTF8ToUTF16(request_url.host()));
162 short_description = l10n_util::GetStringUTF16(
163 IDS_CERT_ERROR_VALIDITY_TOO_LONG_DESCRIPTION);
164 break;
[email protected]6061c142013-10-21 15:13:34165 case CERT_PINNED_KEY_MISSING:
[email protected]6061c142013-10-21 15:13:34166 details = l10n_util::GetStringUTF16(
felt2493b4452015-09-17 20:33:59167 IDS_CERT_ERROR_SUMMARY_PINNING_FAILURE_DETAILS);
[email protected]6061c142013-10-21 15:13:34168 short_description = l10n_util::GetStringUTF16(
felt2493b4452015-09-17 20:33:59169 IDS_CERT_ERROR_SUMMARY_PINNING_FAILURE_DESCRIPTION);
rsleevi4f8012722014-09-30 01:28:01170 break;
feltf752492a2015-09-14 16:17:35171 case CERT_UNABLE_TO_CHECK_REVOCATION:
Matt Mueller936511442019-09-03 18:15:12172 details = l10n_util::GetStringFUTF16(
173 IDS_CERT_ERROR_UNABLE_TO_CHECK_REVOCATION_DETAILS,
174 UTF8ToUTF16(request_url.host()));
feltf752492a2015-09-14 16:17:35175 short_description = l10n_util::GetStringUTF16(
176 IDS_CERT_ERROR_UNABLE_TO_CHECK_REVOCATION_DESCRIPTION);
177 break;
rsleevi41b34612016-06-30 00:18:25178 case CERTIFICATE_TRANSPARENCY_REQUIRED:
179 details = l10n_util::GetStringUTF16(
180 IDS_CERT_ERROR_CERTIFICATE_TRANSPARENCY_REQUIRED_DETAILS);
181 short_description = l10n_util::GetStringUTF16(
182 IDS_CERT_ERROR_CERTIFICATE_TRANSPARENCY_REQUIRED_DESCRIPTION);
183 break;
Emily Stark25f01b82024-04-19 00:04:50184 case CERT_NON_UNIQUE_NAME:
185 details =
186 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_NON_UNIQUE_NAME_DETAILS,
187 UTF8ToUTF16(request_url.host()));
188 short_description =
189 l10n_util::GetStringUTF16(IDS_CERT_ERROR_NON_UNIQUE_NAME_DESCRIPTION);
190 break;
initial.commit09911bf2008-07-26 23:55:29191 case UNKNOWN:
[email protected]42197a22010-12-28 23:29:42192 details = l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DETAILS);
initial.commit09911bf2008-07-26 23:55:29193 short_description =
[email protected]42197a22010-12-28 23:29:42194 l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29195 break;
196 default:
Peter Boströmaaf19db2024-05-14 22:08:09197 NOTREACHED_IN_MIGRATION();
initial.commit09911bf2008-07-26 23:55:29198 }
felt2493b4452015-09-17 20:33:59199 return ErrorInfo(details, short_description);
initial.commit09911bf2008-07-26 23:55:29200}
201
Sorin Jianuaa967bc2024-10-09 04:44:37202ErrorInfo::~ErrorInfo() = default;
initial.commit09911bf2008-07-26 23:55:29203
204// static
felt2493b4452015-09-17 20:33:59205ErrorInfo::ErrorType ErrorInfo::NetErrorToErrorType(int net_error) {
initial.commit09911bf2008-07-26 23:55:29206 switch (net_error) {
207 case net::ERR_CERT_COMMON_NAME_INVALID:
208 return CERT_COMMON_NAME_INVALID;
209 case net::ERR_CERT_DATE_INVALID:
210 return CERT_DATE_INVALID;
211 case net::ERR_CERT_AUTHORITY_INVALID:
212 return CERT_AUTHORITY_INVALID;
213 case net::ERR_CERT_CONTAINS_ERRORS:
214 return CERT_CONTAINS_ERRORS;
215 case net::ERR_CERT_NO_REVOCATION_MECHANISM:
216 return CERT_NO_REVOCATION_MECHANISM;
217 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
218 return CERT_UNABLE_TO_CHECK_REVOCATION;
219 case net::ERR_CERT_REVOKED:
220 return CERT_REVOKED;
221 case net::ERR_CERT_INVALID:
222 return CERT_INVALID;
[email protected]0374b292009-08-14 23:49:19223 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
224 return CERT_WEAK_SIGNATURE_ALGORITHM;
Emily Stark25f01b82024-04-19 00:04:50225 case net::ERR_CERT_NON_UNIQUE_NAME:
226 return CERT_NON_UNIQUE_NAME;
[email protected]aaa20bde2011-12-16 23:27:35227 case net::ERR_CERT_WEAK_KEY:
228 return CERT_WEAK_KEY;
[email protected]f5a393db2013-12-16 18:41:01229 case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION:
230 return CERT_NAME_CONSTRAINT_VIOLATION;
palmer4867d6cc2015-01-28 23:05:09231 case net::ERR_CERT_VALIDITY_TOO_LONG:
232 return CERT_VALIDITY_TOO_LONG;
[email protected]6061c142013-10-21 15:13:34233 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
234 return CERT_PINNED_KEY_MISSING;
rsleevi41b34612016-06-30 00:18:25235 case net::ERR_CERTIFICATE_TRANSPARENCY_REQUIRED:
236 return CERTIFICATE_TRANSPARENCY_REQUIRED;
Emily Starkc6ee87482018-02-27 16:00:17237 case net::ERR_CERT_SYMANTEC_LEGACY:
238 return CERT_SYMANTEC_LEGACY;
Ryan Sleevi54fe7662019-11-21 01:31:58239 case net::ERR_CERT_KNOWN_INTERCEPTION_BLOCKED:
240 return CERT_KNOWN_INTERCEPTION_BLOCKED;
initial.commit09911bf2008-07-26 23:55:29241 default:
Peter Boströmaaf19db2024-05-14 22:08:09242 NOTREACHED_IN_MIGRATION();
initial.commit09911bf2008-07-26 23:55:29243 return UNKNOWN;
felt2493b4452015-09-17 20:33:59244 }
initial.commit09911bf2008-07-26 23:55:29245}
246
247// static
felt2493b4452015-09-17 20:33:59248void ErrorInfo::GetErrorsForCertStatus(
249 const scoped_refptr<net::X509Certificate>& cert,
250 net::CertStatus cert_status,
251 const GURL& url,
252 std::vector<ErrorInfo>* errors) {
[email protected]70d66502011-09-23 00:55:08253 const net::CertStatus kErrorFlags[] = {
palmer4867d6cc2015-01-28 23:05:09254 net::CERT_STATUS_COMMON_NAME_INVALID,
255 net::CERT_STATUS_DATE_INVALID,
256 net::CERT_STATUS_AUTHORITY_INVALID,
257 net::CERT_STATUS_NO_REVOCATION_MECHANISM,
258 net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION,
259 net::CERT_STATUS_REVOKED,
260 net::CERT_STATUS_INVALID,
261 net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM,
Emily Stark25f01b82024-04-19 00:04:50262 net::CERT_STATUS_NON_UNIQUE_NAME,
palmer4867d6cc2015-01-28 23:05:09263 net::CERT_STATUS_WEAK_KEY,
264 net::CERT_STATUS_NAME_CONSTRAINT_VIOLATION,
265 net::CERT_STATUS_VALIDITY_TOO_LONG,
rsleevi41b34612016-06-30 00:18:25266 net::CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED,
Emily Starkc6ee87482018-02-27 16:00:17267 net::CERT_STATUS_SYMANTEC_LEGACY,
Ryan Sleevi54fe7662019-11-21 01:31:58268 net::CERT_STATUS_KNOWN_INTERCEPTION_BLOCKED,
initial.commit09911bf2008-07-26 23:55:29269 };
270
271 const ErrorType kErrorTypes[] = {
palmer4867d6cc2015-01-28 23:05:09272 CERT_COMMON_NAME_INVALID,
273 CERT_DATE_INVALID,
274 CERT_AUTHORITY_INVALID,
275 CERT_NO_REVOCATION_MECHANISM,
276 CERT_UNABLE_TO_CHECK_REVOCATION,
277 CERT_REVOKED,
278 CERT_INVALID,
279 CERT_WEAK_SIGNATURE_ALGORITHM,
Emily Stark25f01b82024-04-19 00:04:50280 CERT_NON_UNIQUE_NAME,
palmer4867d6cc2015-01-28 23:05:09281 CERT_WEAK_KEY,
282 CERT_NAME_CONSTRAINT_VIOLATION,
283 CERT_VALIDITY_TOO_LONG,
rsleevi41b34612016-06-30 00:18:25284 CERTIFICATE_TRANSPARENCY_REQUIRED,
Emily Starkc6ee87482018-02-27 16:00:17285 CERT_SYMANTEC_LEGACY,
Ryan Sleevi54fe7662019-11-21 01:31:58286 CERT_KNOWN_INTERCEPTION_BLOCKED,
initial.commit09911bf2008-07-26 23:55:29287 };
Daniel Cheng7b7aaecc2022-02-26 17:57:25288 DCHECK(std::size(kErrorFlags) == std::size(kErrorTypes));
initial.commit09911bf2008-07-26 23:55:29289
Daniel Cheng7b7aaecc2022-02-26 17:57:25290 for (size_t i = 0; i < std::size(kErrorFlags); ++i) {
felt2493b4452015-09-17 20:33:59291 if ((cert_status & kErrorFlags[i]) && errors) {
292 errors->push_back(
293 ErrorInfo::CreateError(kErrorTypes[i], cert.get(), url));
initial.commit09911bf2008-07-26 23:55:29294 }
295 }
initial.commit09911bf2008-07-26 23:55:29296}
felt2493b4452015-09-17 20:33:59297
298} // namespace ssl_errors