blob: 9cc1277decebce09696ec90a7abbe1f9573e6a3b [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
David Benjamin4cc36ad2015-12-19 14:23:26 -050015#if !defined(__STDC_FORMAT_MACROS)
16#define __STDC_FORMAT_MACROS
17#endif
18
Adam Langleyded93582014-07-31 15:23:51 -070019#include <openssl/base.h>
20
21#if !defined(OPENSSL_WINDOWS)
David Benjamin025b3d32014-07-01 19:53:04 -040022#include <arpa/inet.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040023#include <netinet/in.h>
David Benjamin87c8a642015-02-21 01:54:29 -050024#include <netinet/tcp.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040025#include <signal.h>
26#include <sys/socket.h>
David Benjamin0abd6f22015-12-04 21:49:53 -050027#include <sys/time.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040028#include <unistd.h>
David Benjamin87c8a642015-02-21 01:54:29 -050029#else
30#include <io.h>
David Benjamina353cdb2016-06-09 16:48:33 -040031OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -070032#include <winsock2.h>
33#include <ws2tcpip.h>
David Benjamina353cdb2016-06-09 16:48:33 -040034OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjamin87c8a642015-02-21 01:54:29 -050035
36#pragma comment(lib, "Ws2_32.lib")
Adam Langleyded93582014-07-31 15:23:51 -070037#endif
38
David Benjamin585d7a42016-06-02 14:58:00 -040039#include <assert.h>
David Benjamin4cc36ad2015-12-19 14:23:26 -050040#include <inttypes.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080041#include <string.h>
David Benjamin025b3d32014-07-01 19:53:04 -040042
David Benjamin025b3d32014-07-01 19:53:04 -040043#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040044#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040045#include <openssl/bytestring.h>
David Benjamind98452d2015-06-16 14:16:23 -040046#include <openssl/cipher.h>
David Benjamin7a1eefd2015-10-17 23:39:22 -040047#include <openssl/crypto.h>
Brian Smith83a82982015-04-09 16:21:10 -100048#include <openssl/err.h>
David Benjamind98452d2015-06-16 14:16:23 -040049#include <openssl/hmac.h>
David Benjamin98193672016-03-25 18:07:11 -040050#include <openssl/nid.h>
David Benjamind98452d2015-06-16 14:16:23 -040051#include <openssl/rand.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040052#include <openssl/ssl.h>
53
David Benjamin45fb1be2015-03-22 16:31:27 -040054#include <memory>
Steven Valdez0d62f262015-09-04 12:41:04 -040055#include <string>
David Benjaminc565ebb2015-04-03 04:06:36 -040056#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040057
Adam Langleyd2b5af52016-07-12 08:03:59 -070058#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040059#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040060#include "packeted_bio.h"
Adam Langleyd2b5af52016-07-12 08:03:59 -070061#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040062#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040063
David Benjamin87c8a642015-02-21 01:54:29 -050064
65#if !defined(OPENSSL_WINDOWS)
66static int closesocket(int sock) {
67 return close(sock);
68}
69
70static void PrintSocketError(const char *func) {
71 perror(func);
72}
73#else
74static void PrintSocketError(const char *func) {
75 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
76}
77#endif
78
David Benjaminc273d2c2015-02-09 12:59:46 -050079static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050080 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040081 return 1;
82}
David Benjamin025b3d32014-07-01 19:53:04 -040083
David Benjamin2d445c02015-02-09 13:03:50 -050084struct TestState {
David Benjamin6c2563e2015-04-03 03:47:47 -040085 // async_bio is async BIO which pauses reads and writes.
86 BIO *async_bio = nullptr;
David Benjamin585d7a42016-06-02 14:58:00 -040087 // packeted_bio is the packeted BIO which simulates read timeouts.
88 BIO *packeted_bio = nullptr;
David Benjamind9e07012015-02-09 03:04:34 -050089 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040090 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050091 ScopedSSL_SESSION session;
92 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040093 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040094 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040095 // private_key is the underlying private key used when testing custom keys.
96 ScopedEVP_PKEY private_key;
nagendra modadugu3398dbf2015-08-07 14:07:52 -070097 std::vector<uint8_t> private_key_result;
98 // private_key_retries is the number of times an asynchronous private key
99 // operation has been retried.
100 unsigned private_key_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400101 bool got_new_session = false;
David Benjamind9e07012015-02-09 03:04:34 -0500102};
103
David Benjamin2d445c02015-02-09 13:03:50 -0500104static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700105 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500106 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500107}
108
109static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500110static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400111
David Benjamin7e7a82d2016-05-20 20:12:42 -0400112static bool SetTestConfig(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500113 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400114}
115
David Benjamin7e7a82d2016-05-20 20:12:42 -0400116static const TestConfig *GetTestConfig(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500117 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400118}
119
David Benjamin22050932015-11-23 13:44:48 -0500120static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
121 // |SSL_set_ex_data| takes ownership of |state| only on success.
122 if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
123 state.release();
David Benjamind9e07012015-02-09 03:04:34 -0500124 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 Benjaminacb6dcc2016-03-10 09:15:01 -0500133static ScopedX509 LoadCertificate(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;
137 }
138 return ScopedX509(PEM_read_bio_X509(bio.get(), NULL, NULL, NULL));
139}
140
David Benjamina7f333d2015-02-09 02:37:18 -0500141static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
142 ScopedBIO bio(BIO_new(BIO_s_file()));
143 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
144 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400145 }
David Benjaminacb6dcc2016-03-10 09:15:01 -0500146 return ScopedEVP_PKEY(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400147}
148
David Benjaminb4d65fd2015-05-29 17:11:21 -0400149static int AsyncPrivateKeyType(SSL *ssl) {
David Benjamin0c0b7e12016-07-14 13:47:55 -0400150 EVP_PKEY *key = GetTestState(ssl)->private_key.get();
151 switch (EVP_PKEY_id(key)) {
152 case EVP_PKEY_RSA:
153 return NID_rsaEncryption;
154 case EVP_PKEY_EC:
155 return EC_GROUP_get_curve_name(
156 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(key)));
157 default:
158 return NID_undef;
159 }
David Benjaminb4d65fd2015-05-29 17:11:21 -0400160}
161
David Benjaminb4d65fd2015-05-29 17:11:21 -0400162static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
163 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
164}
165
166static ssl_private_key_result_t AsyncPrivateKeySign(
167 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamind3440b42016-07-14 14:52:41 -0400168 uint16_t signature_algorithm, const uint8_t *in, size_t in_len) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400169 TestState *test_state = GetTestState(ssl);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700170 if (!test_state->private_key_result.empty()) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400171 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
172 abort();
173 }
174
David Benjamind3440b42016-07-14 14:52:41 -0400175 // Determine the hash.
176 const EVP_MD *md;
177 switch (signature_algorithm) {
178 case SSL_SIGN_RSA_PKCS1_SHA1:
179 case SSL_SIGN_ECDSA_SHA1:
180 md = EVP_sha1();
181 break;
182 case SSL_SIGN_RSA_PKCS1_SHA256:
183 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
184 case SSL_SIGN_RSA_PSS_SHA256:
185 md = EVP_sha256();
186 break;
187 case SSL_SIGN_RSA_PKCS1_SHA384:
188 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
189 case SSL_SIGN_RSA_PSS_SHA384:
190 md = EVP_sha384();
191 break;
192 case SSL_SIGN_RSA_PKCS1_SHA512:
193 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
194 case SSL_SIGN_RSA_PSS_SHA512:
195 md = EVP_sha512();
196 break;
197 case SSL_SIGN_RSA_PKCS1_MD5_SHA1:
198 md = EVP_md5_sha1();
199 break;
200 default:
201 fprintf(stderr, "Unknown signature algorithm %04x.\n",
202 signature_algorithm);
203 return ssl_private_key_failure;
204 }
205
206 ScopedEVP_MD_CTX ctx;
207 EVP_PKEY_CTX *pctx;
208 if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr,
209 test_state->private_key.get())) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400210 return ssl_private_key_failure;
211 }
212
David Benjamind3440b42016-07-14 14:52:41 -0400213 // Configure additional signature parameters.
214 switch (signature_algorithm) {
215 case SSL_SIGN_RSA_PSS_SHA256:
216 case SSL_SIGN_RSA_PSS_SHA384:
217 case SSL_SIGN_RSA_PSS_SHA512:
218 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
219 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
220 -1 /* salt len = hash len */)) {
221 return ssl_private_key_failure;
222 }
223 }
224
David Benjaminb4d65fd2015-05-29 17:11:21 -0400225 // Write the signature into |test_state|.
226 size_t len = 0;
David Benjamind3440b42016-07-14 14:52:41 -0400227 if (!EVP_DigestSignUpdate(ctx.get(), in, in_len) ||
228 !EVP_DigestSignFinal(ctx.get(), nullptr, &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400229 return ssl_private_key_failure;
230 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700231 test_state->private_key_result.resize(len);
David Benjamind3440b42016-07-14 14:52:41 -0400232 if (!EVP_DigestSignFinal(ctx.get(), test_state->private_key_result.data(),
233 &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400234 return ssl_private_key_failure;
235 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700236 test_state->private_key_result.resize(len);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400237
David Benjamind3440b42016-07-14 14:52:41 -0400238 // The signature will be released asynchronously in |AsyncPrivateKeyComplete|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400239 return ssl_private_key_retry;
240}
241
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700242static ssl_private_key_result_t AsyncPrivateKeyDecrypt(
243 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
244 const uint8_t *in, size_t in_len) {
245 TestState *test_state = GetTestState(ssl);
246 if (!test_state->private_key_result.empty()) {
247 fprintf(stderr,
248 "AsyncPrivateKeyDecrypt called with operation pending.\n");
249 abort();
250 }
251
David Benjamin758d1272015-11-20 17:47:25 -0500252 RSA *rsa = EVP_PKEY_get0_RSA(test_state->private_key.get());
253 if (rsa == NULL) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700254 fprintf(stderr,
255 "AsyncPrivateKeyDecrypt called with incorrect key type.\n");
256 abort();
257 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700258 test_state->private_key_result.resize(RSA_size(rsa));
David Benjaminef14b2d2015-11-11 14:01:27 -0800259 if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700260 RSA_size(rsa), in, in_len, RSA_NO_PADDING)) {
261 return ssl_private_key_failure;
262 }
263
264 test_state->private_key_result.resize(*out_len);
265
David Benjamind3440b42016-07-14 14:52:41 -0400266 // The decryption will be released asynchronously in |AsyncPrivateComplete|.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700267 return ssl_private_key_retry;
268}
269
David Benjamind3440b42016-07-14 14:52:41 -0400270static ssl_private_key_result_t AsyncPrivateKeyComplete(
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700271 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
272 TestState *test_state = GetTestState(ssl);
273 if (test_state->private_key_result.empty()) {
274 fprintf(stderr,
David Benjamind3440b42016-07-14 14:52:41 -0400275 "AsyncPrivateKeyComplete called without operation pending.\n");
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700276 abort();
277 }
278
279 if (test_state->private_key_retries < 2) {
280 // Only return the decryption on the second attempt, to test both incomplete
281 // |decrypt| and |decrypt_complete|.
282 return ssl_private_key_retry;
283 }
284
285 if (max_out < test_state->private_key_result.size()) {
286 fprintf(stderr, "Output buffer too small.\n");
287 return ssl_private_key_failure;
288 }
David Benjaminef14b2d2015-11-11 14:01:27 -0800289 memcpy(out, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700290 test_state->private_key_result.size());
291 *out_len = test_state->private_key_result.size();
292
293 test_state->private_key_result.clear();
294 test_state->private_key_retries = 0;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400295 return ssl_private_key_success;
296}
297
298static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
299 AsyncPrivateKeyType,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400300 AsyncPrivateKeyMaxSignatureLen,
301 AsyncPrivateKeySign,
David Benjamind3440b42016-07-14 14:52:41 -0400302 nullptr /* sign_digest */,
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700303 AsyncPrivateKeyDecrypt,
David Benjamind3440b42016-07-14 14:52:41 -0400304 AsyncPrivateKeyComplete,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400305};
306
Steven Valdez0d62f262015-09-04 12:41:04 -0400307template<typename T>
Adam Langley10f97f32016-07-12 08:09:33 -0700308struct Free {
Steven Valdez0d62f262015-09-04 12:41:04 -0400309 void operator()(T *buf) {
310 free(buf);
311 }
312};
313
David Benjaminacb6dcc2016-03-10 09:15:01 -0500314static bool GetCertificate(SSL *ssl, ScopedX509 *out_x509,
315 ScopedEVP_PKEY *out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400316 const TestConfig *config = GetTestConfig(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -0400317
318 if (!config->digest_prefs.empty()) {
Adam Langley10f97f32016-07-12 08:09:33 -0700319 std::unique_ptr<char, Free<char>> digest_prefs(
Steven Valdez0d62f262015-09-04 12:41:04 -0400320 strdup(config->digest_prefs.c_str()));
Steven Valdez0d62f262015-09-04 12:41:04 -0400321 std::vector<int> digest_list;
322
323 for (;;) {
Adam Langley67251f22015-09-23 15:01:07 -0700324 char *token =
325 strtok(digest_list.empty() ? digest_prefs.get() : nullptr, ",");
Steven Valdez0d62f262015-09-04 12:41:04 -0400326 if (token == nullptr) {
327 break;
328 }
329
330 digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token)));
331 }
332
333 if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(),
334 digest_list.size())) {
335 return false;
336 }
337 }
338
David Benjaminca3d5452016-07-14 12:51:01 -0400339 if (!config->signing_prefs.empty()) {
340 std::vector<uint16_t> u16s(config->signing_prefs.begin(),
341 config->signing_prefs.end());
342 if (!SSL_set_signing_algorithm_prefs(ssl, u16s.data(), u16s.size())) {
343 return false;
344 }
345 }
346
David Benjaminb4d65fd2015-05-29 17:11:21 -0400347 if (!config->key_file.empty()) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500348 *out_pkey = LoadPrivateKey(config->key_file.c_str());
349 if (!*out_pkey) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400350 return false;
351 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500352 }
David Benjaminacb6dcc2016-03-10 09:15:01 -0500353 if (!config->cert_file.empty()) {
354 *out_x509 = LoadCertificate(config->cert_file.c_str());
355 if (!*out_x509) {
356 return false;
357 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500358 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100359 if (!config->ocsp_response.empty() &&
360 !SSL_CTX_set_ocsp_response(ssl->ctx,
361 (const uint8_t *)config->ocsp_response.data(),
362 config->ocsp_response.size())) {
363 return false;
364 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500365 return true;
366}
367
David Benjaminacb6dcc2016-03-10 09:15:01 -0500368static bool InstallCertificate(SSL *ssl) {
369 ScopedX509 x509;
370 ScopedEVP_PKEY pkey;
371 if (!GetCertificate(ssl, &x509, &pkey)) {
372 return false;
373 }
374
375 if (pkey) {
376 TestState *test_state = GetTestState(ssl);
David Benjamin7e7a82d2016-05-20 20:12:42 -0400377 const TestConfig *config = GetTestConfig(ssl);
David Benjaminacb6dcc2016-03-10 09:15:01 -0500378 if (config->async) {
379 test_state->private_key = std::move(pkey);
380 SSL_set_private_key_method(ssl, &g_async_private_key_method);
381 } else if (!SSL_use_PrivateKey(ssl, pkey.get())) {
382 return false;
383 }
384 }
385
386 if (x509 && !SSL_use_certificate(ssl, x509.get())) {
387 return false;
388 }
389
390 return true;
391}
392
David Benjaminc273d2c2015-02-09 12:59:46 -0500393static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400394 const TestConfig *config = GetTestConfig(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500395 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400396
David Benjamin6f5c0f42015-02-24 01:23:21 -0500397 if (!config->expected_server_name.empty()) {
398 const uint8_t *extension_data;
399 size_t extension_len;
400 CBS extension, server_name_list, host_name;
401 uint8_t name_type;
402
403 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
404 &extension_data,
405 &extension_len)) {
406 fprintf(stderr, "Could not find server_name extension.\n");
407 return -1;
408 }
409
410 CBS_init(&extension, extension_data, extension_len);
411 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
412 CBS_len(&extension) != 0 ||
413 !CBS_get_u8(&server_name_list, &name_type) ||
414 name_type != TLSEXT_NAMETYPE_host_name ||
415 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
416 CBS_len(&server_name_list) != 0) {
417 fprintf(stderr, "Could not decode server_name extension.\n");
418 return -1;
419 }
420
421 if (!CBS_mem_equal(&host_name,
422 (const uint8_t*)config->expected_server_name.data(),
423 config->expected_server_name.size())) {
424 fprintf(stderr, "Server name mismatch.\n");
425 }
David Benjamin7b030512014-07-08 17:30:11 -0400426 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400427
David Benjamin6f5c0f42015-02-24 01:23:21 -0500428 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400429 return -1;
430 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400431
David Benjamin6f5c0f42015-02-24 01:23:21 -0500432 // Install the certificate in the early callback.
433 if (config->use_early_callback) {
434 if (config->async) {
435 // Install the certificate asynchronously.
436 return 0;
437 }
438 if (!InstallCertificate(ctx->ssl)) {
439 return -1;
440 }
David Benjamin7b030512014-07-08 17:30:11 -0400441 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400442 return 1;
443}
David Benjamin025b3d32014-07-01 19:53:04 -0400444
David Benjaminacb6dcc2016-03-10 09:15:01 -0500445static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400446 if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500447 return -1;
448 }
449
450 ScopedX509 x509;
451 ScopedEVP_PKEY pkey;
452 if (!GetCertificate(ssl, &x509, &pkey)) {
453 return -1;
454 }
455
456 // Return zero for no certificate.
457 if (!x509) {
458 return 0;
459 }
460
461 // Asynchronous private keys are not supported with client_cert_cb.
462 *out_x509 = x509.release();
463 *out_pkey = pkey.release();
464 return 1;
465}
466
Paul Lietar8f1c2682015-08-18 12:21:54 +0100467static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
468 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
469 SSL_get_ex_data_X509_STORE_CTX_idx());
David Benjamin7e7a82d2016-05-20 20:12:42 -0400470 const TestConfig *config = GetTestConfig(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100471
472 if (!config->expected_ocsp_response.empty()) {
473 const uint8_t *data;
474 size_t len;
475 SSL_get0_ocsp_response(ssl, &data, &len);
476 if (len == 0) {
477 fprintf(stderr, "OCSP response not available in verify callback\n");
478 return 0;
479 }
480 }
481
David Benjamin67666e72014-07-12 15:47:52 -0400482 return 1;
483}
484
Paul Lietar8f1c2682015-08-18 12:21:54 +0100485static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
486 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
487 return 0;
488}
489
David Benjaminc273d2c2015-02-09 12:59:46 -0500490static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
491 unsigned int *out_len, void *arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400492 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500493 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400494 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500495 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400496
David Benjamin5a593af2014-08-11 19:51:50 -0400497 *out = (const uint8_t*)config->advertise_npn.data();
498 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400499 return SSL_TLSEXT_ERR_OK;
500}
501
David Benjaminc273d2c2015-02-09 12:59:46 -0500502static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400503 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400504 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500505 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400506 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500507 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400508
David Benjamin5a593af2014-08-11 19:51:50 -0400509 *out = (uint8_t*)config->select_next_proto.data();
510 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400511 return SSL_TLSEXT_ERR_OK;
512}
513
David Benjaminc273d2c2015-02-09 12:59:46 -0500514static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
515 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400516 const TestConfig *config = GetTestConfig(ssl);
David Benjamin594e7d22016-03-17 17:49:56 -0400517 if (config->decline_alpn) {
David Benjaminae2888f2014-09-06 12:58:58 -0400518 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500519 }
David Benjaminae2888f2014-09-06 12:58:58 -0400520
521 if (!config->expected_advertised_alpn.empty() &&
522 (config->expected_advertised_alpn.size() != inlen ||
523 memcmp(config->expected_advertised_alpn.data(),
524 in, inlen) != 0)) {
525 fprintf(stderr, "bad ALPN select callback inputs\n");
526 exit(1);
527 }
528
529 *out = (const uint8_t*)config->select_alpn.data();
530 *outlen = config->select_alpn.size();
531 return SSL_TLSEXT_ERR_OK;
532}
533
David Benjaminc273d2c2015-02-09 12:59:46 -0500534static unsigned PskClientCallback(SSL *ssl, const char *hint,
535 char *out_identity,
536 unsigned max_identity_len,
537 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400538 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400539
540 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
541 fprintf(stderr, "Server PSK hint did not match.\n");
542 return 0;
543 }
544
545 // Account for the trailing '\0' for the identity.
546 if (config->psk_identity.size() >= max_identity_len ||
547 config->psk.size() > max_psk_len) {
548 fprintf(stderr, "PSK buffers too small\n");
549 return 0;
550 }
551
552 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
553 max_identity_len);
554 memcpy(out_psk, config->psk.data(), config->psk.size());
555 return config->psk.size();
556}
557
David Benjaminc273d2c2015-02-09 12:59:46 -0500558static unsigned PskServerCallback(SSL *ssl, const char *identity,
559 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400560 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400561
562 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
563 fprintf(stderr, "Client PSK identity did not match.\n");
564 return 0;
565 }
566
567 if (config->psk.size() > max_psk_len) {
568 fprintf(stderr, "PSK buffers too small\n");
569 return 0;
570 }
571
572 memcpy(out_psk, config->psk.data(), config->psk.size());
573 return config->psk.size();
574}
575
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400576static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin585d7a42016-06-02 14:58:00 -0400577 *out_clock = PacketedBioGetClock(GetTestState(ssl)->packeted_bio);
David Benjamin377fc312015-01-26 00:22:12 -0500578}
579
David Benjaminc273d2c2015-02-09 12:59:46 -0500580static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500581 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500582}
583
David Benjaminc273d2c2015-02-09 12:59:46 -0500584static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500585 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500586 return -1;
587 }
588 if (!InstallCertificate(ssl)) {
589 return 0;
590 }
591 return 1;
592}
593
David Benjaminc273d2c2015-02-09 12:59:46 -0500594static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
595 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500596 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500597 if (async_state->session) {
598 *copy = 0;
599 return async_state->session.release();
600 } else if (async_state->pending_session) {
601 return SSL_magic_pending_session_ptr();
602 } else {
603 return NULL;
604 }
605}
606
Adam Langley524e7172015-02-20 16:04:00 -0800607static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400608 const TestConfig *config = GetTestConfig(early_context->ssl);
Adam Langley524e7172015-02-20 16:04:00 -0800609 static int callback_num = 0;
610
611 callback_num++;
612 if (config->fail_ddos_callback ||
613 (config->fail_second_ddos_callback && callback_num == 2)) {
614 return 0;
615 }
616 return 1;
617}
618
David Benjamin87e4acd2015-04-02 19:57:35 -0400619static void InfoCallback(const SSL *ssl, int type, int val) {
620 if (type == SSL_CB_HANDSHAKE_DONE) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400621 if (GetTestConfig(ssl)->handshake_never_done) {
David Benjamin87e4acd2015-04-02 19:57:35 -0400622 fprintf(stderr, "handshake completed\n");
623 // Abort before any expected error code is printed, to ensure the overall
624 // test fails.
625 abort();
626 }
627 GetTestState(ssl)->handshake_done = true;
628 }
629}
630
David Benjaminba4594a2015-06-18 18:36:15 -0400631static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
632 GetTestState(ssl)->got_new_session = true;
633 // BoringSSL passes a reference to |session|.
634 SSL_SESSION_free(session);
635 return 1;
636}
637
David Benjamind98452d2015-06-16 14:16:23 -0400638static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
639 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
640 int encrypt) {
641 // This is just test code, so use the all-zeros key.
642 static const uint8_t kZeros[16] = {0};
643
644 if (encrypt) {
645 memcpy(key_name, kZeros, sizeof(kZeros));
646 RAND_bytes(iv, 16);
647 } else if (memcmp(key_name, kZeros, 16) != 0) {
648 return 0;
649 }
650
651 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
652 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
653 return -1;
654 }
655
656 if (!encrypt) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400657 return GetTestConfig(ssl)->renew_ticket ? 2 : 1;
David Benjamind98452d2015-06-16 14:16:23 -0400658 }
659 return 1;
660}
661
Adam Langley09505632015-07-30 18:10:13 -0700662// kCustomExtensionValue is the extension value that the custom extension
663// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700664static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700665static void *const kCustomExtensionAddArg =
666 reinterpret_cast<void *>(kCustomExtensionValue);
667static void *const kCustomExtensionParseArg =
668 reinterpret_cast<void *>(kCustomExtensionValue + 1);
669static const char kCustomExtensionContents[] = "custom extension";
670
671static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
672 const uint8_t **out, size_t *out_len,
673 int *out_alert_value, void *add_arg) {
674 if (extension_value != kCustomExtensionValue ||
675 add_arg != kCustomExtensionAddArg) {
676 abort();
677 }
678
David Benjamin7e7a82d2016-05-20 20:12:42 -0400679 if (GetTestConfig(ssl)->custom_extension_skip) {
Adam Langley09505632015-07-30 18:10:13 -0700680 return 0;
681 }
David Benjamin7e7a82d2016-05-20 20:12:42 -0400682 if (GetTestConfig(ssl)->custom_extension_fail_add) {
Adam Langley09505632015-07-30 18:10:13 -0700683 return -1;
684 }
685
686 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
687 *out_len = sizeof(kCustomExtensionContents) - 1;
688
689 return 1;
690}
691
692static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
693 const uint8_t *out, void *add_arg) {
694 if (extension_value != kCustomExtensionValue ||
695 add_arg != kCustomExtensionAddArg ||
696 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
697 abort();
698 }
699}
700
701static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
702 const uint8_t *contents,
703 size_t contents_len,
704 int *out_alert_value, void *parse_arg) {
705 if (extension_value != kCustomExtensionValue ||
706 parse_arg != kCustomExtensionParseArg) {
707 abort();
708 }
709
710 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
711 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
712 *out_alert_value = SSL_AD_DECODE_ERROR;
713 return 0;
714 }
715
716 return 1;
717}
718
David Benjamin87c8a642015-02-21 01:54:29 -0500719// Connect returns a new socket connected to localhost on |port| or -1 on
720// error.
721static int Connect(uint16_t port) {
722 int sock = socket(AF_INET, SOCK_STREAM, 0);
723 if (sock == -1) {
724 PrintSocketError("socket");
725 return -1;
726 }
727 int nodelay = 1;
728 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
729 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
730 PrintSocketError("setsockopt");
731 closesocket(sock);
732 return -1;
733 }
734 sockaddr_in sin;
735 memset(&sin, 0, sizeof(sin));
736 sin.sin_family = AF_INET;
737 sin.sin_port = htons(port);
738 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
739 PrintSocketError("inet_pton");
740 closesocket(sock);
741 return -1;
742 }
743 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
744 sizeof(sin)) != 0) {
745 PrintSocketError("connect");
746 closesocket(sock);
747 return -1;
748 }
749 return sock;
750}
751
752class SocketCloser {
753 public:
754 explicit SocketCloser(int sock) : sock_(sock) {}
755 ~SocketCloser() {
756 // Half-close and drain the socket before releasing it. This seems to be
757 // necessary for graceful shutdown on Windows. It will also avoid write
758 // failures in the test runner.
759#if defined(OPENSSL_WINDOWS)
760 shutdown(sock_, SD_SEND);
761#else
762 shutdown(sock_, SHUT_WR);
763#endif
764 while (true) {
765 char buf[1024];
766 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
767 break;
768 }
769 }
770 closesocket(sock_);
771 }
772
773 private:
774 const int sock_;
775};
776
David Benjaminc273d2c2015-02-09 12:59:46 -0500777static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500778 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
779 config->is_dtls ? DTLS_method() : TLS_method()));
780 if (!ssl_ctx) {
781 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400782 }
783
Nick Harper1fd39d82016-06-14 18:14:35 -0700784 if (!config->is_dtls) {
785 // Enable TLS 1.3 for tests.
786 SSL_CTX_set_max_version(ssl_ctx.get(), TLS1_3_VERSION);
787 }
788
Adam Langleycef75832015-09-03 14:51:12 -0700789 std::string cipher_list = "ALL";
790 if (!config->cipher.empty()) {
791 cipher_list = config->cipher;
792 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
793 }
794 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
795 return nullptr;
796 }
797
798 if (!config->cipher_tls10.empty() &&
799 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
800 config->cipher_tls10.c_str())) {
801 return nullptr;
802 }
803 if (!config->cipher_tls11.empty() &&
804 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
805 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500806 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400807 }
808
David Benjamina7f333d2015-02-09 02:37:18 -0500809 ScopedDH dh(DH_get_2048_256(NULL));
David Benjaminb7c5e842016-03-28 09:59:10 -0400810 if (!dh) {
811 return nullptr;
812 }
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800813
814 if (config->use_sparse_dh_prime) {
815 // This prime number is 2^1024 + 643 – a value just above a power of two.
816 // Because of its form, values modulo it are essentially certain to be one
817 // byte shorter. This is used to test padding of these values.
818 if (BN_hex2bn(
819 &dh->p,
820 "1000000000000000000000000000000000000000000000000000000000000000"
821 "0000000000000000000000000000000000000000000000000000000000000000"
822 "0000000000000000000000000000000000000000000000000000000000000000"
823 "0000000000000000000000000000000000000000000000000000000000000028"
824 "3") == 0 ||
825 !BN_set_word(dh->g, 2)) {
826 return nullptr;
827 }
David Benjamine66148a2016-02-02 14:14:36 -0500828 BN_free(dh->q);
829 dh->q = NULL;
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800830 dh->priv_length = 0;
831 }
832
David Benjaminb7c5e842016-03-28 09:59:10 -0400833 if (!SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500834 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400835 }
836
David Benjamin1b8b6912015-02-09 04:28:16 -0500837 if (config->async && config->is_server) {
838 // Disable the internal session cache. To test asynchronous session lookup,
839 // we use an external session cache.
840 SSL_CTX_set_session_cache_mode(
841 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500842 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500843 } else {
844 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
845 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400846
David Benjamind4c2bce2015-10-17 12:28:18 -0400847 SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400848
David Benjaminacb6dcc2016-03-10 09:15:01 -0500849 if (config->use_old_client_cert_callback) {
850 SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback);
851 }
852
David Benjamin1f5f62b2014-07-12 16:18:02 -0400853 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500854 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400855 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500856 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500857 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400858 }
859
David Benjamin594e7d22016-03-17 17:49:56 -0400860 if (!config->select_alpn.empty() || config->decline_alpn) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500861 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400862 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400863
Adam Langley49c7af12015-07-10 14:33:46 -0700864 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500865 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400866
David Benjamin82d0ffb2016-06-08 19:19:58 -0400867 SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback);
David Benjamin377fc312015-01-26 00:22:12 -0500868
David Benjamin87e4acd2015-04-02 19:57:35 -0400869 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400870 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400871
David Benjamind98452d2015-06-16 14:16:23 -0400872 if (config->use_ticket_callback) {
873 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
874 }
875
Adam Langley09505632015-07-30 18:10:13 -0700876 if (config->enable_client_custom_extension &&
877 !SSL_CTX_add_client_custom_ext(
878 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
879 CustomExtensionFreeCallback, kCustomExtensionAddArg,
880 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
881 return nullptr;
882 }
883
884 if (config->enable_server_custom_extension &&
885 !SSL_CTX_add_server_custom_ext(
886 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
887 CustomExtensionFreeCallback, kCustomExtensionAddArg,
888 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
889 return nullptr;
890 }
891
Paul Lietar8f1c2682015-08-18 12:21:54 +0100892 if (config->verify_fail) {
893 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
894 } else {
895 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
896 }
897
Paul Lietar4fac72e2015-09-09 13:44:55 +0100898 if (!config->signed_cert_timestamps.empty() &&
899 !SSL_CTX_set_signed_cert_timestamp_list(
900 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
901 config->signed_cert_timestamps.size())) {
902 return nullptr;
903 }
904
David Benjamin2f8935d2016-07-13 19:47:39 -0400905 if (config->use_null_client_ca_list) {
906 SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr);
907 }
908
David Benjamin1d5c83e2014-07-22 19:20:02 -0400909 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400910}
911
David Benjamin40f101b2015-02-20 11:23:42 -0500912// RetryAsync is called after a failed operation on |ssl| with return code
913// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400914// event and returns true. Otherwise it returns false.
915static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400916 // No error; don't retry.
917 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500918 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400919 }
David Benjamin83f90402015-01-27 01:09:43 -0500920
David Benjamin6c2563e2015-04-03 03:47:47 -0400921 TestState *test_state = GetTestState(ssl);
Matt Braithwaite6278e242016-06-14 08:18:22 -0700922 assert(GetTestConfig(ssl)->async);
David Benjamin83f90402015-01-27 01:09:43 -0500923
David Benjamin585d7a42016-06-02 14:58:00 -0400924 if (test_state->packeted_bio != nullptr &&
925 PacketedBioAdvanceClock(test_state->packeted_bio)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500926 // The DTLS retransmit logic silently ignores write failures. So the test
927 // may progress, allow writes through synchronously.
David Benjamin585d7a42016-06-02 14:58:00 -0400928 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
David Benjamin13e81fc2015-11-02 17:16:13 -0500929 int timeout_ret = DTLSv1_handle_timeout(ssl);
David Benjamin585d7a42016-06-02 14:58:00 -0400930 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
David Benjamin13e81fc2015-11-02 17:16:13 -0500931
932 if (timeout_ret < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400933 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500934 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500935 }
David Benjamin40f101b2015-02-20 11:23:42 -0500936 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500937 }
938
David Benjamin43ec06f2014-08-05 02:28:57 -0400939 // See if we needed to read or write more. If so, allow one byte through on
940 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500941 switch (SSL_get_error(ssl, ret)) {
942 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400943 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500944 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500945 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400946 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500947 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500948 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400949 ScopedEVP_PKEY pkey = LoadPrivateKey(GetTestConfig(ssl)->send_channel_id);
David Benjamin9d0847a2015-02-16 03:57:55 -0500950 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500951 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500952 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400953 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500954 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500955 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500956 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400957 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500958 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500959 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400960 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500961 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500962 case SSL_ERROR_PENDING_CERTIFICATE:
963 // The handshake will resume without a second call to the early callback.
964 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400965 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700966 test_state->private_key_retries++;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400967 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500968 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500969 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400970 }
David Benjamin025b3d32014-07-01 19:53:04 -0400971}
972
David Benjamin6c2563e2015-04-03 03:47:47 -0400973// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
974// the result value of the final |SSL_read| call.
975static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400976 const TestConfig *config = GetTestConfig(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -0500977 TestState *test_state = GetTestState(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -0400978 int ret;
979 do {
David Benjamin13e81fc2015-11-02 17:16:13 -0500980 if (config->async) {
981 // The DTLS retransmit logic silently ignores write failures. So the test
982 // may progress, allow writes through synchronously. |SSL_read| may
983 // trigger a retransmit, so disconnect the write quota.
984 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
985 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400986 ret = SSL_read(ssl, out, max_out);
David Benjamin13e81fc2015-11-02 17:16:13 -0500987 if (config->async) {
988 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
989 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400990 } while (config->async && RetryAsync(ssl, ret));
991 return ret;
992}
993
994// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
995// operations. It returns the result of the final |SSL_write| call.
996static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400997 const TestConfig *config = GetTestConfig(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -0400998 int ret;
999 do {
1000 ret = SSL_write(ssl, in, in_len);
1001 if (ret > 0) {
1002 in += ret;
1003 in_len -= ret;
1004 }
1005 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
1006 return ret;
1007}
1008
David Benjamin30789da2015-08-29 22:56:45 -04001009// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
1010// returns the result of the final |SSL_shutdown| call.
1011static int DoShutdown(SSL *ssl) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001012 const TestConfig *config = GetTestConfig(ssl);
David Benjamin30789da2015-08-29 22:56:45 -04001013 int ret;
1014 do {
1015 ret = SSL_shutdown(ssl);
1016 } while (config->async && RetryAsync(ssl, ret));
1017 return ret;
1018}
1019
David Benjamin1d4f4c02016-07-26 18:03:08 -04001020// DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous
1021// operations. It returns the result of the final |SSL_send_fatal_alert| call.
1022static int DoSendFatalAlert(SSL *ssl, uint8_t alert) {
1023 const TestConfig *config = GetTestConfig(ssl);
1024 int ret;
1025 do {
1026 ret = SSL_send_fatal_alert(ssl, alert);
1027 } while (config->async && RetryAsync(ssl, ret));
1028 return ret;
1029}
1030
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001031static uint16_t GetProtocolVersion(const SSL *ssl) {
1032 uint16_t version = SSL_version(ssl);
1033 if (!SSL_is_dtls(ssl)) {
1034 return version;
1035 }
1036 return 0x0201 + ~version;
1037}
1038
David Benjamin91eab5c2015-06-18 18:35:46 -04001039// CheckHandshakeProperties checks, immediately after |ssl| completes its
1040// initial handshake (or False Starts), whether all the properties are
1041// consistent with the test configuration and invariants.
1042static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001043 const TestConfig *config = GetTestConfig(ssl);
David Benjamin91eab5c2015-06-18 18:35:46 -04001044
1045 if (SSL_get_current_cipher(ssl) == nullptr) {
1046 fprintf(stderr, "null cipher after handshake\n");
1047 return false;
1048 }
1049
1050 if (is_resume &&
1051 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
1052 fprintf(stderr, "session was%s reused\n",
1053 SSL_session_reused(ssl) ? "" : " not");
1054 return false;
1055 }
1056
1057 bool expect_handshake_done = is_resume || !config->false_start;
1058 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
1059 fprintf(stderr, "handshake was%s completed\n",
1060 GetTestState(ssl)->handshake_done ? "" : " not");
1061 return false;
1062 }
1063
David Benjaminba4594a2015-06-18 18:36:15 -04001064 if (expect_handshake_done && !config->is_server) {
1065 bool expect_new_session =
1066 !config->expect_no_session &&
Steven Valdez143e8b32016-07-11 13:19:03 -04001067 (!SSL_session_reused(ssl) || config->expect_ticket_renewal) &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001068 // Session tickets are sent post-handshake in TLS 1.3.
1069 GetProtocolVersion(ssl) < TLS1_3_VERSION;
David Benjaminba4594a2015-06-18 18:36:15 -04001070 if (expect_new_session != GetTestState(ssl)->got_new_session) {
1071 fprintf(stderr,
David Benjamindd6fed92015-10-23 17:41:12 -04001072 "new session was%s cached, but we expected the opposite\n",
David Benjaminba4594a2015-06-18 18:36:15 -04001073 GetTestState(ssl)->got_new_session ? "" : " not");
1074 return false;
1075 }
1076 }
1077
David Benjamin91eab5c2015-06-18 18:35:46 -04001078 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
1079 fprintf(stderr, "early callback not called\n");
1080 return false;
1081 }
1082
1083 if (!config->expected_server_name.empty()) {
1084 const char *server_name =
1085 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1086 if (server_name != config->expected_server_name) {
1087 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
1088 server_name, config->expected_server_name.c_str());
1089 return false;
1090 }
1091 }
1092
1093 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -04001094 const uint8_t *certificate_types;
1095 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -04001096 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -04001097 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -04001098 memcmp(certificate_types,
1099 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -04001100 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001101 fprintf(stderr, "certificate types mismatch\n");
1102 return false;
1103 }
1104 }
1105
1106 if (!config->expected_next_proto.empty()) {
1107 const uint8_t *next_proto;
1108 unsigned next_proto_len;
1109 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
1110 if (next_proto_len != config->expected_next_proto.size() ||
1111 memcmp(next_proto, config->expected_next_proto.data(),
1112 next_proto_len) != 0) {
1113 fprintf(stderr, "negotiated next proto mismatch\n");
1114 return false;
1115 }
1116 }
1117
1118 if (!config->expected_alpn.empty()) {
1119 const uint8_t *alpn_proto;
1120 unsigned alpn_proto_len;
1121 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
1122 if (alpn_proto_len != config->expected_alpn.size() ||
1123 memcmp(alpn_proto, config->expected_alpn.data(),
1124 alpn_proto_len) != 0) {
1125 fprintf(stderr, "negotiated alpn proto mismatch\n");
1126 return false;
1127 }
1128 }
1129
1130 if (!config->expected_channel_id.empty()) {
1131 uint8_t channel_id[64];
1132 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
1133 fprintf(stderr, "no channel id negotiated\n");
1134 return false;
1135 }
1136 if (config->expected_channel_id.size() != 64 ||
1137 memcmp(config->expected_channel_id.data(),
1138 channel_id, 64) != 0) {
1139 fprintf(stderr, "channel id mismatch\n");
1140 return false;
1141 }
1142 }
1143
1144 if (config->expect_extended_master_secret) {
David Benjamin8ac35712016-07-13 21:07:29 -04001145 if (!SSL_get_extms_support(ssl)) {
1146 fprintf(stderr, "No EMS for connection when expected");
David Benjamin91eab5c2015-06-18 18:35:46 -04001147 return false;
1148 }
1149 }
1150
1151 if (!config->expected_ocsp_response.empty()) {
1152 const uint8_t *data;
1153 size_t len;
1154 SSL_get0_ocsp_response(ssl, &data, &len);
1155 if (config->expected_ocsp_response.size() != len ||
1156 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
1157 fprintf(stderr, "OCSP response mismatch\n");
1158 return false;
1159 }
1160 }
1161
1162 if (!config->expected_signed_cert_timestamps.empty()) {
1163 const uint8_t *data;
1164 size_t len;
1165 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
1166 if (config->expected_signed_cert_timestamps.size() != len ||
1167 memcmp(config->expected_signed_cert_timestamps.data(),
1168 data, len) != 0) {
1169 fprintf(stderr, "SCT list mismatch\n");
1170 return false;
1171 }
1172 }
1173
Paul Lietar8f1c2682015-08-18 12:21:54 +01001174 if (config->expect_verify_result) {
1175 int expected_verify_result = config->verify_fail ?
1176 X509_V_ERR_APPLICATION_VERIFICATION :
1177 X509_V_OK;
1178
1179 if (SSL_get_verify_result(ssl) != expected_verify_result) {
1180 fprintf(stderr, "Wrong certificate verification result\n");
1181 return false;
1182 }
1183 }
1184
Nick Harper60edffd2016-06-21 15:19:24 -07001185 if (config->expect_peer_signature_algorithm != 0 &&
1186 config->expect_peer_signature_algorithm !=
1187 SSL_get_peer_signature_algorithm(ssl)) {
1188 fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n",
1189 SSL_get_peer_signature_algorithm(ssl),
1190 config->expect_peer_signature_algorithm);
David Benjamin6e807652015-11-02 12:02:20 -05001191 return false;
1192 }
1193
David Benjamin9e68f192016-06-30 14:55:33 -04001194 if (config->expect_curve_id != 0) {
1195 uint16_t curve_id = SSL_get_curve_id(ssl);
1196 if (static_cast<uint16_t>(config->expect_curve_id) != curve_id) {
1197 fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id,
1198 static_cast<uint16_t>(config->expect_curve_id));
1199 return false;
1200 }
1201 }
1202
1203 if (config->expect_dhe_group_size != 0) {
1204 unsigned dhe_group_size = SSL_get_dhe_group_size(ssl);
1205 if (static_cast<unsigned>(config->expect_dhe_group_size) !=
1206 dhe_group_size) {
1207 fprintf(stderr, "dhe_group_size was %u, wanted %d\n", dhe_group_size,
1208 config->expect_dhe_group_size);
David Benjamin4cc36ad2015-12-19 14:23:26 -05001209 return false;
1210 }
1211 }
1212
David Benjamin91eab5c2015-06-18 18:35:46 -04001213 if (!config->is_server) {
1214 /* Clients should expect a peer certificate chain iff this was not a PSK
1215 * cipher suite. */
1216 if (config->psk.empty()) {
1217 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
1218 fprintf(stderr, "Missing peer certificate chain!\n");
1219 return false;
1220 }
1221 } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
1222 fprintf(stderr, "Unexpected peer certificate chain!\n");
1223 return false;
1224 }
1225 }
1226 return true;
1227}
1228
David Benjamin87c8a642015-02-21 01:54:29 -05001229// DoExchange runs a test SSL exchange against the peer. On success, it returns
1230// true and sets |*out_session| to the negotiated SSL session. If the test is a
1231// resumption attempt, |is_resume| is true and |session| is the session from the
1232// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -05001233static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
1234 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -05001235 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001236 ScopedSSL ssl(SSL_new(ssl_ctx));
1237 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001238 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001239 }
1240
David Benjamin7e7a82d2016-05-20 20:12:42 -04001241 if (!SetTestConfig(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001242 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001243 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001244 }
David Benjamin5a593af2014-08-11 19:51:50 -04001245
Adam Langley5f0efe02015-02-20 13:03:16 -08001246 if (config->fallback_scsv &&
1247 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1248 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001249 }
David Benjaminacb6dcc2016-03-10 09:15:01 -05001250 if (!config->use_early_callback && !config->use_old_client_cert_callback) {
David Benjamin6f5c0f42015-02-24 01:23:21 -05001251 if (config->async) {
David Benjamin6f5c0f42015-02-24 01:23:21 -05001252 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
1253 } else if (!InstallCertificate(ssl.get())) {
1254 return false;
1255 }
David Benjamin5a593af2014-08-11 19:51:50 -04001256 }
1257 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001258 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001259 NULL);
1260 }
1261 if (config->verify_peer) {
1262 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001263 }
1264 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001265 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001266 }
1267 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001268 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001269 }
1270 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001271 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001272 }
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001273 if (config->no_tls13) {
1274 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3);
1275 }
David Benjamin5a593af2014-08-11 19:51:50 -04001276 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001277 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001278 }
1279 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001280 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001281 }
1282 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001283 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001284 }
1285 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001286 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001287 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001288 if (!config->expected_channel_id.empty() ||
1289 config->enable_channel_id) {
David Benjamina7f333d2015-02-09 02:37:18 -05001290 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001291 }
1292 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001293 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001294 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001295 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -05001296 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
1297 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001298 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001299 }
David Benjamina08e49d2014-08-24 01:46:07 -04001300 }
David Benjamina08e49d2014-08-24 01:46:07 -04001301 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001302 if (!config->host_name.empty() &&
1303 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001304 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001305 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001306 if (!config->advertise_alpn.empty() &&
1307 SSL_set_alpn_protos(ssl.get(),
1308 (const uint8_t *)config->advertise_alpn.data(),
1309 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001310 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001311 }
David Benjamin48cae082014-10-27 01:06:24 -04001312 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001313 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1314 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001315 }
David Benjamin61f95272014-11-25 01:55:35 -05001316 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001317 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001318 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001319 }
David Benjamin61f95272014-11-25 01:55:35 -05001320 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001321 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001322 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001323 }
1324 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001325 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001326 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001327 }
1328 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001329 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001330 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001331 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001332 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001333 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001334 }
1335 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001336 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001337 }
David Benjamin5e7e7cc2016-07-21 12:55:28 +02001338 if (config->fallback_version != 0) {
1339 SSL_set_fallback_version(ssl.get(), (uint16_t)config->fallback_version);
1340 }
David Benjamin13be1de2015-01-11 16:29:36 -05001341 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001342 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1343 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001344 }
Adam Langley524e7172015-02-20 16:04:00 -08001345 if (config->install_ddos_callback) {
1346 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1347 }
David Benjamin1d5ef3b2015-10-12 19:54:18 -04001348 if (config->renegotiate_once) {
1349 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once);
1350 }
1351 if (config->renegotiate_freely) {
1352 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04001353 }
Adam Langley27a0d082015-11-03 13:34:10 -08001354 if (config->renegotiate_ignore) {
1355 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore);
1356 }
David Benjamin30789da2015-08-29 22:56:45 -04001357 if (!config->check_close_notify) {
1358 SSL_set_quiet_shutdown(ssl.get(), 1);
1359 }
David Benjamin091c4b92015-10-26 13:33:21 -04001360 if (config->disable_npn) {
1361 SSL_set_options(ssl.get(), SSL_OP_DISABLE_NPN);
1362 }
David Benjamin99fdfb92015-11-02 12:11:35 -05001363 if (config->p384_only) {
1364 int nid = NID_secp384r1;
1365 if (!SSL_set1_curves(ssl.get(), &nid, 1)) {
1366 return false;
1367 }
1368 }
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001369 if (config->enable_all_curves) {
1370 static const int kAllCurves[] = {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001371 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519,
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001372 };
1373 if (!SSL_set1_curves(ssl.get(), kAllCurves,
1374 sizeof(kAllCurves) / sizeof(kAllCurves[0]))) {
1375 return false;
1376 }
1377 }
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -07001378 if (config->initial_timeout_duration_ms > 0) {
1379 DTLSv1_set_initial_timeout_duration(ssl.get(),
1380 config->initial_timeout_duration_ms);
1381 }
David Benjamin025b3d32014-07-01 19:53:04 -04001382
David Benjamin87c8a642015-02-21 01:54:29 -05001383 int sock = Connect(config->port);
1384 if (sock == -1) {
1385 return false;
1386 }
1387 SocketCloser closer(sock);
1388
1389 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001390 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001391 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001392 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001393 if (config->is_dtls) {
David Benjamin585d7a42016-06-02 14:58:00 -04001394 ScopedBIO packeted = PacketedBioCreate(!config->async);
David Benjaminb7c5e842016-03-28 09:59:10 -04001395 if (!packeted) {
1396 return false;
1397 }
David Benjamin585d7a42016-06-02 14:58:00 -04001398 GetTestState(ssl.get())->packeted_bio = packeted.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001399 BIO_push(packeted.get(), bio.release());
1400 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001401 }
David Benjamin5a593af2014-08-11 19:51:50 -04001402 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001403 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001404 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjaminb7c5e842016-03-28 09:59:10 -04001405 if (!async_scoped) {
1406 return false;
1407 }
David Benjamina7f333d2015-02-09 02:37:18 -05001408 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001409 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001410 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001411 }
David Benjamina7f333d2015-02-09 02:37:18 -05001412 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1413 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001414
David Benjamin1d5c83e2014-07-22 19:20:02 -04001415 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001416 if (!config->is_server) {
1417 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001418 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001419 }
1420 } else if (config->async) {
1421 // The internal session cache is disabled, so install the session
1422 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -05001423 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -05001424 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001425 }
1426 }
1427
David Benjamina07c0fc2015-05-13 13:19:42 -04001428 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1429 fprintf(stderr, "non-null cipher before handshake\n");
1430 return false;
1431 }
1432
David Benjamin1d5c83e2014-07-22 19:20:02 -04001433 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001434 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001435 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001436 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001437 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001438 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001439 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001440 } else {
1441 do {
1442 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001443 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001444 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001445 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001446 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001447 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001448 if (ret != 1 ||
1449 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001450 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001451 }
David Benjamin025b3d32014-07-01 19:53:04 -04001452
David Benjaminba4594a2015-06-18 18:36:15 -04001453 // Reset the state to assert later that the callback isn't called in
1454 // renegotations.
1455 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001456 }
1457
David Benjaminc565ebb2015-04-03 04:06:36 -04001458 if (config->export_keying_material > 0) {
1459 std::vector<uint8_t> result(
1460 static_cast<size_t>(config->export_keying_material));
1461 if (!SSL_export_keying_material(
1462 ssl.get(), result.data(), result.size(),
1463 config->export_label.data(), config->export_label.size(),
1464 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1465 config->export_context.size(), config->use_export_context)) {
1466 fprintf(stderr, "failed to export keying material\n");
1467 return false;
1468 }
1469 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1470 return false;
1471 }
1472 }
1473
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001474 if (config->tls_unique) {
1475 uint8_t tls_unique[16];
1476 size_t tls_unique_len;
1477 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1478 sizeof(tls_unique))) {
1479 fprintf(stderr, "failed to get tls-unique\n");
1480 return false;
1481 }
1482
1483 if (tls_unique_len != 12) {
1484 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1485 static_cast<unsigned>(tls_unique_len));
1486 return false;
1487 }
1488
1489 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1490 return false;
1491 }
1492 }
1493
David Benjamin1d4f4c02016-07-26 18:03:08 -04001494 if (config->send_alert) {
1495 if (DoSendFatalAlert(ssl.get(), SSL_AD_DECOMPRESSION_FAILURE) < 0) {
1496 return false;
1497 }
1498 return true;
1499 }
1500
David Benjamin5a593af2014-08-11 19:51:50 -04001501 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001502 if (config->is_dtls) {
1503 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001504 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001505 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001506 // This mode writes a number of different record sizes in an attempt to
1507 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001508 static const size_t kBufLen = 32769;
1509 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1510 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001511 static const size_t kRecordSizes[] = {
1512 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1513 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1514 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001515 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001516 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001517 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001518 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001519 }
Adam Langleybc949292015-06-18 21:32:44 -07001520 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001521 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001522 }
1523 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001524 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001525 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001526 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1527 5) < 0) {
1528 return false;
1529 }
David Benjamine58c4f52014-08-24 03:47:07 -04001530 }
David Benjamin30789da2015-08-29 22:56:45 -04001531 if (!config->shim_shuts_down) {
1532 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001533 static const size_t kBufLen = 16384;
1534 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1535
David Benjamin2c99d282015-09-01 10:23:00 -04001536 // Read only 512 bytes at a time in TLS to ensure records may be
1537 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001538 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001539 int err = SSL_get_error(ssl.get(), n);
1540 if (err == SSL_ERROR_ZERO_RETURN ||
1541 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1542 if (n != 0) {
1543 fprintf(stderr, "Invalid SSL_get_error output\n");
1544 return false;
1545 }
1546 // Stop on either clean or unclean shutdown.
1547 break;
1548 } else if (err != SSL_ERROR_NONE) {
1549 if (n > 0) {
1550 fprintf(stderr, "Invalid SSL_get_error output\n");
1551 return false;
1552 }
1553 return false;
1554 }
1555 // Successfully read data.
1556 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001557 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001558 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001559 }
David Benjamin30789da2015-08-29 22:56:45 -04001560
1561 // After a successful read, with or without False Start, the handshake
1562 // must be complete.
1563 if (!GetTestState(ssl.get())->handshake_done) {
1564 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001565 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001566 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001567
David Benjamin30789da2015-08-29 22:56:45 -04001568 for (int i = 0; i < n; i++) {
1569 buf[i] ^= 0xff;
1570 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001571 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001572 return false;
1573 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001574 }
1575 }
David Benjamin025b3d32014-07-01 19:53:04 -04001576 }
1577
David Benjaminba4594a2015-06-18 18:36:15 -04001578 if (!config->is_server && !config->false_start &&
1579 !config->implicit_handshake &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001580 // Session tickets are sent post-handshake in TLS 1.3.
1581 GetProtocolVersion(ssl.get()) < TLS1_3_VERSION &&
David Benjaminba4594a2015-06-18 18:36:15 -04001582 GetTestState(ssl.get())->got_new_session) {
1583 fprintf(stderr, "new session was established after the handshake\n");
1584 return false;
1585 }
1586
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001587 if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) {
1588 bool expect_new_session =
1589 !config->expect_no_session && !config->shim_shuts_down;
1590 if (expect_new_session != GetTestState(ssl.get())->got_new_session) {
1591 fprintf(stderr,
1592 "new session was%s cached, but we expected the opposite\n",
1593 GetTestState(ssl.get())->got_new_session ? "" : " not");
1594 return false;
1595 }
1596 }
1597
David Benjamin1d5c83e2014-07-22 19:20:02 -04001598 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001599 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001600 }
1601
David Benjamin30789da2015-08-29 22:56:45 -04001602 ret = DoShutdown(ssl.get());
1603
1604 if (config->shim_shuts_down && config->check_close_notify) {
1605 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1606 // it returns zero when our close_notify is sent, then one when the peer's
1607 // is received.
1608 if (ret != 0) {
1609 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1610 return false;
1611 }
1612 ret = DoShutdown(ssl.get());
1613 }
1614
1615 if (ret != 1) {
1616 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1617 return false;
1618 }
1619
David Benjamin324dce42015-10-12 19:49:00 -04001620 if (SSL_total_renegotiations(ssl.get()) !=
1621 config->expect_total_renegotiations) {
1622 fprintf(stderr, "Expected %d renegotiations, got %d\n",
1623 config->expect_total_renegotiations,
1624 SSL_total_renegotiations(ssl.get()));
1625 return false;
1626 }
1627
David Benjamin40f101b2015-02-20 11:23:42 -05001628 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001629}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001630
David Benjaminff3a1492016-03-02 10:12:06 -05001631class StderrDelimiter {
1632 public:
1633 ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
1634};
1635
Adam Langley10f97f32016-07-12 08:09:33 -07001636int main(int argc, char **argv) {
David Benjaminff3a1492016-03-02 10:12:06 -05001637 // To distinguish ASan's output from ours, add a trailing message to stderr.
1638 // Anything following this line will be considered an error.
1639 StderrDelimiter delimiter;
1640
David Benjamin87c8a642015-02-21 01:54:29 -05001641#if defined(OPENSSL_WINDOWS)
1642 /* Initialize Winsock. */
1643 WORD wsa_version = MAKEWORD(2, 2);
1644 WSADATA wsa_data;
1645 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1646 if (wsa_err != 0) {
1647 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1648 return 1;
1649 }
1650 if (wsa_data.wVersion != wsa_version) {
1651 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1652 return 1;
1653 }
1654#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001655 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001656#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001657
David Benjamin7a1eefd2015-10-17 23:39:22 -04001658 CRYPTO_library_init();
David Benjamind9e07012015-02-09 03:04:34 -05001659 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001660 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001661 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001662 return 1;
1663 }
David Benjamin5a593af2014-08-11 19:51:50 -04001664
1665 TestConfig config;
1666 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001667 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001668 }
1669
David Benjaminc273d2c2015-02-09 12:59:46 -05001670 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001671 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001672 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001673 return 1;
1674 }
1675
David Benjamina7f333d2015-02-09 02:37:18 -05001676 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001677 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001678 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001679 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001680 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001681 }
1682
David Benjamin40f101b2015-02-20 11:23:42 -05001683 if (config.resume &&
1684 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001685 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001686 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001687 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001688 }
1689
David Benjamina7f333d2015-02-09 02:37:18 -05001690 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001691}