blob: b22819e0ff2f61ef9094406837c1a3b60cdd632e [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 Benjamind9e07012015-02-09 03:04:34 -050071 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040072 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050073 ScopedSSL_SESSION session;
74 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040075 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040076 bool handshake_done = false;
David Benjamind9e07012015-02-09 03:04:34 -050077};
78
David Benjamin2d445c02015-02-09 13:03:50 -050079static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
80 int index, long argl, void *argp) {
81 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -050082}
83
84static int g_config_index = 0;
85static int g_clock_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -050086static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -040087
Adam Langley69a01602014-11-17 17:26:55 -080088static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -050089 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -040090}
91
David Benjamin87e4acd2015-04-02 19:57:35 -040092static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -050093 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -040094}
95
David Benjamin83f90402015-01-27 01:09:43 -050096static bool SetClockPtr(SSL *ssl, OPENSSL_timeval *clock) {
David Benjamind9e07012015-02-09 03:04:34 -050097 return SSL_set_ex_data(ssl, g_clock_index, (void *)clock) == 1;
David Benjamin83f90402015-01-27 01:09:43 -050098}
99
David Benjamin87e4acd2015-04-02 19:57:35 -0400100static OPENSSL_timeval *GetClockPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500101 return (OPENSSL_timeval *)SSL_get_ex_data(ssl, g_clock_index);
102}
103
David Benjamin2d445c02015-02-09 13:03:50 -0500104static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
105 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500106 async.release();
107 return true;
108 }
109 return false;
110}
111
David Benjamin87e4acd2015-04-02 19:57:35 -0400112static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500113 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500114}
115
David Benjamina7f333d2015-02-09 02:37:18 -0500116static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
117 ScopedBIO bio(BIO_new(BIO_s_file()));
118 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
119 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400120 }
David Benjamina7f333d2015-02-09 02:37:18 -0500121 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400122 return pkey;
123}
124
David Benjamin41fdbcd2015-02-09 03:13:35 -0500125static bool InstallCertificate(SSL *ssl) {
126 const TestConfig *config = GetConfigPtr(ssl);
127 if (!config->key_file.empty() &&
128 !SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
129 SSL_FILETYPE_PEM)) {
130 return false;
131 }
132 if (!config->cert_file.empty() &&
133 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
134 SSL_FILETYPE_PEM)) {
135 return false;
136 }
137 return true;
138}
139
David Benjaminc273d2c2015-02-09 12:59:46 -0500140static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400141 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500142 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400143
David Benjamin6f5c0f42015-02-24 01:23:21 -0500144 if (!config->expected_server_name.empty()) {
145 const uint8_t *extension_data;
146 size_t extension_len;
147 CBS extension, server_name_list, host_name;
148 uint8_t name_type;
149
150 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
151 &extension_data,
152 &extension_len)) {
153 fprintf(stderr, "Could not find server_name extension.\n");
154 return -1;
155 }
156
157 CBS_init(&extension, extension_data, extension_len);
158 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
159 CBS_len(&extension) != 0 ||
160 !CBS_get_u8(&server_name_list, &name_type) ||
161 name_type != TLSEXT_NAMETYPE_host_name ||
162 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
163 CBS_len(&server_name_list) != 0) {
164 fprintf(stderr, "Could not decode server_name extension.\n");
165 return -1;
166 }
167
168 if (!CBS_mem_equal(&host_name,
169 (const uint8_t*)config->expected_server_name.data(),
170 config->expected_server_name.size())) {
171 fprintf(stderr, "Server name mismatch.\n");
172 }
David Benjamin7b030512014-07-08 17:30:11 -0400173 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400174
David Benjamin6f5c0f42015-02-24 01:23:21 -0500175 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400176 return -1;
177 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400178
David Benjamin6f5c0f42015-02-24 01:23:21 -0500179 // Install the certificate in the early callback.
180 if (config->use_early_callback) {
181 if (config->async) {
182 // Install the certificate asynchronously.
183 return 0;
184 }
185 if (!InstallCertificate(ctx->ssl)) {
186 return -1;
187 }
David Benjamin7b030512014-07-08 17:30:11 -0400188 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400189 return 1;
190}
David Benjamin025b3d32014-07-01 19:53:04 -0400191
David Benjaminc273d2c2015-02-09 12:59:46 -0500192static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400193 return 1;
194}
195
David Benjaminc273d2c2015-02-09 12:59:46 -0500196static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
197 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400198 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500199 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400200 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500201 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400202
David Benjamin5a593af2014-08-11 19:51:50 -0400203 *out = (const uint8_t*)config->advertise_npn.data();
204 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400205 return SSL_TLSEXT_ERR_OK;
206}
207
David Benjaminc273d2c2015-02-09 12:59:46 -0500208static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400209 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400210 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500211 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400212 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500213 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400214
David Benjamin5a593af2014-08-11 19:51:50 -0400215 *out = (uint8_t*)config->select_next_proto.data();
216 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400217 return SSL_TLSEXT_ERR_OK;
218}
219
David Benjaminc273d2c2015-02-09 12:59:46 -0500220static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
221 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400222 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500223 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400224 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500225 }
David Benjaminae2888f2014-09-06 12:58:58 -0400226
227 if (!config->expected_advertised_alpn.empty() &&
228 (config->expected_advertised_alpn.size() != inlen ||
229 memcmp(config->expected_advertised_alpn.data(),
230 in, inlen) != 0)) {
231 fprintf(stderr, "bad ALPN select callback inputs\n");
232 exit(1);
233 }
234
235 *out = (const uint8_t*)config->select_alpn.data();
236 *outlen = config->select_alpn.size();
237 return SSL_TLSEXT_ERR_OK;
238}
239
David Benjaminc273d2c2015-02-09 12:59:46 -0500240static unsigned PskClientCallback(SSL *ssl, const char *hint,
241 char *out_identity,
242 unsigned max_identity_len,
243 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400244 const TestConfig *config = GetConfigPtr(ssl);
245
246 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
247 fprintf(stderr, "Server PSK hint did not match.\n");
248 return 0;
249 }
250
251 // Account for the trailing '\0' for the identity.
252 if (config->psk_identity.size() >= max_identity_len ||
253 config->psk.size() > max_psk_len) {
254 fprintf(stderr, "PSK buffers too small\n");
255 return 0;
256 }
257
258 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
259 max_identity_len);
260 memcpy(out_psk, config->psk.data(), config->psk.size());
261 return config->psk.size();
262}
263
David Benjaminc273d2c2015-02-09 12:59:46 -0500264static unsigned PskServerCallback(SSL *ssl, const char *identity,
265 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400266 const TestConfig *config = GetConfigPtr(ssl);
267
268 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
269 fprintf(stderr, "Client PSK identity did not match.\n");
270 return 0;
271 }
272
273 if (config->psk.size() > max_psk_len) {
274 fprintf(stderr, "PSK buffers too small\n");
275 return 0;
276 }
277
278 memcpy(out_psk, config->psk.data(), config->psk.size());
279 return config->psk.size();
280}
281
David Benjaminc273d2c2015-02-09 12:59:46 -0500282static void CurrentTimeCallback(SSL *ssl, OPENSSL_timeval *out_clock) {
David Benjamin83f90402015-01-27 01:09:43 -0500283 *out_clock = *GetClockPtr(ssl);
David Benjamin377fc312015-01-26 00:22:12 -0500284}
285
David Benjaminc273d2c2015-02-09 12:59:46 -0500286static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500287 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500288}
289
David Benjaminc273d2c2015-02-09 12:59:46 -0500290static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500291 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500292 return -1;
293 }
294 if (!InstallCertificate(ssl)) {
295 return 0;
296 }
297 return 1;
298}
299
David Benjaminc273d2c2015-02-09 12:59:46 -0500300static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
301 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500302 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500303 if (async_state->session) {
304 *copy = 0;
305 return async_state->session.release();
306 } else if (async_state->pending_session) {
307 return SSL_magic_pending_session_ptr();
308 } else {
309 return NULL;
310 }
311}
312
Adam Langley524e7172015-02-20 16:04:00 -0800313static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
314 const TestConfig *config = GetConfigPtr(early_context->ssl);
315 static int callback_num = 0;
316
317 callback_num++;
318 if (config->fail_ddos_callback ||
319 (config->fail_second_ddos_callback && callback_num == 2)) {
320 return 0;
321 }
322 return 1;
323}
324
David Benjamin87e4acd2015-04-02 19:57:35 -0400325static void InfoCallback(const SSL *ssl, int type, int val) {
326 if (type == SSL_CB_HANDSHAKE_DONE) {
327 if (GetConfigPtr(ssl)->handshake_never_done) {
328 fprintf(stderr, "handshake completed\n");
329 // Abort before any expected error code is printed, to ensure the overall
330 // test fails.
331 abort();
332 }
333 GetTestState(ssl)->handshake_done = true;
334 }
335}
336
David Benjamin87c8a642015-02-21 01:54:29 -0500337// Connect returns a new socket connected to localhost on |port| or -1 on
338// error.
339static int Connect(uint16_t port) {
340 int sock = socket(AF_INET, SOCK_STREAM, 0);
341 if (sock == -1) {
342 PrintSocketError("socket");
343 return -1;
344 }
345 int nodelay = 1;
346 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
347 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
348 PrintSocketError("setsockopt");
349 closesocket(sock);
350 return -1;
351 }
352 sockaddr_in sin;
353 memset(&sin, 0, sizeof(sin));
354 sin.sin_family = AF_INET;
355 sin.sin_port = htons(port);
356 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
357 PrintSocketError("inet_pton");
358 closesocket(sock);
359 return -1;
360 }
361 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
362 sizeof(sin)) != 0) {
363 PrintSocketError("connect");
364 closesocket(sock);
365 return -1;
366 }
367 return sock;
368}
369
370class SocketCloser {
371 public:
372 explicit SocketCloser(int sock) : sock_(sock) {}
373 ~SocketCloser() {
374 // Half-close and drain the socket before releasing it. This seems to be
375 // necessary for graceful shutdown on Windows. It will also avoid write
376 // failures in the test runner.
377#if defined(OPENSSL_WINDOWS)
378 shutdown(sock_, SD_SEND);
379#else
380 shutdown(sock_, SHUT_WR);
381#endif
382 while (true) {
383 char buf[1024];
384 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
385 break;
386 }
387 }
388 closesocket(sock_);
389 }
390
391 private:
392 const int sock_;
393};
394
David Benjaminc273d2c2015-02-09 12:59:46 -0500395static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500396 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
397 config->is_dtls ? DTLS_method() : TLS_method()));
398 if (!ssl_ctx) {
399 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400400 }
401
David Benjamin6fd297b2014-08-11 18:43:38 -0400402 if (config->is_dtls) {
403 // DTLS needs read-ahead to function on a datagram BIO.
404 //
405 // TODO(davidben): this should not be necessary. DTLS code should only
406 // expect a datagram BIO.
David Benjamina7f333d2015-02-09 02:37:18 -0500407 SSL_CTX_set_read_ahead(ssl_ctx.get(), 1);
David Benjamin6fd297b2014-08-11 18:43:38 -0400408 }
409
David Benjamina7f333d2015-02-09 02:37:18 -0500410 if (!SSL_CTX_set_ecdh_auto(ssl_ctx.get(), 1)) {
411 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400412 }
413
David Benjamina7f333d2015-02-09 02:37:18 -0500414 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
415 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400416 }
417
David Benjamina7f333d2015-02-09 02:37:18 -0500418 ScopedDH dh(DH_get_2048_256(NULL));
419 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
420 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400421 }
422
David Benjamin1b8b6912015-02-09 04:28:16 -0500423 if (config->async && config->is_server) {
424 // Disable the internal session cache. To test asynchronous session lookup,
425 // we use an external session cache.
426 SSL_CTX_set_session_cache_mode(
427 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500428 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500429 } else {
430 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
431 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400432
David Benjaminc273d2c2015-02-09 12:59:46 -0500433 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400434
David Benjamin1f5f62b2014-07-12 16:18:02 -0400435 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500436 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400437 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500438 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500439 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400440 }
441
442 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500443 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400444 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400445
David Benjamina08e49d2014-08-24 01:46:07 -0400446 ssl_ctx->tlsext_channel_id_enabled_new = 1;
David Benjaminc273d2c2015-02-09 12:59:46 -0500447 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400448
David Benjaminc273d2c2015-02-09 12:59:46 -0500449 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500450
David Benjamin87e4acd2015-04-02 19:57:35 -0400451 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
452
David Benjamin1d5c83e2014-07-22 19:20:02 -0400453 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400454}
455
David Benjamin40f101b2015-02-20 11:23:42 -0500456// RetryAsync is called after a failed operation on |ssl| with return code
457// |ret|. If the operation should be retried, it simulates one asynchronous
458// event and returns true. Otherwise it returns false. |async| and |clock_delta|
459// are the AsyncBio and simulated timeout for |ssl|, respectively.
460static bool RetryAsync(SSL *ssl, int ret, BIO *async,
461 OPENSSL_timeval *clock_delta) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400462 // No error; don't retry.
463 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500464 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400465 }
David Benjamin83f90402015-01-27 01:09:43 -0500466
467 if (clock_delta->tv_usec != 0 || clock_delta->tv_sec != 0) {
468 // Process the timeout and retry.
469 OPENSSL_timeval *clock = GetClockPtr(ssl);
470 clock->tv_usec += clock_delta->tv_usec;
471 clock->tv_sec += clock->tv_usec / 1000000;
472 clock->tv_usec %= 1000000;
473 clock->tv_sec += clock_delta->tv_sec;
474 memset(clock_delta, 0, sizeof(*clock_delta));
475
476 if (DTLSv1_handle_timeout(ssl) < 0) {
477 printf("Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500478 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500479 }
David Benjamin40f101b2015-02-20 11:23:42 -0500480 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500481 }
482
David Benjamin43ec06f2014-08-05 02:28:57 -0400483 // See if we needed to read or write more. If so, allow one byte through on
484 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500485 switch (SSL_get_error(ssl, ret)) {
486 case SSL_ERROR_WANT_READ:
David Benjaminc273d2c2015-02-09 12:59:46 -0500487 AsyncBioAllowRead(async, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500488 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500489 case SSL_ERROR_WANT_WRITE:
David Benjaminc273d2c2015-02-09 12:59:46 -0500490 AsyncBioAllowWrite(async, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500491 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500492 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
493 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
494 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500495 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500496 }
497 GetTestState(ssl)->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500498 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500499 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500500 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin2d445c02015-02-09 13:03:50 -0500501 GetTestState(ssl)->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500502 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500503 case SSL_ERROR_PENDING_SESSION:
David Benjamin2d445c02015-02-09 13:03:50 -0500504 GetTestState(ssl)->session =
505 std::move(GetTestState(ssl)->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500506 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500507 case SSL_ERROR_PENDING_CERTIFICATE:
508 // The handshake will resume without a second call to the early callback.
509 return InstallCertificate(ssl);
David Benjamind9e07012015-02-09 03:04:34 -0500510 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500511 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400512 }
David Benjamin025b3d32014-07-01 19:53:04 -0400513}
514
David Benjamin87c8a642015-02-21 01:54:29 -0500515// DoExchange runs a test SSL exchange against the peer. On success, it returns
516// true and sets |*out_session| to the negotiated SSL session. If the test is a
517// resumption attempt, |is_resume| is true and |session| is the session from the
518// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500519static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
520 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500521 SSL_SESSION *session) {
David Benjamin83f90402015-01-27 01:09:43 -0500522 OPENSSL_timeval clock = {0}, clock_delta = {0};
David Benjamina7f333d2015-02-09 02:37:18 -0500523 ScopedSSL ssl(SSL_new(ssl_ctx));
524 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500525 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400526 }
527
David Benjamina7f333d2015-02-09 02:37:18 -0500528 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamind9e07012015-02-09 03:04:34 -0500529 !SetClockPtr(ssl.get(), &clock) |
David Benjamin2d445c02015-02-09 13:03:50 -0500530 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500531 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800532 }
David Benjamin5a593af2014-08-11 19:51:50 -0400533
Adam Langley5f0efe02015-02-20 13:03:16 -0800534 if (config->fallback_scsv &&
535 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
536 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400537 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500538 if (!config->use_early_callback) {
539 if (config->async) {
540 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
541 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
542 } else if (!InstallCertificate(ssl.get())) {
543 return false;
544 }
David Benjamin5a593af2014-08-11 19:51:50 -0400545 }
546 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500547 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500548 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400549 }
550 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500551 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400552 }
553 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500554 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400555 }
556 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500557 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400558 }
559 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500560 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400561 }
562 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500563 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400564 }
565 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500566 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400567 }
568 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500569 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400570 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400571 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500572 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400573 }
David Benjaminca6554b2014-11-08 12:31:52 -0500574 if (config->allow_unsafe_legacy_renegotiation) {
David Benjamina7f333d2015-02-09 02:37:18 -0500575 SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
David Benjaminca6554b2014-11-08 12:31:52 -0500576 }
David Benjamina08e49d2014-08-24 01:46:07 -0400577 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500578 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400579 }
580 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500581 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500582 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500583 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500584 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
585 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500586 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500587 }
David Benjamina08e49d2014-08-24 01:46:07 -0400588 }
David Benjamina08e49d2014-08-24 01:46:07 -0400589 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500590 if (!config->host_name.empty() &&
591 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500592 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400593 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500594 if (!config->advertise_alpn.empty() &&
595 SSL_set_alpn_protos(ssl.get(),
596 (const uint8_t *)config->advertise_alpn.data(),
597 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500598 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400599 }
David Benjamin48cae082014-10-27 01:06:24 -0400600 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500601 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
602 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400603 }
David Benjamin61f95272014-11-25 01:55:35 -0500604 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500605 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500606 return false;
David Benjamin48cae082014-10-27 01:06:24 -0400607 }
David Benjamin61f95272014-11-25 01:55:35 -0500608 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500609 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500610 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500611 }
612 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -0500613 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500614 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500615 }
616 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -0500617 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500618 return false;
David Benjaminca6c8262014-11-15 19:06:08 -0500619 }
David Benjamina7f333d2015-02-09 02:37:18 -0500620 SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500621 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500622 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500623 }
624 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500625 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500626 }
David Benjamin13be1de2015-01-11 16:29:36 -0500627 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500628 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
629 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -0500630 }
Adam Langley524e7172015-02-20 16:04:00 -0800631 if (config->install_ddos_callback) {
632 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
633 }
David Benjamin67d1fb52015-03-16 15:16:23 -0400634 if (!config->cipher.empty() &&
635 !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
636 return false;
637 }
David Benjamin025b3d32014-07-01 19:53:04 -0400638
David Benjamin87c8a642015-02-21 01:54:29 -0500639 int sock = Connect(config->port);
640 if (sock == -1) {
641 return false;
642 }
643 SocketCloser closer(sock);
644
645 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -0500646 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -0500647 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -0400648 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400649 if (config->is_dtls) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500650 ScopedBIO packeted = PacketedBioCreate(&clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -0500651 BIO_push(packeted.get(), bio.release());
652 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -0400653 }
David Benjamina7f333d2015-02-09 02:37:18 -0500654 BIO *async = NULL;
David Benjamin5a593af2014-08-11 19:51:50 -0400655 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -0500656 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -0500657 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -0500658 BIO_push(async_scoped.get(), bio.release());
659 async = async_scoped.get();
660 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -0400661 }
David Benjamina7f333d2015-02-09 02:37:18 -0500662 SSL_set_bio(ssl.get(), bio.get(), bio.get());
663 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -0400664
David Benjamin1d5c83e2014-07-22 19:20:02 -0400665 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -0500666 if (!config->is_server) {
667 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500668 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -0500669 }
670 } else if (config->async) {
671 // The internal session cache is disabled, so install the session
672 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -0500673 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -0500674 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400675 }
676 }
677
678 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500679 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -0400680 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500681 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400682 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500683 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400684 }
David Benjamine0e7d0d2015-02-08 19:33:25 -0500685 } else {
686 do {
687 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500688 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500689 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500690 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500691 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500692 } while (config->async && RetryAsync(ssl.get(), ret, async, &clock_delta));
David Benjamine0e7d0d2015-02-08 19:33:25 -0500693 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500694 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500695 }
David Benjamin025b3d32014-07-01 19:53:04 -0400696
David Benjamine0e7d0d2015-02-08 19:33:25 -0500697 if (is_resume &&
David Benjamina7f333d2015-02-09 02:37:18 -0500698 (!!SSL_session_reused(ssl.get()) == config->expect_session_miss)) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500699 fprintf(stderr, "session was%s reused\n",
David Benjamina7f333d2015-02-09 02:37:18 -0500700 SSL_session_reused(ssl.get()) ? "" : " not");
David Benjamin40f101b2015-02-20 11:23:42 -0500701 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500702 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400703
David Benjamin87e4acd2015-04-02 19:57:35 -0400704 bool expect_handshake_done = is_resume || !config->false_start;
705 if (expect_handshake_done != GetTestState(ssl.get())->handshake_done) {
706 fprintf(stderr, "handshake was%s completed\n",
707 GetTestState(ssl.get())->handshake_done ? "" : " not");
708 return false;
709 }
710
David Benjamin6f5c0f42015-02-24 01:23:21 -0500711 if (config->is_server && !GetTestState(ssl.get())->early_callback_called) {
712 fprintf(stderr, "early callback not called\n");
713 return false;
714 }
715
David Benjamine0e7d0d2015-02-08 19:33:25 -0500716 if (!config->expected_server_name.empty()) {
717 const char *server_name =
David Benjamina7f333d2015-02-09 02:37:18 -0500718 SSL_get_servername(ssl.get(), TLSEXT_NAMETYPE_host_name);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500719 if (server_name != config->expected_server_name) {
720 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
721 server_name, config->expected_server_name.c_str());
David Benjamin40f101b2015-02-20 11:23:42 -0500722 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500723 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400724 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400725
David Benjamine0e7d0d2015-02-08 19:33:25 -0500726 if (!config->expected_certificate_types.empty()) {
727 uint8_t *certificate_types;
728 int num_certificate_types =
David Benjamina7f333d2015-02-09 02:37:18 -0500729 SSL_get0_certificate_types(ssl.get(), &certificate_types);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500730 if (num_certificate_types !=
731 (int)config->expected_certificate_types.size() ||
732 memcmp(certificate_types,
733 config->expected_certificate_types.data(),
734 num_certificate_types) != 0) {
735 fprintf(stderr, "certificate types mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500736 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500737 }
David Benjamin7b030512014-07-08 17:30:11 -0400738 }
David Benjamin7b030512014-07-08 17:30:11 -0400739
David Benjamine0e7d0d2015-02-08 19:33:25 -0500740 if (!config->expected_next_proto.empty()) {
741 const uint8_t *next_proto;
742 unsigned next_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500743 SSL_get0_next_proto_negotiated(ssl.get(), &next_proto, &next_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500744 if (next_proto_len != config->expected_next_proto.size() ||
745 memcmp(next_proto, config->expected_next_proto.data(),
746 next_proto_len) != 0) {
747 fprintf(stderr, "negotiated next proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500748 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500749 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400750 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400751
David Benjamine0e7d0d2015-02-08 19:33:25 -0500752 if (!config->expected_alpn.empty()) {
753 const uint8_t *alpn_proto;
754 unsigned alpn_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500755 SSL_get0_alpn_selected(ssl.get(), &alpn_proto, &alpn_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500756 if (alpn_proto_len != config->expected_alpn.size() ||
757 memcmp(alpn_proto, config->expected_alpn.data(),
758 alpn_proto_len) != 0) {
759 fprintf(stderr, "negotiated alpn proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500760 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500761 }
David Benjaminae2888f2014-09-06 12:58:58 -0400762 }
David Benjaminae2888f2014-09-06 12:58:58 -0400763
David Benjamine0e7d0d2015-02-08 19:33:25 -0500764 if (!config->expected_channel_id.empty()) {
765 uint8_t channel_id[64];
David Benjamina7f333d2015-02-09 02:37:18 -0500766 if (!SSL_get_tls_channel_id(ssl.get(), channel_id, sizeof(channel_id))) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500767 fprintf(stderr, "no channel id negotiated\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500768 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500769 }
770 if (config->expected_channel_id.size() != 64 ||
771 memcmp(config->expected_channel_id.data(),
772 channel_id, 64) != 0) {
773 fprintf(stderr, "channel id mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500774 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500775 }
David Benjamina08e49d2014-08-24 01:46:07 -0400776 }
David Benjamina08e49d2014-08-24 01:46:07 -0400777
David Benjamine0e7d0d2015-02-08 19:33:25 -0500778 if (config->expect_extended_master_secret) {
779 if (!ssl->session->extended_master_secret) {
780 fprintf(stderr, "No EMS for session when expected");
David Benjamin40f101b2015-02-20 11:23:42 -0500781 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500782 }
Adam Langley75712922014-10-10 16:23:43 -0700783 }
Adam Langley75712922014-10-10 16:23:43 -0700784
David Benjamine0e7d0d2015-02-08 19:33:25 -0500785 if (!config->expected_ocsp_response.empty()) {
786 const uint8_t *data;
787 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500788 SSL_get0_ocsp_response(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500789 if (config->expected_ocsp_response.size() != len ||
790 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
791 fprintf(stderr, "OCSP response mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500792 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500793 }
David Benjamin61f95272014-11-25 01:55:35 -0500794 }
David Benjamin61f95272014-11-25 01:55:35 -0500795
David Benjamine0e7d0d2015-02-08 19:33:25 -0500796 if (!config->expected_signed_cert_timestamps.empty()) {
797 const uint8_t *data;
798 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500799 SSL_get0_signed_cert_timestamp_list(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500800 if (config->expected_signed_cert_timestamps.size() != len ||
801 memcmp(config->expected_signed_cert_timestamps.data(),
802 data, len) != 0) {
803 fprintf(stderr, "SCT list mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500804 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500805 }
David Benjamin61f95272014-11-25 01:55:35 -0500806 }
807 }
808
Adam Langley2ae77d22014-10-28 17:29:33 -0700809 if (config->renegotiate) {
810 if (config->async) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500811 fprintf(stderr, "-renegotiate is not supported with -async.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500812 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500813 }
814 if (config->implicit_handshake) {
815 fprintf(stderr, "-renegotiate is not supported with -implicit-handshake.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500816 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700817 }
818
David Benjamina7f333d2015-02-09 02:37:18 -0500819 SSL_renegotiate(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700820
David Benjamina7f333d2015-02-09 02:37:18 -0500821 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700822 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500823 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700824 }
825
David Benjamina7f333d2015-02-09 02:37:18 -0500826 SSL_set_state(ssl.get(), SSL_ST_ACCEPT);
827 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700828 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500829 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700830 }
831 }
832
David Benjamin5a593af2014-08-11 19:51:50 -0400833 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400834 if (config->is_dtls) {
835 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500836 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -0400837 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700838 // This mode writes a number of different record sizes in an attempt to
839 // trip up the CBC record splitting code.
840 uint8_t buf[32769];
841 memset(buf, 0x42, sizeof(buf));
842 static const size_t kRecordSizes[] = {
843 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
844 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
845 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400846 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700847 const size_t len = kRecordSizes[i];
848 size_t off = 0;
849
850 if (len > sizeof(buf)) {
851 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500852 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700853 }
854
David Benjamin43ec06f2014-08-05 02:28:57 -0400855 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500856 w = SSL_write(ssl.get(), buf + off, len - off);
Kenny Root7fdeaf12014-08-05 15:23:37 -0700857 if (w > 0) {
858 off += (size_t) w;
859 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500860 } while ((config->async && RetryAsync(ssl.get(), w, async, &clock_delta)) ||
David Benjamin5a593af2014-08-11 19:51:50 -0400861 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700862
863 if (w < 0 || off != len) {
David Benjamin40f101b2015-02-20 11:23:42 -0500864 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400865 }
866 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700867 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400868 if (config->shim_writes_first) {
869 int w;
870 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500871 w = SSL_write(ssl.get(), "hello", 5);
David Benjaminc273d2c2015-02-09 12:59:46 -0500872 } while (config->async && RetryAsync(ssl.get(), w, async, &clock_delta));
David Benjamine58c4f52014-08-24 03:47:07 -0400873 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700874 for (;;) {
875 uint8_t buf[512];
876 int n;
877 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500878 n = SSL_read(ssl.get(), buf, sizeof(buf));
David Benjaminc273d2c2015-02-09 12:59:46 -0500879 } while (config->async && RetryAsync(ssl.get(), n, async, &clock_delta));
David Benjamina7f333d2015-02-09 02:37:18 -0500880 int err = SSL_get_error(ssl.get(), n);
David Benjamin9a38e922015-01-22 16:06:11 -0500881 if (err == SSL_ERROR_ZERO_RETURN ||
882 (n == 0 && err == SSL_ERROR_SYSCALL)) {
883 if (n != 0) {
884 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500885 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500886 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500887 // Accept shutdowns with or without close_notify.
888 // TODO(davidben): Write tests which distinguish these two cases.
David Benjamin9a38e922015-01-22 16:06:11 -0500889 break;
890 } else if (err != SSL_ERROR_NONE) {
891 if (n > 0) {
892 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500893 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500894 }
David Benjamin40f101b2015-02-20 11:23:42 -0500895 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500896 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500897 // Successfully read data.
David Benjamin9a38e922015-01-22 16:06:11 -0500898 if (n <= 0) {
899 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500900 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500901 }
David Benjamin87e4acd2015-04-02 19:57:35 -0400902
903 // After a successful read, with or without False Start, the handshake
904 // must be complete.
905 if (!GetTestState(ssl.get())->handshake_done) {
906 fprintf(stderr, "handshake was not completed after SSL_read\n");
907 return false;
908 }
909
David Benjamin9a38e922015-01-22 16:06:11 -0500910 for (int i = 0; i < n; i++) {
911 buf[i] ^= 0xff;
912 }
913 int w;
914 do {
David Benjamina7f333d2015-02-09 02:37:18 -0500915 w = SSL_write(ssl.get(), buf, n);
David Benjaminc273d2c2015-02-09 12:59:46 -0500916 } while (config->async && RetryAsync(ssl.get(), w, async, &clock_delta));
David Benjamin9a38e922015-01-22 16:06:11 -0500917 if (w != n) {
David Benjamin40f101b2015-02-20 11:23:42 -0500918 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700919 }
920 }
David Benjamin025b3d32014-07-01 19:53:04 -0400921 }
922
David Benjamin1d5c83e2014-07-22 19:20:02 -0400923 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500924 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400925 }
926
David Benjamina7f333d2015-02-09 02:37:18 -0500927 SSL_shutdown(ssl.get());
David Benjamin40f101b2015-02-20 11:23:42 -0500928 return true;
David Benjamin025b3d32014-07-01 19:53:04 -0400929}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400930
931int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -0500932#if defined(OPENSSL_WINDOWS)
933 /* Initialize Winsock. */
934 WORD wsa_version = MAKEWORD(2, 2);
935 WSADATA wsa_data;
936 int wsa_err = WSAStartup(wsa_version, &wsa_data);
937 if (wsa_err != 0) {
938 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
939 return 1;
940 }
941 if (wsa_data.wVersion != wsa_version) {
942 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
943 return 1;
944 }
945#else
David Benjamin1d5c83e2014-07-22 19:20:02 -0400946 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700947#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400948
David Benjamin5a593af2014-08-11 19:51:50 -0400949 if (!SSL_library_init()) {
950 return 1;
951 }
David Benjamind9e07012015-02-09 03:04:34 -0500952 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
953 g_clock_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);
955 if (g_config_index < 0 || g_clock_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}