[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This test suite uses SSLClientSocket to test the implementation of |
| 6 | // SSLServerSocket. In order to establish connections between the sockets |
| 7 | // we need two additional classes: |
| 8 | // 1. FakeSocket |
| 9 | // Connects SSL socket to FakeDataChannel. This class is just a stub. |
| 10 | // |
| 11 | // 2. FakeDataChannel |
| 12 | // Implements the actual exchange of data between two FakeSockets. |
| 13 | // |
| 14 | // Implementations of these two classes are included in this file. |
| 15 | |
| 16 | #include "net/socket/ssl_server_socket.h" |
| 17 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 18 | #include <stdint.h> |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 19 | #include <stdlib.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 20 | #include <utility> |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 21 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 22 | #include "base/callback_helpers.h" |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 23 | #include "base/compiler_specific.h" |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 24 | #include "base/containers/queue.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 26 | #include "base/files/file_util.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 27 | #include "base/location.h" |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 28 | #include "base/logging.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 29 | #include "base/macros.h" |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 30 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 31 | #include "base/single_thread_task_runner.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 32 | #include "base/test/scoped_task_environment.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 33 | #include "base/threading/thread_task_runner_handle.h" |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 34 | #include "build/build_config.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 35 | #include "crypto/nss_util.h" |
| 36 | #include "crypto/rsa_private_key.h" |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 37 | #include "crypto/signature_creator.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 38 | #include "net/base/address_list.h" |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 39 | #include "net/base/completion_once_callback.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 40 | #include "net/base/host_port_pair.h" |
| 41 | #include "net/base/io_buffer.h" |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 42 | #include "net/base/ip_address.h" |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 43 | #include "net/base/ip_endpoint.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 44 | #include "net/base/net_errors.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 45 | #include "net/cert/cert_status_flags.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 46 | #include "net/cert/ct_policy_enforcer.h" |
| 47 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 48 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 49 | #include "net/cert/mock_cert_verifier.h" |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 50 | #include "net/cert/mock_client_cert_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 51 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 52 | #include "net/cert/x509_certificate.h" |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 53 | #include "net/http/transport_security_state.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 54 | #include "net/log/net_log_with_source.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 55 | #include "net/socket/client_socket_factory.h" |
| 56 | #include "net/socket/socket_test_util.h" |
| 57 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 58 | #include "net/socket/stream_socket.h" |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 59 | #include "net/ssl/ssl_cert_request_info.h" |
davidben | 9dd8487 | 2015-05-02 00:22:58 | [diff] [blame] | 60 | #include "net/ssl/ssl_cipher_suite_names.h" |
davidben | 9dd8487 | 2015-05-02 00:22:58 | [diff] [blame] | 61 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 62 | #include "net/ssl/ssl_info.h" |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 63 | #include "net/ssl/ssl_private_key.h" |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 64 | #include "net/ssl/ssl_server_config.h" |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 65 | #include "net/ssl/test_ssl_private_key.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 66 | #include "net/test/cert_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 67 | #include "net/test/gtest_util.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 68 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 69 | #include "net/test/test_with_scoped_task_environment.h" |
[email protected] | 578968d4 | 2017-12-13 15:39:32 | [diff] [blame] | 70 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 71 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 72 | #include "testing/gtest/include/gtest/gtest.h" |
| 73 | #include "testing/platform_test.h" |
tfarina | e8cb8aa | 2016-10-21 02:44:01 | [diff] [blame] | 74 | #include "third_party/boringssl/src/include/openssl/evp.h" |
| 75 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 76 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 77 | using net::test::IsError; |
| 78 | using net::test::IsOk; |
| 79 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 80 | namespace net { |
| 81 | |
| 82 | namespace { |
| 83 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 84 | const char kClientCertFileName[] = "client_1.pem"; |
| 85 | const char kClientPrivateKeyFileName[] = "client_1.pk8"; |
| 86 | const char kWrongClientCertFileName[] = "client_2.pem"; |
| 87 | const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 88 | |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 89 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 90 | public: |
| 91 | MockCTPolicyEnforcer() = default; |
| 92 | ~MockCTPolicyEnforcer() override = default; |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 93 | ct::CTPolicyCompliance CheckCompliance( |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 94 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 95 | const ct::SCTList& verified_scts, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 96 | const NetLogWithSource& net_log) override { |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 97 | return ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS; |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 98 | } |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 99 | }; |
| 100 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 101 | class FakeDataChannel { |
| 102 | public: |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 103 | FakeDataChannel() |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 104 | : read_buf_len_(0), |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 105 | closed_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 106 | write_called_after_close_(false), |
| 107 | weak_factory_(this) { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 108 | } |
| 109 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 110 | int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback) { |
[email protected] | 4da8228 | 2014-07-16 18:40:43 | [diff] [blame] | 111 | DCHECK(read_callback_.is_null()); |
dcheng | 08ea2af0 | 2014-08-25 23:38:09 | [diff] [blame] | 112 | DCHECK(!read_buf_.get()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 113 | if (closed_) |
| 114 | return 0; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 115 | if (data_.empty()) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 116 | read_callback_ = std::move(callback); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 117 | read_buf_ = buf; |
| 118 | read_buf_len_ = buf_len; |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 119 | return ERR_IO_PENDING; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 120 | } |
tfarina | 9b638144 | 2015-10-05 22:38:11 | [diff] [blame] | 121 | return PropagateData(buf, buf_len); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 124 | int Write(IOBuffer* buf, |
| 125 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 126 | CompletionOnceCallback callback, |
[email protected] | 578968d4 | 2017-12-13 15:39:32 | [diff] [blame] | 127 | const NetworkTrafficAnnotationTag& traffic_annotation) { |
[email protected] | 4da8228 | 2014-07-16 18:40:43 | [diff] [blame] | 128 | DCHECK(write_callback_.is_null()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 129 | if (closed_) { |
| 130 | if (write_called_after_close_) |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 131 | return ERR_CONNECTION_RESET; |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 132 | write_called_after_close_ = true; |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 133 | write_callback_ = std::move(callback); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 134 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 135 | FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback, |
| 136 | weak_factory_.GetWeakPtr())); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 137 | return ERR_IO_PENDING; |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 138 | } |
[email protected] | 4da8228 | 2014-07-16 18:40:43 | [diff] [blame] | 139 | // This function returns synchronously, so make a copy of the buffer. |
Victor Costan | cd43978 | 2018-08-30 07:27:57 | [diff] [blame] | 140 | data_.push(base::MakeRefCounted<DrainableIOBuffer>( |
| 141 | base::MakeRefCounted<StringIOBuffer>(std::string(buf->data(), buf_len)), |
[email protected] | 4da8228 | 2014-07-16 18:40:43 | [diff] [blame] | 142 | buf_len)); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 143 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 144 | FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, |
| 145 | weak_factory_.GetWeakPtr())); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 146 | return buf_len; |
| 147 | } |
| 148 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 149 | // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
| 150 | // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
| 151 | // after the FakeDataChannel is closed, the first Write() call completes |
| 152 | // asynchronously, which is necessary to reproduce bug 127822. |
| 153 | void Close() { |
| 154 | closed_ = true; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 155 | if (!read_callback_.is_null()) { |
| 156 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 157 | FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, |
| 158 | weak_factory_.GetWeakPtr())); |
| 159 | } |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 162 | private: |
| 163 | void DoReadCallback() { |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 164 | if (read_callback_.is_null()) |
| 165 | return; |
| 166 | |
| 167 | if (closed_) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 168 | std::move(read_callback_).Run(ERR_CONNECTION_CLOSED); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (data_.empty()) |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 173 | return; |
| 174 | |
tfarina | 9b638144 | 2015-10-05 22:38:11 | [diff] [blame] | 175 | int copied = PropagateData(read_buf_, read_buf_len_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 176 | read_buf_ = NULL; |
| 177 | read_buf_len_ = 0; |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 178 | std::move(read_callback_).Run(copied); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 181 | void DoWriteCallback() { |
| 182 | if (write_callback_.is_null()) |
| 183 | return; |
| 184 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 185 | std::move(write_callback_).Run(ERR_CONNECTION_RESET); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 186 | } |
| 187 | |
tfarina | 9b638144 | 2015-10-05 22:38:11 | [diff] [blame] | 188 | int PropagateData(scoped_refptr<IOBuffer> read_buf, int read_buf_len) { |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 189 | scoped_refptr<DrainableIOBuffer> buf = data_.front(); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 190 | int copied = std::min(buf->BytesRemaining(), read_buf_len); |
| 191 | memcpy(read_buf->data(), buf->data(), copied); |
| 192 | buf->DidConsume(copied); |
| 193 | |
| 194 | if (!buf->BytesRemaining()) |
| 195 | data_.pop(); |
| 196 | return copied; |
| 197 | } |
| 198 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 199 | CompletionOnceCallback read_callback_; |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 200 | scoped_refptr<IOBuffer> read_buf_; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 201 | int read_buf_len_; |
| 202 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 203 | CompletionOnceCallback write_callback_; |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 204 | |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 205 | base::queue<scoped_refptr<DrainableIOBuffer>> data_; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 206 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 207 | // True if Close() has been called. |
| 208 | bool closed_; |
| 209 | |
| 210 | // Controls the completion of Write() after the FakeDataChannel is closed. |
| 211 | // After the FakeDataChannel is closed, the first Write() call completes |
| 212 | // asynchronously. |
| 213 | bool write_called_after_close_; |
| 214 | |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 215 | base::WeakPtrFactory<FakeDataChannel> weak_factory_; |
| 216 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 217 | DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); |
| 218 | }; |
| 219 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 220 | class FakeSocket : public StreamSocket { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 221 | public: |
| 222 | FakeSocket(FakeDataChannel* incoming_channel, |
| 223 | FakeDataChannel* outgoing_channel) |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 224 | : incoming_(incoming_channel), outgoing_(outgoing_channel) {} |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 225 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 226 | ~FakeSocket() override = default; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 227 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 228 | int Read(IOBuffer* buf, |
| 229 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 230 | CompletionOnceCallback callback) override { |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 231 | // Read random number of bytes. |
| 232 | buf_len = rand() % buf_len + 1; |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 233 | return incoming_->Read(buf, buf_len, std::move(callback)); |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 234 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 235 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 236 | int Write(IOBuffer* buf, |
| 237 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 238 | CompletionOnceCallback callback, |
[email protected] | 578968d4 | 2017-12-13 15:39:32 | [diff] [blame] | 239 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 240 | // Write random number of bytes. |
| 241 | buf_len = rand() % buf_len + 1; |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 242 | return outgoing_->Write(buf, buf_len, std::move(callback), |
[email protected] | 578968d4 | 2017-12-13 15:39:32 | [diff] [blame] | 243 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 244 | } |
| 245 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 246 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 247 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 248 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 249 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 250 | int Connect(CompletionOnceCallback callback) override { return OK; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 251 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 252 | void Disconnect() override { |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 253 | incoming_->Close(); |
| 254 | outgoing_->Close(); |
| 255 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 256 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 257 | bool IsConnected() const override { return true; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 258 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 259 | bool IsConnectedAndIdle() const override { return true; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 260 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 261 | int GetPeerAddress(IPEndPoint* address) const override { |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 262 | *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 263 | return OK; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 264 | } |
| 265 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 266 | int GetLocalAddress(IPEndPoint* address) const override { |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 267 | *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 268 | return OK; |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 269 | } |
| 270 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 271 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 272 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 273 | bool WasEverUsed() const override { return true; } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 274 | |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 275 | bool WasAlpnNegotiated() const override { return false; } |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame] | 276 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 277 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 278 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 279 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 280 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 281 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 282 | out->clear(); |
| 283 | } |
| 284 | |
| 285 | void ClearConnectionAttempts() override {} |
| 286 | |
| 287 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
| 288 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 289 | int64_t GetTotalReceivedBytes() const override { |
| 290 | NOTIMPLEMENTED(); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 294 | void ApplySocketTag(const SocketTag& tag) override {} |
| 295 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 296 | private: |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 297 | NetLogWithSource net_log_; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 298 | FakeDataChannel* incoming_; |
| 299 | FakeDataChannel* outgoing_; |
| 300 | |
| 301 | DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
| 302 | }; |
| 303 | |
| 304 | } // namespace |
| 305 | |
| 306 | // Verify the correctness of the test helper classes first. |
| 307 | TEST(FakeSocketTest, DataTransfer) { |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 308 | base::test::ScopedTaskEnvironment scoped_task_environment; |
| 309 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 310 | // Establish channels between two sockets. |
| 311 | FakeDataChannel channel_1; |
| 312 | FakeDataChannel channel_2; |
| 313 | FakeSocket client(&channel_1, &channel_2); |
| 314 | FakeSocket server(&channel_2, &channel_1); |
| 315 | |
| 316 | const char kTestData[] = "testing123"; |
| 317 | const int kTestDataSize = strlen(kTestData); |
| 318 | const int kReadBufSize = 1024; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 319 | scoped_refptr<IOBuffer> write_buf = |
| 320 | base::MakeRefCounted<StringIOBuffer>(kTestData); |
| 321 | scoped_refptr<IOBuffer> read_buf = |
| 322 | base::MakeRefCounted<IOBuffer>(kReadBufSize); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 323 | |
| 324 | // Write then read. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 325 | int written = |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 326 | server.Write(write_buf.get(), kTestDataSize, CompletionOnceCallback(), |
[email protected] | 578968d4 | 2017-12-13 15:39:32 | [diff] [blame] | 327 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 328 | EXPECT_GT(written, 0); |
| 329 | EXPECT_LE(written, kTestDataSize); |
| 330 | |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 331 | int read = |
| 332 | client.Read(read_buf.get(), kReadBufSize, CompletionOnceCallback()); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 333 | EXPECT_GT(read, 0); |
| 334 | EXPECT_LE(read, written); |
| 335 | EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 336 | |
| 337 | // Read then write. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 338 | TestCompletionCallback callback; |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 339 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 340 | server.Read(read_buf.get(), kReadBufSize, callback.callback())); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 341 | |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 342 | written = |
| 343 | client.Write(write_buf.get(), kTestDataSize, CompletionOnceCallback(), |
| 344 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 345 | EXPECT_GT(written, 0); |
| 346 | EXPECT_LE(written, kTestDataSize); |
| 347 | |
| 348 | read = callback.WaitForResult(); |
| 349 | EXPECT_GT(read, 0); |
| 350 | EXPECT_LE(read, written); |
| 351 | EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 352 | } |
| 353 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 354 | class SSLServerSocketTest : public PlatformTest, |
| 355 | public WithScopedTaskEnvironment { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 356 | public: |
| 357 | SSLServerSocketTest() |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 358 | : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 359 | cert_verifier_(new MockCertVerifier()), |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 360 | client_cert_verifier_(new MockClientCertVerifier()), |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 361 | transport_security_state_(new TransportSecurityState), |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 362 | ct_verifier_(new DoNothingCTVerifier), |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 363 | ct_policy_enforcer_(new MockCTPolicyEnforcer) {} |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 364 | |
| 365 | void SetUp() override { |
| 366 | PlatformTest::SetUp(); |
| 367 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 368 | cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); |
| 369 | client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 370 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 371 | server_cert_ = |
| 372 | ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der"); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 373 | ASSERT_TRUE(server_cert_); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 374 | server_private_key_ = ReadTestKey("unittest.key.bin"); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 375 | ASSERT_TRUE(server_private_key_); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 376 | |
Ryan Ki Sing Chung | 665861e | 2017-12-15 22:05:55 | [diff] [blame] | 377 | std::unique_ptr<crypto::RSAPrivateKey> key = |
| 378 | ReadTestKey("unittest.key.bin"); |
| 379 | ASSERT_TRUE(key); |
David Benjamin | 139c557 | 2018-07-06 23:53:42 | [diff] [blame] | 380 | server_ssl_private_key_ = WrapOpenSSLPrivateKey(bssl::UpRef(key->key())); |
Ryan Ki Sing Chung | 665861e | 2017-12-15 22:05:55 | [diff] [blame] | 381 | |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 382 | client_ssl_config_.false_start_enabled = false; |
| 383 | client_ssl_config_.channel_id_enabled = false; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 384 | |
| 385 | // Certificate provided by the host doesn't need authority. |
rsleevi | 74e9974 | 2016-09-13 20:35:25 | [diff] [blame] | 386 | client_ssl_config_.allowed_bad_certs.emplace_back( |
| 387 | server_cert_, CERT_STATUS_AUTHORITY_INVALID); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | protected: |
| 391 | void CreateContext() { |
| 392 | client_socket_.reset(); |
| 393 | server_socket_.reset(); |
| 394 | channel_1_.reset(); |
| 395 | channel_2_.reset(); |
| 396 | server_context_.reset(); |
| 397 | server_context_ = CreateSSLServerContext( |
| 398 | server_cert_.get(), *server_private_key_, server_ssl_config_); |
| 399 | } |
| 400 | |
Ryan Ki Sing Chung | 665861e | 2017-12-15 22:05:55 | [diff] [blame] | 401 | void CreateContextSSLPrivateKey() { |
| 402 | client_socket_.reset(); |
| 403 | server_socket_.reset(); |
| 404 | channel_1_.reset(); |
| 405 | channel_2_.reset(); |
| 406 | server_context_.reset(); |
| 407 | server_context_ = CreateSSLServerContext( |
| 408 | server_cert_.get(), server_ssl_private_key_, server_ssl_config_); |
| 409 | } |
| 410 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 411 | void CreateSockets() { |
| 412 | client_socket_.reset(); |
| 413 | server_socket_.reset(); |
| 414 | channel_1_.reset(new FakeDataChannel()); |
| 415 | channel_2_.reset(new FakeDataChannel()); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 416 | std::unique_ptr<ClientSocketHandle> client_connection( |
| 417 | new ClientSocketHandle); |
| 418 | client_connection->SetSocket(std::unique_ptr<StreamSocket>( |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 419 | new FakeSocket(channel_1_.get(), channel_2_.get()))); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 420 | std::unique_ptr<StreamSocket> server_socket( |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 421 | new FakeSocket(channel_2_.get(), channel_1_.get())); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 422 | |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 423 | HostPortPair host_and_pair("unittest", 0); |
| 424 | SSLClientSocketContext context; |
[email protected] | 9f59fac | 2012-03-21 23:18:11 | [diff] [blame] | 425 | context.cert_verifier = cert_verifier_.get(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 426 | context.transport_security_state = transport_security_state_.get(); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 427 | context.cert_transparency_verifier = ct_verifier_.get(); |
| 428 | context.ct_policy_enforcer = ct_policy_enforcer_.get(); |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 429 | // Set a dummy session cache shard to enable session caching. |
| 430 | context.ssl_session_cache_shard = "shard"; |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 431 | |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 432 | client_socket_ = socket_factory_->CreateSSLClientSocket( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 433 | std::move(client_connection), host_and_pair, client_ssl_config_, |
| 434 | context); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 435 | ASSERT_TRUE(client_socket_); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 436 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 437 | server_socket_ = |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 438 | server_context_->CreateSSLServerSocket(std::move(server_socket)); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 439 | ASSERT_TRUE(server_socket_); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 440 | } |
| 441 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 442 | void ConfigureClientCertsForClient(const char* cert_file_name, |
| 443 | const char* private_key_file_name) { |
| 444 | client_ssl_config_.send_client_cert = true; |
| 445 | client_ssl_config_.client_cert = |
| 446 | ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); |
| 447 | ASSERT_TRUE(client_ssl_config_.client_cert); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 448 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 449 | std::unique_ptr<crypto::RSAPrivateKey> key = |
| 450 | ReadTestKey(private_key_file_name); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 451 | ASSERT_TRUE(key); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 452 | |
agl | 5a7cadf | 2016-07-13 16:52:53 | [diff] [blame] | 453 | client_ssl_config_.client_private_key = |
David Benjamin | 139c557 | 2018-07-06 23:53:42 | [diff] [blame] | 454 | WrapOpenSSLPrivateKey(bssl::UpRef(key->key())); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void ConfigureClientCertsForServer() { |
| 458 | server_ssl_config_.client_cert_type = |
| 459 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 460 | |
David Benjamin | 99dada2 | 2017-09-28 20:04:00 | [diff] [blame] | 461 | // "CN=B CA" - DER encoded DN of the issuer of client_1.pem |
| 462 | static const uint8_t kClientCertCAName[] = { |
| 463 | 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, |
| 464 | 0x04, 0x03, 0x0c, 0x04, 0x42, 0x20, 0x43, 0x41}; |
| 465 | server_ssl_config_.cert_authorities_.push_back(std::string( |
| 466 | std::begin(kClientCertCAName), std::end(kClientCertCAName))); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 467 | |
| 468 | scoped_refptr<X509Certificate> expected_client_cert( |
| 469 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName)); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 470 | ASSERT_TRUE(expected_client_cert); |
| 471 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 472 | client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK); |
| 473 | |
| 474 | server_ssl_config_.client_cert_verifier = client_cert_verifier_.get(); |
| 475 | } |
| 476 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 477 | std::unique_ptr<crypto::RSAPrivateKey> ReadTestKey( |
| 478 | const base::StringPiece& name) { |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 479 | base::FilePath certs_dir(GetTestCertsDirectory()); |
| 480 | base::FilePath key_path = certs_dir.AppendASCII(name); |
| 481 | std::string key_string; |
| 482 | if (!base::ReadFileToString(key_path, &key_string)) |
| 483 | return nullptr; |
| 484 | std::vector<uint8_t> key_vector( |
| 485 | reinterpret_cast<const uint8_t*>(key_string.data()), |
| 486 | reinterpret_cast<const uint8_t*>(key_string.data() + |
| 487 | key_string.length())); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 488 | std::unique_ptr<crypto::RSAPrivateKey> key( |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 489 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 490 | return key; |
| 491 | } |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 492 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 493 | void PumpServerToClient() { |
| 494 | const int kReadBufSize = 1024; |
| 495 | scoped_refptr<StringIOBuffer> write_buf = |
| 496 | base::MakeRefCounted<StringIOBuffer>("testing123"); |
| 497 | scoped_refptr<DrainableIOBuffer> read_buf = |
| 498 | base::MakeRefCounted<DrainableIOBuffer>( |
| 499 | base::MakeRefCounted<IOBuffer>(kReadBufSize), kReadBufSize); |
| 500 | TestCompletionCallback write_callback; |
| 501 | TestCompletionCallback read_callback; |
| 502 | int server_ret = server_socket_->Write(write_buf.get(), write_buf->size(), |
| 503 | write_callback.callback(), |
| 504 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 505 | EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
| 506 | int client_ret = client_socket_->Read( |
| 507 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
| 508 | EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING); |
| 509 | |
| 510 | server_ret = write_callback.GetResult(server_ret); |
| 511 | EXPECT_GT(server_ret, 0); |
| 512 | client_ret = read_callback.GetResult(client_ret); |
| 513 | ASSERT_GT(client_ret, 0); |
| 514 | } |
| 515 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 516 | std::unique_ptr<FakeDataChannel> channel_1_; |
| 517 | std::unique_ptr<FakeDataChannel> channel_2_; |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 518 | SSLConfig client_ssl_config_; |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 519 | SSLServerConfig server_ssl_config_; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 520 | std::unique_ptr<SSLClientSocket> client_socket_; |
| 521 | std::unique_ptr<SSLServerSocket> server_socket_; |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 522 | ClientSocketFactory* socket_factory_; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 523 | std::unique_ptr<MockCertVerifier> cert_verifier_; |
| 524 | std::unique_ptr<MockClientCertVerifier> client_cert_verifier_; |
| 525 | std::unique_ptr<TransportSecurityState> transport_security_state_; |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 526 | std::unique_ptr<DoNothingCTVerifier> ct_verifier_; |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 527 | std::unique_ptr<MockCTPolicyEnforcer> ct_policy_enforcer_; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 528 | std::unique_ptr<SSLServerContext> server_context_; |
| 529 | std::unique_ptr<crypto::RSAPrivateKey> server_private_key_; |
Ryan Ki Sing Chung | 665861e | 2017-12-15 22:05:55 | [diff] [blame] | 530 | scoped_refptr<SSLPrivateKey> server_ssl_private_key_; |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 531 | scoped_refptr<X509Certificate> server_cert_; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 532 | }; |
| 533 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 534 | // This test only executes creation of client and server sockets. This is to |
| 535 | // test that creation of sockets doesn't crash and have minimal code to run |
Mostyn Bramley-Moore | 699c531 | 2018-05-01 10:48:09 | [diff] [blame] | 536 | // with memory leak/corruption checking tools. |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 537 | TEST_F(SSLServerSocketTest, Initialize) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 538 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 539 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 540 | } |
| 541 | |
[email protected] | a7ac3c3 | 2011-06-17 19:10:15 | [diff] [blame] | 542 | // This test executes Connect() on SSLClientSocket and Handshake() on |
| 543 | // SSLServerSocket to make sure handshaking between the two sockets is |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 544 | // completed successfully. |
| 545 | TEST_F(SSLServerSocketTest, Handshake) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 546 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 547 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 548 | |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 549 | TestCompletionCallback handshake_callback; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 550 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 551 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 552 | TestCompletionCallback connect_callback; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 553 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 554 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 555 | client_ret = connect_callback.GetResult(client_ret); |
| 556 | server_ret = handshake_callback.GetResult(server_ret); |
| 557 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 558 | ASSERT_THAT(client_ret, IsOk()); |
| 559 | ASSERT_THAT(server_ret, IsOk()); |
[email protected] | 4dc832e | 2011-04-28 22:04:24 | [diff] [blame] | 560 | |
| 561 | // Make sure the cert status is expected. |
| 562 | SSLInfo ssl_info; |
davidben | 9dd8487 | 2015-05-02 00:22:58 | [diff] [blame] | 563 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
[email protected] | 4dc832e | 2011-04-28 22:04:24 | [diff] [blame] | 564 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
davidben | 9dd8487 | 2015-05-02 00:22:58 | [diff] [blame] | 565 | |
davidben | 56a8aece | 2016-10-14 18:20:56 | [diff] [blame] | 566 | // The default cipher suite should be ECDHE and an AEAD. |
davidben | 9dd8487 | 2015-05-02 00:22:58 | [diff] [blame] | 567 | uint16_t cipher_suite = |
| 568 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
| 569 | const char* key_exchange; |
| 570 | const char* cipher; |
| 571 | const char* mac; |
| 572 | bool is_aead; |
davidben | 56a8aece | 2016-10-14 18:20:56 | [diff] [blame] | 573 | bool is_tls13; |
| 574 | SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, &is_tls13, |
| 575 | cipher_suite); |
hans | c048c38b | 2016-10-14 11:30:09 | [diff] [blame] | 576 | EXPECT_TRUE(is_aead); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 577 | } |
| 578 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 579 | // This test makes sure the session cache is working. |
| 580 | TEST_F(SSLServerSocketTest, HandshakeCached) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 581 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 582 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 583 | |
| 584 | TestCompletionCallback handshake_callback; |
| 585 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 586 | |
| 587 | TestCompletionCallback connect_callback; |
| 588 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 589 | |
| 590 | client_ret = connect_callback.GetResult(client_ret); |
| 591 | server_ret = handshake_callback.GetResult(server_ret); |
| 592 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 593 | ASSERT_THAT(client_ret, IsOk()); |
| 594 | ASSERT_THAT(server_ret, IsOk()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 595 | |
| 596 | // Make sure the cert status is expected. |
| 597 | SSLInfo ssl_info; |
| 598 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
| 599 | EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 600 | SSLInfo ssl_server_info; |
| 601 | ASSERT_TRUE(server_socket_->GetSSLInfo(&ssl_server_info)); |
| 602 | EXPECT_EQ(ssl_server_info.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 603 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 604 | // Pump client read to get new session tickets. |
| 605 | PumpServerToClient(); |
| 606 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 607 | // Make sure the second connection is cached. |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 608 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 609 | TestCompletionCallback handshake_callback2; |
| 610 | int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); |
| 611 | |
| 612 | TestCompletionCallback connect_callback2; |
| 613 | int client_ret2 = client_socket_->Connect(connect_callback2.callback()); |
| 614 | |
| 615 | client_ret2 = connect_callback2.GetResult(client_ret2); |
| 616 | server_ret2 = handshake_callback2.GetResult(server_ret2); |
| 617 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 618 | ASSERT_THAT(client_ret2, IsOk()); |
| 619 | ASSERT_THAT(server_ret2, IsOk()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 620 | |
| 621 | // Make sure the cert status is expected. |
| 622 | SSLInfo ssl_info2; |
| 623 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info2)); |
| 624 | EXPECT_EQ(ssl_info2.handshake_type, SSLInfo::HANDSHAKE_RESUME); |
| 625 | SSLInfo ssl_server_info2; |
| 626 | ASSERT_TRUE(server_socket_->GetSSLInfo(&ssl_server_info2)); |
| 627 | EXPECT_EQ(ssl_server_info2.handshake_type, SSLInfo::HANDSHAKE_RESUME); |
| 628 | } |
| 629 | |
| 630 | // This test makes sure the session cache separates out by server context. |
| 631 | TEST_F(SSLServerSocketTest, HandshakeCachedContextSwitch) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 632 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 633 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 634 | |
| 635 | TestCompletionCallback handshake_callback; |
| 636 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 637 | |
| 638 | TestCompletionCallback connect_callback; |
| 639 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 640 | |
| 641 | client_ret = connect_callback.GetResult(client_ret); |
| 642 | server_ret = handshake_callback.GetResult(server_ret); |
| 643 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 644 | ASSERT_THAT(client_ret, IsOk()); |
| 645 | ASSERT_THAT(server_ret, IsOk()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 646 | |
| 647 | // Make sure the cert status is expected. |
| 648 | SSLInfo ssl_info; |
| 649 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
| 650 | EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 651 | SSLInfo ssl_server_info; |
| 652 | ASSERT_TRUE(server_socket_->GetSSLInfo(&ssl_server_info)); |
| 653 | EXPECT_EQ(ssl_server_info.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 654 | |
| 655 | // Make sure the second connection is NOT cached when using a new context. |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 656 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 657 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 658 | |
| 659 | TestCompletionCallback handshake_callback2; |
| 660 | int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); |
| 661 | |
| 662 | TestCompletionCallback connect_callback2; |
| 663 | int client_ret2 = client_socket_->Connect(connect_callback2.callback()); |
| 664 | |
| 665 | client_ret2 = connect_callback2.GetResult(client_ret2); |
| 666 | server_ret2 = handshake_callback2.GetResult(server_ret2); |
| 667 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 668 | ASSERT_THAT(client_ret2, IsOk()); |
| 669 | ASSERT_THAT(server_ret2, IsOk()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 670 | |
| 671 | // Make sure the cert status is expected. |
| 672 | SSLInfo ssl_info2; |
| 673 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info2)); |
| 674 | EXPECT_EQ(ssl_info2.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 675 | SSLInfo ssl_server_info2; |
| 676 | ASSERT_TRUE(server_socket_->GetSSLInfo(&ssl_server_info2)); |
| 677 | EXPECT_EQ(ssl_server_info2.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 678 | } |
| 679 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 680 | // This test executes Connect() on SSLClientSocket and Handshake() on |
| 681 | // SSLServerSocket to make sure handshaking between the two sockets is |
| 682 | // completed successfully, using client certificate. |
| 683 | TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { |
| 684 | scoped_refptr<X509Certificate> client_cert = |
| 685 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 686 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForClient( |
| 687 | kClientCertFileName, kClientPrivateKeyFileName)); |
| 688 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 689 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 690 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 691 | |
| 692 | TestCompletionCallback handshake_callback; |
| 693 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 694 | |
| 695 | TestCompletionCallback connect_callback; |
| 696 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 697 | |
| 698 | client_ret = connect_callback.GetResult(client_ret); |
| 699 | server_ret = handshake_callback.GetResult(server_ret); |
| 700 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 701 | ASSERT_THAT(client_ret, IsOk()); |
| 702 | ASSERT_THAT(server_ret, IsOk()); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 703 | |
| 704 | // Make sure the cert status is expected. |
| 705 | SSLInfo ssl_info; |
| 706 | client_socket_->GetSSLInfo(&ssl_info); |
| 707 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 708 | server_socket_->GetSSLInfo(&ssl_info); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 709 | ASSERT_TRUE(ssl_info.cert.get()); |
Matt Mueller | 294998d | 2018-04-17 03:04:53 | [diff] [blame] | 710 | EXPECT_TRUE(client_cert->EqualsExcludingChain(ssl_info.cert.get())); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 711 | } |
| 712 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 713 | // This test executes Connect() on SSLClientSocket and Handshake() twice on |
| 714 | // SSLServerSocket to make sure handshaking between the two sockets is |
| 715 | // completed successfully, using client certificate. The second connection is |
| 716 | // expected to succeed through the session cache. |
| 717 | TEST_F(SSLServerSocketTest, HandshakeWithClientCertCached) { |
| 718 | scoped_refptr<X509Certificate> client_cert = |
| 719 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 720 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForClient( |
| 721 | kClientCertFileName, kClientPrivateKeyFileName)); |
| 722 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 723 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 724 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 725 | |
| 726 | TestCompletionCallback handshake_callback; |
| 727 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 728 | |
| 729 | TestCompletionCallback connect_callback; |
| 730 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 731 | |
| 732 | client_ret = connect_callback.GetResult(client_ret); |
| 733 | server_ret = handshake_callback.GetResult(server_ret); |
| 734 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 735 | ASSERT_THAT(client_ret, IsOk()); |
| 736 | ASSERT_THAT(server_ret, IsOk()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 737 | |
| 738 | // Make sure the cert status is expected. |
| 739 | SSLInfo ssl_info; |
| 740 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
| 741 | EXPECT_EQ(ssl_info.handshake_type, SSLInfo::HANDSHAKE_FULL); |
| 742 | SSLInfo ssl_server_info; |
| 743 | ASSERT_TRUE(server_socket_->GetSSLInfo(&ssl_server_info)); |
| 744 | ASSERT_TRUE(ssl_server_info.cert.get()); |
Matt Mueller | 294998d | 2018-04-17 03:04:53 | [diff] [blame] | 745 | EXPECT_TRUE(client_cert->EqualsExcludingChain(ssl_server_info.cert.get())); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 746 | EXPECT_EQ(ssl_server_info.handshake_type, SSLInfo::HANDSHAKE_FULL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 747 | // Pump client read to get new session tickets. |
| 748 | PumpServerToClient(); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 749 | server_socket_->Disconnect(); |
| 750 | client_socket_->Disconnect(); |
| 751 | |
| 752 | // Create the connection again. |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 753 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 754 | TestCompletionCallback handshake_callback2; |
| 755 | int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); |
| 756 | |
| 757 | TestCompletionCallback connect_callback2; |
| 758 | int client_ret2 = client_socket_->Connect(connect_callback2.callback()); |
| 759 | |
| 760 | client_ret2 = connect_callback2.GetResult(client_ret2); |
| 761 | server_ret2 = handshake_callback2.GetResult(server_ret2); |
| 762 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 763 | ASSERT_THAT(client_ret2, IsOk()); |
| 764 | ASSERT_THAT(server_ret2, IsOk()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 765 | |
| 766 | // Make sure the cert status is expected. |
| 767 | SSLInfo ssl_info2; |
| 768 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info2)); |
| 769 | EXPECT_EQ(ssl_info2.handshake_type, SSLInfo::HANDSHAKE_RESUME); |
| 770 | SSLInfo ssl_server_info2; |
| 771 | ASSERT_TRUE(server_socket_->GetSSLInfo(&ssl_server_info2)); |
| 772 | ASSERT_TRUE(ssl_server_info2.cert.get()); |
Matt Mueller | 294998d | 2018-04-17 03:04:53 | [diff] [blame] | 773 | EXPECT_TRUE(client_cert->EqualsExcludingChain(ssl_server_info2.cert.get())); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 774 | EXPECT_EQ(ssl_server_info2.handshake_type, SSLInfo::HANDSHAKE_RESUME); |
| 775 | } |
| 776 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 777 | TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 778 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 779 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 780 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 781 | // Use the default setting for the client socket, which is to not send |
| 782 | // a client certificate. This will cause the client to receive an |
| 783 | // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the |
| 784 | // requested cert_authorities from the CertificateRequest sent by the |
| 785 | // server. |
| 786 | |
| 787 | TestCompletionCallback handshake_callback; |
| 788 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 789 | |
| 790 | TestCompletionCallback connect_callback; |
| 791 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
| 792 | connect_callback.GetResult( |
| 793 | client_socket_->Connect(connect_callback.callback()))); |
| 794 | |
| 795 | scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); |
| 796 | client_socket_->GetSSLCertRequestInfo(request_info.get()); |
| 797 | |
| 798 | // Check that the authority name that arrived in the CertificateRequest |
| 799 | // handshake message is as expected. |
| 800 | scoped_refptr<X509Certificate> client_cert = |
| 801 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 802 | ASSERT_TRUE(client_cert); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 803 | EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); |
| 804 | |
| 805 | client_socket_->Disconnect(); |
| 806 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 807 | EXPECT_THAT(handshake_callback.GetResult(server_ret), |
| 808 | IsError(ERR_CONNECTION_CLOSED)); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 809 | } |
| 810 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 811 | TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSuppliedCached) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 812 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 813 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 814 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 815 | // Use the default setting for the client socket, which is to not send |
| 816 | // a client certificate. This will cause the client to receive an |
| 817 | // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the |
| 818 | // requested cert_authorities from the CertificateRequest sent by the |
| 819 | // server. |
| 820 | |
| 821 | TestCompletionCallback handshake_callback; |
| 822 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 823 | |
| 824 | TestCompletionCallback connect_callback; |
| 825 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
| 826 | connect_callback.GetResult( |
| 827 | client_socket_->Connect(connect_callback.callback()))); |
| 828 | |
| 829 | scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); |
| 830 | client_socket_->GetSSLCertRequestInfo(request_info.get()); |
| 831 | |
| 832 | // Check that the authority name that arrived in the CertificateRequest |
| 833 | // handshake message is as expected. |
| 834 | scoped_refptr<X509Certificate> client_cert = |
| 835 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 836 | ASSERT_TRUE(client_cert); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 837 | EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); |
| 838 | |
| 839 | client_socket_->Disconnect(); |
| 840 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 841 | EXPECT_THAT(handshake_callback.GetResult(server_ret), |
| 842 | IsError(ERR_CONNECTION_CLOSED)); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 843 | server_socket_->Disconnect(); |
| 844 | |
| 845 | // Below, check that the cache didn't store the result of a failed handshake. |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 846 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 847 | TestCompletionCallback handshake_callback2; |
| 848 | int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); |
| 849 | |
| 850 | TestCompletionCallback connect_callback2; |
| 851 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
| 852 | connect_callback2.GetResult( |
| 853 | client_socket_->Connect(connect_callback2.callback()))); |
| 854 | |
| 855 | scoped_refptr<SSLCertRequestInfo> request_info2 = new SSLCertRequestInfo(); |
| 856 | client_socket_->GetSSLCertRequestInfo(request_info2.get()); |
| 857 | |
| 858 | // Check that the authority name that arrived in the CertificateRequest |
| 859 | // handshake message is as expected. |
| 860 | EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info2->cert_authorities)); |
| 861 | |
| 862 | client_socket_->Disconnect(); |
| 863 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 864 | EXPECT_THAT(handshake_callback2.GetResult(server_ret2), |
| 865 | IsError(ERR_CONNECTION_CLOSED)); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 866 | } |
| 867 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 868 | TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { |
| 869 | scoped_refptr<X509Certificate> client_cert = |
| 870 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 871 | ASSERT_TRUE(client_cert); |
| 872 | |
| 873 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForClient( |
| 874 | kWrongClientCertFileName, kWrongClientPrivateKeyFileName)); |
| 875 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 876 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 877 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 878 | |
| 879 | TestCompletionCallback handshake_callback; |
| 880 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 881 | |
| 882 | TestCompletionCallback connect_callback; |
| 883 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 884 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 885 | // In TLS 1.3, the client cert error isn't exposed until Read is called. |
| 886 | EXPECT_EQ(OK, connect_callback.GetResult(client_ret)); |
| 887 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 888 | handshake_callback.GetResult(server_ret)); |
| 889 | |
| 890 | // Pump client read to get client cert error. |
| 891 | const int kReadBufSize = 1024; |
| 892 | scoped_refptr<DrainableIOBuffer> read_buf = |
| 893 | base::MakeRefCounted<DrainableIOBuffer>( |
| 894 | base::MakeRefCounted<IOBuffer>(kReadBufSize), kReadBufSize); |
| 895 | TestCompletionCallback read_callback; |
| 896 | client_ret = client_socket_->Read(read_buf.get(), read_buf->BytesRemaining(), |
| 897 | read_callback.callback()); |
| 898 | client_ret = read_callback.GetResult(client_ret); |
| 899 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, client_ret); |
| 900 | } |
| 901 | |
| 902 | TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSuppliedTLS12) { |
| 903 | scoped_refptr<X509Certificate> client_cert = |
| 904 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 905 | ASSERT_TRUE(client_cert); |
| 906 | |
| 907 | client_ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 908 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForClient( |
| 909 | kWrongClientCertFileName, kWrongClientPrivateKeyFileName)); |
| 910 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 911 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 912 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
| 913 | |
| 914 | TestCompletionCallback handshake_callback; |
| 915 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 916 | |
| 917 | TestCompletionCallback connect_callback; |
| 918 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 919 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 920 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 921 | connect_callback.GetResult(client_ret)); |
| 922 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 923 | handshake_callback.GetResult(server_ret)); |
| 924 | } |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 925 | |
| 926 | TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSuppliedCached) { |
| 927 | scoped_refptr<X509Certificate> client_cert = |
| 928 | ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 929 | ASSERT_TRUE(client_cert); |
| 930 | |
| 931 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForClient( |
| 932 | kWrongClientCertFileName, kWrongClientPrivateKeyFileName)); |
| 933 | ASSERT_NO_FATAL_FAILURE(ConfigureClientCertsForServer()); |
| 934 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 935 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 936 | |
| 937 | TestCompletionCallback handshake_callback; |
| 938 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 939 | |
| 940 | TestCompletionCallback connect_callback; |
| 941 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 942 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 943 | // In TLS 1.3, the client cert error isn't exposed until Read is called. |
| 944 | EXPECT_EQ(OK, connect_callback.GetResult(client_ret)); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 945 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 946 | handshake_callback.GetResult(server_ret)); |
| 947 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 948 | // Pump client read to get client cert error. |
| 949 | const int kReadBufSize = 1024; |
| 950 | scoped_refptr<DrainableIOBuffer> read_buf = |
| 951 | base::MakeRefCounted<DrainableIOBuffer>( |
| 952 | base::MakeRefCounted<IOBuffer>(kReadBufSize), kReadBufSize); |
| 953 | TestCompletionCallback read_callback; |
| 954 | client_ret = client_socket_->Read(read_buf.get(), read_buf->BytesRemaining(), |
| 955 | read_callback.callback()); |
| 956 | client_ret = read_callback.GetResult(client_ret); |
| 957 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, client_ret); |
| 958 | |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 959 | client_socket_->Disconnect(); |
| 960 | server_socket_->Disconnect(); |
| 961 | |
| 962 | // Below, check that the cache didn't store the result of a failed handshake. |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 963 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 964 | TestCompletionCallback handshake_callback2; |
| 965 | int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); |
| 966 | |
| 967 | TestCompletionCallback connect_callback2; |
| 968 | int client_ret2 = client_socket_->Connect(connect_callback2.callback()); |
| 969 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 970 | // In TLS 1.3, the client cert error isn't exposed until Read is called. |
| 971 | EXPECT_EQ(OK, connect_callback2.GetResult(client_ret2)); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 972 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 973 | handshake_callback2.GetResult(server_ret2)); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 974 | |
| 975 | // Pump client read to get client cert error. |
| 976 | client_ret = client_socket_->Read(read_buf.get(), read_buf->BytesRemaining(), |
| 977 | read_callback.callback()); |
| 978 | client_ret = read_callback.GetResult(client_ret); |
| 979 | EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, client_ret); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 980 | } |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 981 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 982 | TEST_F(SSLServerSocketTest, DataTransfer) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 983 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 984 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 985 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 986 | // Establish connection. |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 987 | TestCompletionCallback connect_callback; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 988 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 989 | ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 990 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 991 | TestCompletionCallback handshake_callback; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 992 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 993 | ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 994 | |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 995 | client_ret = connect_callback.GetResult(client_ret); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 996 | ASSERT_THAT(client_ret, IsOk()); |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 997 | server_ret = handshake_callback.GetResult(server_ret); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 998 | ASSERT_THAT(server_ret, IsOk()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 999 | |
| 1000 | const int kReadBufSize = 1024; |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1001 | scoped_refptr<StringIOBuffer> write_buf = |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 1002 | base::MakeRefCounted<StringIOBuffer>("testing123"); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1003 | scoped_refptr<DrainableIOBuffer> read_buf = |
Victor Costan | cd43978 | 2018-08-30 07:27:57 | [diff] [blame] | 1004 | base::MakeRefCounted<DrainableIOBuffer>( |
| 1005 | base::MakeRefCounted<IOBuffer>(kReadBufSize), kReadBufSize); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1006 | |
| 1007 | // Write then read. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 1008 | TestCompletionCallback write_callback; |
| 1009 | TestCompletionCallback read_callback; |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1010 | server_ret = server_socket_->Write(write_buf.get(), write_buf->size(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 1011 | write_callback.callback(), |
| 1012 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1013 | EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1014 | client_ret = client_socket_->Read( |
| 1015 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1016 | EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1017 | |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 1018 | server_ret = write_callback.GetResult(server_ret); |
| 1019 | EXPECT_GT(server_ret, 0); |
| 1020 | client_ret = read_callback.GetResult(client_ret); |
| 1021 | ASSERT_GT(client_ret, 0); |
| 1022 | |
| 1023 | read_buf->DidConsume(client_ret); |
| 1024 | while (read_buf->BytesConsumed() < write_buf->size()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1025 | client_ret = client_socket_->Read( |
| 1026 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1027 | EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING); |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 1028 | client_ret = read_callback.GetResult(client_ret); |
| 1029 | ASSERT_GT(client_ret, 0); |
| 1030 | read_buf->DidConsume(client_ret); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1031 | } |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 1032 | EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed()); |
| 1033 | read_buf->SetOffset(0); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1034 | EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 1035 | |
| 1036 | // Read then write. |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 1037 | write_buf = base::MakeRefCounted<StringIOBuffer>("hello123"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1038 | server_ret = server_socket_->Read( |
| 1039 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1040 | EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1041 | client_ret = client_socket_->Write(write_buf.get(), write_buf->size(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 1042 | write_callback.callback(), |
| 1043 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1044 | EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1045 | |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 1046 | server_ret = read_callback.GetResult(server_ret); |
| 1047 | ASSERT_GT(server_ret, 0); |
| 1048 | client_ret = write_callback.GetResult(client_ret); |
| 1049 | EXPECT_GT(client_ret, 0); |
| 1050 | |
| 1051 | read_buf->DidConsume(server_ret); |
| 1052 | while (read_buf->BytesConsumed() < write_buf->size()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1053 | server_ret = server_socket_->Read( |
| 1054 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1055 | EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 1056 | server_ret = read_callback.GetResult(server_ret); |
| 1057 | ASSERT_GT(server_ret, 0); |
| 1058 | read_buf->DidConsume(server_ret); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1059 | } |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 1060 | EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed()); |
| 1061 | read_buf->SetOffset(0); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1062 | EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 1063 | } |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1064 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1065 | // A regression test for bug 127822 (http://crbug.com/127822). |
| 1066 | // If the server closes the connection after the handshake is finished, |
| 1067 | // the client's Write() call should not cause an infinite loop. |
| 1068 | // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. |
[email protected] | 4da8228 | 2014-07-16 18:40:43 | [diff] [blame] | 1069 | TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 1070 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 1071 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1072 | |
| 1073 | // Establish connection. |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 1074 | TestCompletionCallback connect_callback; |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1075 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1076 | ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1077 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 1078 | TestCompletionCallback handshake_callback; |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1079 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1080 | ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1081 | |
| 1082 | client_ret = connect_callback.GetResult(client_ret); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1083 | ASSERT_THAT(client_ret, IsOk()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1084 | server_ret = handshake_callback.GetResult(server_ret); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1085 | ASSERT_THAT(server_ret, IsOk()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1086 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 1087 | scoped_refptr<StringIOBuffer> write_buf = |
| 1088 | base::MakeRefCounted<StringIOBuffer>("testing123"); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1089 | |
| 1090 | // The server closes the connection. The server needs to write some |
| 1091 | // data first so that the client's Read() calls from the transport |
| 1092 | // socket won't return ERR_IO_PENDING. This ensures that the client |
| 1093 | // will call Read() on the transport socket again. |
| 1094 | TestCompletionCallback write_callback; |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1095 | server_ret = server_socket_->Write(write_buf.get(), write_buf->size(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 1096 | write_callback.callback(), |
| 1097 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1098 | EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1099 | |
| 1100 | server_ret = write_callback.GetResult(server_ret); |
| 1101 | EXPECT_GT(server_ret, 0); |
| 1102 | |
| 1103 | server_socket_->Disconnect(); |
| 1104 | |
| 1105 | // The client writes some data. This should not cause an infinite loop. |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1106 | client_ret = client_socket_->Write(write_buf.get(), write_buf->size(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 1107 | write_callback.callback(), |
| 1108 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1109 | EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1110 | |
| 1111 | client_ret = write_callback.GetResult(client_ret); |
| 1112 | EXPECT_GT(client_ret, 0); |
| 1113 | |
Wez | a03bae0 | 2018-07-13 17:17:33 | [diff] [blame] | 1114 | base::RunLoop run_loop; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 1115 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Wez | a03bae0 | 2018-07-13 17:17:33 | [diff] [blame] | 1116 | FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromMilliseconds(10)); |
| 1117 | run_loop.Run(); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 1118 | } |
| 1119 | |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1120 | // This test executes ExportKeyingMaterial() on the client and server sockets, |
| 1121 | // after connecting them, and verifies that the results match. |
| 1122 | // This test will fail if False Start is enabled (see crbug.com/90208). |
| 1123 | TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 1124 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 1125 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1126 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 1127 | TestCompletionCallback connect_callback; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 1128 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1129 | ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1130 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 1131 | TestCompletionCallback handshake_callback; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 1132 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1133 | ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1134 | |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1135 | if (client_ret == ERR_IO_PENDING) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1136 | ASSERT_THAT(connect_callback.WaitForResult(), IsOk()); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1137 | } |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1138 | if (server_ret == ERR_IO_PENDING) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1139 | ASSERT_THAT(handshake_callback.WaitForResult(), IsOk()); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | const int kKeyingMaterialSize = 32; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 1143 | const char kKeyingLabel[] = "EXPERIMENTAL-server-socket-test"; |
| 1144 | const char kKeyingContext[] = ""; |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1145 | unsigned char server_out[kKeyingMaterialSize]; |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1146 | int rv = server_socket_->ExportKeyingMaterial( |
| 1147 | kKeyingLabel, false, kKeyingContext, server_out, sizeof(server_out)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1148 | ASSERT_THAT(rv, IsOk()); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1149 | |
| 1150 | unsigned char client_out[kKeyingMaterialSize]; |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1151 | rv = client_socket_->ExportKeyingMaterial(kKeyingLabel, false, kKeyingContext, |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1152 | client_out, sizeof(client_out)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1153 | ASSERT_THAT(rv, IsOk()); |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 1154 | EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out))); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1155 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 1156 | const char kKeyingLabelBad[] = "EXPERIMENTAL-server-socket-test-bad"; |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1157 | unsigned char client_bad[kKeyingMaterialSize]; |
ryanchung | eb9e3bc | 2016-03-08 05:08:10 | [diff] [blame] | 1158 | rv = client_socket_->ExportKeyingMaterial( |
| 1159 | kKeyingLabelBad, false, kKeyingContext, client_bad, sizeof(client_bad)); |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 1160 | ASSERT_EQ(rv, OK); |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 1161 | EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out))); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 1162 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1163 | |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1164 | // Verifies that SSLConfig::require_ecdhe flags works properly. |
| 1165 | TEST_F(SSLServerSocketTest, RequireEcdheFlag) { |
| 1166 | // Disable all ECDHE suites on the client side. |
| 1167 | uint16_t kEcdheCiphers[] = { |
| 1168 | 0xc007, // ECDHE_ECDSA_WITH_RC4_128_SHA |
| 1169 | 0xc009, // ECDHE_ECDSA_WITH_AES_128_CBC_SHA |
| 1170 | 0xc00a, // ECDHE_ECDSA_WITH_AES_256_CBC_SHA |
| 1171 | 0xc011, // ECDHE_RSA_WITH_RC4_128_SHA |
| 1172 | 0xc013, // ECDHE_RSA_WITH_AES_128_CBC_SHA |
| 1173 | 0xc014, // ECDHE_RSA_WITH_AES_256_CBC_SHA |
| 1174 | 0xc02b, // ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 |
| 1175 | 0xc02f, // ECDHE_RSA_WITH_AES_128_GCM_SHA256 |
davidben | 17f89c8 | 2017-01-24 20:56:49 | [diff] [blame] | 1176 | 0xcca8, // ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
| 1177 | 0xcca9, // ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1178 | }; |
| 1179 | client_ssl_config_.disabled_cipher_suites.assign( |
| 1180 | kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); |
| 1181 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame^] | 1182 | // Legacy RSA key exchange ciphers only exist in TLS 1.2 and below. |
| 1183 | client_ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 1184 | |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1185 | // Require ECDHE on the server. |
| 1186 | server_ssl_config_.require_ecdhe = true; |
| 1187 | |
rsleevi | a5c43022 | 2016-03-11 05:55:12 | [diff] [blame] | 1188 | ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 1189 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1190 | |
| 1191 | TestCompletionCallback connect_callback; |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1192 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 1193 | |
| 1194 | TestCompletionCallback handshake_callback; |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1195 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 1196 | |
| 1197 | client_ret = connect_callback.GetResult(client_ret); |
| 1198 | server_ret = handshake_callback.GetResult(server_ret); |
| 1199 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1200 | ASSERT_THAT(client_ret, IsError(ERR_SSL_VERSION_OR_CIPHER_MISMATCH)); |
| 1201 | ASSERT_THAT(server_ret, IsError(ERR_SSL_VERSION_OR_CIPHER_MISMATCH)); |
sergeyu | ff826d5e | 2015-05-13 20:35:22 | [diff] [blame] | 1202 | } |
| 1203 | |
Ryan Ki Sing Chung | 665861e | 2017-12-15 22:05:55 | [diff] [blame] | 1204 | // This test executes Connect() on SSLClientSocket and Handshake() on |
| 1205 | // SSLServerSocket to make sure handshaking between the two sockets is |
| 1206 | // completed successfully. The server key is represented by SSLPrivateKey. |
| 1207 | TEST_F(SSLServerSocketTest, HandshakeServerSSLPrivateKey) { |
| 1208 | ASSERT_NO_FATAL_FAILURE(CreateContextSSLPrivateKey()); |
| 1209 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
| 1210 | |
| 1211 | TestCompletionCallback handshake_callback; |
| 1212 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 1213 | |
| 1214 | TestCompletionCallback connect_callback; |
| 1215 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 1216 | |
| 1217 | client_ret = connect_callback.GetResult(client_ret); |
| 1218 | server_ret = handshake_callback.GetResult(server_ret); |
| 1219 | |
| 1220 | ASSERT_THAT(client_ret, IsOk()); |
| 1221 | ASSERT_THAT(server_ret, IsOk()); |
| 1222 | |
| 1223 | // Make sure the cert status is expected. |
| 1224 | SSLInfo ssl_info; |
| 1225 | ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
| 1226 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 1227 | |
| 1228 | // The default cipher suite should be ECDHE and an AEAD. |
| 1229 | uint16_t cipher_suite = |
| 1230 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
| 1231 | const char* key_exchange; |
| 1232 | const char* cipher; |
| 1233 | const char* mac; |
| 1234 | bool is_aead; |
| 1235 | bool is_tls13; |
| 1236 | SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, &is_tls13, |
| 1237 | cipher_suite); |
| 1238 | EXPECT_TRUE(is_aead); |
Ryan Ki Sing Chung | 665861e | 2017-12-15 22:05:55 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | // Verifies that non-ECDHE ciphers are disabled when using SSLPrivateKey as the |
| 1242 | // server key. |
| 1243 | TEST_F(SSLServerSocketTest, HandshakeServerSSLPrivateKeyRequireEcdhe) { |
| 1244 | // Disable all ECDHE suites on the client side. |
| 1245 | uint16_t kEcdheCiphers[] = { |
| 1246 | 0xc007, // ECDHE_ECDSA_WITH_RC4_128_SHA |
| 1247 | 0xc009, // ECDHE_ECDSA_WITH_AES_128_CBC_SHA |
| 1248 | 0xc00a, // ECDHE_ECDSA_WITH_AES_256_CBC_SHA |
| 1249 | 0xc011, // ECDHE_RSA_WITH_RC4_128_SHA |
| 1250 | 0xc013, // ECDHE_RSA_WITH_AES_128_CBC_SHA |
| 1251 | 0xc014, // ECDHE_RSA_WITH_AES_256_CBC_SHA |
| 1252 | 0xc02b, // ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 |
| 1253 | 0xc02f, // ECDHE_RSA_WITH_AES_128_GCM_SHA256 |
| 1254 | 0xcca8, // ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
| 1255 | 0xcca9, // ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
| 1256 | }; |
| 1257 | client_ssl_config_.disabled_cipher_suites.assign( |
| 1258 | kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); |
| 1259 | // TLS 1.3 always works with SSLPrivateKey. |
| 1260 | client_ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 1261 | |
| 1262 | ASSERT_NO_FATAL_FAILURE(CreateContextSSLPrivateKey()); |
| 1263 | ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
| 1264 | |
| 1265 | TestCompletionCallback connect_callback; |
| 1266 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 1267 | |
| 1268 | TestCompletionCallback handshake_callback; |
| 1269 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 1270 | |
| 1271 | client_ret = connect_callback.GetResult(client_ret); |
| 1272 | server_ret = handshake_callback.GetResult(server_ret); |
| 1273 | |
| 1274 | ASSERT_THAT(client_ret, IsError(ERR_SSL_VERSION_OR_CIPHER_MISMATCH)); |
| 1275 | ASSERT_THAT(server_ret, IsError(ERR_SSL_VERSION_OR_CIPHER_MISMATCH)); |
| 1276 | } |
| 1277 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 1278 | } // namespace net |