blob: eca7b6b615b7a64141562b076ae4beef2ffce68e [file] [log] [blame]
David Benjamin025b3d32014-07-01 19:53:04 -04001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
Adam Langleyded93582014-07-31 15:23:51 -070015#include <openssl/base.h>
16
17#if !defined(OPENSSL_WINDOWS)
David Benjamin025b3d32014-07-01 19:53:04 -040018#include <arpa/inet.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040019#include <netinet/in.h>
David Benjamin87c8a642015-02-21 01:54:29 -050020#include <netinet/tcp.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040021#include <signal.h>
22#include <sys/socket.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040023#include <unistd.h>
David Benjamin87c8a642015-02-21 01:54:29 -050024#else
25#include <io.h>
26#pragma warning(push, 3)
Adam Langley3e719312015-03-20 16:32:23 -070027#include <winsock2.h>
28#include <ws2tcpip.h>
David Benjamin87c8a642015-02-21 01:54:29 -050029#pragma warning(pop)
30
31#pragma comment(lib, "Ws2_32.lib")
Adam Langleyded93582014-07-31 15:23:51 -070032#endif
33
Adam Langley2b2d66d2015-01-30 17:08:37 -080034#include <string.h>
Adam Langleyded93582014-07-31 15:23:51 -070035#include <sys/types.h>
David Benjamin025b3d32014-07-01 19:53:04 -040036
David Benjamin025b3d32014-07-01 19:53:04 -040037#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040038#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040039#include <openssl/bytestring.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040040#include <openssl/ssl.h>
41
David Benjamin45fb1be2015-03-22 16:31:27 -040042#include <memory>
43
44#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040045#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040046#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050047#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040048#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040049
David Benjamin87c8a642015-02-21 01:54:29 -050050
51#if !defined(OPENSSL_WINDOWS)
52static int closesocket(int sock) {
53 return close(sock);
54}
55
56static void PrintSocketError(const char *func) {
57 perror(func);
58}
59#else
60static void PrintSocketError(const char *func) {
61 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
62}
63#endif
64
David Benjaminc273d2c2015-02-09 12:59:46 -050065static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050066 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040067 return 1;
68}
David Benjamin025b3d32014-07-01 19:53:04 -040069
David Benjamin2d445c02015-02-09 13:03:50 -050070struct TestState {
David Benjamin6c2563e2015-04-03 03:47:47 -040071 // async_bio is async BIO which pauses reads and writes.
72 BIO *async_bio = nullptr;
73 // clock is the current time for the SSL connection.
74 OPENSSL_timeval clock = {0};
75 // clock_delta is how far the clock advanced in the most recent failed
76 // |BIO_read|.
77 OPENSSL_timeval clock_delta = {0};
David Benjamind9e07012015-02-09 03:04:34 -050078 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040079 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050080 ScopedSSL_SESSION session;
81 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040082 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040083 bool handshake_done = false;
David Benjamind9e07012015-02-09 03:04:34 -050084};
85
David Benjamin2d445c02015-02-09 13:03:50 -050086static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
87 int index, long argl, void *argp) {
88 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -050089}
90
91static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -050092static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -040093
Adam Langley69a01602014-11-17 17:26:55 -080094static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -050095 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -040096}
97
David Benjamin87e4acd2015-04-02 19:57:35 -040098static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -050099 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400100}
101
David Benjamin2d445c02015-02-09 13:03:50 -0500102static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
103 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500104 async.release();
105 return true;
106 }
107 return false;
108}
109
David Benjamin87e4acd2015-04-02 19:57:35 -0400110static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500111 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500112}
113
David Benjamina7f333d2015-02-09 02:37:18 -0500114static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
115 ScopedBIO bio(BIO_new(BIO_s_file()));
116 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
117 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400118 }
David Benjamina7f333d2015-02-09 02:37:18 -0500119 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400120 return pkey;
121}
122
David Benjamin41fdbcd2015-02-09 03:13:35 -0500123static bool InstallCertificate(SSL *ssl) {
124 const TestConfig *config = GetConfigPtr(ssl);
125 if (!config->key_file.empty() &&
126 !SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
127 SSL_FILETYPE_PEM)) {
128 return false;
129 }
130 if (!config->cert_file.empty() &&
131 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
132 SSL_FILETYPE_PEM)) {
133 return false;
134 }
135 return true;
136}
137
David Benjaminc273d2c2015-02-09 12:59:46 -0500138static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400139 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500140 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400141
David Benjamin6f5c0f42015-02-24 01:23:21 -0500142 if (!config->expected_server_name.empty()) {
143 const uint8_t *extension_data;
144 size_t extension_len;
145 CBS extension, server_name_list, host_name;
146 uint8_t name_type;
147
148 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
149 &extension_data,
150 &extension_len)) {
151 fprintf(stderr, "Could not find server_name extension.\n");
152 return -1;
153 }
154
155 CBS_init(&extension, extension_data, extension_len);
156 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
157 CBS_len(&extension) != 0 ||
158 !CBS_get_u8(&server_name_list, &name_type) ||
159 name_type != TLSEXT_NAMETYPE_host_name ||
160 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
161 CBS_len(&server_name_list) != 0) {
162 fprintf(stderr, "Could not decode server_name extension.\n");
163 return -1;
164 }
165
166 if (!CBS_mem_equal(&host_name,
167 (const uint8_t*)config->expected_server_name.data(),
168 config->expected_server_name.size())) {
169 fprintf(stderr, "Server name mismatch.\n");
170 }
David Benjamin7b030512014-07-08 17:30:11 -0400171 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400172
David Benjamin6f5c0f42015-02-24 01:23:21 -0500173 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400174 return -1;
175 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400176
David Benjamin6f5c0f42015-02-24 01:23:21 -0500177 // Install the certificate in the early callback.
178 if (config->use_early_callback) {
179 if (config->async) {
180 // Install the certificate asynchronously.
181 return 0;
182 }
183 if (!InstallCertificate(ctx->ssl)) {
184 return -1;
185 }
David Benjamin7b030512014-07-08 17:30:11 -0400186 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400187 return 1;
188}
David Benjamin025b3d32014-07-01 19:53:04 -0400189
David Benjaminc273d2c2015-02-09 12:59:46 -0500190static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400191 return 1;
192}
193
David Benjaminc273d2c2015-02-09 12:59:46 -0500194static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
195 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400196 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500197 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400198 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500199 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400200
David Benjamin5a593af2014-08-11 19:51:50 -0400201 *out = (const uint8_t*)config->advertise_npn.data();
202 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400203 return SSL_TLSEXT_ERR_OK;
204}
205
David Benjaminc273d2c2015-02-09 12:59:46 -0500206static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400207 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400208 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500209 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400210 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500211 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400212
David Benjamin5a593af2014-08-11 19:51:50 -0400213 *out = (uint8_t*)config->select_next_proto.data();
214 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400215 return SSL_TLSEXT_ERR_OK;
216}
217
David Benjaminc273d2c2015-02-09 12:59:46 -0500218static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
219 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400220 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500221 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400222 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500223 }
David Benjaminae2888f2014-09-06 12:58:58 -0400224
225 if (!config->expected_advertised_alpn.empty() &&
226 (config->expected_advertised_alpn.size() != inlen ||
227 memcmp(config->expected_advertised_alpn.data(),
228 in, inlen) != 0)) {
229 fprintf(stderr, "bad ALPN select callback inputs\n");
230 exit(1);
231 }
232
233 *out = (const uint8_t*)config->select_alpn.data();
234 *outlen = config->select_alpn.size();
235 return SSL_TLSEXT_ERR_OK;
236}
237
David Benjaminc273d2c2015-02-09 12:59:46 -0500238static unsigned PskClientCallback(SSL *ssl, const char *hint,
239 char *out_identity,
240 unsigned max_identity_len,
241 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400242 const TestConfig *config = GetConfigPtr(ssl);
243
244 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
245 fprintf(stderr, "Server PSK hint did not match.\n");
246 return 0;
247 }
248
249 // Account for the trailing '\0' for the identity.
250 if (config->psk_identity.size() >= max_identity_len ||
251 config->psk.size() > max_psk_len) {
252 fprintf(stderr, "PSK buffers too small\n");
253 return 0;
254 }
255
256 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
257 max_identity_len);
258 memcpy(out_psk, config->psk.data(), config->psk.size());
259 return config->psk.size();
260}
261
David Benjaminc273d2c2015-02-09 12:59:46 -0500262static unsigned PskServerCallback(SSL *ssl, const char *identity,
263 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400264 const TestConfig *config = GetConfigPtr(ssl);
265
266 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
267 fprintf(stderr, "Client PSK identity did not match.\n");
268 return 0;
269 }
270
271 if (config->psk.size() > max_psk_len) {
272 fprintf(stderr, "PSK buffers too small\n");
273 return 0;
274 }
275
276 memcpy(out_psk, config->psk.data(), config->psk.size());
277 return config->psk.size();
278}
279
David Benjaminc273d2c2015-02-09 12:59:46 -0500280static void CurrentTimeCallback(SSL *ssl, OPENSSL_timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400281 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500282}
283
David Benjaminc273d2c2015-02-09 12:59:46 -0500284static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500285 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500286}
287
David Benjaminc273d2c2015-02-09 12:59:46 -0500288static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500289 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500290 return -1;
291 }
292 if (!InstallCertificate(ssl)) {
293 return 0;
294 }
295 return 1;
296}
297
David Benjaminc273d2c2015-02-09 12:59:46 -0500298static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
299 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500300 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500301 if (async_state->session) {
302 *copy = 0;
303 return async_state->session.release();
304 } else if (async_state->pending_session) {
305 return SSL_magic_pending_session_ptr();
306 } else {
307 return NULL;
308 }
309}
310
Adam Langley524e7172015-02-20 16:04:00 -0800311static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
312 const TestConfig *config = GetConfigPtr(early_context->ssl);
313 static int callback_num = 0;
314
315 callback_num++;
316 if (config->fail_ddos_callback ||
317 (config->fail_second_ddos_callback && callback_num == 2)) {
318 return 0;
319 }
320 return 1;
321}
322
David Benjamin87e4acd2015-04-02 19:57:35 -0400323static void InfoCallback(const SSL *ssl, int type, int val) {
324 if (type == SSL_CB_HANDSHAKE_DONE) {
325 if (GetConfigPtr(ssl)->handshake_never_done) {
326 fprintf(stderr, "handshake completed\n");
327 // Abort before any expected error code is printed, to ensure the overall
328 // test fails.
329 abort();
330 }
331 GetTestState(ssl)->handshake_done = true;
332 }
333}
334
David Benjamin87c8a642015-02-21 01:54:29 -0500335// Connect returns a new socket connected to localhost on |port| or -1 on
336// error.
337static int Connect(uint16_t port) {
338 int sock = socket(AF_INET, SOCK_STREAM, 0);
339 if (sock == -1) {
340 PrintSocketError("socket");
341 return -1;
342 }
343 int nodelay = 1;
344 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
345 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
346 PrintSocketError("setsockopt");
347 closesocket(sock);
348 return -1;
349 }
350 sockaddr_in sin;
351 memset(&sin, 0, sizeof(sin));
352 sin.sin_family = AF_INET;
353 sin.sin_port = htons(port);
354 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
355 PrintSocketError("inet_pton");
356 closesocket(sock);
357 return -1;
358 }
359 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
360 sizeof(sin)) != 0) {
361 PrintSocketError("connect");
362 closesocket(sock);
363 return -1;
364 }
365 return sock;
366}
367
368class SocketCloser {
369 public:
370 explicit SocketCloser(int sock) : sock_(sock) {}
371 ~SocketCloser() {
372 // Half-close and drain the socket before releasing it. This seems to be
373 // necessary for graceful shutdown on Windows. It will also avoid write
374 // failures in the test runner.
375#if defined(OPENSSL_WINDOWS)
376 shutdown(sock_, SD_SEND);
377#else
378 shutdown(sock_, SHUT_WR);
379#endif
380 while (true) {
381 char buf[1024];
382 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
383 break;
384 }
385 }
386 closesocket(sock_);
387 }
388
389 private:
390 const int sock_;
391};
392
David Benjaminc273d2c2015-02-09 12:59:46 -0500393static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500394 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
395 config->is_dtls ? DTLS_method() : TLS_method()));
396 if (!ssl_ctx) {
397 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400398 }
399
David Benjamin6fd297b2014-08-11 18:43:38 -0400400 if (config->is_dtls) {
401 // DTLS needs read-ahead to function on a datagram BIO.
402 //
403 // TODO(davidben): this should not be necessary. DTLS code should only
404 // expect a datagram BIO.
David Benjamina7f333d2015-02-09 02:37:18 -0500405 SSL_CTX_set_read_ahead(ssl_ctx.get(), 1);
David Benjamin6fd297b2014-08-11 18:43:38 -0400406 }
407
David Benjamina7f333d2015-02-09 02:37:18 -0500408 if (!SSL_CTX_set_ecdh_auto(ssl_ctx.get(), 1)) {
409 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400410 }
411
David Benjamina7f333d2015-02-09 02:37:18 -0500412 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
413 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400414 }
415
David Benjamina7f333d2015-02-09 02:37:18 -0500416 ScopedDH dh(DH_get_2048_256(NULL));
417 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
418 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400419 }
420
David Benjamin1b8b6912015-02-09 04:28:16 -0500421 if (config->async && config->is_server) {
422 // Disable the internal session cache. To test asynchronous session lookup,
423 // we use an external session cache.
424 SSL_CTX_set_session_cache_mode(
425 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500426 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500427 } else {
428 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
429 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400430
David Benjaminc273d2c2015-02-09 12:59:46 -0500431 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400432
David Benjamin1f5f62b2014-07-12 16:18:02 -0400433 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500434 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400435 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500436 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500437 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400438 }
439
440 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500441 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400442 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400443
David Benjamina08e49d2014-08-24 01:46:07 -0400444 ssl_ctx->tlsext_channel_id_enabled_new = 1;
David Benjaminc273d2c2015-02-09 12:59:46 -0500445 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400446
David Benjaminc273d2c2015-02-09 12:59:46 -0500447 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500448
David Benjamin87e4acd2015-04-02 19:57:35 -0400449 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
450
David Benjamin1d5c83e2014-07-22 19:20:02 -0400451 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400452}
453
David Benjamin40f101b2015-02-20 11:23:42 -0500454// RetryAsync is called after a failed operation on |ssl| with return code
455// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400456// event and returns true. Otherwise it returns false.
457static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400458 // No error; don't retry.
459 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500460 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400461 }
David Benjamin83f90402015-01-27 01:09:43 -0500462
David Benjamin6c2563e2015-04-03 03:47:47 -0400463 TestState *test_state = GetTestState(ssl);
464 if (test_state->clock_delta.tv_usec != 0 ||
465 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500466 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400467 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
468 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
469 test_state->clock.tv_usec %= 1000000;
470 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
471 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500472
473 if (DTLSv1_handle_timeout(ssl) < 0) {
474 printf("Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500475 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500476 }
David Benjamin40f101b2015-02-20 11:23:42 -0500477 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500478 }
479
David Benjamin43ec06f2014-08-05 02:28:57 -0400480 // See if we needed to read or write more. If so, allow one byte through on
481 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500482 switch (SSL_get_error(ssl, ret)) {
483 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400484 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500485 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500486 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400487 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500488 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500489 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
490 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
491 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500492 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500493 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400494 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500495 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500496 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500497 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400498 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500499 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500500 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400501 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500502 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500503 case SSL_ERROR_PENDING_CERTIFICATE:
504 // The handshake will resume without a second call to the early callback.
505 return InstallCertificate(ssl);
David Benjamind9e07012015-02-09 03:04:34 -0500506 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500507 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400508 }
David Benjamin025b3d32014-07-01 19:53:04 -0400509}
510
David Benjamin6c2563e2015-04-03 03:47:47 -0400511// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
512// the result value of the final |SSL_read| call.
513static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
514 const TestConfig *config = GetConfigPtr(ssl);
515 int ret;
516 do {
517 ret = SSL_read(ssl, out, max_out);
518 } while (config->async && RetryAsync(ssl, ret));
519 return ret;
520}
521
522// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
523// operations. It returns the result of the final |SSL_write| call.
524static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
525 const TestConfig *config = GetConfigPtr(ssl);
526 int ret;
527 do {
528 ret = SSL_write(ssl, in, in_len);
529 if (ret > 0) {
530 in += ret;
531 in_len -= ret;
532 }
533 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
534 return ret;
535}
536
David Benjamin87c8a642015-02-21 01:54:29 -0500537// DoExchange runs a test SSL exchange against the peer. On success, it returns
538// true and sets |*out_session| to the negotiated SSL session. If the test is a
539// resumption attempt, |is_resume| is true and |session| is the session from the
540// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500541static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
542 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500543 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500544 ScopedSSL ssl(SSL_new(ssl_ctx));
545 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500546 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400547 }
548
David Benjamina7f333d2015-02-09 02:37:18 -0500549 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -0500550 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500551 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800552 }
David Benjamin5a593af2014-08-11 19:51:50 -0400553
Adam Langley5f0efe02015-02-20 13:03:16 -0800554 if (config->fallback_scsv &&
555 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
556 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400557 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500558 if (!config->use_early_callback) {
559 if (config->async) {
560 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
561 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
562 } else if (!InstallCertificate(ssl.get())) {
563 return false;
564 }
David Benjamin5a593af2014-08-11 19:51:50 -0400565 }
566 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500567 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500568 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400569 }
570 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500571 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400572 }
573 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500574 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400575 }
576 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500577 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400578 }
579 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500580 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400581 }
582 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500583 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400584 }
585 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500586 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400587 }
588 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500589 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400590 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400591 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500592 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400593 }
David Benjaminca6554b2014-11-08 12:31:52 -0500594 if (config->allow_unsafe_legacy_renegotiation) {
David Benjamina7f333d2015-02-09 02:37:18 -0500595 SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
David Benjaminca6554b2014-11-08 12:31:52 -0500596 }
David Benjamina08e49d2014-08-24 01:46:07 -0400597 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500598 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400599 }
600 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500601 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500602 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500603 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500604 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
605 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500606 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500607 }
David Benjamina08e49d2014-08-24 01:46:07 -0400608 }
David Benjamina08e49d2014-08-24 01:46:07 -0400609 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500610 if (!config->host_name.empty() &&
611 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500612 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400613 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500614 if (!config->advertise_alpn.empty() &&
615 SSL_set_alpn_protos(ssl.get(),
616 (const uint8_t *)config->advertise_alpn.data(),
617 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500618 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400619 }
David Benjamin48cae082014-10-27 01:06:24 -0400620 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500621 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
622 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400623 }
David Benjamin61f95272014-11-25 01:55:35 -0500624 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500625 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500626 return false;
David Benjamin48cae082014-10-27 01:06:24 -0400627 }
David Benjamin61f95272014-11-25 01:55:35 -0500628 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500629 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500630 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500631 }
632 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -0500633 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500634 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500635 }
636 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -0500637 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500638 return false;
David Benjaminca6c8262014-11-15 19:06:08 -0500639 }
David Benjamina7f333d2015-02-09 02:37:18 -0500640 SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500641 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500642 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500643 }
644 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500645 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500646 }
David Benjamin13be1de2015-01-11 16:29:36 -0500647 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500648 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
649 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -0500650 }
Adam Langley524e7172015-02-20 16:04:00 -0800651 if (config->install_ddos_callback) {
652 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
653 }
David Benjamin67d1fb52015-03-16 15:16:23 -0400654 if (!config->cipher.empty() &&
655 !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
656 return false;
657 }
David Benjamin025b3d32014-07-01 19:53:04 -0400658
David Benjamin87c8a642015-02-21 01:54:29 -0500659 int sock = Connect(config->port);
660 if (sock == -1) {
661 return false;
662 }
663 SocketCloser closer(sock);
664
665 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -0500666 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -0500667 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -0400668 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400669 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400670 ScopedBIO packeted =
671 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -0500672 BIO_push(packeted.get(), bio.release());
673 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -0400674 }
David Benjamin5a593af2014-08-11 19:51:50 -0400675 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -0500676 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -0500677 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -0500678 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -0400679 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -0500680 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -0400681 }
David Benjamina7f333d2015-02-09 02:37:18 -0500682 SSL_set_bio(ssl.get(), bio.get(), bio.get());
683 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -0400684
David Benjamin1d5c83e2014-07-22 19:20:02 -0400685 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -0500686 if (!config->is_server) {
687 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500688 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -0500689 }
690 } else if (config->async) {
691 // The internal session cache is disabled, so install the session
692 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -0500693 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -0500694 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400695 }
696 }
697
698 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500699 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -0400700 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500701 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400702 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500703 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400704 }
David Benjamine0e7d0d2015-02-08 19:33:25 -0500705 } else {
706 do {
707 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500708 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500709 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500710 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500711 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400712 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamine0e7d0d2015-02-08 19:33:25 -0500713 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500714 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500715 }
David Benjamin025b3d32014-07-01 19:53:04 -0400716
David Benjamine0e7d0d2015-02-08 19:33:25 -0500717 if (is_resume &&
David Benjamina7f333d2015-02-09 02:37:18 -0500718 (!!SSL_session_reused(ssl.get()) == config->expect_session_miss)) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500719 fprintf(stderr, "session was%s reused\n",
David Benjamina7f333d2015-02-09 02:37:18 -0500720 SSL_session_reused(ssl.get()) ? "" : " not");
David Benjamin40f101b2015-02-20 11:23:42 -0500721 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500722 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400723
David Benjamin87e4acd2015-04-02 19:57:35 -0400724 bool expect_handshake_done = is_resume || !config->false_start;
725 if (expect_handshake_done != GetTestState(ssl.get())->handshake_done) {
726 fprintf(stderr, "handshake was%s completed\n",
727 GetTestState(ssl.get())->handshake_done ? "" : " not");
728 return false;
729 }
730
David Benjamin6f5c0f42015-02-24 01:23:21 -0500731 if (config->is_server && !GetTestState(ssl.get())->early_callback_called) {
732 fprintf(stderr, "early callback not called\n");
733 return false;
734 }
735
David Benjamine0e7d0d2015-02-08 19:33:25 -0500736 if (!config->expected_server_name.empty()) {
737 const char *server_name =
David Benjamina7f333d2015-02-09 02:37:18 -0500738 SSL_get_servername(ssl.get(), TLSEXT_NAMETYPE_host_name);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500739 if (server_name != config->expected_server_name) {
740 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
741 server_name, config->expected_server_name.c_str());
David Benjamin40f101b2015-02-20 11:23:42 -0500742 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500743 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400744 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400745
David Benjamine0e7d0d2015-02-08 19:33:25 -0500746 if (!config->expected_certificate_types.empty()) {
747 uint8_t *certificate_types;
748 int num_certificate_types =
David Benjamina7f333d2015-02-09 02:37:18 -0500749 SSL_get0_certificate_types(ssl.get(), &certificate_types);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500750 if (num_certificate_types !=
751 (int)config->expected_certificate_types.size() ||
752 memcmp(certificate_types,
753 config->expected_certificate_types.data(),
754 num_certificate_types) != 0) {
755 fprintf(stderr, "certificate types mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500756 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500757 }
David Benjamin7b030512014-07-08 17:30:11 -0400758 }
David Benjamin7b030512014-07-08 17:30:11 -0400759
David Benjamine0e7d0d2015-02-08 19:33:25 -0500760 if (!config->expected_next_proto.empty()) {
761 const uint8_t *next_proto;
762 unsigned next_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500763 SSL_get0_next_proto_negotiated(ssl.get(), &next_proto, &next_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500764 if (next_proto_len != config->expected_next_proto.size() ||
765 memcmp(next_proto, config->expected_next_proto.data(),
766 next_proto_len) != 0) {
767 fprintf(stderr, "negotiated next proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500768 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500769 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400770 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400771
David Benjamine0e7d0d2015-02-08 19:33:25 -0500772 if (!config->expected_alpn.empty()) {
773 const uint8_t *alpn_proto;
774 unsigned alpn_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500775 SSL_get0_alpn_selected(ssl.get(), &alpn_proto, &alpn_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500776 if (alpn_proto_len != config->expected_alpn.size() ||
777 memcmp(alpn_proto, config->expected_alpn.data(),
778 alpn_proto_len) != 0) {
779 fprintf(stderr, "negotiated alpn proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500780 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500781 }
David Benjaminae2888f2014-09-06 12:58:58 -0400782 }
David Benjaminae2888f2014-09-06 12:58:58 -0400783
David Benjamine0e7d0d2015-02-08 19:33:25 -0500784 if (!config->expected_channel_id.empty()) {
785 uint8_t channel_id[64];
David Benjamina7f333d2015-02-09 02:37:18 -0500786 if (!SSL_get_tls_channel_id(ssl.get(), channel_id, sizeof(channel_id))) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500787 fprintf(stderr, "no channel id negotiated\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500788 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500789 }
790 if (config->expected_channel_id.size() != 64 ||
791 memcmp(config->expected_channel_id.data(),
792 channel_id, 64) != 0) {
793 fprintf(stderr, "channel id mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500794 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500795 }
David Benjamina08e49d2014-08-24 01:46:07 -0400796 }
David Benjamina08e49d2014-08-24 01:46:07 -0400797
David Benjamine0e7d0d2015-02-08 19:33:25 -0500798 if (config->expect_extended_master_secret) {
799 if (!ssl->session->extended_master_secret) {
800 fprintf(stderr, "No EMS for session when expected");
David Benjamin40f101b2015-02-20 11:23:42 -0500801 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500802 }
Adam Langley75712922014-10-10 16:23:43 -0700803 }
Adam Langley75712922014-10-10 16:23:43 -0700804
David Benjamine0e7d0d2015-02-08 19:33:25 -0500805 if (!config->expected_ocsp_response.empty()) {
806 const uint8_t *data;
807 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500808 SSL_get0_ocsp_response(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500809 if (config->expected_ocsp_response.size() != len ||
810 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
811 fprintf(stderr, "OCSP response mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500812 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500813 }
David Benjamin61f95272014-11-25 01:55:35 -0500814 }
David Benjamin61f95272014-11-25 01:55:35 -0500815
David Benjamine0e7d0d2015-02-08 19:33:25 -0500816 if (!config->expected_signed_cert_timestamps.empty()) {
817 const uint8_t *data;
818 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500819 SSL_get0_signed_cert_timestamp_list(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500820 if (config->expected_signed_cert_timestamps.size() != len ||
821 memcmp(config->expected_signed_cert_timestamps.data(),
822 data, len) != 0) {
823 fprintf(stderr, "SCT list mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500824 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500825 }
David Benjamin61f95272014-11-25 01:55:35 -0500826 }
827 }
828
Adam Langley2ae77d22014-10-28 17:29:33 -0700829 if (config->renegotiate) {
830 if (config->async) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500831 fprintf(stderr, "-renegotiate is not supported with -async.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500832 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500833 }
834 if (config->implicit_handshake) {
835 fprintf(stderr, "-renegotiate is not supported with -implicit-handshake.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500836 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700837 }
838
David Benjamina7f333d2015-02-09 02:37:18 -0500839 SSL_renegotiate(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700840
David Benjamina7f333d2015-02-09 02:37:18 -0500841 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700842 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500843 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700844 }
845
David Benjamina7f333d2015-02-09 02:37:18 -0500846 SSL_set_state(ssl.get(), SSL_ST_ACCEPT);
847 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700848 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500849 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700850 }
851 }
852
David Benjamin5a593af2014-08-11 19:51:50 -0400853 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400854 if (config->is_dtls) {
855 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500856 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -0400857 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700858 // This mode writes a number of different record sizes in an attempt to
859 // trip up the CBC record splitting code.
860 uint8_t buf[32769];
861 memset(buf, 0x42, sizeof(buf));
862 static const size_t kRecordSizes[] = {
863 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
864 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
865 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700866 const size_t len = kRecordSizes[i];
Kenny Root7fdeaf12014-08-05 15:23:37 -0700867 if (len > sizeof(buf)) {
868 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500869 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700870 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400871 if (WriteAll(ssl.get(), buf, len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500872 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400873 }
874 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700875 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400876 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400877 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
878 5) < 0) {
879 return false;
880 }
David Benjamine58c4f52014-08-24 03:47:07 -0400881 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700882 for (;;) {
883 uint8_t buf[512];
David Benjamin6c2563e2015-04-03 03:47:47 -0400884 int n = DoRead(ssl.get(), buf, sizeof(buf));
David Benjamina7f333d2015-02-09 02:37:18 -0500885 int err = SSL_get_error(ssl.get(), n);
David Benjamin9a38e922015-01-22 16:06:11 -0500886 if (err == SSL_ERROR_ZERO_RETURN ||
887 (n == 0 && err == SSL_ERROR_SYSCALL)) {
888 if (n != 0) {
889 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500890 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500891 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500892 // Accept shutdowns with or without close_notify.
893 // TODO(davidben): Write tests which distinguish these two cases.
David Benjamin9a38e922015-01-22 16:06:11 -0500894 break;
895 } else if (err != SSL_ERROR_NONE) {
896 if (n > 0) {
897 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500898 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500899 }
David Benjamin40f101b2015-02-20 11:23:42 -0500900 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500901 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500902 // Successfully read data.
David Benjamin9a38e922015-01-22 16:06:11 -0500903 if (n <= 0) {
904 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500905 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500906 }
David Benjamin87e4acd2015-04-02 19:57:35 -0400907
908 // After a successful read, with or without False Start, the handshake
909 // must be complete.
910 if (!GetTestState(ssl.get())->handshake_done) {
911 fprintf(stderr, "handshake was not completed after SSL_read\n");
912 return false;
913 }
914
David Benjamin9a38e922015-01-22 16:06:11 -0500915 for (int i = 0; i < n; i++) {
916 buf[i] ^= 0xff;
917 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400918 if (WriteAll(ssl.get(), buf, n) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500919 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700920 }
921 }
David Benjamin025b3d32014-07-01 19:53:04 -0400922 }
923
David Benjamin1d5c83e2014-07-22 19:20:02 -0400924 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500925 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400926 }
927
David Benjamina7f333d2015-02-09 02:37:18 -0500928 SSL_shutdown(ssl.get());
David Benjamin40f101b2015-02-20 11:23:42 -0500929 return true;
David Benjamin025b3d32014-07-01 19:53:04 -0400930}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400931
932int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -0500933#if defined(OPENSSL_WINDOWS)
934 /* Initialize Winsock. */
935 WORD wsa_version = MAKEWORD(2, 2);
936 WSADATA wsa_data;
937 int wsa_err = WSAStartup(wsa_version, &wsa_data);
938 if (wsa_err != 0) {
939 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
940 return 1;
941 }
942 if (wsa_data.wVersion != wsa_version) {
943 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
944 return 1;
945 }
946#else
David Benjamin1d5c83e2014-07-22 19:20:02 -0400947 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700948#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400949
David Benjamin5a593af2014-08-11 19:51:50 -0400950 if (!SSL_library_init()) {
951 return 1;
952 }
David Benjamind9e07012015-02-09 03:04:34 -0500953 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -0500954 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -0400955 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -0500956 return 1;
957 }
David Benjamin5a593af2014-08-11 19:51:50 -0400958
959 TestConfig config;
960 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500961 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400962 }
963
David Benjaminc273d2c2015-02-09 12:59:46 -0500964 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -0500965 if (!ssl_ctx) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400966 BIO_print_errors_fp(stdout);
967 return 1;
968 }
969
David Benjamina7f333d2015-02-09 02:37:18 -0500970 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -0500971 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -0500972 NULL /* session */)) {
David Benjamin40f101b2015-02-20 11:23:42 -0500973 BIO_print_errors_fp(stdout);
974 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400975 }
976
David Benjamin40f101b2015-02-20 11:23:42 -0500977 if (config.resume &&
978 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -0500979 session.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500980 BIO_print_errors_fp(stdout);
981 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400982 }
983
David Benjamina7f333d2015-02-09 02:37:18 -0500984 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400985}