blob: f4ae9828790777f6628106d5187ecf0792029471 [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 Benjamin4d2e7ce2015-05-08 13:29:45 -040023#include <sys/types.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040024#include <unistd.h>
David Benjamin87c8a642015-02-21 01:54:29 -050025#else
26#include <io.h>
27#pragma warning(push, 3)
Adam Langley3e719312015-03-20 16:32:23 -070028#include <winsock2.h>
29#include <ws2tcpip.h>
David Benjamin87c8a642015-02-21 01:54:29 -050030#pragma warning(pop)
31
32#pragma comment(lib, "Ws2_32.lib")
Adam Langleyded93582014-07-31 15:23:51 -070033#endif
34
Adam Langley2b2d66d2015-01-30 17:08:37 -080035#include <string.h>
Adam Langleyded93582014-07-31 15:23:51 -070036#include <sys/types.h>
David Benjamin025b3d32014-07-01 19:53:04 -040037
David Benjamin025b3d32014-07-01 19:53:04 -040038#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040039#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040040#include <openssl/bytestring.h>
Brian Smith83a82982015-04-09 16:21:10 -100041#include <openssl/err.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040042#include <openssl/ssl.h>
43
David Benjamin45fb1be2015-03-22 16:31:27 -040044#include <memory>
David Benjaminc565ebb2015-04-03 04:06:36 -040045#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040046
47#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040048#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040049#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050050#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040051#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040052
David Benjamin87c8a642015-02-21 01:54:29 -050053
54#if !defined(OPENSSL_WINDOWS)
55static int closesocket(int sock) {
56 return close(sock);
57}
58
59static void PrintSocketError(const char *func) {
60 perror(func);
61}
62#else
63static void PrintSocketError(const char *func) {
64 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
65}
66#endif
67
David Benjaminc273d2c2015-02-09 12:59:46 -050068static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050069 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040070 return 1;
71}
David Benjamin025b3d32014-07-01 19:53:04 -040072
David Benjamin2d445c02015-02-09 13:03:50 -050073struct TestState {
David Benjaminff9c74f2015-04-06 20:17:56 -040074 TestState() {
75 // MSVC cannot initialize these inline.
76 memset(&clock, 0, sizeof(clock));
77 memset(&clock_delta, 0, sizeof(clock_delta));
78 }
79
David Benjamin6c2563e2015-04-03 03:47:47 -040080 // async_bio is async BIO which pauses reads and writes.
81 BIO *async_bio = nullptr;
82 // clock is the current time for the SSL connection.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040083 timeval clock;
David Benjamin6c2563e2015-04-03 03:47:47 -040084 // clock_delta is how far the clock advanced in the most recent failed
85 // |BIO_read|.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040086 timeval clock_delta;
David Benjamind9e07012015-02-09 03:04:34 -050087 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040088 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050089 ScopedSSL_SESSION session;
90 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040091 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040092 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040093 // private_key is the underlying private key used when testing custom keys.
94 ScopedEVP_PKEY private_key;
95 std::vector<uint8_t> signature;
96 // signature_retries is the number of times an asynchronous sign operation has
97 // been retried.
98 unsigned signature_retries = 0;
David Benjamind9e07012015-02-09 03:04:34 -050099};
100
David Benjamin2d445c02015-02-09 13:03:50 -0500101static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
102 int index, long argl, void *argp) {
103 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500104}
105
106static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500107static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400108
Adam Langley69a01602014-11-17 17:26:55 -0800109static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500110 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400111}
112
David Benjamin87e4acd2015-04-02 19:57:35 -0400113static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500114 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400115}
116
David Benjamin2d445c02015-02-09 13:03:50 -0500117static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
118 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500119 async.release();
120 return true;
121 }
122 return false;
123}
124
David Benjamin87e4acd2015-04-02 19:57:35 -0400125static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500126 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500127}
128
David Benjamina7f333d2015-02-09 02:37:18 -0500129static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
130 ScopedBIO bio(BIO_new(BIO_s_file()));
131 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
132 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400133 }
David Benjamina7f333d2015-02-09 02:37:18 -0500134 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400135 return pkey;
136}
137
David Benjaminb4d65fd2015-05-29 17:11:21 -0400138static int AsyncPrivateKeyType(SSL *ssl) {
139 return EVP_PKEY_id(GetTestState(ssl)->private_key.get());
140}
141
142static int AsyncPrivateKeySupportsDigest(SSL *ssl, const EVP_MD *md) {
143 return EVP_PKEY_supports_digest(GetTestState(ssl)->private_key.get(), md);
144}
145
146static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
147 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
148}
149
150static ssl_private_key_result_t AsyncPrivateKeySign(
151 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
152 const EVP_MD *md, const uint8_t *in, size_t in_len) {
153 TestState *test_state = GetTestState(ssl);
154 if (!test_state->signature.empty()) {
155 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
156 abort();
157 }
158
159 ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(test_state->private_key.get(),
160 nullptr));
161 if (!ctx) {
162 return ssl_private_key_failure;
163 }
164
165 // Write the signature into |test_state|.
166 size_t len = 0;
167 if (!EVP_PKEY_sign_init(ctx.get()) ||
168 !EVP_PKEY_CTX_set_signature_md(ctx.get(), md) ||
169 !EVP_PKEY_sign(ctx.get(), nullptr, &len, in, in_len)) {
170 return ssl_private_key_failure;
171 }
172 test_state->signature.resize(len);
173 if (!EVP_PKEY_sign(ctx.get(), bssl::vector_data(&test_state->signature), &len,
174 in, in_len)) {
175 return ssl_private_key_failure;
176 }
177 test_state->signature.resize(len);
178
179 // The signature will be released asynchronously in |AsyncPrivateKeySignComplete|.
180 return ssl_private_key_retry;
181}
182
183static ssl_private_key_result_t AsyncPrivateKeySignComplete(
184 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
185 TestState *test_state = GetTestState(ssl);
186 if (test_state->signature.empty()) {
187 fprintf(stderr,
188 "AsyncPrivateKeySignComplete called without operation pending.\n");
189 abort();
190 }
191
192 if (test_state->signature_retries < 2) {
193 // Only return the signature on the second attempt, to test both incomplete
194 // |sign| and |sign_complete|.
195 return ssl_private_key_retry;
196 }
197
198 if (max_out < test_state->signature.size()) {
199 fprintf(stderr, "Output buffer too small.\n");
200 return ssl_private_key_failure;
201 }
202 memcpy(out, bssl::vector_data(&test_state->signature),
203 test_state->signature.size());
204
205 test_state->signature.clear();
206 test_state->signature_retries = 0;
207 return ssl_private_key_success;
208}
209
210static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
211 AsyncPrivateKeyType,
212 AsyncPrivateKeySupportsDigest,
213 AsyncPrivateKeyMaxSignatureLen,
214 AsyncPrivateKeySign,
215 AsyncPrivateKeySignComplete,
216};
217
David Benjamin41fdbcd2015-02-09 03:13:35 -0500218static bool InstallCertificate(SSL *ssl) {
219 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400220 TestState *test_state = GetTestState(ssl);
221 if (!config->key_file.empty()) {
222 if (config->use_async_private_key) {
223 test_state->private_key = LoadPrivateKey(config->key_file.c_str());
224 if (!test_state->private_key) {
225 return false;
226 }
227 SSL_set_private_key_method(ssl, &g_async_private_key_method);
228 } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
229 SSL_FILETYPE_PEM)) {
230 return false;
231 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500232 }
233 if (!config->cert_file.empty() &&
234 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
235 SSL_FILETYPE_PEM)) {
236 return false;
237 }
238 return true;
239}
240
David Benjaminc273d2c2015-02-09 12:59:46 -0500241static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400242 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500243 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400244
David Benjamin6f5c0f42015-02-24 01:23:21 -0500245 if (!config->expected_server_name.empty()) {
246 const uint8_t *extension_data;
247 size_t extension_len;
248 CBS extension, server_name_list, host_name;
249 uint8_t name_type;
250
251 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
252 &extension_data,
253 &extension_len)) {
254 fprintf(stderr, "Could not find server_name extension.\n");
255 return -1;
256 }
257
258 CBS_init(&extension, extension_data, extension_len);
259 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
260 CBS_len(&extension) != 0 ||
261 !CBS_get_u8(&server_name_list, &name_type) ||
262 name_type != TLSEXT_NAMETYPE_host_name ||
263 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
264 CBS_len(&server_name_list) != 0) {
265 fprintf(stderr, "Could not decode server_name extension.\n");
266 return -1;
267 }
268
269 if (!CBS_mem_equal(&host_name,
270 (const uint8_t*)config->expected_server_name.data(),
271 config->expected_server_name.size())) {
272 fprintf(stderr, "Server name mismatch.\n");
273 }
David Benjamin7b030512014-07-08 17:30:11 -0400274 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400275
David Benjamin6f5c0f42015-02-24 01:23:21 -0500276 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400277 return -1;
278 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400279
David Benjamin6f5c0f42015-02-24 01:23:21 -0500280 // Install the certificate in the early callback.
281 if (config->use_early_callback) {
282 if (config->async) {
283 // Install the certificate asynchronously.
284 return 0;
285 }
286 if (!InstallCertificate(ctx->ssl)) {
287 return -1;
288 }
David Benjamin7b030512014-07-08 17:30:11 -0400289 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290 return 1;
291}
David Benjamin025b3d32014-07-01 19:53:04 -0400292
David Benjaminc273d2c2015-02-09 12:59:46 -0500293static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400294 return 1;
295}
296
David Benjaminc273d2c2015-02-09 12:59:46 -0500297static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
298 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400299 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500300 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400301 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500302 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400303
David Benjamin5a593af2014-08-11 19:51:50 -0400304 *out = (const uint8_t*)config->advertise_npn.data();
305 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400306 return SSL_TLSEXT_ERR_OK;
307}
308
David Benjaminc273d2c2015-02-09 12:59:46 -0500309static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400310 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400311 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500312 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400313 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500314 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400315
David Benjamin5a593af2014-08-11 19:51:50 -0400316 *out = (uint8_t*)config->select_next_proto.data();
317 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400318 return SSL_TLSEXT_ERR_OK;
319}
320
David Benjaminc273d2c2015-02-09 12:59:46 -0500321static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
322 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400323 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500324 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400325 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500326 }
David Benjaminae2888f2014-09-06 12:58:58 -0400327
328 if (!config->expected_advertised_alpn.empty() &&
329 (config->expected_advertised_alpn.size() != inlen ||
330 memcmp(config->expected_advertised_alpn.data(),
331 in, inlen) != 0)) {
332 fprintf(stderr, "bad ALPN select callback inputs\n");
333 exit(1);
334 }
335
336 *out = (const uint8_t*)config->select_alpn.data();
337 *outlen = config->select_alpn.size();
338 return SSL_TLSEXT_ERR_OK;
339}
340
David Benjaminc273d2c2015-02-09 12:59:46 -0500341static unsigned PskClientCallback(SSL *ssl, const char *hint,
342 char *out_identity,
343 unsigned max_identity_len,
344 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400345 const TestConfig *config = GetConfigPtr(ssl);
346
347 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
348 fprintf(stderr, "Server PSK hint did not match.\n");
349 return 0;
350 }
351
352 // Account for the trailing '\0' for the identity.
353 if (config->psk_identity.size() >= max_identity_len ||
354 config->psk.size() > max_psk_len) {
355 fprintf(stderr, "PSK buffers too small\n");
356 return 0;
357 }
358
359 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
360 max_identity_len);
361 memcpy(out_psk, config->psk.data(), config->psk.size());
362 return config->psk.size();
363}
364
David Benjaminc273d2c2015-02-09 12:59:46 -0500365static unsigned PskServerCallback(SSL *ssl, const char *identity,
366 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400367 const TestConfig *config = GetConfigPtr(ssl);
368
369 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
370 fprintf(stderr, "Client PSK identity did not match.\n");
371 return 0;
372 }
373
374 if (config->psk.size() > max_psk_len) {
375 fprintf(stderr, "PSK buffers too small\n");
376 return 0;
377 }
378
379 memcpy(out_psk, config->psk.data(), config->psk.size());
380 return config->psk.size();
381}
382
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400383static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400384 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500385}
386
David Benjaminc273d2c2015-02-09 12:59:46 -0500387static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500388 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500389}
390
David Benjaminc273d2c2015-02-09 12:59:46 -0500391static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500392 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500393 return -1;
394 }
395 if (!InstallCertificate(ssl)) {
396 return 0;
397 }
398 return 1;
399}
400
David Benjaminc273d2c2015-02-09 12:59:46 -0500401static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
402 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500403 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500404 if (async_state->session) {
405 *copy = 0;
406 return async_state->session.release();
407 } else if (async_state->pending_session) {
408 return SSL_magic_pending_session_ptr();
409 } else {
410 return NULL;
411 }
412}
413
Adam Langley524e7172015-02-20 16:04:00 -0800414static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
415 const TestConfig *config = GetConfigPtr(early_context->ssl);
416 static int callback_num = 0;
417
418 callback_num++;
419 if (config->fail_ddos_callback ||
420 (config->fail_second_ddos_callback && callback_num == 2)) {
421 return 0;
422 }
423 return 1;
424}
425
David Benjamin87e4acd2015-04-02 19:57:35 -0400426static void InfoCallback(const SSL *ssl, int type, int val) {
427 if (type == SSL_CB_HANDSHAKE_DONE) {
428 if (GetConfigPtr(ssl)->handshake_never_done) {
429 fprintf(stderr, "handshake completed\n");
430 // Abort before any expected error code is printed, to ensure the overall
431 // test fails.
432 abort();
433 }
434 GetTestState(ssl)->handshake_done = true;
435 }
436}
437
David Benjamin87c8a642015-02-21 01:54:29 -0500438// Connect returns a new socket connected to localhost on |port| or -1 on
439// error.
440static int Connect(uint16_t port) {
441 int sock = socket(AF_INET, SOCK_STREAM, 0);
442 if (sock == -1) {
443 PrintSocketError("socket");
444 return -1;
445 }
446 int nodelay = 1;
447 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
448 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
449 PrintSocketError("setsockopt");
450 closesocket(sock);
451 return -1;
452 }
453 sockaddr_in sin;
454 memset(&sin, 0, sizeof(sin));
455 sin.sin_family = AF_INET;
456 sin.sin_port = htons(port);
457 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
458 PrintSocketError("inet_pton");
459 closesocket(sock);
460 return -1;
461 }
462 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
463 sizeof(sin)) != 0) {
464 PrintSocketError("connect");
465 closesocket(sock);
466 return -1;
467 }
468 return sock;
469}
470
471class SocketCloser {
472 public:
473 explicit SocketCloser(int sock) : sock_(sock) {}
474 ~SocketCloser() {
475 // Half-close and drain the socket before releasing it. This seems to be
476 // necessary for graceful shutdown on Windows. It will also avoid write
477 // failures in the test runner.
478#if defined(OPENSSL_WINDOWS)
479 shutdown(sock_, SD_SEND);
480#else
481 shutdown(sock_, SHUT_WR);
482#endif
483 while (true) {
484 char buf[1024];
485 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
486 break;
487 }
488 }
489 closesocket(sock_);
490 }
491
492 private:
493 const int sock_;
494};
495
David Benjaminc273d2c2015-02-09 12:59:46 -0500496static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500497 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
498 config->is_dtls ? DTLS_method() : TLS_method()));
499 if (!ssl_ctx) {
500 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400501 }
502
David Benjamina7f333d2015-02-09 02:37:18 -0500503 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
504 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400505 }
506
David Benjamina7f333d2015-02-09 02:37:18 -0500507 ScopedDH dh(DH_get_2048_256(NULL));
508 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
509 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400510 }
511
David Benjamin1b8b6912015-02-09 04:28:16 -0500512 if (config->async && config->is_server) {
513 // Disable the internal session cache. To test asynchronous session lookup,
514 // we use an external session cache.
515 SSL_CTX_set_session_cache_mode(
516 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500517 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500518 } else {
519 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
520 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400521
David Benjaminc273d2c2015-02-09 12:59:46 -0500522 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400523
David Benjamin1f5f62b2014-07-12 16:18:02 -0400524 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500525 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400526 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500527 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500528 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400529 }
530
531 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500532 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400533 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400534
David Benjamina08e49d2014-08-24 01:46:07 -0400535 ssl_ctx->tlsext_channel_id_enabled_new = 1;
David Benjaminc273d2c2015-02-09 12:59:46 -0500536 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400537
David Benjaminc273d2c2015-02-09 12:59:46 -0500538 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500539
David Benjamin87e4acd2015-04-02 19:57:35 -0400540 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
541
David Benjamin1d5c83e2014-07-22 19:20:02 -0400542 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400543}
544
David Benjamin40f101b2015-02-20 11:23:42 -0500545// RetryAsync is called after a failed operation on |ssl| with return code
546// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400547// event and returns true. Otherwise it returns false.
548static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400549 // No error; don't retry.
550 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500551 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400552 }
David Benjamin83f90402015-01-27 01:09:43 -0500553
David Benjamin6c2563e2015-04-03 03:47:47 -0400554 TestState *test_state = GetTestState(ssl);
555 if (test_state->clock_delta.tv_usec != 0 ||
556 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500557 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400558 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
559 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
560 test_state->clock.tv_usec %= 1000000;
561 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
562 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500563
564 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400565 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500566 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500567 }
David Benjamin40f101b2015-02-20 11:23:42 -0500568 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500569 }
570
David Benjamin43ec06f2014-08-05 02:28:57 -0400571 // See if we needed to read or write more. If so, allow one byte through on
572 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500573 switch (SSL_get_error(ssl, ret)) {
574 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400575 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500576 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500577 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400578 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500579 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500580 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
581 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
582 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500583 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500584 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400585 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500586 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500587 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500588 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400589 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500590 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500591 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400592 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500593 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500594 case SSL_ERROR_PENDING_CERTIFICATE:
595 // The handshake will resume without a second call to the early callback.
596 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400597 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
598 test_state->signature_retries++;
599 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500600 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500601 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400602 }
David Benjamin025b3d32014-07-01 19:53:04 -0400603}
604
David Benjamin6c2563e2015-04-03 03:47:47 -0400605// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
606// the result value of the final |SSL_read| call.
607static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
608 const TestConfig *config = GetConfigPtr(ssl);
609 int ret;
610 do {
611 ret = SSL_read(ssl, out, max_out);
612 } while (config->async && RetryAsync(ssl, ret));
613 return ret;
614}
615
616// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
617// operations. It returns the result of the final |SSL_write| call.
618static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
619 const TestConfig *config = GetConfigPtr(ssl);
620 int ret;
621 do {
622 ret = SSL_write(ssl, in, in_len);
623 if (ret > 0) {
624 in += ret;
625 in_len -= ret;
626 }
627 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
628 return ret;
629}
630
David Benjamin87c8a642015-02-21 01:54:29 -0500631// DoExchange runs a test SSL exchange against the peer. On success, it returns
632// true and sets |*out_session| to the negotiated SSL session. If the test is a
633// resumption attempt, |is_resume| is true and |session| is the session from the
634// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500635static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
636 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500637 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500638 ScopedSSL ssl(SSL_new(ssl_ctx));
639 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500640 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400641 }
642
David Benjamina7f333d2015-02-09 02:37:18 -0500643 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -0500644 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500645 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800646 }
David Benjamin5a593af2014-08-11 19:51:50 -0400647
Adam Langley5f0efe02015-02-20 13:03:16 -0800648 if (config->fallback_scsv &&
649 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
650 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400651 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500652 if (!config->use_early_callback) {
653 if (config->async) {
654 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
655 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
656 } else if (!InstallCertificate(ssl.get())) {
657 return false;
658 }
David Benjamin5a593af2014-08-11 19:51:50 -0400659 }
660 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500661 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500662 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400663 }
664 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500665 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400666 }
667 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500668 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400669 }
670 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500671 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400672 }
673 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500674 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400675 }
676 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500677 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400678 }
679 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500680 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400681 }
682 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500683 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400684 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400685 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500686 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400687 }
David Benjaminca6554b2014-11-08 12:31:52 -0500688 if (config->allow_unsafe_legacy_renegotiation) {
David Benjamina7f333d2015-02-09 02:37:18 -0500689 SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
David Benjaminca6554b2014-11-08 12:31:52 -0500690 }
David Benjamincff0b902015-05-15 23:09:47 -0400691 if (config->no_legacy_server_connect) {
692 SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
693 }
David Benjamina08e49d2014-08-24 01:46:07 -0400694 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500695 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400696 }
697 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500698 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500699 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500700 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500701 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
702 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500703 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500704 }
David Benjamina08e49d2014-08-24 01:46:07 -0400705 }
David Benjamina08e49d2014-08-24 01:46:07 -0400706 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500707 if (!config->host_name.empty() &&
708 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500709 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400710 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500711 if (!config->advertise_alpn.empty() &&
712 SSL_set_alpn_protos(ssl.get(),
713 (const uint8_t *)config->advertise_alpn.data(),
714 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500715 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400716 }
David Benjamin48cae082014-10-27 01:06:24 -0400717 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500718 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
719 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400720 }
David Benjamin61f95272014-11-25 01:55:35 -0500721 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500722 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500723 return false;
David Benjamin48cae082014-10-27 01:06:24 -0400724 }
David Benjamin61f95272014-11-25 01:55:35 -0500725 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500726 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500727 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500728 }
729 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -0500730 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500731 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500732 }
733 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -0500734 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500735 return false;
David Benjaminca6c8262014-11-15 19:06:08 -0500736 }
David Benjamina7f333d2015-02-09 02:37:18 -0500737 SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500738 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500739 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500740 }
741 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500742 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -0500743 }
David Benjamin13be1de2015-01-11 16:29:36 -0500744 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500745 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
746 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -0500747 }
Adam Langley524e7172015-02-20 16:04:00 -0800748 if (config->install_ddos_callback) {
749 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
750 }
David Benjamin67d1fb52015-03-16 15:16:23 -0400751 if (!config->cipher.empty() &&
752 !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
753 return false;
754 }
David Benjamin897e5e02015-05-12 17:03:54 -0400755 if (!config->reject_peer_renegotiations) {
756 /* Renegotiations are disabled by default. */
757 SSL_set_reject_peer_renegotiations(ssl.get(), 0);
David Benjaminb16346b2015-04-08 19:16:58 -0400758 }
David Benjamin025b3d32014-07-01 19:53:04 -0400759
David Benjamin87c8a642015-02-21 01:54:29 -0500760 int sock = Connect(config->port);
761 if (sock == -1) {
762 return false;
763 }
764 SocketCloser closer(sock);
765
766 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -0500767 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -0500768 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -0400769 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400770 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400771 ScopedBIO packeted =
772 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -0500773 BIO_push(packeted.get(), bio.release());
774 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -0400775 }
David Benjamin5a593af2014-08-11 19:51:50 -0400776 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -0500777 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -0500778 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -0500779 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -0400780 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -0500781 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -0400782 }
David Benjamina7f333d2015-02-09 02:37:18 -0500783 SSL_set_bio(ssl.get(), bio.get(), bio.get());
784 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -0400785
David Benjamin1d5c83e2014-07-22 19:20:02 -0400786 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -0500787 if (!config->is_server) {
788 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500789 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -0500790 }
791 } else if (config->async) {
792 // The internal session cache is disabled, so install the session
793 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -0500794 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -0500795 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -0400796 }
797 }
798
David Benjamina07c0fc2015-05-13 13:19:42 -0400799 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
800 fprintf(stderr, "non-null cipher before handshake\n");
801 return false;
802 }
803
David Benjamin1d5c83e2014-07-22 19:20:02 -0400804 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500805 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -0400806 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500807 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400808 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500809 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -0400810 }
David Benjamine0e7d0d2015-02-08 19:33:25 -0500811 } else {
812 do {
813 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -0500814 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500815 } else {
David Benjamina7f333d2015-02-09 02:37:18 -0500816 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -0500817 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400818 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamine0e7d0d2015-02-08 19:33:25 -0500819 if (ret != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -0500820 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500821 }
David Benjamin025b3d32014-07-01 19:53:04 -0400822
David Benjamina07c0fc2015-05-13 13:19:42 -0400823 if (SSL_get_current_cipher(ssl.get()) == nullptr) {
824 fprintf(stderr, "null cipher after handshake\n");
825 return false;
826 }
827
David Benjamine0e7d0d2015-02-08 19:33:25 -0500828 if (is_resume &&
David Benjamina7f333d2015-02-09 02:37:18 -0500829 (!!SSL_session_reused(ssl.get()) == config->expect_session_miss)) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500830 fprintf(stderr, "session was%s reused\n",
David Benjamina7f333d2015-02-09 02:37:18 -0500831 SSL_session_reused(ssl.get()) ? "" : " not");
David Benjamin40f101b2015-02-20 11:23:42 -0500832 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500833 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400834
David Benjamin87e4acd2015-04-02 19:57:35 -0400835 bool expect_handshake_done = is_resume || !config->false_start;
836 if (expect_handshake_done != GetTestState(ssl.get())->handshake_done) {
837 fprintf(stderr, "handshake was%s completed\n",
838 GetTestState(ssl.get())->handshake_done ? "" : " not");
839 return false;
840 }
841
David Benjamin6f5c0f42015-02-24 01:23:21 -0500842 if (config->is_server && !GetTestState(ssl.get())->early_callback_called) {
843 fprintf(stderr, "early callback not called\n");
844 return false;
845 }
846
David Benjamine0e7d0d2015-02-08 19:33:25 -0500847 if (!config->expected_server_name.empty()) {
848 const char *server_name =
David Benjamina7f333d2015-02-09 02:37:18 -0500849 SSL_get_servername(ssl.get(), TLSEXT_NAMETYPE_host_name);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500850 if (server_name != config->expected_server_name) {
851 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
852 server_name, config->expected_server_name.c_str());
David Benjamin40f101b2015-02-20 11:23:42 -0500853 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500854 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400855 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400856
David Benjamine0e7d0d2015-02-08 19:33:25 -0500857 if (!config->expected_certificate_types.empty()) {
858 uint8_t *certificate_types;
859 int num_certificate_types =
David Benjamina7f333d2015-02-09 02:37:18 -0500860 SSL_get0_certificate_types(ssl.get(), &certificate_types);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500861 if (num_certificate_types !=
862 (int)config->expected_certificate_types.size() ||
863 memcmp(certificate_types,
864 config->expected_certificate_types.data(),
865 num_certificate_types) != 0) {
866 fprintf(stderr, "certificate types mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500867 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500868 }
David Benjamin7b030512014-07-08 17:30:11 -0400869 }
David Benjamin7b030512014-07-08 17:30:11 -0400870
David Benjamine0e7d0d2015-02-08 19:33:25 -0500871 if (!config->expected_next_proto.empty()) {
872 const uint8_t *next_proto;
873 unsigned next_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500874 SSL_get0_next_proto_negotiated(ssl.get(), &next_proto, &next_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500875 if (next_proto_len != config->expected_next_proto.size() ||
876 memcmp(next_proto, config->expected_next_proto.data(),
877 next_proto_len) != 0) {
878 fprintf(stderr, "negotiated next proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500879 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500880 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400881 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400882
David Benjamine0e7d0d2015-02-08 19:33:25 -0500883 if (!config->expected_alpn.empty()) {
884 const uint8_t *alpn_proto;
885 unsigned alpn_proto_len;
David Benjamina7f333d2015-02-09 02:37:18 -0500886 SSL_get0_alpn_selected(ssl.get(), &alpn_proto, &alpn_proto_len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500887 if (alpn_proto_len != config->expected_alpn.size() ||
888 memcmp(alpn_proto, config->expected_alpn.data(),
889 alpn_proto_len) != 0) {
890 fprintf(stderr, "negotiated alpn proto mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500891 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500892 }
David Benjaminae2888f2014-09-06 12:58:58 -0400893 }
David Benjaminae2888f2014-09-06 12:58:58 -0400894
David Benjamine0e7d0d2015-02-08 19:33:25 -0500895 if (!config->expected_channel_id.empty()) {
896 uint8_t channel_id[64];
David Benjamina7f333d2015-02-09 02:37:18 -0500897 if (!SSL_get_tls_channel_id(ssl.get(), channel_id, sizeof(channel_id))) {
David Benjamine0e7d0d2015-02-08 19:33:25 -0500898 fprintf(stderr, "no channel id negotiated\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500899 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500900 }
901 if (config->expected_channel_id.size() != 64 ||
902 memcmp(config->expected_channel_id.data(),
903 channel_id, 64) != 0) {
904 fprintf(stderr, "channel id mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500905 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500906 }
David Benjamina08e49d2014-08-24 01:46:07 -0400907 }
David Benjamina08e49d2014-08-24 01:46:07 -0400908
David Benjamine0e7d0d2015-02-08 19:33:25 -0500909 if (config->expect_extended_master_secret) {
910 if (!ssl->session->extended_master_secret) {
911 fprintf(stderr, "No EMS for session when expected");
David Benjamin40f101b2015-02-20 11:23:42 -0500912 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500913 }
Adam Langley75712922014-10-10 16:23:43 -0700914 }
Adam Langley75712922014-10-10 16:23:43 -0700915
David Benjamine0e7d0d2015-02-08 19:33:25 -0500916 if (!config->expected_ocsp_response.empty()) {
917 const uint8_t *data;
918 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500919 SSL_get0_ocsp_response(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500920 if (config->expected_ocsp_response.size() != len ||
921 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
922 fprintf(stderr, "OCSP response mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500923 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500924 }
David Benjamin61f95272014-11-25 01:55:35 -0500925 }
David Benjamin61f95272014-11-25 01:55:35 -0500926
David Benjamine0e7d0d2015-02-08 19:33:25 -0500927 if (!config->expected_signed_cert_timestamps.empty()) {
928 const uint8_t *data;
929 size_t len;
David Benjamina7f333d2015-02-09 02:37:18 -0500930 SSL_get0_signed_cert_timestamp_list(ssl.get(), &data, &len);
David Benjamine0e7d0d2015-02-08 19:33:25 -0500931 if (config->expected_signed_cert_timestamps.size() != len ||
932 memcmp(config->expected_signed_cert_timestamps.data(),
933 data, len) != 0) {
934 fprintf(stderr, "SCT list mismatch\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500935 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -0500936 }
David Benjamin61f95272014-11-25 01:55:35 -0500937 }
David Benjamin680ca962015-06-18 12:37:23 -0400938
939 if (!config->is_server) {
940 /* Clients should expect a peer certificate chain iff this was not a PSK
941 * cipher suite. */
942 if (config->psk.empty()) {
943 if (SSL_get_peer_cert_chain(ssl.get()) == nullptr) {
944 fprintf(stderr, "Missing peer certificate chain!\n");
945 return false;
946 }
947 } else if (SSL_get_peer_cert_chain(ssl.get()) != nullptr) {
948 fprintf(stderr, "Unexpected peer certificate chain!\n");
949 return false;
950 }
951 }
David Benjamin61f95272014-11-25 01:55:35 -0500952 }
953
David Benjaminc565ebb2015-04-03 04:06:36 -0400954 if (config->export_keying_material > 0) {
955 std::vector<uint8_t> result(
956 static_cast<size_t>(config->export_keying_material));
957 if (!SSL_export_keying_material(
958 ssl.get(), result.data(), result.size(),
959 config->export_label.data(), config->export_label.size(),
960 reinterpret_cast<const uint8_t*>(config->export_context.data()),
961 config->export_context.size(), config->use_export_context)) {
962 fprintf(stderr, "failed to export keying material\n");
963 return false;
964 }
965 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
966 return false;
967 }
968 }
969
Adam Langleyaf0e32c2015-06-03 09:57:23 -0700970 if (config->tls_unique) {
971 uint8_t tls_unique[16];
972 size_t tls_unique_len;
973 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
974 sizeof(tls_unique))) {
975 fprintf(stderr, "failed to get tls-unique\n");
976 return false;
977 }
978
979 if (tls_unique_len != 12) {
980 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
981 static_cast<unsigned>(tls_unique_len));
982 return false;
983 }
984
985 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
986 return false;
987 }
988 }
989
David Benjamin5a593af2014-08-11 19:51:50 -0400990 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400991 if (config->is_dtls) {
992 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500993 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -0400994 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700995 // This mode writes a number of different record sizes in an attempt to
996 // trip up the CBC record splitting code.
997 uint8_t buf[32769];
998 memset(buf, 0x42, sizeof(buf));
999 static const size_t kRecordSizes[] = {
1000 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1001 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1002 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001003 const size_t len = kRecordSizes[i];
Kenny Root7fdeaf12014-08-05 15:23:37 -07001004 if (len > sizeof(buf)) {
1005 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001006 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001007 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001008 if (WriteAll(ssl.get(), buf, len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001009 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001010 }
1011 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001012 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001013 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001014 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1015 5) < 0) {
1016 return false;
1017 }
David Benjamine58c4f52014-08-24 03:47:07 -04001018 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001019 for (;;) {
1020 uint8_t buf[512];
David Benjamin6c2563e2015-04-03 03:47:47 -04001021 int n = DoRead(ssl.get(), buf, sizeof(buf));
David Benjamina7f333d2015-02-09 02:37:18 -05001022 int err = SSL_get_error(ssl.get(), n);
David Benjamin9a38e922015-01-22 16:06:11 -05001023 if (err == SSL_ERROR_ZERO_RETURN ||
1024 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1025 if (n != 0) {
1026 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001027 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001028 }
David Benjaminc273d2c2015-02-09 12:59:46 -05001029 // Accept shutdowns with or without close_notify.
1030 // TODO(davidben): Write tests which distinguish these two cases.
David Benjamin9a38e922015-01-22 16:06:11 -05001031 break;
1032 } else if (err != SSL_ERROR_NONE) {
1033 if (n > 0) {
1034 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001035 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001036 }
David Benjamin40f101b2015-02-20 11:23:42 -05001037 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001038 }
David Benjaminc273d2c2015-02-09 12:59:46 -05001039 // Successfully read data.
David Benjamin9a38e922015-01-22 16:06:11 -05001040 if (n <= 0) {
1041 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001042 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001043 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001044
1045 // After a successful read, with or without False Start, the handshake
1046 // must be complete.
1047 if (!GetTestState(ssl.get())->handshake_done) {
1048 fprintf(stderr, "handshake was not completed after SSL_read\n");
1049 return false;
1050 }
1051
David Benjamin9a38e922015-01-22 16:06:11 -05001052 for (int i = 0; i < n; i++) {
1053 buf[i] ^= 0xff;
1054 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001055 if (WriteAll(ssl.get(), buf, n) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001056 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001057 }
1058 }
David Benjamin025b3d32014-07-01 19:53:04 -04001059 }
1060
David Benjamin1d5c83e2014-07-22 19:20:02 -04001061 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001062 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001063 }
1064
David Benjamina7f333d2015-02-09 02:37:18 -05001065 SSL_shutdown(ssl.get());
David Benjamin40f101b2015-02-20 11:23:42 -05001066 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001067}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001068
1069int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -05001070#if defined(OPENSSL_WINDOWS)
1071 /* Initialize Winsock. */
1072 WORD wsa_version = MAKEWORD(2, 2);
1073 WSADATA wsa_data;
1074 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1075 if (wsa_err != 0) {
1076 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1077 return 1;
1078 }
1079 if (wsa_data.wVersion != wsa_version) {
1080 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1081 return 1;
1082 }
1083#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001084 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001085#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001086
David Benjamin5a593af2014-08-11 19:51:50 -04001087 if (!SSL_library_init()) {
1088 return 1;
1089 }
David Benjamind9e07012015-02-09 03:04:34 -05001090 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001091 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001092 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001093 return 1;
1094 }
David Benjamin5a593af2014-08-11 19:51:50 -04001095
1096 TestConfig config;
1097 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001098 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001099 }
1100
David Benjaminc273d2c2015-02-09 12:59:46 -05001101 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001102 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001103 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001104 return 1;
1105 }
1106
David Benjamina7f333d2015-02-09 02:37:18 -05001107 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001108 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001109 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001110 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001111 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001112 }
1113
David Benjamin40f101b2015-02-20 11:23:42 -05001114 if (config.resume &&
1115 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001116 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001117 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001118 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001119 }
1120
David Benjamina7f333d2015-02-09 02:37:18 -05001121 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001122}