blob: dabe5ec0b8734c393d9736f81bf0e91460234c50 [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
David Benjaminc273d2c2015-02-09 12:59:46 -0500304static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400305 return 1;
306}
307
David Benjaminc273d2c2015-02-09 12:59:46 -0500308static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
309 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400310 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500311 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400312 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500313 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400314
David Benjamin5a593af2014-08-11 19:51:50 -0400315 *out = (const uint8_t*)config->advertise_npn.data();
316 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400317 return SSL_TLSEXT_ERR_OK;
318}
319
David Benjaminc273d2c2015-02-09 12:59:46 -0500320static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400321 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400322 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500323 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400324 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500325 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400326
David Benjamin5a593af2014-08-11 19:51:50 -0400327 *out = (uint8_t*)config->select_next_proto.data();
328 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400329 return SSL_TLSEXT_ERR_OK;
330}
331
David Benjaminc273d2c2015-02-09 12:59:46 -0500332static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
333 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400334 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500335 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400336 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500337 }
David Benjaminae2888f2014-09-06 12:58:58 -0400338
339 if (!config->expected_advertised_alpn.empty() &&
340 (config->expected_advertised_alpn.size() != inlen ||
341 memcmp(config->expected_advertised_alpn.data(),
342 in, inlen) != 0)) {
343 fprintf(stderr, "bad ALPN select callback inputs\n");
344 exit(1);
345 }
346
347 *out = (const uint8_t*)config->select_alpn.data();
348 *outlen = config->select_alpn.size();
349 return SSL_TLSEXT_ERR_OK;
350}
351
David Benjaminc273d2c2015-02-09 12:59:46 -0500352static unsigned PskClientCallback(SSL *ssl, const char *hint,
353 char *out_identity,
354 unsigned max_identity_len,
355 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400356 const TestConfig *config = GetConfigPtr(ssl);
357
358 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
359 fprintf(stderr, "Server PSK hint did not match.\n");
360 return 0;
361 }
362
363 // Account for the trailing '\0' for the identity.
364 if (config->psk_identity.size() >= max_identity_len ||
365 config->psk.size() > max_psk_len) {
366 fprintf(stderr, "PSK buffers too small\n");
367 return 0;
368 }
369
370 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
371 max_identity_len);
372 memcpy(out_psk, config->psk.data(), config->psk.size());
373 return config->psk.size();
374}
375
David Benjaminc273d2c2015-02-09 12:59:46 -0500376static unsigned PskServerCallback(SSL *ssl, const char *identity,
377 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400378 const TestConfig *config = GetConfigPtr(ssl);
379
380 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
381 fprintf(stderr, "Client PSK identity did not match.\n");
382 return 0;
383 }
384
385 if (config->psk.size() > max_psk_len) {
386 fprintf(stderr, "PSK buffers too small\n");
387 return 0;
388 }
389
390 memcpy(out_psk, config->psk.data(), config->psk.size());
391 return config->psk.size();
392}
393
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400394static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400395 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500396}
397
David Benjaminc273d2c2015-02-09 12:59:46 -0500398static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500399 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500400}
401
David Benjaminc273d2c2015-02-09 12:59:46 -0500402static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500403 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500404 return -1;
405 }
406 if (!InstallCertificate(ssl)) {
407 return 0;
408 }
409 return 1;
410}
411
David Benjaminc273d2c2015-02-09 12:59:46 -0500412static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
413 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500414 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500415 if (async_state->session) {
416 *copy = 0;
417 return async_state->session.release();
418 } else if (async_state->pending_session) {
419 return SSL_magic_pending_session_ptr();
420 } else {
421 return NULL;
422 }
423}
424
Adam Langley524e7172015-02-20 16:04:00 -0800425static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
426 const TestConfig *config = GetConfigPtr(early_context->ssl);
427 static int callback_num = 0;
428
429 callback_num++;
430 if (config->fail_ddos_callback ||
431 (config->fail_second_ddos_callback && callback_num == 2)) {
432 return 0;
433 }
434 return 1;
435}
436
David Benjamin87e4acd2015-04-02 19:57:35 -0400437static void InfoCallback(const SSL *ssl, int type, int val) {
438 if (type == SSL_CB_HANDSHAKE_DONE) {
439 if (GetConfigPtr(ssl)->handshake_never_done) {
440 fprintf(stderr, "handshake completed\n");
441 // Abort before any expected error code is printed, to ensure the overall
442 // test fails.
443 abort();
444 }
445 GetTestState(ssl)->handshake_done = true;
446 }
447}
448
David Benjaminba4594a2015-06-18 18:36:15 -0400449static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
450 GetTestState(ssl)->got_new_session = true;
451 // BoringSSL passes a reference to |session|.
452 SSL_SESSION_free(session);
453 return 1;
454}
455
David Benjamind98452d2015-06-16 14:16:23 -0400456static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
457 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
458 int encrypt) {
459 // This is just test code, so use the all-zeros key.
460 static const uint8_t kZeros[16] = {0};
461
462 if (encrypt) {
463 memcpy(key_name, kZeros, sizeof(kZeros));
464 RAND_bytes(iv, 16);
465 } else if (memcmp(key_name, kZeros, 16) != 0) {
466 return 0;
467 }
468
469 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
470 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
471 return -1;
472 }
473
474 if (!encrypt) {
475 return GetConfigPtr(ssl)->renew_ticket ? 2 : 1;
476 }
477 return 1;
478}
479
Adam Langley09505632015-07-30 18:10:13 -0700480// kCustomExtensionValue is the extension value that the custom extension
481// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700482static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700483static void *const kCustomExtensionAddArg =
484 reinterpret_cast<void *>(kCustomExtensionValue);
485static void *const kCustomExtensionParseArg =
486 reinterpret_cast<void *>(kCustomExtensionValue + 1);
487static const char kCustomExtensionContents[] = "custom extension";
488
489static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
490 const uint8_t **out, size_t *out_len,
491 int *out_alert_value, void *add_arg) {
492 if (extension_value != kCustomExtensionValue ||
493 add_arg != kCustomExtensionAddArg) {
494 abort();
495 }
496
497 if (GetConfigPtr(ssl)->custom_extension_skip) {
498 return 0;
499 }
500 if (GetConfigPtr(ssl)->custom_extension_fail_add) {
501 return -1;
502 }
503
504 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
505 *out_len = sizeof(kCustomExtensionContents) - 1;
506
507 return 1;
508}
509
510static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
511 const uint8_t *out, void *add_arg) {
512 if (extension_value != kCustomExtensionValue ||
513 add_arg != kCustomExtensionAddArg ||
514 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
515 abort();
516 }
517}
518
519static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
520 const uint8_t *contents,
521 size_t contents_len,
522 int *out_alert_value, void *parse_arg) {
523 if (extension_value != kCustomExtensionValue ||
524 parse_arg != kCustomExtensionParseArg) {
525 abort();
526 }
527
528 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
529 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
530 *out_alert_value = SSL_AD_DECODE_ERROR;
531 return 0;
532 }
533
534 return 1;
535}
536
David Benjamin87c8a642015-02-21 01:54:29 -0500537// Connect returns a new socket connected to localhost on |port| or -1 on
538// error.
539static int Connect(uint16_t port) {
540 int sock = socket(AF_INET, SOCK_STREAM, 0);
541 if (sock == -1) {
542 PrintSocketError("socket");
543 return -1;
544 }
545 int nodelay = 1;
546 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
547 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
548 PrintSocketError("setsockopt");
549 closesocket(sock);
550 return -1;
551 }
552 sockaddr_in sin;
553 memset(&sin, 0, sizeof(sin));
554 sin.sin_family = AF_INET;
555 sin.sin_port = htons(port);
556 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
557 PrintSocketError("inet_pton");
558 closesocket(sock);
559 return -1;
560 }
561 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
562 sizeof(sin)) != 0) {
563 PrintSocketError("connect");
564 closesocket(sock);
565 return -1;
566 }
567 return sock;
568}
569
570class SocketCloser {
571 public:
572 explicit SocketCloser(int sock) : sock_(sock) {}
573 ~SocketCloser() {
574 // Half-close and drain the socket before releasing it. This seems to be
575 // necessary for graceful shutdown on Windows. It will also avoid write
576 // failures in the test runner.
577#if defined(OPENSSL_WINDOWS)
578 shutdown(sock_, SD_SEND);
579#else
580 shutdown(sock_, SHUT_WR);
581#endif
582 while (true) {
583 char buf[1024];
584 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
585 break;
586 }
587 }
588 closesocket(sock_);
589 }
590
591 private:
592 const int sock_;
593};
594
David Benjaminc273d2c2015-02-09 12:59:46 -0500595static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500596 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
597 config->is_dtls ? DTLS_method() : TLS_method()));
598 if (!ssl_ctx) {
599 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400600 }
601
David Benjamina7f333d2015-02-09 02:37:18 -0500602 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
603 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400604 }
605
David Benjamina7f333d2015-02-09 02:37:18 -0500606 ScopedDH dh(DH_get_2048_256(NULL));
607 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
608 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400609 }
610
David Benjamin1b8b6912015-02-09 04:28:16 -0500611 if (config->async && config->is_server) {
612 // Disable the internal session cache. To test asynchronous session lookup,
613 // we use an external session cache.
614 SSL_CTX_set_session_cache_mode(
615 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500616 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500617 } else {
618 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
619 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400620
David Benjaminc273d2c2015-02-09 12:59:46 -0500621 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400622
David Benjamin1f5f62b2014-07-12 16:18:02 -0400623 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500624 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400625 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500626 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500627 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400628 }
629
630 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500631 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400632 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400633
Adam Langley49c7af12015-07-10 14:33:46 -0700634 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500635 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400636
David Benjaminc273d2c2015-02-09 12:59:46 -0500637 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500638
David Benjamin87e4acd2015-04-02 19:57:35 -0400639 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400640 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400641
David Benjamind98452d2015-06-16 14:16:23 -0400642 if (config->use_ticket_callback) {
643 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
644 }
645
Adam Langley09505632015-07-30 18:10:13 -0700646 if (config->enable_client_custom_extension &&
647 !SSL_CTX_add_client_custom_ext(
648 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
649 CustomExtensionFreeCallback, kCustomExtensionAddArg,
650 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
651 return nullptr;
652 }
653
654 if (config->enable_server_custom_extension &&
655 !SSL_CTX_add_server_custom_ext(
656 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
657 CustomExtensionFreeCallback, kCustomExtensionAddArg,
658 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
659 return nullptr;
660 }
661
David Benjamin1d5c83e2014-07-22 19:20:02 -0400662 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400663}
664
David Benjamin40f101b2015-02-20 11:23:42 -0500665// RetryAsync is called after a failed operation on |ssl| with return code
666// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400667// event and returns true. Otherwise it returns false.
668static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400669 // No error; don't retry.
670 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500671 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400672 }
David Benjamin83f90402015-01-27 01:09:43 -0500673
David Benjamin6c2563e2015-04-03 03:47:47 -0400674 TestState *test_state = GetTestState(ssl);
675 if (test_state->clock_delta.tv_usec != 0 ||
676 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500677 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400678 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
679 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
680 test_state->clock.tv_usec %= 1000000;
681 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
682 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500683
684 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400685 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500686 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500687 }
David Benjamin40f101b2015-02-20 11:23:42 -0500688 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500689 }
690
David Benjamin43ec06f2014-08-05 02:28:57 -0400691 // See if we needed to read or write more. If so, allow one byte through on
692 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500693 switch (SSL_get_error(ssl, ret)) {
694 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400695 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500696 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500697 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400698 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500699 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500700 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
701 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
702 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500703 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500704 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400705 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500706 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500707 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500708 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400709 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500710 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500711 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400712 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500713 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500714 case SSL_ERROR_PENDING_CERTIFICATE:
715 // The handshake will resume without a second call to the early callback.
716 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400717 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
718 test_state->signature_retries++;
719 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500720 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500721 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400722 }
David Benjamin025b3d32014-07-01 19:53:04 -0400723}
724
David Benjamin6c2563e2015-04-03 03:47:47 -0400725// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
726// the result value of the final |SSL_read| call.
727static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
728 const TestConfig *config = GetConfigPtr(ssl);
729 int ret;
730 do {
731 ret = SSL_read(ssl, out, max_out);
732 } while (config->async && RetryAsync(ssl, ret));
733 return ret;
734}
735
736// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
737// operations. It returns the result of the final |SSL_write| call.
738static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
739 const TestConfig *config = GetConfigPtr(ssl);
740 int ret;
741 do {
742 ret = SSL_write(ssl, in, in_len);
743 if (ret > 0) {
744 in += ret;
745 in_len -= ret;
746 }
747 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
748 return ret;
749}
750
David Benjamin30789da2015-08-29 22:56:45 -0400751// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
752// returns the result of the final |SSL_shutdown| call.
753static int DoShutdown(SSL *ssl) {
754 const TestConfig *config = GetConfigPtr(ssl);
755 int ret;
756 do {
757 ret = SSL_shutdown(ssl);
758 } while (config->async && RetryAsync(ssl, ret));
759 return ret;
760}
761
David Benjamin91eab5c2015-06-18 18:35:46 -0400762// CheckHandshakeProperties checks, immediately after |ssl| completes its
763// initial handshake (or False Starts), whether all the properties are
764// consistent with the test configuration and invariants.
765static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
766 const TestConfig *config = GetConfigPtr(ssl);
767
768 if (SSL_get_current_cipher(ssl) == nullptr) {
769 fprintf(stderr, "null cipher after handshake\n");
770 return false;
771 }
772
773 if (is_resume &&
774 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
775 fprintf(stderr, "session was%s reused\n",
776 SSL_session_reused(ssl) ? "" : " not");
777 return false;
778 }
779
780 bool expect_handshake_done = is_resume || !config->false_start;
781 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
782 fprintf(stderr, "handshake was%s completed\n",
783 GetTestState(ssl)->handshake_done ? "" : " not");
784 return false;
785 }
786
David Benjaminba4594a2015-06-18 18:36:15 -0400787 if (expect_handshake_done && !config->is_server) {
788 bool expect_new_session =
789 !config->expect_no_session &&
790 (!SSL_session_reused(ssl) || config->expect_ticket_renewal);
791 if (expect_new_session != GetTestState(ssl)->got_new_session) {
792 fprintf(stderr,
793 "new session was%s established, but we expected the opposite\n",
794 GetTestState(ssl)->got_new_session ? "" : " not");
795 return false;
796 }
797 }
798
David Benjamin91eab5c2015-06-18 18:35:46 -0400799 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
800 fprintf(stderr, "early callback not called\n");
801 return false;
802 }
803
804 if (!config->expected_server_name.empty()) {
805 const char *server_name =
806 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
807 if (server_name != config->expected_server_name) {
808 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
809 server_name, config->expected_server_name.c_str());
810 return false;
811 }
812 }
813
814 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -0400815 const uint8_t *certificate_types;
816 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -0400817 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -0400818 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -0400819 memcmp(certificate_types,
820 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -0400821 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -0400822 fprintf(stderr, "certificate types mismatch\n");
823 return false;
824 }
825 }
826
827 if (!config->expected_next_proto.empty()) {
828 const uint8_t *next_proto;
829 unsigned next_proto_len;
830 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
831 if (next_proto_len != config->expected_next_proto.size() ||
832 memcmp(next_proto, config->expected_next_proto.data(),
833 next_proto_len) != 0) {
834 fprintf(stderr, "negotiated next proto mismatch\n");
835 return false;
836 }
837 }
838
839 if (!config->expected_alpn.empty()) {
840 const uint8_t *alpn_proto;
841 unsigned alpn_proto_len;
842 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
843 if (alpn_proto_len != config->expected_alpn.size() ||
844 memcmp(alpn_proto, config->expected_alpn.data(),
845 alpn_proto_len) != 0) {
846 fprintf(stderr, "negotiated alpn proto mismatch\n");
847 return false;
848 }
849 }
850
851 if (!config->expected_channel_id.empty()) {
852 uint8_t channel_id[64];
853 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
854 fprintf(stderr, "no channel id negotiated\n");
855 return false;
856 }
857 if (config->expected_channel_id.size() != 64 ||
858 memcmp(config->expected_channel_id.data(),
859 channel_id, 64) != 0) {
860 fprintf(stderr, "channel id mismatch\n");
861 return false;
862 }
863 }
864
865 if (config->expect_extended_master_secret) {
866 if (!ssl->session->extended_master_secret) {
867 fprintf(stderr, "No EMS for session when expected");
868 return false;
869 }
870 }
871
872 if (!config->expected_ocsp_response.empty()) {
873 const uint8_t *data;
874 size_t len;
875 SSL_get0_ocsp_response(ssl, &data, &len);
876 if (config->expected_ocsp_response.size() != len ||
877 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
878 fprintf(stderr, "OCSP response mismatch\n");
879 return false;
880 }
881 }
882
883 if (!config->expected_signed_cert_timestamps.empty()) {
884 const uint8_t *data;
885 size_t len;
886 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
887 if (config->expected_signed_cert_timestamps.size() != len ||
888 memcmp(config->expected_signed_cert_timestamps.data(),
889 data, len) != 0) {
890 fprintf(stderr, "SCT list mismatch\n");
891 return false;
892 }
893 }
894
895 if (!config->is_server) {
896 /* Clients should expect a peer certificate chain iff this was not a PSK
897 * cipher suite. */
898 if (config->psk.empty()) {
899 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
900 fprintf(stderr, "Missing peer certificate chain!\n");
901 return false;
902 }
903 } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
904 fprintf(stderr, "Unexpected peer certificate chain!\n");
905 return false;
906 }
907 }
908 return true;
909}
910
David Benjamin87c8a642015-02-21 01:54:29 -0500911// DoExchange runs a test SSL exchange against the peer. On success, it returns
912// true and sets |*out_session| to the negotiated SSL session. If the test is a
913// resumption attempt, |is_resume| is true and |session| is the session from the
914// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500915static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
916 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500917 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500918 ScopedSSL ssl(SSL_new(ssl_ctx));
919 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500920 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400921 }
922
David Benjamina7f333d2015-02-09 02:37:18 -0500923 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -0500924 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500925 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800926 }
David Benjamin5a593af2014-08-11 19:51:50 -0400927
Adam Langley5f0efe02015-02-20 13:03:16 -0800928 if (config->fallback_scsv &&
929 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
930 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400931 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500932 if (!config->use_early_callback) {
933 if (config->async) {
934 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
935 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
936 } else if (!InstallCertificate(ssl.get())) {
937 return false;
938 }
David Benjamin5a593af2014-08-11 19:51:50 -0400939 }
940 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500941 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500942 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400943 }
944 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500945 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400946 }
947 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500948 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400949 }
950 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500951 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400952 }
953 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500954 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400955 }
956 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500957 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400958 }
959 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500960 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400961 }
962 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500963 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400964 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400965 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500966 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400967 }
David Benjamincff0b902015-05-15 23:09:47 -0400968 if (config->no_legacy_server_connect) {
969 SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
970 }
David Benjamina08e49d2014-08-24 01:46:07 -0400971 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500972 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400973 }
974 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500975 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500976 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500977 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500978 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
979 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500980 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500981 }
David Benjamina08e49d2014-08-24 01:46:07 -0400982 }
David Benjamina08e49d2014-08-24 01:46:07 -0400983 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500984 if (!config->host_name.empty() &&
985 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500986 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400987 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500988 if (!config->advertise_alpn.empty() &&
989 SSL_set_alpn_protos(ssl.get(),
990 (const uint8_t *)config->advertise_alpn.data(),
991 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500992 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400993 }
David Benjamin48cae082014-10-27 01:06:24 -0400994 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500995 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
996 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400997 }
David Benjamin61f95272014-11-25 01:55:35 -0500998 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500999 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001000 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001001 }
David Benjamin61f95272014-11-25 01:55:35 -05001002 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001003 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001004 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001005 }
1006 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001007 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001008 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001009 }
1010 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001011 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001012 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001013 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001014 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001015 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001016 }
1017 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001018 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001019 }
David Benjamin13be1de2015-01-11 16:29:36 -05001020 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001021 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1022 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001023 }
Adam Langley524e7172015-02-20 16:04:00 -08001024 if (config->install_ddos_callback) {
1025 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1026 }
David Benjamin67d1fb52015-03-16 15:16:23 -04001027 if (!config->cipher.empty() &&
1028 !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
1029 return false;
1030 }
David Benjamin897e5e02015-05-12 17:03:54 -04001031 if (!config->reject_peer_renegotiations) {
1032 /* Renegotiations are disabled by default. */
1033 SSL_set_reject_peer_renegotiations(ssl.get(), 0);
David Benjaminb16346b2015-04-08 19:16:58 -04001034 }
David Benjamin30789da2015-08-29 22:56:45 -04001035 if (!config->check_close_notify) {
1036 SSL_set_quiet_shutdown(ssl.get(), 1);
1037 }
David Benjamin025b3d32014-07-01 19:53:04 -04001038
David Benjamin87c8a642015-02-21 01:54:29 -05001039 int sock = Connect(config->port);
1040 if (sock == -1) {
1041 return false;
1042 }
1043 SocketCloser closer(sock);
1044
1045 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001046 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001047 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001048 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001049 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001050 ScopedBIO packeted =
1051 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -05001052 BIO_push(packeted.get(), bio.release());
1053 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001054 }
David Benjamin5a593af2014-08-11 19:51:50 -04001055 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001056 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001057 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -05001058 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001059 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001060 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001061 }
David Benjamina7f333d2015-02-09 02:37:18 -05001062 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1063 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001064
David Benjamin1d5c83e2014-07-22 19:20:02 -04001065 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001066 if (!config->is_server) {
1067 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001068 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001069 }
1070 } else if (config->async) {
1071 // The internal session cache is disabled, so install the session
1072 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -05001073 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -05001074 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001075 }
1076 }
1077
David Benjamina07c0fc2015-05-13 13:19:42 -04001078 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1079 fprintf(stderr, "non-null cipher before handshake\n");
1080 return false;
1081 }
1082
David Benjamin1d5c83e2014-07-22 19:20:02 -04001083 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001084 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001085 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001086 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001087 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001088 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001089 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001090 } else {
1091 do {
1092 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001093 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001094 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001095 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001096 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001097 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001098 if (ret != 1 ||
1099 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001100 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001101 }
David Benjamin025b3d32014-07-01 19:53:04 -04001102
David Benjaminba4594a2015-06-18 18:36:15 -04001103 // Reset the state to assert later that the callback isn't called in
1104 // renegotations.
1105 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001106 }
1107
David Benjaminc565ebb2015-04-03 04:06:36 -04001108 if (config->export_keying_material > 0) {
1109 std::vector<uint8_t> result(
1110 static_cast<size_t>(config->export_keying_material));
1111 if (!SSL_export_keying_material(
1112 ssl.get(), result.data(), result.size(),
1113 config->export_label.data(), config->export_label.size(),
1114 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1115 config->export_context.size(), config->use_export_context)) {
1116 fprintf(stderr, "failed to export keying material\n");
1117 return false;
1118 }
1119 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1120 return false;
1121 }
1122 }
1123
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001124 if (config->tls_unique) {
1125 uint8_t tls_unique[16];
1126 size_t tls_unique_len;
1127 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1128 sizeof(tls_unique))) {
1129 fprintf(stderr, "failed to get tls-unique\n");
1130 return false;
1131 }
1132
1133 if (tls_unique_len != 12) {
1134 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1135 static_cast<unsigned>(tls_unique_len));
1136 return false;
1137 }
1138
1139 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1140 return false;
1141 }
1142 }
1143
David Benjamin5a593af2014-08-11 19:51:50 -04001144 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001145 if (config->is_dtls) {
1146 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001147 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001148 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001149 // This mode writes a number of different record sizes in an attempt to
1150 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001151 static const size_t kBufLen = 32769;
1152 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1153 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001154 static const size_t kRecordSizes[] = {
1155 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1156 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1157 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001158 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001159 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001160 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001161 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001162 }
Adam Langleybc949292015-06-18 21:32:44 -07001163 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001164 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001165 }
1166 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001167 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001168 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001169 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1170 5) < 0) {
1171 return false;
1172 }
David Benjamine58c4f52014-08-24 03:47:07 -04001173 }
David Benjamin30789da2015-08-29 22:56:45 -04001174 if (!config->shim_shuts_down) {
1175 for (;;) {
1176 uint8_t buf[512];
1177 int n = DoRead(ssl.get(), buf, sizeof(buf));
1178 int err = SSL_get_error(ssl.get(), n);
1179 if (err == SSL_ERROR_ZERO_RETURN ||
1180 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1181 if (n != 0) {
1182 fprintf(stderr, "Invalid SSL_get_error output\n");
1183 return false;
1184 }
1185 // Stop on either clean or unclean shutdown.
1186 break;
1187 } else if (err != SSL_ERROR_NONE) {
1188 if (n > 0) {
1189 fprintf(stderr, "Invalid SSL_get_error output\n");
1190 return false;
1191 }
1192 return false;
1193 }
1194 // Successfully read data.
1195 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001196 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001197 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001198 }
David Benjamin30789da2015-08-29 22:56:45 -04001199
1200 // After a successful read, with or without False Start, the handshake
1201 // must be complete.
1202 if (!GetTestState(ssl.get())->handshake_done) {
1203 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001204 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001205 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001206
David Benjamin30789da2015-08-29 22:56:45 -04001207 for (int i = 0; i < n; i++) {
1208 buf[i] ^= 0xff;
1209 }
1210 if (WriteAll(ssl.get(), buf, n) < 0) {
1211 return false;
1212 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001213 }
1214 }
David Benjamin025b3d32014-07-01 19:53:04 -04001215 }
1216
David Benjaminba4594a2015-06-18 18:36:15 -04001217 if (!config->is_server && !config->false_start &&
1218 !config->implicit_handshake &&
1219 GetTestState(ssl.get())->got_new_session) {
1220 fprintf(stderr, "new session was established after the handshake\n");
1221 return false;
1222 }
1223
David Benjamin1d5c83e2014-07-22 19:20:02 -04001224 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001225 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001226 }
1227
David Benjamin30789da2015-08-29 22:56:45 -04001228 ret = DoShutdown(ssl.get());
1229
1230 if (config->shim_shuts_down && config->check_close_notify) {
1231 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1232 // it returns zero when our close_notify is sent, then one when the peer's
1233 // is received.
1234 if (ret != 0) {
1235 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1236 return false;
1237 }
1238 ret = DoShutdown(ssl.get());
1239 }
1240
1241 if (ret != 1) {
1242 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1243 return false;
1244 }
1245
David Benjamin40f101b2015-02-20 11:23:42 -05001246 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001247}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001248
1249int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -05001250#if defined(OPENSSL_WINDOWS)
1251 /* Initialize Winsock. */
1252 WORD wsa_version = MAKEWORD(2, 2);
1253 WSADATA wsa_data;
1254 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1255 if (wsa_err != 0) {
1256 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1257 return 1;
1258 }
1259 if (wsa_data.wVersion != wsa_version) {
1260 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1261 return 1;
1262 }
1263#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001264 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001265#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001266
David Benjamin5a593af2014-08-11 19:51:50 -04001267 if (!SSL_library_init()) {
1268 return 1;
1269 }
David Benjamind9e07012015-02-09 03:04:34 -05001270 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001271 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001272 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001273 return 1;
1274 }
David Benjamin5a593af2014-08-11 19:51:50 -04001275
1276 TestConfig config;
1277 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001278 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001279 }
1280
David Benjaminc273d2c2015-02-09 12:59:46 -05001281 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001282 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001283 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001284 return 1;
1285 }
1286
David Benjamina7f333d2015-02-09 02:37:18 -05001287 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001288 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001289 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001290 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001291 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001292 }
1293
David Benjamin40f101b2015-02-20 11:23:42 -05001294 if (config.resume &&
1295 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001296 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001297 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001298 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001299 }
1300
David Benjamina7f333d2015-02-09 02:37:18 -05001301 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001302}