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" |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "u2f_apdu_command.h" | 8 #include "u2f_apdu_command.h" |
7 #include "u2f_device.h" | 9 #include "u2f_apdu_response.h" |
8 | 10 |
9 namespace device { | 11 namespace device { |
10 | 12 |
11 U2fDevice::U2fDevice() : weak_factory_(this) {} | 13 U2fDevice::U2fDevice() : weak_factory_(this) {} |
12 | 14 |
13 U2fDevice::~U2fDevice() {} | 15 U2fDevice::~U2fDevice() {} |
14 | 16 |
15 void U2fDevice::Register(const std::vector<uint8_t>& app_param, | 17 void U2fDevice::Register(const std::vector<uint8_t>& app_param, |
16 U2fDevice::ProtocolVersion version, | |
17 const std::vector<uint8_t>& challenge_param, | 18 const std::vector<uint8_t>& challenge_param, |
18 const MessageCallback& callback) { | 19 const MessageCallback& callback) { |
19 scoped_refptr<U2fApduCommand> register_cmd = | 20 scoped_refptr<U2fApduCommand> register_cmd = |
20 U2fApduCommand::CreateRegister(app_param, challenge_param); | 21 U2fApduCommand::CreateRegister(app_param, challenge_param); |
21 if (!register_cmd) { | 22 if (!register_cmd) { |
22 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); | 23 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
23 return; | 24 return; |
24 } | 25 } |
25 DeviceTransact(std::move(register_cmd), | 26 DeviceTransact(std::move(register_cmd), |
26 base::Bind(&U2fDevice::OnRegisterComplete, | 27 base::Bind(&U2fDevice::OnRegisterComplete, |
(...skipping 23 matching lines...) Expand all Loading... |
50 } | 51 } |
51 DeviceTransact(std::move(version_cmd), | 52 DeviceTransact(std::move(version_cmd), |
52 base::Bind(&U2fDevice::OnVersionComplete, | 53 base::Bind(&U2fDevice::OnVersionComplete, |
53 weak_factory_.GetWeakPtr(), callback)); | 54 weak_factory_.GetWeakPtr(), callback)); |
54 } | 55 } |
55 | 56 |
56 void U2fDevice::OnRegisterComplete( | 57 void U2fDevice::OnRegisterComplete( |
57 const MessageCallback& callback, | 58 const MessageCallback& callback, |
58 bool success, | 59 bool success, |
59 scoped_refptr<U2fApduResponse> register_response) { | 60 scoped_refptr<U2fApduResponse> register_response) { |
60 NOTIMPLEMENTED(); | 61 if (!success || !register_response) { |
| 62 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); |
| 63 return; |
| 64 } |
| 65 switch (register_response->status()) { |
| 66 case U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED: |
| 67 callback.Run(ReturnCode::CONDITIONS_NOT_SATISFIED, |
| 68 std::vector<uint8_t>()); |
| 69 break; |
| 70 case U2fApduResponse::Status::SW_NO_ERROR: |
| 71 callback.Run(ReturnCode::SUCCESS, register_response->data()); |
| 72 break; |
| 73 case U2fApduResponse::Status::SW_WRONG_DATA: |
| 74 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
| 75 break; |
| 76 default: |
| 77 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); |
| 78 break; |
| 79 } |
61 } | 80 } |
62 | 81 |
63 void U2fDevice::OnSignComplete(const MessageCallback& callback, | 82 void U2fDevice::OnSignComplete(const MessageCallback& callback, |
64 bool success, | 83 bool success, |
65 scoped_refptr<U2fApduResponse> sign_response) { | 84 scoped_refptr<U2fApduResponse> sign_response) { |
66 NOTIMPLEMENTED(); | 85 if (!success || !sign_response) { |
| 86 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); |
| 87 return; |
| 88 } |
| 89 switch (sign_response->status()) { |
| 90 case U2fApduResponse::Status::SW_CONDITIONS_NOT_SATISFIED: |
| 91 callback.Run(ReturnCode::CONDITIONS_NOT_SATISFIED, |
| 92 std::vector<uint8_t>()); |
| 93 break; |
| 94 case U2fApduResponse::Status::SW_NO_ERROR: |
| 95 callback.Run(ReturnCode::SUCCESS, sign_response->data()); |
| 96 break; |
| 97 case U2fApduResponse::Status::SW_WRONG_DATA: |
| 98 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
| 99 break; |
| 100 default: |
| 101 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); |
| 102 break; |
| 103 } |
67 } | 104 } |
68 | 105 |
69 void U2fDevice::OnVersionComplete( | 106 void U2fDevice::OnVersionComplete( |
70 const VersionCallback& callback, | 107 const VersionCallback& callback, |
71 bool success, | 108 bool success, |
72 scoped_refptr<U2fApduResponse> version_response) { | 109 scoped_refptr<U2fApduResponse> version_response) { |
73 if (success && version_response && | 110 if (success && version_response && |
74 version_response->status() == U2fApduResponse::Status::SW_NO_ERROR && | 111 version_response->status() == U2fApduResponse::Status::SW_NO_ERROR && |
75 version_response->data() == | 112 version_response->data() == |
76 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { | 113 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { |
77 callback.Run(success, ProtocolVersion::U2F_V2); | 114 callback.Run(success, ProtocolVersion::U2F_V2); |
78 return; | 115 return; |
79 } | 116 } |
80 callback.Run(success, ProtocolVersion::UNKNOWN); | 117 callback.Run(success, ProtocolVersion::UNKNOWN); |
81 } | 118 } |
82 | 119 |
83 void U2fDevice::OnLegacyVersionComplete( | 120 void U2fDevice::OnLegacyVersionComplete( |
84 const VersionCallback& callback, | 121 const VersionCallback& callback, |
85 bool success, | 122 bool success, |
86 scoped_refptr<U2fApduResponse> legacy_version_response) { | 123 scoped_refptr<U2fApduResponse> legacy_version_response) { |
87 NOTIMPLEMENTED(); | 124 if (success && legacy_version_response && |
| 125 legacy_version_response->status() == |
| 126 U2fApduResponse::Status::SW_NO_ERROR && |
| 127 legacy_version_response->data() == |
| 128 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { |
| 129 callback.Run(success, ProtocolVersion::U2F_V2); |
| 130 return; |
| 131 } |
| 132 callback.Run(success, ProtocolVersion::UNKNOWN); |
88 } | 133 } |
89 | 134 |
90 } // namespace device | 135 } // namespace device |
OLD | NEW |