blob: 9e30bfce9516c85cca828a02673a35af9fd52010 [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>
David Benjamind98452d2015-06-16 14:16:23 -040041#include <openssl/cipher.h>
Brian Smith83a82982015-04-09 16:21:10 -100042#include <openssl/err.h>
David Benjamind98452d2015-06-16 14:16:23 -040043#include <openssl/hmac.h>
44#include <openssl/rand.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040045#include <openssl/ssl.h>
46
David Benjamin45fb1be2015-03-22 16:31:27 -040047#include <memory>
Steven Valdez0d62f262015-09-04 12:41:04 -040048#include <string>
David Benjaminc565ebb2015-04-03 04:06:36 -040049#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040050
51#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040052#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040053#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050054#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040055#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040056
David Benjamin87c8a642015-02-21 01:54:29 -050057
58#if !defined(OPENSSL_WINDOWS)
59static int closesocket(int sock) {
60 return close(sock);
61}
62
63static void PrintSocketError(const char *func) {
64 perror(func);
65}
66#else
67static void PrintSocketError(const char *func) {
68 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
69}
70#endif
71
David Benjaminc273d2c2015-02-09 12:59:46 -050072static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050073 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040074 return 1;
75}
David Benjamin025b3d32014-07-01 19:53:04 -040076
David Benjamin2d445c02015-02-09 13:03:50 -050077struct TestState {
David Benjaminff9c74f2015-04-06 20:17:56 -040078 TestState() {
79 // MSVC cannot initialize these inline.
80 memset(&clock, 0, sizeof(clock));
81 memset(&clock_delta, 0, sizeof(clock_delta));
82 }
83
David Benjamin6c2563e2015-04-03 03:47:47 -040084 // async_bio is async BIO which pauses reads and writes.
85 BIO *async_bio = nullptr;
86 // clock is the current time for the SSL connection.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040087 timeval clock;
David Benjamin6c2563e2015-04-03 03:47:47 -040088 // clock_delta is how far the clock advanced in the most recent failed
89 // |BIO_read|.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040090 timeval clock_delta;
David Benjamind9e07012015-02-09 03:04:34 -050091 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040092 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050093 ScopedSSL_SESSION session;
94 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040095 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040096 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040097 // private_key is the underlying private key used when testing custom keys.
98 ScopedEVP_PKEY private_key;
99 std::vector<uint8_t> signature;
100 // signature_retries is the number of times an asynchronous sign operation has
101 // been retried.
102 unsigned signature_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400103 bool got_new_session = false;
David Benjamind9e07012015-02-09 03:04:34 -0500104};
105
David Benjamin2d445c02015-02-09 13:03:50 -0500106static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700107 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500108 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500109}
110
111static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500112static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400113
Adam Langley69a01602014-11-17 17:26:55 -0800114static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500115 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400116}
117
David Benjamin87e4acd2015-04-02 19:57:35 -0400118static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500119 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400120}
121
David Benjamin2d445c02015-02-09 13:03:50 -0500122static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
123 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500124 async.release();
125 return true;
126 }
127 return false;
128}
129
David Benjamin87e4acd2015-04-02 19:57:35 -0400130static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500131 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500132}
133
David Benjamina7f333d2015-02-09 02:37:18 -0500134static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
135 ScopedBIO bio(BIO_new(BIO_s_file()));
136 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
137 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400138 }
David Benjamina7f333d2015-02-09 02:37:18 -0500139 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400140 return pkey;
141}
142
David Benjaminb4d65fd2015-05-29 17:11:21 -0400143static int AsyncPrivateKeyType(SSL *ssl) {
144 return EVP_PKEY_id(GetTestState(ssl)->private_key.get());
145}
146
David Benjaminb4d65fd2015-05-29 17:11:21 -0400147static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
148 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
149}
150
151static ssl_private_key_result_t AsyncPrivateKeySign(
152 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
153 const EVP_MD *md, const uint8_t *in, size_t in_len) {
154 TestState *test_state = GetTestState(ssl);
155 if (!test_state->signature.empty()) {
156 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
157 abort();
158 }
159
160 ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(test_state->private_key.get(),
161 nullptr));
162 if (!ctx) {
163 return ssl_private_key_failure;
164 }
165
166 // Write the signature into |test_state|.
167 size_t len = 0;
168 if (!EVP_PKEY_sign_init(ctx.get()) ||
169 !EVP_PKEY_CTX_set_signature_md(ctx.get(), md) ||
170 !EVP_PKEY_sign(ctx.get(), nullptr, &len, in, in_len)) {
171 return ssl_private_key_failure;
172 }
173 test_state->signature.resize(len);
174 if (!EVP_PKEY_sign(ctx.get(), bssl::vector_data(&test_state->signature), &len,
175 in, in_len)) {
176 return ssl_private_key_failure;
177 }
178 test_state->signature.resize(len);
179
180 // The signature will be released asynchronously in |AsyncPrivateKeySignComplete|.
181 return ssl_private_key_retry;
182}
183
184static ssl_private_key_result_t AsyncPrivateKeySignComplete(
185 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
186 TestState *test_state = GetTestState(ssl);
187 if (test_state->signature.empty()) {
188 fprintf(stderr,
189 "AsyncPrivateKeySignComplete called without operation pending.\n");
190 abort();
191 }
192
193 if (test_state->signature_retries < 2) {
194 // Only return the signature on the second attempt, to test both incomplete
195 // |sign| and |sign_complete|.
196 return ssl_private_key_retry;
197 }
198
199 if (max_out < test_state->signature.size()) {
200 fprintf(stderr, "Output buffer too small.\n");
201 return ssl_private_key_failure;
202 }
203 memcpy(out, bssl::vector_data(&test_state->signature),
204 test_state->signature.size());
nagendra modadugu601448a2015-07-24 09:31:31 -0700205 *out_len = test_state->signature.size();
David Benjaminb4d65fd2015-05-29 17:11:21 -0400206
207 test_state->signature.clear();
208 test_state->signature_retries = 0;
209 return ssl_private_key_success;
210}
211
212static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
213 AsyncPrivateKeyType,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400214 AsyncPrivateKeyMaxSignatureLen,
215 AsyncPrivateKeySign,
216 AsyncPrivateKeySignComplete,
217};
218
Steven Valdez0d62f262015-09-04 12:41:04 -0400219template<typename T>
220struct Free {
221 void operator()(T *buf) {
222 free(buf);
223 }
224};
225
David Benjamin41fdbcd2015-02-09 03:13:35 -0500226static bool InstallCertificate(SSL *ssl) {
227 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400228 TestState *test_state = GetTestState(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -0400229
230 if (!config->digest_prefs.empty()) {
231 std::unique_ptr<char, Free<char>> digest_prefs(
232 strdup(config->digest_prefs.c_str()));
233 char *tokptr = nullptr;
234 std::vector<int> digest_list;
235
236 for (;;) {
237 char *token = strtok_r(digest_list.empty() ? digest_prefs.get() : nullptr,
238 ",", &tokptr);
239 if (token == nullptr) {
240 break;
241 }
242
243 digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token)));
244 }
245
246 if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(),
247 digest_list.size())) {
248 return false;
249 }
250 }
251
David Benjaminb4d65fd2015-05-29 17:11:21 -0400252 if (!config->key_file.empty()) {
253 if (config->use_async_private_key) {
254 test_state->private_key = LoadPrivateKey(config->key_file.c_str());
255 if (!test_state->private_key) {
256 return false;
257 }
258 SSL_set_private_key_method(ssl, &g_async_private_key_method);
259 } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
260 SSL_FILETYPE_PEM)) {
261 return false;
262 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500263 }
264 if (!config->cert_file.empty() &&
265 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
266 SSL_FILETYPE_PEM)) {
267 return false;
268 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100269 if (!config->ocsp_response.empty() &&
270 !SSL_CTX_set_ocsp_response(ssl->ctx,
271 (const uint8_t *)config->ocsp_response.data(),
272 config->ocsp_response.size())) {
273 return false;
274 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500275 return true;
276}
277
David Benjaminc273d2c2015-02-09 12:59:46 -0500278static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400279 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500280 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400281
David Benjamin6f5c0f42015-02-24 01:23:21 -0500282 if (!config->expected_server_name.empty()) {
283 const uint8_t *extension_data;
284 size_t extension_len;
285 CBS extension, server_name_list, host_name;
286 uint8_t name_type;
287
288 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
289 &extension_data,
290 &extension_len)) {
291 fprintf(stderr, "Could not find server_name extension.\n");
292 return -1;
293 }
294
295 CBS_init(&extension, extension_data, extension_len);
296 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
297 CBS_len(&extension) != 0 ||
298 !CBS_get_u8(&server_name_list, &name_type) ||
299 name_type != TLSEXT_NAMETYPE_host_name ||
300 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
301 CBS_len(&server_name_list) != 0) {
302 fprintf(stderr, "Could not decode server_name extension.\n");
303 return -1;
304 }
305
306 if (!CBS_mem_equal(&host_name,
307 (const uint8_t*)config->expected_server_name.data(),
308 config->expected_server_name.size())) {
309 fprintf(stderr, "Server name mismatch.\n");
310 }
David Benjamin7b030512014-07-08 17:30:11 -0400311 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400312
David Benjamin6f5c0f42015-02-24 01:23:21 -0500313 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400314 return -1;
315 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400316
David Benjamin6f5c0f42015-02-24 01:23:21 -0500317 // Install the certificate in the early callback.
318 if (config->use_early_callback) {
319 if (config->async) {
320 // Install the certificate asynchronously.
321 return 0;
322 }
323 if (!InstallCertificate(ctx->ssl)) {
324 return -1;
325 }
David Benjamin7b030512014-07-08 17:30:11 -0400326 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400327 return 1;
328}
David Benjamin025b3d32014-07-01 19:53:04 -0400329
Paul Lietar8f1c2682015-08-18 12:21:54 +0100330static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
331 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
332 SSL_get_ex_data_X509_STORE_CTX_idx());
333 const TestConfig *config = GetConfigPtr(ssl);
334
335 if (!config->expected_ocsp_response.empty()) {
336 const uint8_t *data;
337 size_t len;
338 SSL_get0_ocsp_response(ssl, &data, &len);
339 if (len == 0) {
340 fprintf(stderr, "OCSP response not available in verify callback\n");
341 return 0;
342 }
343 }
344
David Benjamin67666e72014-07-12 15:47:52 -0400345 return 1;
346}
347
Paul Lietar8f1c2682015-08-18 12:21:54 +0100348static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
349 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
350 return 0;
351}
352
David Benjaminc273d2c2015-02-09 12:59:46 -0500353static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
354 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400355 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500356 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400357 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500358 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400359
David Benjamin5a593af2014-08-11 19:51:50 -0400360 *out = (const uint8_t*)config->advertise_npn.data();
361 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400362 return SSL_TLSEXT_ERR_OK;
363}
364
David Benjaminc273d2c2015-02-09 12:59:46 -0500365static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400366 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400367 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500368 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400369 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500370 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400371
David Benjamin5a593af2014-08-11 19:51:50 -0400372 *out = (uint8_t*)config->select_next_proto.data();
373 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400374 return SSL_TLSEXT_ERR_OK;
375}
376
David Benjaminc273d2c2015-02-09 12:59:46 -0500377static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
378 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400379 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500380 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400381 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500382 }
David Benjaminae2888f2014-09-06 12:58:58 -0400383
384 if (!config->expected_advertised_alpn.empty() &&
385 (config->expected_advertised_alpn.size() != inlen ||
386 memcmp(config->expected_advertised_alpn.data(),
387 in, inlen) != 0)) {
388 fprintf(stderr, "bad ALPN select callback inputs\n");
389 exit(1);
390 }
391
392 *out = (const uint8_t*)config->select_alpn.data();
393 *outlen = config->select_alpn.size();
394 return SSL_TLSEXT_ERR_OK;
395}
396
David Benjaminc273d2c2015-02-09 12:59:46 -0500397static unsigned PskClientCallback(SSL *ssl, const char *hint,
398 char *out_identity,
399 unsigned max_identity_len,
400 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400401 const TestConfig *config = GetConfigPtr(ssl);
402
403 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
404 fprintf(stderr, "Server PSK hint did not match.\n");
405 return 0;
406 }
407
408 // Account for the trailing '\0' for the identity.
409 if (config->psk_identity.size() >= max_identity_len ||
410 config->psk.size() > max_psk_len) {
411 fprintf(stderr, "PSK buffers too small\n");
412 return 0;
413 }
414
415 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
416 max_identity_len);
417 memcpy(out_psk, config->psk.data(), config->psk.size());
418 return config->psk.size();
419}
420
David Benjaminc273d2c2015-02-09 12:59:46 -0500421static unsigned PskServerCallback(SSL *ssl, const char *identity,
422 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400423 const TestConfig *config = GetConfigPtr(ssl);
424
425 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
426 fprintf(stderr, "Client PSK identity did not match.\n");
427 return 0;
428 }
429
430 if (config->psk.size() > max_psk_len) {
431 fprintf(stderr, "PSK buffers too small\n");
432 return 0;
433 }
434
435 memcpy(out_psk, config->psk.data(), config->psk.size());
436 return config->psk.size();
437}
438
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400439static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400440 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500441}
442
David Benjaminc273d2c2015-02-09 12:59:46 -0500443static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500444 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500445}
446
David Benjaminc273d2c2015-02-09 12:59:46 -0500447static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500448 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500449 return -1;
450 }
451 if (!InstallCertificate(ssl)) {
452 return 0;
453 }
454 return 1;
455}
456
David Benjaminc273d2c2015-02-09 12:59:46 -0500457static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
458 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500459 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500460 if (async_state->session) {
461 *copy = 0;
462 return async_state->session.release();
463 } else if (async_state->pending_session) {
464 return SSL_magic_pending_session_ptr();
465 } else {
466 return NULL;
467 }
468}
469
Adam Langley524e7172015-02-20 16:04:00 -0800470static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
471 const TestConfig *config = GetConfigPtr(early_context->ssl);
472 static int callback_num = 0;
473
474 callback_num++;
475 if (config->fail_ddos_callback ||
476 (config->fail_second_ddos_callback && callback_num == 2)) {
477 return 0;
478 }
479 return 1;
480}
481
David Benjamin87e4acd2015-04-02 19:57:35 -0400482static void InfoCallback(const SSL *ssl, int type, int val) {
483 if (type == SSL_CB_HANDSHAKE_DONE) {
484 if (GetConfigPtr(ssl)->handshake_never_done) {
485 fprintf(stderr, "handshake completed\n");
486 // Abort before any expected error code is printed, to ensure the overall
487 // test fails.
488 abort();
489 }
490 GetTestState(ssl)->handshake_done = true;
491 }
492}
493
David Benjaminba4594a2015-06-18 18:36:15 -0400494static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
495 GetTestState(ssl)->got_new_session = true;
496 // BoringSSL passes a reference to |session|.
497 SSL_SESSION_free(session);
498 return 1;
499}
500
David Benjamind98452d2015-06-16 14:16:23 -0400501static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
502 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
503 int encrypt) {
504 // This is just test code, so use the all-zeros key.
505 static const uint8_t kZeros[16] = {0};
506
507 if (encrypt) {
508 memcpy(key_name, kZeros, sizeof(kZeros));
509 RAND_bytes(iv, 16);
510 } else if (memcmp(key_name, kZeros, 16) != 0) {
511 return 0;
512 }
513
514 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
515 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
516 return -1;
517 }
518
519 if (!encrypt) {
520 return GetConfigPtr(ssl)->renew_ticket ? 2 : 1;
521 }
522 return 1;
523}
524
Adam Langley09505632015-07-30 18:10:13 -0700525// kCustomExtensionValue is the extension value that the custom extension
526// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700527static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700528static void *const kCustomExtensionAddArg =
529 reinterpret_cast<void *>(kCustomExtensionValue);
530static void *const kCustomExtensionParseArg =
531 reinterpret_cast<void *>(kCustomExtensionValue + 1);
532static const char kCustomExtensionContents[] = "custom extension";
533
534static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
535 const uint8_t **out, size_t *out_len,
536 int *out_alert_value, void *add_arg) {
537 if (extension_value != kCustomExtensionValue ||
538 add_arg != kCustomExtensionAddArg) {
539 abort();
540 }
541
542 if (GetConfigPtr(ssl)->custom_extension_skip) {
543 return 0;
544 }
545 if (GetConfigPtr(ssl)->custom_extension_fail_add) {
546 return -1;
547 }
548
549 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
550 *out_len = sizeof(kCustomExtensionContents) - 1;
551
552 return 1;
553}
554
555static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
556 const uint8_t *out, void *add_arg) {
557 if (extension_value != kCustomExtensionValue ||
558 add_arg != kCustomExtensionAddArg ||
559 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
560 abort();
561 }
562}
563
564static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
565 const uint8_t *contents,
566 size_t contents_len,
567 int *out_alert_value, void *parse_arg) {
568 if (extension_value != kCustomExtensionValue ||
569 parse_arg != kCustomExtensionParseArg) {
570 abort();
571 }
572
573 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
574 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
575 *out_alert_value = SSL_AD_DECODE_ERROR;
576 return 0;
577 }
578
579 return 1;
580}
581
David Benjamin87c8a642015-02-21 01:54:29 -0500582// Connect returns a new socket connected to localhost on |port| or -1 on
583// error.
584static int Connect(uint16_t port) {
585 int sock = socket(AF_INET, SOCK_STREAM, 0);
586 if (sock == -1) {
587 PrintSocketError("socket");
588 return -1;
589 }
590 int nodelay = 1;
591 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
592 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
593 PrintSocketError("setsockopt");
594 closesocket(sock);
595 return -1;
596 }
597 sockaddr_in sin;
598 memset(&sin, 0, sizeof(sin));
599 sin.sin_family = AF_INET;
600 sin.sin_port = htons(port);
601 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
602 PrintSocketError("inet_pton");
603 closesocket(sock);
604 return -1;
605 }
606 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
607 sizeof(sin)) != 0) {
608 PrintSocketError("connect");
609 closesocket(sock);
610 return -1;
611 }
612 return sock;
613}
614
615class SocketCloser {
616 public:
617 explicit SocketCloser(int sock) : sock_(sock) {}
618 ~SocketCloser() {
619 // Half-close and drain the socket before releasing it. This seems to be
620 // necessary for graceful shutdown on Windows. It will also avoid write
621 // failures in the test runner.
622#if defined(OPENSSL_WINDOWS)
623 shutdown(sock_, SD_SEND);
624#else
625 shutdown(sock_, SHUT_WR);
626#endif
627 while (true) {
628 char buf[1024];
629 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
630 break;
631 }
632 }
633 closesocket(sock_);
634 }
635
636 private:
637 const int sock_;
638};
639
David Benjaminc273d2c2015-02-09 12:59:46 -0500640static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500641 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
642 config->is_dtls ? DTLS_method() : TLS_method()));
643 if (!ssl_ctx) {
644 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400645 }
646
Adam Langleycef75832015-09-03 14:51:12 -0700647 std::string cipher_list = "ALL";
648 if (!config->cipher.empty()) {
649 cipher_list = config->cipher;
650 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
651 }
652 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
653 return nullptr;
654 }
655
656 if (!config->cipher_tls10.empty() &&
657 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
658 config->cipher_tls10.c_str())) {
659 return nullptr;
660 }
661 if (!config->cipher_tls11.empty() &&
662 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
663 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500664 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400665 }
666
David Benjamina7f333d2015-02-09 02:37:18 -0500667 ScopedDH dh(DH_get_2048_256(NULL));
668 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
669 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400670 }
671
David Benjamin1b8b6912015-02-09 04:28:16 -0500672 if (config->async && config->is_server) {
673 // Disable the internal session cache. To test asynchronous session lookup,
674 // we use an external session cache.
675 SSL_CTX_set_session_cache_mode(
676 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500677 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500678 } else {
679 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
680 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400681
David Benjaminc273d2c2015-02-09 12:59:46 -0500682 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400683
David Benjamin1f5f62b2014-07-12 16:18:02 -0400684 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500685 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400686 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500687 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500688 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400689 }
690
691 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500692 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400693 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400694
Adam Langley49c7af12015-07-10 14:33:46 -0700695 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500696 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400697
David Benjaminc273d2c2015-02-09 12:59:46 -0500698 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500699
David Benjamin87e4acd2015-04-02 19:57:35 -0400700 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400701 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400702
David Benjamind98452d2015-06-16 14:16:23 -0400703 if (config->use_ticket_callback) {
704 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
705 }
706
Adam Langley09505632015-07-30 18:10:13 -0700707 if (config->enable_client_custom_extension &&
708 !SSL_CTX_add_client_custom_ext(
709 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
710 CustomExtensionFreeCallback, kCustomExtensionAddArg,
711 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
712 return nullptr;
713 }
714
715 if (config->enable_server_custom_extension &&
716 !SSL_CTX_add_server_custom_ext(
717 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
718 CustomExtensionFreeCallback, kCustomExtensionAddArg,
719 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
720 return nullptr;
721 }
722
Paul Lietar8f1c2682015-08-18 12:21:54 +0100723 if (config->verify_fail) {
724 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
725 } else {
726 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
727 }
728
Paul Lietar4fac72e2015-09-09 13:44:55 +0100729 if (!config->signed_cert_timestamps.empty() &&
730 !SSL_CTX_set_signed_cert_timestamp_list(
731 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
732 config->signed_cert_timestamps.size())) {
733 return nullptr;
734 }
735
David Benjamin1d5c83e2014-07-22 19:20:02 -0400736 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400737}
738
David Benjamin40f101b2015-02-20 11:23:42 -0500739// RetryAsync is called after a failed operation on |ssl| with return code
740// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400741// event and returns true. Otherwise it returns false.
742static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400743 // No error; don't retry.
744 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500745 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400746 }
David Benjamin83f90402015-01-27 01:09:43 -0500747
David Benjamin6c2563e2015-04-03 03:47:47 -0400748 TestState *test_state = GetTestState(ssl);
749 if (test_state->clock_delta.tv_usec != 0 ||
750 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500751 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400752 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
753 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
754 test_state->clock.tv_usec %= 1000000;
755 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
756 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500757
758 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400759 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500760 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500761 }
David Benjamin40f101b2015-02-20 11:23:42 -0500762 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500763 }
764
David Benjamin43ec06f2014-08-05 02:28:57 -0400765 // See if we needed to read or write more. If so, allow one byte through on
766 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500767 switch (SSL_get_error(ssl, ret)) {
768 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400769 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500770 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500771 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400772 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500773 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500774 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
775 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
776 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500777 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500778 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400779 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500780 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500781 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500782 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400783 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500784 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500785 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400786 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500787 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500788 case SSL_ERROR_PENDING_CERTIFICATE:
789 // The handshake will resume without a second call to the early callback.
790 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400791 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
792 test_state->signature_retries++;
793 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500794 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500795 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400796 }
David Benjamin025b3d32014-07-01 19:53:04 -0400797}
798
David Benjamin6c2563e2015-04-03 03:47:47 -0400799// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
800// the result value of the final |SSL_read| call.
801static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
802 const TestConfig *config = GetConfigPtr(ssl);
803 int ret;
804 do {
805 ret = SSL_read(ssl, out, max_out);
806 } while (config->async && RetryAsync(ssl, ret));
807 return ret;
808}
809
810// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
811// operations. It returns the result of the final |SSL_write| call.
812static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
813 const TestConfig *config = GetConfigPtr(ssl);
814 int ret;
815 do {
816 ret = SSL_write(ssl, in, in_len);
817 if (ret > 0) {
818 in += ret;
819 in_len -= ret;
820 }
821 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
822 return ret;
823}
824
David Benjamin30789da2015-08-29 22:56:45 -0400825// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
826// returns the result of the final |SSL_shutdown| call.
827static int DoShutdown(SSL *ssl) {
828 const TestConfig *config = GetConfigPtr(ssl);
829 int ret;
830 do {
831 ret = SSL_shutdown(ssl);
832 } while (config->async && RetryAsync(ssl, ret));
833 return ret;
834}
835
David Benjamin91eab5c2015-06-18 18:35:46 -0400836// CheckHandshakeProperties checks, immediately after |ssl| completes its
837// initial handshake (or False Starts), whether all the properties are
838// consistent with the test configuration and invariants.
839static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
840 const TestConfig *config = GetConfigPtr(ssl);
841
842 if (SSL_get_current_cipher(ssl) == nullptr) {
843 fprintf(stderr, "null cipher after handshake\n");
844 return false;
845 }
846
847 if (is_resume &&
848 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
849 fprintf(stderr, "session was%s reused\n",
850 SSL_session_reused(ssl) ? "" : " not");
851 return false;
852 }
853
854 bool expect_handshake_done = is_resume || !config->false_start;
855 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
856 fprintf(stderr, "handshake was%s completed\n",
857 GetTestState(ssl)->handshake_done ? "" : " not");
858 return false;
859 }
860
David Benjaminba4594a2015-06-18 18:36:15 -0400861 if (expect_handshake_done && !config->is_server) {
862 bool expect_new_session =
863 !config->expect_no_session &&
864 (!SSL_session_reused(ssl) || config->expect_ticket_renewal);
865 if (expect_new_session != GetTestState(ssl)->got_new_session) {
866 fprintf(stderr,
867 "new session was%s established, but we expected the opposite\n",
868 GetTestState(ssl)->got_new_session ? "" : " not");
869 return false;
870 }
871 }
872
David Benjamin91eab5c2015-06-18 18:35:46 -0400873 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
874 fprintf(stderr, "early callback not called\n");
875 return false;
876 }
877
878 if (!config->expected_server_name.empty()) {
879 const char *server_name =
880 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
881 if (server_name != config->expected_server_name) {
882 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
883 server_name, config->expected_server_name.c_str());
884 return false;
885 }
886 }
887
888 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -0400889 const uint8_t *certificate_types;
890 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -0400891 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -0400892 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -0400893 memcmp(certificate_types,
894 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -0400895 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -0400896 fprintf(stderr, "certificate types mismatch\n");
897 return false;
898 }
899 }
900
901 if (!config->expected_next_proto.empty()) {
902 const uint8_t *next_proto;
903 unsigned next_proto_len;
904 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
905 if (next_proto_len != config->expected_next_proto.size() ||
906 memcmp(next_proto, config->expected_next_proto.data(),
907 next_proto_len) != 0) {
908 fprintf(stderr, "negotiated next proto mismatch\n");
909 return false;
910 }
911 }
912
913 if (!config->expected_alpn.empty()) {
914 const uint8_t *alpn_proto;
915 unsigned alpn_proto_len;
916 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
917 if (alpn_proto_len != config->expected_alpn.size() ||
918 memcmp(alpn_proto, config->expected_alpn.data(),
919 alpn_proto_len) != 0) {
920 fprintf(stderr, "negotiated alpn proto mismatch\n");
921 return false;
922 }
923 }
924
925 if (!config->expected_channel_id.empty()) {
926 uint8_t channel_id[64];
927 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
928 fprintf(stderr, "no channel id negotiated\n");
929 return false;
930 }
931 if (config->expected_channel_id.size() != 64 ||
932 memcmp(config->expected_channel_id.data(),
933 channel_id, 64) != 0) {
934 fprintf(stderr, "channel id mismatch\n");
935 return false;
936 }
937 }
938
939 if (config->expect_extended_master_secret) {
940 if (!ssl->session->extended_master_secret) {
941 fprintf(stderr, "No EMS for session when expected");
942 return false;
943 }
944 }
945
946 if (!config->expected_ocsp_response.empty()) {
947 const uint8_t *data;
948 size_t len;
949 SSL_get0_ocsp_response(ssl, &data, &len);
950 if (config->expected_ocsp_response.size() != len ||
951 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
952 fprintf(stderr, "OCSP response mismatch\n");
953 return false;
954 }
955 }
956
957 if (!config->expected_signed_cert_timestamps.empty()) {
958 const uint8_t *data;
959 size_t len;
960 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
961 if (config->expected_signed_cert_timestamps.size() != len ||
962 memcmp(config->expected_signed_cert_timestamps.data(),
963 data, len) != 0) {
964 fprintf(stderr, "SCT list mismatch\n");
965 return false;
966 }
967 }
968
Paul Lietar8f1c2682015-08-18 12:21:54 +0100969 if (config->expect_verify_result) {
970 int expected_verify_result = config->verify_fail ?
971 X509_V_ERR_APPLICATION_VERIFICATION :
972 X509_V_OK;
973
974 if (SSL_get_verify_result(ssl) != expected_verify_result) {
975 fprintf(stderr, "Wrong certificate verification result\n");
976 return false;
977 }
978 }
979
David Benjamin91eab5c2015-06-18 18:35:46 -0400980 if (!config->is_server) {
981 /* Clients should expect a peer certificate chain iff this was not a PSK
982 * cipher suite. */
983 if (config->psk.empty()) {
984 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
985 fprintf(stderr, "Missing peer certificate chain!\n");
986 return false;
987 }
988 } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
989 fprintf(stderr, "Unexpected peer certificate chain!\n");
990 return false;
991 }
992 }
993 return true;
994}
995
David Benjamin87c8a642015-02-21 01:54:29 -0500996// DoExchange runs a test SSL exchange against the peer. On success, it returns
997// true and sets |*out_session| to the negotiated SSL session. If the test is a
998// resumption attempt, |is_resume| is true and |session| is the session from the
999// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -05001000static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
1001 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -05001002 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001003 ScopedSSL ssl(SSL_new(ssl_ctx));
1004 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001005 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001006 }
1007
David Benjamina7f333d2015-02-09 02:37:18 -05001008 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001009 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001010 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001011 }
David Benjamin5a593af2014-08-11 19:51:50 -04001012
Adam Langley5f0efe02015-02-20 13:03:16 -08001013 if (config->fallback_scsv &&
1014 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1015 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001016 }
David Benjamin6f5c0f42015-02-24 01:23:21 -05001017 if (!config->use_early_callback) {
1018 if (config->async) {
1019 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
1020 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
1021 } else if (!InstallCertificate(ssl.get())) {
1022 return false;
1023 }
David Benjamin5a593af2014-08-11 19:51:50 -04001024 }
1025 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001026 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001027 NULL);
1028 }
1029 if (config->verify_peer) {
1030 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001031 }
1032 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001033 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001034 }
1035 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001036 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001037 }
1038 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001039 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001040 }
1041 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001042 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001043 }
1044 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001045 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001046 }
1047 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001048 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001049 }
1050 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001051 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001052 }
David Benjamin5c24a1d2014-08-31 00:59:27 -04001053 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -05001054 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -04001055 }
David Benjamin2c99d282015-09-01 10:23:00 -04001056 if (config->microsoft_big_sslv3_buffer) {
1057 SSL_set_options(ssl.get(), SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
1058 }
David Benjamincff0b902015-05-15 23:09:47 -04001059 if (config->no_legacy_server_connect) {
1060 SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
1061 }
David Benjamina08e49d2014-08-24 01:46:07 -04001062 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001063 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001064 }
1065 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001066 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001067 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001068 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -05001069 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
1070 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001071 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001072 }
David Benjamina08e49d2014-08-24 01:46:07 -04001073 }
David Benjamina08e49d2014-08-24 01:46:07 -04001074 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001075 if (!config->host_name.empty() &&
1076 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001077 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001078 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001079 if (!config->advertise_alpn.empty() &&
1080 SSL_set_alpn_protos(ssl.get(),
1081 (const uint8_t *)config->advertise_alpn.data(),
1082 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001083 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001084 }
David Benjamin48cae082014-10-27 01:06:24 -04001085 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001086 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1087 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001088 }
David Benjamin61f95272014-11-25 01:55:35 -05001089 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001090 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001091 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001092 }
David Benjamin61f95272014-11-25 01:55:35 -05001093 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001094 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001095 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001096 }
1097 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001098 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001099 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001100 }
1101 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001102 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001103 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001104 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001105 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001106 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001107 }
1108 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001109 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001110 }
David Benjamin13be1de2015-01-11 16:29:36 -05001111 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001112 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1113 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001114 }
Adam Langley524e7172015-02-20 16:04:00 -08001115 if (config->install_ddos_callback) {
1116 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1117 }
David Benjamin897e5e02015-05-12 17:03:54 -04001118 if (!config->reject_peer_renegotiations) {
1119 /* Renegotiations are disabled by default. */
1120 SSL_set_reject_peer_renegotiations(ssl.get(), 0);
David Benjaminb16346b2015-04-08 19:16:58 -04001121 }
David Benjamin30789da2015-08-29 22:56:45 -04001122 if (!config->check_close_notify) {
1123 SSL_set_quiet_shutdown(ssl.get(), 1);
1124 }
David Benjamin025b3d32014-07-01 19:53:04 -04001125
David Benjamin87c8a642015-02-21 01:54:29 -05001126 int sock = Connect(config->port);
1127 if (sock == -1) {
1128 return false;
1129 }
1130 SocketCloser closer(sock);
1131
1132 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001133 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001134 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001135 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001136 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001137 ScopedBIO packeted =
1138 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -05001139 BIO_push(packeted.get(), bio.release());
1140 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001141 }
David Benjamin5a593af2014-08-11 19:51:50 -04001142 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001143 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001144 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -05001145 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001146 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001147 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001148 }
David Benjamina7f333d2015-02-09 02:37:18 -05001149 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1150 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001151
David Benjamin1d5c83e2014-07-22 19:20:02 -04001152 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001153 if (!config->is_server) {
1154 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001155 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001156 }
1157 } else if (config->async) {
1158 // The internal session cache is disabled, so install the session
1159 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -05001160 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -05001161 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001162 }
1163 }
1164
David Benjamina07c0fc2015-05-13 13:19:42 -04001165 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1166 fprintf(stderr, "non-null cipher before handshake\n");
1167 return false;
1168 }
1169
David Benjamin1d5c83e2014-07-22 19:20:02 -04001170 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001171 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001172 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001173 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001174 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001175 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001176 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001177 } else {
1178 do {
1179 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001180 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001181 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001182 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001183 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001184 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001185 if (ret != 1 ||
1186 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001187 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001188 }
David Benjamin025b3d32014-07-01 19:53:04 -04001189
David Benjaminba4594a2015-06-18 18:36:15 -04001190 // Reset the state to assert later that the callback isn't called in
1191 // renegotations.
1192 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001193 }
1194
David Benjaminc565ebb2015-04-03 04:06:36 -04001195 if (config->export_keying_material > 0) {
1196 std::vector<uint8_t> result(
1197 static_cast<size_t>(config->export_keying_material));
1198 if (!SSL_export_keying_material(
1199 ssl.get(), result.data(), result.size(),
1200 config->export_label.data(), config->export_label.size(),
1201 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1202 config->export_context.size(), config->use_export_context)) {
1203 fprintf(stderr, "failed to export keying material\n");
1204 return false;
1205 }
1206 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1207 return false;
1208 }
1209 }
1210
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001211 if (config->tls_unique) {
1212 uint8_t tls_unique[16];
1213 size_t tls_unique_len;
1214 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1215 sizeof(tls_unique))) {
1216 fprintf(stderr, "failed to get tls-unique\n");
1217 return false;
1218 }
1219
1220 if (tls_unique_len != 12) {
1221 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1222 static_cast<unsigned>(tls_unique_len));
1223 return false;
1224 }
1225
1226 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1227 return false;
1228 }
1229 }
1230
David Benjamin5a593af2014-08-11 19:51:50 -04001231 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001232 if (config->is_dtls) {
1233 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001234 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001235 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001236 // This mode writes a number of different record sizes in an attempt to
1237 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001238 static const size_t kBufLen = 32769;
1239 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1240 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001241 static const size_t kRecordSizes[] = {
1242 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1243 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1244 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001245 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001246 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001247 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001248 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001249 }
Adam Langleybc949292015-06-18 21:32:44 -07001250 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001251 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001252 }
1253 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001254 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001255 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001256 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1257 5) < 0) {
1258 return false;
1259 }
David Benjamine58c4f52014-08-24 03:47:07 -04001260 }
David Benjamin30789da2015-08-29 22:56:45 -04001261 if (!config->shim_shuts_down) {
1262 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001263 static const size_t kBufLen = 16384;
1264 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1265
David Benjamin2c99d282015-09-01 10:23:00 -04001266 // Read only 512 bytes at a time in TLS to ensure records may be
1267 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001268 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001269 int err = SSL_get_error(ssl.get(), n);
1270 if (err == SSL_ERROR_ZERO_RETURN ||
1271 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1272 if (n != 0) {
1273 fprintf(stderr, "Invalid SSL_get_error output\n");
1274 return false;
1275 }
1276 // Stop on either clean or unclean shutdown.
1277 break;
1278 } else if (err != SSL_ERROR_NONE) {
1279 if (n > 0) {
1280 fprintf(stderr, "Invalid SSL_get_error output\n");
1281 return false;
1282 }
1283 return false;
1284 }
1285 // Successfully read data.
1286 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001287 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001288 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001289 }
David Benjamin30789da2015-08-29 22:56:45 -04001290
1291 // After a successful read, with or without False Start, the handshake
1292 // must be complete.
1293 if (!GetTestState(ssl.get())->handshake_done) {
1294 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001295 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001296 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001297
David Benjamin30789da2015-08-29 22:56:45 -04001298 for (int i = 0; i < n; i++) {
1299 buf[i] ^= 0xff;
1300 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001301 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001302 return false;
1303 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001304 }
1305 }
David Benjamin025b3d32014-07-01 19:53:04 -04001306 }
1307
David Benjaminba4594a2015-06-18 18:36:15 -04001308 if (!config->is_server && !config->false_start &&
1309 !config->implicit_handshake &&
1310 GetTestState(ssl.get())->got_new_session) {
1311 fprintf(stderr, "new session was established after the handshake\n");
1312 return false;
1313 }
1314
David Benjamin1d5c83e2014-07-22 19:20:02 -04001315 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001316 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001317 }
1318
David Benjamin30789da2015-08-29 22:56:45 -04001319 ret = DoShutdown(ssl.get());
1320
1321 if (config->shim_shuts_down && config->check_close_notify) {
1322 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1323 // it returns zero when our close_notify is sent, then one when the peer's
1324 // is received.
1325 if (ret != 0) {
1326 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1327 return false;
1328 }
1329 ret = DoShutdown(ssl.get());
1330 }
1331
1332 if (ret != 1) {
1333 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1334 return false;
1335 }
1336
David Benjamin40f101b2015-02-20 11:23:42 -05001337 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001338}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001339
1340int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -05001341#if defined(OPENSSL_WINDOWS)
1342 /* Initialize Winsock. */
1343 WORD wsa_version = MAKEWORD(2, 2);
1344 WSADATA wsa_data;
1345 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1346 if (wsa_err != 0) {
1347 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1348 return 1;
1349 }
1350 if (wsa_data.wVersion != wsa_version) {
1351 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1352 return 1;
1353 }
1354#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001355 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001356#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001357
David Benjamin5a593af2014-08-11 19:51:50 -04001358 if (!SSL_library_init()) {
1359 return 1;
1360 }
David Benjamind9e07012015-02-09 03:04:34 -05001361 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001362 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001363 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001364 return 1;
1365 }
David Benjamin5a593af2014-08-11 19:51:50 -04001366
1367 TestConfig config;
1368 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001369 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001370 }
1371
David Benjaminc273d2c2015-02-09 12:59:46 -05001372 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001373 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001374 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001375 return 1;
1376 }
1377
David Benjamina7f333d2015-02-09 02:37:18 -05001378 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001379 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001380 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001381 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001382 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001383 }
1384
David Benjamin40f101b2015-02-20 11:23:42 -05001385 if (config.resume &&
1386 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001387 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001388 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001389 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001390 }
1391
David Benjamina7f333d2015-02-09 02:37:18 -05001392 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001393}