Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/certificate_provider/certificate_provide r_api.h" | 5 #include "chrome/browser/extensions/api/certificate_provider/certificate_provide r_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice.h" | 15 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice.h" |
| 15 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice_factory.h" | 16 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice_factory.h" |
| 16 #include "chrome/common/extensions/api/certificate_provider.h" | 17 #include "chrome/common/extensions/api/certificate_provider.h" |
| 17 #include "chrome/common/extensions/api/certificate_provider_internal.h" | 18 #include "chrome/common/extensions/api/certificate_provider_internal.h" |
| 19 #include "chrome/grit/generated_resources.h" | |
| 18 #include "content/public/common/console_message_level.h" | 20 #include "content/public/common/console_message_level.h" |
| 19 #include "net/cert/x509_certificate.h" | 21 #include "net/cert/x509_certificate.h" |
| 20 #include "net/ssl/ssl_private_key.h" | 22 #include "net/ssl/ssl_private_key.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 21 | 24 |
| 22 namespace extensions { | 25 namespace extensions { |
| 23 | 26 |
| 24 namespace api_cp = api::certificate_provider; | 27 namespace api_cp = api::certificate_provider; |
| 25 namespace api_cpi = api::certificate_provider_internal; | 28 namespace api_cpi = api::certificate_provider_internal; |
| 26 | 29 |
| 27 namespace { | 30 namespace { |
| 28 | 31 |
| 29 const char kErrorInvalidX509Cert[] = | 32 const char kErrorInvalidX509Cert[] = |
| 30 "Certificate is not a valid X.509 certificate."; | 33 "Certificate is not a valid X.509 certificate."; |
| 31 const char kErrorECDSANotSupported[] = "Key type ECDSA not supported."; | 34 const char kErrorECDSANotSupported[] = "Key type ECDSA not supported."; |
| 32 const char kErrorUnknownKeyType[] = "Key type unknown."; | 35 const char kErrorUnknownKeyType[] = "Key type unknown."; |
| 33 const char kErrorAborted[] = "Request was aborted."; | 36 const char kErrorAborted[] = "Request was aborted."; |
| 34 const char kErrorTimeout[] = "Request timed out, reply rejected."; | 37 const char kErrorTimeout[] = "Request timed out, reply rejected."; |
| 35 | 38 |
| 36 } // namespace | 39 } // namespace |
| 37 | 40 |
| 41 const int MAX_CLOSED_DIALOGS_PER_10_MINUTES = 2; | |
| 42 | |
| 38 CertificateProviderInternalReportCertificatesFunction:: | 43 CertificateProviderInternalReportCertificatesFunction:: |
| 39 ~CertificateProviderInternalReportCertificatesFunction() {} | 44 ~CertificateProviderInternalReportCertificatesFunction() {} |
| 40 | 45 |
| 41 ExtensionFunction::ResponseAction | 46 ExtensionFunction::ResponseAction |
| 42 CertificateProviderInternalReportCertificatesFunction::Run() { | 47 CertificateProviderInternalReportCertificatesFunction::Run() { |
| 43 std::unique_ptr<api_cpi::ReportCertificates::Params> params( | 48 std::unique_ptr<api_cpi::ReportCertificates::Params> params( |
| 44 api_cpi::ReportCertificates::Params::Create(*args_)); | 49 api_cpi::ReportCertificates::Params::Create(*args_)); |
| 45 EXTENSION_FUNCTION_VALIDATE(params); | 50 EXTENSION_FUNCTION_VALIDATE(params); |
| 46 | 51 |
| 47 chromeos::CertificateProviderService* const service = | 52 chromeos::CertificateProviderService* const service = |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 out_info->supported_hashes.push_back(net::SSLPrivateKey::Hash::SHA512); | 146 out_info->supported_hashes.push_back(net::SSLPrivateKey::Hash::SHA512); |
| 142 break; | 147 break; |
| 143 case api_cp::HASH_NONE: | 148 case api_cp::HASH_NONE: |
| 144 NOTREACHED(); | 149 NOTREACHED(); |
| 145 return false; | 150 return false; |
| 146 } | 151 } |
| 147 } | 152 } |
| 148 return true; | 153 return true; |
| 149 } | 154 } |
| 150 | 155 |
| 156 base::string16 GetErrorMessageForType(api_cp::PinRequestErrorType error_type) { | |
| 157 switch (error_type) { | |
| 158 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_INVALID_PIN: | |
| 159 return l10n_util::GetStringUTF16( | |
| 160 IDS_REQUEST_PIN_DIALOG_INVALID_PIN_ERROR); | |
| 161 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_INVALID_PUK: | |
| 162 return l10n_util::GetStringUTF16( | |
| 163 IDS_REQUEST_PIN_DIALOG_INVALID_PUK_ERROR); | |
| 164 case | |
| 165 api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_MAX_ATTEMPTS_EXCEEDED: | |
| 166 return l10n_util::GetStringUTF16( | |
| 167 IDS_REQUEST_PIN_DIALOG_MAX_ATTEMPTS_EXCEEDED_ERROR); | |
| 168 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_UNKNOWN_ERROR: | |
| 169 return l10n_util::GetStringUTF16( | |
| 170 IDS_REQUEST_PIN_DIALOG_UNKNOWN_ERROR); | |
| 171 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_NONE: | |
| 172 return base::string16(); | |
| 173 } | |
| 174 } | |
|
stevenjb
2016/08/09 21:04:39
This is UI code, it shouldn't be in the API. We sh
igorcov1
2016/08/10 18:05:04
Done.
| |
| 175 | |
| 176 CertificateProviderStopPinRequestFunction:: | |
| 177 ~CertificateProviderStopPinRequestFunction() {} | |
| 178 | |
| 179 ExtensionFunction::ResponseAction | |
| 180 CertificateProviderStopPinRequestFunction::Run() { | |
| 181 std::unique_ptr<api_cp::RequestPin::Params> params( | |
| 182 api_cp::RequestPin::Params::Create(*args_)); | |
| 183 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 184 | |
| 185 base::string16 error_message = GetErrorMessageForType( | |
| 186 params->details.error_type); | |
| 187 chromeos::CertificateProviderService* const service = | |
| 188 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( | |
| 189 browser_context()); | |
| 190 DCHECK(service); | |
| 191 if (error_message.empty()) { | |
| 192 bool dialog_closed = service->CloseDialog(extension_id()); | |
| 193 if (!dialog_closed) { | |
| 194 // This might happen if the user closed the dialog while extension was | |
| 195 // processing the input. | |
| 196 LOG(ERROR) << "Wrong extension requesting to close the dialog"; | |
| 197 return RespondNow(Error("No active dialog from extension.")); | |
| 198 } | |
| 199 | |
| 200 std::unique_ptr<base::ListValue> create_results(new base::ListValue()); | |
| 201 return RespondNow(ArgumentList(std::move(create_results))); | |
| 202 } else { | |
|
stevenjb
2016/08/09 21:04:39
no else
igorcov1
2016/08/10 18:05:04
Done.
| |
| 203 bool success = service->UpdatePinDialog( | |
| 204 extension()->id(), | |
| 205 error_message, | |
| 206 false, | |
| 207 base::Bind(&CertificateProviderStopPinRequestFunction::DialogClosed, | |
| 208 this)); | |
| 209 if (success) { | |
| 210 return RespondLater(); | |
| 211 } else { | |
|
stevenjb
2016/08/09 21:04:39
invert, no else
igorcov1
2016/08/10 18:05:04
Done.
| |
| 212 return RespondNow(Error("No active dialog from extension.")); | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 void CertificateProviderStopPinRequestFunction::DialogClosed( | |
| 218 const base::string16& value) { | |
| 219 std::unique_ptr<base::ListValue> create_results(new base::ListValue()); | |
| 220 chromeos::CertificateProviderService* const service = | |
| 221 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( | |
| 222 browser_context()); | |
| 223 DCHECK(service); | |
| 224 | |
| 225 Respond(ArgumentList(std::move(create_results))); | |
| 226 service->OnPinDialogInput(extension_id(), true); | |
| 227 } | |
| 228 | |
| 229 CertificateProviderRequestPinFunction:: | |
| 230 ~CertificateProviderRequestPinFunction() {} | |
| 231 | |
| 232 bool CertificateProviderRequestPinFunction::ShouldSkipQuotaLimiting() const { | |
| 233 chromeos::CertificateProviderService* const service = | |
| 234 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( | |
| 235 browser_context()); | |
| 236 DCHECK(service); | |
| 237 | |
| 238 return !service->LastPinDialogClosed(extension_id()); | |
| 239 } | |
| 240 | |
| 241 void CertificateProviderRequestPinFunction::GetQuotaLimitHeuristics( | |
| 242 extensions::QuotaLimitHeuristics* heuristics) const { | |
| 243 QuotaLimitHeuristic::Config short_limit_config = { | |
| 244 extensions::MAX_CLOSED_DIALOGS_PER_10_MINUTES, | |
| 245 base::TimeDelta::FromMinutes(1)}; | |
| 246 heuristics->push_back(new QuotaService::TimedLimit( | |
| 247 short_limit_config, new QuotaLimitHeuristic::SingletonBucketMapper(), | |
| 248 "MAX_SHOW_DIALOGS_PER_MINUTE")); | |
| 249 } | |
| 250 | |
| 251 ExtensionFunction::ResponseAction | |
| 252 CertificateProviderRequestPinFunction::Run() { | |
| 253 std::unique_ptr<api_cp::RequestPin::Params> params( | |
| 254 api_cp::RequestPin::Params::Create(*args_)); | |
| 255 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 256 | |
| 257 api_cp::PinRequestType pin_dialog_type = | |
| 258 (params->details.request_type) ? | |
| 259 params->details.request_type : | |
| 260 api_cp::PinRequestType::PIN_REQUEST_TYPE_PIN; | |
| 261 | |
| 262 base::string16 error_message = GetErrorMessageForType( | |
| 263 params->details.error_type); | |
| 264 | |
| 265 bool accept_input = true; | |
| 266 if (params->details.attempts_left) { | |
| 267 int attempts_left = *(params->details.attempts_left.get()); | |
| 268 accept_input = attempts_left > 0; | |
| 269 error_message.append(l10n_util::GetStringFUTF16( | |
| 270 IDS_REQUEST_PIN_DIALOG_ATTEMPTS_LEFT, | |
| 271 base::ASCIIToUTF16(std::to_string(attempts_left)))); | |
| 272 } | |
| 273 | |
| 274 const std::string dialog_type = | |
| 275 (pin_dialog_type == api_cp::PinRequestType::PIN_REQUEST_TYPE_PIN) ? | |
| 276 "PIN: " : "PUK: "; | |
| 277 | |
| 278 chromeos::CertificateProviderService* const service = | |
| 279 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( | |
| 280 browser_context()); | |
| 281 DCHECK(service); | |
| 282 | |
| 283 chromeos::RequestPinResponse success = service->ShowPinDialog( | |
| 284 extension()->id(), | |
| 285 extension()->name(), | |
| 286 params->details.sign_request_id, | |
| 287 dialog_type, | |
| 288 error_message, | |
| 289 accept_input, | |
| 290 base::Bind(&CertificateProviderRequestPinFunction::OnInputReceived, | |
| 291 this)); | |
| 292 switch (success) { | |
| 293 case chromeos::RequestPinResponse::SUCCESS: return RespondLater(); | |
| 294 case chromeos::RequestPinResponse::INVALID_ID: | |
| 295 return RespondNow(Error("Invalid signRequestId")); | |
| 296 case chromeos::RequestPinResponse::OTHER_FLOW_IN_PROGRESS: | |
| 297 return RespondNow(Error("Other flow in progress")); | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 void CertificateProviderRequestPinFunction::OnInputReceived( | |
| 302 const base::string16& value) { | |
| 303 std::unique_ptr<base::ListValue> create_results(new base::ListValue()); | |
| 304 chromeos::CertificateProviderService* const service = | |
| 305 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( | |
| 306 browser_context()); | |
| 307 DCHECK(service); | |
| 308 if (!value.empty()) { | |
| 309 api::certificate_provider::PinResponseDetails details; | |
| 310 details.user_input.reset(new std::string(value.begin(), value.end())); | |
| 311 create_results->Append(details.ToValue()); | |
| 312 } | |
| 313 | |
| 314 Respond(ArgumentList(std::move(create_results))); | |
| 315 service->OnPinDialogInput(extension_id(), value.empty()); | |
| 316 } | |
| 317 | |
| 151 CertificateProviderInternalReportSignatureFunction:: | 318 CertificateProviderInternalReportSignatureFunction:: |
| 152 ~CertificateProviderInternalReportSignatureFunction() {} | 319 ~CertificateProviderInternalReportSignatureFunction() {} |
| 153 | 320 |
| 154 ExtensionFunction::ResponseAction | 321 ExtensionFunction::ResponseAction |
| 155 CertificateProviderInternalReportSignatureFunction::Run() { | 322 CertificateProviderInternalReportSignatureFunction::Run() { |
| 156 std::unique_ptr<api_cpi::ReportSignature::Params> params( | 323 std::unique_ptr<api_cpi::ReportSignature::Params> params( |
| 157 api_cpi::ReportSignature::Params::Create(*args_)); | 324 api_cpi::ReportSignature::Params::Create(*args_)); |
| 158 EXTENSION_FUNCTION_VALIDATE(params); | 325 EXTENSION_FUNCTION_VALIDATE(params); |
| 159 | 326 |
| 160 chromeos::CertificateProviderService* const service = | 327 chromeos::CertificateProviderService* const service = |
| 161 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( | 328 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( |
| 162 browser_context()); | 329 browser_context()); |
| 163 DCHECK(service); | 330 DCHECK(service); |
| 164 | 331 |
| 165 std::vector<uint8_t> signature; | 332 std::vector<uint8_t> signature; |
| 166 // If an error occurred, |signature| will not be set. | 333 // If an error occurred, |signature| will not be set. |
| 167 if (params->signature) | 334 if (params->signature) |
| 168 signature.assign(params->signature->begin(), params->signature->end()); | 335 signature.assign(params->signature->begin(), params->signature->end()); |
| 169 | 336 |
| 170 service->ReplyToSignRequest(extension_id(), params->request_id, signature); | 337 service->ReplyToSignRequest(extension_id(), params->request_id, signature); |
| 171 return RespondNow(NoArguments()); | 338 return RespondNow(NoArguments()); |
| 172 } | 339 } |
| 173 | 340 |
| 174 } // namespace extensions | 341 } // namespace extensions |
| OLD | NEW |