| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/quic/quic_connection.h" | 5 #include "net/quic/quic_connection.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. | 86 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. |
| 87 class TaggingEncrypter : public QuicEncrypter { | 87 class TaggingEncrypter : public QuicEncrypter { |
| 88 public: | 88 public: |
| 89 explicit TaggingEncrypter(uint8 tag) | 89 explicit TaggingEncrypter(uint8 tag) |
| 90 : tag_(tag) { | 90 : tag_(tag) { |
| 91 } | 91 } |
| 92 | 92 |
| 93 virtual ~TaggingEncrypter() {} | 93 virtual ~TaggingEncrypter() {} |
| 94 | 94 |
| 95 // QuicEncrypter interface. | 95 // QuicEncrypter interface. |
| 96 virtual bool SetKey(StringPiece key) OVERRIDE { return true; } | 96 virtual bool SetKey(StringPiece key) override { return true; } |
| 97 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE { | 97 virtual bool SetNoncePrefix(StringPiece nonce_prefix) override { |
| 98 return true; | 98 return true; |
| 99 } | 99 } |
| 100 | 100 |
| 101 virtual bool Encrypt(StringPiece nonce, | 101 virtual bool Encrypt(StringPiece nonce, |
| 102 StringPiece associated_data, | 102 StringPiece associated_data, |
| 103 StringPiece plaintext, | 103 StringPiece plaintext, |
| 104 unsigned char* output) OVERRIDE { | 104 unsigned char* output) override { |
| 105 memcpy(output, plaintext.data(), plaintext.size()); | 105 memcpy(output, plaintext.data(), plaintext.size()); |
| 106 output += plaintext.size(); | 106 output += plaintext.size(); |
| 107 memset(output, tag_, kTagSize); | 107 memset(output, tag_, kTagSize); |
| 108 return true; | 108 return true; |
| 109 } | 109 } |
| 110 | 110 |
| 111 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, | 111 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, |
| 112 StringPiece associated_data, | 112 StringPiece associated_data, |
| 113 StringPiece plaintext) OVERRIDE { | 113 StringPiece plaintext) override { |
| 114 const size_t len = plaintext.size() + kTagSize; | 114 const size_t len = plaintext.size() + kTagSize; |
| 115 uint8* buffer = new uint8[len]; | 115 uint8* buffer = new uint8[len]; |
| 116 Encrypt(StringPiece(), associated_data, plaintext, buffer); | 116 Encrypt(StringPiece(), associated_data, plaintext, buffer); |
| 117 return new QuicData(reinterpret_cast<char*>(buffer), len, true); | 117 return new QuicData(reinterpret_cast<char*>(buffer), len, true); |
| 118 } | 118 } |
| 119 | 119 |
| 120 virtual size_t GetKeySize() const OVERRIDE { return 0; } | 120 virtual size_t GetKeySize() const override { return 0; } |
| 121 virtual size_t GetNoncePrefixSize() const OVERRIDE { return 0; } | 121 virtual size_t GetNoncePrefixSize() const override { return 0; } |
| 122 | 122 |
| 123 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE { | 123 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { |
| 124 return ciphertext_size - kTagSize; | 124 return ciphertext_size - kTagSize; |
| 125 } | 125 } |
| 126 | 126 |
| 127 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE { | 127 virtual size_t GetCiphertextSize(size_t plaintext_size) const override { |
| 128 return plaintext_size + kTagSize; | 128 return plaintext_size + kTagSize; |
| 129 } | 129 } |
| 130 | 130 |
| 131 virtual StringPiece GetKey() const OVERRIDE { | 131 virtual StringPiece GetKey() const override { |
| 132 return StringPiece(); | 132 return StringPiece(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 virtual StringPiece GetNoncePrefix() const OVERRIDE { | 135 virtual StringPiece GetNoncePrefix() const override { |
| 136 return StringPiece(); | 136 return StringPiece(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 private: | 139 private: |
| 140 enum { | 140 enum { |
| 141 kTagSize = 12, | 141 kTagSize = 12, |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 const uint8 tag_; | 144 const uint8 tag_; |
| 145 | 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter); | 146 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter); |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 // TaggingDecrypter ensures that the final kTagSize bytes of the message all | 149 // TaggingDecrypter ensures that the final kTagSize bytes of the message all |
| 150 // have the same value and then removes them. | 150 // have the same value and then removes them. |
| 151 class TaggingDecrypter : public QuicDecrypter { | 151 class TaggingDecrypter : public QuicDecrypter { |
| 152 public: | 152 public: |
| 153 virtual ~TaggingDecrypter() {} | 153 virtual ~TaggingDecrypter() {} |
| 154 | 154 |
| 155 // QuicDecrypter interface | 155 // QuicDecrypter interface |
| 156 virtual bool SetKey(StringPiece key) OVERRIDE { return true; } | 156 virtual bool SetKey(StringPiece key) override { return true; } |
| 157 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE { | 157 virtual bool SetNoncePrefix(StringPiece nonce_prefix) override { |
| 158 return true; | 158 return true; |
| 159 } | 159 } |
| 160 | 160 |
| 161 virtual bool Decrypt(StringPiece nonce, | 161 virtual bool Decrypt(StringPiece nonce, |
| 162 StringPiece associated_data, | 162 StringPiece associated_data, |
| 163 StringPiece ciphertext, | 163 StringPiece ciphertext, |
| 164 unsigned char* output, | 164 unsigned char* output, |
| 165 size_t* output_length) OVERRIDE { | 165 size_t* output_length) override { |
| 166 if (ciphertext.size() < kTagSize) { | 166 if (ciphertext.size() < kTagSize) { |
| 167 return false; | 167 return false; |
| 168 } | 168 } |
| 169 if (!CheckTag(ciphertext, GetTag(ciphertext))) { | 169 if (!CheckTag(ciphertext, GetTag(ciphertext))) { |
| 170 return false; | 170 return false; |
| 171 } | 171 } |
| 172 *output_length = ciphertext.size() - kTagSize; | 172 *output_length = ciphertext.size() - kTagSize; |
| 173 memcpy(output, ciphertext.data(), *output_length); | 173 memcpy(output, ciphertext.data(), *output_length); |
| 174 return true; | 174 return true; |
| 175 } | 175 } |
| 176 | 176 |
| 177 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, | 177 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, |
| 178 StringPiece associated_data, | 178 StringPiece associated_data, |
| 179 StringPiece ciphertext) OVERRIDE { | 179 StringPiece ciphertext) override { |
| 180 if (ciphertext.size() < kTagSize) { | 180 if (ciphertext.size() < kTagSize) { |
| 181 return nullptr; | 181 return nullptr; |
| 182 } | 182 } |
| 183 if (!CheckTag(ciphertext, GetTag(ciphertext))) { | 183 if (!CheckTag(ciphertext, GetTag(ciphertext))) { |
| 184 return nullptr; | 184 return nullptr; |
| 185 } | 185 } |
| 186 const size_t len = ciphertext.size() - kTagSize; | 186 const size_t len = ciphertext.size() - kTagSize; |
| 187 uint8* buf = new uint8[len]; | 187 uint8* buf = new uint8[len]; |
| 188 memcpy(buf, ciphertext.data(), len); | 188 memcpy(buf, ciphertext.data(), len); |
| 189 return new QuicData(reinterpret_cast<char*>(buf), len, | 189 return new QuicData(reinterpret_cast<char*>(buf), len, |
| 190 true /* owns buffer */); | 190 true /* owns buffer */); |
| 191 } | 191 } |
| 192 | 192 |
| 193 virtual StringPiece GetKey() const OVERRIDE { return StringPiece(); } | 193 virtual StringPiece GetKey() const override { return StringPiece(); } |
| 194 virtual StringPiece GetNoncePrefix() const OVERRIDE { return StringPiece(); } | 194 virtual StringPiece GetNoncePrefix() const override { return StringPiece(); } |
| 195 | 195 |
| 196 protected: | 196 protected: |
| 197 virtual uint8 GetTag(StringPiece ciphertext) { | 197 virtual uint8 GetTag(StringPiece ciphertext) { |
| 198 return ciphertext.data()[ciphertext.size()-1]; | 198 return ciphertext.data()[ciphertext.size()-1]; |
| 199 } | 199 } |
| 200 | 200 |
| 201 private: | 201 private: |
| 202 enum { | 202 enum { |
| 203 kTagSize = 12, | 203 kTagSize = 12, |
| 204 }; | 204 }; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 215 }; | 215 }; |
| 216 | 216 |
| 217 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message | 217 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message |
| 218 // match the expected value. | 218 // match the expected value. |
| 219 class StrictTaggingDecrypter : public TaggingDecrypter { | 219 class StrictTaggingDecrypter : public TaggingDecrypter { |
| 220 public: | 220 public: |
| 221 explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {} | 221 explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {} |
| 222 virtual ~StrictTaggingDecrypter() {} | 222 virtual ~StrictTaggingDecrypter() {} |
| 223 | 223 |
| 224 // TaggingQuicDecrypter | 224 // TaggingQuicDecrypter |
| 225 virtual uint8 GetTag(StringPiece ciphertext) OVERRIDE { | 225 virtual uint8 GetTag(StringPiece ciphertext) override { |
| 226 return tag_; | 226 return tag_; |
| 227 } | 227 } |
| 228 | 228 |
| 229 private: | 229 private: |
| 230 const uint8 tag_; | 230 const uint8 tag_; |
| 231 }; | 231 }; |
| 232 | 232 |
| 233 class TestConnectionHelper : public QuicConnectionHelperInterface { | 233 class TestConnectionHelper : public QuicConnectionHelperInterface { |
| 234 public: | 234 public: |
| 235 class TestAlarm : public QuicAlarm { | 235 class TestAlarm : public QuicAlarm { |
| 236 public: | 236 public: |
| 237 explicit TestAlarm(QuicAlarm::Delegate* delegate) | 237 explicit TestAlarm(QuicAlarm::Delegate* delegate) |
| 238 : QuicAlarm(delegate) { | 238 : QuicAlarm(delegate) { |
| 239 } | 239 } |
| 240 | 240 |
| 241 virtual void SetImpl() OVERRIDE {} | 241 virtual void SetImpl() override {} |
| 242 virtual void CancelImpl() OVERRIDE {} | 242 virtual void CancelImpl() override {} |
| 243 using QuicAlarm::Fire; | 243 using QuicAlarm::Fire; |
| 244 }; | 244 }; |
| 245 | 245 |
| 246 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) | 246 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) |
| 247 : clock_(clock), | 247 : clock_(clock), |
| 248 random_generator_(random_generator) { | 248 random_generator_(random_generator) { |
| 249 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); | 249 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 250 } | 250 } |
| 251 | 251 |
| 252 // QuicConnectionHelperInterface | 252 // QuicConnectionHelperInterface |
| 253 virtual const QuicClock* GetClock() const OVERRIDE { | 253 virtual const QuicClock* GetClock() const override { |
| 254 return clock_; | 254 return clock_; |
| 255 } | 255 } |
| 256 | 256 |
| 257 virtual QuicRandom* GetRandomGenerator() OVERRIDE { | 257 virtual QuicRandom* GetRandomGenerator() override { |
| 258 return random_generator_; | 258 return random_generator_; |
| 259 } | 259 } |
| 260 | 260 |
| 261 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) OVERRIDE { | 261 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override { |
| 262 return new TestAlarm(delegate); | 262 return new TestAlarm(delegate); |
| 263 } | 263 } |
| 264 | 264 |
| 265 private: | 265 private: |
| 266 MockClock* clock_; | 266 MockClock* clock_; |
| 267 MockRandom* random_generator_; | 267 MockRandom* random_generator_; |
| 268 | 268 |
| 269 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); | 269 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); |
| 270 }; | 270 }; |
| 271 | 271 |
| 272 class TestPacketWriter : public QuicPacketWriter { | 272 class TestPacketWriter : public QuicPacketWriter { |
| 273 public: | 273 public: |
| 274 explicit TestPacketWriter(QuicVersion version) | 274 explicit TestPacketWriter(QuicVersion version) |
| 275 : version_(version), | 275 : version_(version), |
| 276 framer_(SupportedVersions(version_)), | 276 framer_(SupportedVersions(version_)), |
| 277 last_packet_size_(0), | 277 last_packet_size_(0), |
| 278 write_blocked_(false), | 278 write_blocked_(false), |
| 279 block_on_next_write_(false), | 279 block_on_next_write_(false), |
| 280 is_write_blocked_data_buffered_(false), | 280 is_write_blocked_data_buffered_(false), |
| 281 final_bytes_of_last_packet_(0), | 281 final_bytes_of_last_packet_(0), |
| 282 final_bytes_of_previous_packet_(0), | 282 final_bytes_of_previous_packet_(0), |
| 283 use_tagging_decrypter_(false), | 283 use_tagging_decrypter_(false), |
| 284 packets_write_attempts_(0) { | 284 packets_write_attempts_(0) { |
| 285 } | 285 } |
| 286 | 286 |
| 287 // QuicPacketWriter interface | 287 // QuicPacketWriter interface |
| 288 virtual WriteResult WritePacket( | 288 virtual WriteResult WritePacket( |
| 289 const char* buffer, size_t buf_len, | 289 const char* buffer, size_t buf_len, |
| 290 const IPAddressNumber& self_address, | 290 const IPAddressNumber& self_address, |
| 291 const IPEndPoint& peer_address) OVERRIDE { | 291 const IPEndPoint& peer_address) override { |
| 292 QuicEncryptedPacket packet(buffer, buf_len); | 292 QuicEncryptedPacket packet(buffer, buf_len); |
| 293 ++packets_write_attempts_; | 293 ++packets_write_attempts_; |
| 294 | 294 |
| 295 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { | 295 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { |
| 296 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; | 296 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; |
| 297 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, | 297 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, |
| 298 sizeof(final_bytes_of_last_packet_)); | 298 sizeof(final_bytes_of_last_packet_)); |
| 299 } | 299 } |
| 300 | 300 |
| 301 if (use_tagging_decrypter_) { | 301 if (use_tagging_decrypter_) { |
| 302 framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE); | 302 framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE); |
| 303 } | 303 } |
| 304 EXPECT_TRUE(framer_.ProcessPacket(packet)); | 304 EXPECT_TRUE(framer_.ProcessPacket(packet)); |
| 305 if (block_on_next_write_) { | 305 if (block_on_next_write_) { |
| 306 write_blocked_ = true; | 306 write_blocked_ = true; |
| 307 block_on_next_write_ = false; | 307 block_on_next_write_ = false; |
| 308 } | 308 } |
| 309 if (IsWriteBlocked()) { | 309 if (IsWriteBlocked()) { |
| 310 return WriteResult(WRITE_STATUS_BLOCKED, -1); | 310 return WriteResult(WRITE_STATUS_BLOCKED, -1); |
| 311 } | 311 } |
| 312 last_packet_size_ = packet.length(); | 312 last_packet_size_ = packet.length(); |
| 313 return WriteResult(WRITE_STATUS_OK, last_packet_size_); | 313 return WriteResult(WRITE_STATUS_OK, last_packet_size_); |
| 314 } | 314 } |
| 315 | 315 |
| 316 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE { | 316 virtual bool IsWriteBlockedDataBuffered() const override { |
| 317 return is_write_blocked_data_buffered_; | 317 return is_write_blocked_data_buffered_; |
| 318 } | 318 } |
| 319 | 319 |
| 320 virtual bool IsWriteBlocked() const OVERRIDE { return write_blocked_; } | 320 virtual bool IsWriteBlocked() const override { return write_blocked_; } |
| 321 | 321 |
| 322 virtual void SetWritable() OVERRIDE { write_blocked_ = false; } | 322 virtual void SetWritable() override { write_blocked_ = false; } |
| 323 | 323 |
| 324 void BlockOnNextWrite() { block_on_next_write_ = true; } | 324 void BlockOnNextWrite() { block_on_next_write_ = true; } |
| 325 | 325 |
| 326 const QuicPacketHeader& header() { return framer_.header(); } | 326 const QuicPacketHeader& header() { return framer_.header(); } |
| 327 | 327 |
| 328 size_t frame_count() const { return framer_.num_frames(); } | 328 size_t frame_count() const { return framer_.num_frames(); } |
| 329 | 329 |
| 330 const vector<QuicAckFrame>& ack_frames() const { | 330 const vector<QuicAckFrame>& ack_frames() const { |
| 331 return framer_.ack_frames(); | 331 return framer_.ack_frames(); |
| 332 } | 332 } |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 } | 584 } |
| 585 | 585 |
| 586 DISALLOW_COPY_AND_ASSIGN(TestConnection); | 586 DISALLOW_COPY_AND_ASSIGN(TestConnection); |
| 587 }; | 587 }; |
| 588 | 588 |
| 589 // Used for testing packets revived from FEC packets. | 589 // Used for testing packets revived from FEC packets. |
| 590 class FecQuicConnectionDebugVisitor | 590 class FecQuicConnectionDebugVisitor |
| 591 : public QuicConnectionDebugVisitor { | 591 : public QuicConnectionDebugVisitor { |
| 592 public: | 592 public: |
| 593 virtual void OnRevivedPacket(const QuicPacketHeader& header, | 593 virtual void OnRevivedPacket(const QuicPacketHeader& header, |
| 594 StringPiece data) OVERRIDE { | 594 StringPiece data) override { |
| 595 revived_header_ = header; | 595 revived_header_ = header; |
| 596 } | 596 } |
| 597 | 597 |
| 598 // Public accessor method. | 598 // Public accessor method. |
| 599 QuicPacketHeader revived_header() const { | 599 QuicPacketHeader revived_header() const { |
| 600 return revived_header_; | 600 return revived_header_; |
| 601 } | 601 } |
| 602 | 602 |
| 603 private: | 603 private: |
| 604 QuicPacketHeader revived_header_; | 604 QuicPacketHeader revived_header_; |
| (...skipping 3403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4008 QuicBlockedFrame blocked; | 4008 QuicBlockedFrame blocked; |
| 4009 blocked.stream_id = 3; | 4009 blocked.stream_id = 3; |
| 4010 EXPECT_CALL(visitor_, OnBlockedFrames(_)); | 4010 EXPECT_CALL(visitor_, OnBlockedFrames(_)); |
| 4011 ProcessFramePacket(QuicFrame(&blocked)); | 4011 ProcessFramePacket(QuicFrame(&blocked)); |
| 4012 EXPECT_TRUE(ack_alarm->IsSet()); | 4012 EXPECT_TRUE(ack_alarm->IsSet()); |
| 4013 } | 4013 } |
| 4014 | 4014 |
| 4015 } // namespace | 4015 } // namespace |
| 4016 } // namespace test | 4016 } // namespace test |
| 4017 } // namespace net | 4017 } // namespace net |
| OLD | NEW |