| 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 scoped_refptr<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(ReturnCode::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 scoped_refptr<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(ReturnCode::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 scoped_refptr<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 scoped_refptr<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(ReturnCode::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(ReturnCode::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(ReturnCode::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(ReturnCode::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(ReturnCode::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 scoped_refptr<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(ReturnCode::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(ReturnCode::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(ReturnCode::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 callback.Run(ReturnCode::INVALID_PARAMS, std::vector<uint8_t>()); |
| 99 break; | 99 break; |
| 100 default: | 100 default: |
| 101 callback.Run(ReturnCode::FAILURE, std::vector<uint8_t>()); | 101 callback.Run(ReturnCode::FAILURE, 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 scoped_refptr<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 && |
| 112 version_response->data() == | 112 version_response->data() == |
| 113 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { | 113 std::vector<uint8_t>({'U', '2', 'F', '_', 'V', '2'})) { |
| 114 callback.Run(success, ProtocolVersion::U2F_V2); | 114 callback.Run(success, ProtocolVersion::U2F_V2); |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 callback.Run(success, ProtocolVersion::UNKNOWN); | 117 callback.Run(success, ProtocolVersion::UNKNOWN); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void U2fDevice::OnLegacyVersionComplete( | 120 void U2fDevice::OnLegacyVersionComplete( |
| 121 const VersionCallback& callback, | 121 const VersionCallback& callback, |
| 122 bool success, | 122 bool success, |
| 123 scoped_refptr<U2fApduResponse> legacy_version_response) { | 123 std::unique_ptr<U2fApduResponse> legacy_version_response) { |
| 124 if (success && legacy_version_response && | 124 if (success && legacy_version_response && |
| 125 legacy_version_response->status() == | 125 legacy_version_response->status() == |
| 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 |