OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/strings/string_number_conversions.h" |
5 #include "net/quic/crypto/crypto_server_config.h" | 6 #include "net/quic/crypto/crypto_server_config.h" |
| 7 #include "net/quic/crypto/crypto_utils.h" |
6 #include "net/quic/crypto/quic_random.h" | 8 #include "net/quic/crypto/quic_random.h" |
7 #include "net/quic/test_tools/crypto_test_utils.h" | 9 #include "net/quic/test_tools/crypto_test_utils.h" |
8 #include "net/quic/test_tools/mock_clock.h" | 10 #include "net/quic/test_tools/mock_clock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
| 13 using base::StringPiece; |
11 using std::string; | 14 using std::string; |
12 | 15 |
13 namespace net { | 16 namespace net { |
14 namespace test { | 17 namespace test { |
15 | 18 |
16 class CryptoServerTest : public ::testing::Test { | 19 class CryptoServerTest : public ::testing::Test { |
17 public: | 20 public: |
18 CryptoServerTest() | 21 CryptoServerTest() |
19 : rand_(QuicRandom::GetInstance()), | 22 : rand_(QuicRandom::GetInstance()), |
20 config_(QuicCryptoServerConfig::TESTING, rand_), | 23 config_(QuicCryptoServerConfig::TESTING, rand_), |
21 addr_(ParseIPLiteralToNumber("192.0.2.33", &ip_) ? | 24 addr_(ParseIPLiteralToNumber("192.0.2.33", &ip_) ? |
22 ip_ : IPAddressNumber(), 1) { | 25 ip_ : IPAddressNumber(), 1) { |
23 } | 26 } |
24 | 27 |
25 virtual void SetUp() { | 28 virtual void SetUp() { |
26 scoped_ptr<CryptoHandshakeMessage> msg( | 29 scoped_ptr<CryptoHandshakeMessage> msg( |
27 config_.AddDefaultConfig(rand_, &clock_, | 30 config_.AddDefaultConfig(rand_, &clock_, |
28 QuicCryptoServerConfig::ConfigOptions())); | 31 QuicCryptoServerConfig::ConfigOptions())); |
| 32 |
| 33 StringPiece orbit; |
| 34 CHECK(msg->GetStringPiece(kORBT, &orbit)); |
| 35 CHECK_EQ(sizeof(orbit_), orbit.size()); |
| 36 memcpy(orbit_, orbit.data(), orbit.size()); |
29 } | 37 } |
30 | 38 |
31 void ShouldSucceed(const CryptoHandshakeMessage& message) { | 39 void ShouldSucceed(const CryptoHandshakeMessage& message) { |
32 string error_details; | 40 string error_details; |
33 QuicErrorCode error = config_.ProcessClientHello( | 41 QuicErrorCode error = config_.ProcessClientHello( |
34 message, 1 /* GUID */, addr_, &clock_, | 42 message, 1 /* GUID */, addr_, &clock_, |
35 rand_, ¶ms_, &out_, &error_details); | 43 rand_, ¶ms_, &out_, &error_details); |
36 | 44 |
37 ASSERT_EQ(error, QUIC_NO_ERROR) | 45 ASSERT_EQ(error, QUIC_NO_ERROR) |
38 << "Message failed with error " << error_details << ": " | 46 << "Message failed with error " << error_details << ": " |
(...skipping 19 matching lines...) Expand all Loading... |
58 va_start(ap, message_tag); | 66 va_start(ap, message_tag); |
59 | 67 |
60 CryptoHandshakeMessage message = | 68 CryptoHandshakeMessage message = |
61 CryptoTestUtils::BuildMessage(message_tag, ap); | 69 CryptoTestUtils::BuildMessage(message_tag, ap); |
62 va_end(ap); | 70 va_end(ap); |
63 | 71 |
64 message.SetStringPiece(kPAD, string(kClientHelloMinimumSize, '-')); | 72 message.SetStringPiece(kPAD, string(kClientHelloMinimumSize, '-')); |
65 return message; | 73 return message; |
66 } | 74 } |
67 | 75 |
68 private: | 76 string GenerateNonce() { |
| 77 string nonce; |
| 78 CryptoUtils::GenerateNonce( |
| 79 clock_.WallNow(), rand_, |
| 80 StringPiece(reinterpret_cast<const char*>(orbit_), sizeof(orbit_)), |
| 81 &nonce); |
| 82 return nonce; |
| 83 } |
| 84 |
| 85 protected: |
69 QuicRandom* const rand_; | 86 QuicRandom* const rand_; |
70 MockClock clock_; | 87 MockClock clock_; |
71 QuicCryptoServerConfig config_; | 88 QuicCryptoServerConfig config_; |
72 QuicCryptoNegotiatedParameters params_; | 89 QuicCryptoNegotiatedParameters params_; |
73 CryptoHandshakeMessage out_; | 90 CryptoHandshakeMessage out_; |
74 IPAddressNumber ip_; | 91 IPAddressNumber ip_; |
75 IPEndPoint addr_; | 92 IPEndPoint addr_; |
| 93 uint8 orbit_[kOrbitSize]; |
76 }; | 94 }; |
77 | 95 |
78 TEST_F(CryptoServerTest, BadSNI) { | 96 TEST_F(CryptoServerTest, BadSNI) { |
79 static const char* kBadSNIs[] = { | 97 static const char* kBadSNIs[] = { |
80 "", | 98 "", |
81 "foo", | 99 "foo", |
82 "#00", | 100 "#00", |
83 "#ff00", | 101 "#ff00", |
84 "127.0.0.1", | 102 "127.0.0.1", |
85 "ffee::1", | 103 "ffee::1", |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 }; | 143 }; |
126 | 144 |
127 for (size_t i = 0; i < arraysize(kBadNonces); i++) { | 145 for (size_t i = 0; i < arraysize(kBadNonces); i++) { |
128 ShouldSucceed(InchoateClientHello( | 146 ShouldSucceed(InchoateClientHello( |
129 "CHLO", | 147 "CHLO", |
130 "NONC", kBadNonces[i], | 148 "NONC", kBadNonces[i], |
131 NULL)); | 149 NULL)); |
132 } | 150 } |
133 } | 151 } |
134 | 152 |
| 153 TEST_F(CryptoServerTest, ReplayProtection) { |
| 154 // This tests that disabling replay protection works. |
| 155 |
| 156 char public_value[32]; |
| 157 memset(public_value, 42, sizeof(public_value)); |
| 158 |
| 159 const string nonce_str = GenerateNonce(); |
| 160 const string nonce("#" + base::HexEncode(nonce_str.data(), |
| 161 nonce_str.size())); |
| 162 const string pub("#" + base::HexEncode(public_value, sizeof(public_value))); |
| 163 |
| 164 CryptoHandshakeMessage msg = CryptoTestUtils::Message( |
| 165 "CHLO", |
| 166 "AEAD", "AESG", |
| 167 "KEXS", "C255", |
| 168 "PUBS", pub.c_str(), |
| 169 "NONC", nonce.c_str(), |
| 170 "$padding", static_cast<int>(kClientHelloMinimumSize), |
| 171 NULL); |
| 172 ShouldSucceed(msg); |
| 173 // The message should be rejected because the source-address token is missing. |
| 174 ASSERT_EQ(kREJ, out_.tag()); |
| 175 |
| 176 StringPiece srct; |
| 177 ASSERT_TRUE(out_.GetStringPiece(kSourceAddressTokenTag, &srct)); |
| 178 const string srct_hex = "#" + base::HexEncode(srct.data(), srct.size()); |
| 179 |
| 180 StringPiece scfg; |
| 181 ASSERT_TRUE(out_.GetStringPiece(kSCFG, &scfg)); |
| 182 scoped_ptr<CryptoHandshakeMessage> server_config( |
| 183 CryptoFramer::ParseMessage(scfg)); |
| 184 |
| 185 StringPiece scid; |
| 186 ASSERT_TRUE(server_config->GetStringPiece(kSCID, &scid)); |
| 187 const string scid_hex("#" + base::HexEncode(scid.data(), scid.size())); |
| 188 |
| 189 msg = CryptoTestUtils::Message( |
| 190 "CHLO", |
| 191 "AEAD", "AESG", |
| 192 "KEXS", "C255", |
| 193 "SCID", scid_hex.c_str(), |
| 194 "#004b5453", srct_hex.c_str(), |
| 195 "PUBS", pub.c_str(), |
| 196 "NONC", nonce.c_str(), |
| 197 "$padding", static_cast<int>(kClientHelloMinimumSize), |
| 198 NULL); |
| 199 ShouldSucceed(msg); |
| 200 // The message should be rejected because the strike-register is still |
| 201 // quiescent. |
| 202 ASSERT_EQ(kREJ, out_.tag()); |
| 203 |
| 204 config_.set_replay_protection(false); |
| 205 |
| 206 ShouldSucceed(msg); |
| 207 // The message should be accepted now. |
| 208 ASSERT_EQ(kSHLO, out_.tag()); |
| 209 |
| 210 ShouldSucceed(msg); |
| 211 // The message should accepted twice when replay protection is off. |
| 212 ASSERT_EQ(kSHLO, out_.tag()); |
| 213 } |
| 214 |
135 class CryptoServerTestNoConfig : public CryptoServerTest { | 215 class CryptoServerTestNoConfig : public CryptoServerTest { |
136 public: | 216 public: |
137 virtual void SetUp() { | 217 virtual void SetUp() { |
138 // Deliberately don't add a config so that we can test this situation. | 218 // Deliberately don't add a config so that we can test this situation. |
139 } | 219 } |
140 }; | 220 }; |
141 | 221 |
142 TEST_F(CryptoServerTestNoConfig, DontCrash) { | 222 TEST_F(CryptoServerTestNoConfig, DontCrash) { |
143 ShouldFailMentioning("No config", InchoateClientHello( | 223 ShouldFailMentioning("No config", InchoateClientHello( |
144 "CHLO", | 224 "CHLO", |
145 NULL)); | 225 NULL)); |
146 } | 226 } |
147 | 227 |
148 } // namespace test | 228 } // namespace test |
149 } // namespace net | 229 } // namespace net |
OLD | NEW |