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