blob: 751a80f3375fec9c46e5d23f295a83d111b47575 [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>
David Benjaminc565ebb2015-04-03 04:06:36 -040043#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040044
45#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040046#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040047#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050048#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040049#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040050
David Benjamin87c8a642015-02-21 01:54:29 -050051
52#if !defined(OPENSSL_WINDOWS)
53static int closesocket(int sock) {
54 return close(sock);
55}
56
57static void PrintSocketError(const char *func) {
58 perror(func);
59}
60#else
61static void PrintSocketError(const char *func) {
62 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
63}
64#endif
65
David Benjaminc273d2c2015-02-09 12:59:46 -050066static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050067 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040068 return 1;
69}
David Benjamin025b3d32014-07-01 19:53:04 -040070
David Benjamin2d445c02015-02-09 13:03:50 -050071struct TestState {
David Benjamin6c2563e2015-04-03 03:47:47 -040072 // async_bio is async BIO which pauses reads and writes.
73 BIO *async_bio = nullptr;
74 // clock is the current time for the SSL connection.
75 OPENSSL_timeval clock = {0};
76 // clock_delta is how far the clock advanced in the most recent failed
77 // |BIO_read|.
78 OPENSSL_timeval clock_delta = {0};
David Benjamind9e07012015-02-09 03:04:34 -050079 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040080 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050081 ScopedSSL_SESSION session;
82 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040083 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040084 bool handshake_done = false;
David Benjamind9e07012015-02-09 03:04:34 -050085};
86
David Benjamin2d445c02015-02-09 13:03:50 -050087static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
88 int index, long argl, void *argp) {
89 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -050090}
91
92static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -050093static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -040094
Adam Langley69a01602014-11-17 17:26:55 -080095static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -050096 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -040097}
98
David Benjamin87e4acd2015-04-02 19:57:35 -040099static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500100 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400101}
102
David Benjamin2d445c02015-02-09 13:03:50 -0500103static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
104 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500105 async.release();
106 return true;
107 }
108 return false;
109}
110
David Benjamin87e4acd2015-04-02 19:57:35 -0400111static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500112 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500113}
114
David Benjamina7f333d2015-02-09 02:37:18 -0500115static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
116 ScopedBIO bio(BIO_new(BIO_s_file()));
117 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
118 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400119 }
David Benjamina7f333d2015-02-09 02:37:18 -0500120 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400121 return pkey;
122}
123
David Benjamin41fdbcd2015-02-09 03:13:35 -0500124static bool InstallCertificate(SSL *ssl) {
125 const TestConfig *config = GetConfigPtr(ssl);
126 if (!config->key_file.empty() &&
127 !SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
128 SSL_FILETYPE_PEM)) {
129 return false;
130 }
131 if (!config->cert_file.empty() &&
132 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
133 SSL_FILETYPE_PEM)) {
134 return false;
135 }
136 return true;
137}
138
David Benjaminc273d2c2015-02-09 12:59:46 -0500139static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400140 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500141 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400142
David Benjamin6f5c0f42015-02-24 01:23:21 -0500143 if (!config->expected_server_name.empty()) {
144 const uint8_t *extension_data;
145 size_t extension_len;
146 CBS extension, server_name_list, host_name;
147 uint8_t name_type;
148
149 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
150 &extension_data,
151 &extension_len)) {
152 fprintf(stderr, "Could not find server_name extension.\n");
153 return -1;
154 }
155
156 CBS_init(&extension, extension_data, extension_len);
157 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
158 CBS_len(&extension) != 0 ||
159 !CBS_get_u8(&server_name_list, &name_type) ||
160 name_type != TLSEXT_NAMETYPE_host_name ||
161 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
162 CBS_len(&server_name_list) != 0) {
163 fprintf(stderr, "Could not decode server_name extension.\n");
164 return -1;
165 }
166
167 if (!CBS_mem_equal(&host_name,
168 (const uint8_t*)config->expected_server_name.data(),
169 config->expected_server_name.size())) {
170 fprintf(stderr, "Server name mismatch.\n");
171 }
David Benjamin7b030512014-07-08 17:30:11 -0400172 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400173
David Benjamin6f5c0f42015-02-24 01:23:21 -0500174 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400175 return -1;
176 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400177
David Benjamin6f5c0f42015-02-24 01:23:21 -0500178 // Install the certificate in the early callback.
179 if (config->use_early_callback) {
180 if (config->async) {
181 // Install the certificate asynchronously.
182 return 0;
183 }
184 if (!InstallCertificate(ctx->ssl)) {
185 return -1;
186 }
David Benjamin7b030512014-07-08 17:30:11 -0400187 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400188 return 1;
189}
David Benjamin025b3d32014-07-01 19:53:04 -0400190
David Benjaminc273d2c2015-02-09 12:59:46 -0500191static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400192 return 1;
193}
194
David Benjaminc273d2c2015-02-09 12:59:46 -0500195static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
196 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400197 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500198 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400199 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500200 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400201
David Benjamin5a593af2014-08-11 19:51:50 -0400202 *out = (const uint8_t*)config->advertise_npn.data();
203 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400204 return SSL_TLSEXT_ERR_OK;
205}
206
David Benjaminc273d2c2015-02-09 12:59:46 -0500207static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400208 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400209 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500210 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400211 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500212 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400213
David Benjamin5a593af2014-08-11 19:51:50 -0400214 *out = (uint8_t*)config->select_next_proto.data();
215 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400216 return SSL_TLSEXT_ERR_OK;
217}
218
David Benjaminc273d2c2015-02-09 12:59:46 -0500219static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
220 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400221 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500222 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400223 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500224 }
David Benjaminae2888f2014-09-06 12:58:58 -0400225
226 if (!config->expected_advertised_alpn.empty() &&
227 (config->expected_advertised_alpn.size() != inlen ||
228 memcmp(config->expected_advertised_alpn.data(),
229 in, inlen) != 0)) {
230 fprintf(stderr, "bad ALPN select callback inputs\n");
231 exit(1);
232 }
233
234 *out = (const uint8_t*)config->select_alpn.data();
235 *outlen = config->select_alpn.size();
236 return SSL_TLSEXT_ERR_OK;
237}
238
David Benjaminc273d2c2015-02-09 12:59:46 -0500239static unsigned PskClientCallback(SSL *ssl, const char *hint,
240 char *out_identity,
241 unsigned max_identity_len,
242 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400243 const TestConfig *config = GetConfigPtr(ssl);
244
245 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
246 fprintf(stderr, "Server PSK hint did not match.\n");
247 return 0;
248 }
249
250 // Account for the trailing '\0' for the identity.
251 if (config->psk_identity.size() >= max_identity_len ||
252 config->psk.size() > max_psk_len) {
253 fprintf(stderr, "PSK buffers too small\n");
254 return 0;
255 }
256
257 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
258 max_identity_len);
259 memcpy(out_psk, config->psk.data(), config->psk.size());
260 return config->psk.size();
261}
262
David Benjaminc273d2c2015-02-09 12:59:46 -0500263static unsigned PskServerCallback(SSL *ssl, const char *identity,
264 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400265 const TestConfig *config = GetConfigPtr(ssl);
266
267 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
268 fprintf(stderr, "Client PSK identity did not match.\n");
269 return 0;
270 }
271
272 if (config->psk.size() > max_psk_len) {
273 fprintf(stderr, "PSK buffers too small\n");
274 return 0;
275 }
276
277 memcpy(out_psk, config->psk.data(), config->psk.size());
278 return config->psk.size();
279}
280
David Benjaminc273d2c2015-02-09 12:59:46 -0500281static void CurrentTimeCallback(SSL *ssl, OPENSSL_timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400282 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500283}
284
David Benjaminc273d2c2015-02-09 12:59:46 -0500285static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500286 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500287}
288
David Benjaminc273d2c2015-02-09 12:59:46 -0500289static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500290 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500291 return -1;
292 }
293 if (!InstallCertificate(ssl)) {
294 return 0;
295 }
296 return 1;
297}
298
David Benjaminc273d2c2015-02-09 12:59:46 -0500299static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
300 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500301 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500302 if (async_state->session) {
303 *copy = 0;
304 return async_state->session.release();
305 } else if (async_state->pending_session) {
306 return SSL_magic_pending_session_ptr();
307 } else {
308 return NULL;
309 }
310}
311
Adam Langley524e7172015-02-20 16:04:00 -0800312static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
313 const TestConfig *config = GetConfigPtr(early_context->ssl);
314 static int callback_num = 0;
315
316 callback_num++;
317 if (config->fail_ddos_callback ||
318 (config->fail_second_ddos_callback && callback_num == 2)) {
319 return 0;
320 }
321 return 1;
322}
323
David Benjamin87e4acd2015-04-02 19:57:35 -0400324static void InfoCallback(const SSL *ssl, int type, int val) {
325 if (type == SSL_CB_HANDSHAKE_DONE) {
326 if (GetConfigPtr(ssl)->handshake_never_done) {
327 fprintf(stderr, "handshake completed\n");
328 // Abort before any expected error code is printed, to ensure the overall
329 // test fails.
330 abort();
331 }
332 GetTestState(ssl)->handshake_done = true;
333 }
334}
335
David Benjamin87c8a642015-02-21 01:54:29 -0500336// Connect returns a new socket connected to localhost on |port| or -1 on
337// error.
338static int Connect(uint16_t port) {
339 int sock = socket(AF_INET, SOCK_STREAM, 0);
340 if (sock == -1) {
341 PrintSocketError("socket");
342 return -1;
343 }
344 int nodelay = 1;
345 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
346 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
347 PrintSocketError("setsockopt");
348 closesocket(sock);
349 return -1;
350 }
351 sockaddr_in sin;
352 memset(&sin, 0, sizeof(sin));
353 sin.sin_family = AF_INET;
354 sin.sin_port = htons(port);
355 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
356 PrintSocketError("inet_pton");
357 closesocket(sock);
358 return -1;
359 }
360 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
361 sizeof(sin)) != 0) {
362 PrintSocketError("connect");
363 closesocket(sock);
364 return -1;
365 }
366 return sock;
367}
368
369class SocketCloser {
370 public:
371 explicit SocketCloser(int sock) : sock_(sock) {}
372 ~SocketCloser() {
373 // Half-close and drain the socket before releasing it. This seems to be
374 // necessary for graceful shutdown on Windows. It will also avoid write
375 // failures in the test runner.
376#if defined(OPENSSL_WINDOWS)
377 shutdown(sock_, SD_SEND);
378#else
379 shutdown(sock_, SHUT_WR);
380#endif
381 while (true) {
382 char buf[1024];
383 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
384 break;
385 }
386 }
387 closesocket(sock_);
388 }
389
390 private:
391 const int sock_;
392};
393
David Benjaminc273d2c2015-02-09 12:59:46 -0500394static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500395 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
396 config->is_dtls ? DTLS_method() : TLS_method()));
397 if (!ssl_ctx) {
398 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400399 }
400
David Benjamin6fd297b2014-08-11 18:43:38 -0400401 if (config->is_dtls) {
402 // DTLS needs read-ahead to function on a datagram BIO.
403 //
404 // TODO(davidben): this should not be necessary. DTLS code should only
405 // expect a datagram BIO.
David Benjamina7f333d2015-02-09 02:37:18 -0500406 SSL_CTX_set_read_ahead(ssl_ctx.get(), 1);
David Benjamin6fd297b2014-08-11 18:43:38 -0400407 }
408
David Benjamina7f333d2015-02-09 02:37:18 -0500409 if (!SSL_CTX_set_ecdh_auto(ssl_ctx.get(), 1)) {
410 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400411 }
412
David Benjamina7f333d2015-02-09 02:37:18 -0500413 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
414 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400415 }
416
David Benjamina7f333d2015-02-09 02:37:18 -0500417 ScopedDH dh(DH_get_2048_256(NULL));
418 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
419 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400420 }
421
David Benjamin1b8b6912015-02-09 04:28:16 -0500422 if (config->async && config->is_server) {
423 // Disable the internal session cache. To test asynchronous session lookup,
424 // we use an external session cache.
425 SSL_CTX_set_session_cache_mode(
426 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500427 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500428 } else {
429 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
430 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400431
David Benjaminc273d2c2015-02-09 12:59:46 -0500432 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400433
David Benjamin1f5f62b2014-07-12 16:18:02 -0400434 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500435 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400436 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500437 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500438 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400439 }
440
441 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500442 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400443 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400444
David Benjamina08e49d2014-08-24 01:46:07 -0400445 ssl_ctx->tlsext_channel_id_enabled_new = 1;
David Benjaminc273d2c2015-02-09 12:59:46 -0500446 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400447
David Benjaminc273d2c2015-02-09 12:59:46 -0500448 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500449
David Benjamin87e4acd2015-04-02 19:57:35 -0400450 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
451
David Benjamin1d5c83e2014-07-22 19:20:02 -0400452 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400453}
454
David Benjamin40f101b2015-02-20 11:23:42 -0500455// RetryAsync is called after a failed operation on |ssl| with return code
456// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400457// event and returns true. Otherwise it returns false.
458static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400459 // No error; don't retry.
460 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500461 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400462 }
David Benjamin83f90402015-01-27 01:09:43 -0500463
David Benjamin6c2563e2015-04-03 03:47:47 -0400464 TestState *test_state = GetTestState(ssl);
465 if (test_state->clock_delta.tv_usec != 0 ||
466 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500467 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400468 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
469 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
470 test_state->clock.tv_usec %= 1000000;
471 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
472 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500473
474 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400475 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500476 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500477 }
David Benjamin40f101b2015-02-20 11:23:42 -0500478 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500479 }
480
David Benjamin43ec06f2014-08-05 02:28:57 -0400481 // See if we needed to read or write more. If so, allow one byte through on
482 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500483 switch (SSL_get_error(ssl, ret)) {
484 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400485 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500486 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500487 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400488 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500489 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500490 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
491 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
492 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500493 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500494 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400495 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500496 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500497 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500498 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400499 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500500 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500501 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400502 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500503 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500504 case SSL_ERROR_PENDING_CERTIFICATE:
505 // The handshake will resume without a second call to the early callback.
506 return InstallCertificate(ssl);
David Benjamind9e07012015-02-09 03:04:34 -0500507 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500508 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400509 }
David Benjamin025b3d32014-07-01 19:53:04 -0400510}
511
David Benjamin6c2563e2015-04-03 03:47:47 -0400512// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
513// the result value of the final |SSL_read| call.
514static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
515 const TestConfig *config = GetConfigPtr(ssl);
516 int ret;
517 do {
518 ret = SSL_read(ssl, out, max_out);
519 } while (config->async && RetryAsync(ssl, ret));
520 return ret;
521}
522
523// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
524// operations. It returns the result of the final |SSL_write| call.
525static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
526 const TestConfig *config = GetConfigPtr(ssl);
527 int ret;
528 do {
529 ret = SSL_write(ssl, in, in_len);
530 if (ret > 0) {
531 in += ret;
532 in_len -= ret;
533 }
534 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
535 return ret;
536}
537
David Benjamin87c8a642015-02-21 01:54:29 -0500538// DoExchange runs a test SSL exchange against the peer. On success, it returns
539// true and sets |*out_session| to the negotiated SSL session. If the test is a
540// resumption attempt, |is_resume| is true and |session| is the session from the
541// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500542static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
543 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500544 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500545 ScopedSSL ssl(SSL_new(ssl_ctx));
546 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500547 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400548 }
549
David Benjamina7f333d2015-02-09 02:37:18 -0500550 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -0500551 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500552 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800553 }
David Benjamin5a593af2014-08-11 19:51:50 -0400554
Adam Langley5f0efe02015-02-20 13:03:16 -0800555 if (config->fallback_scsv &&
556 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
557 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400558 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500559 if (!config->use_early_callback) {
560 if (config->async) {
561 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
562 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
563 } else if (!InstallCertificate(ssl.get())) {
564 return false;
565 }
David Benjamin5a593af2014-08-11 19:51:50 -0400566 }
567 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500568 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500569 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400570 }
571 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500572 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400573 }
574 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500575 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400576 }
577 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500578 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400579 }
580 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500581 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400582 }
583 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500584 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400585 }
586 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500587 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400588 }
589 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500590 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400591 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400592 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500593 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400594 }
David Benjaminca6554b2014-11-08 12:31:52 -0500595 if (config->allow_unsafe_legacy_renegotiation) {
David Benjamina7f333d2015-02-09 02:37:18 -0500596 SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
David Benjaminca6554b2014-11-08 12:31:52 -0500597 }
David Benjamina08e49d2014-08-24 01:46:07 -0400598 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500599 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400600 }
601 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500602 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500603 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500604 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500605 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
606 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500607 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500608 }
David Benjamina08e49d2014-08-24 01:46:07 -0400609 }
David Benjamina08e49d2014-08-24 01:46:07 -0400610 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500611 if (!config->host_name.empty() &&
612 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500613 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400614 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500615 if (!config->advertise_alpn.empty() &&
616 SSL_set_alpn_protos(ssl.get(),
617 (const uint8_t *)config->advertise_alpn.data(),
618 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500619 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400620 }
David Benjamin48cae082014-10-27 01:06:24 -0400621 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500622 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
623 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400624 }
David Benjamin61f95272014-11-25 01:55:35 -0500625 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500626 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500627 return false;
David Benjamin48cae082014-10-27 01:06:24 -0400628 }
David Benjamin61f95272014-11-25 01:55:35 -0500629 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500630 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500631 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500632 }
633 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -0500634 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500635 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500636 }
637 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -0500638 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500639 return false;
David Benjaminca6c8262014-11-15 19:06:08 -0500640 }
David Benjamina7f333d2015-02-09 02:37:18 -0500641 SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500642 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500643 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500644 }
645 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500646 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500647 }
David Benjamin13be1de2015-01-11 16:29:36 -0500648 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500649 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
650 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -0500651 }
Adam Langley524e7172015-02-20 16:04:00 -0800652 if (config->install_ddos_callback) {
653 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
654 }
David Benjamin67d1fb52015-03-16 15:16:23 -0400655 if (!config->cipher.empty() &&
656 !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
657 return false;
658 }
David Benjamin025b3d32014-07-01 19:53:04 -0400659
David Benjamin87c8a642015-02-21 01:54:29 -0500660 int sock = Connect(config->port);
661 if (sock == -1) {
662 return false;
663 }
664 SocketCloser closer(sock);
665
666 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -0500667 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -0500668 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -0400669 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400670 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400671 ScopedBIO packeted =
672 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -0500673 BIO_push(packeted.get(), bio.release());
674 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -0400675 }
David Benjamin5a593af2014-08-11 19:51:50 -0400676 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -0500677 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -0500678 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -0500679 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -0400680 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -0500681 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -0400682 }
David Benjamina7f333d2015-02-09 02:37:18 -0500683 SSL_set_bio(ssl.get(), bio.get(), bio.get());
684 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -0400685
David Benjamin1d5c83e2014-07-22 19:20:02 -0400686 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -0500687 if (!config->is_server) {
688 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500689 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -0500690 }
691 } else if (config->async) {
692 // The internal session cache is disabled, so install the session
693 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -0500694 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -0500695 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400696 }
697 }
698
699 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500700 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -0400701 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500702 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400703 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500704 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400705 }
David Benjamine0e7d0d2015-02-08 19:33:25 -0500706 } else {
707 do {
708 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500709 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500710 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500711 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500712 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400713 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamine0e7d0d2015-02-08 19:33:25 -0500714 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500715 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500716 }
David Benjamin025b3d32014-07-01 19:53:04 -0400717
David Benjamine0e7d0d2015-02-08 19:33:25 -0500718 if (is_resume &&
David Benjamina7f333d2015-02-09 02:37:18 -0500719 (!!SSL_session_reused(ssl.get()) == config->expect_session_miss)) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500720 fprintf(stderr, "session was%s reused\n",
David Benjamina7f333d2015-02-09 02:37:18 -0500721 SSL_session_reused(ssl.get()) ? "" : " not");
David Benjamin40f101b2015-02-20 11:23:42 -0500722 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500723 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400724
David Benjamin87e4acd2015-04-02 19:57:35 -0400725 bool expect_handshake_done = is_resume || !config->false_start;
726 if (expect_handshake_done != GetTestState(ssl.get())->handshake_done) {
727 fprintf(stderr, "handshake was%s completed\n",
728 GetTestState(ssl.get())->handshake_done ? "" : " not");
729 return false;
730 }
731
David Benjamin6f5c0f42015-02-24 01:23:21 -0500732 if (config->is_server && !GetTestState(ssl.get())->early_callback_called) {
733 fprintf(stderr, "early callback not called\n");
734 return false;
735 }
736
David Benjamine0e7d0d2015-02-08 19:33:25 -0500737 if (!config->expected_server_name.empty()) {
738 const char *server_name =
David Benjamina7f333d2015-02-09 02:37:18 -0500739 SSL_get_servername(ssl.get(), TLSEXT_NAMETYPE_host_name);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500740 if (server_name != config->expected_server_name) {
741 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
742 server_name, config->expected_server_name.c_str());
David Benjamin40f101b2015-02-20 11:23:42 -0500743 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500744 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400745 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400746
David Benjamine0e7d0d2015-02-08 19:33:25 -0500747 if (!config->expected_certificate_types.empty()) {
748 uint8_t *certificate_types;
749 int num_certificate_types =
David Benjamina7f333d2015-02-09 02:37:18 -0500750 SSL_get0_certificate_types(ssl.get(), &certificate_types);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500751 if (num_certificate_types !=
752 (int)config->expected_certificate_types.size() ||
753 memcmp(certificate_types,
754 config->expected_certificate_types.data(),
755 num_certificate_types) != 0) {
756 fprintf(stderr, "certificate types mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500757 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500758 }
David Benjamin7b030512014-07-08 17:30:11 -0400759 }
David Benjamin7b030512014-07-08 17:30:11 -0400760
David Benjamine0e7d0d2015-02-08 19:33:25 -0500761 if (!config->expected_next_proto.empty()) {
762 const uint8_t *next_proto;
763 unsigned next_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500764 SSL_get0_next_proto_negotiated(ssl.get(), &next_proto, &next_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500765 if (next_proto_len != config->expected_next_proto.size() ||
766 memcmp(next_proto, config->expected_next_proto.data(),
767 next_proto_len) != 0) {
768 fprintf(stderr, "negotiated next proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500769 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500770 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400771 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400772
David Benjamine0e7d0d2015-02-08 19:33:25 -0500773 if (!config->expected_alpn.empty()) {
774 const uint8_t *alpn_proto;
775 unsigned alpn_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500776 SSL_get0_alpn_selected(ssl.get(), &alpn_proto, &alpn_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500777 if (alpn_proto_len != config->expected_alpn.size() ||
778 memcmp(alpn_proto, config->expected_alpn.data(),
779 alpn_proto_len) != 0) {
780 fprintf(stderr, "negotiated alpn proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500781 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500782 }
David Benjaminae2888f2014-09-06 12:58:58 -0400783 }
David Benjaminae2888f2014-09-06 12:58:58 -0400784
David Benjamine0e7d0d2015-02-08 19:33:25 -0500785 if (!config->expected_channel_id.empty()) {
786 uint8_t channel_id[64];
David Benjamina7f333d2015-02-09 02:37:18 -0500787 if (!SSL_get_tls_channel_id(ssl.get(), channel_id, sizeof(channel_id))) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500788 fprintf(stderr, "no channel id negotiated\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500789 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500790 }
791 if (config->expected_channel_id.size() != 64 ||
792 memcmp(config->expected_channel_id.data(),
793 channel_id, 64) != 0) {
794 fprintf(stderr, "channel id mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500795 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500796 }
David Benjamina08e49d2014-08-24 01:46:07 -0400797 }
David Benjamina08e49d2014-08-24 01:46:07 -0400798
David Benjamine0e7d0d2015-02-08 19:33:25 -0500799 if (config->expect_extended_master_secret) {
800 if (!ssl->session->extended_master_secret) {
801 fprintf(stderr, "No EMS for session when expected");
David Benjamin40f101b2015-02-20 11:23:42 -0500802 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500803 }
Adam Langley75712922014-10-10 16:23:43 -0700804 }
Adam Langley75712922014-10-10 16:23:43 -0700805
David Benjamine0e7d0d2015-02-08 19:33:25 -0500806 if (!config->expected_ocsp_response.empty()) {
807 const uint8_t *data;
808 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500809 SSL_get0_ocsp_response(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500810 if (config->expected_ocsp_response.size() != len ||
811 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
812 fprintf(stderr, "OCSP response mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500813 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500814 }
David Benjamin61f95272014-11-25 01:55:35 -0500815 }
David Benjamin61f95272014-11-25 01:55:35 -0500816
David Benjamine0e7d0d2015-02-08 19:33:25 -0500817 if (!config->expected_signed_cert_timestamps.empty()) {
818 const uint8_t *data;
819 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500820 SSL_get0_signed_cert_timestamp_list(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500821 if (config->expected_signed_cert_timestamps.size() != len ||
822 memcmp(config->expected_signed_cert_timestamps.data(),
823 data, len) != 0) {
824 fprintf(stderr, "SCT list mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500825 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500826 }
David Benjamin61f95272014-11-25 01:55:35 -0500827 }
828 }
829
Adam Langley2ae77d22014-10-28 17:29:33 -0700830 if (config->renegotiate) {
831 if (config->async) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500832 fprintf(stderr, "-renegotiate is not supported with -async.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500833 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500834 }
835 if (config->implicit_handshake) {
836 fprintf(stderr, "-renegotiate is not supported with -implicit-handshake.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500837 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700838 }
839
David Benjamina7f333d2015-02-09 02:37:18 -0500840 SSL_renegotiate(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700841
David Benjamina7f333d2015-02-09 02:37:18 -0500842 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700843 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500844 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700845 }
846
David Benjamina7f333d2015-02-09 02:37:18 -0500847 SSL_set_state(ssl.get(), SSL_ST_ACCEPT);
848 ret = SSL_do_handshake(ssl.get());
Adam Langley2ae77d22014-10-28 17:29:33 -0700849 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500850 return false;
Adam Langley2ae77d22014-10-28 17:29:33 -0700851 }
852 }
853
David Benjaminc565ebb2015-04-03 04:06:36 -0400854 if (config->export_keying_material > 0) {
855 std::vector<uint8_t> result(
856 static_cast<size_t>(config->export_keying_material));
857 if (!SSL_export_keying_material(
858 ssl.get(), result.data(), result.size(),
859 config->export_label.data(), config->export_label.size(),
860 reinterpret_cast<const uint8_t*>(config->export_context.data()),
861 config->export_context.size(), config->use_export_context)) {
862 fprintf(stderr, "failed to export keying material\n");
863 return false;
864 }
865 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
866 return false;
867 }
868 }
869
David Benjamin5a593af2014-08-11 19:51:50 -0400870 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400871 if (config->is_dtls) {
872 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500873 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -0400874 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700875 // This mode writes a number of different record sizes in an attempt to
876 // trip up the CBC record splitting code.
877 uint8_t buf[32769];
878 memset(buf, 0x42, sizeof(buf));
879 static const size_t kRecordSizes[] = {
880 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
881 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
882 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700883 const size_t len = kRecordSizes[i];
Kenny Root7fdeaf12014-08-05 15:23:37 -0700884 if (len > sizeof(buf)) {
885 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500886 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700887 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400888 if (WriteAll(ssl.get(), buf, len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500889 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400890 }
891 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700892 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400893 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400894 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
895 5) < 0) {
896 return false;
897 }
David Benjamine58c4f52014-08-24 03:47:07 -0400898 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700899 for (;;) {
900 uint8_t buf[512];
David Benjamin6c2563e2015-04-03 03:47:47 -0400901 int n = DoRead(ssl.get(), buf, sizeof(buf));
David Benjamina7f333d2015-02-09 02:37:18 -0500902 int err = SSL_get_error(ssl.get(), n);
David Benjamin9a38e922015-01-22 16:06:11 -0500903 if (err == SSL_ERROR_ZERO_RETURN ||
904 (n == 0 && err == SSL_ERROR_SYSCALL)) {
905 if (n != 0) {
906 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500907 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500908 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500909 // Accept shutdowns with or without close_notify.
910 // TODO(davidben): Write tests which distinguish these two cases.
David Benjamin9a38e922015-01-22 16:06:11 -0500911 break;
912 } else if (err != SSL_ERROR_NONE) {
913 if (n > 0) {
914 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500915 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500916 }
David Benjamin40f101b2015-02-20 11:23:42 -0500917 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500918 }
David Benjaminc273d2c2015-02-09 12:59:46 -0500919 // Successfully read data.
David Benjamin9a38e922015-01-22 16:06:11 -0500920 if (n <= 0) {
921 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500922 return false;
David Benjamin9a38e922015-01-22 16:06:11 -0500923 }
David Benjamin87e4acd2015-04-02 19:57:35 -0400924
925 // After a successful read, with or without False Start, the handshake
926 // must be complete.
927 if (!GetTestState(ssl.get())->handshake_done) {
928 fprintf(stderr, "handshake was not completed after SSL_read\n");
929 return false;
930 }
931
David Benjamin9a38e922015-01-22 16:06:11 -0500932 for (int i = 0; i < n; i++) {
933 buf[i] ^= 0xff;
934 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400935 if (WriteAll(ssl.get(), buf, n) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500936 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700937 }
938 }
David Benjamin025b3d32014-07-01 19:53:04 -0400939 }
940
David Benjamin1d5c83e2014-07-22 19:20:02 -0400941 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500942 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400943 }
944
David Benjamina7f333d2015-02-09 02:37:18 -0500945 SSL_shutdown(ssl.get());
David Benjamin40f101b2015-02-20 11:23:42 -0500946 return true;
David Benjamin025b3d32014-07-01 19:53:04 -0400947}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400948
949int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -0500950#if defined(OPENSSL_WINDOWS)
951 /* Initialize Winsock. */
952 WORD wsa_version = MAKEWORD(2, 2);
953 WSADATA wsa_data;
954 int wsa_err = WSAStartup(wsa_version, &wsa_data);
955 if (wsa_err != 0) {
956 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
957 return 1;
958 }
959 if (wsa_data.wVersion != wsa_version) {
960 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
961 return 1;
962 }
963#else
David Benjamin1d5c83e2014-07-22 19:20:02 -0400964 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700965#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400966
David Benjamin5a593af2014-08-11 19:51:50 -0400967 if (!SSL_library_init()) {
968 return 1;
969 }
David Benjamind9e07012015-02-09 03:04:34 -0500970 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -0500971 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -0400972 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -0500973 return 1;
974 }
David Benjamin5a593af2014-08-11 19:51:50 -0400975
976 TestConfig config;
977 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500978 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400979 }
980
David Benjaminc273d2c2015-02-09 12:59:46 -0500981 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -0500982 if (!ssl_ctx) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400983 BIO_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400984 return 1;
985 }
986
David Benjamina7f333d2015-02-09 02:37:18 -0500987 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -0500988 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -0500989 NULL /* session */)) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400990 BIO_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -0500991 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400992 }
993
David Benjamin40f101b2015-02-20 11:23:42 -0500994 if (config.resume &&
995 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -0500996 session.get())) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400997 BIO_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -0500998 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400999 }
1000
David Benjamina7f333d2015-02-09 02:37:18 -05001001 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001002}