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