[email protected] | 09581d1 | 2012-02-27 05:12:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 3b073b2 | 2009-01-16 03:29:03 | [diff] [blame] | 5 | #include "chrome/browser/ssl/ssl_error_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 09581d1 | 2012-02-27 05:12:47 | [diff] [blame] | 7 | #include "base/i18n/time_formatting.h" |
[email protected] | 903df2b | 2014-07-23 06:38:19 | [diff] [blame] | 8 | #include "base/strings/string_number_conversions.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | b59c6cf0 | 2012-03-12 20:51:42 | [diff] [blame] | 10 | #include "content/public/browser/cert_store.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 11 | #include "grit/chromium_strings.h" |
| 12 | #include "grit/generated_resources.h" |
[email protected] | 68b6502 | 2012-08-18 01:58:42 | [diff] [blame] | 13 | #include "net/base/escape.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 15 | #include "net/cert/cert_status_flags.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 16 | #include "net/ssl/ssl_info.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 17 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 18 | #include "url/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 20 | using base::UTF8ToUTF16; |
| 21 | |
[email protected] | 4c5f2b9 | 2014-08-09 06:32:24 | [diff] [blame^] | 22 | SSLErrorInfo::SSLErrorInfo(const base::string16& details, |
| 23 | const base::string16& short_description) |
| 24 | : details_(details), |
| 25 | short_description_(short_description) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // static |
| 29 | SSLErrorInfo SSLErrorInfo::CreateError(ErrorType error_type, |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 30 | net::X509Certificate* cert, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | const GURL& request_url) { |
[email protected] | 4c5f2b9 | 2014-08-09 06:32:24 | [diff] [blame^] | 32 | base::string16 details, short_description; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | switch (error_type) { |
| 34 | case CERT_COMMON_NAME_INVALID: { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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; |
| 48 | } |
| 49 | if (i == dns_names.size()) |
| 50 | i = 0; |
| 51 | details = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 52 | l10n_util::GetStringFUTF16(IDS_CERT_ERROR_COMMON_NAME_INVALID_DETAILS, |
| 53 | UTF8ToUTF16(request_url.host()), |
[email protected] | 68b6502 | 2012-08-18 01:58:42 | [diff] [blame] | 54 | net::EscapeForHTML( |
[email protected] | 903df2b | 2014-07-23 06:38:19 | [diff] [blame] | 55 | UTF8ToUTF16(dns_names[i]))); |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 56 | short_description = l10n_util::GetStringUTF16( |
| 57 | IDS_CERT_ERROR_COMMON_NAME_INVALID_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | break; |
| 59 | } |
| 60 | case CERT_DATE_INVALID: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | if (cert->HasExpired()) { |
[email protected] | 09581d1 | 2012-02-27 05:12:47 | [diff] [blame] | 62 | details = l10n_util::GetStringFUTF16( |
| 63 | IDS_CERT_ERROR_EXPIRED_DETAILS, |
| 64 | UTF8ToUTF16(request_url.host()), |
[email protected] | 903df2b | 2014-07-23 06:38:19 | [diff] [blame] | 65 | base::IntToString16( |
| 66 | (base::Time::Now() - cert->valid_expiry()).InDays()), |
| 67 | base::TimeFormatFriendlyDate(base::Time::Now())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | short_description = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 69 | l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXPIRED_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | } 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] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 74 | details = l10n_util::GetStringFUTF16( |
| 75 | IDS_CERT_ERROR_NOT_YET_VALID_DETAILS, |
| 76 | UTF8ToUTF16(request_url.host()), |
[email protected] | 903df2b | 2014-07-23 06:38:19 | [diff] [blame] | 77 | base::IntToString16( |
| 78 | (cert->valid_start() - base::Time::Now()).InDays())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | short_description = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 80 | l10n_util::GetStringUTF16(IDS_CERT_ERROR_NOT_YET_VALID_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | case CERT_AUTHORITY_INVALID: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 84 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | break; |
| 90 | case CERT_CONTAINS_ERRORS: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 91 | details = l10n_util::GetStringFUTF16( |
| 92 | IDS_CERT_ERROR_CONTAINS_ERRORS_DETAILS, |
| 93 | UTF8ToUTF16(request_url.host())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | short_description = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 95 | l10n_util::GetStringUTF16(IDS_CERT_ERROR_CONTAINS_ERRORS_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | break; |
| 97 | case CERT_NO_REVOCATION_MECHANISM: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 98 | details = l10n_util::GetStringUTF16( |
| 99 | IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DETAILS); |
| 100 | short_description = l10n_util::GetStringUTF16( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | IDS_CERT_ERROR_NO_REVOCATION_MECHANISM_DESCRIPTION); |
| 102 | break; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | case CERT_REVOKED: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 104 | details = l10n_util::GetStringFUTF16(IDS_CERT_ERROR_REVOKED_CERT_DETAILS, |
| 105 | UTF8ToUTF16(request_url.host())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | short_description = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 107 | l10n_util::GetStringUTF16(IDS_CERT_ERROR_REVOKED_CERT_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | break; |
| 109 | case CERT_INVALID: |
[email protected] | eb8a06d | 2011-03-04 04:40:38 | [diff] [blame] | 110 | details = l10n_util::GetStringFUTF16( |
| 111 | IDS_CERT_ERROR_INVALID_CERT_DETAILS, |
| 112 | UTF8ToUTF16(request_url.host())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | short_description = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 114 | l10n_util::GetStringUTF16(IDS_CERT_ERROR_INVALID_CERT_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | break; |
[email protected] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 116 | case CERT_WEAK_SIGNATURE_ALGORITHM: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 117 | details = l10n_util::GetStringFUTF16( |
[email protected] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 118 | IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DETAILS, |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 119 | UTF8ToUTF16(request_url.host())); |
| 120 | short_description = l10n_util::GetStringUTF16( |
[email protected] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 121 | IDS_CERT_ERROR_WEAK_SIGNATURE_ALGORITHM_DESCRIPTION); |
[email protected] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 122 | break; |
[email protected] | aaa20bde | 2011-12-16 23:27:35 | [diff] [blame] | 123 | case CERT_WEAK_KEY: |
[email protected] | aaa20bde | 2011-12-16 23:27:35 | [diff] [blame] | 124 | 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] | aaa20bde | 2011-12-16 23:27:35 | [diff] [blame] | 128 | break; |
[email protected] | 6061c14 | 2013-10-21 15:13:34 | [diff] [blame] | 129 | case CERT_WEAK_KEY_DH: |
[email protected] | 6061c14 | 2013-10-21 15:13:34 | [diff] [blame] | 130 | 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] | f5a393db | 2013-12-16 18:41:01 | [diff] [blame] | 134 | case CERT_NAME_CONSTRAINT_VIOLATION: |
[email protected] | f5a393db | 2013-12-16 18:41:01 | [diff] [blame] | 135 | 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] | 6061c14 | 2013-10-21 15:13:34 | [diff] [blame] | 141 | case CERT_PINNED_KEY_MISSING: |
[email protected] | 6061c14 | 2013-10-21 15:13:34 | [diff] [blame] | 142 | details = l10n_util::GetStringUTF16( |
| 143 | IDS_ERRORPAGES_SUMMARY_PINNING_FAILURE); |
| 144 | short_description = l10n_util::GetStringUTF16( |
| 145 | IDS_ERRORPAGES_DETAILS_PINNING_FAILURE); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | case UNKNOWN: |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 147 | details = l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DETAILS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | short_description = |
[email protected] | 42197a2 | 2010-12-28 23:29:42 | [diff] [blame] | 149 | l10n_util::GetStringUTF16(IDS_CERT_ERROR_UNKNOWN_ERROR_DESCRIPTION); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | break; |
[email protected] | 4c5f2b9 | 2014-08-09 06:32:24 | [diff] [blame^] | 151 | case CERT_UNABLE_TO_CHECK_REVOCATION: // Deprecated. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | default: |
| 153 | NOTREACHED(); |
| 154 | } |
[email protected] | 4c5f2b9 | 2014-08-09 06:32:24 | [diff] [blame^] | 155 | return SSLErrorInfo(details, short_description); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | SSLErrorInfo::~SSLErrorInfo() { |
| 159 | } |
| 160 | |
| 161 | // static |
| 162 | SSLErrorInfo::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] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 180 | case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM: |
| 181 | return CERT_WEAK_SIGNATURE_ALGORITHM; |
[email protected] | aaa20bde | 2011-12-16 23:27:35 | [diff] [blame] | 182 | case net::ERR_CERT_WEAK_KEY: |
| 183 | return CERT_WEAK_KEY; |
[email protected] | f5a393db | 2013-12-16 18:41:01 | [diff] [blame] | 184 | case net::ERR_CERT_NAME_CONSTRAINT_VIOLATION: |
| 185 | return CERT_NAME_CONSTRAINT_VIOLATION; |
[email protected] | 6061c14 | 2013-10-21 15:13:34 | [diff] [blame] | 186 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | default: |
| 191 | NOTREACHED(); |
| 192 | return UNKNOWN; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // static |
| 197 | int SSLErrorInfo::GetErrorsForCertStatus(int cert_id, |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 198 | net::CertStatus cert_status, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | const GURL& url, |
| 200 | std::vector<SSLErrorInfo>* errors) { |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 201 | const net::CertStatus kErrorFlags[] = { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | 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] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 208 | net::CERT_STATUS_INVALID, |
[email protected] | aaa20bde | 2011-12-16 23:27:35 | [diff] [blame] | 209 | net::CERT_STATUS_WEAK_SIGNATURE_ALGORITHM, |
[email protected] | f5a393db | 2013-12-16 18:41:01 | [diff] [blame] | 210 | net::CERT_STATUS_WEAK_KEY, |
| 211 | net::CERT_STATUS_NAME_CONSTRAINT_VIOLATION, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | }; |
| 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] | 0374b29 | 2009-08-14 23:49:19 | [diff] [blame] | 221 | CERT_INVALID, |
[email protected] | aaa20bde | 2011-12-16 23:27:35 | [diff] [blame] | 222 | CERT_WEAK_SIGNATURE_ALGORITHM, |
[email protected] | f5a393db | 2013-12-16 18:41:01 | [diff] [blame] | 223 | CERT_WEAK_KEY, |
| 224 | CERT_NAME_CONSTRAINT_VIOLATION, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | }; |
| 226 | DCHECK(arraysize(kErrorFlags) == arraysize(kErrorTypes)); |
| 227 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 228 | scoped_refptr<net::X509Certificate> cert = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | int count = 0; |
[email protected] | 85e0f1f | 2008-12-17 18:30:28 | [diff] [blame] | 230 | for (size_t i = 0; i < arraysize(kErrorFlags); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | if (cert_status & kErrorFlags[i]) { |
| 232 | count++; |
| 233 | if (!cert.get()) { |
[email protected] | b59c6cf0 | 2012-03-12 20:51:42 | [diff] [blame] | 234 | bool r = content::CertStore::GetInstance()->RetrieveCert( |
| 235 | cert_id, &cert); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | DCHECK(r); |
| 237 | } |
| 238 | if (errors) |
[email protected] | 39790b7f | 2013-06-03 00:10:59 | [diff] [blame] | 239 | errors->push_back( |
| 240 | SSLErrorInfo::CreateError(kErrorTypes[i], cert.get(), url)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | return count; |
| 244 | } |