blob: 3e712fc894fd1109f3c47cfad1897e821a8c6dee [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)
27#include <WinSock2.h>
28#include <WS2tcpip.h>
29#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 Benjamin43ec06f2014-08-05 02:28:57 -040042#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040043#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050044#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040045#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040046
David Benjamin87c8a642015-02-21 01:54:29 -050047
48#if !defined(OPENSSL_WINDOWS)
49static int closesocket(int sock) {
50 return close(sock);
51}
52
53static void PrintSocketError(const char *func) {
54 perror(func);
55}
56#else
57static void PrintSocketError(const char *func) {
58 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
59}
60#endif
61
David Benjaminc273d2c2015-02-09 12:59:46 -050062static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050063 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040064 return 1;
65}
David Benjamin025b3d32014-07-01 19:53:04 -040066
David Benjamin2d445c02015-02-09 13:03:50 -050067struct TestState {
68 TestState() : cert_ready(false), early_callback_called(false) {}
David Benjamin41fdbcd2015-02-09 03:13:35 -050069
David Benjamind9e07012015-02-09 03:04:34 -050070 ScopedEVP_PKEY channel_id;
David Benjamin41fdbcd2015-02-09 03:13:35 -050071 bool cert_ready;
David Benjamin1b8b6912015-02-09 04:28:16 -050072 ScopedSSL_SESSION session;
73 ScopedSSL_SESSION pending_session;
David Benjamin2d445c02015-02-09 13:03:50 -050074 bool early_callback_called;
David Benjamind9e07012015-02-09 03:04:34 -050075};
76
David Benjamin2d445c02015-02-09 13:03:50 -050077static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
78 int index, long argl, void *argp) {
79 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -050080}
81
82static int g_config_index = 0;
83static int g_clock_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -050084static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -040085
Adam Langley69a01602014-11-17 17:26:55 -080086static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -050087 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -040088}
89
90static const TestConfig *GetConfigPtr(SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -050091 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -040092}
93
David Benjamin83f90402015-01-27 01:09:43 -050094static bool SetClockPtr(SSL *ssl, OPENSSL_timeval *clock) {
David Benjamind9e07012015-02-09 03:04:34 -050095 return SSL_set_ex_data(ssl, g_clock_index, (void *)clock) == 1;
David Benjamin83f90402015-01-27 01:09:43 -050096}
97
98static OPENSSL_timeval *GetClockPtr(SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -050099 return (OPENSSL_timeval *)SSL_get_ex_data(ssl, g_clock_index);
100}
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 Benjamin2d445c02015-02-09 13:03:50 -0500110static TestState *GetTestState(SSL *ssl) {
111 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,
207 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 Benjamin83f90402015-01-27 01:09:43 -0500281 *out_clock = *GetClockPtr(ssl);
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 Benjamin87c8a642015-02-21 01:54:29 -0500323// Connect returns a new socket connected to localhost on |port| or -1 on
324// error.
325static int Connect(uint16_t port) {
326 int sock = socket(AF_INET, SOCK_STREAM, 0);
327 if (sock == -1) {
328 PrintSocketError("socket");
329 return -1;
330 }
331 int nodelay = 1;
332 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
333 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
334 PrintSocketError("setsockopt");
335 closesocket(sock);
336 return -1;
337 }
338 sockaddr_in sin;
339 memset(&sin, 0, sizeof(sin));
340 sin.sin_family = AF_INET;
341 sin.sin_port = htons(port);
342 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
343 PrintSocketError("inet_pton");
344 closesocket(sock);
345 return -1;
346 }
347 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
348 sizeof(sin)) != 0) {
349 PrintSocketError("connect");
350 closesocket(sock);
351 return -1;
352 }
353 return sock;
354}
355
356class SocketCloser {
357 public:
358 explicit SocketCloser(int sock) : sock_(sock) {}
359 ~SocketCloser() {
360 // Half-close and drain the socket before releasing it. This seems to be
361 // necessary for graceful shutdown on Windows. It will also avoid write
362 // failures in the test runner.
363#if defined(OPENSSL_WINDOWS)
364 shutdown(sock_, SD_SEND);
365#else
366 shutdown(sock_, SHUT_WR);
367#endif
368 while (true) {
369 char buf[1024];
370 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
371 break;
372 }
373 }
374 closesocket(sock_);
375 }
376
377 private:
378 const int sock_;
379};
380
David Benjaminc273d2c2015-02-09 12:59:46 -0500381static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500382 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
383 config->is_dtls ? DTLS_method() : TLS_method()));
384 if (!ssl_ctx) {
385 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400386 }
387
David Benjamin6fd297b2014-08-11 18:43:38 -0400388 if (config->is_dtls) {
389 // DTLS needs read-ahead to function on a datagram BIO.
390 //
391 // TODO(davidben): this should not be necessary. DTLS code should only
392 // expect a datagram BIO.
David Benjamina7f333d2015-02-09 02:37:18 -0500393 SSL_CTX_set_read_ahead(ssl_ctx.get(), 1);
David Benjamin6fd297b2014-08-11 18:43:38 -0400394 }
395
David Benjamina7f333d2015-02-09 02:37:18 -0500396 if (!SSL_CTX_set_ecdh_auto(ssl_ctx.get(), 1)) {
397 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400398 }
399
David Benjamina7f333d2015-02-09 02:37:18 -0500400 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
401 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400402 }
403
David Benjamina7f333d2015-02-09 02:37:18 -0500404 ScopedDH dh(DH_get_2048_256(NULL));
405 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
406 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400407 }
408
David Benjamin1b8b6912015-02-09 04:28:16 -0500409 if (config->async && config->is_server) {
410 // Disable the internal session cache. To test asynchronous session lookup,
411 // we use an external session cache.
412 SSL_CTX_set_session_cache_mode(
413 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500414 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500415 } else {
416 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
417 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400418
David Benjaminc273d2c2015-02-09 12:59:46 -0500419 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400420
David Benjamin1f5f62b2014-07-12 16:18:02 -0400421 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500422 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400423 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500424 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500425 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400426 }
427
428 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500429 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400430 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400431
David Benjamina08e49d2014-08-24 01:46:07 -0400432 ssl_ctx->tlsext_channel_id_enabled_new = 1;
David Benjaminc273d2c2015-02-09 12:59:46 -0500433 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400434
David Benjaminc273d2c2015-02-09 12:59:46 -0500435 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500436
David Benjamin1d5c83e2014-07-22 19:20:02 -0400437 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400438}
439
David Benjamin40f101b2015-02-20 11:23:42 -0500440// RetryAsync is called after a failed operation on |ssl| with return code
441// |ret|. If the operation should be retried, it simulates one asynchronous
442// event and returns true. Otherwise it returns false. |async| and |clock_delta|
443// are the AsyncBio and simulated timeout for |ssl|, respectively.
444static bool RetryAsync(SSL *ssl, int ret, BIO *async,
445 OPENSSL_timeval *clock_delta) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400446 // No error; don't retry.
447 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500448 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400449 }
David Benjamin83f90402015-01-27 01:09:43 -0500450
451 if (clock_delta->tv_usec != 0 || clock_delta->tv_sec != 0) {
452 // Process the timeout and retry.
453 OPENSSL_timeval *clock = GetClockPtr(ssl);
454 clock->tv_usec += clock_delta->tv_usec;
455 clock->tv_sec += clock->tv_usec / 1000000;
456 clock->tv_usec %= 1000000;
457 clock->tv_sec += clock_delta->tv_sec;
458 memset(clock_delta, 0, sizeof(*clock_delta));
459
460 if (DTLSv1_handle_timeout(ssl) < 0) {
461 printf("Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500462 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500463 }
David Benjamin40f101b2015-02-20 11:23:42 -0500464 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500465 }
466
David Benjamin43ec06f2014-08-05 02:28:57 -0400467 // See if we needed to read or write more. If so, allow one byte through on
468 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500469 switch (SSL_get_error(ssl, ret)) {
470 case SSL_ERROR_WANT_READ:
David Benjaminc273d2c2015-02-09 12:59:46 -0500471 AsyncBioAllowRead(async, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500472 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500473 case SSL_ERROR_WANT_WRITE:
David Benjaminc273d2c2015-02-09 12:59:46 -0500474 AsyncBioAllowWrite(async, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500475 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500476 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
477 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
478 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500479 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500480 }
481 GetTestState(ssl)->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500482 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500483 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500484 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin2d445c02015-02-09 13:03:50 -0500485 GetTestState(ssl)->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500486 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500487 case SSL_ERROR_PENDING_SESSION:
David Benjamin2d445c02015-02-09 13:03:50 -0500488 GetTestState(ssl)->session =
489 std::move(GetTestState(ssl)->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500490 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500491 case SSL_ERROR_PENDING_CERTIFICATE:
492 // The handshake will resume without a second call to the early callback.
493 return InstallCertificate(ssl);
David Benjamind9e07012015-02-09 03:04:34 -0500494 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500495 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400496 }
David Benjamin025b3d32014-07-01 19:53:04 -0400497}
498
David Benjamin87c8a642015-02-21 01:54:29 -0500499// DoExchange runs a test SSL exchange against the peer. On success, it returns
500// true and sets |*out_session| to the negotiated SSL session. If the test is a
501// resumption attempt, |is_resume| is true and |session| is the session from the
502// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500503static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
504 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500505 SSL_SESSION *session) {
David Benjamin83f90402015-01-27 01:09:43 -0500506 OPENSSL_timeval clock = {0}, clock_delta = {0};
David Benjamina7f333d2015-02-09 02:37:18 -0500507 ScopedSSL ssl(SSL_new(ssl_ctx));
508 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500509 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400510 }
511
David Benjamina7f333d2015-02-09 02:37:18 -0500512 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamind9e07012015-02-09 03:04:34 -0500513 !SetClockPtr(ssl.get(), &clock) |
David Benjamin2d445c02015-02-09 13:03:50 -0500514 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500515 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800516 }
David Benjamin5a593af2014-08-11 19:51:50 -0400517
Adam Langley5f0efe02015-02-20 13:03:16 -0800518 if (config->fallback_scsv &&
519 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
520 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400521 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500522 if (!config->use_early_callback) {
523 if (config->async) {
524 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
525 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
526 } else if (!InstallCertificate(ssl.get())) {
527 return false;
528 }
David Benjamin5a593af2014-08-11 19:51:50 -0400529 }
530 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500531 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500532 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400533 }
534 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500535 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400536 }
537 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500538 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400539 }
540 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500541 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400542 }
543 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500544 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400545 }
546 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500547 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400548 }
549 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500550 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400551 }
552 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500553 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400554 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400555 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500556 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400557 }
David Benjaminca6554b2014-11-08 12:31:52 -0500558 if (config->allow_unsafe_legacy_renegotiation) {
David Benjamina7f333d2015-02-09 02:37:18 -0500559 SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
David Benjaminca6554b2014-11-08 12:31:52 -0500560 }
David Benjamina08e49d2014-08-24 01:46:07 -0400561 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500562 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400563 }
564 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500565 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500566 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500567 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500568 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
569 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500570 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500571 }
David Benjamina08e49d2014-08-24 01:46:07 -0400572 }
David Benjamina08e49d2014-08-24 01:46:07 -0400573 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500574 if (!config->host_name.empty() &&
575 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500576 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400577 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500578 if (!config->advertise_alpn.empty() &&
579 SSL_set_alpn_protos(ssl.get(),
580 (const uint8_t *)config->advertise_alpn.data(),
581 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500582 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400583 }
David Benjamin48cae082014-10-27 01:06:24 -0400584 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500585 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
586 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400587 }
David Benjamin61f95272014-11-25 01:55:35 -0500588 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500589 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500590 return false;
David Benjamin48cae082014-10-27 01:06:24 -0400591 }
David Benjamin61f95272014-11-25 01:55:35 -0500592 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500593 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500594 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500595 }
596 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -0500597 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500598 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500599 }
600 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -0500601 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500602 return false;
David Benjaminca6c8262014-11-15 19:06:08 -0500603 }
David Benjamina7f333d2015-02-09 02:37:18 -0500604 SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500605 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500606 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500607 }
608 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500609 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500610 }
David Benjamin13be1de2015-01-11 16:29:36 -0500611 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500612 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
613 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -0500614 }
Adam Langley524e7172015-02-20 16:04:00 -0800615 if (config->install_ddos_callback) {
616 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
617 }
David Benjamin025b3d32014-07-01 19:53:04 -0400618
David Benjamin87c8a642015-02-21 01:54:29 -0500619 int sock = Connect(config->port);
620 if (sock == -1) {
621 return false;
622 }
623 SocketCloser closer(sock);
624
625 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -0500626 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -0500627 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -0400628 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400629 if (config->is_dtls) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500630 ScopedBIO packeted = PacketedBioCreate(&clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -0500631 BIO_push(packeted.get(), bio.release());
632 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -0400633 }
David Benjamina7f333d2015-02-09 02:37:18 -0500634 BIO *async = NULL;
David Benjamin5a593af2014-08-11 19:51:50 -0400635 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -0500636 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -0500637 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -0500638 BIO_push(async_scoped.get(), bio.release());
639 async = async_scoped.get();
640 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -0400641 }
David Benjamina7f333d2015-02-09 02:37:18 -0500642 SSL_set_bio(ssl.get(), bio.get(), bio.get());
643 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -0400644
David Benjamin1d5c83e2014-07-22 19:20:02 -0400645 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -0500646 if (!config->is_server) {
647 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500648 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -0500649 }
650 } else if (config->async) {
651 // The internal session cache is disabled, so install the session
652 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -0500653 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -0500654 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400655 }
656 }
657
658 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500659 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -0400660 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500661 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400662 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500663 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400664 }
David Benjamine0e7d0d2015-02-08 19:33:25 -0500665 } else {
666 do {
667 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500668 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500669 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500670 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500671 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500672 } while (config->async && RetryAsync(ssl.get(), ret, async, &clock_delta));
David Benjamine0e7d0d2015-02-08 19:33:25 -0500673 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500674 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500675 }
David Benjamin025b3d32014-07-01 19:53:04 -0400676
David Benjamine0e7d0d2015-02-08 19:33:25 -0500677 if (is_resume &&
David Benjamina7f333d2015-02-09 02:37:18 -0500678 (!!SSL_session_reused(ssl.get()) == config->expect_session_miss)) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500679 fprintf(stderr, "session was%s reused\n",
David Benjamina7f333d2015-02-09 02:37:18 -0500680 SSL_session_reused(ssl.get()) ? "" : " not");
David Benjamin40f101b2015-02-20 11:23:42 -0500681 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500682 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400683
David Benjamin6f5c0f42015-02-24 01:23:21 -0500684 if (config->is_server && !GetTestState(ssl.get())->early_callback_called) {
685 fprintf(stderr, "early callback not called\n");
686 return false;
687 }
688
David Benjamine0e7d0d2015-02-08 19:33:25 -0500689 if (!config->expected_server_name.empty()) {
690 const char *server_name =
David Benjamina7f333d2015-02-09 02:37:18 -0500691 SSL_get_servername(ssl.get(), TLSEXT_NAMETYPE_host_name);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500692 if (server_name != config->expected_server_name) {
693 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
694 server_name, config->expected_server_name.c_str());
David Benjamin40f101b2015-02-20 11:23:42 -0500695 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500696 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400697 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400698
David Benjamine0e7d0d2015-02-08 19:33:25 -0500699 if (!config->expected_certificate_types.empty()) {
700 uint8_t *certificate_types;
701 int num_certificate_types =
David Benjamina7f333d2015-02-09 02:37:18 -0500702 SSL_get0_certificate_types(ssl.get(), &certificate_types);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500703 if (num_certificate_types !=
704 (int)config->expected_certificate_types.size() ||
705 memcmp(certificate_types,
706 config->expected_certificate_types.data(),
707 num_certificate_types) != 0) {
708 fprintf(stderr, "certificate types mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500709 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500710 }
David Benjamin7b030512014-07-08 17:30:11 -0400711 }
David Benjamin7b030512014-07-08 17:30:11 -0400712
David Benjamine0e7d0d2015-02-08 19:33:25 -0500713 if (!config->expected_next_proto.empty()) {
714 const uint8_t *next_proto;
715 unsigned next_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500716 SSL_get0_next_proto_negotiated(ssl.get(), &next_proto, &next_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500717 if (next_proto_len != config->expected_next_proto.size() ||
718 memcmp(next_proto, config->expected_next_proto.data(),
719 next_proto_len) != 0) {
720 fprintf(stderr, "negotiated next proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500721 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500722 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400723 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400724
David Benjamine0e7d0d2015-02-08 19:33:25 -0500725 if (!config->expected_alpn.empty()) {
726 const uint8_t *alpn_proto;
727 unsigned alpn_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500728 SSL_get0_alpn_selected(ssl.get(), &alpn_proto, &alpn_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500729 if (alpn_proto_len != config->expected_alpn.size() ||
730 memcmp(alpn_proto, config->expected_alpn.data(),
731 alpn_proto_len) != 0) {
732 fprintf(stderr, "negotiated alpn proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500733 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500734 }
David Benjaminae2888f2014-09-06 12:58:58 -0400735 }
David Benjaminae2888f2014-09-06 12:58:58 -0400736
David Benjamine0e7d0d2015-02-08 19:33:25 -0500737 if (!config->expected_channel_id.empty()) {
738 uint8_t channel_id[64];
David Benjamina7f333d2015-02-09 02:37:18 -0500739 if (!SSL_get_tls_channel_id(ssl.get(), channel_id, sizeof(channel_id))) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500740 fprintf(stderr, "no channel id negotiated\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500741 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500742 }
743 if (config->expected_channel_id.size() != 64 ||
744 memcmp(config->expected_channel_id.data(),
745 channel_id, 64) != 0) {
746 fprintf(stderr, "channel id mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500747 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500748 }
David Benjamina08e49d2014-08-24 01:46:07 -0400749 }
David Benjamina08e49d2014-08-24 01:46:07 -0400750
David Benjamine0e7d0d2015-02-08 19:33:25 -0500751 if (config->expect_extended_master_secret) {
752 if (!ssl->session->extended_master_secret) {
753 fprintf(stderr, "No EMS for session when expected");
David Benjamin40f101b2015-02-20 11:23:42 -0500754 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500755 }
Adam Langley75712922014-10-10 16:23:43 -0700756 }
Adam Langley75712922014-10-10 16:23:43 -0700757
David Benjamine0e7d0d2015-02-08 19:33:25 -0500758 if (!config->expected_ocsp_response.empty()) {
759 const uint8_t *data;
760 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500761 SSL_get0_ocsp_response(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500762 if (config->expected_ocsp_response.size() != len ||
763 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
764 fprintf(stderr, "OCSP response mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500765 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500766 }
David Benjamin61f95272014-11-25 01:55:35 -0500767 }
David Benjamin61f95272014-11-25 01:55:35 -0500768
David Benjamine0e7d0d2015-02-08 19:33:25 -0500769 if (!config->expected_signed_cert_timestamps.empty()) {
770 const uint8_t *data;
771 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500772 SSL_get0_signed_cert_timestamp_list(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500773 if (config->expected_signed_cert_timestamps.size() != len ||
774 memcmp(config->expected_signed_cert_timestamps.data(),
775 data, len) != 0) {
776 fprintf(stderr, "SCT list mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500777 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500778 }
David Benjamin61f95272014-11-25 01:55:35 -0500779 }
780 }
781
Adam Langley2ae77d22014-10-28 17:29:33 -0700782 if (config->renegotiate) {
783 if (config->async) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500784 fprintf(stderr, "-renegotiate is not supported with -async.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500785 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500786 }
787 if (config->implicit_handshake) {
788 fprintf(stderr, "-renegotiate is not supported with -implicit-handshake.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500789 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700790 }
791
David Benjamina7f333d2015-02-09 02:37:18 -0500792 SSL_renegotiate(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700793
David Benjamina7f333d2015-02-09 02:37:18 -0500794 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700795 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500796 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700797 }
798
David Benjamina7f333d2015-02-09 02:37:18 -0500799 SSL_set_state(ssl.get(), SSL_ST_ACCEPT);
800 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700801 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500802 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700803 }
804 }
805
David Benjamin5a593af2014-08-11 19:51:50 -0400806 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400807 if (config->is_dtls) {
808 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500809 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -0400810 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700811 // This mode writes a number of different record sizes in an attempt to
812 // trip up the CBC record splitting code.
813 uint8_t buf[32769];
814 memset(buf, 0x42, sizeof(buf));
815 static const size_t kRecordSizes[] = {
816 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
817 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
818 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400819 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700820 const size_t len = kRecordSizes[i];
821 size_t off = 0;
822
823 if (len > sizeof(buf)) {
824 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500825 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700826 }
827
David Benjamin43ec06f2014-08-05 02:28:57 -0400828 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500829 w = SSL_write(ssl.get(), buf + off, len - off);
Kenny Root7fdeaf12014-08-05 15:23:37 -0700830 if (w > 0) {
831 off += (size_t) w;
832 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500833 } while ((config->async && RetryAsync(ssl.get(), w, async, &clock_delta)) ||
David Benjamin5a593af2014-08-11 19:51:50 -0400834 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700835
836 if (w < 0 || off != len) {
David Benjamin40f101b2015-02-20 11:23:42 -0500837 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400838 }
839 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700840 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400841 if (config->shim_writes_first) {
842 int w;
843 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500844 w = SSL_write(ssl.get(), "hello", 5);
David Benjaminc273d2c2015-02-09 12:59:46 -0500845 } while (config->async && RetryAsync(ssl.get(), w, async, &clock_delta));
David Benjamine58c4f52014-08-24 03:47:07 -0400846 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700847 for (;;) {
848 uint8_t buf[512];
849 int n;
850 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500851 n = SSL_read(ssl.get(), buf, sizeof(buf));
David Benjaminc273d2c2015-02-09 12:59:46 -0500852 } while (config->async && RetryAsync(ssl.get(), n, async, &clock_delta));
David Benjamina7f333d2015-02-09 02:37:18 -0500853 int err = SSL_get_error(ssl.get(), n);
David Benjamin9a38e922015-01-22 16:06:11 -0500854 if (err == SSL_ERROR_ZERO_RETURN ||
855 (n == 0 && err == SSL_ERROR_SYSCALL)) {
856 if (n != 0) {
857 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500858 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500859 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500860 // Accept shutdowns with or without close_notify.
861 // TODO(davidben): Write tests which distinguish these two cases.
David Benjamin9a38e922015-01-22 16:06:11 -0500862 break;
863 } else if (err != SSL_ERROR_NONE) {
864 if (n > 0) {
865 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500866 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500867 }
David Benjamin40f101b2015-02-20 11:23:42 -0500868 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500869 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500870 // Successfully read data.
David Benjamin9a38e922015-01-22 16:06:11 -0500871 if (n <= 0) {
872 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500873 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500874 }
875 for (int i = 0; i < n; i++) {
876 buf[i] ^= 0xff;
877 }
878 int w;
879 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500880 w = SSL_write(ssl.get(), buf, n);
David Benjaminc273d2c2015-02-09 12:59:46 -0500881 } while (config->async && RetryAsync(ssl.get(), w, async, &clock_delta));
David Benjamin9a38e922015-01-22 16:06:11 -0500882 if (w != n) {
David Benjamin40f101b2015-02-20 11:23:42 -0500883 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700884 }
885 }
David Benjamin025b3d32014-07-01 19:53:04 -0400886 }
887
David Benjamin1d5c83e2014-07-22 19:20:02 -0400888 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500889 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400890 }
891
David Benjamina7f333d2015-02-09 02:37:18 -0500892 SSL_shutdown(ssl.get());
David Benjamin40f101b2015-02-20 11:23:42 -0500893 return true;
David Benjamin025b3d32014-07-01 19:53:04 -0400894}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400895
896int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -0500897#if defined(OPENSSL_WINDOWS)
898 /* Initialize Winsock. */
899 WORD wsa_version = MAKEWORD(2, 2);
900 WSADATA wsa_data;
901 int wsa_err = WSAStartup(wsa_version, &wsa_data);
902 if (wsa_err != 0) {
903 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
904 return 1;
905 }
906 if (wsa_data.wVersion != wsa_version) {
907 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
908 return 1;
909 }
910#else
David Benjamin1d5c83e2014-07-22 19:20:02 -0400911 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700912#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400913
David Benjamin5a593af2014-08-11 19:51:50 -0400914 if (!SSL_library_init()) {
915 return 1;
916 }
David Benjamind9e07012015-02-09 03:04:34 -0500917 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
918 g_clock_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -0500919 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
920 if (g_config_index < 0 || g_clock_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -0500921 return 1;
922 }
David Benjamin5a593af2014-08-11 19:51:50 -0400923
924 TestConfig config;
925 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500926 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400927 }
928
David Benjaminc273d2c2015-02-09 12:59:46 -0500929 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -0500930 if (!ssl_ctx) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400931 BIO_print_errors_fp(stdout);
932 return 1;
933 }
934
David Benjamina7f333d2015-02-09 02:37:18 -0500935 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -0500936 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -0500937 NULL /* session */)) {
David Benjamin40f101b2015-02-20 11:23:42 -0500938 BIO_print_errors_fp(stdout);
939 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400940 }
941
David Benjamin40f101b2015-02-20 11:23:42 -0500942 if (config.resume &&
943 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -0500944 session.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500945 BIO_print_errors_fp(stdout);
946 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400947 }
948
David Benjamina7f333d2015-02-09 02:37:18 -0500949 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400950}