| 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 #ifndef DEVICE_U2F_U2F_APDU_COMMAND_H_ | 5 #ifndef DEVICE_U2F_U2F_APDU_COMMAND_H_ |
| 6 #define DEVICE_U2F_U2F_APDU_COMMAND_H_ | 6 #define DEVICE_U2F_U2F_APDU_COMMAND_H_ |
| 7 | 7 |
| 8 #include <cinttypes> |
| 9 #include <memory> |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 11 #include "base/memory/ref_counted.h" | |
| 12 | 13 |
| 13 namespace device { | 14 namespace device { |
| 14 | 15 |
| 15 // APDU commands are defined as part of ISO 7816-4. Commands can be serialized | 16 // APDU commands are defined as part of ISO 7816-4. Commands can be serialized |
| 16 // into either short length encodings, where the maximum data length is 255 | 17 // into either short length encodings, where the maximum data length is 255 |
| 17 // bytes, or an extended length encoding, where the maximum data length is 65536 | 18 // bytes, or an extended length encoding, where the maximum data length is 65536 |
| 18 // bytes. This class implements only the extended length encoding. Serialized | 19 // bytes. This class implements only the extended length encoding. Serialized |
| 19 // commands consist of a CLA byte, denoting the class of instruction, an INS | 20 // commands consist of a CLA byte, denoting the class of instruction, an INS |
| 20 // byte, denoting the instruction code, P1 and P2, each one byte denoting | 21 // byte, denoting the instruction code, P1 and P2, each one byte denoting |
| 21 // instruction parameters, a length field (Lc), a data field of length Lc, and | 22 // instruction parameters, a length field (Lc), a data field of length Lc, and |
| 22 // a maximum expected response length (Le). | 23 // a maximum expected response length (Le). |
| 23 class U2fApduCommand : public base::RefCountedThreadSafe<U2fApduCommand> { | 24 class U2fApduCommand { |
| 24 public: | 25 public: |
| 26 U2fApduCommand(); |
| 27 U2fApduCommand(uint8_t cla, |
| 28 uint8_t ins, |
| 29 uint8_t p1, |
| 30 uint8_t p2, |
| 31 size_t response_length, |
| 32 std::vector<uint8_t> data, |
| 33 std::vector<uint8_t> suffix); |
| 34 ~U2fApduCommand(); |
| 35 |
| 25 // Construct an apdu command from the serialized message data | 36 // Construct an apdu command from the serialized message data |
| 26 static scoped_refptr<U2fApduCommand> CreateFromMessage( | 37 static std::unique_ptr<U2fApduCommand> CreateFromMessage( |
| 27 const std::vector<uint8_t>& data); | 38 const std::vector<uint8_t>& data); |
| 28 // Create an empty apdu command object | |
| 29 static scoped_refptr<U2fApduCommand> Create(); | |
| 30 // Returns serialized message data | 39 // Returns serialized message data |
| 31 std::vector<uint8_t> GetEncodedCommand() const; | 40 std::vector<uint8_t> GetEncodedCommand() const; |
| 32 void set_cla(uint8_t cla) { cla_ = cla; } | 41 void set_cla(uint8_t cla) { cla_ = cla; } |
| 33 void set_ins(uint8_t ins) { ins_ = ins; } | 42 void set_ins(uint8_t ins) { ins_ = ins; } |
| 34 void set_p1(uint8_t p1) { p1_ = p1; } | 43 void set_p1(uint8_t p1) { p1_ = p1; } |
| 35 void set_p2(uint8_t p2) { p2_ = p2; } | 44 void set_p2(uint8_t p2) { p2_ = p2; } |
| 36 void set_data(const std::vector<uint8_t>& data) { data_ = data; } | 45 void set_data(const std::vector<uint8_t>& data) { data_ = data; } |
| 37 void set_response_length(size_t response_length) { | 46 void set_response_length(size_t response_length) { |
| 38 response_length_ = response_length; | 47 response_length_ = response_length; |
| 39 } | 48 } |
| 40 void set_suffix(const std::vector<uint8_t>& suffix) { suffix_ = suffix; } | 49 void set_suffix(const std::vector<uint8_t>& suffix) { suffix_ = suffix; } |
| 41 static scoped_refptr<U2fApduCommand> CreateRegister( | 50 static std::unique_ptr<U2fApduCommand> CreateRegister( |
| 42 const std::vector<uint8_t>& appid_digest, | 51 const std::vector<uint8_t>& appid_digest, |
| 43 const std::vector<uint8_t>& challenge_digest); | 52 const std::vector<uint8_t>& challenge_digest); |
| 44 static scoped_refptr<U2fApduCommand> CreateVersion(); | 53 static std::unique_ptr<U2fApduCommand> CreateVersion(); |
| 45 // Early U2F drafts defined a non-ISO 7816-4 conforming layout | 54 // Early U2F drafts defined a non-ISO 7816-4 conforming layout |
| 46 static scoped_refptr<U2fApduCommand> CreateLegacyVersion(); | 55 static std::unique_ptr<U2fApduCommand> CreateLegacyVersion(); |
| 47 static scoped_refptr<U2fApduCommand> CreateSign( | 56 static std::unique_ptr<U2fApduCommand> CreateSign( |
| 48 const std::vector<uint8_t>& appid_digest, | 57 const std::vector<uint8_t>& appid_digest, |
| 49 const std::vector<uint8_t>& challenge_digest, | 58 const std::vector<uint8_t>& challenge_digest, |
| 50 const std::vector<uint8_t>& key_handle); | 59 const std::vector<uint8_t>& key_handle); |
| 51 | 60 |
| 52 private: | 61 private: |
| 53 friend class base::RefCountedThreadSafe<U2fApduCommand>; | |
| 54 friend class U2fApduBuilder; | |
| 55 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestDeserializeBasic); | 62 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestDeserializeBasic); |
| 56 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestDeserializeComplex); | 63 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestDeserializeComplex); |
| 57 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestSerializeEdgeCases); | 64 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestSerializeEdgeCases); |
| 58 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateSign); | 65 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateSign); |
| 59 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateRegister); | 66 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateRegister); |
| 60 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateVersion); | 67 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateVersion); |
| 61 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateLegacyVersion); | 68 FRIEND_TEST_ALL_PREFIXES(U2fApduTest, TestCreateLegacyVersion); |
| 62 | 69 |
| 63 static constexpr size_t kApduMinHeader = 4; | 70 static constexpr size_t kApduMinHeader = 4; |
| 64 static constexpr size_t kApduMaxHeader = 7; | 71 static constexpr size_t kApduMaxHeader = 7; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 76 static constexpr uint8_t kInsU2fVersion = 0x03; | 83 static constexpr uint8_t kInsU2fVersion = 0x03; |
| 77 // P1 instructions | 84 // P1 instructions |
| 78 static constexpr uint8_t kP1TupRequired = 0x01; | 85 static constexpr uint8_t kP1TupRequired = 0x01; |
| 79 static constexpr uint8_t kP1TupConsumed = 0x02; | 86 static constexpr uint8_t kP1TupConsumed = 0x02; |
| 80 static constexpr uint8_t kP1TupRequiredConsumed = | 87 static constexpr uint8_t kP1TupRequiredConsumed = |
| 81 kP1TupRequired | kP1TupConsumed; | 88 kP1TupRequired | kP1TupConsumed; |
| 82 static constexpr size_t kMaxKeyHandleLength = 255; | 89 static constexpr size_t kMaxKeyHandleLength = 255; |
| 83 static constexpr size_t kChallengeDigestLen = 32; | 90 static constexpr size_t kChallengeDigestLen = 32; |
| 84 static constexpr size_t kAppIdDigestLen = 32; | 91 static constexpr size_t kAppIdDigestLen = 32; |
| 85 | 92 |
| 86 U2fApduCommand(); | |
| 87 U2fApduCommand(uint8_t cla, | |
| 88 uint8_t ins, | |
| 89 uint8_t p1, | |
| 90 uint8_t p2, | |
| 91 size_t response_length, | |
| 92 std::vector<uint8_t> data, | |
| 93 std::vector<uint8_t> suffix); | |
| 94 ~U2fApduCommand(); | |
| 95 | |
| 96 uint8_t cla_; | 93 uint8_t cla_; |
| 97 uint8_t ins_; | 94 uint8_t ins_; |
| 98 uint8_t p1_; | 95 uint8_t p1_; |
| 99 uint8_t p2_; | 96 uint8_t p2_; |
| 100 size_t response_length_; | 97 size_t response_length_; |
| 101 std::vector<uint8_t> data_; | 98 std::vector<uint8_t> data_; |
| 102 std::vector<uint8_t> suffix_; | 99 std::vector<uint8_t> suffix_; |
| 103 }; | 100 }; |
| 104 } // namespace device | 101 } // namespace device |
| 105 | 102 |
| 106 #endif // DEVICE_U2F_U2F_APDU_COMMAND_H_ | 103 #endif // DEVICE_U2F_U2F_APDU_COMMAND_H_ |
| OLD | NEW |