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