blob: 1527173720d8b872c9bf2e6fc6f493e6e462a974 [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>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070048#include <openssl/dh.h>
David Benjaminf0e935d2016-09-06 18:10:19 -040049#include <openssl/digest.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
David Benjamin78679342016-09-16 19:42:05 -0400555 if (config->psk_identity.empty()) {
556 if (hint != nullptr) {
557 fprintf(stderr, "Server PSK hint was non-null.\n");
558 return 0;
559 }
560 } else if (hint == nullptr ||
561 strcmp(hint, config->psk_identity.c_str()) != 0) {
David Benjamin48cae082014-10-27 01:06:24 -0400562 fprintf(stderr, "Server PSK hint did not match.\n");
563 return 0;
564 }
565
566 // Account for the trailing '\0' for the identity.
567 if (config->psk_identity.size() >= max_identity_len ||
568 config->psk.size() > max_psk_len) {
569 fprintf(stderr, "PSK buffers too small\n");
570 return 0;
571 }
572
573 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
574 max_identity_len);
575 memcpy(out_psk, config->psk.data(), config->psk.size());
576 return config->psk.size();
577}
578
David Benjaminc273d2c2015-02-09 12:59:46 -0500579static unsigned PskServerCallback(SSL *ssl, const char *identity,
580 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400581 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400582
583 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
584 fprintf(stderr, "Client PSK identity did not match.\n");
585 return 0;
586 }
587
588 if (config->psk.size() > max_psk_len) {
589 fprintf(stderr, "PSK buffers too small\n");
590 return 0;
591 }
592
593 memcpy(out_psk, config->psk.data(), config->psk.size());
594 return config->psk.size();
595}
596
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400597static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin585d7a42016-06-02 14:58:00 -0400598 *out_clock = PacketedBioGetClock(GetTestState(ssl)->packeted_bio);
David Benjamin377fc312015-01-26 00:22:12 -0500599}
600
David Benjaminc273d2c2015-02-09 12:59:46 -0500601static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500602 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500603}
604
David Benjaminc273d2c2015-02-09 12:59:46 -0500605static int CertCallback(SSL *ssl, void *arg) {
David Benjamina0486782016-10-06 19:11:32 -0400606 const TestConfig *config = GetTestConfig(ssl);
607
608 // Check the CertificateRequest metadata is as expected.
609 //
610 // TODO(davidben): Test |SSL_get_client_CA_list|.
611 if (!SSL_is_server(ssl) &&
612 !config->expected_certificate_types.empty()) {
613 const uint8_t *certificate_types;
614 size_t certificate_types_len =
615 SSL_get0_certificate_types(ssl, &certificate_types);
616 if (certificate_types_len != config->expected_certificate_types.size() ||
617 memcmp(certificate_types,
618 config->expected_certificate_types.data(),
619 certificate_types_len) != 0) {
620 fprintf(stderr, "certificate types mismatch\n");
621 return 0;
622 }
623 }
624
625 // The certificate will be installed via other means.
626 if (!config->async || config->use_early_callback ||
627 config->use_old_client_cert_callback) {
628 return 1;
629 }
630
David Benjamin2d445c02015-02-09 13:03:50 -0500631 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500632 return -1;
633 }
634 if (!InstallCertificate(ssl)) {
635 return 0;
636 }
637 return 1;
638}
639
David Benjaminc273d2c2015-02-09 12:59:46 -0500640static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
641 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500642 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500643 if (async_state->session) {
644 *copy = 0;
645 return async_state->session.release();
646 } else if (async_state->pending_session) {
647 return SSL_magic_pending_session_ptr();
648 } else {
649 return NULL;
650 }
651}
652
Adam Langley524e7172015-02-20 16:04:00 -0800653static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400654 const TestConfig *config = GetTestConfig(early_context->ssl);
Adam Langley524e7172015-02-20 16:04:00 -0800655 static int callback_num = 0;
656
657 callback_num++;
658 if (config->fail_ddos_callback ||
659 (config->fail_second_ddos_callback && callback_num == 2)) {
660 return 0;
661 }
662 return 1;
663}
664
David Benjamin87e4acd2015-04-02 19:57:35 -0400665static void InfoCallback(const SSL *ssl, int type, int val) {
666 if (type == SSL_CB_HANDSHAKE_DONE) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400667 if (GetTestConfig(ssl)->handshake_never_done) {
David Benjamin7a4aaa42016-09-20 17:58:14 -0400668 fprintf(stderr, "Handshake unexpectedly completed.\n");
David Benjamin87e4acd2015-04-02 19:57:35 -0400669 // Abort before any expected error code is printed, to ensure the overall
670 // test fails.
671 abort();
672 }
673 GetTestState(ssl)->handshake_done = true;
David Benjamin25fe85b2016-08-09 20:00:32 -0400674
675 // Callbacks may be called again on a new handshake.
676 GetTestState(ssl)->ticket_decrypt_done = false;
677 GetTestState(ssl)->alpn_select_done = false;
David Benjamin87e4acd2015-04-02 19:57:35 -0400678 }
679}
680
David Benjaminba4594a2015-06-18 18:36:15 -0400681static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
682 GetTestState(ssl)->got_new_session = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400683 GetTestState(ssl)->new_session.reset(session);
David Benjaminba4594a2015-06-18 18:36:15 -0400684 return 1;
685}
686
David Benjamind98452d2015-06-16 14:16:23 -0400687static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
688 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
689 int encrypt) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400690 if (!encrypt) {
691 if (GetTestState(ssl)->ticket_decrypt_done) {
692 fprintf(stderr, "TicketKeyCallback called after completion.\n");
693 return -1;
694 }
695
696 GetTestState(ssl)->ticket_decrypt_done = true;
697 }
698
David Benjamind98452d2015-06-16 14:16:23 -0400699 // This is just test code, so use the all-zeros key.
700 static const uint8_t kZeros[16] = {0};
701
702 if (encrypt) {
703 memcpy(key_name, kZeros, sizeof(kZeros));
704 RAND_bytes(iv, 16);
705 } else if (memcmp(key_name, kZeros, 16) != 0) {
706 return 0;
707 }
708
709 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
710 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
711 return -1;
712 }
713
714 if (!encrypt) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400715 return GetTestConfig(ssl)->renew_ticket ? 2 : 1;
David Benjamind98452d2015-06-16 14:16:23 -0400716 }
717 return 1;
718}
719
Adam Langley09505632015-07-30 18:10:13 -0700720// kCustomExtensionValue is the extension value that the custom extension
721// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700722static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700723static void *const kCustomExtensionAddArg =
724 reinterpret_cast<void *>(kCustomExtensionValue);
725static void *const kCustomExtensionParseArg =
726 reinterpret_cast<void *>(kCustomExtensionValue + 1);
727static const char kCustomExtensionContents[] = "custom extension";
728
729static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
730 const uint8_t **out, size_t *out_len,
731 int *out_alert_value, void *add_arg) {
732 if (extension_value != kCustomExtensionValue ||
733 add_arg != kCustomExtensionAddArg) {
734 abort();
735 }
736
David Benjamin7e7a82d2016-05-20 20:12:42 -0400737 if (GetTestConfig(ssl)->custom_extension_skip) {
Adam Langley09505632015-07-30 18:10:13 -0700738 return 0;
739 }
David Benjamin7e7a82d2016-05-20 20:12:42 -0400740 if (GetTestConfig(ssl)->custom_extension_fail_add) {
Adam Langley09505632015-07-30 18:10:13 -0700741 return -1;
742 }
743
744 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
745 *out_len = sizeof(kCustomExtensionContents) - 1;
746
747 return 1;
748}
749
750static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
751 const uint8_t *out, void *add_arg) {
752 if (extension_value != kCustomExtensionValue ||
753 add_arg != kCustomExtensionAddArg ||
754 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
755 abort();
756 }
757}
758
759static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
760 const uint8_t *contents,
761 size_t contents_len,
762 int *out_alert_value, void *parse_arg) {
763 if (extension_value != kCustomExtensionValue ||
764 parse_arg != kCustomExtensionParseArg) {
765 abort();
766 }
767
768 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
769 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
770 *out_alert_value = SSL_AD_DECODE_ERROR;
771 return 0;
772 }
773
774 return 1;
775}
776
David Benjamin87c8a642015-02-21 01:54:29 -0500777// Connect returns a new socket connected to localhost on |port| or -1 on
778// error.
779static int Connect(uint16_t port) {
780 int sock = socket(AF_INET, SOCK_STREAM, 0);
781 if (sock == -1) {
782 PrintSocketError("socket");
783 return -1;
784 }
785 int nodelay = 1;
786 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
787 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
788 PrintSocketError("setsockopt");
789 closesocket(sock);
790 return -1;
791 }
792 sockaddr_in sin;
793 memset(&sin, 0, sizeof(sin));
794 sin.sin_family = AF_INET;
795 sin.sin_port = htons(port);
796 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
797 PrintSocketError("inet_pton");
798 closesocket(sock);
799 return -1;
800 }
801 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
802 sizeof(sin)) != 0) {
803 PrintSocketError("connect");
804 closesocket(sock);
805 return -1;
806 }
807 return sock;
808}
809
810class SocketCloser {
811 public:
812 explicit SocketCloser(int sock) : sock_(sock) {}
813 ~SocketCloser() {
814 // Half-close and drain the socket before releasing it. This seems to be
815 // necessary for graceful shutdown on Windows. It will also avoid write
816 // failures in the test runner.
817#if defined(OPENSSL_WINDOWS)
818 shutdown(sock_, SD_SEND);
819#else
820 shutdown(sock_, SHUT_WR);
821#endif
822 while (true) {
823 char buf[1024];
824 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
825 break;
826 }
827 }
828 closesocket(sock_);
829 }
830
831 private:
832 const int sock_;
833};
834
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700835static bssl::UniquePtr<SSL_CTX> SetupCtx(const TestConfig *config) {
836 bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(
David Benjamina7f333d2015-02-09 02:37:18 -0500837 config->is_dtls ? DTLS_method() : TLS_method()));
838 if (!ssl_ctx) {
839 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400840 }
841
David Benjamin2dc02042016-09-19 19:57:37 -0400842 // Enable TLS 1.3 for tests.
843 if (!config->is_dtls &&
David Benjamine4706902016-09-20 15:12:23 -0400844 !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION)) {
David Benjamin2dc02042016-09-19 19:57:37 -0400845 return nullptr;
Nick Harper1fd39d82016-06-14 18:14:35 -0700846 }
847
Adam Langleycef75832015-09-03 14:51:12 -0700848 std::string cipher_list = "ALL";
849 if (!config->cipher.empty()) {
850 cipher_list = config->cipher;
851 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
852 }
853 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
854 return nullptr;
855 }
856
857 if (!config->cipher_tls10.empty() &&
858 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
859 config->cipher_tls10.c_str())) {
860 return nullptr;
861 }
862 if (!config->cipher_tls11.empty() &&
863 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
864 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500865 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400866 }
867
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700868 bssl::UniquePtr<DH> dh(DH_get_2048_256(NULL));
David Benjaminb7c5e842016-03-28 09:59:10 -0400869 if (!dh) {
870 return nullptr;
871 }
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800872
873 if (config->use_sparse_dh_prime) {
874 // This prime number is 2^1024 + 643 – a value just above a power of two.
875 // Because of its form, values modulo it are essentially certain to be one
876 // byte shorter. This is used to test padding of these values.
877 if (BN_hex2bn(
878 &dh->p,
879 "1000000000000000000000000000000000000000000000000000000000000000"
880 "0000000000000000000000000000000000000000000000000000000000000000"
881 "0000000000000000000000000000000000000000000000000000000000000000"
882 "0000000000000000000000000000000000000000000000000000000000000028"
883 "3") == 0 ||
884 !BN_set_word(dh->g, 2)) {
885 return nullptr;
886 }
David Benjamine66148a2016-02-02 14:14:36 -0500887 BN_free(dh->q);
888 dh->q = NULL;
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800889 dh->priv_length = 0;
890 }
891
David Benjaminb7c5e842016-03-28 09:59:10 -0400892 if (!SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500893 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400894 }
895
David Benjamin1b8b6912015-02-09 04:28:16 -0500896 if (config->async && config->is_server) {
897 // Disable the internal session cache. To test asynchronous session lookup,
898 // we use an external session cache.
899 SSL_CTX_set_session_cache_mode(
900 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500901 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500902 } else {
903 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
904 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400905
David Benjamind4c2bce2015-10-17 12:28:18 -0400906 SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400907
David Benjaminacb6dcc2016-03-10 09:15:01 -0500908 if (config->use_old_client_cert_callback) {
909 SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback);
910 }
911
David Benjamin1f5f62b2014-07-12 16:18:02 -0400912 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500913 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400914 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500915 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500916 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400917 }
918
David Benjamin594e7d22016-03-17 17:49:56 -0400919 if (!config->select_alpn.empty() || config->decline_alpn) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500920 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400921 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400922
Adam Langley49c7af12015-07-10 14:33:46 -0700923 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500924 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400925
David Benjamin721e8b72016-08-03 13:13:17 -0400926 if (config->is_dtls) {
927 SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback);
928 }
David Benjamin377fc312015-01-26 00:22:12 -0500929
David Benjamin87e4acd2015-04-02 19:57:35 -0400930 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400931 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400932
David Benjamind98452d2015-06-16 14:16:23 -0400933 if (config->use_ticket_callback) {
934 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
935 }
936
Adam Langley09505632015-07-30 18:10:13 -0700937 if (config->enable_client_custom_extension &&
938 !SSL_CTX_add_client_custom_ext(
939 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
940 CustomExtensionFreeCallback, kCustomExtensionAddArg,
941 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
942 return nullptr;
943 }
944
945 if (config->enable_server_custom_extension &&
946 !SSL_CTX_add_server_custom_ext(
947 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
948 CustomExtensionFreeCallback, kCustomExtensionAddArg,
949 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
950 return nullptr;
951 }
952
Paul Lietar8f1c2682015-08-18 12:21:54 +0100953 if (config->verify_fail) {
954 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
955 } else {
956 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
957 }
958
Paul Lietar4fac72e2015-09-09 13:44:55 +0100959 if (!config->signed_cert_timestamps.empty() &&
960 !SSL_CTX_set_signed_cert_timestamp_list(
961 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
962 config->signed_cert_timestamps.size())) {
963 return nullptr;
964 }
965
David Benjamin2f8935d2016-07-13 19:47:39 -0400966 if (config->use_null_client_ca_list) {
967 SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr);
968 }
969
David Benjamin65ac9972016-09-02 21:35:25 -0400970 if (config->enable_grease) {
971 SSL_CTX_set_grease_enabled(ssl_ctx.get(), 1);
972 }
973
David Benjamin1d5c83e2014-07-22 19:20:02 -0400974 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400975}
976
David Benjamin40f101b2015-02-20 11:23:42 -0500977// RetryAsync is called after a failed operation on |ssl| with return code
978// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400979// event and returns true. Otherwise it returns false.
980static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400981 // No error; don't retry.
982 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500983 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400984 }
David Benjamin83f90402015-01-27 01:09:43 -0500985
David Benjamin6c2563e2015-04-03 03:47:47 -0400986 TestState *test_state = GetTestState(ssl);
Matt Braithwaite6278e242016-06-14 08:18:22 -0700987 assert(GetTestConfig(ssl)->async);
David Benjamin83f90402015-01-27 01:09:43 -0500988
David Benjamin585d7a42016-06-02 14:58:00 -0400989 if (test_state->packeted_bio != nullptr &&
990 PacketedBioAdvanceClock(test_state->packeted_bio)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500991 // The DTLS retransmit logic silently ignores write failures. So the test
992 // may progress, allow writes through synchronously.
David Benjamin585d7a42016-06-02 14:58:00 -0400993 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
David Benjamin13e81fc2015-11-02 17:16:13 -0500994 int timeout_ret = DTLSv1_handle_timeout(ssl);
David Benjamin585d7a42016-06-02 14:58:00 -0400995 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
David Benjamin13e81fc2015-11-02 17:16:13 -0500996
997 if (timeout_ret < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400998 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500999 return false;
David Benjamin83f90402015-01-27 01:09:43 -05001000 }
David Benjamin40f101b2015-02-20 11:23:42 -05001001 return true;
David Benjamin83f90402015-01-27 01:09:43 -05001002 }
1003
David Benjamin43ec06f2014-08-05 02:28:57 -04001004 // See if we needed to read or write more. If so, allow one byte through on
1005 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -05001006 switch (SSL_get_error(ssl, ret)) {
1007 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -04001008 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -05001009 return true;
David Benjamind9e07012015-02-09 03:04:34 -05001010 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -04001011 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -05001012 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -05001013 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001014 bssl::UniquePtr<EVP_PKEY> pkey =
1015 LoadPrivateKey(GetTestConfig(ssl)->send_channel_id);
David Benjamin9d0847a2015-02-16 03:57:55 -05001016 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -05001017 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -05001018 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001019 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -05001020 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -05001021 }
David Benjamin41fdbcd2015-02-09 03:13:35 -05001022 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -04001023 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -05001024 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -05001025 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -04001026 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -05001027 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -05001028 case SSL_ERROR_PENDING_CERTIFICATE:
1029 // The handshake will resume without a second call to the early callback.
1030 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -04001031 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001032 test_state->private_key_retries++;
David Benjaminb4d65fd2015-05-29 17:11:21 -04001033 return true;
David Benjamind9e07012015-02-09 03:04:34 -05001034 default:
David Benjamin40f101b2015-02-20 11:23:42 -05001035 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001036 }
David Benjamin025b3d32014-07-01 19:53:04 -04001037}
1038
David Benjamin6c2563e2015-04-03 03:47:47 -04001039// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
1040// the result value of the final |SSL_read| call.
1041static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001042 const TestConfig *config = GetTestConfig(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -05001043 TestState *test_state = GetTestState(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001044 int ret;
1045 do {
David Benjamin13e81fc2015-11-02 17:16:13 -05001046 if (config->async) {
1047 // The DTLS retransmit logic silently ignores write failures. So the test
1048 // may progress, allow writes through synchronously. |SSL_read| may
1049 // trigger a retransmit, so disconnect the write quota.
1050 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
1051 }
David Benjaminf3fbade2016-09-19 13:08:16 -04001052 ret = config->peek_then_read ? SSL_peek(ssl, out, max_out)
1053 : SSL_read(ssl, out, max_out);
David Benjamin13e81fc2015-11-02 17:16:13 -05001054 if (config->async) {
1055 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
1056 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001057 } while (config->async && RetryAsync(ssl, ret));
David Benjaminf3fbade2016-09-19 13:08:16 -04001058
1059 if (config->peek_then_read && ret > 0) {
1060 std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]);
1061
1062 // SSL_peek should synchronously return the same data.
1063 int ret2 = SSL_peek(ssl, buf.get(), ret);
1064 if (ret2 != ret ||
1065 memcmp(buf.get(), out, ret) != 0) {
1066 fprintf(stderr, "First and second SSL_peek did not match.\n");
1067 return -1;
1068 }
1069
1070 // SSL_read should synchronously return the same data and consume it.
1071 ret2 = SSL_read(ssl, buf.get(), ret);
1072 if (ret2 != ret ||
1073 memcmp(buf.get(), out, ret) != 0) {
1074 fprintf(stderr, "SSL_peek and SSL_read did not match.\n");
1075 return -1;
1076 }
1077 }
1078
David Benjamin6c2563e2015-04-03 03:47:47 -04001079 return ret;
1080}
1081
1082// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
1083// operations. It returns the result of the final |SSL_write| call.
1084static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001085 const TestConfig *config = GetTestConfig(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001086 int ret;
1087 do {
1088 ret = SSL_write(ssl, in, in_len);
1089 if (ret > 0) {
1090 in += ret;
1091 in_len -= ret;
1092 }
1093 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
1094 return ret;
1095}
1096
David Benjamin30789da2015-08-29 22:56:45 -04001097// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
1098// returns the result of the final |SSL_shutdown| call.
1099static int DoShutdown(SSL *ssl) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001100 const TestConfig *config = GetTestConfig(ssl);
David Benjamin30789da2015-08-29 22:56:45 -04001101 int ret;
1102 do {
1103 ret = SSL_shutdown(ssl);
1104 } while (config->async && RetryAsync(ssl, ret));
1105 return ret;
1106}
1107
David Benjamin1d4f4c02016-07-26 18:03:08 -04001108// DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous
1109// operations. It returns the result of the final |SSL_send_fatal_alert| call.
1110static int DoSendFatalAlert(SSL *ssl, uint8_t alert) {
1111 const TestConfig *config = GetTestConfig(ssl);
1112 int ret;
1113 do {
1114 ret = SSL_send_fatal_alert(ssl, alert);
1115 } while (config->async && RetryAsync(ssl, ret));
1116 return ret;
1117}
1118
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001119static uint16_t GetProtocolVersion(const SSL *ssl) {
1120 uint16_t version = SSL_version(ssl);
1121 if (!SSL_is_dtls(ssl)) {
1122 return version;
1123 }
1124 return 0x0201 + ~version;
1125}
1126
David Benjamin91eab5c2015-06-18 18:35:46 -04001127// CheckHandshakeProperties checks, immediately after |ssl| completes its
1128// initial handshake (or False Starts), whether all the properties are
1129// consistent with the test configuration and invariants.
1130static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001131 const TestConfig *config = GetTestConfig(ssl);
David Benjamin91eab5c2015-06-18 18:35:46 -04001132
1133 if (SSL_get_current_cipher(ssl) == nullptr) {
1134 fprintf(stderr, "null cipher after handshake\n");
1135 return false;
1136 }
1137
1138 if (is_resume &&
1139 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
1140 fprintf(stderr, "session was%s reused\n",
1141 SSL_session_reused(ssl) ? "" : " not");
1142 return false;
1143 }
1144
1145 bool expect_handshake_done = is_resume || !config->false_start;
1146 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
1147 fprintf(stderr, "handshake was%s completed\n",
1148 GetTestState(ssl)->handshake_done ? "" : " not");
1149 return false;
1150 }
1151
David Benjaminba4594a2015-06-18 18:36:15 -04001152 if (expect_handshake_done && !config->is_server) {
1153 bool expect_new_session =
1154 !config->expect_no_session &&
Steven Valdez143e8b32016-07-11 13:19:03 -04001155 (!SSL_session_reused(ssl) || config->expect_ticket_renewal) &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001156 // Session tickets are sent post-handshake in TLS 1.3.
1157 GetProtocolVersion(ssl) < TLS1_3_VERSION;
David Benjaminba4594a2015-06-18 18:36:15 -04001158 if (expect_new_session != GetTestState(ssl)->got_new_session) {
1159 fprintf(stderr,
David Benjamindd6fed92015-10-23 17:41:12 -04001160 "new session was%s cached, but we expected the opposite\n",
David Benjaminba4594a2015-06-18 18:36:15 -04001161 GetTestState(ssl)->got_new_session ? "" : " not");
1162 return false;
1163 }
1164 }
1165
David Benjamin91eab5c2015-06-18 18:35:46 -04001166 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
1167 fprintf(stderr, "early callback not called\n");
1168 return false;
1169 }
1170
1171 if (!config->expected_server_name.empty()) {
1172 const char *server_name =
1173 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1174 if (server_name != config->expected_server_name) {
1175 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
1176 server_name, config->expected_server_name.c_str());
1177 return false;
1178 }
1179 }
1180
David Benjamin91eab5c2015-06-18 18:35:46 -04001181 if (!config->expected_next_proto.empty()) {
1182 const uint8_t *next_proto;
1183 unsigned next_proto_len;
1184 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
1185 if (next_proto_len != config->expected_next_proto.size() ||
1186 memcmp(next_proto, config->expected_next_proto.data(),
1187 next_proto_len) != 0) {
1188 fprintf(stderr, "negotiated next proto mismatch\n");
1189 return false;
1190 }
1191 }
1192
1193 if (!config->expected_alpn.empty()) {
1194 const uint8_t *alpn_proto;
1195 unsigned alpn_proto_len;
1196 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
1197 if (alpn_proto_len != config->expected_alpn.size() ||
1198 memcmp(alpn_proto, config->expected_alpn.data(),
1199 alpn_proto_len) != 0) {
1200 fprintf(stderr, "negotiated alpn proto mismatch\n");
1201 return false;
1202 }
1203 }
1204
1205 if (!config->expected_channel_id.empty()) {
1206 uint8_t channel_id[64];
1207 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
1208 fprintf(stderr, "no channel id negotiated\n");
1209 return false;
1210 }
1211 if (config->expected_channel_id.size() != 64 ||
1212 memcmp(config->expected_channel_id.data(),
1213 channel_id, 64) != 0) {
1214 fprintf(stderr, "channel id mismatch\n");
1215 return false;
1216 }
1217 }
1218
1219 if (config->expect_extended_master_secret) {
David Benjamin8ac35712016-07-13 21:07:29 -04001220 if (!SSL_get_extms_support(ssl)) {
1221 fprintf(stderr, "No EMS for connection when expected");
David Benjamin91eab5c2015-06-18 18:35:46 -04001222 return false;
1223 }
1224 }
1225
1226 if (!config->expected_ocsp_response.empty()) {
1227 const uint8_t *data;
1228 size_t len;
1229 SSL_get0_ocsp_response(ssl, &data, &len);
1230 if (config->expected_ocsp_response.size() != len ||
1231 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
1232 fprintf(stderr, "OCSP response mismatch\n");
1233 return false;
1234 }
1235 }
1236
1237 if (!config->expected_signed_cert_timestamps.empty()) {
1238 const uint8_t *data;
1239 size_t len;
1240 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
1241 if (config->expected_signed_cert_timestamps.size() != len ||
1242 memcmp(config->expected_signed_cert_timestamps.data(),
1243 data, len) != 0) {
1244 fprintf(stderr, "SCT list mismatch\n");
1245 return false;
1246 }
1247 }
1248
Paul Lietar8f1c2682015-08-18 12:21:54 +01001249 if (config->expect_verify_result) {
1250 int expected_verify_result = config->verify_fail ?
1251 X509_V_ERR_APPLICATION_VERIFICATION :
1252 X509_V_OK;
1253
1254 if (SSL_get_verify_result(ssl) != expected_verify_result) {
1255 fprintf(stderr, "Wrong certificate verification result\n");
1256 return false;
1257 }
1258 }
1259
Nick Harper60edffd2016-06-21 15:19:24 -07001260 if (config->expect_peer_signature_algorithm != 0 &&
1261 config->expect_peer_signature_algorithm !=
1262 SSL_get_peer_signature_algorithm(ssl)) {
1263 fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n",
1264 SSL_get_peer_signature_algorithm(ssl),
1265 config->expect_peer_signature_algorithm);
David Benjamin6e807652015-11-02 12:02:20 -05001266 return false;
1267 }
1268
David Benjamin9e68f192016-06-30 14:55:33 -04001269 if (config->expect_curve_id != 0) {
1270 uint16_t curve_id = SSL_get_curve_id(ssl);
1271 if (static_cast<uint16_t>(config->expect_curve_id) != curve_id) {
1272 fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id,
1273 static_cast<uint16_t>(config->expect_curve_id));
1274 return false;
1275 }
1276 }
1277
1278 if (config->expect_dhe_group_size != 0) {
1279 unsigned dhe_group_size = SSL_get_dhe_group_size(ssl);
1280 if (static_cast<unsigned>(config->expect_dhe_group_size) !=
1281 dhe_group_size) {
1282 fprintf(stderr, "dhe_group_size was %u, wanted %d\n", dhe_group_size,
1283 config->expect_dhe_group_size);
David Benjamin4cc36ad2015-12-19 14:23:26 -05001284 return false;
1285 }
1286 }
1287
David Benjaminbb9e36e2016-08-03 14:14:47 -04001288 if (!config->psk.empty()) {
1289 if (SSL_get_peer_cert_chain(ssl) != nullptr) {
1290 fprintf(stderr, "Received peer certificate on a PSK cipher.\n");
1291 return false;
1292 }
1293 } else if (!config->is_server || config->require_any_client_certificate) {
1294 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
1295 fprintf(stderr, "Received no peer certificate but expected one.\n");
David Benjamin91eab5c2015-06-18 18:35:46 -04001296 return false;
1297 }
1298 }
David Benjaminbb9e36e2016-08-03 14:14:47 -04001299
David Benjamin91eab5c2015-06-18 18:35:46 -04001300 return true;
1301}
1302
David Benjamin87c8a642015-02-21 01:54:29 -05001303// DoExchange runs a test SSL exchange against the peer. On success, it returns
1304// true and sets |*out_session| to the negotiated SSL session. If the test is a
1305// resumption attempt, |is_resume| is true and |session| is the session from the
1306// previous exchange.
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001307static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session,
1308 SSL_CTX *ssl_ctx, const TestConfig *config,
1309 bool is_resume, SSL_SESSION *session) {
1310 bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx));
David Benjamina7f333d2015-02-09 02:37:18 -05001311 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001312 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001313 }
1314
David Benjamin7e7a82d2016-05-20 20:12:42 -04001315 if (!SetTestConfig(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001316 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001317 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001318 }
David Benjamin5a593af2014-08-11 19:51:50 -04001319
Adam Langley5f0efe02015-02-20 13:03:16 -08001320 if (config->fallback_scsv &&
1321 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1322 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001323 }
David Benjamina0486782016-10-06 19:11:32 -04001324 // Install the certificate synchronously if nothing else will handle it.
1325 if (!config->use_early_callback &&
1326 !config->use_old_client_cert_callback &&
1327 !config->async &&
1328 !InstallCertificate(ssl.get())) {
1329 return false;
David Benjamin5a593af2014-08-11 19:51:50 -04001330 }
David Benjamina0486782016-10-06 19:11:32 -04001331 SSL_set_cert_cb(ssl.get(), CertCallback, nullptr);
David Benjamin5a593af2014-08-11 19:51:50 -04001332 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001333 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001334 NULL);
1335 }
1336 if (config->verify_peer) {
1337 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001338 }
1339 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001340 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001341 }
1342 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001343 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001344 }
1345 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001346 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001347 }
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001348 if (config->no_tls13) {
1349 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3);
1350 }
David Benjamin5a593af2014-08-11 19:51:50 -04001351 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001352 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001353 }
1354 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001355 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001356 }
1357 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001358 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001359 }
1360 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001361 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001362 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001363 if (!config->expected_channel_id.empty() ||
1364 config->enable_channel_id) {
David Benjamina7f333d2015-02-09 02:37:18 -05001365 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001366 }
1367 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001368 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001369 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001370 // The async case will be supplied by |ChannelIdCallback|.
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001371 bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(config->send_channel_id);
David Benjamind9e07012015-02-09 03:04:34 -05001372 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001373 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001374 }
David Benjamina08e49d2014-08-24 01:46:07 -04001375 }
David Benjamina08e49d2014-08-24 01:46:07 -04001376 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001377 if (!config->host_name.empty() &&
1378 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001379 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001380 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001381 if (!config->advertise_alpn.empty() &&
1382 SSL_set_alpn_protos(ssl.get(),
1383 (const uint8_t *)config->advertise_alpn.data(),
1384 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001385 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001386 }
David Benjamin48cae082014-10-27 01:06:24 -04001387 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001388 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1389 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001390 }
David Benjamin61f95272014-11-25 01:55:35 -05001391 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001392 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001393 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001394 }
David Benjamin61f95272014-11-25 01:55:35 -05001395 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001396 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001397 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001398 }
1399 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001400 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001401 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001402 }
1403 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001404 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001405 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001406 }
David Benjamin2dc02042016-09-19 19:57:37 -04001407 if (config->min_version != 0 &&
David Benjamine4706902016-09-20 15:12:23 -04001408 !SSL_set_min_proto_version(ssl.get(), (uint16_t)config->min_version)) {
David Benjamin2dc02042016-09-19 19:57:37 -04001409 return false;
David Benjamin1eb367c2014-12-12 18:17:51 -05001410 }
David Benjamin2dc02042016-09-19 19:57:37 -04001411 if (config->max_version != 0 &&
David Benjamine4706902016-09-20 15:12:23 -04001412 !SSL_set_max_proto_version(ssl.get(), (uint16_t)config->max_version)) {
David Benjamin2dc02042016-09-19 19:57:37 -04001413 return false;
David Benjamin1eb367c2014-12-12 18:17:51 -05001414 }
David Benjamin13be1de2015-01-11 16:29:36 -05001415 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001416 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1417 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001418 }
Adam Langley524e7172015-02-20 16:04:00 -08001419 if (config->install_ddos_callback) {
1420 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1421 }
David Benjamin1d5ef3b2015-10-12 19:54:18 -04001422 if (config->renegotiate_once) {
1423 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once);
1424 }
1425 if (config->renegotiate_freely) {
1426 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04001427 }
Adam Langley27a0d082015-11-03 13:34:10 -08001428 if (config->renegotiate_ignore) {
1429 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore);
1430 }
David Benjamin30789da2015-08-29 22:56:45 -04001431 if (!config->check_close_notify) {
1432 SSL_set_quiet_shutdown(ssl.get(), 1);
1433 }
David Benjamin091c4b92015-10-26 13:33:21 -04001434 if (config->disable_npn) {
1435 SSL_set_options(ssl.get(), SSL_OP_DISABLE_NPN);
1436 }
David Benjamin99fdfb92015-11-02 12:11:35 -05001437 if (config->p384_only) {
1438 int nid = NID_secp384r1;
1439 if (!SSL_set1_curves(ssl.get(), &nid, 1)) {
1440 return false;
1441 }
1442 }
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001443 if (config->enable_all_curves) {
1444 static const int kAllCurves[] = {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001445 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519,
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001446 };
1447 if (!SSL_set1_curves(ssl.get(), kAllCurves,
Steven Valdezcb966542016-08-17 16:56:14 -04001448 OPENSSL_ARRAY_SIZE(kAllCurves))) {
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001449 return false;
1450 }
1451 }
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -07001452 if (config->initial_timeout_duration_ms > 0) {
1453 DTLSv1_set_initial_timeout_duration(ssl.get(),
1454 config->initial_timeout_duration_ms);
1455 }
David Benjamina252b342016-09-26 19:57:53 -04001456 if (config->max_cert_list > 0) {
1457 SSL_set_max_cert_list(ssl.get(), config->max_cert_list);
1458 }
David Benjamin025b3d32014-07-01 19:53:04 -04001459
David Benjamin87c8a642015-02-21 01:54:29 -05001460 int sock = Connect(config->port);
1461 if (sock == -1) {
1462 return false;
1463 }
1464 SocketCloser closer(sock);
1465
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001466 bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001467 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001468 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001469 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001470 if (config->is_dtls) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001471 bssl::UniquePtr<BIO> packeted = PacketedBioCreate(!config->async);
David Benjaminb7c5e842016-03-28 09:59:10 -04001472 if (!packeted) {
1473 return false;
1474 }
David Benjamin585d7a42016-06-02 14:58:00 -04001475 GetTestState(ssl.get())->packeted_bio = packeted.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001476 BIO_push(packeted.get(), bio.release());
1477 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001478 }
David Benjamin5a593af2014-08-11 19:51:50 -04001479 if (config->async) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001480 bssl::UniquePtr<BIO> async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001481 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjaminb7c5e842016-03-28 09:59:10 -04001482 if (!async_scoped) {
1483 return false;
1484 }
David Benjamina7f333d2015-02-09 02:37:18 -05001485 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001486 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001487 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001488 }
David Benjamina7f333d2015-02-09 02:37:18 -05001489 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1490 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001491
David Benjamin1d5c83e2014-07-22 19:20:02 -04001492 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001493 if (!config->is_server) {
1494 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001495 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001496 }
1497 } else if (config->async) {
1498 // The internal session cache is disabled, so install the session
1499 // manually.
David Benjaminb9195402016-08-05 10:51:43 -04001500 SSL_SESSION_up_ref(session);
1501 GetTestState(ssl.get())->pending_session.reset(session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001502 }
1503 }
1504
David Benjamina07c0fc2015-05-13 13:19:42 -04001505 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1506 fprintf(stderr, "non-null cipher before handshake\n");
1507 return false;
1508 }
1509
David Benjamin1d5c83e2014-07-22 19:20:02 -04001510 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001511 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001512 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001513 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001514 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001515 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001516 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001517 } else {
1518 do {
1519 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001520 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001521 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001522 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001523 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001524 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001525 if (ret != 1 ||
1526 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001527 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001528 }
David Benjamin025b3d32014-07-01 19:53:04 -04001529
David Benjaminba4594a2015-06-18 18:36:15 -04001530 // Reset the state to assert later that the callback isn't called in
1531 // renegotations.
1532 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001533 }
1534
David Benjaminc565ebb2015-04-03 04:06:36 -04001535 if (config->export_keying_material > 0) {
1536 std::vector<uint8_t> result(
1537 static_cast<size_t>(config->export_keying_material));
1538 if (!SSL_export_keying_material(
1539 ssl.get(), result.data(), result.size(),
1540 config->export_label.data(), config->export_label.size(),
1541 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1542 config->export_context.size(), config->use_export_context)) {
1543 fprintf(stderr, "failed to export keying material\n");
1544 return false;
1545 }
1546 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1547 return false;
1548 }
1549 }
1550
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001551 if (config->tls_unique) {
1552 uint8_t tls_unique[16];
1553 size_t tls_unique_len;
1554 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1555 sizeof(tls_unique))) {
1556 fprintf(stderr, "failed to get tls-unique\n");
1557 return false;
1558 }
1559
1560 if (tls_unique_len != 12) {
1561 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1562 static_cast<unsigned>(tls_unique_len));
1563 return false;
1564 }
1565
1566 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1567 return false;
1568 }
1569 }
1570
David Benjamin1d4f4c02016-07-26 18:03:08 -04001571 if (config->send_alert) {
1572 if (DoSendFatalAlert(ssl.get(), SSL_AD_DECOMPRESSION_FAILURE) < 0) {
1573 return false;
1574 }
1575 return true;
1576 }
1577
David Benjamin5a593af2014-08-11 19:51:50 -04001578 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001579 if (config->is_dtls) {
1580 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001581 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001582 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001583 // This mode writes a number of different record sizes in an attempt to
1584 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001585 static const size_t kBufLen = 32769;
1586 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1587 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001588 static const size_t kRecordSizes[] = {
1589 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
Steven Valdezcb966542016-08-17 16:56:14 -04001590 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001591 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001592 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001593 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001594 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001595 }
Adam Langleybc949292015-06-18 21:32:44 -07001596 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001597 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001598 }
1599 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001600 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001601 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001602 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1603 5) < 0) {
1604 return false;
1605 }
David Benjamine58c4f52014-08-24 03:47:07 -04001606 }
David Benjamin30789da2015-08-29 22:56:45 -04001607 if (!config->shim_shuts_down) {
1608 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001609 static const size_t kBufLen = 16384;
1610 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1611
David Benjamin2c99d282015-09-01 10:23:00 -04001612 // Read only 512 bytes at a time in TLS to ensure records may be
1613 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001614 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001615 int err = SSL_get_error(ssl.get(), n);
1616 if (err == SSL_ERROR_ZERO_RETURN ||
1617 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1618 if (n != 0) {
1619 fprintf(stderr, "Invalid SSL_get_error output\n");
1620 return false;
1621 }
1622 // Stop on either clean or unclean shutdown.
1623 break;
1624 } else if (err != SSL_ERROR_NONE) {
1625 if (n > 0) {
1626 fprintf(stderr, "Invalid SSL_get_error output\n");
1627 return false;
1628 }
1629 return false;
1630 }
1631 // Successfully read data.
1632 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001633 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001634 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001635 }
David Benjamin30789da2015-08-29 22:56:45 -04001636
1637 // After a successful read, with or without False Start, the handshake
1638 // must be complete.
1639 if (!GetTestState(ssl.get())->handshake_done) {
1640 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001641 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001642 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001643
David Benjamin30789da2015-08-29 22:56:45 -04001644 for (int i = 0; i < n; i++) {
1645 buf[i] ^= 0xff;
1646 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001647 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001648 return false;
1649 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001650 }
1651 }
David Benjamin025b3d32014-07-01 19:53:04 -04001652 }
1653
David Benjaminba4594a2015-06-18 18:36:15 -04001654 if (!config->is_server && !config->false_start &&
1655 !config->implicit_handshake &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001656 // Session tickets are sent post-handshake in TLS 1.3.
1657 GetProtocolVersion(ssl.get()) < TLS1_3_VERSION &&
David Benjaminba4594a2015-06-18 18:36:15 -04001658 GetTestState(ssl.get())->got_new_session) {
1659 fprintf(stderr, "new session was established after the handshake\n");
1660 return false;
1661 }
1662
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001663 if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) {
1664 bool expect_new_session =
1665 !config->expect_no_session && !config->shim_shuts_down;
1666 if (expect_new_session != GetTestState(ssl.get())->got_new_session) {
1667 fprintf(stderr,
1668 "new session was%s cached, but we expected the opposite\n",
1669 GetTestState(ssl.get())->got_new_session ? "" : " not");
1670 return false;
1671 }
1672 }
1673
David Benjamin1d5c83e2014-07-22 19:20:02 -04001674 if (out_session) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001675 *out_session = std::move(GetTestState(ssl.get())->new_session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001676 }
1677
David Benjamin30789da2015-08-29 22:56:45 -04001678 ret = DoShutdown(ssl.get());
1679
1680 if (config->shim_shuts_down && config->check_close_notify) {
1681 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1682 // it returns zero when our close_notify is sent, then one when the peer's
1683 // is received.
1684 if (ret != 0) {
1685 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1686 return false;
1687 }
1688 ret = DoShutdown(ssl.get());
1689 }
1690
1691 if (ret != 1) {
1692 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1693 return false;
1694 }
1695
David Benjamin324dce42015-10-12 19:49:00 -04001696 if (SSL_total_renegotiations(ssl.get()) !=
1697 config->expect_total_renegotiations) {
1698 fprintf(stderr, "Expected %d renegotiations, got %d\n",
1699 config->expect_total_renegotiations,
1700 SSL_total_renegotiations(ssl.get()));
1701 return false;
1702 }
1703
David Benjamin40f101b2015-02-20 11:23:42 -05001704 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001705}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001706
David Benjaminff3a1492016-03-02 10:12:06 -05001707class StderrDelimiter {
1708 public:
1709 ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
1710};
1711
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -07001712static int Main(int argc, char **argv) {
David Benjaminff3a1492016-03-02 10:12:06 -05001713 // To distinguish ASan's output from ours, add a trailing message to stderr.
1714 // Anything following this line will be considered an error.
1715 StderrDelimiter delimiter;
1716
David Benjamin87c8a642015-02-21 01:54:29 -05001717#if defined(OPENSSL_WINDOWS)
1718 /* Initialize Winsock. */
1719 WORD wsa_version = MAKEWORD(2, 2);
1720 WSADATA wsa_data;
1721 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1722 if (wsa_err != 0) {
1723 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1724 return 1;
1725 }
1726 if (wsa_data.wVersion != wsa_version) {
1727 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1728 return 1;
1729 }
1730#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001731 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001732#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001733
David Benjamin7a1eefd2015-10-17 23:39:22 -04001734 CRYPTO_library_init();
David Benjamind9e07012015-02-09 03:04:34 -05001735 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001736 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001737 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001738 return 1;
1739 }
David Benjamin5a593af2014-08-11 19:51:50 -04001740
1741 TestConfig config;
1742 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001743 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001744 }
1745
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001746 bssl::UniquePtr<SSL_CTX> ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001747 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001748 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001749 return 1;
1750 }
1751
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001752 bssl::UniquePtr<SSL_SESSION> session;
David Benjamin46662482016-08-17 00:51:00 -04001753 for (int i = 0; i < config.resume_count + 1; i++) {
1754 bool is_resume = i > 0;
1755 if (is_resume && !config.is_server && !session) {
1756 fprintf(stderr, "No session to offer.\n");
1757 return 1;
1758 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001759
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001760 bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session);
David Benjamin46662482016-08-17 00:51:00 -04001761 if (!DoExchange(&session, ssl_ctx.get(), &config, is_resume,
1762 offer_session.get())) {
1763 fprintf(stderr, "Connection %d failed.\n", i + 1);
1764 ERR_print_errors_fp(stderr);
1765 return 1;
1766 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001767 }
1768
David Benjamina7f333d2015-02-09 02:37:18 -05001769 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001770}
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -07001771
1772} // namespace bssl
1773
1774int main(int argc, char **argv) {
1775 return bssl::Main(argc, argv);
1776}