Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/browser/extensions/api/certificate_provider/certificate_provider_api.cc

Issue 2094333002: Implementation for chrome.certificateProvider.requestPin/stopPinRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed review comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice.h" 14 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice.h"
15 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice_factory.h" 15 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv ice_factory.h"
16 #include "chrome/common/extensions/api/certificate_provider.h" 16 #include "chrome/common/extensions/api/certificate_provider.h"
17 #include "chrome/common/extensions/api/certificate_provider_internal.h" 17 #include "chrome/common/extensions/api/certificate_provider_internal.h"
18 #include "content/public/common/console_message_level.h" 18 #include "content/public/common/console_message_level.h"
19 #include "net/cert/x509_certificate.h" 19 #include "net/cert/x509_certificate.h"
20 #include "net/ssl/ssl_private_key.h" 20 #include "net/ssl/ssl_private_key.h"
21 21
22 namespace api_cp = extensions::api::certificate_provider;
23 namespace api_cpi = extensions::api::certificate_provider_internal;
24
25 chromeos::RequestPinErrorType GetErrorTypeForView(
26 api_cp::PinRequestErrorType error_type) {
27 switch (error_type) {
28 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_INVALID_PIN:
29 return chromeos::RequestPinErrorType::INVALID_PIN;
30 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_INVALID_PUK:
31 return chromeos::RequestPinErrorType::INVALID_PUK;
32 case
33 api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_MAX_ATTEMPTS_EXCEEDED:
34 return chromeos::RequestPinErrorType::MAX_ATTEMPTS_EXCEEDED;
35 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_UNKNOWN_ERROR:
36 return chromeos::RequestPinErrorType::UNKNOWN_ERROR;
37 case api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_NONE:
38 return chromeos::RequestPinErrorType::NONE;
39 }
40 }
41
22 namespace extensions { 42 namespace extensions {
23 43
24 namespace api_cp = api::certificate_provider;
25 namespace api_cpi = api::certificate_provider_internal;
26
27 namespace { 44 namespace {
28 45
29 const char kErrorInvalidX509Cert[] = 46 const char kErrorInvalidX509Cert[] =
30 "Certificate is not a valid X.509 certificate."; 47 "Certificate is not a valid X.509 certificate.";
31 const char kErrorECDSANotSupported[] = "Key type ECDSA not supported."; 48 const char kErrorECDSANotSupported[] = "Key type ECDSA not supported.";
32 const char kErrorUnknownKeyType[] = "Key type unknown."; 49 const char kErrorUnknownKeyType[] = "Key type unknown.";
33 const char kErrorAborted[] = "Request was aborted."; 50 const char kErrorAborted[] = "Request was aborted.";
34 const char kErrorTimeout[] = "Request timed out, reply rejected."; 51 const char kErrorTimeout[] = "Request timed out, reply rejected.";
35 52
36 } // namespace 53 } // namespace
37 54
55 const int MAX_CLOSED_DIALOGS_PER_10_MINUTES = 2;
56
38 CertificateProviderInternalReportCertificatesFunction:: 57 CertificateProviderInternalReportCertificatesFunction::
39 ~CertificateProviderInternalReportCertificatesFunction() {} 58 ~CertificateProviderInternalReportCertificatesFunction() {}
40 59
41 ExtensionFunction::ResponseAction 60 ExtensionFunction::ResponseAction
42 CertificateProviderInternalReportCertificatesFunction::Run() { 61 CertificateProviderInternalReportCertificatesFunction::Run() {
43 std::unique_ptr<api_cpi::ReportCertificates::Params> params( 62 std::unique_ptr<api_cpi::ReportCertificates::Params> params(
44 api_cpi::ReportCertificates::Params::Create(*args_)); 63 api_cpi::ReportCertificates::Params::Create(*args_));
45 EXTENSION_FUNCTION_VALIDATE(params); 64 EXTENSION_FUNCTION_VALIDATE(params);
46 65
47 chromeos::CertificateProviderService* const service = 66 chromeos::CertificateProviderService* const service =
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 out_info->supported_hashes.push_back(net::SSLPrivateKey::Hash::SHA512); 160 out_info->supported_hashes.push_back(net::SSLPrivateKey::Hash::SHA512);
142 break; 161 break;
143 case api_cp::HASH_NONE: 162 case api_cp::HASH_NONE:
144 NOTREACHED(); 163 NOTREACHED();
145 return false; 164 return false;
146 } 165 }
147 } 166 }
148 return true; 167 return true;
149 } 168 }
150 169
170 CertificateProviderStopPinRequestFunction::
171 ~CertificateProviderStopPinRequestFunction() {}
172
173 ExtensionFunction::ResponseAction
174 CertificateProviderStopPinRequestFunction::Run() {
175 std::unique_ptr<api_cp::RequestPin::Params> params(
176 api_cp::RequestPin::Params::Create(*args_));
177 EXTENSION_FUNCTION_VALIDATE(params.get());
178
179 chromeos::CertificateProviderService* const service =
180 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
181 browser_context());
182 DCHECK(service);
183 if (params->details.error_type ==
184 api_cp::PinRequestErrorType::PIN_REQUEST_ERROR_TYPE_NONE) {
185 bool dialog_closed =
186 service->pin_dialog_manager()->CloseDialog(extension_id());
187 if (!dialog_closed) {
188 // This might happen if the user closed the dialog while extension was
189 // processing the input.
190 LOG(ERROR) << "Wrong extension requesting to close the dialog";
191 return RespondNow(Error("No active dialog from extension."));
stevenjb 2016/09/12 21:16:40 This string should be defined as a const in this f
igorcov 2016/09/13 14:19:33 Done.
192 }
193
194 std::unique_ptr<base::ListValue> create_results(new base::ListValue());
195 return RespondNow(ArgumentList(std::move(create_results)));
196 }
197
198 chromeos::RequestPinErrorType error_type = GetErrorTypeForView(
199 params->details.error_type);
200 bool success = service->pin_dialog_manager()->UpdatePinDialog(
201 extension()->id(),
202 error_type,
203 false, // Don't accept any input.
204 base::Bind(&CertificateProviderStopPinRequestFunction::DialogClosed,
205 this));
206 if (!success) {
207 return RespondNow(Error("No active dialog from extension."));
208 }
209
210 return RespondLater();
211 }
212
213 void CertificateProviderStopPinRequestFunction::DialogClosed(
214 const base::string16& value) {
215 std::unique_ptr<base::ListValue> create_results(new base::ListValue());
216 chromeos::CertificateProviderService* const service =
217 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
218 browser_context());
219 DCHECK(service);
220
221 Respond(ArgumentList(std::move(create_results)));
222 service->pin_dialog_manager()->OnPinDialogClosed();
223 }
224
225 CertificateProviderRequestPinFunction::
226 ~CertificateProviderRequestPinFunction() {}
227
228 bool CertificateProviderRequestPinFunction::ShouldSkipQuotaLimiting() const {
229 chromeos::CertificateProviderService* const service =
230 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
231 browser_context());
232 DCHECK(service);
233
234 return !service->pin_dialog_manager()->LastPinDialogClosed(extension_id());
235 }
236
237 void CertificateProviderRequestPinFunction::GetQuotaLimitHeuristics(
238 extensions::QuotaLimitHeuristics* heuristics) const {
239 QuotaLimitHeuristic::Config short_limit_config = {
240 extensions::MAX_CLOSED_DIALOGS_PER_10_MINUTES,
241 base::TimeDelta::FromMinutes(1)};
242 heuristics->push_back(new QuotaService::TimedLimit(
243 short_limit_config, new QuotaLimitHeuristic::SingletonBucketMapper(),
244 "MAX_SHOW_DIALOGS_PER_MINUTE"));
stevenjb 2016/09/12 21:16:40 We should name this more specifically.
igorcov 2016/09/13 14:19:33 Done.
245 }
246
247 ExtensionFunction::ResponseAction
248 CertificateProviderRequestPinFunction::Run() {
249 std::unique_ptr<api_cp::RequestPin::Params> params(
250 api_cp::RequestPin::Params::Create(*args_));
251 EXTENSION_FUNCTION_VALIDATE(params.get());
252
253 api_cp::PinRequestType pin_request_type =
254 (params->details.request_type) ?
255 params->details.request_type :
256 api_cp::PinRequestType::PIN_REQUEST_TYPE_PIN;
257
258 chromeos::RequestPinErrorType error_type = GetErrorTypeForView(
259 params->details.error_type);
260
261 chromeos::RequestPinCodeType code_type =
262 (pin_request_type == api_cp::PinRequestType::PIN_REQUEST_TYPE_PIN) ?
263 chromeos::RequestPinCodeType::PIN : chromeos::RequestPinCodeType::PUK;
264
265 chromeos::CertificateProviderService* const service =
266 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
267 browser_context());
268 DCHECK(service);
269
270 int attempts_left = params->details.attempts_left ?
271 *params->details.attempts_left.get() : -1;
272 chromeos::PinDialogManager::RequestPinResponse result =
273 service->pin_dialog_manager()->ShowPinDialog(
274 extension()->id(),
275 extension()->name(),
276 params->details.sign_request_id,
277 code_type,
278 error_type,
279 attempts_left,
280 base::Bind(&CertificateProviderRequestPinFunction::OnInputReceived,
281 this));
282 switch (result) {
283 case chromeos::PinDialogManager::RequestPinResponse::SUCCESS:
284 return RespondLater();
285 case chromeos::PinDialogManager::RequestPinResponse::INVALID_ID:
286 return RespondNow(Error("Invalid signRequestId"));
287 case chromeos::PinDialogManager::RequestPinResponse::OTHER_FLOW_IN_PROGRESS:
288 return RespondNow(Error("Other flow in progress"));
289 case
290 chromeos::PinDialogManager::RequestPinResponse::DIALOG_DISPLAYED_ALREADY:
291 return RespondNow(Error("Previous request not finished"));
stevenjb 2016/09/12 21:16:40 nit: These errors should also be defined as consts
igorcov 2016/09/13 14:19:33 Done.
292 }
293 }
294
295 void CertificateProviderRequestPinFunction::OnInputReceived(
296 const base::string16& value) {
297 std::unique_ptr<base::ListValue> create_results(new base::ListValue());
298 chromeos::CertificateProviderService* const service =
299 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
300 browser_context());
301 DCHECK(service);
302 if (!value.empty()) {
303 api::certificate_provider::PinResponseDetails details;
304 details.user_input.reset(new std::string(value.begin(), value.end()));
305 create_results->Append(details.ToValue());
306 }
307
308 Respond(ArgumentList(std::move(create_results)));
309 if (!value.empty()) {
310 service->pin_dialog_manager()->OnPinDialogInput();
311 }
312 }
313
151 CertificateProviderInternalReportSignatureFunction:: 314 CertificateProviderInternalReportSignatureFunction::
152 ~CertificateProviderInternalReportSignatureFunction() {} 315 ~CertificateProviderInternalReportSignatureFunction() {}
153 316
154 ExtensionFunction::ResponseAction 317 ExtensionFunction::ResponseAction
155 CertificateProviderInternalReportSignatureFunction::Run() { 318 CertificateProviderInternalReportSignatureFunction::Run() {
156 std::unique_ptr<api_cpi::ReportSignature::Params> params( 319 std::unique_ptr<api_cpi::ReportSignature::Params> params(
157 api_cpi::ReportSignature::Params::Create(*args_)); 320 api_cpi::ReportSignature::Params::Create(*args_));
158 EXTENSION_FUNCTION_VALIDATE(params); 321 EXTENSION_FUNCTION_VALIDATE(params);
159 322
160 chromeos::CertificateProviderService* const service = 323 chromeos::CertificateProviderService* const service =
161 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( 324 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
162 browser_context()); 325 browser_context());
163 DCHECK(service); 326 DCHECK(service);
164 327
165 std::vector<uint8_t> signature; 328 std::vector<uint8_t> signature;
166 // If an error occurred, |signature| will not be set. 329 // If an error occurred, |signature| will not be set.
167 if (params->signature) 330 if (params->signature)
168 signature.assign(params->signature->begin(), params->signature->end()); 331 signature.assign(params->signature->begin(), params->signature->end());
169 332
170 service->ReplyToSignRequest(extension_id(), params->request_id, signature); 333 service->ReplyToSignRequest(extension_id(), params->request_id, signature);
171 return RespondNow(NoArguments()); 334 return RespondNow(NoArguments());
172 } 335 }
173 336
174 } // namespace extensions 337 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698