| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_MESSAGE_H_ | 5 #ifndef DEVICE_U2F_U2F_MESSAGE_H_ |
| 6 #define DEVICE_U2F_U2F_MESSAGE_H_ | 6 #define DEVICE_U2F_U2F_MESSAGE_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "device/u2f/u2f_packet.h" | 12 #include "device/u2f/u2f_packet.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 class IOBufferWithSize; | 15 class IOBufferWithSize; |
| 16 } // namespace net | 16 } // namespace net |
| 17 | 17 |
| 18 namespace device { | 18 namespace device { |
| 19 | 19 |
| 20 // U2fMessages are defined by the specification at | 20 // U2fMessages are defined by the specification at |
| 21 // http://fidoalliance.org/specs/u2f-specs-1.0-bt-nfc-id-amendment/fido-u2f-hid-
protocol.html | 21 // http://fidoalliance.org/specs/u2f-specs-1.0-bt-nfc-id-amendment/fido-u2f-hid-
protocol.html |
| 22 // A U2fMessage object represents a list of U2fPackets. Basic information | 22 // A U2fMessage object represents a list of U2fPackets. Basic information |
| 23 // about the message are available through class methods. | 23 // about the message are available through class methods. |
| 24 class U2fMessage : public base::RefCountedThreadSafe<U2fMessage> { | 24 class U2fMessage { |
| 25 public: | 25 public: |
| 26 enum class Type : uint8_t { | 26 enum class Type : uint8_t { |
| 27 CMD_PING = 0x81, | 27 CMD_PING = 0x81, |
| 28 CMD_MSG = 0x83, | 28 CMD_MSG = 0x83, |
| 29 CMD_INIT = 0x86, | 29 CMD_INIT = 0x86, |
| 30 CMD_WINK = 0x88, | 30 CMD_WINK = 0x88, |
| 31 CMD_ERROR = 0xbf, | 31 CMD_ERROR = 0xbf, |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 static scoped_refptr<U2fMessage> Create(uint32_t channel_id, | 34 U2fMessage(uint32_t channel_id, Type type, const std::vector<uint8_t>& data); |
| 35 Type type, | 35 U2fMessage(std::unique_ptr<U2fInitPacket> init_packet, size_t remaining_size); |
| 36 const std::vector<uint8_t>& data); | 36 ~U2fMessage(); |
| 37 |
| 38 static std::unique_ptr<U2fMessage> Create(uint32_t channel_id, |
| 39 Type type, |
| 40 const std::vector<uint8_t>& data); |
| 37 // Reconstruct a message from serialized message data | 41 // Reconstruct a message from serialized message data |
| 38 static scoped_refptr<U2fMessage> CreateFromSerializedData( | 42 static std::unique_ptr<U2fMessage> CreateFromSerializedData( |
| 39 scoped_refptr<net::IOBufferWithSize> buf); | 43 scoped_refptr<net::IOBufferWithSize> buf); |
| 40 // Pop front of queue with next packet | 44 // Pop front of queue with next packet |
| 41 scoped_refptr<net::IOBufferWithSize> PopNextPacket(); | 45 scoped_refptr<net::IOBufferWithSize> PopNextPacket(); |
| 42 // Adds a continuation packet to the packet list, from the serialized | 46 // Adds a continuation packet to the packet list, from the serialized |
| 43 // response value | 47 // response value |
| 44 bool AddContinuationPacket(scoped_refptr<net::IOBufferWithSize> packet_buf); | 48 bool AddContinuationPacket(scoped_refptr<net::IOBufferWithSize> packet_buf); |
| 45 size_t NumPackets(); | 49 size_t NumPackets(); |
| 46 // Returns entire message payload | 50 // Returns entire message payload |
| 47 std::vector<uint8_t> GetMessagePayload() const; | 51 std::vector<uint8_t> GetMessagePayload() const; |
| 48 uint32_t channel_id() { return channel_id_; } | 52 uint32_t channel_id() { return channel_id_; } |
| 49 // Message construction complete | 53 // Message construction complete |
| 50 bool MessageComplete(); | 54 bool MessageComplete(); |
| 51 std::list<scoped_refptr<U2fPacket>>::const_iterator begin(); | 55 std::list<std::unique_ptr<U2fPacket>>::const_iterator begin(); |
| 52 std::list<scoped_refptr<U2fPacket>>::const_iterator end(); | 56 std::list<std::unique_ptr<U2fPacket>>::const_iterator end(); |
| 53 | |
| 54 protected: | |
| 55 virtual ~U2fMessage(); | |
| 56 | 57 |
| 57 private: | 58 private: |
| 58 friend class base::RefCountedThreadSafe<U2fMessage>; | |
| 59 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestMaxLengthPacketConstructors); | 59 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestMaxLengthPacketConstructors); |
| 60 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestMessagePartitoning); | 60 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestMessagePartitoning); |
| 61 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestDeconstruct); | 61 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestDeconstruct); |
| 62 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestMaxSize); | 62 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestMaxSize); |
| 63 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestDeserialize); | 63 FRIEND_TEST_ALL_PREFIXES(U2fMessageTest, TestDeserialize); |
| 64 | 64 |
| 65 static constexpr size_t kInitPacketHeader = 7; | 65 static constexpr size_t kInitPacketHeader = 7; |
| 66 static constexpr size_t kContinuationPacketHeader = 5; | 66 static constexpr size_t kContinuationPacketHeader = 5; |
| 67 static constexpr size_t kMaxHidPacketSize = 64; | 67 static constexpr size_t kMaxHidPacketSize = 64; |
| 68 static constexpr size_t kInitPacketDataSize = | 68 static constexpr size_t kInitPacketDataSize = |
| 69 kMaxHidPacketSize - kInitPacketHeader; | 69 kMaxHidPacketSize - kInitPacketHeader; |
| 70 static constexpr size_t kContinuationPacketDataSize = | 70 static constexpr size_t kContinuationPacketDataSize = |
| 71 kMaxHidPacketSize - kContinuationPacketHeader; | 71 kMaxHidPacketSize - kContinuationPacketHeader; |
| 72 // Messages are limited to an init packet and 128 continuation packets | 72 // Messages are limited to an init packet and 128 continuation packets |
| 73 // Maximum payload length therefore is 64-7 + 128 * (64-5) = 7609 bytes | 73 // Maximum payload length therefore is 64-7 + 128 * (64-5) = 7609 bytes |
| 74 static constexpr size_t kMaxMessageSize = 7609; | 74 static constexpr size_t kMaxMessageSize = 7609; |
| 75 | 75 |
| 76 U2fMessage(uint32_t channel_id, Type type, const std::vector<uint8_t>& data); | 76 std::list<std::unique_ptr<U2fPacket>> packets_; |
| 77 U2fMessage(scoped_refptr<U2fInitPacket> init_packet, size_t remaining_size); | |
| 78 | |
| 79 std::list<scoped_refptr<U2fPacket>> packets_; | |
| 80 size_t remaining_size_; | 77 size_t remaining_size_; |
| 81 uint32_t channel_id_; | 78 uint32_t channel_id_; |
| 82 }; | 79 }; |
| 83 } // namespace device | 80 } // namespace device |
| 84 | 81 |
| 85 #endif // DEVICE_U2F_U2F_MESSAGE_H_ | 82 #endif // DEVICE_U2F_U2F_MESSAGE_H_ |
| OLD | NEW |