blob: 8d1f37b64abaf250e31452ebf4c5d0c7220e58ea [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()));
Steven Valdez0d62f262015-09-04 12:41:04 -0400233 std::vector<int> digest_list;
234
235 for (;;) {
Adam Langley67251f22015-09-23 15:01:07 -0700236 char *token =
237 strtok(digest_list.empty() ? digest_prefs.get() : nullptr, ",");
Steven Valdez0d62f262015-09-04 12:41:04 -0400238 if (token == nullptr) {
239 break;
240 }
241
242 digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token)));
243 }
244
245 if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(),
246 digest_list.size())) {
247 return false;
248 }
249 }
250
David Benjaminb4d65fd2015-05-29 17:11:21 -0400251 if (!config->key_file.empty()) {
252 if (config->use_async_private_key) {
253 test_state->private_key = LoadPrivateKey(config->key_file.c_str());
254 if (!test_state->private_key) {
255 return false;
256 }
257 SSL_set_private_key_method(ssl, &g_async_private_key_method);
258 } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
259 SSL_FILETYPE_PEM)) {
260 return false;
261 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500262 }
263 if (!config->cert_file.empty() &&
264 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
265 SSL_FILETYPE_PEM)) {
266 return false;
267 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100268 if (!config->ocsp_response.empty() &&
269 !SSL_CTX_set_ocsp_response(ssl->ctx,
270 (const uint8_t *)config->ocsp_response.data(),
271 config->ocsp_response.size())) {
272 return false;
273 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500274 return true;
275}
276
David Benjaminc273d2c2015-02-09 12:59:46 -0500277static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400278 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500279 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400280
David Benjamin6f5c0f42015-02-24 01:23:21 -0500281 if (!config->expected_server_name.empty()) {
282 const uint8_t *extension_data;
283 size_t extension_len;
284 CBS extension, server_name_list, host_name;
285 uint8_t name_type;
286
287 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
288 &extension_data,
289 &extension_len)) {
290 fprintf(stderr, "Could not find server_name extension.\n");
291 return -1;
292 }
293
294 CBS_init(&extension, extension_data, extension_len);
295 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
296 CBS_len(&extension) != 0 ||
297 !CBS_get_u8(&server_name_list, &name_type) ||
298 name_type != TLSEXT_NAMETYPE_host_name ||
299 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
300 CBS_len(&server_name_list) != 0) {
301 fprintf(stderr, "Could not decode server_name extension.\n");
302 return -1;
303 }
304
305 if (!CBS_mem_equal(&host_name,
306 (const uint8_t*)config->expected_server_name.data(),
307 config->expected_server_name.size())) {
308 fprintf(stderr, "Server name mismatch.\n");
309 }
David Benjamin7b030512014-07-08 17:30:11 -0400310 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400311
David Benjamin6f5c0f42015-02-24 01:23:21 -0500312 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400313 return -1;
314 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400315
David Benjamin6f5c0f42015-02-24 01:23:21 -0500316 // Install the certificate in the early callback.
317 if (config->use_early_callback) {
318 if (config->async) {
319 // Install the certificate asynchronously.
320 return 0;
321 }
322 if (!InstallCertificate(ctx->ssl)) {
323 return -1;
324 }
David Benjamin7b030512014-07-08 17:30:11 -0400325 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400326 return 1;
327}
David Benjamin025b3d32014-07-01 19:53:04 -0400328
Paul Lietar8f1c2682015-08-18 12:21:54 +0100329static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
330 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
331 SSL_get_ex_data_X509_STORE_CTX_idx());
332 const TestConfig *config = GetConfigPtr(ssl);
333
334 if (!config->expected_ocsp_response.empty()) {
335 const uint8_t *data;
336 size_t len;
337 SSL_get0_ocsp_response(ssl, &data, &len);
338 if (len == 0) {
339 fprintf(stderr, "OCSP response not available in verify callback\n");
340 return 0;
341 }
342 }
343
David Benjamin67666e72014-07-12 15:47:52 -0400344 return 1;
345}
346
Paul Lietar8f1c2682015-08-18 12:21:54 +0100347static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
348 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
349 return 0;
350}
351
David Benjaminc273d2c2015-02-09 12:59:46 -0500352static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
353 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400354 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500355 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400356 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500357 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400358
David Benjamin5a593af2014-08-11 19:51:50 -0400359 *out = (const uint8_t*)config->advertise_npn.data();
360 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400361 return SSL_TLSEXT_ERR_OK;
362}
363
David Benjaminc273d2c2015-02-09 12:59:46 -0500364static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400365 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400366 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500367 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400368 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500369 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400370
David Benjamin5a593af2014-08-11 19:51:50 -0400371 *out = (uint8_t*)config->select_next_proto.data();
372 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400373 return SSL_TLSEXT_ERR_OK;
374}
375
David Benjaminc273d2c2015-02-09 12:59:46 -0500376static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
377 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400378 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500379 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400380 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500381 }
David Benjaminae2888f2014-09-06 12:58:58 -0400382
383 if (!config->expected_advertised_alpn.empty() &&
384 (config->expected_advertised_alpn.size() != inlen ||
385 memcmp(config->expected_advertised_alpn.data(),
386 in, inlen) != 0)) {
387 fprintf(stderr, "bad ALPN select callback inputs\n");
388 exit(1);
389 }
390
391 *out = (const uint8_t*)config->select_alpn.data();
392 *outlen = config->select_alpn.size();
393 return SSL_TLSEXT_ERR_OK;
394}
395
David Benjaminc273d2c2015-02-09 12:59:46 -0500396static unsigned PskClientCallback(SSL *ssl, const char *hint,
397 char *out_identity,
398 unsigned max_identity_len,
399 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400400 const TestConfig *config = GetConfigPtr(ssl);
401
402 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
403 fprintf(stderr, "Server PSK hint did not match.\n");
404 return 0;
405 }
406
407 // Account for the trailing '\0' for the identity.
408 if (config->psk_identity.size() >= max_identity_len ||
409 config->psk.size() > max_psk_len) {
410 fprintf(stderr, "PSK buffers too small\n");
411 return 0;
412 }
413
414 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
415 max_identity_len);
416 memcpy(out_psk, config->psk.data(), config->psk.size());
417 return config->psk.size();
418}
419
David Benjaminc273d2c2015-02-09 12:59:46 -0500420static unsigned PskServerCallback(SSL *ssl, const char *identity,
421 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400422 const TestConfig *config = GetConfigPtr(ssl);
423
424 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
425 fprintf(stderr, "Client PSK identity did not match.\n");
426 return 0;
427 }
428
429 if (config->psk.size() > max_psk_len) {
430 fprintf(stderr, "PSK buffers too small\n");
431 return 0;
432 }
433
434 memcpy(out_psk, config->psk.data(), config->psk.size());
435 return config->psk.size();
436}
437
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400438static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400439 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500440}
441
David Benjaminc273d2c2015-02-09 12:59:46 -0500442static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500443 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500444}
445
David Benjaminc273d2c2015-02-09 12:59:46 -0500446static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500447 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500448 return -1;
449 }
450 if (!InstallCertificate(ssl)) {
451 return 0;
452 }
453 return 1;
454}
455
David Benjaminc273d2c2015-02-09 12:59:46 -0500456static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
457 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500458 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500459 if (async_state->session) {
460 *copy = 0;
461 return async_state->session.release();
462 } else if (async_state->pending_session) {
463 return SSL_magic_pending_session_ptr();
464 } else {
465 return NULL;
466 }
467}
468
Adam Langley524e7172015-02-20 16:04:00 -0800469static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
470 const TestConfig *config = GetConfigPtr(early_context->ssl);
471 static int callback_num = 0;
472
473 callback_num++;
474 if (config->fail_ddos_callback ||
475 (config->fail_second_ddos_callback && callback_num == 2)) {
476 return 0;
477 }
478 return 1;
479}
480
David Benjamin87e4acd2015-04-02 19:57:35 -0400481static void InfoCallback(const SSL *ssl, int type, int val) {
482 if (type == SSL_CB_HANDSHAKE_DONE) {
483 if (GetConfigPtr(ssl)->handshake_never_done) {
484 fprintf(stderr, "handshake completed\n");
485 // Abort before any expected error code is printed, to ensure the overall
486 // test fails.
487 abort();
488 }
489 GetTestState(ssl)->handshake_done = true;
490 }
491}
492
David Benjaminba4594a2015-06-18 18:36:15 -0400493static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
494 GetTestState(ssl)->got_new_session = true;
495 // BoringSSL passes a reference to |session|.
496 SSL_SESSION_free(session);
497 return 1;
498}
499
David Benjamind98452d2015-06-16 14:16:23 -0400500static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
501 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
502 int encrypt) {
503 // This is just test code, so use the all-zeros key.
504 static const uint8_t kZeros[16] = {0};
505
506 if (encrypt) {
507 memcpy(key_name, kZeros, sizeof(kZeros));
508 RAND_bytes(iv, 16);
509 } else if (memcmp(key_name, kZeros, 16) != 0) {
510 return 0;
511 }
512
513 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
514 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
515 return -1;
516 }
517
518 if (!encrypt) {
519 return GetConfigPtr(ssl)->renew_ticket ? 2 : 1;
520 }
521 return 1;
522}
523
Adam Langley09505632015-07-30 18:10:13 -0700524// kCustomExtensionValue is the extension value that the custom extension
525// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700526static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700527static void *const kCustomExtensionAddArg =
528 reinterpret_cast<void *>(kCustomExtensionValue);
529static void *const kCustomExtensionParseArg =
530 reinterpret_cast<void *>(kCustomExtensionValue + 1);
531static const char kCustomExtensionContents[] = "custom extension";
532
533static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
534 const uint8_t **out, size_t *out_len,
535 int *out_alert_value, void *add_arg) {
536 if (extension_value != kCustomExtensionValue ||
537 add_arg != kCustomExtensionAddArg) {
538 abort();
539 }
540
541 if (GetConfigPtr(ssl)->custom_extension_skip) {
542 return 0;
543 }
544 if (GetConfigPtr(ssl)->custom_extension_fail_add) {
545 return -1;
546 }
547
548 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
549 *out_len = sizeof(kCustomExtensionContents) - 1;
550
551 return 1;
552}
553
554static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
555 const uint8_t *out, void *add_arg) {
556 if (extension_value != kCustomExtensionValue ||
557 add_arg != kCustomExtensionAddArg ||
558 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
559 abort();
560 }
561}
562
563static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
564 const uint8_t *contents,
565 size_t contents_len,
566 int *out_alert_value, void *parse_arg) {
567 if (extension_value != kCustomExtensionValue ||
568 parse_arg != kCustomExtensionParseArg) {
569 abort();
570 }
571
572 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
573 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
574 *out_alert_value = SSL_AD_DECODE_ERROR;
575 return 0;
576 }
577
578 return 1;
579}
580
David Benjamin87c8a642015-02-21 01:54:29 -0500581// Connect returns a new socket connected to localhost on |port| or -1 on
582// error.
583static int Connect(uint16_t port) {
584 int sock = socket(AF_INET, SOCK_STREAM, 0);
585 if (sock == -1) {
586 PrintSocketError("socket");
587 return -1;
588 }
589 int nodelay = 1;
590 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
591 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
592 PrintSocketError("setsockopt");
593 closesocket(sock);
594 return -1;
595 }
596 sockaddr_in sin;
597 memset(&sin, 0, sizeof(sin));
598 sin.sin_family = AF_INET;
599 sin.sin_port = htons(port);
600 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
601 PrintSocketError("inet_pton");
602 closesocket(sock);
603 return -1;
604 }
605 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
606 sizeof(sin)) != 0) {
607 PrintSocketError("connect");
608 closesocket(sock);
609 return -1;
610 }
611 return sock;
612}
613
614class SocketCloser {
615 public:
616 explicit SocketCloser(int sock) : sock_(sock) {}
617 ~SocketCloser() {
618 // Half-close and drain the socket before releasing it. This seems to be
619 // necessary for graceful shutdown on Windows. It will also avoid write
620 // failures in the test runner.
621#if defined(OPENSSL_WINDOWS)
622 shutdown(sock_, SD_SEND);
623#else
624 shutdown(sock_, SHUT_WR);
625#endif
626 while (true) {
627 char buf[1024];
628 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
629 break;
630 }
631 }
632 closesocket(sock_);
633 }
634
635 private:
636 const int sock_;
637};
638
David Benjaminc273d2c2015-02-09 12:59:46 -0500639static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500640 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
641 config->is_dtls ? DTLS_method() : TLS_method()));
642 if (!ssl_ctx) {
643 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400644 }
645
Adam Langleycef75832015-09-03 14:51:12 -0700646 std::string cipher_list = "ALL";
647 if (!config->cipher.empty()) {
648 cipher_list = config->cipher;
649 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
650 }
651 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
652 return nullptr;
653 }
654
655 if (!config->cipher_tls10.empty() &&
656 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
657 config->cipher_tls10.c_str())) {
658 return nullptr;
659 }
660 if (!config->cipher_tls11.empty() &&
661 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
662 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500663 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400664 }
665
David Benjamina7f333d2015-02-09 02:37:18 -0500666 ScopedDH dh(DH_get_2048_256(NULL));
667 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
668 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400669 }
670
David Benjamin1b8b6912015-02-09 04:28:16 -0500671 if (config->async && config->is_server) {
672 // Disable the internal session cache. To test asynchronous session lookup,
673 // we use an external session cache.
674 SSL_CTX_set_session_cache_mode(
675 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500676 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500677 } else {
678 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
679 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400680
David Benjaminc273d2c2015-02-09 12:59:46 -0500681 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400682
David Benjamin1f5f62b2014-07-12 16:18:02 -0400683 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500684 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400685 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500686 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500687 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400688 }
689
690 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500691 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400692 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400693
Adam Langley49c7af12015-07-10 14:33:46 -0700694 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500695 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400696
David Benjaminc273d2c2015-02-09 12:59:46 -0500697 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500698
David Benjamin87e4acd2015-04-02 19:57:35 -0400699 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400700 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400701
David Benjamind98452d2015-06-16 14:16:23 -0400702 if (config->use_ticket_callback) {
703 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
704 }
705
Adam Langley09505632015-07-30 18:10:13 -0700706 if (config->enable_client_custom_extension &&
707 !SSL_CTX_add_client_custom_ext(
708 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
709 CustomExtensionFreeCallback, kCustomExtensionAddArg,
710 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
711 return nullptr;
712 }
713
714 if (config->enable_server_custom_extension &&
715 !SSL_CTX_add_server_custom_ext(
716 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
717 CustomExtensionFreeCallback, kCustomExtensionAddArg,
718 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
719 return nullptr;
720 }
721
Paul Lietar8f1c2682015-08-18 12:21:54 +0100722 if (config->verify_fail) {
723 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
724 } else {
725 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
726 }
727
Paul Lietar4fac72e2015-09-09 13:44:55 +0100728 if (!config->signed_cert_timestamps.empty() &&
729 !SSL_CTX_set_signed_cert_timestamp_list(
730 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
731 config->signed_cert_timestamps.size())) {
732 return nullptr;
733 }
734
David Benjamin1d5c83e2014-07-22 19:20:02 -0400735 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400736}
737
David Benjamin40f101b2015-02-20 11:23:42 -0500738// RetryAsync is called after a failed operation on |ssl| with return code
739// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400740// event and returns true. Otherwise it returns false.
741static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400742 // No error; don't retry.
743 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500744 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400745 }
David Benjamin83f90402015-01-27 01:09:43 -0500746
David Benjamin6c2563e2015-04-03 03:47:47 -0400747 TestState *test_state = GetTestState(ssl);
748 if (test_state->clock_delta.tv_usec != 0 ||
749 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500750 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400751 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
752 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
753 test_state->clock.tv_usec %= 1000000;
754 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
755 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500756
757 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400758 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500759 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500760 }
David Benjamin40f101b2015-02-20 11:23:42 -0500761 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500762 }
763
David Benjamin43ec06f2014-08-05 02:28:57 -0400764 // See if we needed to read or write more. If so, allow one byte through on
765 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500766 switch (SSL_get_error(ssl, ret)) {
767 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400768 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500769 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500770 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400771 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500772 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500773 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
774 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
775 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500776 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500777 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400778 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500779 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500780 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500781 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400782 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500783 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500784 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400785 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500786 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500787 case SSL_ERROR_PENDING_CERTIFICATE:
788 // The handshake will resume without a second call to the early callback.
789 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400790 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
791 test_state->signature_retries++;
792 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500793 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500794 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400795 }
David Benjamin025b3d32014-07-01 19:53:04 -0400796}
797
David Benjamin6c2563e2015-04-03 03:47:47 -0400798// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
799// the result value of the final |SSL_read| call.
800static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
801 const TestConfig *config = GetConfigPtr(ssl);
802 int ret;
803 do {
804 ret = SSL_read(ssl, out, max_out);
805 } while (config->async && RetryAsync(ssl, ret));
806 return ret;
807}
808
809// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
810// operations. It returns the result of the final |SSL_write| call.
811static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
812 const TestConfig *config = GetConfigPtr(ssl);
813 int ret;
814 do {
815 ret = SSL_write(ssl, in, in_len);
816 if (ret > 0) {
817 in += ret;
818 in_len -= ret;
819 }
820 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
821 return ret;
822}
823
David Benjamin30789da2015-08-29 22:56:45 -0400824// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
825// returns the result of the final |SSL_shutdown| call.
826static int DoShutdown(SSL *ssl) {
827 const TestConfig *config = GetConfigPtr(ssl);
828 int ret;
829 do {
830 ret = SSL_shutdown(ssl);
831 } while (config->async && RetryAsync(ssl, ret));
832 return ret;
833}
834
David Benjamin91eab5c2015-06-18 18:35:46 -0400835// CheckHandshakeProperties checks, immediately after |ssl| completes its
836// initial handshake (or False Starts), whether all the properties are
837// consistent with the test configuration and invariants.
838static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
839 const TestConfig *config = GetConfigPtr(ssl);
840
841 if (SSL_get_current_cipher(ssl) == nullptr) {
842 fprintf(stderr, "null cipher after handshake\n");
843 return false;
844 }
845
846 if (is_resume &&
847 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
848 fprintf(stderr, "session was%s reused\n",
849 SSL_session_reused(ssl) ? "" : " not");
850 return false;
851 }
852
853 bool expect_handshake_done = is_resume || !config->false_start;
854 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
855 fprintf(stderr, "handshake was%s completed\n",
856 GetTestState(ssl)->handshake_done ? "" : " not");
857 return false;
858 }
859
David Benjaminba4594a2015-06-18 18:36:15 -0400860 if (expect_handshake_done && !config->is_server) {
861 bool expect_new_session =
862 !config->expect_no_session &&
863 (!SSL_session_reused(ssl) || config->expect_ticket_renewal);
864 if (expect_new_session != GetTestState(ssl)->got_new_session) {
865 fprintf(stderr,
866 "new session was%s established, but we expected the opposite\n",
867 GetTestState(ssl)->got_new_session ? "" : " not");
868 return false;
869 }
870 }
871
David Benjamin91eab5c2015-06-18 18:35:46 -0400872 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
873 fprintf(stderr, "early callback not called\n");
874 return false;
875 }
876
877 if (!config->expected_server_name.empty()) {
878 const char *server_name =
879 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
880 if (server_name != config->expected_server_name) {
881 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
882 server_name, config->expected_server_name.c_str());
883 return false;
884 }
885 }
886
887 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -0400888 const uint8_t *certificate_types;
889 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -0400890 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -0400891 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -0400892 memcmp(certificate_types,
893 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -0400894 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -0400895 fprintf(stderr, "certificate types mismatch\n");
896 return false;
897 }
898 }
899
900 if (!config->expected_next_proto.empty()) {
901 const uint8_t *next_proto;
902 unsigned next_proto_len;
903 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
904 if (next_proto_len != config->expected_next_proto.size() ||
905 memcmp(next_proto, config->expected_next_proto.data(),
906 next_proto_len) != 0) {
907 fprintf(stderr, "negotiated next proto mismatch\n");
908 return false;
909 }
910 }
911
912 if (!config->expected_alpn.empty()) {
913 const uint8_t *alpn_proto;
914 unsigned alpn_proto_len;
915 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
916 if (alpn_proto_len != config->expected_alpn.size() ||
917 memcmp(alpn_proto, config->expected_alpn.data(),
918 alpn_proto_len) != 0) {
919 fprintf(stderr, "negotiated alpn proto mismatch\n");
920 return false;
921 }
922 }
923
924 if (!config->expected_channel_id.empty()) {
925 uint8_t channel_id[64];
926 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
927 fprintf(stderr, "no channel id negotiated\n");
928 return false;
929 }
930 if (config->expected_channel_id.size() != 64 ||
931 memcmp(config->expected_channel_id.data(),
932 channel_id, 64) != 0) {
933 fprintf(stderr, "channel id mismatch\n");
934 return false;
935 }
936 }
937
938 if (config->expect_extended_master_secret) {
939 if (!ssl->session->extended_master_secret) {
940 fprintf(stderr, "No EMS for session when expected");
941 return false;
942 }
943 }
944
945 if (!config->expected_ocsp_response.empty()) {
946 const uint8_t *data;
947 size_t len;
948 SSL_get0_ocsp_response(ssl, &data, &len);
949 if (config->expected_ocsp_response.size() != len ||
950 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
951 fprintf(stderr, "OCSP response mismatch\n");
952 return false;
953 }
954 }
955
956 if (!config->expected_signed_cert_timestamps.empty()) {
957 const uint8_t *data;
958 size_t len;
959 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
960 if (config->expected_signed_cert_timestamps.size() != len ||
961 memcmp(config->expected_signed_cert_timestamps.data(),
962 data, len) != 0) {
963 fprintf(stderr, "SCT list mismatch\n");
964 return false;
965 }
966 }
967
Paul Lietar8f1c2682015-08-18 12:21:54 +0100968 if (config->expect_verify_result) {
969 int expected_verify_result = config->verify_fail ?
970 X509_V_ERR_APPLICATION_VERIFICATION :
971 X509_V_OK;
972
973 if (SSL_get_verify_result(ssl) != expected_verify_result) {
974 fprintf(stderr, "Wrong certificate verification result\n");
975 return false;
976 }
977 }
978
David Benjamin91eab5c2015-06-18 18:35:46 -0400979 if (!config->is_server) {
980 /* Clients should expect a peer certificate chain iff this was not a PSK
981 * cipher suite. */
982 if (config->psk.empty()) {
983 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
984 fprintf(stderr, "Missing peer certificate chain!\n");
985 return false;
986 }
987 } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
988 fprintf(stderr, "Unexpected peer certificate chain!\n");
989 return false;
990 }
991 }
992 return true;
993}
994
David Benjamin87c8a642015-02-21 01:54:29 -0500995// DoExchange runs a test SSL exchange against the peer. On success, it returns
996// true and sets |*out_session| to the negotiated SSL session. If the test is a
997// resumption attempt, |is_resume| is true and |session| is the session from the
998// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500999static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
1000 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -05001001 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001002 ScopedSSL ssl(SSL_new(ssl_ctx));
1003 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001004 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001005 }
1006
David Benjamina7f333d2015-02-09 02:37:18 -05001007 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001008 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001009 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001010 }
David Benjamin5a593af2014-08-11 19:51:50 -04001011
Adam Langley5f0efe02015-02-20 13:03:16 -08001012 if (config->fallback_scsv &&
1013 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1014 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001015 }
David Benjamin6f5c0f42015-02-24 01:23:21 -05001016 if (!config->use_early_callback) {
1017 if (config->async) {
1018 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
1019 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
1020 } else if (!InstallCertificate(ssl.get())) {
1021 return false;
1022 }
David Benjamin5a593af2014-08-11 19:51:50 -04001023 }
1024 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001025 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001026 NULL);
1027 }
1028 if (config->verify_peer) {
1029 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001030 }
1031 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001032 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001033 }
1034 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001035 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001036 }
1037 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001038 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001039 }
1040 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001041 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001042 }
1043 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001044 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001045 }
1046 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001047 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001048 }
1049 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001050 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001051 }
David Benjamin5c24a1d2014-08-31 00:59:27 -04001052 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -05001053 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -04001054 }
David Benjamin2c99d282015-09-01 10:23:00 -04001055 if (config->microsoft_big_sslv3_buffer) {
1056 SSL_set_options(ssl.get(), SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
1057 }
David Benjamincff0b902015-05-15 23:09:47 -04001058 if (config->no_legacy_server_connect) {
1059 SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
1060 }
David Benjamina08e49d2014-08-24 01:46:07 -04001061 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001062 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001063 }
1064 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001065 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001066 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001067 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -05001068 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
1069 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001070 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001071 }
David Benjamina08e49d2014-08-24 01:46:07 -04001072 }
David Benjamina08e49d2014-08-24 01:46:07 -04001073 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001074 if (!config->host_name.empty() &&
1075 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001076 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001077 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001078 if (!config->advertise_alpn.empty() &&
1079 SSL_set_alpn_protos(ssl.get(),
1080 (const uint8_t *)config->advertise_alpn.data(),
1081 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001082 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001083 }
David Benjamin48cae082014-10-27 01:06:24 -04001084 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001085 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1086 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001087 }
David Benjamin61f95272014-11-25 01:55:35 -05001088 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001089 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001090 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001091 }
David Benjamin61f95272014-11-25 01:55:35 -05001092 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001093 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001094 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001095 }
1096 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001097 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001098 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001099 }
1100 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001101 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001102 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001103 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001104 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001105 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001106 }
1107 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001108 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001109 }
David Benjamin13be1de2015-01-11 16:29:36 -05001110 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001111 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1112 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001113 }
Adam Langley524e7172015-02-20 16:04:00 -08001114 if (config->install_ddos_callback) {
1115 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1116 }
David Benjamin1d5ef3b2015-10-12 19:54:18 -04001117 if (config->renegotiate_once) {
1118 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once);
1119 }
1120 if (config->renegotiate_freely) {
1121 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04001122 }
David Benjamin30789da2015-08-29 22:56:45 -04001123 if (!config->check_close_notify) {
1124 SSL_set_quiet_shutdown(ssl.get(), 1);
1125 }
David Benjamin025b3d32014-07-01 19:53:04 -04001126
David Benjamin87c8a642015-02-21 01:54:29 -05001127 int sock = Connect(config->port);
1128 if (sock == -1) {
1129 return false;
1130 }
1131 SocketCloser closer(sock);
1132
1133 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001134 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001135 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001136 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001137 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001138 ScopedBIO packeted =
1139 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -05001140 BIO_push(packeted.get(), bio.release());
1141 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001142 }
David Benjamin5a593af2014-08-11 19:51:50 -04001143 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001144 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001145 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -05001146 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001147 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001148 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001149 }
David Benjamina7f333d2015-02-09 02:37:18 -05001150 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1151 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001152
David Benjamin1d5c83e2014-07-22 19:20:02 -04001153 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001154 if (!config->is_server) {
1155 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001156 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001157 }
1158 } else if (config->async) {
1159 // The internal session cache is disabled, so install the session
1160 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -05001161 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -05001162 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001163 }
1164 }
1165
David Benjamina07c0fc2015-05-13 13:19:42 -04001166 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1167 fprintf(stderr, "non-null cipher before handshake\n");
1168 return false;
1169 }
1170
David Benjamin1d5c83e2014-07-22 19:20:02 -04001171 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001172 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001173 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001174 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001175 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001176 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001177 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001178 } else {
1179 do {
1180 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001181 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001182 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001183 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001184 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001185 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001186 if (ret != 1 ||
1187 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001188 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001189 }
David Benjamin025b3d32014-07-01 19:53:04 -04001190
David Benjaminba4594a2015-06-18 18:36:15 -04001191 // Reset the state to assert later that the callback isn't called in
1192 // renegotations.
1193 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001194 }
1195
David Benjaminc565ebb2015-04-03 04:06:36 -04001196 if (config->export_keying_material > 0) {
1197 std::vector<uint8_t> result(
1198 static_cast<size_t>(config->export_keying_material));
1199 if (!SSL_export_keying_material(
1200 ssl.get(), result.data(), result.size(),
1201 config->export_label.data(), config->export_label.size(),
1202 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1203 config->export_context.size(), config->use_export_context)) {
1204 fprintf(stderr, "failed to export keying material\n");
1205 return false;
1206 }
1207 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1208 return false;
1209 }
1210 }
1211
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001212 if (config->tls_unique) {
1213 uint8_t tls_unique[16];
1214 size_t tls_unique_len;
1215 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1216 sizeof(tls_unique))) {
1217 fprintf(stderr, "failed to get tls-unique\n");
1218 return false;
1219 }
1220
1221 if (tls_unique_len != 12) {
1222 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1223 static_cast<unsigned>(tls_unique_len));
1224 return false;
1225 }
1226
1227 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1228 return false;
1229 }
1230 }
1231
David Benjamin5a593af2014-08-11 19:51:50 -04001232 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001233 if (config->is_dtls) {
1234 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001235 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001236 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001237 // This mode writes a number of different record sizes in an attempt to
1238 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001239 static const size_t kBufLen = 32769;
1240 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1241 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001242 static const size_t kRecordSizes[] = {
1243 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1244 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1245 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001246 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001247 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001248 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001249 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001250 }
Adam Langleybc949292015-06-18 21:32:44 -07001251 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001252 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001253 }
1254 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001255 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001256 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001257 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1258 5) < 0) {
1259 return false;
1260 }
David Benjamine58c4f52014-08-24 03:47:07 -04001261 }
David Benjamin30789da2015-08-29 22:56:45 -04001262 if (!config->shim_shuts_down) {
1263 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001264 static const size_t kBufLen = 16384;
1265 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1266
David Benjamin2c99d282015-09-01 10:23:00 -04001267 // Read only 512 bytes at a time in TLS to ensure records may be
1268 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001269 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001270 int err = SSL_get_error(ssl.get(), n);
1271 if (err == SSL_ERROR_ZERO_RETURN ||
1272 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1273 if (n != 0) {
1274 fprintf(stderr, "Invalid SSL_get_error output\n");
1275 return false;
1276 }
1277 // Stop on either clean or unclean shutdown.
1278 break;
1279 } else if (err != SSL_ERROR_NONE) {
1280 if (n > 0) {
1281 fprintf(stderr, "Invalid SSL_get_error output\n");
1282 return false;
1283 }
1284 return false;
1285 }
1286 // Successfully read data.
1287 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001288 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001289 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001290 }
David Benjamin30789da2015-08-29 22:56:45 -04001291
1292 // After a successful read, with or without False Start, the handshake
1293 // must be complete.
1294 if (!GetTestState(ssl.get())->handshake_done) {
1295 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001296 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001297 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001298
David Benjamin30789da2015-08-29 22:56:45 -04001299 for (int i = 0; i < n; i++) {
1300 buf[i] ^= 0xff;
1301 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001302 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001303 return false;
1304 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001305 }
1306 }
David Benjamin025b3d32014-07-01 19:53:04 -04001307 }
1308
David Benjaminba4594a2015-06-18 18:36:15 -04001309 if (!config->is_server && !config->false_start &&
1310 !config->implicit_handshake &&
1311 GetTestState(ssl.get())->got_new_session) {
1312 fprintf(stderr, "new session was established after the handshake\n");
1313 return false;
1314 }
1315
David Benjamin1d5c83e2014-07-22 19:20:02 -04001316 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001317 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001318 }
1319
David Benjamin30789da2015-08-29 22:56:45 -04001320 ret = DoShutdown(ssl.get());
1321
1322 if (config->shim_shuts_down && config->check_close_notify) {
1323 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1324 // it returns zero when our close_notify is sent, then one when the peer's
1325 // is received.
1326 if (ret != 0) {
1327 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1328 return false;
1329 }
1330 ret = DoShutdown(ssl.get());
1331 }
1332
1333 if (ret != 1) {
1334 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1335 return false;
1336 }
1337
David Benjamin324dce42015-10-12 19:49:00 -04001338 if (SSL_total_renegotiations(ssl.get()) !=
1339 config->expect_total_renegotiations) {
1340 fprintf(stderr, "Expected %d renegotiations, got %d\n",
1341 config->expect_total_renegotiations,
1342 SSL_total_renegotiations(ssl.get()));
1343 return false;
1344 }
1345
David Benjamin40f101b2015-02-20 11:23:42 -05001346 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001347}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001348
1349int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -05001350#if defined(OPENSSL_WINDOWS)
1351 /* Initialize Winsock. */
1352 WORD wsa_version = MAKEWORD(2, 2);
1353 WSADATA wsa_data;
1354 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1355 if (wsa_err != 0) {
1356 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1357 return 1;
1358 }
1359 if (wsa_data.wVersion != wsa_version) {
1360 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1361 return 1;
1362 }
1363#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001364 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001365#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001366
David Benjamin5a593af2014-08-11 19:51:50 -04001367 if (!SSL_library_init()) {
1368 return 1;
1369 }
David Benjamind9e07012015-02-09 03:04:34 -05001370 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001371 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001372 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001373 return 1;
1374 }
David Benjamin5a593af2014-08-11 19:51:50 -04001375
1376 TestConfig config;
1377 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001378 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001379 }
1380
David Benjaminc273d2c2015-02-09 12:59:46 -05001381 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001382 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001383 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001384 return 1;
1385 }
1386
David Benjamina7f333d2015-02-09 02:37:18 -05001387 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001388 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001389 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001390 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001391 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001392 }
1393
David Benjamin40f101b2015-02-20 11:23:42 -05001394 if (config.resume &&
1395 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001396 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001397 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001398 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001399 }
1400
David Benjamina7f333d2015-02-09 02:37:18 -05001401 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001402}