| OLD | NEW |
| (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(const std::vector<std::vector<uint8_t>>& registered_keys, | |
| 12 const std::vector<uint8_t>& challenge_hash, | |
| 13 const 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 const std::vector<std::vector<uint8_t>>& registered_keys, | |
| 26 const std::vector<uint8_t>& challenge_hash, | |
| 27 const 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 request->Start(); | |
| 32 return request; | |
| 33 } | |
| 34 | |
| 35 void U2fSign::TryDevice() { | |
| 36 DCHECK(current_device_); | |
| 37 | |
| 38 if (registered_keys_.size() == 0) { | |
| 39 // Send registration (Fake enroll) if no keys were provided | |
| 40 current_device_->Register( | |
| 41 kBogusAppParam, kBogusChallenge, | |
| 42 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), | |
| 43 registered_keys_.cbegin())); | |
| 44 return; | |
| 45 } | |
| 46 // Try signing current device with the first registered key | |
| 47 auto it = registered_keys_.cbegin(); | |
| 48 current_device_->Sign( | |
| 49 app_param_, challenge_hash_, *it, | |
| 50 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), it)); | |
| 51 } | |
| 52 | |
| 53 void U2fSign::OnTryDevice(std::vector<std::vector<uint8_t>>::const_iterator it, | |
| 54 U2fReturnCode return_code, | |
| 55 std::vector<uint8_t> response_data) { | |
| 56 switch (return_code) { | |
| 57 case U2fReturnCode::SUCCESS: | |
| 58 state_ = State::COMPLETE; | |
| 59 cb_.Run(return_code, response_data); | |
| 60 break; | |
| 61 case U2fReturnCode::CONDITIONS_NOT_SATISFIED: { | |
| 62 // Key handle is accepted by this device, but waiting on user touch. Move | |
| 63 // on and try this device again later. | |
| 64 state_ = State::IDLE; | |
| 65 Transition(); | |
| 66 break; | |
| 67 } | |
| 68 case U2fReturnCode::INVALID_PARAMS: | |
| 69 if (++it != registered_keys_.end()) { | |
| 70 // Key is not for this device. Try signing with the next key. | |
| 71 current_device_->Sign( | |
| 72 app_param_, challenge_hash_, *it, | |
| 73 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), it)); | |
| 74 } else { | |
| 75 // No provided key was accepted by this device. Send registration | |
| 76 // (Fake enroll) request to device. | |
| 77 current_device_->Register( | |
| 78 kBogusAppParam, kBogusChallenge, | |
| 79 base::Bind(&U2fSign::OnTryDevice, weak_factory_.GetWeakPtr(), | |
| 80 registered_keys_.cbegin())); | |
| 81 } | |
| 82 break; | |
| 83 default: | |
| 84 // Some sort of failure occured. Abandon this device and move on. | |
| 85 state_ = State::IDLE; | |
| 86 current_device_ = nullptr; | |
| 87 Transition(); | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 } // namespace device | |
| OLD | NEW |