1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sslt.h>
#include <vector>
#include <string>
#include "base/eintr_wrapper.h"
#include "base/base64.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/values.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/base/net_log_unittest.h"
#include "net/base/ssl_config_service.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/ssl_client_socket.h"
#include "net/socket/ssl_client_socket_nss.h"
#include "net/socket/ssl_host_info.h"
#include "net/socket/tcp_client_socket.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace net {
// TestSSLHostInfo is an SSLHostInfo which stores a single state in memory and
// pretends that certificate verification always succeeds.
class TestSSLHostInfo : public SSLHostInfo {
public:
TestSSLHostInfo()
: SSLHostInfo("example.com", kDefaultSSLConfig) {
if (!saved_.empty())
Parse(saved_);
cert_verification_complete_ = true;
cert_verification_error_ = OK;
}
virtual void Start() {
}
virtual int WaitForDataReady(CompletionCallback*) { return OK; }
virtual void Persist() {
saved_ = Serialize();
}
static void Reset() {
saved_.clear();
}
private:
static SSLConfig kDefaultSSLConfig;
static std::string saved_;
};
std::string TestSSLHostInfo::saved_;
SSLConfig TestSSLHostInfo::kDefaultSSLConfig;
class SSLClientSocketSnapStartTest : public PlatformTest {
public:
SSLClientSocketSnapStartTest()
: child_(base::kNullProcessHandle),
socket_factory_(ClientSocketFactory::GetDefaultFactory()),
log_(CapturingNetLog::kUnbounded) {
TestSSLHostInfo::Reset();
ssl_config_.snap_start_enabled = true;
}
virtual void TearDown() {
if (child_ != base::kNullProcessHandle) {
int exit_code;
EXPECT_TRUE(base::WaitForExitCode(child_, &exit_code));
EXPECT_EQ(0, exit_code);
}
}
protected:
void StartSnapStartServer(const char* arg, ...) {
FilePath dir_exe;
PathService::Get(base::DIR_EXE, &dir_exe);
FilePath helper_binary = dir_exe.Append("openssl_helper");
std::vector<std::string> args;
args.push_back(helper_binary.value());
va_list ap;
va_start(ap, arg);
while (arg) {
args.push_back(arg);
arg = va_arg(ap, const char*);
}
va_end(ap);
const int listener = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(listener, 0);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = PF_INET;
ASSERT_EQ(0, bind(listener, (struct sockaddr*) &sin, sizeof(sin)));
socklen_t socklen = sizeof(remote_);
ASSERT_EQ(0, getsockname(listener, (struct sockaddr*) &remote_, &socklen));
ASSERT_EQ(sizeof(remote_), socklen);
ASSERT_EQ(0, listen(listener, 1));
base::file_handle_mapping_vector mapping;
// The listening socket is installed as the child's fd 3.
mapping.push_back(std::make_pair(listener, 3));
base::LaunchApp(args, mapping, false /* don't wait */, &child_);
ASSERT_EQ(0, HANDLE_EINTR(close(listener)));
}
// LoadDefaultCert returns the DER encoded default certificate.
std::string LoadDefaultCert() {
FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
path = path.Append("net");
path = path.Append("data");
path = path.Append("ssl");
path = path.Append("certificates");
path = path.Append("ok_cert.pem");
std::string pem;
bool r = file_util::ReadFileToString(path, &pem);
CHECK(r) << "failed to read " << path.value();
static const char kStartMarker[] = "-----BEGIN CERTIFICATE-----\n";
static const char kEndMarker[] = "-----END CERTIFICATE-----\n";
std::string::size_type i = pem.find(kStartMarker);
std::string::size_type j = pem.find(kEndMarker);
CHECK(i != std::string::npos);
CHECK(j != std::string::npos);
CHECK_GT(j, i);
i += sizeof(kStartMarker) - 1;
std::string b64data = pem.substr(i, j - i);
ReplaceSubstringsAfterOffset(&b64data, 0 /* start offset */, "\n", "");
std::string der;
base::Base64Decode(b64data, &der);
return der;
}
void SetupSSLConfig() {
if (ssl_config_.allowed_bad_certs.size())
return;
const std::string der = LoadDefaultCert();
SSLConfig::CertAndStatus cert_and_status;
cert_and_status.cert =
X509Certificate::CreateFromBytes(der.data(), der.size());
cert_and_status.cert_status = ERR_CERT_AUTHORITY_INVALID;
ssl_config_.allowed_bad_certs.push_back(cert_and_status);
}
// PerformConnection makes an SSL connection to the openssl_helper binary and
// does a ping-pong test to check the the SSL socket is working correctly.
void PerformConnection() {
client_ = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_LE(0, client_);
ASSERT_EQ(
0, connect(client_, (struct sockaddr*) &remote_, sizeof(remote_)));
int on = 1;
setsockopt(client_, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
SetupSSLConfig();
log_.Clear();
std::vector<uint8> localhost;
localhost.push_back(127);
localhost.push_back(0);
localhost.push_back(0);
localhost.push_back(1);
AddressList addr_list(localhost, 443, false);
TCPClientSocket* transport = new TCPClientSocket(
addr_list, &log_, NetLog::Source());
transport->AdoptSocket(client_);
scoped_ptr<SSLClientSocket> sock(
socket_factory_->CreateSSLClientSocket(
transport, HostPortPair("example.com", 443), ssl_config_,
new TestSSLHostInfo()));
TestCompletionCallback callback;
int rv = sock->Connect(&callback);
if (rv != OK) {
ASSERT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(sock->IsConnected());
rv = callback.WaitForResult();
EXPECT_EQ(OK, rv);
}
EXPECT_TRUE(sock->IsConnected());
static const char request_text[] = "hello!";
scoped_refptr<IOBuffer> request_buffer =
new IOBuffer(arraysize(request_text) - 1);
memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
rv = sock->Write(request_buffer, arraysize(request_text), &callback);
if (rv < 0) {
ASSERT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
}
EXPECT_EQ(7, rv);
scoped_refptr<IOBuffer> reply_buffer = new IOBuffer(8);
rv = sock->Read(reply_buffer, 8, &callback);
if (rv < 0) {
ASSERT_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
}
EXPECT_EQ(8, rv);
EXPECT_TRUE(memcmp(reply_buffer->data(), "goodbye!", 8) == 0);
next_proto_status_ = sock->GetNextProto(&next_proto_);
sock->Disconnect();
}
// SnapStartEventType extracts the type of Snap Start from the NetLog. See
// the SSL_SNAP_START_* defines in sslt.h
int SnapStartEventType() {
CapturingNetLog::EntryList entries;
log_.GetEntries(&entries);
for (CapturingNetLog::EntryList::const_iterator
i = entries.begin(); i != entries.end(); i++) {
if (i->type == NetLog::TYPE_SSL_SNAP_START) {
scoped_ptr<Value> value(i->extra_parameters->ToValue());
CHECK(value->GetType() == Value::TYPE_DICTIONARY);
DictionaryValue* dict = reinterpret_cast<DictionaryValue*>(value.get());
int ret;
CHECK(dict->GetInteger("type", &ret));
return ret;
}
}
return -1;
}
// DidMerge returns true if the NetLog suggests the the SSL connection merged
// it's certificate validation with the optimistic validation from the
// SSLHostInfo.
bool DidMerge() {
CapturingNetLog::EntryList entries;
log_.GetEntries(&entries);
for (CapturingNetLog::EntryList::const_iterator
i = entries.begin(); i != entries.end(); i++) {
if (i->type == NetLog::TYPE_SSL_VERIFICATION_MERGED)
return true;
}
return false;
}
base::ProcessHandle child_;
ClientSocketFactory* const socket_factory_;
struct sockaddr_in remote_;
int client_;
SSLConfig ssl_config_;
CapturingNetLog log_;
SSLClientSocket::NextProtoStatus next_proto_status_;
std::string next_proto_;
};
TEST_F(SSLClientSocketSnapStartTest, Basic) {
// Not a Snap Start connection.
StartSnapStartServer(NULL);
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
}
TEST_F(SSLClientSocketSnapStartTest, SnapStart) {
StartSnapStartServer("snap-start", NULL);
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
SSLClientSocketNSS::ClearSessionCache();
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_FULL, SnapStartEventType());
EXPECT_TRUE(DidMerge());
}
TEST_F(SSLClientSocketSnapStartTest, SnapStartResume) {
StartSnapStartServer("snap-start", NULL);
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_RESUME, SnapStartEventType());
EXPECT_TRUE(DidMerge());
}
TEST_F(SSLClientSocketSnapStartTest, SnapStartRecovery) {
StartSnapStartServer("snap-start-recovery", NULL);
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
SSLClientSocketNSS::ClearSessionCache();
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_RECOVERY, SnapStartEventType());
EXPECT_TRUE(DidMerge());
}
TEST_F(SSLClientSocketSnapStartTest, SnapStartResumeRecovery) {
StartSnapStartServer("snap-start-recovery", NULL);
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_RESUME_RECOVERY, SnapStartEventType());
EXPECT_TRUE(DidMerge());
}
TEST_F(SSLClientSocketSnapStartTest, SnapStartWithNPN) {
ssl_config_.next_protos.assign("\003foo\003bar");
StartSnapStartServer("snap-start", "npn", NULL);
PerformConnection();
EXPECT_EQ(SSLClientSocket::kNextProtoNegotiated, next_proto_status_);
EXPECT_EQ("foo", next_proto_);
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
SSLClientSocketNSS::ClearSessionCache();
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_FULL, SnapStartEventType());
EXPECT_EQ(SSLClientSocket::kNextProtoNegotiated, next_proto_status_);
EXPECT_EQ("foo", next_proto_);
EXPECT_TRUE(DidMerge());
}
TEST_F(SSLClientSocketSnapStartTest, SnapStartWithNPNMispredict) {
// This tests that we recover in the event of a misprediction.
ssl_config_.next_protos.assign("\003foo\003baz");
StartSnapStartServer("snap-start", "npn-mispredict", NULL);
PerformConnection();
EXPECT_EQ(SSLClientSocket::kNextProtoNegotiated, next_proto_status_);
EXPECT_EQ("foo", next_proto_);
EXPECT_EQ(SSL_SNAP_START_NONE, SnapStartEventType());
EXPECT_FALSE(DidMerge());
SSLClientSocketNSS::ClearSessionCache();
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_RECOVERY, SnapStartEventType());
EXPECT_EQ(SSLClientSocket::kNextProtoNegotiated, next_proto_status_);
EXPECT_EQ("baz", next_proto_);
EXPECT_TRUE(DidMerge());
SSLClientSocketNSS::ClearSessionCache();
PerformConnection();
EXPECT_EQ(SSL_SNAP_START_FULL, SnapStartEventType());
EXPECT_EQ(SSLClientSocket::kNextProtoNegotiated, next_proto_status_);
EXPECT_EQ("baz", next_proto_);
EXPECT_TRUE(DidMerge());
}
} // namespace net
|