OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "u2f_device.h" | 5 #include "u2f_device.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "u2f_apdu_command.h" | 8 #include "u2f_apdu_command.h" |
9 #include "u2f_apdu_response.h" | 9 #include "u2f_apdu_response.h" |
10 | 10 |
11 namespace device { | 11 namespace device { |
12 | 12 |
13 U2fDevice::U2fDevice() | 13 U2fDevice::U2fDevice() : channel_id_(kBroadcastChannel), capabilities_(0) {} |
14 : channel_id_(kBroadcastChannel), capabilities_(0), weak_factory_(this) {} | |
15 | 14 |
16 U2fDevice::~U2fDevice() {} | 15 U2fDevice::~U2fDevice() {} |
17 | 16 |
18 void U2fDevice::Register(const std::vector<uint8_t>& app_param, | 17 void U2fDevice::Register(const std::vector<uint8_t>& app_param, |
19 const std::vector<uint8_t>& challenge_param, | 18 const std::vector<uint8_t>& challenge_param, |
20 const MessageCallback& callback) { | 19 const MessageCallback& callback) { |
21 std::unique_ptr<U2fApduCommand> register_cmd = | 20 std::unique_ptr<U2fApduCommand> register_cmd = |
22 U2fApduCommand::CreateRegister(app_param, challenge_param); | 21 U2fApduCommand::CreateRegister(app_param, challenge_param); |
23 if (!register_cmd) { | 22 if (!register_cmd) { |
24 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 23 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
25 return; | 24 return; |
26 } | 25 } |
27 DeviceTransact(std::move(register_cmd), | 26 DeviceTransact( |
28 base::Bind(&U2fDevice::OnRegisterComplete, | 27 std::move(register_cmd), |
29 weak_factory_.GetWeakPtr(), callback)); | 28 base::Bind(&U2fDevice::OnRegisterComplete, GetWeakPtr(), callback)); |
30 } | 29 } |
31 | 30 |
32 void U2fDevice::Sign(const std::vector<uint8_t>& app_param, | 31 void U2fDevice::Sign(const std::vector<uint8_t>& app_param, |
33 const std::vector<uint8_t>& challenge_param, | 32 const std::vector<uint8_t>& challenge_param, |
34 const std::vector<uint8_t>& key_handle, | 33 const std::vector<uint8_t>& key_handle, |
35 const MessageCallback& callback) { | 34 const MessageCallback& callback) { |
36 std::unique_ptr<U2fApduCommand> sign_cmd = | 35 std::unique_ptr<U2fApduCommand> sign_cmd = |
37 U2fApduCommand::CreateSign(app_param, challenge_param, key_handle); | 36 U2fApduCommand::CreateSign(app_param, challenge_param, key_handle); |
38 if (!sign_cmd) { | 37 if (!sign_cmd) { |
39 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 38 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
40 return; | 39 return; |
41 } | 40 } |
42 DeviceTransact(std::move(sign_cmd), | 41 DeviceTransact(std::move(sign_cmd), base::Bind(&U2fDevice::OnSignComplete, |
43 base::Bind(&U2fDevice::OnSignComplete, | 42 GetWeakPtr(), callback)); |
44 weak_factory_.GetWeakPtr(), callback)); | |
45 } | 43 } |
46 | 44 |
47 void U2fDevice::Version(const VersionCallback& callback) { | 45 void U2fDevice::Version(const VersionCallback& callback) { |
48 std::unique_ptr<U2fApduCommand> version_cmd = U2fApduCommand::CreateVersion(); | 46 std::unique_ptr<U2fApduCommand> version_cmd = U2fApduCommand::CreateVersion(); |
49 if (!version_cmd) { | 47 if (!version_cmd) { |
50 callback.Run(false, ProtocolVersion::UNKNOWN); | 48 callback.Run(false, ProtocolVersion::UNKNOWN); |
51 return; | 49 return; |
52 } | 50 } |
53 DeviceTransact(std::move(version_cmd), | 51 DeviceTransact( |
54 base::Bind(&U2fDevice::OnVersionComplete, | 52 std::move(version_cmd), |
55 weak_factory_.GetWeakPtr(), callback)); | 53 base::Bind(&U2fDevice::OnVersionComplete, GetWeakPtr(), callback)); |
56 } | 54 } |
57 | 55 |
58 void U2fDevice::OnRegisterComplete( | 56 void U2fDevice::OnRegisterComplete( |
59 const MessageCallback& callback, | 57 const MessageCallback& callback, |
60 bool success, | 58 bool success, |
61 std::unique_ptr<U2fApduResponse> register_response) { | 59 std::unique_ptr<U2fApduResponse> register_response) { |
62 if (!success || !register_response) { | 60 if (!success || !register_response) { |
63 callback.Run(U2fReturnCode::FAILURE, std::vector<uint8_t>()); | 61 callback.Run(U2fReturnCode::FAILURE, std::vector<uint8_t>()); |
64 return; | 62 return; |
65 } | 63 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 U2fApduResponse::Status::SW_NO_ERROR && | 124 U2fApduResponse::Status::SW_NO_ERROR && |
127 legacy_version_response->data() == | 125 legacy_version_response->data() == |
128 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { | 126 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { |
129 callback.Run(success, ProtocolVersion::U2F_V2); | 127 callback.Run(success, ProtocolVersion::U2F_V2); |
130 return; | 128 return; |
131 } | 129 } |
132 callback.Run(success, ProtocolVersion::UNKNOWN); | 130 callback.Run(success, ProtocolVersion::UNKNOWN); |
133 } | 131 } |
134 | 132 |
135 } // namespace device | 133 } // namespace device |
OLD | NEW |