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

Unified Diff: device/u2f/u2f_sign.cc

Issue 2821263005: Add U2F request state machines (Closed)
Patch Set: Compilation changes Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: device/u2f/u2f_sign.cc
diff --git a/device/u2f/u2f_sign.cc b/device/u2f/u2f_sign.cc
new file mode 100644
index 0000000000000000000000000000000000000000..76ebbb0ee03e45626a92b1909f666ae20b3f65c3
--- /dev/null
+++ b/device/u2f/u2f_sign.cc
@@ -0,0 +1,94 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "u2f_sign.h"
+
+#include "base/memory/ptr_util.h"
+
+namespace device {
+
+U2fSign::U2fSign(std::vector<std::vector<uint8_t>> registered_keys,
+ std::vector<uint8_t> challenge_hash,
+ std::vector<uint8_t> app_param,
+ const ResponseCallback& cb)
+ : U2fRequest(cb),
+ registered_keys_(registered_keys),
+ challenge_hash_(challenge_hash),
+ app_param_(app_param),
+ weak_factory_(this) {}
+
+U2fSign::~U2fSign() {}
+
+// static
+std::unique_ptr<U2fRequest> U2fSign::TrySign(
+ std::vector<std::vector<uint8_t>> registered_keys,
+ std::vector<uint8_t> challenge_hash,
+ std::vector<uint8_t> app_param,
+ const ResponseCallback& cb) {
+ std::unique_ptr<U2fRequest> request =
+ base::MakeUnique<U2fSign>(registered_keys, challenge_hash, app_param, cb);
+ if (request)
Reilly Grant (use Gerrit) 2017/04/21 17:19:17 base::MakeUnique does not fail.
Casey Piper 2017/04/21 18:42:26 Acknowledged.
+ request->Start();
+ return request;
+}
+
+void U2fSign::TryDevice() {
+ if (!current_device_)
+ return;
Reilly Grant (use Gerrit) 2017/04/21 17:19:16 DCHECK?
Casey Piper 2017/04/21 18:42:26 Done.
+
+ if (registered_keys_.size() == 0) {
+ // Send registration (Fake enroll) if no keys were provided
+ current_device_->Register(
+ kBogusAppParam, kBogusChallenge,
+ base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(),
+ registered_keys_.cbegin()));
+ return;
+ }
+ // Try signing current device with the first registered key
+ auto it = registered_keys_.cbegin();
+ current_device_->Sign(
+ app_param_, challenge_hash_, *it,
+ base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), it));
+}
+
+void U2fSign::OnTryDevice(std::vector<std::vector<uint8_t>>::const_iterator it,
+ U2fDevice::ReturnCode return_code,
+ std::vector<uint8_t> response_data) {
+ switch (return_code) {
+ case U2fDevice::ReturnCode::SUCCESS:
+ state_ = State::COMPLETE;
+ cb_.Run(static_cast<uint8_t>(return_code), response_data);
+ break;
+ case U2fDevice::ReturnCode::CONDITIONS_NOT_SATISFIED: {
+ // Key handle is accepted by this device, but waiting on user touch. Move
+ // on and try this device again later.
+ state_ = State::IDLE;
+ Transition();
+ break;
+ }
+ case U2fDevice::ReturnCode::INVALID_PARAMS:
+ if (++it != registered_keys_.end()) {
+ // Key is not for this device. Try signing with the next key.
+ current_device_->Sign(
+ app_param_, challenge_hash_, *it,
+ base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), it));
+ } else {
+ // No provided key was accepted by this device. Send registration
+ // (Fake enroll) request to device.
+ current_device_->Register(
+ kBogusAppParam, kBogusChallenge,
+ base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(),
+ registered_keys_.cbegin()));
+ }
+ break;
+ default:
+ // Some sort of failure occured. Abandon this device and move on.
+ state_ = State::IDLE;
+ current_device_ = nullptr;
+ Transition();
+ break;
+ }
+}
+
+} // namespace device

Powered by Google App Engine
This is Rietveld 408576698