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() : weak_factory_(this) {} | 13 U2fDevice::U2fDevice() |
| 14 : channel_id_(kBroadcastChannel), capabilities_(0), weak_factory_(this) {} |
14 | 15 |
15 U2fDevice::~U2fDevice() {} | 16 U2fDevice::~U2fDevice() {} |
16 | 17 |
17 void U2fDevice::Register(const std::vector<uint8_t>& app_param, | 18 void U2fDevice::Register(const std::vector<uint8_t>& app_param, |
18 const std::vector<uint8_t>& challenge_param, | 19 const std::vector<uint8_t>& challenge_param, |
19 const MessageCallback& callback) { | 20 const MessageCallback& callback) { |
20 std::unique_ptr<U2fApduCommand> register_cmd = | 21 std::unique_ptr<U2fApduCommand> register_cmd = |
21 U2fApduCommand::CreateRegister(app_param, challenge_param); | 22 U2fApduCommand::CreateRegister(app_param, challenge_param); |
22 if (!register_cmd) { | 23 if (!register_cmd) { |
23 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 24 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
24 return; | 25 return; |
25 } | 26 } |
26 DeviceTransact(std::move(register_cmd), | 27 DeviceTransact(std::move(register_cmd), |
27 base::Bind(&U2fDevice::OnRegisterComplete, | 28 base::Bind(&U2fDevice::OnRegisterComplete, |
28 weak_factory_.GetWeakPtr(), callback)); | 29 weak_factory_.GetWeakPtr(), callback)); |
29 } | 30 } |
30 | 31 |
31 void U2fDevice::Sign(const std::vector<uint8_t>& app_param, | 32 void U2fDevice::Sign(const std::vector<uint8_t>& app_param, |
32 const std::vector<uint8_t>& challenge_param, | 33 const std::vector<uint8_t>& challenge_param, |
33 const std::vector<uint8_t>& key_handle, | 34 const std::vector<uint8_t>& key_handle, |
34 const MessageCallback& callback) { | 35 const MessageCallback& callback) { |
35 std::unique_ptr<U2fApduCommand> sign_cmd = | 36 std::unique_ptr<U2fApduCommand> sign_cmd = |
36 U2fApduCommand::CreateSign(app_param, challenge_param, key_handle); | 37 U2fApduCommand::CreateSign(app_param, challenge_param, key_handle); |
37 if (!sign_cmd) { | 38 if (!sign_cmd) { |
38 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 39 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
39 return; | 40 return; |
40 } | 41 } |
41 DeviceTransact(std::move(sign_cmd), | 42 DeviceTransact(std::move(sign_cmd), |
42 base::Bind(&U2fDevice::OnSignComplete, | 43 base::Bind(&U2fDevice::OnSignComplete, |
43 weak_factory_.GetWeakPtr(), callback)); | 44 weak_factory_.GetWeakPtr(), callback)); |
44 } | 45 } |
45 | 46 |
46 void U2fDevice::Version(const VersionCallback& callback) { | 47 void U2fDevice::Version(const VersionCallback& callback) { |
47 std::unique_ptr<U2fApduCommand> version_cmd = U2fApduCommand::CreateVersion(); | 48 std::unique_ptr<U2fApduCommand> version_cmd = U2fApduCommand::CreateVersion(); |
48 if (!version_cmd) { | 49 if (!version_cmd) { |
49 callback.Run(false, ProtocolVersion::UNKNOWN); | 50 callback.Run(false, ProtocolVersion::UNKNOWN); |
50 return; | 51 return; |
51 } | 52 } |
52 DeviceTransact(std::move(version_cmd), | 53 DeviceTransact(std::move(version_cmd), |
53 base::Bind(&U2fDevice::OnVersionComplete, | 54 base::Bind(&U2fDevice::OnVersionComplete, |
54 weak_factory_.GetWeakPtr(), callback)); | 55 weak_factory_.GetWeakPtr(), callback)); |
55 } | 56 } |
56 | 57 |
57 void U2fDevice::OnRegisterComplete( | 58 void U2fDevice::OnRegisterComplete( |
58 const MessageCallback& callback, | 59 const MessageCallback& callback, |
59 bool success, | 60 bool success, |
60 std::unique_ptr<U2fApduResponse> register_response) { | 61 std::unique_ptr<U2fApduResponse> register_response) { |
61 if (!success || !register_response) { | 62 if (!success || !register_response) { |
62 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); | 63 callback.Run(U2fReturnCode::FAILURE, std::vector<uint8_t>()); |
63 return; | 64 return; |
64 } | 65 } |
65 switch (register_response->status()) { | 66 switch (register_response->status()) { |
66 case U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED: | 67 case U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED: |
67 callback.Run(ReturnCode::CONDITIONS_NOT_SATISFIED, | 68 callback.Run(U2fReturnCode::CONDITIONS_NOT_SATISFIED, |
68 std::vector<uint8_t>()); | 69 std::vector<uint8_t>()); |
69 break; | 70 break; |
70 case U2fApduResponse::Status::SW_NO_ERROR: | 71 case U2fApduResponse::Status::SW_NO_ERROR: |
71 callback.Run(ReturnCode::SUCCESS, register_response->data()); | 72 callback.Run(U2fReturnCode::SUCCESS, register_response->data()); |
72 break; | 73 break; |
73 case U2fApduResponse::Status::SW_WRONG_DATA: | 74 case U2fApduResponse::Status::SW_WRONG_DATA: |
74 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 75 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
75 break; | 76 break; |
76 default: | 77 default: |
77 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); | 78 callback.Run(U2fReturnCode::FAILURE, std::vector<uint8_t>()); |
78 break; | 79 break; |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 void U2fDevice::OnSignComplete(const MessageCallback& callback, | 83 void U2fDevice::OnSignComplete(const MessageCallback& callback, |
83 bool success, | 84 bool success, |
84 std::unique_ptr<U2fApduResponse> sign_response) { | 85 std::unique_ptr<U2fApduResponse> sign_response) { |
85 if (!success || !sign_response) { | 86 if (!success || !sign_response) { |
86 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); | 87 callback.Run(U2fReturnCode::FAILURE, std::vector<uint8_t>()); |
87 return; | 88 return; |
88 } | 89 } |
89 switch (sign_response->status()) { | 90 switch (sign_response->status()) { |
90 case U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED: | 91 case U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED: |
91 callback.Run(ReturnCode::CONDITIONS_NOT_SATISFIED, | 92 callback.Run(U2fReturnCode::CONDITIONS_NOT_SATISFIED, |
92 std::vector<uint8_t>()); | 93 std::vector<uint8_t>()); |
93 break; | 94 break; |
94 case U2fApduResponse::Status::SW_NO_ERROR: | 95 case U2fApduResponse::Status::SW_NO_ERROR: |
95 callback.Run(ReturnCode::SUCCESS, sign_response->data()); | 96 callback.Run(U2fReturnCode::SUCCESS, sign_response->data()); |
96 break; | 97 break; |
97 case U2fApduResponse::Status::SW_WRONG_DATA: | 98 case U2fApduResponse::Status::SW_WRONG_DATA: |
98 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 99 case U2fApduResponse::Status::SW_WRONG_LENGTH: |
99 break; | |
100 default: | 100 default: |
101 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); | 101 callback.Run(U2fReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
102 break; | 102 break; |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 void U2fDevice::OnVersionComplete( | 106 void U2fDevice::OnVersionComplete( |
107 const VersionCallback& callback, | 107 const VersionCallback& callback, |
108 bool success, | 108 bool success, |
109 std::unique_ptr<U2fApduResponse> version_response) { | 109 std::unique_ptr<U2fApduResponse> version_response) { |
110 if (success && version_response && | 110 if (success && version_response && |
111 version_response->status() == U2fApduResponse::Status::SW_NO_ERROR && | 111 version_response->status() == U2fApduResponse::Status::SW_NO_ERROR && |
(...skipping 14 matching lines...) Expand all Loading... |
126 U2fApduResponse::Status::SW_NO_ERROR && | 126 U2fApduResponse::Status::SW_NO_ERROR && |
127 legacy_version_response->data() == | 127 legacy_version_response->data() == |
128 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { | 128 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { |
129 callback.Run(success, ProtocolVersion::U2F_V2); | 129 callback.Run(success, ProtocolVersion::U2F_V2); |
130 return; | 130 return; |
131 } | 131 } |
132 callback.Run(success, ProtocolVersion::UNKNOWN); | 132 callback.Run(success, ProtocolVersion::UNKNOWN); |
133 } | 133 } |
134 | 134 |
135 } // namespace device | 135 } // namespace device |
OLD | NEW |