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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "u2f_sign.h"
6
7 #include "base/memory/ptr_util.h"
8
9 namespace device {
10
11 U2fSign::U2fSign(std::vector<std::vector<uint8_t>> registered_keys,
12 std::vector<uint8_t> challenge_hash,
13 std::vector<uint8_t> app_param,
14 const ResponseCallback& cb)
15 : U2fRequest(cb),
16 registered_keys_(registered_keys),
17 challenge_hash_(challenge_hash),
18 app_param_(app_param),
19 weak_factory_(this) {}
20
21 U2fSign::~U2fSign() {}
22
23 // static
24 std::unique_ptr<U2fRequest> U2fSign::TrySign(
25 std::vector<std::vector<uint8_t>> registered_keys,
26 std::vector<uint8_t> challenge_hash,
27 std::vector<uint8_t> app_param,
28 const ResponseCallback& cb) {
29 std::unique_ptr<U2fRequest> request =
30 base::MakeUnique<U2fSign>(registered_keys, challenge_hash, app_param, cb);
31 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.
32 request->Start();
33 return request;
34 }
35
36 void U2fSign::TryDevice() {
37 if (!current_device_)
38 return;
Reilly Grant (use Gerrit) 2017/04/21 17:19:16 DCHECK?
Casey Piper 2017/04/21 18:42:26 Done.
39
40 if (registered_keys_.size() == 0) {
41 // Send registration (Fake enroll) if no keys were provided
42 current_device_->Register(
43 kBogusAppParam, kBogusChallenge,
44 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(),
45 registered_keys_.cbegin()));
46 return;
47 }
48 // Try signing current device with the first registered key
49 auto it = registered_keys_.cbegin();
50 current_device_->Sign(
51 app_param_, challenge_hash_, *it,
52 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), it));
53 }
54
55 void U2fSign::OnTryDevice(std::vector<std::vector<uint8_t>>::const_iterator it,
56 U2fDevice::ReturnCode return_code,
57 std::vector<uint8_t> response_data) {
58 switch (return_code) {
59 case U2fDevice::ReturnCode::SUCCESS:
60 state_ = State::COMPLETE;
61 cb_.Run(static_cast<uint8_t>(return_code), response_data);
62 break;
63 case U2fDevice::ReturnCode::CONDITIONS_NOT_SATISFIED: {
64 // Key handle is accepted by this device, but waiting on user touch. Move
65 // on and try this device again later.
66 state_ = State::IDLE;
67 Transition();
68 break;
69 }
70 case U2fDevice::ReturnCode::INVALID_PARAMS:
71 if (++it != registered_keys_.end()) {
72 // Key is not for this device. Try signing with the next key.
73 current_device_->Sign(
74 app_param_, challenge_hash_, *it,
75 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), it));
76 } else {
77 // No provided key was accepted by this device. Send registration
78 // (Fake enroll) request to device.
79 current_device_->Register(
80 kBogusAppParam, kBogusChallenge,
81 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(),
82 registered_keys_.cbegin()));
83 }
84 break;
85 default:
86 // Some sort of failure occured. Abandon this device and move on.
87 state_ = State::IDLE;
88 current_device_ = nullptr;
89 Transition();
90 break;
91 }
92 }
93
94 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698