blob: c30eb551d8207c449f9e198c91e46b8c802ee9dc [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>
David Benjaminc565ebb2015-04-03 04:06:36 -040048#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040049
50#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040051#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040052#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050053#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040054#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040055
David Benjamin87c8a642015-02-21 01:54:29 -050056
57#if !defined(OPENSSL_WINDOWS)
58static int closesocket(int sock) {
59 return close(sock);
60}
61
62static void PrintSocketError(const char *func) {
63 perror(func);
64}
65#else
66static void PrintSocketError(const char *func) {
67 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
68}
69#endif
70
David Benjaminc273d2c2015-02-09 12:59:46 -050071static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050072 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040073 return 1;
74}
David Benjamin025b3d32014-07-01 19:53:04 -040075
David Benjamin2d445c02015-02-09 13:03:50 -050076struct TestState {
David Benjaminff9c74f2015-04-06 20:17:56 -040077 TestState() {
78 // MSVC cannot initialize these inline.
79 memset(&clock, 0, sizeof(clock));
80 memset(&clock_delta, 0, sizeof(clock_delta));
81 }
82
David Benjamin6c2563e2015-04-03 03:47:47 -040083 // async_bio is async BIO which pauses reads and writes.
84 BIO *async_bio = nullptr;
85 // clock is the current time for the SSL connection.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040086 timeval clock;
David Benjamin6c2563e2015-04-03 03:47:47 -040087 // clock_delta is how far the clock advanced in the most recent failed
88 // |BIO_read|.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040089 timeval clock_delta;
David Benjamind9e07012015-02-09 03:04:34 -050090 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040091 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050092 ScopedSSL_SESSION session;
93 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040094 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040095 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040096 // private_key is the underlying private key used when testing custom keys.
97 ScopedEVP_PKEY private_key;
98 std::vector<uint8_t> signature;
99 // signature_retries is the number of times an asynchronous sign operation has
100 // been retried.
101 unsigned signature_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400102 bool got_new_session = false;
David Benjamind9e07012015-02-09 03:04:34 -0500103};
104
David Benjamin2d445c02015-02-09 13:03:50 -0500105static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700106 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500107 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500108}
109
110static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500111static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400112
Adam Langley69a01602014-11-17 17:26:55 -0800113static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500114 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400115}
116
David Benjamin87e4acd2015-04-02 19:57:35 -0400117static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500118 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400119}
120
David Benjamin2d445c02015-02-09 13:03:50 -0500121static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
122 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500123 async.release();
124 return true;
125 }
126 return false;
127}
128
David Benjamin87e4acd2015-04-02 19:57:35 -0400129static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500130 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500131}
132
David Benjamina7f333d2015-02-09 02:37:18 -0500133static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
134 ScopedBIO bio(BIO_new(BIO_s_file()));
135 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
136 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400137 }
David Benjamina7f333d2015-02-09 02:37:18 -0500138 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400139 return pkey;
140}
141
David Benjaminb4d65fd2015-05-29 17:11:21 -0400142static int AsyncPrivateKeyType(SSL *ssl) {
143 return EVP_PKEY_id(GetTestState(ssl)->private_key.get());
144}
145
146static int AsyncPrivateKeySupportsDigest(SSL *ssl, const EVP_MD *md) {
147 return EVP_PKEY_supports_digest(GetTestState(ssl)->private_key.get(), md);
148}
149
150static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
151 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
152}
153
154static ssl_private_key_result_t AsyncPrivateKeySign(
155 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
156 const EVP_MD *md, const uint8_t *in, size_t in_len) {
157 TestState *test_state = GetTestState(ssl);
158 if (!test_state->signature.empty()) {
159 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
160 abort();
161 }
162
163 ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(test_state->private_key.get(),
164 nullptr));
165 if (!ctx) {
166 return ssl_private_key_failure;
167 }
168
169 // Write the signature into |test_state|.
170 size_t len = 0;
171 if (!EVP_PKEY_sign_init(ctx.get()) ||
172 !EVP_PKEY_CTX_set_signature_md(ctx.get(), md) ||
173 !EVP_PKEY_sign(ctx.get(), nullptr, &len, in, in_len)) {
174 return ssl_private_key_failure;
175 }
176 test_state->signature.resize(len);
177 if (!EVP_PKEY_sign(ctx.get(), bssl::vector_data(&test_state->signature), &len,
178 in, in_len)) {
179 return ssl_private_key_failure;
180 }
181 test_state->signature.resize(len);
182
183 // The signature will be released asynchronously in |AsyncPrivateKeySignComplete|.
184 return ssl_private_key_retry;
185}
186
187static ssl_private_key_result_t AsyncPrivateKeySignComplete(
188 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
189 TestState *test_state = GetTestState(ssl);
190 if (test_state->signature.empty()) {
191 fprintf(stderr,
192 "AsyncPrivateKeySignComplete called without operation pending.\n");
193 abort();
194 }
195
196 if (test_state->signature_retries < 2) {
197 // Only return the signature on the second attempt, to test both incomplete
198 // |sign| and |sign_complete|.
199 return ssl_private_key_retry;
200 }
201
202 if (max_out < test_state->signature.size()) {
203 fprintf(stderr, "Output buffer too small.\n");
204 return ssl_private_key_failure;
205 }
206 memcpy(out, bssl::vector_data(&test_state->signature),
207 test_state->signature.size());
nagendra modadugu601448a2015-07-24 09:31:31 -0700208 *out_len = test_state->signature.size();
David Benjaminb4d65fd2015-05-29 17:11:21 -0400209
210 test_state->signature.clear();
211 test_state->signature_retries = 0;
212 return ssl_private_key_success;
213}
214
215static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
216 AsyncPrivateKeyType,
217 AsyncPrivateKeySupportsDigest,
218 AsyncPrivateKeyMaxSignatureLen,
219 AsyncPrivateKeySign,
220 AsyncPrivateKeySignComplete,
221};
222
David Benjamin41fdbcd2015-02-09 03:13:35 -0500223static bool InstallCertificate(SSL *ssl) {
224 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400225 TestState *test_state = GetTestState(ssl);
226 if (!config->key_file.empty()) {
227 if (config->use_async_private_key) {
228 test_state->private_key = LoadPrivateKey(config->key_file.c_str());
229 if (!test_state->private_key) {
230 return false;
231 }
232 SSL_set_private_key_method(ssl, &g_async_private_key_method);
233 } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
234 SSL_FILETYPE_PEM)) {
235 return false;
236 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500237 }
238 if (!config->cert_file.empty() &&
239 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
240 SSL_FILETYPE_PEM)) {
241 return false;
242 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100243 if (!config->ocsp_response.empty() &&
244 !SSL_CTX_set_ocsp_response(ssl->ctx,
245 (const uint8_t *)config->ocsp_response.data(),
246 config->ocsp_response.size())) {
247 return false;
248 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500249 return true;
250}
251
David Benjaminc273d2c2015-02-09 12:59:46 -0500252static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400253 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500254 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400255
David Benjamin6f5c0f42015-02-24 01:23:21 -0500256 if (!config->expected_server_name.empty()) {
257 const uint8_t *extension_data;
258 size_t extension_len;
259 CBS extension, server_name_list, host_name;
260 uint8_t name_type;
261
262 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
263 &extension_data,
264 &extension_len)) {
265 fprintf(stderr, "Could not find server_name extension.\n");
266 return -1;
267 }
268
269 CBS_init(&extension, extension_data, extension_len);
270 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
271 CBS_len(&extension) != 0 ||
272 !CBS_get_u8(&server_name_list, &name_type) ||
273 name_type != TLSEXT_NAMETYPE_host_name ||
274 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
275 CBS_len(&server_name_list) != 0) {
276 fprintf(stderr, "Could not decode server_name extension.\n");
277 return -1;
278 }
279
280 if (!CBS_mem_equal(&host_name,
281 (const uint8_t*)config->expected_server_name.data(),
282 config->expected_server_name.size())) {
283 fprintf(stderr, "Server name mismatch.\n");
284 }
David Benjamin7b030512014-07-08 17:30:11 -0400285 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400286
David Benjamin6f5c0f42015-02-24 01:23:21 -0500287 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400288 return -1;
289 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400290
David Benjamin6f5c0f42015-02-24 01:23:21 -0500291 // Install the certificate in the early callback.
292 if (config->use_early_callback) {
293 if (config->async) {
294 // Install the certificate asynchronously.
295 return 0;
296 }
297 if (!InstallCertificate(ctx->ssl)) {
298 return -1;
299 }
David Benjamin7b030512014-07-08 17:30:11 -0400300 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400301 return 1;
302}
David Benjamin025b3d32014-07-01 19:53:04 -0400303
Paul Lietar8f1c2682015-08-18 12:21:54 +0100304static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
305 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
306 SSL_get_ex_data_X509_STORE_CTX_idx());
307 const TestConfig *config = GetConfigPtr(ssl);
308
309 if (!config->expected_ocsp_response.empty()) {
310 const uint8_t *data;
311 size_t len;
312 SSL_get0_ocsp_response(ssl, &data, &len);
313 if (len == 0) {
314 fprintf(stderr, "OCSP response not available in verify callback\n");
315 return 0;
316 }
317 }
318
David Benjamin67666e72014-07-12 15:47:52 -0400319 return 1;
320}
321
Paul Lietar8f1c2682015-08-18 12:21:54 +0100322static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
323 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
324 return 0;
325}
326
David Benjaminc273d2c2015-02-09 12:59:46 -0500327static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
328 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400329 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500330 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400331 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500332 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400333
David Benjamin5a593af2014-08-11 19:51:50 -0400334 *out = (const uint8_t*)config->advertise_npn.data();
335 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400336 return SSL_TLSEXT_ERR_OK;
337}
338
David Benjaminc273d2c2015-02-09 12:59:46 -0500339static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400340 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400341 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500342 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400343 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500344 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400345
David Benjamin5a593af2014-08-11 19:51:50 -0400346 *out = (uint8_t*)config->select_next_proto.data();
347 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400348 return SSL_TLSEXT_ERR_OK;
349}
350
David Benjaminc273d2c2015-02-09 12:59:46 -0500351static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
352 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400353 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500354 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400355 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500356 }
David Benjaminae2888f2014-09-06 12:58:58 -0400357
358 if (!config->expected_advertised_alpn.empty() &&
359 (config->expected_advertised_alpn.size() != inlen ||
360 memcmp(config->expected_advertised_alpn.data(),
361 in, inlen) != 0)) {
362 fprintf(stderr, "bad ALPN select callback inputs\n");
363 exit(1);
364 }
365
366 *out = (const uint8_t*)config->select_alpn.data();
367 *outlen = config->select_alpn.size();
368 return SSL_TLSEXT_ERR_OK;
369}
370
David Benjaminc273d2c2015-02-09 12:59:46 -0500371static unsigned PskClientCallback(SSL *ssl, const char *hint,
372 char *out_identity,
373 unsigned max_identity_len,
374 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400375 const TestConfig *config = GetConfigPtr(ssl);
376
377 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
378 fprintf(stderr, "Server PSK hint did not match.\n");
379 return 0;
380 }
381
382 // Account for the trailing '\0' for the identity.
383 if (config->psk_identity.size() >= max_identity_len ||
384 config->psk.size() > max_psk_len) {
385 fprintf(stderr, "PSK buffers too small\n");
386 return 0;
387 }
388
389 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
390 max_identity_len);
391 memcpy(out_psk, config->psk.data(), config->psk.size());
392 return config->psk.size();
393}
394
David Benjaminc273d2c2015-02-09 12:59:46 -0500395static unsigned PskServerCallback(SSL *ssl, const char *identity,
396 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400397 const TestConfig *config = GetConfigPtr(ssl);
398
399 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
400 fprintf(stderr, "Client PSK identity did not match.\n");
401 return 0;
402 }
403
404 if (config->psk.size() > max_psk_len) {
405 fprintf(stderr, "PSK buffers too small\n");
406 return 0;
407 }
408
409 memcpy(out_psk, config->psk.data(), config->psk.size());
410 return config->psk.size();
411}
412
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400413static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400414 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500415}
416
David Benjaminc273d2c2015-02-09 12:59:46 -0500417static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500418 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500419}
420
David Benjaminc273d2c2015-02-09 12:59:46 -0500421static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500422 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500423 return -1;
424 }
425 if (!InstallCertificate(ssl)) {
426 return 0;
427 }
428 return 1;
429}
430
David Benjaminc273d2c2015-02-09 12:59:46 -0500431static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
432 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500433 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500434 if (async_state->session) {
435 *copy = 0;
436 return async_state->session.release();
437 } else if (async_state->pending_session) {
438 return SSL_magic_pending_session_ptr();
439 } else {
440 return NULL;
441 }
442}
443
Adam Langley524e7172015-02-20 16:04:00 -0800444static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
445 const TestConfig *config = GetConfigPtr(early_context->ssl);
446 static int callback_num = 0;
447
448 callback_num++;
449 if (config->fail_ddos_callback ||
450 (config->fail_second_ddos_callback && callback_num == 2)) {
451 return 0;
452 }
453 return 1;
454}
455
David Benjamin87e4acd2015-04-02 19:57:35 -0400456static void InfoCallback(const SSL *ssl, int type, int val) {
457 if (type == SSL_CB_HANDSHAKE_DONE) {
458 if (GetConfigPtr(ssl)->handshake_never_done) {
459 fprintf(stderr, "handshake completed\n");
460 // Abort before any expected error code is printed, to ensure the overall
461 // test fails.
462 abort();
463 }
464 GetTestState(ssl)->handshake_done = true;
465 }
466}
467
David Benjaminba4594a2015-06-18 18:36:15 -0400468static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
469 GetTestState(ssl)->got_new_session = true;
470 // BoringSSL passes a reference to |session|.
471 SSL_SESSION_free(session);
472 return 1;
473}
474
David Benjamind98452d2015-06-16 14:16:23 -0400475static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
476 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
477 int encrypt) {
478 // This is just test code, so use the all-zeros key.
479 static const uint8_t kZeros[16] = {0};
480
481 if (encrypt) {
482 memcpy(key_name, kZeros, sizeof(kZeros));
483 RAND_bytes(iv, 16);
484 } else if (memcmp(key_name, kZeros, 16) != 0) {
485 return 0;
486 }
487
488 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
489 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
490 return -1;
491 }
492
493 if (!encrypt) {
494 return GetConfigPtr(ssl)->renew_ticket ? 2 : 1;
495 }
496 return 1;
497}
498
Adam Langley09505632015-07-30 18:10:13 -0700499// kCustomExtensionValue is the extension value that the custom extension
500// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700501static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700502static void *const kCustomExtensionAddArg =
503 reinterpret_cast<void *>(kCustomExtensionValue);
504static void *const kCustomExtensionParseArg =
505 reinterpret_cast<void *>(kCustomExtensionValue + 1);
506static const char kCustomExtensionContents[] = "custom extension";
507
508static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
509 const uint8_t **out, size_t *out_len,
510 int *out_alert_value, void *add_arg) {
511 if (extension_value != kCustomExtensionValue ||
512 add_arg != kCustomExtensionAddArg) {
513 abort();
514 }
515
516 if (GetConfigPtr(ssl)->custom_extension_skip) {
517 return 0;
518 }
519 if (GetConfigPtr(ssl)->custom_extension_fail_add) {
520 return -1;
521 }
522
523 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
524 *out_len = sizeof(kCustomExtensionContents) - 1;
525
526 return 1;
527}
528
529static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
530 const uint8_t *out, void *add_arg) {
531 if (extension_value != kCustomExtensionValue ||
532 add_arg != kCustomExtensionAddArg ||
533 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
534 abort();
535 }
536}
537
538static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
539 const uint8_t *contents,
540 size_t contents_len,
541 int *out_alert_value, void *parse_arg) {
542 if (extension_value != kCustomExtensionValue ||
543 parse_arg != kCustomExtensionParseArg) {
544 abort();
545 }
546
547 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
548 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
549 *out_alert_value = SSL_AD_DECODE_ERROR;
550 return 0;
551 }
552
553 return 1;
554}
555
David Benjamin87c8a642015-02-21 01:54:29 -0500556// Connect returns a new socket connected to localhost on |port| or -1 on
557// error.
558static int Connect(uint16_t port) {
559 int sock = socket(AF_INET, SOCK_STREAM, 0);
560 if (sock == -1) {
561 PrintSocketError("socket");
562 return -1;
563 }
564 int nodelay = 1;
565 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
566 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
567 PrintSocketError("setsockopt");
568 closesocket(sock);
569 return -1;
570 }
571 sockaddr_in sin;
572 memset(&sin, 0, sizeof(sin));
573 sin.sin_family = AF_INET;
574 sin.sin_port = htons(port);
575 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
576 PrintSocketError("inet_pton");
577 closesocket(sock);
578 return -1;
579 }
580 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
581 sizeof(sin)) != 0) {
582 PrintSocketError("connect");
583 closesocket(sock);
584 return -1;
585 }
586 return sock;
587}
588
589class SocketCloser {
590 public:
591 explicit SocketCloser(int sock) : sock_(sock) {}
592 ~SocketCloser() {
593 // Half-close and drain the socket before releasing it. This seems to be
594 // necessary for graceful shutdown on Windows. It will also avoid write
595 // failures in the test runner.
596#if defined(OPENSSL_WINDOWS)
597 shutdown(sock_, SD_SEND);
598#else
599 shutdown(sock_, SHUT_WR);
600#endif
601 while (true) {
602 char buf[1024];
603 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
604 break;
605 }
606 }
607 closesocket(sock_);
608 }
609
610 private:
611 const int sock_;
612};
613
David Benjaminc273d2c2015-02-09 12:59:46 -0500614static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500615 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
616 config->is_dtls ? DTLS_method() : TLS_method()));
617 if (!ssl_ctx) {
618 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400619 }
620
Adam Langleycef75832015-09-03 14:51:12 -0700621 std::string cipher_list = "ALL";
622 if (!config->cipher.empty()) {
623 cipher_list = config->cipher;
624 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
625 }
626 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
627 return nullptr;
628 }
629
630 if (!config->cipher_tls10.empty() &&
631 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
632 config->cipher_tls10.c_str())) {
633 return nullptr;
634 }
635 if (!config->cipher_tls11.empty() &&
636 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
637 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500638 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400639 }
640
David Benjamina7f333d2015-02-09 02:37:18 -0500641 ScopedDH dh(DH_get_2048_256(NULL));
642 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
643 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400644 }
645
David Benjamin1b8b6912015-02-09 04:28:16 -0500646 if (config->async && config->is_server) {
647 // Disable the internal session cache. To test asynchronous session lookup,
648 // we use an external session cache.
649 SSL_CTX_set_session_cache_mode(
650 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500651 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500652 } else {
653 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
654 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400655
David Benjaminc273d2c2015-02-09 12:59:46 -0500656 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400657
David Benjamin1f5f62b2014-07-12 16:18:02 -0400658 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500659 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400660 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500661 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500662 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400663 }
664
665 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500666 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400667 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400668
Adam Langley49c7af12015-07-10 14:33:46 -0700669 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500670 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400671
David Benjaminc273d2c2015-02-09 12:59:46 -0500672 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500673
David Benjamin87e4acd2015-04-02 19:57:35 -0400674 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400675 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400676
David Benjamind98452d2015-06-16 14:16:23 -0400677 if (config->use_ticket_callback) {
678 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
679 }
680
Adam Langley09505632015-07-30 18:10:13 -0700681 if (config->enable_client_custom_extension &&
682 !SSL_CTX_add_client_custom_ext(
683 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
684 CustomExtensionFreeCallback, kCustomExtensionAddArg,
685 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
686 return nullptr;
687 }
688
689 if (config->enable_server_custom_extension &&
690 !SSL_CTX_add_server_custom_ext(
691 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
692 CustomExtensionFreeCallback, kCustomExtensionAddArg,
693 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
694 return nullptr;
695 }
696
Paul Lietar8f1c2682015-08-18 12:21:54 +0100697 if (config->verify_fail) {
698 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
699 } else {
700 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
701 }
702
Paul Lietar4fac72e2015-09-09 13:44:55 +0100703 if (!config->signed_cert_timestamps.empty() &&
704 !SSL_CTX_set_signed_cert_timestamp_list(
705 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
706 config->signed_cert_timestamps.size())) {
707 return nullptr;
708 }
709
David Benjamin1d5c83e2014-07-22 19:20:02 -0400710 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400711}
712
David Benjamin40f101b2015-02-20 11:23:42 -0500713// RetryAsync is called after a failed operation on |ssl| with return code
714// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400715// event and returns true. Otherwise it returns false.
716static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400717 // No error; don't retry.
718 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500719 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400720 }
David Benjamin83f90402015-01-27 01:09:43 -0500721
David Benjamin6c2563e2015-04-03 03:47:47 -0400722 TestState *test_state = GetTestState(ssl);
723 if (test_state->clock_delta.tv_usec != 0 ||
724 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500725 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400726 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
727 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
728 test_state->clock.tv_usec %= 1000000;
729 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
730 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500731
732 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400733 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500734 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500735 }
David Benjamin40f101b2015-02-20 11:23:42 -0500736 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500737 }
738
David Benjamin43ec06f2014-08-05 02:28:57 -0400739 // See if we needed to read or write more. If so, allow one byte through on
740 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500741 switch (SSL_get_error(ssl, ret)) {
742 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400743 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500744 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500745 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400746 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500747 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500748 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
749 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
750 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500751 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500752 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400753 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500754 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500755 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500756 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400757 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500758 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500759 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400760 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500761 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500762 case SSL_ERROR_PENDING_CERTIFICATE:
763 // The handshake will resume without a second call to the early callback.
764 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400765 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
766 test_state->signature_retries++;
767 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500768 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500769 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400770 }
David Benjamin025b3d32014-07-01 19:53:04 -0400771}
772
David Benjamin6c2563e2015-04-03 03:47:47 -0400773// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
774// the result value of the final |SSL_read| call.
775static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
776 const TestConfig *config = GetConfigPtr(ssl);
777 int ret;
778 do {
779 ret = SSL_read(ssl, out, max_out);
780 } while (config->async && RetryAsync(ssl, ret));
781 return ret;
782}
783
784// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
785// operations. It returns the result of the final |SSL_write| call.
786static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
787 const TestConfig *config = GetConfigPtr(ssl);
788 int ret;
789 do {
790 ret = SSL_write(ssl, in, in_len);
791 if (ret > 0) {
792 in += ret;
793 in_len -= ret;
794 }
795 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
796 return ret;
797}
798
David Benjamin30789da2015-08-29 22:56:45 -0400799// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
800// returns the result of the final |SSL_shutdown| call.
801static int DoShutdown(SSL *ssl) {
802 const TestConfig *config = GetConfigPtr(ssl);
803 int ret;
804 do {
805 ret = SSL_shutdown(ssl);
806 } while (config->async && RetryAsync(ssl, ret));
807 return ret;
808}
809
David Benjamin91eab5c2015-06-18 18:35:46 -0400810// CheckHandshakeProperties checks, immediately after |ssl| completes its
811// initial handshake (or False Starts), whether all the properties are
812// consistent with the test configuration and invariants.
813static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
814 const TestConfig *config = GetConfigPtr(ssl);
815
816 if (SSL_get_current_cipher(ssl) == nullptr) {
817 fprintf(stderr, "null cipher after handshake\n");
818 return false;
819 }
820
821 if (is_resume &&
822 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
823 fprintf(stderr, "session was%s reused\n",
824 SSL_session_reused(ssl) ? "" : " not");
825 return false;
826 }
827
828 bool expect_handshake_done = is_resume || !config->false_start;
829 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
830 fprintf(stderr, "handshake was%s completed\n",
831 GetTestState(ssl)->handshake_done ? "" : " not");
832 return false;
833 }
834
David Benjaminba4594a2015-06-18 18:36:15 -0400835 if (expect_handshake_done && !config->is_server) {
836 bool expect_new_session =
837 !config->expect_no_session &&
838 (!SSL_session_reused(ssl) || config->expect_ticket_renewal);
839 if (expect_new_session != GetTestState(ssl)->got_new_session) {
840 fprintf(stderr,
841 "new session was%s established, but we expected the opposite\n",
842 GetTestState(ssl)->got_new_session ? "" : " not");
843 return false;
844 }
845 }
846
David Benjamin91eab5c2015-06-18 18:35:46 -0400847 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
848 fprintf(stderr, "early callback not called\n");
849 return false;
850 }
851
852 if (!config->expected_server_name.empty()) {
853 const char *server_name =
854 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
855 if (server_name != config->expected_server_name) {
856 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
857 server_name, config->expected_server_name.c_str());
858 return false;
859 }
860 }
861
862 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -0400863 const uint8_t *certificate_types;
864 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -0400865 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -0400866 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -0400867 memcmp(certificate_types,
868 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -0400869 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -0400870 fprintf(stderr, "certificate types mismatch\n");
871 return false;
872 }
873 }
874
875 if (!config->expected_next_proto.empty()) {
876 const uint8_t *next_proto;
877 unsigned next_proto_len;
878 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
879 if (next_proto_len != config->expected_next_proto.size() ||
880 memcmp(next_proto, config->expected_next_proto.data(),
881 next_proto_len) != 0) {
882 fprintf(stderr, "negotiated next proto mismatch\n");
883 return false;
884 }
885 }
886
887 if (!config->expected_alpn.empty()) {
888 const uint8_t *alpn_proto;
889 unsigned alpn_proto_len;
890 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
891 if (alpn_proto_len != config->expected_alpn.size() ||
892 memcmp(alpn_proto, config->expected_alpn.data(),
893 alpn_proto_len) != 0) {
894 fprintf(stderr, "negotiated alpn proto mismatch\n");
895 return false;
896 }
897 }
898
899 if (!config->expected_channel_id.empty()) {
900 uint8_t channel_id[64];
901 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
902 fprintf(stderr, "no channel id negotiated\n");
903 return false;
904 }
905 if (config->expected_channel_id.size() != 64 ||
906 memcmp(config->expected_channel_id.data(),
907 channel_id, 64) != 0) {
908 fprintf(stderr, "channel id mismatch\n");
909 return false;
910 }
911 }
912
913 if (config->expect_extended_master_secret) {
914 if (!ssl->session->extended_master_secret) {
915 fprintf(stderr, "No EMS for session when expected");
916 return false;
917 }
918 }
919
920 if (!config->expected_ocsp_response.empty()) {
921 const uint8_t *data;
922 size_t len;
923 SSL_get0_ocsp_response(ssl, &data, &len);
924 if (config->expected_ocsp_response.size() != len ||
925 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
926 fprintf(stderr, "OCSP response mismatch\n");
927 return false;
928 }
929 }
930
931 if (!config->expected_signed_cert_timestamps.empty()) {
932 const uint8_t *data;
933 size_t len;
934 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
935 if (config->expected_signed_cert_timestamps.size() != len ||
936 memcmp(config->expected_signed_cert_timestamps.data(),
937 data, len) != 0) {
938 fprintf(stderr, "SCT list mismatch\n");
939 return false;
940 }
941 }
942
Paul Lietar8f1c2682015-08-18 12:21:54 +0100943 if (config->expect_verify_result) {
944 int expected_verify_result = config->verify_fail ?
945 X509_V_ERR_APPLICATION_VERIFICATION :
946 X509_V_OK;
947
948 if (SSL_get_verify_result(ssl) != expected_verify_result) {
949 fprintf(stderr, "Wrong certificate verification result\n");
950 return false;
951 }
952 }
953
David Benjamin91eab5c2015-06-18 18:35:46 -0400954 if (!config->is_server) {
955 /* Clients should expect a peer certificate chain iff this was not a PSK
956 * cipher suite. */
957 if (config->psk.empty()) {
958 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
959 fprintf(stderr, "Missing peer certificate chain!\n");
960 return false;
961 }
962 } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
963 fprintf(stderr, "Unexpected peer certificate chain!\n");
964 return false;
965 }
966 }
967 return true;
968}
969
David Benjamin87c8a642015-02-21 01:54:29 -0500970// DoExchange runs a test SSL exchange against the peer. On success, it returns
971// true and sets |*out_session| to the negotiated SSL session. If the test is a
972// resumption attempt, |is_resume| is true and |session| is the session from the
973// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500974static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
975 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500976 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500977 ScopedSSL ssl(SSL_new(ssl_ctx));
978 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500979 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400980 }
981
David Benjamina7f333d2015-02-09 02:37:18 -0500982 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -0500983 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500984 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800985 }
David Benjamin5a593af2014-08-11 19:51:50 -0400986
Adam Langley5f0efe02015-02-20 13:03:16 -0800987 if (config->fallback_scsv &&
988 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
989 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400990 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500991 if (!config->use_early_callback) {
992 if (config->async) {
993 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
994 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
995 } else if (!InstallCertificate(ssl.get())) {
996 return false;
997 }
David Benjamin5a593af2014-08-11 19:51:50 -0400998 }
999 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001000 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001001 NULL);
1002 }
1003 if (config->verify_peer) {
1004 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001005 }
1006 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001007 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001008 }
1009 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001010 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001011 }
1012 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001013 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001014 }
1015 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001016 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001017 }
1018 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001019 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001020 }
1021 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001022 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001023 }
1024 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001025 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001026 }
David Benjamin5c24a1d2014-08-31 00:59:27 -04001027 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -05001028 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -04001029 }
David Benjamin2c99d282015-09-01 10:23:00 -04001030 if (config->microsoft_big_sslv3_buffer) {
1031 SSL_set_options(ssl.get(), SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
1032 }
David Benjamincff0b902015-05-15 23:09:47 -04001033 if (config->no_legacy_server_connect) {
1034 SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
1035 }
David Benjamina08e49d2014-08-24 01:46:07 -04001036 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001037 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001038 }
1039 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001040 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001041 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001042 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -05001043 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
1044 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001045 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001046 }
David Benjamina08e49d2014-08-24 01:46:07 -04001047 }
David Benjamina08e49d2014-08-24 01:46:07 -04001048 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001049 if (!config->host_name.empty() &&
1050 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001051 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001052 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001053 if (!config->advertise_alpn.empty() &&
1054 SSL_set_alpn_protos(ssl.get(),
1055 (const uint8_t *)config->advertise_alpn.data(),
1056 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001057 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001058 }
David Benjamin48cae082014-10-27 01:06:24 -04001059 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001060 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1061 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001062 }
David Benjamin61f95272014-11-25 01:55:35 -05001063 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001064 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001065 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001066 }
David Benjamin61f95272014-11-25 01:55:35 -05001067 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001068 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001069 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001070 }
1071 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001072 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001073 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001074 }
1075 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001076 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001077 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001078 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001079 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001080 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001081 }
1082 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001083 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001084 }
David Benjamin13be1de2015-01-11 16:29:36 -05001085 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001086 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1087 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001088 }
Adam Langley524e7172015-02-20 16:04:00 -08001089 if (config->install_ddos_callback) {
1090 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1091 }
David Benjamin897e5e02015-05-12 17:03:54 -04001092 if (!config->reject_peer_renegotiations) {
1093 /* Renegotiations are disabled by default. */
1094 SSL_set_reject_peer_renegotiations(ssl.get(), 0);
David Benjaminb16346b2015-04-08 19:16:58 -04001095 }
David Benjamin30789da2015-08-29 22:56:45 -04001096 if (!config->check_close_notify) {
1097 SSL_set_quiet_shutdown(ssl.get(), 1);
1098 }
David Benjamin025b3d32014-07-01 19:53:04 -04001099
David Benjamin87c8a642015-02-21 01:54:29 -05001100 int sock = Connect(config->port);
1101 if (sock == -1) {
1102 return false;
1103 }
1104 SocketCloser closer(sock);
1105
1106 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001107 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001108 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001109 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001110 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001111 ScopedBIO packeted =
1112 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -05001113 BIO_push(packeted.get(), bio.release());
1114 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001115 }
David Benjamin5a593af2014-08-11 19:51:50 -04001116 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001117 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001118 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -05001119 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001120 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001121 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001122 }
David Benjamina7f333d2015-02-09 02:37:18 -05001123 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1124 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001125
David Benjamin1d5c83e2014-07-22 19:20:02 -04001126 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001127 if (!config->is_server) {
1128 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001129 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001130 }
1131 } else if (config->async) {
1132 // The internal session cache is disabled, so install the session
1133 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -05001134 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -05001135 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001136 }
1137 }
1138
David Benjamina07c0fc2015-05-13 13:19:42 -04001139 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1140 fprintf(stderr, "non-null cipher before handshake\n");
1141 return false;
1142 }
1143
David Benjamin1d5c83e2014-07-22 19:20:02 -04001144 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001145 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001146 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001147 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001148 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001149 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001150 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001151 } else {
1152 do {
1153 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001154 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001155 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001156 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001157 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001158 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001159 if (ret != 1 ||
1160 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001161 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001162 }
David Benjamin025b3d32014-07-01 19:53:04 -04001163
David Benjaminba4594a2015-06-18 18:36:15 -04001164 // Reset the state to assert later that the callback isn't called in
1165 // renegotations.
1166 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001167 }
1168
David Benjaminc565ebb2015-04-03 04:06:36 -04001169 if (config->export_keying_material > 0) {
1170 std::vector<uint8_t> result(
1171 static_cast<size_t>(config->export_keying_material));
1172 if (!SSL_export_keying_material(
1173 ssl.get(), result.data(), result.size(),
1174 config->export_label.data(), config->export_label.size(),
1175 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1176 config->export_context.size(), config->use_export_context)) {
1177 fprintf(stderr, "failed to export keying material\n");
1178 return false;
1179 }
1180 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1181 return false;
1182 }
1183 }
1184
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001185 if (config->tls_unique) {
1186 uint8_t tls_unique[16];
1187 size_t tls_unique_len;
1188 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1189 sizeof(tls_unique))) {
1190 fprintf(stderr, "failed to get tls-unique\n");
1191 return false;
1192 }
1193
1194 if (tls_unique_len != 12) {
1195 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1196 static_cast<unsigned>(tls_unique_len));
1197 return false;
1198 }
1199
1200 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1201 return false;
1202 }
1203 }
1204
David Benjamin5a593af2014-08-11 19:51:50 -04001205 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001206 if (config->is_dtls) {
1207 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001208 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001209 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001210 // This mode writes a number of different record sizes in an attempt to
1211 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001212 static const size_t kBufLen = 32769;
1213 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1214 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001215 static const size_t kRecordSizes[] = {
1216 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1217 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1218 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001219 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001220 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001221 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001222 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001223 }
Adam Langleybc949292015-06-18 21:32:44 -07001224 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001225 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001226 }
1227 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001228 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001229 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001230 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1231 5) < 0) {
1232 return false;
1233 }
David Benjamine58c4f52014-08-24 03:47:07 -04001234 }
David Benjamin30789da2015-08-29 22:56:45 -04001235 if (!config->shim_shuts_down) {
1236 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001237 static const size_t kBufLen = 16384;
1238 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1239
David Benjamin2c99d282015-09-01 10:23:00 -04001240 // Read only 512 bytes at a time in TLS to ensure records may be
1241 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001242 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001243 int err = SSL_get_error(ssl.get(), n);
1244 if (err == SSL_ERROR_ZERO_RETURN ||
1245 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1246 if (n != 0) {
1247 fprintf(stderr, "Invalid SSL_get_error output\n");
1248 return false;
1249 }
1250 // Stop on either clean or unclean shutdown.
1251 break;
1252 } else if (err != SSL_ERROR_NONE) {
1253 if (n > 0) {
1254 fprintf(stderr, "Invalid SSL_get_error output\n");
1255 return false;
1256 }
1257 return false;
1258 }
1259 // Successfully read data.
1260 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001261 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001262 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001263 }
David Benjamin30789da2015-08-29 22:56:45 -04001264
1265 // After a successful read, with or without False Start, the handshake
1266 // must be complete.
1267 if (!GetTestState(ssl.get())->handshake_done) {
1268 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001269 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001270 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001271
David Benjamin30789da2015-08-29 22:56:45 -04001272 for (int i = 0; i < n; i++) {
1273 buf[i] ^= 0xff;
1274 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001275 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001276 return false;
1277 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001278 }
1279 }
David Benjamin025b3d32014-07-01 19:53:04 -04001280 }
1281
David Benjaminba4594a2015-06-18 18:36:15 -04001282 if (!config->is_server && !config->false_start &&
1283 !config->implicit_handshake &&
1284 GetTestState(ssl.get())->got_new_session) {
1285 fprintf(stderr, "new session was established after the handshake\n");
1286 return false;
1287 }
1288
David Benjamin1d5c83e2014-07-22 19:20:02 -04001289 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001290 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001291 }
1292
David Benjamin30789da2015-08-29 22:56:45 -04001293 ret = DoShutdown(ssl.get());
1294
1295 if (config->shim_shuts_down && config->check_close_notify) {
1296 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1297 // it returns zero when our close_notify is sent, then one when the peer's
1298 // is received.
1299 if (ret != 0) {
1300 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1301 return false;
1302 }
1303 ret = DoShutdown(ssl.get());
1304 }
1305
1306 if (ret != 1) {
1307 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1308 return false;
1309 }
1310
David Benjamin40f101b2015-02-20 11:23:42 -05001311 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001312}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001313
1314int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -05001315#if defined(OPENSSL_WINDOWS)
1316 /* Initialize Winsock. */
1317 WORD wsa_version = MAKEWORD(2, 2);
1318 WSADATA wsa_data;
1319 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1320 if (wsa_err != 0) {
1321 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1322 return 1;
1323 }
1324 if (wsa_data.wVersion != wsa_version) {
1325 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1326 return 1;
1327 }
1328#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001329 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001330#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001331
David Benjamin5a593af2014-08-11 19:51:50 -04001332 if (!SSL_library_init()) {
1333 return 1;
1334 }
David Benjamind9e07012015-02-09 03:04:34 -05001335 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001336 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001337 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001338 return 1;
1339 }
David Benjamin5a593af2014-08-11 19:51:50 -04001340
1341 TestConfig config;
1342 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001343 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001344 }
1345
David Benjaminc273d2c2015-02-09 12:59:46 -05001346 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001347 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001348 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001349 return 1;
1350 }
1351
David Benjamina7f333d2015-02-09 02:37:18 -05001352 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001353 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001354 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001355 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001356 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001357 }
1358
David Benjamin40f101b2015-02-20 11:23:42 -05001359 if (config.resume &&
1360 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001361 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001362 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001363 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001364 }
1365
David Benjamina7f333d2015-02-09 02:37:18 -05001366 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001367}