Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: net/socket/ssl_client_socket_openssl_unittest.cc

Issue 12220104: Wire up SSL client authentication for OpenSSL/Android through the net/ stack (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: address recent nits Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/socket/ssl_client_socket.h"
6
7 #include <errno.h>
8 #include <string.h>
9
10 #include <openssl/bn.h>
11 #include <openssl/evp.h>
12 #include <openssl/pem.h>
13 #include <openssl/rsa.h>
14
15 #include "base/file_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_handle.h"
18 #include "base/values.h"
19 #include "crypto/openssl_util.h"
20 #include "net/base/address_list.h"
21 #include "net/base/cert_test_util.h"
22 #include "net/base/host_resolver.h"
23 #include "net/base/io_buffer.h"
24 #include "net/base/mock_cert_verifier.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/net_log.h"
27 #include "net/base/net_log_unittest.h"
28 #include "net/base/openssl_private_key_store.h"
29 #include "net/base/ssl_cert_request_info.h"
30 #include "net/base/ssl_config_service.h"
31 #include "net/base/test_completion_callback.h"
32 #include "net/base/test_data_directory.h"
33 #include "net/base/test_root_certs.h"
34 #include "net/socket/client_socket_factory.h"
35 #include "net/socket/client_socket_handle.h"
36 #include "net/socket/socket_test_util.h"
37 #include "net/socket/tcp_client_socket.h"
38 #include "net/test/test_server.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "testing/platform_test.h"
41
42 namespace {
43
44 typedef net::OpenSSLPrivateKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
Ryan Sleevi 2013/02/14 07:54:01 When you do the namespace move suggested below, pl
digit1 2013/02/25 14:26:22 Done.
45
46 typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA;
47 typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM;
48
49 } // namespace
50
51 namespace net {
Ryan Sleevi 2013/02/14 07:54:01 Move this to line 41 - that is namespace net { na
digit1 2013/02/25 14:26:22 Done.
52
53 // The following is needed to construct paths to certificates passed as
54 // |client_authorities| in server SSLOptions. Current implementation of
55 // RemoteTestServer (used on Android) expects relative paths, as opposed to
56 // LocalTestServer, which expects absolute paths (what to fix?).
57 static base::FilePath CertDirectory() {
58 #ifdef OS_ANDROID
59 return net::GetTestCertsDirectoryRelative();
60 #else
61 return net::GetTestCertsDirectory();
62 #endif
63 }
64
65 // Loads a PEM-encoded private key file into a scoped EVP_PKEY object.
66 // |filepath| is the private key file path.
67 // |*pkey| is reset to the new EVP_PKEY on success, untouched otherwise.
68 // Returns true on success, false on failure.
69 static bool LoadPrivateKeyOpenSSL(
70 const base::FilePath& filepath,
71 net::OpenSSLPrivateKeyStore::ScopedEVP_PKEY* pkey) {
72 ScopedStdioHandle file(file_util::OpenFile(filepath, "rb"));
73 if (!file.get()) {
74 LOG(ERROR) << "Could not open private key file: "
75 << filepath.value() << ": " << strerror(errno);
76 return false;
77 }
78 EVP_PKEY* result = PEM_read_PrivateKey(file.get(), NULL, NULL, NULL);
79 if (result == NULL) {
80 LOG(ERROR) << "Could not read private key file: "
81 << filepath.value();
82 return false;
83 }
84 pkey->reset(result);
85 return true;
86 }
87
88 // LogContainsSSLConnectEndEvent returns true if the given index in the given
89 // log is an SSL connect end event. The NSS sockets will cork in an attempt to
Ryan Sleevi 2013/02/15 23:53:26 You've still got this comment referring to NSS. D
digit1 2013/02/25 14:26:22 This is no longer needed (see comment below), so h
90 // merge the first application data record with the Finished message when false
91 // starting. However, in order to avoid the server timing out the handshake,
92 // they'll give up waiting for application data and send the Finished after a
93 // timeout. This means that an SSL connect end event may appear as a socket
94 // write.
95 static bool LogContainsSSLConnectEndEvent(
96 const net::CapturingNetLog::CapturedEntryList& log, int i) {
97 return net::LogContainsEndEvent(log, i, net::NetLog::TYPE_SSL_CONNECT) ||
98 net::LogContainsEvent(log, i, net::NetLog::TYPE_SOCKET_BYTES_SENT,
99 net::NetLog::PHASE_NONE);
100 };
101
102 static const net::SSLConfig kDefaultSSLConfig;
103
104 class SSLClientSocketOpenSSLClientAuthTest : public PlatformTest {
105 public:
106 SSLClientSocketOpenSSLClientAuthTest()
107 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()),
108 cert_verifier_(new net::MockCertVerifier) {
109 cert_verifier_->set_default_result(net::OK);
110 context_.cert_verifier = cert_verifier_.get();
111 key_store_ = net::OpenSSLPrivateKeyStore::GetInstance();
112 }
113
114 protected:
115 virtual void TearDown() {
116 key_store_->Flush();
117 }
Ryan Sleevi 2013/02/15 23:53:26 See previous remarks re: SetUp and TearDown using
digit1 2013/02/25 14:26:22 I've changed it to use SetUp/TearDown exclusively.
118
119 net::SSLClientSocket* CreateSSLClientSocket(
120 net::StreamSocket* transport_socket,
121 const net::HostPortPair& host_and_port,
122 const net::SSLConfig& ssl_config) {
123 return socket_factory_->CreateSSLClientSocket(transport_socket,
124 host_and_port,
125 ssl_config,
126 context_);
127 }
128
129 // Connect to a HTTPS test server.
130 bool ConnectToTestServer(net::TestServer::SSLOptions& ssl_options) {
131 test_server_.reset(new net::TestServer(net::TestServer::TYPE_HTTPS,
132 ssl_options,
133 base::FilePath()));
134 if (!test_server_.get()) {
135 LOG(ERROR) << "Could not create new TestServer";
136 return false;
137 }
Ryan Sleevi 2013/02/15 23:53:26 This is an unnecessary check, at least following C
digit1 2013/02/25 14:26:22 Done.
138 if (!test_server_->Start()) {
139 LOG(ERROR) << "Could not start TestServer";
140 return false;
141 }
142
143 if (!test_server_->GetAddressList(&addr_)) {
144 LOG(ERROR) << "Could not get TestServer address list";
145 return false;
146 }
147
148 transport_.reset(new net::TCPClientSocket(
149 addr_, &log_, net::NetLog::Source()));
150 int rv = transport_->Connect(callback_.callback());
Ryan Sleevi 2013/02/14 08:51:15 int rv = callback.GetResult(transport_->Connect(ca
digit1 2013/02/25 14:26:22 Good to know, thanks.
151 if (rv == net::ERR_IO_PENDING)
152 rv = callback_.WaitForResult();
153 if (rv != net::OK) {
154 LOG(ERROR) << "Could not connect to TestServer";
155 return false;
156 }
157 return true;
158 }
159
160 bool RecordPrivateKey(net::SSLConfig& ssl_config,
161 EVP_PKEY* private_key) {
162 return key_store_->RecordClientCertPrivateKey(
163 ssl_config.client_cert.get(), private_key);
164 }
165
166 bool CreateAndConnectSSLClientSocket(net::SSLConfig& ssl_config,
Ryan Sleevi 2013/02/15 23:53:26 comment this function.
digit1 2013/02/25 14:26:22 Done.
167 int* result) {
168 sock_.reset(CreateSSLClientSocket(transport_.release(),
169 test_server_->host_port_pair(),
170 ssl_config));
171
172 if (sock_->IsConnected()) {
173 LOG(ERROR) << "SSL Socket prematurely connected";
174 return false;
175 }
176
177 int rv = sock_->Connect(callback_.callback());
178
179 net::CapturingNetLog::CapturedEntryList entries;
180 log_.GetEntries(&entries);
181 if (!net::LogContainsBeginEvent(
182 entries, 5, net::NetLog::TYPE_SSL_CONNECT)) {
183 LOG(ERROR) << "SSL connection not started in logs";
184 return false;
185 }
186 if (rv == net::ERR_IO_PENDING)
187 rv = callback_.WaitForResult();
188
189 *result = rv;
190 return true;
191 }
192
193
194 bool CheckSSLClientSocketSentCert() {
195 net::CapturingNetLog::CapturedEntryList entries;
196 log_.GetEntries(&entries);
197 if (!LogContainsSSLConnectEndEvent(entries, -1)) {
Ryan Sleevi 2013/02/15 23:53:26 Are you sure this needs to be checked?
digit1 2013/02/25 14:26:22 Not really, this came from ssl_client_socket_unitt
198 LOG(ERROR) << "!LogContainsSSLConnectEndEvent()";
199 return false;
200 }
201
202 // Check that the client certificate was sent.
203 net::SSLInfo ssl_info;
204 sock_->GetSSLInfo(&ssl_info);
205 return ssl_info.client_cert_sent;
206 }
207
208 net::ClientSocketFactory* socket_factory_;
209 scoped_ptr<net::MockCertVerifier> cert_verifier_;
210 net::SSLClientSocketContext context_;
211 net::OpenSSLPrivateKeyStore* key_store_;
212 scoped_ptr<net::TestServer> test_server_;
213 net::AddressList addr_;
214 net::TestCompletionCallback callback_;
215 net::CapturingNetLog log_;
216 scoped_ptr<net::StreamSocket> transport_;
217 scoped_ptr<net::SSLClientSocket> sock_;
218 };
219
220 // Connect to a server requesting client authentication, do not send
221 // any client certificates. It should refuse the connection.
222 TEST_F(SSLClientSocketOpenSSLClientAuthTest, NoCert) {
223 net::TestServer::SSLOptions ssl_options;
224 ssl_options.request_client_certificate = true;
225
226 ASSERT_TRUE(ConnectToTestServer(ssl_options));
227
228 base::FilePath certs_dir = net::GetTestCertsDirectory();
229 net::SSLConfig ssl_config = kDefaultSSLConfig;
230
231 int rv;
232 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
233
234 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv);
235 EXPECT_FALSE(sock_->IsConnected());
236 }
237
238 // Connect to a server requesting client authentication, and send it
239 // an empty certificate. It should refuse the connection.
240 TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendEmptyCert) {
241 net::TestServer::SSLOptions ssl_options;
242 ssl_options.request_client_certificate = true;
243
244 ASSERT_TRUE(ConnectToTestServer(ssl_options));
245
246 base::FilePath certs_dir = net::GetTestCertsDirectory();
247 net::SSLConfig ssl_config = kDefaultSSLConfig;
248 ssl_config.send_client_cert = true;
249 ssl_config.client_cert = NULL;
250
251 int rv;
252 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
253
254 EXPECT_EQ(net::OK, rv);
255 EXPECT_TRUE(sock_->IsConnected());
256 }
257
258 // Connect to a server requesting client authentication. Send it a
259 // matching certificate. It should allow the connection.
260 TEST_F(SSLClientSocketOpenSSLClientAuthTest, SendGoodCert) {
261 net::TestServer::SSLOptions ssl_options;
262 ssl_options.request_client_certificate = true;
263 ssl_options.client_authorities.push_back(
264 CertDirectory().AppendASCII("client_1_root.pem"));
265
266 ASSERT_TRUE(ConnectToTestServer(ssl_options));
267
268 base::FilePath certs_dir = net::GetTestCertsDirectory();
269 net::SSLConfig ssl_config = kDefaultSSLConfig;
270 ssl_config.send_client_cert = true;
271 ssl_config.client_cert = net::ImportCertFromFile(certs_dir,
272 "client_1.pem");
273
274 // This is required to ensure that signing works with the client
275 // certificate's private key.
276 net::OpenSSLPrivateKeyStore::ScopedEVP_PKEY client_private_key;
277 ASSERT_TRUE(LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_1.key"),
278 &client_private_key));
279 EXPECT_TRUE(RecordPrivateKey(ssl_config, client_private_key.get()));
280
281 int rv;
282 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
283
284 EXPECT_EQ(net::OK, rv);
285 EXPECT_TRUE(sock_->IsConnected());
286
287 EXPECT_TRUE(CheckSSLClientSocketSentCert());
288
289 sock_->Disconnect();
290 EXPECT_FALSE(sock_->IsConnected());
291 }
292
293 // Connect to a server requesting client authentication. Send it a
294 // non-matching certificate. It should not allow the connection.
295 // NOTE: Disabled because our TestServer never verifies that the client
296 // certificate matches the required CA authorities. Thus is always
297 // accepts the connection.
298 TEST_F(SSLClientSocketOpenSSLClientAuthTest, DISABLED_SendBadCert) {
Ryan Sleevi 2013/02/15 23:53:26 Unless you have a bug # and plan to be working on
digit1 2013/02/25 14:26:22 Ok, I've removed this test.
299 net::TestServer::SSLOptions ssl_options;
300 ssl_options.request_client_certificate = true;
301 ssl_options.client_authorities.push_back(
302 CertDirectory().AppendASCII("client_1_root.pem"));
303
304 ASSERT_TRUE(ConnectToTestServer(ssl_options));
305
306 base::FilePath certs_dir = net::GetTestCertsDirectory();
307 net::SSLConfig ssl_config = kDefaultSSLConfig;
308 ssl_config.send_client_cert = true;
309 ssl_config.client_cert = net::ImportCertFromFile(certs_dir,
310 "client_2.pem");
311
312 net::OpenSSLPrivateKeyStore::ScopedEVP_PKEY client_private_key;
313 ASSERT_TRUE(LoadPrivateKeyOpenSSL(certs_dir.AppendASCII("client_2.key"),
314 &client_private_key));
315 EXPECT_TRUE(RecordPrivateKey(ssl_config, client_private_key.get()));
316
317 int rv;
318 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
319
320 EXPECT_EQ(net::ERR_BAD_SSL_CLIENT_AUTH_CERT, rv);
321 EXPECT_FALSE(sock_->IsConnected());
322
323 EXPECT_TRUE(CheckSSLClientSocketSentCert());
324 }
325
326 } // namespace net
OLDNEW
« net/socket/ssl_client_socket_openssl.cc ('K') | « net/socket/ssl_client_socket_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698