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