blob: ff77b630cde595bc7f6f0d30c6a98907a23908b4 [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>
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -070046#include <openssl/c++/digest.h>
David Benjamind98452d2015-06-16 14:16:23 -040047#include <openssl/cipher.h>
David Benjamin7a1eefd2015-10-17 23:39:22 -040048#include <openssl/crypto.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070049#include <openssl/dh.h>
Brian Smith83a82982015-04-09 16:21:10 -100050#include <openssl/err.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070051#include <openssl/evp.h>
David Benjamind98452d2015-06-16 14:16:23 -040052#include <openssl/hmac.h>
David Benjamin98193672016-03-25 18:07:11 -040053#include <openssl/nid.h>
David Benjamind98452d2015-06-16 14:16:23 -040054#include <openssl/rand.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040055#include <openssl/ssl.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070056#include <openssl/x509.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040057
David Benjamin45fb1be2015-03-22 16:31:27 -040058#include <memory>
Steven Valdez0d62f262015-09-04 12:41:04 -040059#include <string>
David Benjaminc565ebb2015-04-03 04:06:36 -040060#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040061
Steven Valdezcb966542016-08-17 16:56:14 -040062#include "../../crypto/internal.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040063#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040064#include "packeted_bio.h"
David Benjamin5a593af2014-08-11 19:51:50 -040065#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040066
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -070067namespace bssl {
David Benjamin87c8a642015-02-21 01:54:29 -050068
69#if !defined(OPENSSL_WINDOWS)
70static int closesocket(int sock) {
71 return close(sock);
72}
73
74static void PrintSocketError(const char *func) {
75 perror(func);
76}
77#else
78static void PrintSocketError(const char *func) {
79 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
80}
81#endif
82
David Benjaminc273d2c2015-02-09 12:59:46 -050083static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050084 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040085 return 1;
86}
David Benjamin025b3d32014-07-01 19:53:04 -040087
David Benjamin2d445c02015-02-09 13:03:50 -050088struct TestState {
David Benjamin6c2563e2015-04-03 03:47:47 -040089 // async_bio is async BIO which pauses reads and writes.
90 BIO *async_bio = nullptr;
David Benjamin585d7a42016-06-02 14:58:00 -040091 // packeted_bio is the packeted BIO which simulates read timeouts.
92 BIO *packeted_bio = nullptr;
Matt Braithwaited17d74d2016-08-17 20:10:28 -070093 bssl::UniquePtr<EVP_PKEY> channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040094 bool cert_ready = false;
Matt Braithwaited17d74d2016-08-17 20:10:28 -070095 bssl::UniquePtr<SSL_SESSION> session;
96 bssl::UniquePtr<SSL_SESSION> pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040097 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040098 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040099 // private_key is the underlying private key used when testing custom keys.
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700100 bssl::UniquePtr<EVP_PKEY> private_key;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700101 std::vector<uint8_t> private_key_result;
102 // private_key_retries is the number of times an asynchronous private key
103 // operation has been retried.
104 unsigned private_key_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400105 bool got_new_session = false;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700106 bssl::UniquePtr<SSL_SESSION> new_session;
David Benjamin25fe85b2016-08-09 20:00:32 -0400107 bool ticket_decrypt_done = false;
108 bool alpn_select_done = false;
David Benjamind9e07012015-02-09 03:04:34 -0500109};
110
David Benjamin2d445c02015-02-09 13:03:50 -0500111static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700112 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500113 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500114}
115
116static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500117static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400118
David Benjamin7e7a82d2016-05-20 20:12:42 -0400119static bool SetTestConfig(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500120 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400121}
122
David Benjamin7e7a82d2016-05-20 20:12:42 -0400123static const TestConfig *GetTestConfig(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500124 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400125}
126
David Benjamin22050932015-11-23 13:44:48 -0500127static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
128 // |SSL_set_ex_data| takes ownership of |state| only on success.
129 if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
130 state.release();
David Benjamind9e07012015-02-09 03:04:34 -0500131 return true;
132 }
133 return false;
134}
135
David Benjamin87e4acd2015-04-02 19:57:35 -0400136static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500137 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500138}
139
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700140static bssl::UniquePtr<X509> LoadCertificate(const std::string &file) {
141 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
David Benjaminacb6dcc2016-03-10 09:15:01 -0500142 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
143 return nullptr;
144 }
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700145 return bssl::UniquePtr<X509>(PEM_read_bio_X509(bio.get(), NULL, NULL, NULL));
David Benjaminacb6dcc2016-03-10 09:15:01 -0500146}
147
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700148static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) {
149 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
David Benjamina7f333d2015-02-09 02:37:18 -0500150 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
151 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400152 }
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700153 return bssl::UniquePtr<EVP_PKEY>(
154 PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400155}
156
David Benjaminb4d65fd2015-05-29 17:11:21 -0400157static int AsyncPrivateKeyType(SSL *ssl) {
David Benjamin0c0b7e12016-07-14 13:47:55 -0400158 EVP_PKEY *key = GetTestState(ssl)->private_key.get();
159 switch (EVP_PKEY_id(key)) {
160 case EVP_PKEY_RSA:
161 return NID_rsaEncryption;
162 case EVP_PKEY_EC:
163 return EC_GROUP_get_curve_name(
164 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(key)));
165 default:
166 return NID_undef;
167 }
David Benjaminb4d65fd2015-05-29 17:11:21 -0400168}
169
David Benjaminb4d65fd2015-05-29 17:11:21 -0400170static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
171 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
172}
173
174static ssl_private_key_result_t AsyncPrivateKeySign(
175 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamind3440b42016-07-14 14:52:41 -0400176 uint16_t signature_algorithm, const uint8_t *in, size_t in_len) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400177 TestState *test_state = GetTestState(ssl);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700178 if (!test_state->private_key_result.empty()) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400179 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
180 abort();
181 }
182
David Benjamind3440b42016-07-14 14:52:41 -0400183 // Determine the hash.
184 const EVP_MD *md;
185 switch (signature_algorithm) {
186 case SSL_SIGN_RSA_PKCS1_SHA1:
187 case SSL_SIGN_ECDSA_SHA1:
188 md = EVP_sha1();
189 break;
190 case SSL_SIGN_RSA_PKCS1_SHA256:
191 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
192 case SSL_SIGN_RSA_PSS_SHA256:
193 md = EVP_sha256();
194 break;
195 case SSL_SIGN_RSA_PKCS1_SHA384:
196 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
197 case SSL_SIGN_RSA_PSS_SHA384:
198 md = EVP_sha384();
199 break;
200 case SSL_SIGN_RSA_PKCS1_SHA512:
201 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
202 case SSL_SIGN_RSA_PSS_SHA512:
203 md = EVP_sha512();
204 break;
205 case SSL_SIGN_RSA_PKCS1_MD5_SHA1:
206 md = EVP_md5_sha1();
207 break;
208 default:
209 fprintf(stderr, "Unknown signature algorithm %04x.\n",
210 signature_algorithm);
211 return ssl_private_key_failure;
212 }
213
214 ScopedEVP_MD_CTX ctx;
215 EVP_PKEY_CTX *pctx;
216 if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr,
217 test_state->private_key.get())) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400218 return ssl_private_key_failure;
219 }
220
David Benjamind3440b42016-07-14 14:52:41 -0400221 // Configure additional signature parameters.
222 switch (signature_algorithm) {
223 case SSL_SIGN_RSA_PSS_SHA256:
224 case SSL_SIGN_RSA_PSS_SHA384:
225 case SSL_SIGN_RSA_PSS_SHA512:
226 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
227 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
228 -1 /* salt len = hash len */)) {
229 return ssl_private_key_failure;
230 }
231 }
232
David Benjaminb4d65fd2015-05-29 17:11:21 -0400233 // Write the signature into |test_state|.
234 size_t len = 0;
David Benjamind3440b42016-07-14 14:52:41 -0400235 if (!EVP_DigestSignUpdate(ctx.get(), in, in_len) ||
236 !EVP_DigestSignFinal(ctx.get(), nullptr, &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400237 return ssl_private_key_failure;
238 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700239 test_state->private_key_result.resize(len);
David Benjamind3440b42016-07-14 14:52:41 -0400240 if (!EVP_DigestSignFinal(ctx.get(), test_state->private_key_result.data(),
241 &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400242 return ssl_private_key_failure;
243 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700244 test_state->private_key_result.resize(len);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400245
David Benjamind3440b42016-07-14 14:52:41 -0400246 // The signature will be released asynchronously in |AsyncPrivateKeyComplete|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400247 return ssl_private_key_retry;
248}
249
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700250static ssl_private_key_result_t AsyncPrivateKeyDecrypt(
251 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
252 const uint8_t *in, size_t in_len) {
253 TestState *test_state = GetTestState(ssl);
254 if (!test_state->private_key_result.empty()) {
255 fprintf(stderr,
256 "AsyncPrivateKeyDecrypt called with operation pending.\n");
257 abort();
258 }
259
David Benjamin758d1272015-11-20 17:47:25 -0500260 RSA *rsa = EVP_PKEY_get0_RSA(test_state->private_key.get());
261 if (rsa == NULL) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700262 fprintf(stderr,
263 "AsyncPrivateKeyDecrypt called with incorrect key type.\n");
264 abort();
265 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700266 test_state->private_key_result.resize(RSA_size(rsa));
David Benjaminef14b2d2015-11-11 14:01:27 -0800267 if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700268 RSA_size(rsa), in, in_len, RSA_NO_PADDING)) {
269 return ssl_private_key_failure;
270 }
271
272 test_state->private_key_result.resize(*out_len);
273
David Benjamind3440b42016-07-14 14:52:41 -0400274 // The decryption will be released asynchronously in |AsyncPrivateComplete|.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700275 return ssl_private_key_retry;
276}
277
David Benjamind3440b42016-07-14 14:52:41 -0400278static ssl_private_key_result_t AsyncPrivateKeyComplete(
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700279 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
280 TestState *test_state = GetTestState(ssl);
281 if (test_state->private_key_result.empty()) {
282 fprintf(stderr,
David Benjamind3440b42016-07-14 14:52:41 -0400283 "AsyncPrivateKeyComplete called without operation pending.\n");
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700284 abort();
285 }
286
287 if (test_state->private_key_retries < 2) {
288 // Only return the decryption on the second attempt, to test both incomplete
289 // |decrypt| and |decrypt_complete|.
290 return ssl_private_key_retry;
291 }
292
293 if (max_out < test_state->private_key_result.size()) {
294 fprintf(stderr, "Output buffer too small.\n");
295 return ssl_private_key_failure;
296 }
David Benjaminef14b2d2015-11-11 14:01:27 -0800297 memcpy(out, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700298 test_state->private_key_result.size());
299 *out_len = test_state->private_key_result.size();
300
301 test_state->private_key_result.clear();
302 test_state->private_key_retries = 0;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400303 return ssl_private_key_success;
304}
305
306static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
307 AsyncPrivateKeyType,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400308 AsyncPrivateKeyMaxSignatureLen,
309 AsyncPrivateKeySign,
David Benjamind3440b42016-07-14 14:52:41 -0400310 nullptr /* sign_digest */,
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700311 AsyncPrivateKeyDecrypt,
David Benjamind3440b42016-07-14 14:52:41 -0400312 AsyncPrivateKeyComplete,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400313};
314
Steven Valdez0d62f262015-09-04 12:41:04 -0400315template<typename T>
Adam Langley10f97f32016-07-12 08:09:33 -0700316struct Free {
Steven Valdez0d62f262015-09-04 12:41:04 -0400317 void operator()(T *buf) {
318 free(buf);
319 }
320};
321
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700322static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509,
323 bssl::UniquePtr<EVP_PKEY> *out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400324 const TestConfig *config = GetTestConfig(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -0400325
326 if (!config->digest_prefs.empty()) {
Adam Langley10f97f32016-07-12 08:09:33 -0700327 std::unique_ptr<char, Free<char>> digest_prefs(
Steven Valdez0d62f262015-09-04 12:41:04 -0400328 strdup(config->digest_prefs.c_str()));
Steven Valdez0d62f262015-09-04 12:41:04 -0400329 std::vector<int> digest_list;
330
331 for (;;) {
Adam Langley67251f22015-09-23 15:01:07 -0700332 char *token =
333 strtok(digest_list.empty() ? digest_prefs.get() : nullptr, ",");
Steven Valdez0d62f262015-09-04 12:41:04 -0400334 if (token == nullptr) {
335 break;
336 }
337
338 digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token)));
339 }
340
341 if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(),
342 digest_list.size())) {
343 return false;
344 }
345 }
346
David Benjaminca3d5452016-07-14 12:51:01 -0400347 if (!config->signing_prefs.empty()) {
348 std::vector<uint16_t> u16s(config->signing_prefs.begin(),
349 config->signing_prefs.end());
350 if (!SSL_set_signing_algorithm_prefs(ssl, u16s.data(), u16s.size())) {
351 return false;
352 }
353 }
354
David Benjaminb4d65fd2015-05-29 17:11:21 -0400355 if (!config->key_file.empty()) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500356 *out_pkey = LoadPrivateKey(config->key_file.c_str());
357 if (!*out_pkey) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400358 return false;
359 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500360 }
David Benjaminacb6dcc2016-03-10 09:15:01 -0500361 if (!config->cert_file.empty()) {
362 *out_x509 = LoadCertificate(config->cert_file.c_str());
363 if (!*out_x509) {
364 return false;
365 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500366 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100367 if (!config->ocsp_response.empty() &&
368 !SSL_CTX_set_ocsp_response(ssl->ctx,
369 (const uint8_t *)config->ocsp_response.data(),
370 config->ocsp_response.size())) {
371 return false;
372 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500373 return true;
374}
375
David Benjaminacb6dcc2016-03-10 09:15:01 -0500376static bool InstallCertificate(SSL *ssl) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700377 bssl::UniquePtr<X509> x509;
378 bssl::UniquePtr<EVP_PKEY> pkey;
David Benjaminacb6dcc2016-03-10 09:15:01 -0500379 if (!GetCertificate(ssl, &x509, &pkey)) {
380 return false;
381 }
382
383 if (pkey) {
384 TestState *test_state = GetTestState(ssl);
David Benjamin7e7a82d2016-05-20 20:12:42 -0400385 const TestConfig *config = GetTestConfig(ssl);
David Benjaminacb6dcc2016-03-10 09:15:01 -0500386 if (config->async) {
387 test_state->private_key = std::move(pkey);
388 SSL_set_private_key_method(ssl, &g_async_private_key_method);
389 } else if (!SSL_use_PrivateKey(ssl, pkey.get())) {
390 return false;
391 }
392 }
393
394 if (x509 && !SSL_use_certificate(ssl, x509.get())) {
395 return false;
396 }
397
398 return true;
399}
400
David Benjaminc273d2c2015-02-09 12:59:46 -0500401static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400402 const TestConfig *config = GetTestConfig(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500403 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400404
David Benjamin6f5c0f42015-02-24 01:23:21 -0500405 if (!config->expected_server_name.empty()) {
406 const uint8_t *extension_data;
407 size_t extension_len;
408 CBS extension, server_name_list, host_name;
409 uint8_t name_type;
410
411 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
412 &extension_data,
413 &extension_len)) {
414 fprintf(stderr, "Could not find server_name extension.\n");
415 return -1;
416 }
417
418 CBS_init(&extension, extension_data, extension_len);
419 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
420 CBS_len(&extension) != 0 ||
421 !CBS_get_u8(&server_name_list, &name_type) ||
422 name_type != TLSEXT_NAMETYPE_host_name ||
423 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
424 CBS_len(&server_name_list) != 0) {
425 fprintf(stderr, "Could not decode server_name extension.\n");
426 return -1;
427 }
428
429 if (!CBS_mem_equal(&host_name,
430 (const uint8_t*)config->expected_server_name.data(),
431 config->expected_server_name.size())) {
432 fprintf(stderr, "Server name mismatch.\n");
433 }
David Benjamin7b030512014-07-08 17:30:11 -0400434 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400435
David Benjamin6f5c0f42015-02-24 01:23:21 -0500436 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400437 return -1;
438 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400439
David Benjamin6f5c0f42015-02-24 01:23:21 -0500440 // Install the certificate in the early callback.
441 if (config->use_early_callback) {
442 if (config->async) {
443 // Install the certificate asynchronously.
444 return 0;
445 }
446 if (!InstallCertificate(ctx->ssl)) {
447 return -1;
448 }
David Benjamin7b030512014-07-08 17:30:11 -0400449 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400450 return 1;
451}
David Benjamin025b3d32014-07-01 19:53:04 -0400452
David Benjaminacb6dcc2016-03-10 09:15:01 -0500453static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400454 if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500455 return -1;
456 }
457
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700458 bssl::UniquePtr<X509> x509;
459 bssl::UniquePtr<EVP_PKEY> pkey;
David Benjaminacb6dcc2016-03-10 09:15:01 -0500460 if (!GetCertificate(ssl, &x509, &pkey)) {
461 return -1;
462 }
463
464 // Return zero for no certificate.
465 if (!x509) {
466 return 0;
467 }
468
469 // Asynchronous private keys are not supported with client_cert_cb.
470 *out_x509 = x509.release();
471 *out_pkey = pkey.release();
472 return 1;
473}
474
Paul Lietar8f1c2682015-08-18 12:21:54 +0100475static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
476 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
477 SSL_get_ex_data_X509_STORE_CTX_idx());
David Benjamin7e7a82d2016-05-20 20:12:42 -0400478 const TestConfig *config = GetTestConfig(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100479
480 if (!config->expected_ocsp_response.empty()) {
481 const uint8_t *data;
482 size_t len;
483 SSL_get0_ocsp_response(ssl, &data, &len);
484 if (len == 0) {
485 fprintf(stderr, "OCSP response not available in verify callback\n");
486 return 0;
487 }
488 }
489
David Benjamin67666e72014-07-12 15:47:52 -0400490 return 1;
491}
492
Paul Lietar8f1c2682015-08-18 12:21:54 +0100493static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
494 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
495 return 0;
496}
497
David Benjaminc273d2c2015-02-09 12:59:46 -0500498static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
499 unsigned int *out_len, void *arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400500 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500501 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400502 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500503 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400504
David Benjamin5a593af2014-08-11 19:51:50 -0400505 *out = (const uint8_t*)config->advertise_npn.data();
506 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400507 return SSL_TLSEXT_ERR_OK;
508}
509
David Benjaminc273d2c2015-02-09 12:59:46 -0500510static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400511 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400512 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500513 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400514 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500515 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400516
David Benjamin5a593af2014-08-11 19:51:50 -0400517 *out = (uint8_t*)config->select_next_proto.data();
518 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400519 return SSL_TLSEXT_ERR_OK;
520}
521
David Benjaminc273d2c2015-02-09 12:59:46 -0500522static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
523 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400524 if (GetTestState(ssl)->alpn_select_done) {
525 fprintf(stderr, "AlpnSelectCallback called after completion.\n");
526 exit(1);
527 }
528
529 GetTestState(ssl)->alpn_select_done = true;
530
David Benjamin7e7a82d2016-05-20 20:12:42 -0400531 const TestConfig *config = GetTestConfig(ssl);
David Benjamin594e7d22016-03-17 17:49:56 -0400532 if (config->decline_alpn) {
David Benjaminae2888f2014-09-06 12:58:58 -0400533 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500534 }
David Benjaminae2888f2014-09-06 12:58:58 -0400535
536 if (!config->expected_advertised_alpn.empty() &&
537 (config->expected_advertised_alpn.size() != inlen ||
538 memcmp(config->expected_advertised_alpn.data(),
539 in, inlen) != 0)) {
540 fprintf(stderr, "bad ALPN select callback inputs\n");
541 exit(1);
542 }
543
544 *out = (const uint8_t*)config->select_alpn.data();
545 *outlen = config->select_alpn.size();
546 return SSL_TLSEXT_ERR_OK;
547}
548
David Benjaminc273d2c2015-02-09 12:59:46 -0500549static unsigned PskClientCallback(SSL *ssl, const char *hint,
550 char *out_identity,
551 unsigned max_identity_len,
552 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400553 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400554
555 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
556 fprintf(stderr, "Server PSK hint did not match.\n");
557 return 0;
558 }
559
560 // Account for the trailing '\0' for the identity.
561 if (config->psk_identity.size() >= max_identity_len ||
562 config->psk.size() > max_psk_len) {
563 fprintf(stderr, "PSK buffers too small\n");
564 return 0;
565 }
566
567 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
568 max_identity_len);
569 memcpy(out_psk, config->psk.data(), config->psk.size());
570 return config->psk.size();
571}
572
David Benjaminc273d2c2015-02-09 12:59:46 -0500573static unsigned PskServerCallback(SSL *ssl, const char *identity,
574 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400575 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400576
577 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
578 fprintf(stderr, "Client PSK identity did not match.\n");
579 return 0;
580 }
581
582 if (config->psk.size() > max_psk_len) {
583 fprintf(stderr, "PSK buffers too small\n");
584 return 0;
585 }
586
587 memcpy(out_psk, config->psk.data(), config->psk.size());
588 return config->psk.size();
589}
590
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400591static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin585d7a42016-06-02 14:58:00 -0400592 *out_clock = PacketedBioGetClock(GetTestState(ssl)->packeted_bio);
David Benjamin377fc312015-01-26 00:22:12 -0500593}
594
David Benjaminc273d2c2015-02-09 12:59:46 -0500595static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500596 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500597}
598
David Benjaminc273d2c2015-02-09 12:59:46 -0500599static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500600 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500601 return -1;
602 }
603 if (!InstallCertificate(ssl)) {
604 return 0;
605 }
606 return 1;
607}
608
David Benjaminc273d2c2015-02-09 12:59:46 -0500609static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
610 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500611 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500612 if (async_state->session) {
613 *copy = 0;
614 return async_state->session.release();
615 } else if (async_state->pending_session) {
616 return SSL_magic_pending_session_ptr();
617 } else {
618 return NULL;
619 }
620}
621
Adam Langley524e7172015-02-20 16:04:00 -0800622static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400623 const TestConfig *config = GetTestConfig(early_context->ssl);
Adam Langley524e7172015-02-20 16:04:00 -0800624 static int callback_num = 0;
625
626 callback_num++;
627 if (config->fail_ddos_callback ||
628 (config->fail_second_ddos_callback && callback_num == 2)) {
629 return 0;
630 }
631 return 1;
632}
633
David Benjamin87e4acd2015-04-02 19:57:35 -0400634static void InfoCallback(const SSL *ssl, int type, int val) {
635 if (type == SSL_CB_HANDSHAKE_DONE) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400636 if (GetTestConfig(ssl)->handshake_never_done) {
David Benjamin87e4acd2015-04-02 19:57:35 -0400637 fprintf(stderr, "handshake completed\n");
638 // Abort before any expected error code is printed, to ensure the overall
639 // test fails.
640 abort();
641 }
642 GetTestState(ssl)->handshake_done = true;
David Benjamin25fe85b2016-08-09 20:00:32 -0400643
644 // Callbacks may be called again on a new handshake.
645 GetTestState(ssl)->ticket_decrypt_done = false;
646 GetTestState(ssl)->alpn_select_done = false;
David Benjamin87e4acd2015-04-02 19:57:35 -0400647 }
648}
649
David Benjaminba4594a2015-06-18 18:36:15 -0400650static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
651 GetTestState(ssl)->got_new_session = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400652 GetTestState(ssl)->new_session.reset(session);
David Benjaminba4594a2015-06-18 18:36:15 -0400653 return 1;
654}
655
David Benjamind98452d2015-06-16 14:16:23 -0400656static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
657 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
658 int encrypt) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400659 if (!encrypt) {
660 if (GetTestState(ssl)->ticket_decrypt_done) {
661 fprintf(stderr, "TicketKeyCallback called after completion.\n");
662 return -1;
663 }
664
665 GetTestState(ssl)->ticket_decrypt_done = true;
666 }
667
David Benjamind98452d2015-06-16 14:16:23 -0400668 // This is just test code, so use the all-zeros key.
669 static const uint8_t kZeros[16] = {0};
670
671 if (encrypt) {
672 memcpy(key_name, kZeros, sizeof(kZeros));
673 RAND_bytes(iv, 16);
674 } else if (memcmp(key_name, kZeros, 16) != 0) {
675 return 0;
676 }
677
678 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
679 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
680 return -1;
681 }
682
683 if (!encrypt) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400684 return GetTestConfig(ssl)->renew_ticket ? 2 : 1;
David Benjamind98452d2015-06-16 14:16:23 -0400685 }
686 return 1;
687}
688
Adam Langley09505632015-07-30 18:10:13 -0700689// kCustomExtensionValue is the extension value that the custom extension
690// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700691static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700692static void *const kCustomExtensionAddArg =
693 reinterpret_cast<void *>(kCustomExtensionValue);
694static void *const kCustomExtensionParseArg =
695 reinterpret_cast<void *>(kCustomExtensionValue + 1);
696static const char kCustomExtensionContents[] = "custom extension";
697
698static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
699 const uint8_t **out, size_t *out_len,
700 int *out_alert_value, void *add_arg) {
701 if (extension_value != kCustomExtensionValue ||
702 add_arg != kCustomExtensionAddArg) {
703 abort();
704 }
705
David Benjamin7e7a82d2016-05-20 20:12:42 -0400706 if (GetTestConfig(ssl)->custom_extension_skip) {
Adam Langley09505632015-07-30 18:10:13 -0700707 return 0;
708 }
David Benjamin7e7a82d2016-05-20 20:12:42 -0400709 if (GetTestConfig(ssl)->custom_extension_fail_add) {
Adam Langley09505632015-07-30 18:10:13 -0700710 return -1;
711 }
712
713 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
714 *out_len = sizeof(kCustomExtensionContents) - 1;
715
716 return 1;
717}
718
719static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
720 const uint8_t *out, void *add_arg) {
721 if (extension_value != kCustomExtensionValue ||
722 add_arg != kCustomExtensionAddArg ||
723 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
724 abort();
725 }
726}
727
728static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
729 const uint8_t *contents,
730 size_t contents_len,
731 int *out_alert_value, void *parse_arg) {
732 if (extension_value != kCustomExtensionValue ||
733 parse_arg != kCustomExtensionParseArg) {
734 abort();
735 }
736
737 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
738 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
739 *out_alert_value = SSL_AD_DECODE_ERROR;
740 return 0;
741 }
742
743 return 1;
744}
745
David Benjamin87c8a642015-02-21 01:54:29 -0500746// Connect returns a new socket connected to localhost on |port| or -1 on
747// error.
748static int Connect(uint16_t port) {
749 int sock = socket(AF_INET, SOCK_STREAM, 0);
750 if (sock == -1) {
751 PrintSocketError("socket");
752 return -1;
753 }
754 int nodelay = 1;
755 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
756 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
757 PrintSocketError("setsockopt");
758 closesocket(sock);
759 return -1;
760 }
761 sockaddr_in sin;
762 memset(&sin, 0, sizeof(sin));
763 sin.sin_family = AF_INET;
764 sin.sin_port = htons(port);
765 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
766 PrintSocketError("inet_pton");
767 closesocket(sock);
768 return -1;
769 }
770 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
771 sizeof(sin)) != 0) {
772 PrintSocketError("connect");
773 closesocket(sock);
774 return -1;
775 }
776 return sock;
777}
778
779class SocketCloser {
780 public:
781 explicit SocketCloser(int sock) : sock_(sock) {}
782 ~SocketCloser() {
783 // Half-close and drain the socket before releasing it. This seems to be
784 // necessary for graceful shutdown on Windows. It will also avoid write
785 // failures in the test runner.
786#if defined(OPENSSL_WINDOWS)
787 shutdown(sock_, SD_SEND);
788#else
789 shutdown(sock_, SHUT_WR);
790#endif
791 while (true) {
792 char buf[1024];
793 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
794 break;
795 }
796 }
797 closesocket(sock_);
798 }
799
800 private:
801 const int sock_;
802};
803
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700804static bssl::UniquePtr<SSL_CTX> SetupCtx(const TestConfig *config) {
805 bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(
David Benjamina7f333d2015-02-09 02:37:18 -0500806 config->is_dtls ? DTLS_method() : TLS_method()));
807 if (!ssl_ctx) {
808 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400809 }
810
Nick Harper1fd39d82016-06-14 18:14:35 -0700811 if (!config->is_dtls) {
812 // Enable TLS 1.3 for tests.
813 SSL_CTX_set_max_version(ssl_ctx.get(), TLS1_3_VERSION);
814 }
815
Adam Langleycef75832015-09-03 14:51:12 -0700816 std::string cipher_list = "ALL";
817 if (!config->cipher.empty()) {
818 cipher_list = config->cipher;
819 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
820 }
821 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
822 return nullptr;
823 }
824
825 if (!config->cipher_tls10.empty() &&
826 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
827 config->cipher_tls10.c_str())) {
828 return nullptr;
829 }
830 if (!config->cipher_tls11.empty() &&
831 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
832 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500833 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400834 }
835
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700836 bssl::UniquePtr<DH> dh(DH_get_2048_256(NULL));
David Benjaminb7c5e842016-03-28 09:59:10 -0400837 if (!dh) {
838 return nullptr;
839 }
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800840
841 if (config->use_sparse_dh_prime) {
842 // This prime number is 2^1024 + 643 – a value just above a power of two.
843 // Because of its form, values modulo it are essentially certain to be one
844 // byte shorter. This is used to test padding of these values.
845 if (BN_hex2bn(
846 &dh->p,
847 "1000000000000000000000000000000000000000000000000000000000000000"
848 "0000000000000000000000000000000000000000000000000000000000000000"
849 "0000000000000000000000000000000000000000000000000000000000000000"
850 "0000000000000000000000000000000000000000000000000000000000000028"
851 "3") == 0 ||
852 !BN_set_word(dh->g, 2)) {
853 return nullptr;
854 }
David Benjamine66148a2016-02-02 14:14:36 -0500855 BN_free(dh->q);
856 dh->q = NULL;
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800857 dh->priv_length = 0;
858 }
859
David Benjaminb7c5e842016-03-28 09:59:10 -0400860 if (!SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500861 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400862 }
863
David Benjamin1b8b6912015-02-09 04:28:16 -0500864 if (config->async && config->is_server) {
865 // Disable the internal session cache. To test asynchronous session lookup,
866 // we use an external session cache.
867 SSL_CTX_set_session_cache_mode(
868 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500869 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500870 } else {
871 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
872 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400873
David Benjamind4c2bce2015-10-17 12:28:18 -0400874 SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400875
David Benjaminacb6dcc2016-03-10 09:15:01 -0500876 if (config->use_old_client_cert_callback) {
877 SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback);
878 }
879
David Benjamin1f5f62b2014-07-12 16:18:02 -0400880 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500881 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400882 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500883 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500884 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400885 }
886
David Benjamin594e7d22016-03-17 17:49:56 -0400887 if (!config->select_alpn.empty() || config->decline_alpn) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500888 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400889 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400890
Adam Langley49c7af12015-07-10 14:33:46 -0700891 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500892 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400893
David Benjamin721e8b72016-08-03 13:13:17 -0400894 if (config->is_dtls) {
895 SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback);
896 }
David Benjamin377fc312015-01-26 00:22:12 -0500897
David Benjamin87e4acd2015-04-02 19:57:35 -0400898 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400899 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400900
David Benjamind98452d2015-06-16 14:16:23 -0400901 if (config->use_ticket_callback) {
902 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
903 }
904
Adam Langley09505632015-07-30 18:10:13 -0700905 if (config->enable_client_custom_extension &&
906 !SSL_CTX_add_client_custom_ext(
907 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
908 CustomExtensionFreeCallback, kCustomExtensionAddArg,
909 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
910 return nullptr;
911 }
912
913 if (config->enable_server_custom_extension &&
914 !SSL_CTX_add_server_custom_ext(
915 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
916 CustomExtensionFreeCallback, kCustomExtensionAddArg,
917 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
918 return nullptr;
919 }
920
Paul Lietar8f1c2682015-08-18 12:21:54 +0100921 if (config->verify_fail) {
922 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
923 } else {
924 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
925 }
926
Paul Lietar4fac72e2015-09-09 13:44:55 +0100927 if (!config->signed_cert_timestamps.empty() &&
928 !SSL_CTX_set_signed_cert_timestamp_list(
929 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
930 config->signed_cert_timestamps.size())) {
931 return nullptr;
932 }
933
David Benjamin2f8935d2016-07-13 19:47:39 -0400934 if (config->use_null_client_ca_list) {
935 SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr);
936 }
937
David Benjamin1d5c83e2014-07-22 19:20:02 -0400938 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400939}
940
David Benjamin40f101b2015-02-20 11:23:42 -0500941// RetryAsync is called after a failed operation on |ssl| with return code
942// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400943// event and returns true. Otherwise it returns false.
944static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400945 // No error; don't retry.
946 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500947 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400948 }
David Benjamin83f90402015-01-27 01:09:43 -0500949
David Benjamin6c2563e2015-04-03 03:47:47 -0400950 TestState *test_state = GetTestState(ssl);
Matt Braithwaite6278e242016-06-14 08:18:22 -0700951 assert(GetTestConfig(ssl)->async);
David Benjamin83f90402015-01-27 01:09:43 -0500952
David Benjamin585d7a42016-06-02 14:58:00 -0400953 if (test_state->packeted_bio != nullptr &&
954 PacketedBioAdvanceClock(test_state->packeted_bio)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500955 // The DTLS retransmit logic silently ignores write failures. So the test
956 // may progress, allow writes through synchronously.
David Benjamin585d7a42016-06-02 14:58:00 -0400957 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
David Benjamin13e81fc2015-11-02 17:16:13 -0500958 int timeout_ret = DTLSv1_handle_timeout(ssl);
David Benjamin585d7a42016-06-02 14:58:00 -0400959 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
David Benjamin13e81fc2015-11-02 17:16:13 -0500960
961 if (timeout_ret < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400962 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500963 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500964 }
David Benjamin40f101b2015-02-20 11:23:42 -0500965 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500966 }
967
David Benjamin43ec06f2014-08-05 02:28:57 -0400968 // See if we needed to read or write more. If so, allow one byte through on
969 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500970 switch (SSL_get_error(ssl, ret)) {
971 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400972 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500973 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500974 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400975 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500976 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500977 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700978 bssl::UniquePtr<EVP_PKEY> pkey =
979 LoadPrivateKey(GetTestConfig(ssl)->send_channel_id);
David Benjamin9d0847a2015-02-16 03:57:55 -0500980 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500981 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500982 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400983 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500984 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500985 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500986 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400987 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500988 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500989 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400990 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500991 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500992 case SSL_ERROR_PENDING_CERTIFICATE:
993 // The handshake will resume without a second call to the early callback.
994 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400995 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700996 test_state->private_key_retries++;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400997 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500998 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500999 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001000 }
David Benjamin025b3d32014-07-01 19:53:04 -04001001}
1002
David Benjamin6c2563e2015-04-03 03:47:47 -04001003// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
1004// the result value of the final |SSL_read| call.
1005static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001006 const TestConfig *config = GetTestConfig(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -05001007 TestState *test_state = GetTestState(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001008 int ret;
1009 do {
David Benjamin13e81fc2015-11-02 17:16:13 -05001010 if (config->async) {
1011 // The DTLS retransmit logic silently ignores write failures. So the test
1012 // may progress, allow writes through synchronously. |SSL_read| may
1013 // trigger a retransmit, so disconnect the write quota.
1014 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
1015 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001016 ret = SSL_read(ssl, out, max_out);
David Benjamin13e81fc2015-11-02 17:16:13 -05001017 if (config->async) {
1018 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
1019 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001020 } while (config->async && RetryAsync(ssl, ret));
1021 return ret;
1022}
1023
1024// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
1025// operations. It returns the result of the final |SSL_write| call.
1026static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001027 const TestConfig *config = GetTestConfig(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001028 int ret;
1029 do {
1030 ret = SSL_write(ssl, in, in_len);
1031 if (ret > 0) {
1032 in += ret;
1033 in_len -= ret;
1034 }
1035 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
1036 return ret;
1037}
1038
David Benjamin30789da2015-08-29 22:56:45 -04001039// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
1040// returns the result of the final |SSL_shutdown| call.
1041static int DoShutdown(SSL *ssl) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001042 const TestConfig *config = GetTestConfig(ssl);
David Benjamin30789da2015-08-29 22:56:45 -04001043 int ret;
1044 do {
1045 ret = SSL_shutdown(ssl);
1046 } while (config->async && RetryAsync(ssl, ret));
1047 return ret;
1048}
1049
David Benjamin1d4f4c02016-07-26 18:03:08 -04001050// DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous
1051// operations. It returns the result of the final |SSL_send_fatal_alert| call.
1052static int DoSendFatalAlert(SSL *ssl, uint8_t alert) {
1053 const TestConfig *config = GetTestConfig(ssl);
1054 int ret;
1055 do {
1056 ret = SSL_send_fatal_alert(ssl, alert);
1057 } while (config->async && RetryAsync(ssl, ret));
1058 return ret;
1059}
1060
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001061static uint16_t GetProtocolVersion(const SSL *ssl) {
1062 uint16_t version = SSL_version(ssl);
1063 if (!SSL_is_dtls(ssl)) {
1064 return version;
1065 }
1066 return 0x0201 + ~version;
1067}
1068
David Benjamin91eab5c2015-06-18 18:35:46 -04001069// CheckHandshakeProperties checks, immediately after |ssl| completes its
1070// initial handshake (or False Starts), whether all the properties are
1071// consistent with the test configuration and invariants.
1072static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001073 const TestConfig *config = GetTestConfig(ssl);
David Benjamin91eab5c2015-06-18 18:35:46 -04001074
1075 if (SSL_get_current_cipher(ssl) == nullptr) {
1076 fprintf(stderr, "null cipher after handshake\n");
1077 return false;
1078 }
1079
1080 if (is_resume &&
1081 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
1082 fprintf(stderr, "session was%s reused\n",
1083 SSL_session_reused(ssl) ? "" : " not");
1084 return false;
1085 }
1086
1087 bool expect_handshake_done = is_resume || !config->false_start;
1088 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
1089 fprintf(stderr, "handshake was%s completed\n",
1090 GetTestState(ssl)->handshake_done ? "" : " not");
1091 return false;
1092 }
1093
David Benjaminba4594a2015-06-18 18:36:15 -04001094 if (expect_handshake_done && !config->is_server) {
1095 bool expect_new_session =
1096 !config->expect_no_session &&
Steven Valdez143e8b32016-07-11 13:19:03 -04001097 (!SSL_session_reused(ssl) || config->expect_ticket_renewal) &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001098 // Session tickets are sent post-handshake in TLS 1.3.
1099 GetProtocolVersion(ssl) < TLS1_3_VERSION;
David Benjaminba4594a2015-06-18 18:36:15 -04001100 if (expect_new_session != GetTestState(ssl)->got_new_session) {
1101 fprintf(stderr,
David Benjamindd6fed92015-10-23 17:41:12 -04001102 "new session was%s cached, but we expected the opposite\n",
David Benjaminba4594a2015-06-18 18:36:15 -04001103 GetTestState(ssl)->got_new_session ? "" : " not");
1104 return false;
1105 }
1106 }
1107
David Benjamin91eab5c2015-06-18 18:35:46 -04001108 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
1109 fprintf(stderr, "early callback not called\n");
1110 return false;
1111 }
1112
1113 if (!config->expected_server_name.empty()) {
1114 const char *server_name =
1115 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1116 if (server_name != config->expected_server_name) {
1117 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
1118 server_name, config->expected_server_name.c_str());
1119 return false;
1120 }
1121 }
1122
1123 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -04001124 const uint8_t *certificate_types;
1125 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -04001126 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -04001127 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -04001128 memcmp(certificate_types,
1129 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -04001130 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001131 fprintf(stderr, "certificate types mismatch\n");
1132 return false;
1133 }
1134 }
1135
1136 if (!config->expected_next_proto.empty()) {
1137 const uint8_t *next_proto;
1138 unsigned next_proto_len;
1139 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
1140 if (next_proto_len != config->expected_next_proto.size() ||
1141 memcmp(next_proto, config->expected_next_proto.data(),
1142 next_proto_len) != 0) {
1143 fprintf(stderr, "negotiated next proto mismatch\n");
1144 return false;
1145 }
1146 }
1147
1148 if (!config->expected_alpn.empty()) {
1149 const uint8_t *alpn_proto;
1150 unsigned alpn_proto_len;
1151 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
1152 if (alpn_proto_len != config->expected_alpn.size() ||
1153 memcmp(alpn_proto, config->expected_alpn.data(),
1154 alpn_proto_len) != 0) {
1155 fprintf(stderr, "negotiated alpn proto mismatch\n");
1156 return false;
1157 }
1158 }
1159
1160 if (!config->expected_channel_id.empty()) {
1161 uint8_t channel_id[64];
1162 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
1163 fprintf(stderr, "no channel id negotiated\n");
1164 return false;
1165 }
1166 if (config->expected_channel_id.size() != 64 ||
1167 memcmp(config->expected_channel_id.data(),
1168 channel_id, 64) != 0) {
1169 fprintf(stderr, "channel id mismatch\n");
1170 return false;
1171 }
1172 }
1173
1174 if (config->expect_extended_master_secret) {
David Benjamin8ac35712016-07-13 21:07:29 -04001175 if (!SSL_get_extms_support(ssl)) {
1176 fprintf(stderr, "No EMS for connection when expected");
David Benjamin91eab5c2015-06-18 18:35:46 -04001177 return false;
1178 }
1179 }
1180
1181 if (!config->expected_ocsp_response.empty()) {
1182 const uint8_t *data;
1183 size_t len;
1184 SSL_get0_ocsp_response(ssl, &data, &len);
1185 if (config->expected_ocsp_response.size() != len ||
1186 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
1187 fprintf(stderr, "OCSP response mismatch\n");
1188 return false;
1189 }
1190 }
1191
1192 if (!config->expected_signed_cert_timestamps.empty()) {
1193 const uint8_t *data;
1194 size_t len;
1195 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
1196 if (config->expected_signed_cert_timestamps.size() != len ||
1197 memcmp(config->expected_signed_cert_timestamps.data(),
1198 data, len) != 0) {
1199 fprintf(stderr, "SCT list mismatch\n");
1200 return false;
1201 }
1202 }
1203
Paul Lietar8f1c2682015-08-18 12:21:54 +01001204 if (config->expect_verify_result) {
1205 int expected_verify_result = config->verify_fail ?
1206 X509_V_ERR_APPLICATION_VERIFICATION :
1207 X509_V_OK;
1208
1209 if (SSL_get_verify_result(ssl) != expected_verify_result) {
1210 fprintf(stderr, "Wrong certificate verification result\n");
1211 return false;
1212 }
1213 }
1214
Nick Harper60edffd2016-06-21 15:19:24 -07001215 if (config->expect_peer_signature_algorithm != 0 &&
1216 config->expect_peer_signature_algorithm !=
1217 SSL_get_peer_signature_algorithm(ssl)) {
1218 fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n",
1219 SSL_get_peer_signature_algorithm(ssl),
1220 config->expect_peer_signature_algorithm);
David Benjamin6e807652015-11-02 12:02:20 -05001221 return false;
1222 }
1223
David Benjamin9e68f192016-06-30 14:55:33 -04001224 if (config->expect_curve_id != 0) {
1225 uint16_t curve_id = SSL_get_curve_id(ssl);
1226 if (static_cast<uint16_t>(config->expect_curve_id) != curve_id) {
1227 fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id,
1228 static_cast<uint16_t>(config->expect_curve_id));
1229 return false;
1230 }
1231 }
1232
1233 if (config->expect_dhe_group_size != 0) {
1234 unsigned dhe_group_size = SSL_get_dhe_group_size(ssl);
1235 if (static_cast<unsigned>(config->expect_dhe_group_size) !=
1236 dhe_group_size) {
1237 fprintf(stderr, "dhe_group_size was %u, wanted %d\n", dhe_group_size,
1238 config->expect_dhe_group_size);
David Benjamin4cc36ad2015-12-19 14:23:26 -05001239 return false;
1240 }
1241 }
1242
David Benjaminbb9e36e2016-08-03 14:14:47 -04001243 if (!config->psk.empty()) {
1244 if (SSL_get_peer_cert_chain(ssl) != nullptr) {
1245 fprintf(stderr, "Received peer certificate on a PSK cipher.\n");
1246 return false;
1247 }
1248 } else if (!config->is_server || config->require_any_client_certificate) {
1249 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
1250 fprintf(stderr, "Received no peer certificate but expected one.\n");
David Benjamin91eab5c2015-06-18 18:35:46 -04001251 return false;
1252 }
1253 }
David Benjaminbb9e36e2016-08-03 14:14:47 -04001254
David Benjamin91eab5c2015-06-18 18:35:46 -04001255 return true;
1256}
1257
David Benjamin87c8a642015-02-21 01:54:29 -05001258// DoExchange runs a test SSL exchange against the peer. On success, it returns
1259// true and sets |*out_session| to the negotiated SSL session. If the test is a
1260// resumption attempt, |is_resume| is true and |session| is the session from the
1261// previous exchange.
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001262static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session,
1263 SSL_CTX *ssl_ctx, const TestConfig *config,
1264 bool is_resume, SSL_SESSION *session) {
1265 bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx));
David Benjamina7f333d2015-02-09 02:37:18 -05001266 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001267 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001268 }
1269
David Benjamin7e7a82d2016-05-20 20:12:42 -04001270 if (!SetTestConfig(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001271 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001272 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001273 }
David Benjamin5a593af2014-08-11 19:51:50 -04001274
Adam Langley5f0efe02015-02-20 13:03:16 -08001275 if (config->fallback_scsv &&
1276 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1277 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001278 }
David Benjaminacb6dcc2016-03-10 09:15:01 -05001279 if (!config->use_early_callback && !config->use_old_client_cert_callback) {
David Benjamin6f5c0f42015-02-24 01:23:21 -05001280 if (config->async) {
David Benjamin6f5c0f42015-02-24 01:23:21 -05001281 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
1282 } else if (!InstallCertificate(ssl.get())) {
1283 return false;
1284 }
David Benjamin5a593af2014-08-11 19:51:50 -04001285 }
1286 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001287 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001288 NULL);
1289 }
1290 if (config->verify_peer) {
1291 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001292 }
1293 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001294 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001295 }
1296 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001297 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001298 }
1299 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001300 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001301 }
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001302 if (config->no_tls13) {
1303 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3);
1304 }
David Benjamin5a593af2014-08-11 19:51:50 -04001305 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001306 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001307 }
1308 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001309 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001310 }
1311 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001312 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001313 }
1314 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001315 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001316 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001317 if (!config->expected_channel_id.empty() ||
1318 config->enable_channel_id) {
David Benjamina7f333d2015-02-09 02:37:18 -05001319 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001320 }
1321 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001322 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001323 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001324 // The async case will be supplied by |ChannelIdCallback|.
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001325 bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(config->send_channel_id);
David Benjamind9e07012015-02-09 03:04:34 -05001326 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001327 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001328 }
David Benjamina08e49d2014-08-24 01:46:07 -04001329 }
David Benjamina08e49d2014-08-24 01:46:07 -04001330 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001331 if (!config->host_name.empty() &&
1332 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001333 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001334 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001335 if (!config->advertise_alpn.empty() &&
1336 SSL_set_alpn_protos(ssl.get(),
1337 (const uint8_t *)config->advertise_alpn.data(),
1338 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001339 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001340 }
David Benjamin48cae082014-10-27 01:06:24 -04001341 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001342 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1343 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001344 }
David Benjamin61f95272014-11-25 01:55:35 -05001345 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001346 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001347 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001348 }
David Benjamin61f95272014-11-25 01:55:35 -05001349 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001350 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001351 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001352 }
1353 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001354 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001355 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001356 }
1357 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001358 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001359 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001360 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001361 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001362 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001363 }
1364 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001365 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001366 }
David Benjamin5e7e7cc2016-07-21 12:55:28 +02001367 if (config->fallback_version != 0) {
1368 SSL_set_fallback_version(ssl.get(), (uint16_t)config->fallback_version);
1369 }
David Benjamin13be1de2015-01-11 16:29:36 -05001370 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001371 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1372 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001373 }
Adam Langley524e7172015-02-20 16:04:00 -08001374 if (config->install_ddos_callback) {
1375 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1376 }
David Benjamin1d5ef3b2015-10-12 19:54:18 -04001377 if (config->renegotiate_once) {
1378 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once);
1379 }
1380 if (config->renegotiate_freely) {
1381 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04001382 }
Adam Langley27a0d082015-11-03 13:34:10 -08001383 if (config->renegotiate_ignore) {
1384 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore);
1385 }
David Benjamin30789da2015-08-29 22:56:45 -04001386 if (!config->check_close_notify) {
1387 SSL_set_quiet_shutdown(ssl.get(), 1);
1388 }
David Benjamin091c4b92015-10-26 13:33:21 -04001389 if (config->disable_npn) {
1390 SSL_set_options(ssl.get(), SSL_OP_DISABLE_NPN);
1391 }
David Benjamin99fdfb92015-11-02 12:11:35 -05001392 if (config->p384_only) {
1393 int nid = NID_secp384r1;
1394 if (!SSL_set1_curves(ssl.get(), &nid, 1)) {
1395 return false;
1396 }
1397 }
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001398 if (config->enable_all_curves) {
1399 static const int kAllCurves[] = {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001400 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519,
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001401 };
1402 if (!SSL_set1_curves(ssl.get(), kAllCurves,
Steven Valdezcb966542016-08-17 16:56:14 -04001403 OPENSSL_ARRAY_SIZE(kAllCurves))) {
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001404 return false;
1405 }
1406 }
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -07001407 if (config->initial_timeout_duration_ms > 0) {
1408 DTLSv1_set_initial_timeout_duration(ssl.get(),
1409 config->initial_timeout_duration_ms);
1410 }
David Benjamin025b3d32014-07-01 19:53:04 -04001411
David Benjamin87c8a642015-02-21 01:54:29 -05001412 int sock = Connect(config->port);
1413 if (sock == -1) {
1414 return false;
1415 }
1416 SocketCloser closer(sock);
1417
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001418 bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001419 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001420 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001421 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001422 if (config->is_dtls) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001423 bssl::UniquePtr<BIO> packeted = PacketedBioCreate(!config->async);
David Benjaminb7c5e842016-03-28 09:59:10 -04001424 if (!packeted) {
1425 return false;
1426 }
David Benjamin585d7a42016-06-02 14:58:00 -04001427 GetTestState(ssl.get())->packeted_bio = packeted.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001428 BIO_push(packeted.get(), bio.release());
1429 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001430 }
David Benjamin5a593af2014-08-11 19:51:50 -04001431 if (config->async) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001432 bssl::UniquePtr<BIO> async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001433 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjaminb7c5e842016-03-28 09:59:10 -04001434 if (!async_scoped) {
1435 return false;
1436 }
David Benjamina7f333d2015-02-09 02:37:18 -05001437 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001438 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001439 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001440 }
David Benjamina7f333d2015-02-09 02:37:18 -05001441 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1442 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001443
David Benjamin1d5c83e2014-07-22 19:20:02 -04001444 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001445 if (!config->is_server) {
1446 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001447 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001448 }
1449 } else if (config->async) {
1450 // The internal session cache is disabled, so install the session
1451 // manually.
David Benjaminb9195402016-08-05 10:51:43 -04001452 SSL_SESSION_up_ref(session);
1453 GetTestState(ssl.get())->pending_session.reset(session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001454 }
1455 }
1456
David Benjamina07c0fc2015-05-13 13:19:42 -04001457 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1458 fprintf(stderr, "non-null cipher before handshake\n");
1459 return false;
1460 }
1461
David Benjamin1d5c83e2014-07-22 19:20:02 -04001462 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001463 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001464 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001465 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001466 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001467 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001468 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001469 } else {
1470 do {
1471 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001472 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001473 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001474 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001475 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001476 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001477 if (ret != 1 ||
1478 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001479 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001480 }
David Benjamin025b3d32014-07-01 19:53:04 -04001481
David Benjaminba4594a2015-06-18 18:36:15 -04001482 // Reset the state to assert later that the callback isn't called in
1483 // renegotations.
1484 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001485 }
1486
David Benjaminc565ebb2015-04-03 04:06:36 -04001487 if (config->export_keying_material > 0) {
1488 std::vector<uint8_t> result(
1489 static_cast<size_t>(config->export_keying_material));
1490 if (!SSL_export_keying_material(
1491 ssl.get(), result.data(), result.size(),
1492 config->export_label.data(), config->export_label.size(),
1493 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1494 config->export_context.size(), config->use_export_context)) {
1495 fprintf(stderr, "failed to export keying material\n");
1496 return false;
1497 }
1498 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1499 return false;
1500 }
1501 }
1502
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001503 if (config->tls_unique) {
1504 uint8_t tls_unique[16];
1505 size_t tls_unique_len;
1506 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1507 sizeof(tls_unique))) {
1508 fprintf(stderr, "failed to get tls-unique\n");
1509 return false;
1510 }
1511
1512 if (tls_unique_len != 12) {
1513 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1514 static_cast<unsigned>(tls_unique_len));
1515 return false;
1516 }
1517
1518 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1519 return false;
1520 }
1521 }
1522
David Benjamin1d4f4c02016-07-26 18:03:08 -04001523 if (config->send_alert) {
1524 if (DoSendFatalAlert(ssl.get(), SSL_AD_DECOMPRESSION_FAILURE) < 0) {
1525 return false;
1526 }
1527 return true;
1528 }
1529
David Benjamin5a593af2014-08-11 19:51:50 -04001530 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001531 if (config->is_dtls) {
1532 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001533 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001534 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001535 // This mode writes a number of different record sizes in an attempt to
1536 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001537 static const size_t kBufLen = 32769;
1538 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1539 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001540 static const size_t kRecordSizes[] = {
1541 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
Steven Valdezcb966542016-08-17 16:56:14 -04001542 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001543 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001544 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001545 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001546 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001547 }
Adam Langleybc949292015-06-18 21:32:44 -07001548 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001549 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001550 }
1551 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001552 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001553 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001554 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1555 5) < 0) {
1556 return false;
1557 }
David Benjamine58c4f52014-08-24 03:47:07 -04001558 }
David Benjamin30789da2015-08-29 22:56:45 -04001559 if (!config->shim_shuts_down) {
1560 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001561 static const size_t kBufLen = 16384;
1562 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1563
David Benjamin2c99d282015-09-01 10:23:00 -04001564 // Read only 512 bytes at a time in TLS to ensure records may be
1565 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001566 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001567 int err = SSL_get_error(ssl.get(), n);
1568 if (err == SSL_ERROR_ZERO_RETURN ||
1569 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1570 if (n != 0) {
1571 fprintf(stderr, "Invalid SSL_get_error output\n");
1572 return false;
1573 }
1574 // Stop on either clean or unclean shutdown.
1575 break;
1576 } else if (err != SSL_ERROR_NONE) {
1577 if (n > 0) {
1578 fprintf(stderr, "Invalid SSL_get_error output\n");
1579 return false;
1580 }
1581 return false;
1582 }
1583 // Successfully read data.
1584 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001585 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001586 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001587 }
David Benjamin30789da2015-08-29 22:56:45 -04001588
1589 // After a successful read, with or without False Start, the handshake
1590 // must be complete.
1591 if (!GetTestState(ssl.get())->handshake_done) {
1592 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001593 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001594 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001595
David Benjamin30789da2015-08-29 22:56:45 -04001596 for (int i = 0; i < n; i++) {
1597 buf[i] ^= 0xff;
1598 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001599 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001600 return false;
1601 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001602 }
1603 }
David Benjamin025b3d32014-07-01 19:53:04 -04001604 }
1605
David Benjaminba4594a2015-06-18 18:36:15 -04001606 if (!config->is_server && !config->false_start &&
1607 !config->implicit_handshake &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001608 // Session tickets are sent post-handshake in TLS 1.3.
1609 GetProtocolVersion(ssl.get()) < TLS1_3_VERSION &&
David Benjaminba4594a2015-06-18 18:36:15 -04001610 GetTestState(ssl.get())->got_new_session) {
1611 fprintf(stderr, "new session was established after the handshake\n");
1612 return false;
1613 }
1614
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001615 if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) {
1616 bool expect_new_session =
1617 !config->expect_no_session && !config->shim_shuts_down;
1618 if (expect_new_session != GetTestState(ssl.get())->got_new_session) {
1619 fprintf(stderr,
1620 "new session was%s cached, but we expected the opposite\n",
1621 GetTestState(ssl.get())->got_new_session ? "" : " not");
1622 return false;
1623 }
1624 }
1625
David Benjamin1d5c83e2014-07-22 19:20:02 -04001626 if (out_session) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001627 *out_session = std::move(GetTestState(ssl.get())->new_session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001628 }
1629
David Benjamin30789da2015-08-29 22:56:45 -04001630 ret = DoShutdown(ssl.get());
1631
1632 if (config->shim_shuts_down && config->check_close_notify) {
1633 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1634 // it returns zero when our close_notify is sent, then one when the peer's
1635 // is received.
1636 if (ret != 0) {
1637 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1638 return false;
1639 }
1640 ret = DoShutdown(ssl.get());
1641 }
1642
1643 if (ret != 1) {
1644 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1645 return false;
1646 }
1647
David Benjamin324dce42015-10-12 19:49:00 -04001648 if (SSL_total_renegotiations(ssl.get()) !=
1649 config->expect_total_renegotiations) {
1650 fprintf(stderr, "Expected %d renegotiations, got %d\n",
1651 config->expect_total_renegotiations,
1652 SSL_total_renegotiations(ssl.get()));
1653 return false;
1654 }
1655
David Benjamin40f101b2015-02-20 11:23:42 -05001656 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001657}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001658
David Benjaminff3a1492016-03-02 10:12:06 -05001659class StderrDelimiter {
1660 public:
1661 ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
1662};
1663
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -07001664static int Main(int argc, char **argv) {
David Benjaminff3a1492016-03-02 10:12:06 -05001665 // To distinguish ASan's output from ours, add a trailing message to stderr.
1666 // Anything following this line will be considered an error.
1667 StderrDelimiter delimiter;
1668
David Benjamin87c8a642015-02-21 01:54:29 -05001669#if defined(OPENSSL_WINDOWS)
1670 /* Initialize Winsock. */
1671 WORD wsa_version = MAKEWORD(2, 2);
1672 WSADATA wsa_data;
1673 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1674 if (wsa_err != 0) {
1675 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1676 return 1;
1677 }
1678 if (wsa_data.wVersion != wsa_version) {
1679 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1680 return 1;
1681 }
1682#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001683 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001684#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001685
David Benjamin7a1eefd2015-10-17 23:39:22 -04001686 CRYPTO_library_init();
David Benjamind9e07012015-02-09 03:04:34 -05001687 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001688 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001689 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001690 return 1;
1691 }
David Benjamin5a593af2014-08-11 19:51:50 -04001692
1693 TestConfig config;
1694 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001695 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001696 }
1697
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001698 bssl::UniquePtr<SSL_CTX> ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001699 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001700 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001701 return 1;
1702 }
1703
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001704 bssl::UniquePtr<SSL_SESSION> session;
David Benjamin46662482016-08-17 00:51:00 -04001705 for (int i = 0; i < config.resume_count + 1; i++) {
1706 bool is_resume = i > 0;
1707 if (is_resume && !config.is_server && !session) {
1708 fprintf(stderr, "No session to offer.\n");
1709 return 1;
1710 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001711
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001712 bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session);
David Benjamin46662482016-08-17 00:51:00 -04001713 if (!DoExchange(&session, ssl_ctx.get(), &config, is_resume,
1714 offer_session.get())) {
1715 fprintf(stderr, "Connection %d failed.\n", i + 1);
1716 ERR_print_errors_fp(stderr);
1717 return 1;
1718 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001719 }
1720
David Benjamina7f333d2015-02-09 02:37:18 -05001721 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001722}
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -07001723
1724} // namespace bssl
1725
1726int main(int argc, char **argv) {
1727 return bssl::Main(argc, argv);
1728}