blob: 6eaaee0536b2d01029f7084721889dbd897d8b8a [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]6061c142013-10-21 15:13:34134 case CERT_WEAK_KEY_DH:
felt2493b4452015-09-17 20:33:59135 details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_WEAK_KEY_DETAILS,
136 UTF8ToUTF16(request_url.host()));
137 short_description =
138 l10n_util::GetStringUTF16(IDS_CERT_ERROR_WEAK_KEY_DESCRIPTION);
[email protected]f5a393db2013-12-16 18:41:01139 case CERT_NAME_CONSTRAINT_VIOLATION:
[email protected]f5a393db2013-12-16 18:41:01140 details = l10n_util::GetStringFUTF16(
141 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DETAILS,
142 UTF8ToUTF16(request_url.host()));
143 short_description = l10n_util::GetStringUTF16(
144 IDS_CERT_ERROR_NAME_CONSTRAINT_VIOLATION_DESCRIPTION);
145 break;
palmer4867d6cc2015-01-28 23:05:09146 case CERT_VALIDITY_TOO_LONG:
147 details =
148 l10n_util::GetStringFUTF16(IDS_CERT_ERROR_VALIDITY_TOO_LONG_DETAILS,
149 UTF8ToUTF16(request_url.host()));
150 short_description = l10n_util::GetStringUTF16(
151 IDS_CERT_ERROR_VALIDITY_TOO_LONG_DESCRIPTION);
152 break;
[email protected]6061c142013-10-21 15:13:34153 case CERT_PINNED_KEY_MISSING:
[email protected]6061c142013-10-21 15:13:34154 details = l10n_util::GetStringUTF16(
felt2493b4452015-09-17 20:33:59155 IDS_CERT_ERROR_SUMMARY_PINNING_FAILURE_DETAILS);
[email protected]6061c142013-10-21 15:13:34156 short_description = l10n_util::GetStringUTF16(
felt2493b4452015-09-17 20:33:59157 IDS_CERT_ERROR_SUMMARY_PINNING_FAILURE_DESCRIPTION);
rsleevi4f8012722014-09-30 01:28:01158 break;
feltf752492a2015-09-14 16:17:35159 case CERT_UNABLE_TO_CHECK_REVOCATION:
160 details = l10n_util::GetStringUTF16(
161 IDS_CERT_ERROR_UNABLE_TO_CHECK_REVOCATION_DETAILS);
162 short_description = l10n_util::GetStringUTF16(
163 IDS_CERT_ERROR_UNABLE_TO_CHECK_REVOCATION_DESCRIPTION);
164 break;
initial.commit09911bf2008-07-26 23:55:29165 case UNKNOWN:
[email protected]42197a22010-12-28 23:29:42166 details = l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DETAILS);
initial.commit09911bf2008-07-26 23:55:29167 short_description =
[email protected]42197a22010-12-28 23:29:42168 l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DESCRIPTION);
initial.commit09911bf2008-07-26 23:55:29169 break;
initial.commit09911bf2008-07-26 23:55:29170 default:
171 NOTREACHED();
172 }
felt2493b4452015-09-17 20:33:59173 return ErrorInfo(details, short_description);
initial.commit09911bf2008-07-26 23:55:29174}
175
felt2493b4452015-09-17 20:33:59176ErrorInfo::~ErrorInfo() {}
initial.commit09911bf2008-07-26 23:55:29177
178// static
felt2493b4452015-09-17 20:33:59179ErrorInfo::ErrorType ErrorInfo::NetErrorToErrorType(int net_error) {
initial.commit09911bf2008-07-26 23:55:29180 switch (net_error) {
181 case net::ERR_CERT_COMMON_NAME_INVALID:
182 return CERT_COMMON_NAME_INVALID;
183 case net::ERR_CERT_DATE_INVALID:
184 return CERT_DATE_INVALID;
185 case net::ERR_CERT_AUTHORITY_INVALID:
186 return CERT_AUTHORITY_INVALID;
187 case net::ERR_CERT_CONTAINS_ERRORS:
188 return CERT_CONTAINS_ERRORS;
189 case net::ERR_CERT_NO_REVOCATION_MECHANISM:
190 return CERT_NO_REVOCATION_MECHANISM;
191 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
192 return CERT_UNABLE_TO_CHECK_REVOCATION;
193 case net::ERR_CERT_REVOKED:
194 return CERT_REVOKED;
195 case net::ERR_CERT_INVALID:
196 return CERT_INVALID;
[email protected]0374b292009-08-14 23:49:19197 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
198 return CERT_WEAK_SIGNATURE_ALGORITHM;
[email protected]aaa20bde2011-12-16 23:27:35199 case net::ERR_CERT_WEAK_KEY:
200 return CERT_WEAK_KEY;
[email protected]f5a393db2013-12-16 18:41:01201 case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION:
202 return CERT_NAME_CONSTRAINT_VIOLATION;
palmer4867d6cc2015-01-28 23:05:09203 case net::ERR_CERT_VALIDITY_TOO_LONG:
204 return CERT_VALIDITY_TOO_LONG;
[email protected]6061c142013-10-21 15:13:34205 case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY:
206 return CERT_WEAK_KEY_DH;
207 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
208 return CERT_PINNED_KEY_MISSING;
initial.commit09911bf2008-07-26 23:55:29209 default:
210 NOTREACHED();
211 return UNKNOWN;
felt2493b4452015-09-17 20:33:59212 }
initial.commit09911bf2008-07-26 23:55:29213}
214
215// static
felt2493b4452015-09-17 20:33:59216void ErrorInfo::GetErrorsForCertStatus(
217 const scoped_refptr<net::X509Certificate>& cert,
218 net::CertStatus cert_status,
219 const GURL& url,
220 std::vector<ErrorInfo>* errors) {
[email protected]70d66502011-09-23 00:55:08221 const net::CertStatus kErrorFlags[] = {
palmer4867d6cc2015-01-28 23:05:09222 net::CERT_STATUS_COMMON_NAME_INVALID,
223 net::CERT_STATUS_DATE_INVALID,
224 net::CERT_STATUS_AUTHORITY_INVALID,
225 net::CERT_STATUS_NO_REVOCATION_MECHANISM,
226 net::CERT_STATUS_UNABLE_TO_CHECK_REVOCATION,
227 net::CERT_STATUS_REVOKED,
228 net::CERT_STATUS_INVALID,
229 net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM,
230 net::CERT_STATUS_WEAK_KEY,
231 net::CERT_STATUS_NAME_CONSTRAINT_VIOLATION,
232 net::CERT_STATUS_VALIDITY_TOO_LONG,
initial.commit09911bf2008-07-26 23:55:29233 };
234
235 const ErrorType kErrorTypes[] = {
palmer4867d6cc2015-01-28 23:05:09236 CERT_COMMON_NAME_INVALID,
237 CERT_DATE_INVALID,
238 CERT_AUTHORITY_INVALID,
239 CERT_NO_REVOCATION_MECHANISM,
240 CERT_UNABLE_TO_CHECK_REVOCATION,
241 CERT_REVOKED,
242 CERT_INVALID,
243 CERT_WEAK_SIGNATURE_ALGORITHM,
244 CERT_WEAK_KEY,
245 CERT_NAME_CONSTRAINT_VIOLATION,
246 CERT_VALIDITY_TOO_LONG,
initial.commit09911bf2008-07-26 23:55:29247 };
248 DCHECK(arraysize(kErrorFlags) == arraysize(kErrorTypes));
249
[email protected]85e0f1f2008-12-17 18:30:28250 for (size_t i = 0; i < arraysize(kErrorFlags); ++i) {
felt2493b4452015-09-17 20:33:59251 if ((cert_status & kErrorFlags[i]) && errors) {
252 errors->push_back(
253 ErrorInfo::CreateError(kErrorTypes[i], cert.get(), url));
initial.commit09911bf2008-07-26 23:55:29254 }
255 }
initial.commit09911bf2008-07-26 23:55:29256}
felt2493b4452015-09-17 20:33:59257
258} // namespace ssl_errors