blob: 2a9ba88e57b678d249c314a84154f207cd09cbb3 [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>
Brian Smith83a82982015-04-09 16:21:10 -100049#include <openssl/err.h>
David Benjamind98452d2015-06-16 14:16:23 -040050#include <openssl/hmac.h>
David Benjamin98193672016-03-25 18:07:11 -040051#include <openssl/nid.h>
David Benjamind98452d2015-06-16 14:16:23 -040052#include <openssl/rand.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040053#include <openssl/ssl.h>
54
David Benjamin45fb1be2015-03-22 16:31:27 -040055#include <memory>
Steven Valdez0d62f262015-09-04 12:41:04 -040056#include <string>
David Benjaminc565ebb2015-04-03 04:06:36 -040057#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040058
Adam Langleyd2b5af52016-07-12 08:03:59 -070059#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040060#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040061#include "packeted_bio.h"
Adam Langleyd2b5af52016-07-12 08:03:59 -070062#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040063#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040064
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -070065namespace bssl {
David Benjamin87c8a642015-02-21 01:54:29 -050066
67#if !defined(OPENSSL_WINDOWS)
68static int closesocket(int sock) {
69 return close(sock);
70}
71
72static void PrintSocketError(const char *func) {
73 perror(func);
74}
75#else
76static void PrintSocketError(const char *func) {
77 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
78}
79#endif
80
David Benjaminc273d2c2015-02-09 12:59:46 -050081static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050082 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040083 return 1;
84}
David Benjamin025b3d32014-07-01 19:53:04 -040085
David Benjamin2d445c02015-02-09 13:03:50 -050086struct TestState {
David Benjamin6c2563e2015-04-03 03:47:47 -040087 // async_bio is async BIO which pauses reads and writes.
88 BIO *async_bio = nullptr;
David Benjamin585d7a42016-06-02 14:58:00 -040089 // packeted_bio is the packeted BIO which simulates read timeouts.
90 BIO *packeted_bio = nullptr;
David Benjamind9e07012015-02-09 03:04:34 -050091 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040092 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050093 ScopedSSL_SESSION session;
94 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040095 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040096 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040097 // private_key is the underlying private key used when testing custom keys.
98 ScopedEVP_PKEY private_key;
nagendra modadugu3398dbf2015-08-07 14:07:52 -070099 std::vector<uint8_t> private_key_result;
100 // private_key_retries is the number of times an asynchronous private key
101 // operation has been retried.
102 unsigned private_key_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400103 bool got_new_session = false;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400104 ScopedSSL_SESSION new_session;
David Benjamin25fe85b2016-08-09 20:00:32 -0400105 bool ticket_decrypt_done = false;
106 bool alpn_select_done = false;
David Benjamind9e07012015-02-09 03:04:34 -0500107};
108
David Benjamin2d445c02015-02-09 13:03:50 -0500109static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700110 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500111 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500112}
113
114static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500115static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400116
David Benjamin7e7a82d2016-05-20 20:12:42 -0400117static bool SetTestConfig(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500118 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400119}
120
David Benjamin7e7a82d2016-05-20 20:12:42 -0400121static const TestConfig *GetTestConfig(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500122 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400123}
124
David Benjamin22050932015-11-23 13:44:48 -0500125static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
126 // |SSL_set_ex_data| takes ownership of |state| only on success.
127 if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
128 state.release();
David Benjamind9e07012015-02-09 03:04:34 -0500129 return true;
130 }
131 return false;
132}
133
David Benjamin87e4acd2015-04-02 19:57:35 -0400134static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500135 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500136}
137
David Benjaminacb6dcc2016-03-10 09:15:01 -0500138static ScopedX509 LoadCertificate(const std::string &file) {
139 ScopedBIO bio(BIO_new(BIO_s_file()));
140 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
141 return nullptr;
142 }
143 return ScopedX509(PEM_read_bio_X509(bio.get(), NULL, NULL, NULL));
144}
145
David Benjamina7f333d2015-02-09 02:37:18 -0500146static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
147 ScopedBIO bio(BIO_new(BIO_s_file()));
148 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
149 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400150 }
David Benjaminacb6dcc2016-03-10 09:15:01 -0500151 return ScopedEVP_PKEY(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400152}
153
David Benjaminb4d65fd2015-05-29 17:11:21 -0400154static int AsyncPrivateKeyType(SSL *ssl) {
David Benjamin0c0b7e12016-07-14 13:47:55 -0400155 EVP_PKEY *key = GetTestState(ssl)->private_key.get();
156 switch (EVP_PKEY_id(key)) {
157 case EVP_PKEY_RSA:
158 return NID_rsaEncryption;
159 case EVP_PKEY_EC:
160 return EC_GROUP_get_curve_name(
161 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(key)));
162 default:
163 return NID_undef;
164 }
David Benjaminb4d65fd2015-05-29 17:11:21 -0400165}
166
David Benjaminb4d65fd2015-05-29 17:11:21 -0400167static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
168 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
169}
170
171static ssl_private_key_result_t AsyncPrivateKeySign(
172 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamind3440b42016-07-14 14:52:41 -0400173 uint16_t signature_algorithm, const uint8_t *in, size_t in_len) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400174 TestState *test_state = GetTestState(ssl);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700175 if (!test_state->private_key_result.empty()) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400176 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
177 abort();
178 }
179
David Benjamind3440b42016-07-14 14:52:41 -0400180 // Determine the hash.
181 const EVP_MD *md;
182 switch (signature_algorithm) {
183 case SSL_SIGN_RSA_PKCS1_SHA1:
184 case SSL_SIGN_ECDSA_SHA1:
185 md = EVP_sha1();
186 break;
187 case SSL_SIGN_RSA_PKCS1_SHA256:
188 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
189 case SSL_SIGN_RSA_PSS_SHA256:
190 md = EVP_sha256();
191 break;
192 case SSL_SIGN_RSA_PKCS1_SHA384:
193 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
194 case SSL_SIGN_RSA_PSS_SHA384:
195 md = EVP_sha384();
196 break;
197 case SSL_SIGN_RSA_PKCS1_SHA512:
198 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
199 case SSL_SIGN_RSA_PSS_SHA512:
200 md = EVP_sha512();
201 break;
202 case SSL_SIGN_RSA_PKCS1_MD5_SHA1:
203 md = EVP_md5_sha1();
204 break;
205 default:
206 fprintf(stderr, "Unknown signature algorithm %04x.\n",
207 signature_algorithm);
208 return ssl_private_key_failure;
209 }
210
211 ScopedEVP_MD_CTX ctx;
212 EVP_PKEY_CTX *pctx;
213 if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr,
214 test_state->private_key.get())) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400215 return ssl_private_key_failure;
216 }
217
David Benjamind3440b42016-07-14 14:52:41 -0400218 // Configure additional signature parameters.
219 switch (signature_algorithm) {
220 case SSL_SIGN_RSA_PSS_SHA256:
221 case SSL_SIGN_RSA_PSS_SHA384:
222 case SSL_SIGN_RSA_PSS_SHA512:
223 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
224 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
225 -1 /* salt len = hash len */)) {
226 return ssl_private_key_failure;
227 }
228 }
229
David Benjaminb4d65fd2015-05-29 17:11:21 -0400230 // Write the signature into |test_state|.
231 size_t len = 0;
David Benjamind3440b42016-07-14 14:52:41 -0400232 if (!EVP_DigestSignUpdate(ctx.get(), in, in_len) ||
233 !EVP_DigestSignFinal(ctx.get(), nullptr, &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400234 return ssl_private_key_failure;
235 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700236 test_state->private_key_result.resize(len);
David Benjamind3440b42016-07-14 14:52:41 -0400237 if (!EVP_DigestSignFinal(ctx.get(), test_state->private_key_result.data(),
238 &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400239 return ssl_private_key_failure;
240 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700241 test_state->private_key_result.resize(len);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400242
David Benjamind3440b42016-07-14 14:52:41 -0400243 // The signature will be released asynchronously in |AsyncPrivateKeyComplete|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400244 return ssl_private_key_retry;
245}
246
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700247static ssl_private_key_result_t AsyncPrivateKeyDecrypt(
248 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
249 const uint8_t *in, size_t in_len) {
250 TestState *test_state = GetTestState(ssl);
251 if (!test_state->private_key_result.empty()) {
252 fprintf(stderr,
253 "AsyncPrivateKeyDecrypt called with operation pending.\n");
254 abort();
255 }
256
David Benjamin758d1272015-11-20 17:47:25 -0500257 RSA *rsa = EVP_PKEY_get0_RSA(test_state->private_key.get());
258 if (rsa == NULL) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700259 fprintf(stderr,
260 "AsyncPrivateKeyDecrypt called with incorrect key type.\n");
261 abort();
262 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700263 test_state->private_key_result.resize(RSA_size(rsa));
David Benjaminef14b2d2015-11-11 14:01:27 -0800264 if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700265 RSA_size(rsa), in, in_len, RSA_NO_PADDING)) {
266 return ssl_private_key_failure;
267 }
268
269 test_state->private_key_result.resize(*out_len);
270
David Benjamind3440b42016-07-14 14:52:41 -0400271 // The decryption will be released asynchronously in |AsyncPrivateComplete|.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700272 return ssl_private_key_retry;
273}
274
David Benjamind3440b42016-07-14 14:52:41 -0400275static ssl_private_key_result_t AsyncPrivateKeyComplete(
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700276 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
277 TestState *test_state = GetTestState(ssl);
278 if (test_state->private_key_result.empty()) {
279 fprintf(stderr,
David Benjamind3440b42016-07-14 14:52:41 -0400280 "AsyncPrivateKeyComplete called without operation pending.\n");
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700281 abort();
282 }
283
284 if (test_state->private_key_retries < 2) {
285 // Only return the decryption on the second attempt, to test both incomplete
286 // |decrypt| and |decrypt_complete|.
287 return ssl_private_key_retry;
288 }
289
290 if (max_out < test_state->private_key_result.size()) {
291 fprintf(stderr, "Output buffer too small.\n");
292 return ssl_private_key_failure;
293 }
David Benjaminef14b2d2015-11-11 14:01:27 -0800294 memcpy(out, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700295 test_state->private_key_result.size());
296 *out_len = test_state->private_key_result.size();
297
298 test_state->private_key_result.clear();
299 test_state->private_key_retries = 0;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400300 return ssl_private_key_success;
301}
302
303static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
304 AsyncPrivateKeyType,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400305 AsyncPrivateKeyMaxSignatureLen,
306 AsyncPrivateKeySign,
David Benjamind3440b42016-07-14 14:52:41 -0400307 nullptr /* sign_digest */,
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700308 AsyncPrivateKeyDecrypt,
David Benjamind3440b42016-07-14 14:52:41 -0400309 AsyncPrivateKeyComplete,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400310};
311
Steven Valdez0d62f262015-09-04 12:41:04 -0400312template<typename T>
Adam Langley10f97f32016-07-12 08:09:33 -0700313struct Free {
Steven Valdez0d62f262015-09-04 12:41:04 -0400314 void operator()(T *buf) {
315 free(buf);
316 }
317};
318
David Benjaminacb6dcc2016-03-10 09:15:01 -0500319static bool GetCertificate(SSL *ssl, ScopedX509 *out_x509,
320 ScopedEVP_PKEY *out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400321 const TestConfig *config = GetTestConfig(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -0400322
323 if (!config->digest_prefs.empty()) {
Adam Langley10f97f32016-07-12 08:09:33 -0700324 std::unique_ptr<char, Free<char>> digest_prefs(
Steven Valdez0d62f262015-09-04 12:41:04 -0400325 strdup(config->digest_prefs.c_str()));
Steven Valdez0d62f262015-09-04 12:41:04 -0400326 std::vector<int> digest_list;
327
328 for (;;) {
Adam Langley67251f22015-09-23 15:01:07 -0700329 char *token =
330 strtok(digest_list.empty() ? digest_prefs.get() : nullptr, ",");
Steven Valdez0d62f262015-09-04 12:41:04 -0400331 if (token == nullptr) {
332 break;
333 }
334
335 digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token)));
336 }
337
338 if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(),
339 digest_list.size())) {
340 return false;
341 }
342 }
343
David Benjaminca3d5452016-07-14 12:51:01 -0400344 if (!config->signing_prefs.empty()) {
345 std::vector<uint16_t> u16s(config->signing_prefs.begin(),
346 config->signing_prefs.end());
347 if (!SSL_set_signing_algorithm_prefs(ssl, u16s.data(), u16s.size())) {
348 return false;
349 }
350 }
351
David Benjaminb4d65fd2015-05-29 17:11:21 -0400352 if (!config->key_file.empty()) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500353 *out_pkey = LoadPrivateKey(config->key_file.c_str());
354 if (!*out_pkey) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400355 return false;
356 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500357 }
David Benjaminacb6dcc2016-03-10 09:15:01 -0500358 if (!config->cert_file.empty()) {
359 *out_x509 = LoadCertificate(config->cert_file.c_str());
360 if (!*out_x509) {
361 return false;
362 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500363 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100364 if (!config->ocsp_response.empty() &&
365 !SSL_CTX_set_ocsp_response(ssl->ctx,
366 (const uint8_t *)config->ocsp_response.data(),
367 config->ocsp_response.size())) {
368 return false;
369 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500370 return true;
371}
372
David Benjaminacb6dcc2016-03-10 09:15:01 -0500373static bool InstallCertificate(SSL *ssl) {
374 ScopedX509 x509;
375 ScopedEVP_PKEY pkey;
376 if (!GetCertificate(ssl, &x509, &pkey)) {
377 return false;
378 }
379
380 if (pkey) {
381 TestState *test_state = GetTestState(ssl);
David Benjamin7e7a82d2016-05-20 20:12:42 -0400382 const TestConfig *config = GetTestConfig(ssl);
David Benjaminacb6dcc2016-03-10 09:15:01 -0500383 if (config->async) {
384 test_state->private_key = std::move(pkey);
385 SSL_set_private_key_method(ssl, &g_async_private_key_method);
386 } else if (!SSL_use_PrivateKey(ssl, pkey.get())) {
387 return false;
388 }
389 }
390
391 if (x509 && !SSL_use_certificate(ssl, x509.get())) {
392 return false;
393 }
394
395 return true;
396}
397
David Benjaminc273d2c2015-02-09 12:59:46 -0500398static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400399 const TestConfig *config = GetTestConfig(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500400 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400401
David Benjamin6f5c0f42015-02-24 01:23:21 -0500402 if (!config->expected_server_name.empty()) {
403 const uint8_t *extension_data;
404 size_t extension_len;
405 CBS extension, server_name_list, host_name;
406 uint8_t name_type;
407
408 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
409 &extension_data,
410 &extension_len)) {
411 fprintf(stderr, "Could not find server_name extension.\n");
412 return -1;
413 }
414
415 CBS_init(&extension, extension_data, extension_len);
416 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
417 CBS_len(&extension) != 0 ||
418 !CBS_get_u8(&server_name_list, &name_type) ||
419 name_type != TLSEXT_NAMETYPE_host_name ||
420 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
421 CBS_len(&server_name_list) != 0) {
422 fprintf(stderr, "Could not decode server_name extension.\n");
423 return -1;
424 }
425
426 if (!CBS_mem_equal(&host_name,
427 (const uint8_t*)config->expected_server_name.data(),
428 config->expected_server_name.size())) {
429 fprintf(stderr, "Server name mismatch.\n");
430 }
David Benjamin7b030512014-07-08 17:30:11 -0400431 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400432
David Benjamin6f5c0f42015-02-24 01:23:21 -0500433 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400434 return -1;
435 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400436
David Benjamin6f5c0f42015-02-24 01:23:21 -0500437 // Install the certificate in the early callback.
438 if (config->use_early_callback) {
439 if (config->async) {
440 // Install the certificate asynchronously.
441 return 0;
442 }
443 if (!InstallCertificate(ctx->ssl)) {
444 return -1;
445 }
David Benjamin7b030512014-07-08 17:30:11 -0400446 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400447 return 1;
448}
David Benjamin025b3d32014-07-01 19:53:04 -0400449
David Benjaminacb6dcc2016-03-10 09:15:01 -0500450static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400451 if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500452 return -1;
453 }
454
455 ScopedX509 x509;
456 ScopedEVP_PKEY pkey;
457 if (!GetCertificate(ssl, &x509, &pkey)) {
458 return -1;
459 }
460
461 // Return zero for no certificate.
462 if (!x509) {
463 return 0;
464 }
465
466 // Asynchronous private keys are not supported with client_cert_cb.
467 *out_x509 = x509.release();
468 *out_pkey = pkey.release();
469 return 1;
470}
471
Paul Lietar8f1c2682015-08-18 12:21:54 +0100472static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
473 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
474 SSL_get_ex_data_X509_STORE_CTX_idx());
David Benjamin7e7a82d2016-05-20 20:12:42 -0400475 const TestConfig *config = GetTestConfig(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100476
477 if (!config->expected_ocsp_response.empty()) {
478 const uint8_t *data;
479 size_t len;
480 SSL_get0_ocsp_response(ssl, &data, &len);
481 if (len == 0) {
482 fprintf(stderr, "OCSP response not available in verify callback\n");
483 return 0;
484 }
485 }
486
David Benjamin67666e72014-07-12 15:47:52 -0400487 return 1;
488}
489
Paul Lietar8f1c2682015-08-18 12:21:54 +0100490static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
491 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
492 return 0;
493}
494
David Benjaminc273d2c2015-02-09 12:59:46 -0500495static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
496 unsigned int *out_len, void *arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400497 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500498 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400499 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500500 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400501
David Benjamin5a593af2014-08-11 19:51:50 -0400502 *out = (const uint8_t*)config->advertise_npn.data();
503 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400504 return SSL_TLSEXT_ERR_OK;
505}
506
David Benjaminc273d2c2015-02-09 12:59:46 -0500507static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400508 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400509 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500510 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400511 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500512 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400513
David Benjamin5a593af2014-08-11 19:51:50 -0400514 *out = (uint8_t*)config->select_next_proto.data();
515 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400516 return SSL_TLSEXT_ERR_OK;
517}
518
David Benjaminc273d2c2015-02-09 12:59:46 -0500519static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
520 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400521 if (GetTestState(ssl)->alpn_select_done) {
522 fprintf(stderr, "AlpnSelectCallback called after completion.\n");
523 exit(1);
524 }
525
526 GetTestState(ssl)->alpn_select_done = true;
527
David Benjamin7e7a82d2016-05-20 20:12:42 -0400528 const TestConfig *config = GetTestConfig(ssl);
David Benjamin594e7d22016-03-17 17:49:56 -0400529 if (config->decline_alpn) {
David Benjaminae2888f2014-09-06 12:58:58 -0400530 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500531 }
David Benjaminae2888f2014-09-06 12:58:58 -0400532
533 if (!config->expected_advertised_alpn.empty() &&
534 (config->expected_advertised_alpn.size() != inlen ||
535 memcmp(config->expected_advertised_alpn.data(),
536 in, inlen) != 0)) {
537 fprintf(stderr, "bad ALPN select callback inputs\n");
538 exit(1);
539 }
540
541 *out = (const uint8_t*)config->select_alpn.data();
542 *outlen = config->select_alpn.size();
543 return SSL_TLSEXT_ERR_OK;
544}
545
David Benjaminc273d2c2015-02-09 12:59:46 -0500546static unsigned PskClientCallback(SSL *ssl, const char *hint,
547 char *out_identity,
548 unsigned max_identity_len,
549 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400550 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400551
552 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
553 fprintf(stderr, "Server PSK hint did not match.\n");
554 return 0;
555 }
556
557 // Account for the trailing '\0' for the identity.
558 if (config->psk_identity.size() >= max_identity_len ||
559 config->psk.size() > max_psk_len) {
560 fprintf(stderr, "PSK buffers too small\n");
561 return 0;
562 }
563
564 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
565 max_identity_len);
566 memcpy(out_psk, config->psk.data(), config->psk.size());
567 return config->psk.size();
568}
569
David Benjaminc273d2c2015-02-09 12:59:46 -0500570static unsigned PskServerCallback(SSL *ssl, const char *identity,
571 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400572 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400573
574 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
575 fprintf(stderr, "Client PSK identity did not match.\n");
576 return 0;
577 }
578
579 if (config->psk.size() > max_psk_len) {
580 fprintf(stderr, "PSK buffers too small\n");
581 return 0;
582 }
583
584 memcpy(out_psk, config->psk.data(), config->psk.size());
585 return config->psk.size();
586}
587
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400588static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin585d7a42016-06-02 14:58:00 -0400589 *out_clock = PacketedBioGetClock(GetTestState(ssl)->packeted_bio);
David Benjamin377fc312015-01-26 00:22:12 -0500590}
591
David Benjaminc273d2c2015-02-09 12:59:46 -0500592static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500593 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500594}
595
David Benjaminc273d2c2015-02-09 12:59:46 -0500596static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500597 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500598 return -1;
599 }
600 if (!InstallCertificate(ssl)) {
601 return 0;
602 }
603 return 1;
604}
605
David Benjaminc273d2c2015-02-09 12:59:46 -0500606static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
607 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500608 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500609 if (async_state->session) {
610 *copy = 0;
611 return async_state->session.release();
612 } else if (async_state->pending_session) {
613 return SSL_magic_pending_session_ptr();
614 } else {
615 return NULL;
616 }
617}
618
Adam Langley524e7172015-02-20 16:04:00 -0800619static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400620 const TestConfig *config = GetTestConfig(early_context->ssl);
Adam Langley524e7172015-02-20 16:04:00 -0800621 static int callback_num = 0;
622
623 callback_num++;
624 if (config->fail_ddos_callback ||
625 (config->fail_second_ddos_callback && callback_num == 2)) {
626 return 0;
627 }
628 return 1;
629}
630
David Benjamin87e4acd2015-04-02 19:57:35 -0400631static void InfoCallback(const SSL *ssl, int type, int val) {
632 if (type == SSL_CB_HANDSHAKE_DONE) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400633 if (GetTestConfig(ssl)->handshake_never_done) {
David Benjamin87e4acd2015-04-02 19:57:35 -0400634 fprintf(stderr, "handshake completed\n");
635 // Abort before any expected error code is printed, to ensure the overall
636 // test fails.
637 abort();
638 }
639 GetTestState(ssl)->handshake_done = true;
David Benjamin25fe85b2016-08-09 20:00:32 -0400640
641 // Callbacks may be called again on a new handshake.
642 GetTestState(ssl)->ticket_decrypt_done = false;
643 GetTestState(ssl)->alpn_select_done = false;
David Benjamin87e4acd2015-04-02 19:57:35 -0400644 }
645}
646
David Benjaminba4594a2015-06-18 18:36:15 -0400647static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
648 GetTestState(ssl)->got_new_session = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400649 GetTestState(ssl)->new_session.reset(session);
David Benjaminba4594a2015-06-18 18:36:15 -0400650 return 1;
651}
652
David Benjamind98452d2015-06-16 14:16:23 -0400653static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
654 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
655 int encrypt) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400656 if (!encrypt) {
657 if (GetTestState(ssl)->ticket_decrypt_done) {
658 fprintf(stderr, "TicketKeyCallback called after completion.\n");
659 return -1;
660 }
661
662 GetTestState(ssl)->ticket_decrypt_done = true;
663 }
664
David Benjamind98452d2015-06-16 14:16:23 -0400665 // This is just test code, so use the all-zeros key.
666 static const uint8_t kZeros[16] = {0};
667
668 if (encrypt) {
669 memcpy(key_name, kZeros, sizeof(kZeros));
670 RAND_bytes(iv, 16);
671 } else if (memcmp(key_name, kZeros, 16) != 0) {
672 return 0;
673 }
674
675 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
676 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
677 return -1;
678 }
679
680 if (!encrypt) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400681 return GetTestConfig(ssl)->renew_ticket ? 2 : 1;
David Benjamind98452d2015-06-16 14:16:23 -0400682 }
683 return 1;
684}
685
Adam Langley09505632015-07-30 18:10:13 -0700686// kCustomExtensionValue is the extension value that the custom extension
687// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700688static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700689static void *const kCustomExtensionAddArg =
690 reinterpret_cast<void *>(kCustomExtensionValue);
691static void *const kCustomExtensionParseArg =
692 reinterpret_cast<void *>(kCustomExtensionValue + 1);
693static const char kCustomExtensionContents[] = "custom extension";
694
695static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
696 const uint8_t **out, size_t *out_len,
697 int *out_alert_value, void *add_arg) {
698 if (extension_value != kCustomExtensionValue ||
699 add_arg != kCustomExtensionAddArg) {
700 abort();
701 }
702
David Benjamin7e7a82d2016-05-20 20:12:42 -0400703 if (GetTestConfig(ssl)->custom_extension_skip) {
Adam Langley09505632015-07-30 18:10:13 -0700704 return 0;
705 }
David Benjamin7e7a82d2016-05-20 20:12:42 -0400706 if (GetTestConfig(ssl)->custom_extension_fail_add) {
Adam Langley09505632015-07-30 18:10:13 -0700707 return -1;
708 }
709
710 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
711 *out_len = sizeof(kCustomExtensionContents) - 1;
712
713 return 1;
714}
715
716static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
717 const uint8_t *out, void *add_arg) {
718 if (extension_value != kCustomExtensionValue ||
719 add_arg != kCustomExtensionAddArg ||
720 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
721 abort();
722 }
723}
724
725static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
726 const uint8_t *contents,
727 size_t contents_len,
728 int *out_alert_value, void *parse_arg) {
729 if (extension_value != kCustomExtensionValue ||
730 parse_arg != kCustomExtensionParseArg) {
731 abort();
732 }
733
734 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
735 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
736 *out_alert_value = SSL_AD_DECODE_ERROR;
737 return 0;
738 }
739
740 return 1;
741}
742
David Benjamin87c8a642015-02-21 01:54:29 -0500743// Connect returns a new socket connected to localhost on |port| or -1 on
744// error.
745static int Connect(uint16_t port) {
746 int sock = socket(AF_INET, SOCK_STREAM, 0);
747 if (sock == -1) {
748 PrintSocketError("socket");
749 return -1;
750 }
751 int nodelay = 1;
752 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
753 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
754 PrintSocketError("setsockopt");
755 closesocket(sock);
756 return -1;
757 }
758 sockaddr_in sin;
759 memset(&sin, 0, sizeof(sin));
760 sin.sin_family = AF_INET;
761 sin.sin_port = htons(port);
762 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
763 PrintSocketError("inet_pton");
764 closesocket(sock);
765 return -1;
766 }
767 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
768 sizeof(sin)) != 0) {
769 PrintSocketError("connect");
770 closesocket(sock);
771 return -1;
772 }
773 return sock;
774}
775
776class SocketCloser {
777 public:
778 explicit SocketCloser(int sock) : sock_(sock) {}
779 ~SocketCloser() {
780 // Half-close and drain the socket before releasing it. This seems to be
781 // necessary for graceful shutdown on Windows. It will also avoid write
782 // failures in the test runner.
783#if defined(OPENSSL_WINDOWS)
784 shutdown(sock_, SD_SEND);
785#else
786 shutdown(sock_, SHUT_WR);
787#endif
788 while (true) {
789 char buf[1024];
790 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
791 break;
792 }
793 }
794 closesocket(sock_);
795 }
796
797 private:
798 const int sock_;
799};
800
David Benjaminc273d2c2015-02-09 12:59:46 -0500801static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500802 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
803 config->is_dtls ? DTLS_method() : TLS_method()));
804 if (!ssl_ctx) {
805 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400806 }
807
Nick Harper1fd39d82016-06-14 18:14:35 -0700808 if (!config->is_dtls) {
809 // Enable TLS 1.3 for tests.
810 SSL_CTX_set_max_version(ssl_ctx.get(), TLS1_3_VERSION);
811 }
812
Adam Langleycef75832015-09-03 14:51:12 -0700813 std::string cipher_list = "ALL";
814 if (!config->cipher.empty()) {
815 cipher_list = config->cipher;
816 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
817 }
818 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
819 return nullptr;
820 }
821
822 if (!config->cipher_tls10.empty() &&
823 !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(),
824 config->cipher_tls10.c_str())) {
825 return nullptr;
826 }
827 if (!config->cipher_tls11.empty() &&
828 !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(),
829 config->cipher_tls11.c_str())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500830 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400831 }
832
David Benjamina7f333d2015-02-09 02:37:18 -0500833 ScopedDH dh(DH_get_2048_256(NULL));
David Benjaminb7c5e842016-03-28 09:59:10 -0400834 if (!dh) {
835 return nullptr;
836 }
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800837
838 if (config->use_sparse_dh_prime) {
839 // This prime number is 2^1024 + 643 – a value just above a power of two.
840 // Because of its form, values modulo it are essentially certain to be one
841 // byte shorter. This is used to test padding of these values.
842 if (BN_hex2bn(
843 &dh->p,
844 "1000000000000000000000000000000000000000000000000000000000000000"
845 "0000000000000000000000000000000000000000000000000000000000000000"
846 "0000000000000000000000000000000000000000000000000000000000000000"
847 "0000000000000000000000000000000000000000000000000000000000000028"
848 "3") == 0 ||
849 !BN_set_word(dh->g, 2)) {
850 return nullptr;
851 }
David Benjamine66148a2016-02-02 14:14:36 -0500852 BN_free(dh->q);
853 dh->q = NULL;
Adam Langleyc4f25ce2015-11-26 16:39:08 -0800854 dh->priv_length = 0;
855 }
856
David Benjaminb7c5e842016-03-28 09:59:10 -0400857 if (!SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
David Benjamina7f333d2015-02-09 02:37:18 -0500858 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400859 }
860
David Benjamin1b8b6912015-02-09 04:28:16 -0500861 if (config->async && config->is_server) {
862 // Disable the internal session cache. To test asynchronous session lookup,
863 // we use an external session cache.
864 SSL_CTX_set_session_cache_mode(
865 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500866 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500867 } else {
868 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
869 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400870
David Benjamind4c2bce2015-10-17 12:28:18 -0400871 SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback);
David Benjamin8f2c20e2014-07-09 09:30:38 -0400872
David Benjaminacb6dcc2016-03-10 09:15:01 -0500873 if (config->use_old_client_cert_callback) {
874 SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback);
875 }
876
David Benjamin1f5f62b2014-07-12 16:18:02 -0400877 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500878 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400879 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500880 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500881 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400882 }
883
David Benjamin594e7d22016-03-17 17:49:56 -0400884 if (!config->select_alpn.empty() || config->decline_alpn) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500885 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400886 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400887
Adam Langley49c7af12015-07-10 14:33:46 -0700888 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500889 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400890
David Benjamin721e8b72016-08-03 13:13:17 -0400891 if (config->is_dtls) {
892 SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback);
893 }
David Benjamin377fc312015-01-26 00:22:12 -0500894
David Benjamin87e4acd2015-04-02 19:57:35 -0400895 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400896 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400897
David Benjamind98452d2015-06-16 14:16:23 -0400898 if (config->use_ticket_callback) {
899 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
900 }
901
Adam Langley09505632015-07-30 18:10:13 -0700902 if (config->enable_client_custom_extension &&
903 !SSL_CTX_add_client_custom_ext(
904 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
905 CustomExtensionFreeCallback, kCustomExtensionAddArg,
906 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
907 return nullptr;
908 }
909
910 if (config->enable_server_custom_extension &&
911 !SSL_CTX_add_server_custom_ext(
912 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
913 CustomExtensionFreeCallback, kCustomExtensionAddArg,
914 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
915 return nullptr;
916 }
917
Paul Lietar8f1c2682015-08-18 12:21:54 +0100918 if (config->verify_fail) {
919 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
920 } else {
921 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
922 }
923
Paul Lietar4fac72e2015-09-09 13:44:55 +0100924 if (!config->signed_cert_timestamps.empty() &&
925 !SSL_CTX_set_signed_cert_timestamp_list(
926 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
927 config->signed_cert_timestamps.size())) {
928 return nullptr;
929 }
930
David Benjamin2f8935d2016-07-13 19:47:39 -0400931 if (config->use_null_client_ca_list) {
932 SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr);
933 }
934
David Benjamin1d5c83e2014-07-22 19:20:02 -0400935 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400936}
937
David Benjamin40f101b2015-02-20 11:23:42 -0500938// RetryAsync is called after a failed operation on |ssl| with return code
939// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400940// event and returns true. Otherwise it returns false.
941static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400942 // No error; don't retry.
943 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500944 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400945 }
David Benjamin83f90402015-01-27 01:09:43 -0500946
David Benjamin6c2563e2015-04-03 03:47:47 -0400947 TestState *test_state = GetTestState(ssl);
Matt Braithwaite6278e242016-06-14 08:18:22 -0700948 assert(GetTestConfig(ssl)->async);
David Benjamin83f90402015-01-27 01:09:43 -0500949
David Benjamin585d7a42016-06-02 14:58:00 -0400950 if (test_state->packeted_bio != nullptr &&
951 PacketedBioAdvanceClock(test_state->packeted_bio)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500952 // The DTLS retransmit logic silently ignores write failures. So the test
953 // may progress, allow writes through synchronously.
David Benjamin585d7a42016-06-02 14:58:00 -0400954 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
David Benjamin13e81fc2015-11-02 17:16:13 -0500955 int timeout_ret = DTLSv1_handle_timeout(ssl);
David Benjamin585d7a42016-06-02 14:58:00 -0400956 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
David Benjamin13e81fc2015-11-02 17:16:13 -0500957
958 if (timeout_ret < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400959 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500960 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500961 }
David Benjamin40f101b2015-02-20 11:23:42 -0500962 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500963 }
964
David Benjamin43ec06f2014-08-05 02:28:57 -0400965 // See if we needed to read or write more. If so, allow one byte through on
966 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500967 switch (SSL_get_error(ssl, ret)) {
968 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400969 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500970 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500971 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400972 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500973 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500974 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400975 ScopedEVP_PKEY pkey = LoadPrivateKey(GetTestConfig(ssl)->send_channel_id);
David Benjamin9d0847a2015-02-16 03:57:55 -0500976 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500977 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500978 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400979 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500980 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500981 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500982 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400983 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500984 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500985 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400986 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500987 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500988 case SSL_ERROR_PENDING_CERTIFICATE:
989 // The handshake will resume without a second call to the early callback.
990 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400991 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700992 test_state->private_key_retries++;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400993 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500994 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500995 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400996 }
David Benjamin025b3d32014-07-01 19:53:04 -0400997}
998
David Benjamin6c2563e2015-04-03 03:47:47 -0400999// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
1000// the result value of the final |SSL_read| call.
1001static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001002 const TestConfig *config = GetTestConfig(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -05001003 TestState *test_state = GetTestState(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001004 int ret;
1005 do {
David Benjamin13e81fc2015-11-02 17:16:13 -05001006 if (config->async) {
1007 // The DTLS retransmit logic silently ignores write failures. So the test
1008 // may progress, allow writes through synchronously. |SSL_read| may
1009 // trigger a retransmit, so disconnect the write quota.
1010 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
1011 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001012 ret = SSL_read(ssl, out, max_out);
David Benjamin13e81fc2015-11-02 17:16:13 -05001013 if (config->async) {
1014 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
1015 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001016 } while (config->async && RetryAsync(ssl, ret));
1017 return ret;
1018}
1019
1020// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
1021// operations. It returns the result of the final |SSL_write| call.
1022static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001023 const TestConfig *config = GetTestConfig(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001024 int ret;
1025 do {
1026 ret = SSL_write(ssl, in, in_len);
1027 if (ret > 0) {
1028 in += ret;
1029 in_len -= ret;
1030 }
1031 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
1032 return ret;
1033}
1034
David Benjamin30789da2015-08-29 22:56:45 -04001035// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
1036// returns the result of the final |SSL_shutdown| call.
1037static int DoShutdown(SSL *ssl) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001038 const TestConfig *config = GetTestConfig(ssl);
David Benjamin30789da2015-08-29 22:56:45 -04001039 int ret;
1040 do {
1041 ret = SSL_shutdown(ssl);
1042 } while (config->async && RetryAsync(ssl, ret));
1043 return ret;
1044}
1045
David Benjamin1d4f4c02016-07-26 18:03:08 -04001046// DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous
1047// operations. It returns the result of the final |SSL_send_fatal_alert| call.
1048static int DoSendFatalAlert(SSL *ssl, uint8_t alert) {
1049 const TestConfig *config = GetTestConfig(ssl);
1050 int ret;
1051 do {
1052 ret = SSL_send_fatal_alert(ssl, alert);
1053 } while (config->async && RetryAsync(ssl, ret));
1054 return ret;
1055}
1056
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001057static uint16_t GetProtocolVersion(const SSL *ssl) {
1058 uint16_t version = SSL_version(ssl);
1059 if (!SSL_is_dtls(ssl)) {
1060 return version;
1061 }
1062 return 0x0201 + ~version;
1063}
1064
David Benjamin91eab5c2015-06-18 18:35:46 -04001065// CheckHandshakeProperties checks, immediately after |ssl| completes its
1066// initial handshake (or False Starts), whether all the properties are
1067// consistent with the test configuration and invariants.
1068static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001069 const TestConfig *config = GetTestConfig(ssl);
David Benjamin91eab5c2015-06-18 18:35:46 -04001070
1071 if (SSL_get_current_cipher(ssl) == nullptr) {
1072 fprintf(stderr, "null cipher after handshake\n");
1073 return false;
1074 }
1075
1076 if (is_resume &&
1077 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
1078 fprintf(stderr, "session was%s reused\n",
1079 SSL_session_reused(ssl) ? "" : " not");
1080 return false;
1081 }
1082
1083 bool expect_handshake_done = is_resume || !config->false_start;
1084 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
1085 fprintf(stderr, "handshake was%s completed\n",
1086 GetTestState(ssl)->handshake_done ? "" : " not");
1087 return false;
1088 }
1089
David Benjaminba4594a2015-06-18 18:36:15 -04001090 if (expect_handshake_done && !config->is_server) {
1091 bool expect_new_session =
1092 !config->expect_no_session &&
Steven Valdez143e8b32016-07-11 13:19:03 -04001093 (!SSL_session_reused(ssl) || config->expect_ticket_renewal) &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001094 // Session tickets are sent post-handshake in TLS 1.3.
1095 GetProtocolVersion(ssl) < TLS1_3_VERSION;
David Benjaminba4594a2015-06-18 18:36:15 -04001096 if (expect_new_session != GetTestState(ssl)->got_new_session) {
1097 fprintf(stderr,
David Benjamindd6fed92015-10-23 17:41:12 -04001098 "new session was%s cached, but we expected the opposite\n",
David Benjaminba4594a2015-06-18 18:36:15 -04001099 GetTestState(ssl)->got_new_session ? "" : " not");
1100 return false;
1101 }
1102 }
1103
David Benjamin91eab5c2015-06-18 18:35:46 -04001104 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
1105 fprintf(stderr, "early callback not called\n");
1106 return false;
1107 }
1108
1109 if (!config->expected_server_name.empty()) {
1110 const char *server_name =
1111 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
1112 if (server_name != config->expected_server_name) {
1113 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
1114 server_name, config->expected_server_name.c_str());
1115 return false;
1116 }
1117 }
1118
1119 if (!config->expected_certificate_types.empty()) {
David Benjamin75910642015-08-09 10:42:33 -04001120 const uint8_t *certificate_types;
1121 size_t certificate_types_len =
David Benjamin91eab5c2015-06-18 18:35:46 -04001122 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin75910642015-08-09 10:42:33 -04001123 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin91eab5c2015-06-18 18:35:46 -04001124 memcmp(certificate_types,
1125 config->expected_certificate_types.data(),
David Benjamin75910642015-08-09 10:42:33 -04001126 certificate_types_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001127 fprintf(stderr, "certificate types mismatch\n");
1128 return false;
1129 }
1130 }
1131
1132 if (!config->expected_next_proto.empty()) {
1133 const uint8_t *next_proto;
1134 unsigned next_proto_len;
1135 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
1136 if (next_proto_len != config->expected_next_proto.size() ||
1137 memcmp(next_proto, config->expected_next_proto.data(),
1138 next_proto_len) != 0) {
1139 fprintf(stderr, "negotiated next proto mismatch\n");
1140 return false;
1141 }
1142 }
1143
1144 if (!config->expected_alpn.empty()) {
1145 const uint8_t *alpn_proto;
1146 unsigned alpn_proto_len;
1147 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
1148 if (alpn_proto_len != config->expected_alpn.size() ||
1149 memcmp(alpn_proto, config->expected_alpn.data(),
1150 alpn_proto_len) != 0) {
1151 fprintf(stderr, "negotiated alpn proto mismatch\n");
1152 return false;
1153 }
1154 }
1155
1156 if (!config->expected_channel_id.empty()) {
1157 uint8_t channel_id[64];
1158 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
1159 fprintf(stderr, "no channel id negotiated\n");
1160 return false;
1161 }
1162 if (config->expected_channel_id.size() != 64 ||
1163 memcmp(config->expected_channel_id.data(),
1164 channel_id, 64) != 0) {
1165 fprintf(stderr, "channel id mismatch\n");
1166 return false;
1167 }
1168 }
1169
1170 if (config->expect_extended_master_secret) {
David Benjamin8ac35712016-07-13 21:07:29 -04001171 if (!SSL_get_extms_support(ssl)) {
1172 fprintf(stderr, "No EMS for connection when expected");
David Benjamin91eab5c2015-06-18 18:35:46 -04001173 return false;
1174 }
1175 }
1176
1177 if (!config->expected_ocsp_response.empty()) {
1178 const uint8_t *data;
1179 size_t len;
1180 SSL_get0_ocsp_response(ssl, &data, &len);
1181 if (config->expected_ocsp_response.size() != len ||
1182 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
1183 fprintf(stderr, "OCSP response mismatch\n");
1184 return false;
1185 }
1186 }
1187
1188 if (!config->expected_signed_cert_timestamps.empty()) {
1189 const uint8_t *data;
1190 size_t len;
1191 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
1192 if (config->expected_signed_cert_timestamps.size() != len ||
1193 memcmp(config->expected_signed_cert_timestamps.data(),
1194 data, len) != 0) {
1195 fprintf(stderr, "SCT list mismatch\n");
1196 return false;
1197 }
1198 }
1199
Paul Lietar8f1c2682015-08-18 12:21:54 +01001200 if (config->expect_verify_result) {
1201 int expected_verify_result = config->verify_fail ?
1202 X509_V_ERR_APPLICATION_VERIFICATION :
1203 X509_V_OK;
1204
1205 if (SSL_get_verify_result(ssl) != expected_verify_result) {
1206 fprintf(stderr, "Wrong certificate verification result\n");
1207 return false;
1208 }
1209 }
1210
Nick Harper60edffd2016-06-21 15:19:24 -07001211 if (config->expect_peer_signature_algorithm != 0 &&
1212 config->expect_peer_signature_algorithm !=
1213 SSL_get_peer_signature_algorithm(ssl)) {
1214 fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n",
1215 SSL_get_peer_signature_algorithm(ssl),
1216 config->expect_peer_signature_algorithm);
David Benjamin6e807652015-11-02 12:02:20 -05001217 return false;
1218 }
1219
David Benjamin9e68f192016-06-30 14:55:33 -04001220 if (config->expect_curve_id != 0) {
1221 uint16_t curve_id = SSL_get_curve_id(ssl);
1222 if (static_cast<uint16_t>(config->expect_curve_id) != curve_id) {
1223 fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id,
1224 static_cast<uint16_t>(config->expect_curve_id));
1225 return false;
1226 }
1227 }
1228
1229 if (config->expect_dhe_group_size != 0) {
1230 unsigned dhe_group_size = SSL_get_dhe_group_size(ssl);
1231 if (static_cast<unsigned>(config->expect_dhe_group_size) !=
1232 dhe_group_size) {
1233 fprintf(stderr, "dhe_group_size was %u, wanted %d\n", dhe_group_size,
1234 config->expect_dhe_group_size);
David Benjamin4cc36ad2015-12-19 14:23:26 -05001235 return false;
1236 }
1237 }
1238
David Benjaminbb9e36e2016-08-03 14:14:47 -04001239 if (!config->psk.empty()) {
1240 if (SSL_get_peer_cert_chain(ssl) != nullptr) {
1241 fprintf(stderr, "Received peer certificate on a PSK cipher.\n");
1242 return false;
1243 }
1244 } else if (!config->is_server || config->require_any_client_certificate) {
1245 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
1246 fprintf(stderr, "Received no peer certificate but expected one.\n");
David Benjamin91eab5c2015-06-18 18:35:46 -04001247 return false;
1248 }
1249 }
David Benjaminbb9e36e2016-08-03 14:14:47 -04001250
David Benjamin91eab5c2015-06-18 18:35:46 -04001251 return true;
1252}
1253
David Benjamin87c8a642015-02-21 01:54:29 -05001254// DoExchange runs a test SSL exchange against the peer. On success, it returns
1255// true and sets |*out_session| to the negotiated SSL session. If the test is a
1256// resumption attempt, |is_resume| is true and |session| is the session from the
1257// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -05001258static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
1259 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -05001260 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001261 ScopedSSL ssl(SSL_new(ssl_ctx));
1262 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001263 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001264 }
1265
David Benjamin7e7a82d2016-05-20 20:12:42 -04001266 if (!SetTestConfig(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001267 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001268 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001269 }
David Benjamin5a593af2014-08-11 19:51:50 -04001270
Adam Langley5f0efe02015-02-20 13:03:16 -08001271 if (config->fallback_scsv &&
1272 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1273 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001274 }
David Benjaminacb6dcc2016-03-10 09:15:01 -05001275 if (!config->use_early_callback && !config->use_old_client_cert_callback) {
David Benjamin6f5c0f42015-02-24 01:23:21 -05001276 if (config->async) {
David Benjamin6f5c0f42015-02-24 01:23:21 -05001277 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
1278 } else if (!InstallCertificate(ssl.get())) {
1279 return false;
1280 }
David Benjamin5a593af2014-08-11 19:51:50 -04001281 }
1282 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001283 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001284 NULL);
1285 }
1286 if (config->verify_peer) {
1287 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001288 }
1289 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001290 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001291 }
1292 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001293 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001294 }
1295 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001296 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001297 }
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001298 if (config->no_tls13) {
1299 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3);
1300 }
David Benjamin5a593af2014-08-11 19:51:50 -04001301 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001302 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001303 }
1304 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001305 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001306 }
1307 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001308 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001309 }
1310 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001311 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001312 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001313 if (!config->expected_channel_id.empty() ||
1314 config->enable_channel_id) {
David Benjamina7f333d2015-02-09 02:37:18 -05001315 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -04001316 }
1317 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -05001318 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -05001319 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001320 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -05001321 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
1322 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001323 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001324 }
David Benjamina08e49d2014-08-24 01:46:07 -04001325 }
David Benjamina08e49d2014-08-24 01:46:07 -04001326 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001327 if (!config->host_name.empty() &&
1328 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001329 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001330 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001331 if (!config->advertise_alpn.empty() &&
1332 SSL_set_alpn_protos(ssl.get(),
1333 (const uint8_t *)config->advertise_alpn.data(),
1334 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001335 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001336 }
David Benjamin48cae082014-10-27 01:06:24 -04001337 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001338 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1339 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001340 }
David Benjamin61f95272014-11-25 01:55:35 -05001341 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001342 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001343 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001344 }
David Benjamin61f95272014-11-25 01:55:35 -05001345 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001346 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001347 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001348 }
1349 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -05001350 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001351 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001352 }
1353 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -05001354 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001355 return false;
David Benjaminca6c8262014-11-15 19:06:08 -05001356 }
David Benjamin1eb367c2014-12-12 18:17:51 -05001357 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001358 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001359 }
1360 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001361 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001362 }
David Benjamin5e7e7cc2016-07-21 12:55:28 +02001363 if (config->fallback_version != 0) {
1364 SSL_set_fallback_version(ssl.get(), (uint16_t)config->fallback_version);
1365 }
David Benjamin13be1de2015-01-11 16:29:36 -05001366 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001367 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1368 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001369 }
Adam Langley524e7172015-02-20 16:04:00 -08001370 if (config->install_ddos_callback) {
1371 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1372 }
David Benjamin1d5ef3b2015-10-12 19:54:18 -04001373 if (config->renegotiate_once) {
1374 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once);
1375 }
1376 if (config->renegotiate_freely) {
1377 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04001378 }
Adam Langley27a0d082015-11-03 13:34:10 -08001379 if (config->renegotiate_ignore) {
1380 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore);
1381 }
David Benjamin30789da2015-08-29 22:56:45 -04001382 if (!config->check_close_notify) {
1383 SSL_set_quiet_shutdown(ssl.get(), 1);
1384 }
David Benjamin091c4b92015-10-26 13:33:21 -04001385 if (config->disable_npn) {
1386 SSL_set_options(ssl.get(), SSL_OP_DISABLE_NPN);
1387 }
David Benjamin99fdfb92015-11-02 12:11:35 -05001388 if (config->p384_only) {
1389 int nid = NID_secp384r1;
1390 if (!SSL_set1_curves(ssl.get(), &nid, 1)) {
1391 return false;
1392 }
1393 }
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001394 if (config->enable_all_curves) {
1395 static const int kAllCurves[] = {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001396 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519,
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001397 };
1398 if (!SSL_set1_curves(ssl.get(), kAllCurves,
1399 sizeof(kAllCurves) / sizeof(kAllCurves[0]))) {
1400 return false;
1401 }
1402 }
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -07001403 if (config->initial_timeout_duration_ms > 0) {
1404 DTLSv1_set_initial_timeout_duration(ssl.get(),
1405 config->initial_timeout_duration_ms);
1406 }
David Benjamin025b3d32014-07-01 19:53:04 -04001407
David Benjamin87c8a642015-02-21 01:54:29 -05001408 int sock = Connect(config->port);
1409 if (sock == -1) {
1410 return false;
1411 }
1412 SocketCloser closer(sock);
1413
1414 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001415 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001416 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001417 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001418 if (config->is_dtls) {
David Benjamin585d7a42016-06-02 14:58:00 -04001419 ScopedBIO packeted = PacketedBioCreate(!config->async);
David Benjaminb7c5e842016-03-28 09:59:10 -04001420 if (!packeted) {
1421 return false;
1422 }
David Benjamin585d7a42016-06-02 14:58:00 -04001423 GetTestState(ssl.get())->packeted_bio = packeted.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001424 BIO_push(packeted.get(), bio.release());
1425 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001426 }
David Benjamin5a593af2014-08-11 19:51:50 -04001427 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001428 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001429 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjaminb7c5e842016-03-28 09:59:10 -04001430 if (!async_scoped) {
1431 return false;
1432 }
David Benjamina7f333d2015-02-09 02:37:18 -05001433 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001434 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001435 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001436 }
David Benjamina7f333d2015-02-09 02:37:18 -05001437 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1438 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001439
David Benjamin1d5c83e2014-07-22 19:20:02 -04001440 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001441 if (!config->is_server) {
1442 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001443 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001444 }
1445 } else if (config->async) {
1446 // The internal session cache is disabled, so install the session
1447 // manually.
David Benjaminb9195402016-08-05 10:51:43 -04001448 SSL_SESSION_up_ref(session);
1449 GetTestState(ssl.get())->pending_session.reset(session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001450 }
1451 }
1452
David Benjamina07c0fc2015-05-13 13:19:42 -04001453 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1454 fprintf(stderr, "non-null cipher before handshake\n");
1455 return false;
1456 }
1457
David Benjamin1d5c83e2014-07-22 19:20:02 -04001458 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001459 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001460 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001461 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001462 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001463 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001464 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001465 } else {
1466 do {
1467 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001468 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001469 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001470 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001471 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001472 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001473 if (ret != 1 ||
1474 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001475 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001476 }
David Benjamin025b3d32014-07-01 19:53:04 -04001477
David Benjaminba4594a2015-06-18 18:36:15 -04001478 // Reset the state to assert later that the callback isn't called in
1479 // renegotations.
1480 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001481 }
1482
David Benjaminc565ebb2015-04-03 04:06:36 -04001483 if (config->export_keying_material > 0) {
1484 std::vector<uint8_t> result(
1485 static_cast<size_t>(config->export_keying_material));
1486 if (!SSL_export_keying_material(
1487 ssl.get(), result.data(), result.size(),
1488 config->export_label.data(), config->export_label.size(),
1489 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1490 config->export_context.size(), config->use_export_context)) {
1491 fprintf(stderr, "failed to export keying material\n");
1492 return false;
1493 }
1494 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1495 return false;
1496 }
1497 }
1498
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001499 if (config->tls_unique) {
1500 uint8_t tls_unique[16];
1501 size_t tls_unique_len;
1502 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1503 sizeof(tls_unique))) {
1504 fprintf(stderr, "failed to get tls-unique\n");
1505 return false;
1506 }
1507
1508 if (tls_unique_len != 12) {
1509 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1510 static_cast<unsigned>(tls_unique_len));
1511 return false;
1512 }
1513
1514 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1515 return false;
1516 }
1517 }
1518
David Benjamin1d4f4c02016-07-26 18:03:08 -04001519 if (config->send_alert) {
1520 if (DoSendFatalAlert(ssl.get(), SSL_AD_DECOMPRESSION_FAILURE) < 0) {
1521 return false;
1522 }
1523 return true;
1524 }
1525
David Benjamin5a593af2014-08-11 19:51:50 -04001526 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001527 if (config->is_dtls) {
1528 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001529 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001530 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001531 // This mode writes a number of different record sizes in an attempt to
1532 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001533 static const size_t kBufLen = 32769;
1534 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1535 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001536 static const size_t kRecordSizes[] = {
1537 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1538 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1539 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001540 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001541 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001542 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001543 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001544 }
Adam Langleybc949292015-06-18 21:32:44 -07001545 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001546 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001547 }
1548 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001549 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001550 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001551 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1552 5) < 0) {
1553 return false;
1554 }
David Benjamine58c4f52014-08-24 03:47:07 -04001555 }
David Benjamin30789da2015-08-29 22:56:45 -04001556 if (!config->shim_shuts_down) {
1557 for (;;) {
Adam Langleya0a8dc22015-09-09 10:22:00 -07001558 static const size_t kBufLen = 16384;
1559 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1560
David Benjamin2c99d282015-09-01 10:23:00 -04001561 // Read only 512 bytes at a time in TLS to ensure records may be
1562 // returned in multiple reads.
Adam Langleya0a8dc22015-09-09 10:22:00 -07001563 int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
David Benjamin30789da2015-08-29 22:56:45 -04001564 int err = SSL_get_error(ssl.get(), n);
1565 if (err == SSL_ERROR_ZERO_RETURN ||
1566 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1567 if (n != 0) {
1568 fprintf(stderr, "Invalid SSL_get_error output\n");
1569 return false;
1570 }
1571 // Stop on either clean or unclean shutdown.
1572 break;
1573 } else if (err != SSL_ERROR_NONE) {
1574 if (n > 0) {
1575 fprintf(stderr, "Invalid SSL_get_error output\n");
1576 return false;
1577 }
1578 return false;
1579 }
1580 // Successfully read data.
1581 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001582 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001583 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001584 }
David Benjamin30789da2015-08-29 22:56:45 -04001585
1586 // After a successful read, with or without False Start, the handshake
1587 // must be complete.
1588 if (!GetTestState(ssl.get())->handshake_done) {
1589 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001590 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001591 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001592
David Benjamin30789da2015-08-29 22:56:45 -04001593 for (int i = 0; i < n; i++) {
1594 buf[i] ^= 0xff;
1595 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001596 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001597 return false;
1598 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001599 }
1600 }
David Benjamin025b3d32014-07-01 19:53:04 -04001601 }
1602
David Benjaminba4594a2015-06-18 18:36:15 -04001603 if (!config->is_server && !config->false_start &&
1604 !config->implicit_handshake &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001605 // Session tickets are sent post-handshake in TLS 1.3.
1606 GetProtocolVersion(ssl.get()) < TLS1_3_VERSION &&
David Benjaminba4594a2015-06-18 18:36:15 -04001607 GetTestState(ssl.get())->got_new_session) {
1608 fprintf(stderr, "new session was established after the handshake\n");
1609 return false;
1610 }
1611
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001612 if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) {
1613 bool expect_new_session =
1614 !config->expect_no_session && !config->shim_shuts_down;
1615 if (expect_new_session != GetTestState(ssl.get())->got_new_session) {
1616 fprintf(stderr,
1617 "new session was%s cached, but we expected the opposite\n",
1618 GetTestState(ssl.get())->got_new_session ? "" : " not");
1619 return false;
1620 }
1621 }
1622
David Benjamin1d5c83e2014-07-22 19:20:02 -04001623 if (out_session) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04001624 *out_session = std::move(GetTestState(ssl.get())->new_session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001625 }
1626
David Benjamin30789da2015-08-29 22:56:45 -04001627 ret = DoShutdown(ssl.get());
1628
1629 if (config->shim_shuts_down && config->check_close_notify) {
1630 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1631 // it returns zero when our close_notify is sent, then one when the peer's
1632 // is received.
1633 if (ret != 0) {
1634 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1635 return false;
1636 }
1637 ret = DoShutdown(ssl.get());
1638 }
1639
1640 if (ret != 1) {
1641 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1642 return false;
1643 }
1644
David Benjamin324dce42015-10-12 19:49:00 -04001645 if (SSL_total_renegotiations(ssl.get()) !=
1646 config->expect_total_renegotiations) {
1647 fprintf(stderr, "Expected %d renegotiations, got %d\n",
1648 config->expect_total_renegotiations,
1649 SSL_total_renegotiations(ssl.get()));
1650 return false;
1651 }
1652
David Benjamin40f101b2015-02-20 11:23:42 -05001653 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001654}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001655
David Benjaminff3a1492016-03-02 10:12:06 -05001656class StderrDelimiter {
1657 public:
1658 ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
1659};
1660
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -07001661static int Main(int argc, char **argv) {
David Benjaminff3a1492016-03-02 10:12:06 -05001662 // To distinguish ASan's output from ours, add a trailing message to stderr.
1663 // Anything following this line will be considered an error.
1664 StderrDelimiter delimiter;
1665
David Benjamin87c8a642015-02-21 01:54:29 -05001666#if defined(OPENSSL_WINDOWS)
1667 /* Initialize Winsock. */
1668 WORD wsa_version = MAKEWORD(2, 2);
1669 WSADATA wsa_data;
1670 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1671 if (wsa_err != 0) {
1672 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1673 return 1;
1674 }
1675 if (wsa_data.wVersion != wsa_version) {
1676 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1677 return 1;
1678 }
1679#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001680 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001681#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001682
David Benjamin7a1eefd2015-10-17 23:39:22 -04001683 CRYPTO_library_init();
David Benjamind9e07012015-02-09 03:04:34 -05001684 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001685 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001686 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001687 return 1;
1688 }
David Benjamin5a593af2014-08-11 19:51:50 -04001689
1690 TestConfig config;
1691 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001692 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001693 }
1694
David Benjaminc273d2c2015-02-09 12:59:46 -05001695 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001696 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001697 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001698 return 1;
1699 }
1700
David Benjamina7f333d2015-02-09 02:37:18 -05001701 ScopedSSL_SESSION session;
David Benjamin46662482016-08-17 00:51:00 -04001702 for (int i = 0; i < config.resume_count + 1; i++) {
1703 bool is_resume = i > 0;
1704 if (is_resume && !config.is_server && !session) {
1705 fprintf(stderr, "No session to offer.\n");
1706 return 1;
1707 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001708
David Benjamin46662482016-08-17 00:51:00 -04001709 ScopedSSL_SESSION offer_session = std::move(session);
1710 if (!DoExchange(&session, ssl_ctx.get(), &config, is_resume,
1711 offer_session.get())) {
1712 fprintf(stderr, "Connection %d failed.\n", i + 1);
1713 ERR_print_errors_fp(stderr);
1714 return 1;
1715 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001716 }
1717
David Benjamina7f333d2015-02-09 02:37:18 -05001718 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001719}
Martin Kreichgauer19d5cf82016-08-09 17:48:22 -07001720
1721} // namespace bssl
1722
1723int main(int argc, char **argv) {
1724 return bssl::Main(argc, argv);
1725}