blob: 02b7558edb7482eb3c954bad18333ca0d6143c97 [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
David Benjamin4fec04b2016-10-10 14:56:47 -040036OPENSSL_MSVC_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 Benjaminabbbee12016-10-31 19:20:42 -040043#include <openssl/aead.h>
David Benjamin025b3d32014-07-01 19:53:04 -040044#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040045#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040046#include <openssl/bytestring.h>
David Benjamind98452d2015-06-16 14:16:23 -040047#include <openssl/cipher.h>
David Benjamin7a1eefd2015-10-17 23:39:22 -040048#include <openssl/crypto.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070049#include <openssl/dh.h>
David Benjaminf0e935d2016-09-06 18:10:19 -040050#include <openssl/digest.h>
Brian Smith83a82982015-04-09 16:21:10 -100051#include <openssl/err.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070052#include <openssl/evp.h>
David Benjamind98452d2015-06-16 14:16:23 -040053#include <openssl/hmac.h>
David Benjamin98193672016-03-25 18:07:11 -040054#include <openssl/nid.h>
David Benjamind98452d2015-06-16 14:16:23 -040055#include <openssl/rand.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040056#include <openssl/ssl.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070057#include <openssl/x509.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040058
David Benjamin45fb1be2015-03-22 16:31:27 -040059#include <memory>
Steven Valdez0d62f262015-09-04 12:41:04 -040060#include <string>
David Benjaminc565ebb2015-04-03 04:06:36 -040061#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040062
Steven Valdezcb966542016-08-17 16:56:14 -040063#include "../../crypto/internal.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040064#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040065#include "packeted_bio.h"
David Benjamin5a593af2014-08-11 19:51:50 -040066#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040067
David Benjamin87c8a642015-02-21 01:54:29 -050068
Adam Langleyd519bf62016-12-12 11:16:44 -080069static CRYPTO_BUFFER_POOL *g_pool = nullptr;
70
David Benjamin87c8a642015-02-21 01:54:29 -050071#if !defined(OPENSSL_WINDOWS)
72static int closesocket(int sock) {
73 return close(sock);
74}
75
76static void PrintSocketError(const char *func) {
77 perror(func);
78}
79#else
80static void PrintSocketError(const char *func) {
81 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
82}
83#endif
84
David Benjaminc273d2c2015-02-09 12:59:46 -050085static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050086 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040087 return 1;
88}
David Benjamin025b3d32014-07-01 19:53:04 -040089
David Benjamin2d445c02015-02-09 13:03:50 -050090struct TestState {
David Benjamin6c2563e2015-04-03 03:47:47 -040091 // async_bio is async BIO which pauses reads and writes.
92 BIO *async_bio = nullptr;
David Benjamin585d7a42016-06-02 14:58:00 -040093 // packeted_bio is the packeted BIO which simulates read timeouts.
94 BIO *packeted_bio = nullptr;
Matt Braithwaited17d74d2016-08-17 20:10:28 -070095 bssl::UniquePtr<EVP_PKEY> channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040096 bool cert_ready = false;
Matt Braithwaited17d74d2016-08-17 20:10:28 -070097 bssl::UniquePtr<SSL_SESSION> session;
98 bssl::UniquePtr<SSL_SESSION> pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040099 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -0400100 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400101 // private_key is the underlying private key used when testing custom keys.
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700102 bssl::UniquePtr<EVP_PKEY> private_key;
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700103 std::vector<uint8_t> private_key_result;
104 // private_key_retries is the number of times an asynchronous private key
105 // operation has been retried.
106 unsigned private_key_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400107 bool got_new_session = false;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700108 bssl::UniquePtr<SSL_SESSION> new_session;
David Benjamin25fe85b2016-08-09 20:00:32 -0400109 bool ticket_decrypt_done = false;
110 bool alpn_select_done = false;
Alessandro Ghedini958346a2016-12-20 19:42:15 +0000111 bool early_callback_ready = false;
David Benjamind9e07012015-02-09 03:04:34 -0500112};
113
David Benjamin2d445c02015-02-09 13:03:50 -0500114static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700115 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500116 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500117}
118
119static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500120static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400121
David Benjamin7e7a82d2016-05-20 20:12:42 -0400122static bool SetTestConfig(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500123 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400124}
125
David Benjamin7e7a82d2016-05-20 20:12:42 -0400126static const TestConfig *GetTestConfig(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500127 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400128}
129
David Benjamin22050932015-11-23 13:44:48 -0500130static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
131 // |SSL_set_ex_data| takes ownership of |state| only on success.
132 if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
133 state.release();
David Benjamind9e07012015-02-09 03:04:34 -0500134 return true;
135 }
136 return false;
137}
138
David Benjamin87e4acd2015-04-02 19:57:35 -0400139static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500140 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500141}
142
David Benjamin2c516452016-11-15 10:16:54 +0900143static bool LoadCertificate(bssl::UniquePtr<X509> *out_x509,
144 bssl::UniquePtr<STACK_OF(X509)> *out_chain,
145 const std::string &file) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700146 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
David Benjaminacb6dcc2016-03-10 09:15:01 -0500147 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
David Benjamin2c516452016-11-15 10:16:54 +0900148 return false;
David Benjaminacb6dcc2016-03-10 09:15:01 -0500149 }
David Benjamin2c516452016-11-15 10:16:54 +0900150
151 out_x509->reset(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
152 if (!*out_x509) {
153 return false;
154 }
155
156 out_chain->reset(sk_X509_new_null());
157 if (!*out_chain) {
158 return false;
159 }
160
161 // Keep reading the certificate chain.
162 for (;;) {
163 bssl::UniquePtr<X509> cert(
164 PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
165 if (!cert) {
166 break;
167 }
168
169 if (!sk_X509_push(out_chain->get(), cert.get())) {
170 return false;
171 }
172 cert.release(); // sk_X509_push takes ownership.
173 }
174
175 uint32_t err = ERR_peek_last_error();
176 if (ERR_GET_LIB(err) != ERR_LIB_PEM ||
177 ERR_GET_REASON(err) != PEM_R_NO_START_LINE) {
178 return false;
179}
180
181 ERR_clear_error();
182 return true;
David Benjaminacb6dcc2016-03-10 09:15:01 -0500183}
184
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700185static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) {
186 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
David Benjamina7f333d2015-02-09 02:37:18 -0500187 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
188 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400189 }
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700190 return bssl::UniquePtr<EVP_PKEY>(
191 PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400192}
193
Adam Langley2ff79332017-02-28 13:45:39 -0800194static bool FromHexDigit(uint8_t *out, char c) {
195 if ('0' <= c && c <= '9') {
196 *out = c - '0';
197 return true;
198 }
199 if ('a' <= c && c <= 'f') {
200 *out = c - 'a' + 10;
201 return true;
202 }
203 if ('A' <= c && c <= 'F') {
204 *out = c - 'A' + 10;
205 return true;
206 }
207 return false;
208}
209
210static bool HexDecode(std::string *out, const std::string &in) {
211 if ((in.size() & 1) != 0) {
212 return false;
213 }
214
215 std::unique_ptr<uint8_t[]> buf(new uint8_t[in.size() / 2]);
216 for (size_t i = 0; i < in.size() / 2; i++) {
217 uint8_t high, low;
218 if (!FromHexDigit(&high, in[i*2]) ||
219 !FromHexDigit(&low, in[i*2+1])) {
220 return false;
221 }
222 buf[i] = (high << 4) | low;
223 }
224
225 out->assign(reinterpret_cast<const char *>(buf.get()), in.size() / 2);
226 return true;
227}
228
229static std::vector<std::string> SplitParts(const std::string &in,
230 const char delim) {
231 std::vector<std::string> ret;
232 size_t start = 0;
233
234 for (size_t i = 0; i < in.size(); i++) {
235 if (in[i] == delim) {
236 ret.push_back(in.substr(start, i - start));
237 start = i + 1;
238 }
239 }
240
241 ret.push_back(in.substr(start, std::string::npos));
242 return ret;
243}
244
245static std::vector<std::string> DecodeHexStrings(
246 const std::string &hex_strings) {
247 std::vector<std::string> ret;
248 const std::vector<std::string> parts = SplitParts(hex_strings, ',');
249
250 for (const auto &part : parts) {
251 std::string binary;
252 if (!HexDecode(&binary, part)) {
253 fprintf(stderr, "Bad hex string: %s\n", part.c_str());
254 return ret;
255 }
256
257 ret.push_back(binary);
258 }
259
260 return ret;
261}
262
263static bssl::UniquePtr<STACK_OF(X509_NAME)> DecodeHexX509Names(
264 const std::string &hex_names) {
265 const std::vector<std::string> der_names = DecodeHexStrings(hex_names);
266 bssl::UniquePtr<STACK_OF(X509_NAME)> ret(sk_X509_NAME_new_null());
267
268 for (const auto &der_name : der_names) {
269 const uint8_t *const data =
270 reinterpret_cast<const uint8_t *>(der_name.data());
271 const uint8_t *derp = data;
272 bssl::UniquePtr<X509_NAME> name(
273 d2i_X509_NAME(nullptr, &derp, der_name.size()));
274 if (!name || derp != data + der_name.size()) {
275 fprintf(stderr, "Failed to parse X509_NAME.\n");
276 return nullptr;
277 }
278
279 if (!sk_X509_NAME_push(ret.get(), name.get())) {
280 return nullptr;
281 }
282 name.release();
283 }
284
285 return ret;
286}
287
David Benjaminb4d65fd2015-05-29 17:11:21 -0400288static int AsyncPrivateKeyType(SSL *ssl) {
David Benjamin0c0b7e12016-07-14 13:47:55 -0400289 EVP_PKEY *key = GetTestState(ssl)->private_key.get();
290 switch (EVP_PKEY_id(key)) {
291 case EVP_PKEY_RSA:
292 return NID_rsaEncryption;
293 case EVP_PKEY_EC:
294 return EC_GROUP_get_curve_name(
295 EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(key)));
296 default:
297 return NID_undef;
298 }
David Benjaminb4d65fd2015-05-29 17:11:21 -0400299}
300
David Benjaminb4d65fd2015-05-29 17:11:21 -0400301static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
302 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
303}
304
305static ssl_private_key_result_t AsyncPrivateKeySign(
306 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
David Benjamind3440b42016-07-14 14:52:41 -0400307 uint16_t signature_algorithm, const uint8_t *in, size_t in_len) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400308 TestState *test_state = GetTestState(ssl);
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700309 if (!test_state->private_key_result.empty()) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400310 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
311 abort();
312 }
313
David Benjamind3440b42016-07-14 14:52:41 -0400314 // Determine the hash.
315 const EVP_MD *md;
316 switch (signature_algorithm) {
317 case SSL_SIGN_RSA_PKCS1_SHA1:
318 case SSL_SIGN_ECDSA_SHA1:
319 md = EVP_sha1();
320 break;
321 case SSL_SIGN_RSA_PKCS1_SHA256:
322 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
323 case SSL_SIGN_RSA_PSS_SHA256:
324 md = EVP_sha256();
325 break;
326 case SSL_SIGN_RSA_PKCS1_SHA384:
327 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
328 case SSL_SIGN_RSA_PSS_SHA384:
329 md = EVP_sha384();
330 break;
331 case SSL_SIGN_RSA_PKCS1_SHA512:
332 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
333 case SSL_SIGN_RSA_PSS_SHA512:
334 md = EVP_sha512();
335 break;
336 case SSL_SIGN_RSA_PKCS1_MD5_SHA1:
337 md = EVP_md5_sha1();
338 break;
339 default:
340 fprintf(stderr, "Unknown signature algorithm %04x.\n",
341 signature_algorithm);
342 return ssl_private_key_failure;
343 }
344
David Benjaminaac1e2d2016-12-06 22:35:41 -0500345 bssl::ScopedEVP_MD_CTX ctx;
David Benjamind3440b42016-07-14 14:52:41 -0400346 EVP_PKEY_CTX *pctx;
347 if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr,
348 test_state->private_key.get())) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400349 return ssl_private_key_failure;
350 }
351
David Benjamind3440b42016-07-14 14:52:41 -0400352 // Configure additional signature parameters.
353 switch (signature_algorithm) {
354 case SSL_SIGN_RSA_PSS_SHA256:
355 case SSL_SIGN_RSA_PSS_SHA384:
356 case SSL_SIGN_RSA_PSS_SHA512:
357 if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
358 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
359 -1 /* salt len = hash len */)) {
360 return ssl_private_key_failure;
361 }
362 }
363
David Benjaminb4d65fd2015-05-29 17:11:21 -0400364 // Write the signature into |test_state|.
365 size_t len = 0;
David Benjamind3440b42016-07-14 14:52:41 -0400366 if (!EVP_DigestSignUpdate(ctx.get(), in, in_len) ||
367 !EVP_DigestSignFinal(ctx.get(), nullptr, &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400368 return ssl_private_key_failure;
369 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700370 test_state->private_key_result.resize(len);
David Benjamind3440b42016-07-14 14:52:41 -0400371 if (!EVP_DigestSignFinal(ctx.get(), test_state->private_key_result.data(),
372 &len)) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400373 return ssl_private_key_failure;
374 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700375 test_state->private_key_result.resize(len);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400376
David Benjamind3440b42016-07-14 14:52:41 -0400377 // The signature will be released asynchronously in |AsyncPrivateKeyComplete|.
David Benjaminb4d65fd2015-05-29 17:11:21 -0400378 return ssl_private_key_retry;
379}
380
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700381static ssl_private_key_result_t AsyncPrivateKeyDecrypt(
382 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
383 const uint8_t *in, size_t in_len) {
384 TestState *test_state = GetTestState(ssl);
385 if (!test_state->private_key_result.empty()) {
386 fprintf(stderr,
387 "AsyncPrivateKeyDecrypt called with operation pending.\n");
388 abort();
389 }
390
David Benjamin758d1272015-11-20 17:47:25 -0500391 RSA *rsa = EVP_PKEY_get0_RSA(test_state->private_key.get());
392 if (rsa == NULL) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700393 fprintf(stderr,
394 "AsyncPrivateKeyDecrypt called with incorrect key type.\n");
395 abort();
396 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700397 test_state->private_key_result.resize(RSA_size(rsa));
David Benjaminef14b2d2015-11-11 14:01:27 -0800398 if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(),
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700399 RSA_size(rsa), in, in_len, RSA_NO_PADDING)) {
400 return ssl_private_key_failure;
401 }
402
403 test_state->private_key_result.resize(*out_len);
404
David Benjamind3440b42016-07-14 14:52:41 -0400405 // The decryption will be released asynchronously in |AsyncPrivateComplete|.
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700406 return ssl_private_key_retry;
407}
408
David Benjamind3440b42016-07-14 14:52:41 -0400409static ssl_private_key_result_t AsyncPrivateKeyComplete(
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700410 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
411 TestState *test_state = GetTestState(ssl);
412 if (test_state->private_key_result.empty()) {
413 fprintf(stderr,
David Benjamind3440b42016-07-14 14:52:41 -0400414 "AsyncPrivateKeyComplete called without operation pending.\n");
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700415 abort();
416 }
417
418 if (test_state->private_key_retries < 2) {
419 // Only return the decryption on the second attempt, to test both incomplete
420 // |decrypt| and |decrypt_complete|.
421 return ssl_private_key_retry;
422 }
423
424 if (max_out < test_state->private_key_result.size()) {
425 fprintf(stderr, "Output buffer too small.\n");
426 return ssl_private_key_failure;
427 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500428 OPENSSL_memcpy(out, test_state->private_key_result.data(),
429 test_state->private_key_result.size());
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700430 *out_len = test_state->private_key_result.size();
431
432 test_state->private_key_result.clear();
433 test_state->private_key_retries = 0;
David Benjaminb4d65fd2015-05-29 17:11:21 -0400434 return ssl_private_key_success;
435}
436
437static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
438 AsyncPrivateKeyType,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400439 AsyncPrivateKeyMaxSignatureLen,
440 AsyncPrivateKeySign,
David Benjamind3440b42016-07-14 14:52:41 -0400441 nullptr /* sign_digest */,
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700442 AsyncPrivateKeyDecrypt,
David Benjamind3440b42016-07-14 14:52:41 -0400443 AsyncPrivateKeyComplete,
David Benjaminb4d65fd2015-05-29 17:11:21 -0400444};
445
Steven Valdez0d62f262015-09-04 12:41:04 -0400446template<typename T>
Adam Langley10f97f32016-07-12 08:09:33 -0700447struct Free {
Steven Valdez0d62f262015-09-04 12:41:04 -0400448 void operator()(T *buf) {
449 free(buf);
450 }
451};
452
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700453static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509,
David Benjamin2c516452016-11-15 10:16:54 +0900454 bssl::UniquePtr<STACK_OF(X509)> *out_chain,
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700455 bssl::UniquePtr<EVP_PKEY> *out_pkey) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400456 const TestConfig *config = GetTestConfig(ssl);
Steven Valdez0d62f262015-09-04 12:41:04 -0400457
458 if (!config->digest_prefs.empty()) {
Adam Langley10f97f32016-07-12 08:09:33 -0700459 std::unique_ptr<char, Free<char>> digest_prefs(
Steven Valdez0d62f262015-09-04 12:41:04 -0400460 strdup(config->digest_prefs.c_str()));
Steven Valdez0d62f262015-09-04 12:41:04 -0400461 std::vector<int> digest_list;
462
463 for (;;) {
Adam Langley67251f22015-09-23 15:01:07 -0700464 char *token =
465 strtok(digest_list.empty() ? digest_prefs.get() : nullptr, ",");
Steven Valdez0d62f262015-09-04 12:41:04 -0400466 if (token == nullptr) {
467 break;
468 }
469
470 digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token)));
471 }
472
473 if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(),
474 digest_list.size())) {
475 return false;
476 }
477 }
478
David Benjaminca3d5452016-07-14 12:51:01 -0400479 if (!config->signing_prefs.empty()) {
480 std::vector<uint16_t> u16s(config->signing_prefs.begin(),
481 config->signing_prefs.end());
482 if (!SSL_set_signing_algorithm_prefs(ssl, u16s.data(), u16s.size())) {
483 return false;
484 }
485 }
486
David Benjaminb4d65fd2015-05-29 17:11:21 -0400487 if (!config->key_file.empty()) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500488 *out_pkey = LoadPrivateKey(config->key_file.c_str());
489 if (!*out_pkey) {
David Benjaminb4d65fd2015-05-29 17:11:21 -0400490 return false;
491 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500492 }
David Benjamin2c516452016-11-15 10:16:54 +0900493 if (!config->cert_file.empty() &&
494 !LoadCertificate(out_x509, out_chain, config->cert_file.c_str())) {
495 return false;
David Benjamin41fdbcd2015-02-09 03:13:35 -0500496 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100497 if (!config->ocsp_response.empty() &&
Alessandro Ghedini559f0642016-12-07 12:55:32 +0000498 !SSL_set_ocsp_response(ssl, (const uint8_t *)config->ocsp_response.data(),
499 config->ocsp_response.size())) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100500 return false;
501 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500502 return true;
503}
504
David Benjaminacb6dcc2016-03-10 09:15:01 -0500505static bool InstallCertificate(SSL *ssl) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700506 bssl::UniquePtr<X509> x509;
David Benjamin2c516452016-11-15 10:16:54 +0900507 bssl::UniquePtr<STACK_OF(X509)> chain;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700508 bssl::UniquePtr<EVP_PKEY> pkey;
David Benjamin2c516452016-11-15 10:16:54 +0900509 if (!GetCertificate(ssl, &x509, &chain, &pkey)) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500510 return false;
511 }
512
513 if (pkey) {
514 TestState *test_state = GetTestState(ssl);
David Benjamin7e7a82d2016-05-20 20:12:42 -0400515 const TestConfig *config = GetTestConfig(ssl);
David Benjaminacb6dcc2016-03-10 09:15:01 -0500516 if (config->async) {
517 test_state->private_key = std::move(pkey);
518 SSL_set_private_key_method(ssl, &g_async_private_key_method);
519 } else if (!SSL_use_PrivateKey(ssl, pkey.get())) {
520 return false;
521 }
522 }
523
524 if (x509 && !SSL_use_certificate(ssl, x509.get())) {
525 return false;
526 }
527
David Benjamin2c516452016-11-15 10:16:54 +0900528 if (sk_X509_num(chain.get()) > 0 &&
529 !SSL_set1_chain(ssl, chain.get())) {
530 return false;
531 }
532
David Benjaminacb6dcc2016-03-10 09:15:01 -0500533 return true;
534}
535
David Benjamin731058e2016-12-03 23:15:13 -0500536static int SelectCertificateCallback(const SSL_CLIENT_HELLO *client_hello) {
537 const TestConfig *config = GetTestConfig(client_hello->ssl);
538 GetTestState(client_hello->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400539
David Benjamin6f5c0f42015-02-24 01:23:21 -0500540 if (!config->expected_server_name.empty()) {
541 const uint8_t *extension_data;
542 size_t extension_len;
543 CBS extension, server_name_list, host_name;
544 uint8_t name_type;
545
David Benjamin731058e2016-12-03 23:15:13 -0500546 if (!SSL_early_callback_ctx_extension_get(
547 client_hello, TLSEXT_TYPE_server_name, &extension_data,
548 &extension_len)) {
David Benjamin6f5c0f42015-02-24 01:23:21 -0500549 fprintf(stderr, "Could not find server_name extension.\n");
550 return -1;
551 }
552
553 CBS_init(&extension, extension_data, extension_len);
554 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
555 CBS_len(&extension) != 0 ||
556 !CBS_get_u8(&server_name_list, &name_type) ||
557 name_type != TLSEXT_NAMETYPE_host_name ||
558 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
559 CBS_len(&server_name_list) != 0) {
560 fprintf(stderr, "Could not decode server_name extension.\n");
561 return -1;
562 }
563
564 if (!CBS_mem_equal(&host_name,
565 (const uint8_t*)config->expected_server_name.data(),
566 config->expected_server_name.size())) {
567 fprintf(stderr, "Server name mismatch.\n");
568 }
David Benjamin7b030512014-07-08 17:30:11 -0400569 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400570
David Benjamin6f5c0f42015-02-24 01:23:21 -0500571 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400572 return -1;
573 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400574
David Benjamin6f5c0f42015-02-24 01:23:21 -0500575 // Install the certificate in the early callback.
576 if (config->use_early_callback) {
Alessandro Ghedini958346a2016-12-20 19:42:15 +0000577 bool early_callback_ready =
578 GetTestState(client_hello->ssl)->early_callback_ready;
579 if (config->async && !early_callback_ready) {
David Benjamin6f5c0f42015-02-24 01:23:21 -0500580 // Install the certificate asynchronously.
581 return 0;
582 }
David Benjamin731058e2016-12-03 23:15:13 -0500583 if (!InstallCertificate(client_hello->ssl)) {
David Benjamin6f5c0f42015-02-24 01:23:21 -0500584 return -1;
585 }
David Benjamin7b030512014-07-08 17:30:11 -0400586 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400587 return 1;
588}
David Benjamin025b3d32014-07-01 19:53:04 -0400589
David Benjamin5edfc8c2016-12-10 15:46:58 -0500590static bool CheckCertificateRequest(SSL *ssl) {
591 const TestConfig *config = GetTestConfig(ssl);
592
593 if (!config->expected_certificate_types.empty()) {
594 const uint8_t *certificate_types;
595 size_t certificate_types_len =
596 SSL_get0_certificate_types(ssl, &certificate_types);
597 if (certificate_types_len != config->expected_certificate_types.size() ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500598 OPENSSL_memcmp(certificate_types,
599 config->expected_certificate_types.data(),
600 certificate_types_len) != 0) {
David Benjamin5edfc8c2016-12-10 15:46:58 -0500601 fprintf(stderr, "certificate types mismatch\n");
602 return false;
603 }
604 }
605
Adam Langley2ff79332017-02-28 13:45:39 -0800606 if (!config->expected_client_ca_list.empty()) {
607 bssl::UniquePtr<STACK_OF(X509_NAME)> expected =
608 DecodeHexX509Names(config->expected_client_ca_list);
609 const size_t num_expected = sk_X509_NAME_num(expected.get());
610
611 const STACK_OF(X509_NAME) *received = SSL_get_client_CA_list(ssl);
612 const size_t num_received = sk_X509_NAME_num(received);
613
614 if (num_received != num_expected) {
615 fprintf(stderr, "expected %u names in CertificateRequest but got %u\n",
616 static_cast<unsigned>(num_expected),
617 static_cast<unsigned>(num_received));
618 return false;
619 }
620
621 for (size_t i = 0; i < num_received; i++) {
622 if (X509_NAME_cmp(sk_X509_NAME_value(received, i),
623 sk_X509_NAME_value(expected.get(), i)) != 0) {
624 fprintf(stderr, "names in CertificateRequest differ at index #%d\n",
625 static_cast<unsigned>(i));
626 return false;
627 }
628 }
Adam Langleyd6c22ee2017-03-02 12:56:32 -0800629
630 STACK_OF(CRYPTO_BUFFER) *buffers = SSL_get0_server_requested_CAs(ssl);
631 if (sk_CRYPTO_BUFFER_num(buffers) != num_received) {
632 fprintf(stderr,
633 "Mismatch between SSL_get_server_requested_CAs and "
634 "SSL_get_client_CA_list.\n");
635 return false;
636 }
Adam Langley2ff79332017-02-28 13:45:39 -0800637 }
638
David Benjamin5edfc8c2016-12-10 15:46:58 -0500639 return true;
640}
641
David Benjaminacb6dcc2016-03-10 09:15:01 -0500642static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
David Benjamin5edfc8c2016-12-10 15:46:58 -0500643 if (!CheckCertificateRequest(ssl)) {
644 return -1;
645 }
646
David Benjamin7e7a82d2016-05-20 20:12:42 -0400647 if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500648 return -1;
649 }
650
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700651 bssl::UniquePtr<X509> x509;
David Benjamin2c516452016-11-15 10:16:54 +0900652 bssl::UniquePtr<STACK_OF(X509)> chain;
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700653 bssl::UniquePtr<EVP_PKEY> pkey;
David Benjamin2c516452016-11-15 10:16:54 +0900654 if (!GetCertificate(ssl, &x509, &chain, &pkey)) {
David Benjaminacb6dcc2016-03-10 09:15:01 -0500655 return -1;
656 }
657
658 // Return zero for no certificate.
659 if (!x509) {
660 return 0;
661 }
662
David Benjamin2c516452016-11-15 10:16:54 +0900663 // Chains and asynchronous private keys are not supported with client_cert_cb.
David Benjaminacb6dcc2016-03-10 09:15:01 -0500664 *out_x509 = x509.release();
665 *out_pkey = pkey.release();
666 return 1;
667}
668
David Benjamin5edfc8c2016-12-10 15:46:58 -0500669static int CertCallback(SSL *ssl, void *arg) {
670 const TestConfig *config = GetTestConfig(ssl);
671
672 // Check the CertificateRequest metadata is as expected.
673 if (!SSL_is_server(ssl) && !CheckCertificateRequest(ssl)) {
674 return -1;
675 }
676
677 if (config->fail_cert_callback) {
678 return 0;
679 }
680
681 // The certificate will be installed via other means.
682 if (!config->async || config->use_early_callback) {
683 return 1;
684 }
685
686 if (!GetTestState(ssl)->cert_ready) {
687 return -1;
688 }
689 if (!InstallCertificate(ssl)) {
690 return 0;
691 }
692 return 1;
693}
694
Paul Lietar8f1c2682015-08-18 12:21:54 +0100695static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
696 SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx,
697 SSL_get_ex_data_X509_STORE_CTX_idx());
David Benjamin7e7a82d2016-05-20 20:12:42 -0400698 const TestConfig *config = GetTestConfig(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100699
700 if (!config->expected_ocsp_response.empty()) {
701 const uint8_t *data;
702 size_t len;
703 SSL_get0_ocsp_response(ssl, &data, &len);
704 if (len == 0) {
705 fprintf(stderr, "OCSP response not available in verify callback\n");
706 return 0;
707 }
708 }
709
David Benjamin67666e72014-07-12 15:47:52 -0400710 return 1;
711}
712
Paul Lietar8f1c2682015-08-18 12:21:54 +0100713static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
714 store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION;
715 return 0;
716}
717
David Benjaminc273d2c2015-02-09 12:59:46 -0500718static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
719 unsigned int *out_len, void *arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400720 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500721 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400722 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500723 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400724
David Benjamin5a593af2014-08-11 19:51:50 -0400725 *out = (const uint8_t*)config->advertise_npn.data();
726 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400727 return SSL_TLSEXT_ERR_OK;
728}
729
David Benjaminc273d2c2015-02-09 12:59:46 -0500730static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400731 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400732 const TestConfig *config = GetTestConfig(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500733 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400734 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500735 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400736
David Benjamin5a593af2014-08-11 19:51:50 -0400737 *out = (uint8_t*)config->select_next_proto.data();
738 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400739 return SSL_TLSEXT_ERR_OK;
740}
741
David Benjaminc273d2c2015-02-09 12:59:46 -0500742static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
743 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400744 if (GetTestState(ssl)->alpn_select_done) {
745 fprintf(stderr, "AlpnSelectCallback called after completion.\n");
746 exit(1);
747 }
748
749 GetTestState(ssl)->alpn_select_done = true;
750
David Benjamin7e7a82d2016-05-20 20:12:42 -0400751 const TestConfig *config = GetTestConfig(ssl);
David Benjamin594e7d22016-03-17 17:49:56 -0400752 if (config->decline_alpn) {
David Benjaminae2888f2014-09-06 12:58:58 -0400753 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500754 }
David Benjaminae2888f2014-09-06 12:58:58 -0400755
756 if (!config->expected_advertised_alpn.empty() &&
757 (config->expected_advertised_alpn.size() != inlen ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500758 OPENSSL_memcmp(config->expected_advertised_alpn.data(), in, inlen) !=
759 0)) {
David Benjaminae2888f2014-09-06 12:58:58 -0400760 fprintf(stderr, "bad ALPN select callback inputs\n");
761 exit(1);
762 }
763
764 *out = (const uint8_t*)config->select_alpn.data();
765 *outlen = config->select_alpn.size();
766 return SSL_TLSEXT_ERR_OK;
767}
768
David Benjaminc273d2c2015-02-09 12:59:46 -0500769static unsigned PskClientCallback(SSL *ssl, const char *hint,
770 char *out_identity,
771 unsigned max_identity_len,
772 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400773 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400774
David Benjamin78679342016-09-16 19:42:05 -0400775 if (config->psk_identity.empty()) {
776 if (hint != nullptr) {
777 fprintf(stderr, "Server PSK hint was non-null.\n");
778 return 0;
779 }
780 } else if (hint == nullptr ||
781 strcmp(hint, config->psk_identity.c_str()) != 0) {
David Benjamin48cae082014-10-27 01:06:24 -0400782 fprintf(stderr, "Server PSK hint did not match.\n");
783 return 0;
784 }
785
786 // Account for the trailing '\0' for the identity.
787 if (config->psk_identity.size() >= max_identity_len ||
788 config->psk.size() > max_psk_len) {
789 fprintf(stderr, "PSK buffers too small\n");
790 return 0;
791 }
792
793 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
794 max_identity_len);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500795 OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size());
David Benjamin48cae082014-10-27 01:06:24 -0400796 return config->psk.size();
797}
798
David Benjaminc273d2c2015-02-09 12:59:46 -0500799static unsigned PskServerCallback(SSL *ssl, const char *identity,
800 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400801 const TestConfig *config = GetTestConfig(ssl);
David Benjamin48cae082014-10-27 01:06:24 -0400802
803 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
804 fprintf(stderr, "Client PSK identity did not match.\n");
805 return 0;
806 }
807
808 if (config->psk.size() > max_psk_len) {
809 fprintf(stderr, "PSK buffers too small\n");
810 return 0;
811 }
812
David Benjamin17cf2cb2016-12-13 01:07:13 -0500813 OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size());
David Benjamin48cae082014-10-27 01:06:24 -0400814 return config->psk.size();
815}
816
David Benjamin1b22f852016-10-27 16:36:32 -0400817static timeval g_clock;
818
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400819static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin1b22f852016-10-27 16:36:32 -0400820 *out_clock = g_clock;
David Benjamin377fc312015-01-26 00:22:12 -0500821}
822
David Benjaminc273d2c2015-02-09 12:59:46 -0500823static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500824 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500825}
826
David Benjaminc273d2c2015-02-09 12:59:46 -0500827static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
828 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500829 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500830 if (async_state->session) {
831 *copy = 0;
832 return async_state->session.release();
833 } else if (async_state->pending_session) {
834 return SSL_magic_pending_session_ptr();
835 } else {
836 return NULL;
837 }
838}
839
David Benjamin731058e2016-12-03 23:15:13 -0500840static int DDoSCallback(const SSL_CLIENT_HELLO *client_hello) {
841 const TestConfig *config = GetTestConfig(client_hello->ssl);
Adam Langley524e7172015-02-20 16:04:00 -0800842 static int callback_num = 0;
843
844 callback_num++;
845 if (config->fail_ddos_callback ||
846 (config->fail_second_ddos_callback && callback_num == 2)) {
847 return 0;
848 }
849 return 1;
850}
851
David Benjamin87e4acd2015-04-02 19:57:35 -0400852static void InfoCallback(const SSL *ssl, int type, int val) {
853 if (type == SSL_CB_HANDSHAKE_DONE) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400854 if (GetTestConfig(ssl)->handshake_never_done) {
David Benjamin7a4aaa42016-09-20 17:58:14 -0400855 fprintf(stderr, "Handshake unexpectedly completed.\n");
David Benjamin87e4acd2015-04-02 19:57:35 -0400856 // Abort before any expected error code is printed, to ensure the overall
857 // test fails.
858 abort();
859 }
860 GetTestState(ssl)->handshake_done = true;
David Benjamin25fe85b2016-08-09 20:00:32 -0400861
862 // Callbacks may be called again on a new handshake.
863 GetTestState(ssl)->ticket_decrypt_done = false;
864 GetTestState(ssl)->alpn_select_done = false;
David Benjamin87e4acd2015-04-02 19:57:35 -0400865 }
866}
867
David Benjaminba4594a2015-06-18 18:36:15 -0400868static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
869 GetTestState(ssl)->got_new_session = true;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400870 GetTestState(ssl)->new_session.reset(session);
David Benjaminba4594a2015-06-18 18:36:15 -0400871 return 1;
872}
873
David Benjamind98452d2015-06-16 14:16:23 -0400874static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
875 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
876 int encrypt) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400877 if (!encrypt) {
878 if (GetTestState(ssl)->ticket_decrypt_done) {
879 fprintf(stderr, "TicketKeyCallback called after completion.\n");
880 return -1;
881 }
882
883 GetTestState(ssl)->ticket_decrypt_done = true;
884 }
885
David Benjamind98452d2015-06-16 14:16:23 -0400886 // This is just test code, so use the all-zeros key.
887 static const uint8_t kZeros[16] = {0};
888
889 if (encrypt) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500890 OPENSSL_memcpy(key_name, kZeros, sizeof(kZeros));
David Benjamind98452d2015-06-16 14:16:23 -0400891 RAND_bytes(iv, 16);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500892 } else if (OPENSSL_memcmp(key_name, kZeros, 16) != 0) {
David Benjamind98452d2015-06-16 14:16:23 -0400893 return 0;
894 }
895
896 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
897 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
898 return -1;
899 }
900
901 if (!encrypt) {
David Benjamin7e7a82d2016-05-20 20:12:42 -0400902 return GetTestConfig(ssl)->renew_ticket ? 2 : 1;
David Benjamind98452d2015-06-16 14:16:23 -0400903 }
904 return 1;
905}
906
Adam Langley09505632015-07-30 18:10:13 -0700907// kCustomExtensionValue is the extension value that the custom extension
908// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700909static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700910static void *const kCustomExtensionAddArg =
911 reinterpret_cast<void *>(kCustomExtensionValue);
912static void *const kCustomExtensionParseArg =
913 reinterpret_cast<void *>(kCustomExtensionValue + 1);
914static const char kCustomExtensionContents[] = "custom extension";
915
916static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
917 const uint8_t **out, size_t *out_len,
918 int *out_alert_value, void *add_arg) {
919 if (extension_value != kCustomExtensionValue ||
920 add_arg != kCustomExtensionAddArg) {
921 abort();
922 }
923
David Benjamin7e7a82d2016-05-20 20:12:42 -0400924 if (GetTestConfig(ssl)->custom_extension_skip) {
Adam Langley09505632015-07-30 18:10:13 -0700925 return 0;
926 }
David Benjamin7e7a82d2016-05-20 20:12:42 -0400927 if (GetTestConfig(ssl)->custom_extension_fail_add) {
Adam Langley09505632015-07-30 18:10:13 -0700928 return -1;
929 }
930
931 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
932 *out_len = sizeof(kCustomExtensionContents) - 1;
933
934 return 1;
935}
936
937static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
938 const uint8_t *out, void *add_arg) {
939 if (extension_value != kCustomExtensionValue ||
940 add_arg != kCustomExtensionAddArg ||
941 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
942 abort();
943 }
944}
945
946static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
947 const uint8_t *contents,
948 size_t contents_len,
949 int *out_alert_value, void *parse_arg) {
950 if (extension_value != kCustomExtensionValue ||
951 parse_arg != kCustomExtensionParseArg) {
952 abort();
953 }
954
955 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500956 OPENSSL_memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
Adam Langley09505632015-07-30 18:10:13 -0700957 *out_alert_value = SSL_AD_DECODE_ERROR;
958 return 0;
959 }
960
961 return 1;
962}
963
David Benjamin8b176712016-10-27 21:51:24 -0400964static int ServerNameCallback(SSL *ssl, int *out_alert, void *arg) {
965 // SNI must be accessible from the SNI callback.
966 const TestConfig *config = GetTestConfig(ssl);
967 const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
968 if (server_name == nullptr ||
969 std::string(server_name) != config->expected_server_name) {
970 fprintf(stderr, "servername mismatch (got %s; want %s)\n", server_name,
971 config->expected_server_name.c_str());
972 return SSL_TLSEXT_ERR_ALERT_FATAL;
973 }
974
975 return SSL_TLSEXT_ERR_OK;
976}
977
David Benjamin87c8a642015-02-21 01:54:29 -0500978// Connect returns a new socket connected to localhost on |port| or -1 on
979// error.
980static int Connect(uint16_t port) {
981 int sock = socket(AF_INET, SOCK_STREAM, 0);
982 if (sock == -1) {
983 PrintSocketError("socket");
984 return -1;
985 }
986 int nodelay = 1;
987 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
988 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
989 PrintSocketError("setsockopt");
990 closesocket(sock);
991 return -1;
992 }
993 sockaddr_in sin;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500994 OPENSSL_memset(&sin, 0, sizeof(sin));
David Benjamin87c8a642015-02-21 01:54:29 -0500995 sin.sin_family = AF_INET;
996 sin.sin_port = htons(port);
997 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
998 PrintSocketError("inet_pton");
999 closesocket(sock);
1000 return -1;
1001 }
1002 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
1003 sizeof(sin)) != 0) {
1004 PrintSocketError("connect");
1005 closesocket(sock);
1006 return -1;
1007 }
1008 return sock;
1009}
1010
1011class SocketCloser {
1012 public:
1013 explicit SocketCloser(int sock) : sock_(sock) {}
1014 ~SocketCloser() {
1015 // Half-close and drain the socket before releasing it. This seems to be
1016 // necessary for graceful shutdown on Windows. It will also avoid write
1017 // failures in the test runner.
1018#if defined(OPENSSL_WINDOWS)
1019 shutdown(sock_, SD_SEND);
1020#else
1021 shutdown(sock_, SHUT_WR);
1022#endif
1023 while (true) {
1024 char buf[1024];
1025 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
1026 break;
1027 }
1028 }
1029 closesocket(sock_);
1030 }
1031
1032 private:
1033 const int sock_;
1034};
1035
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001036static bssl::UniquePtr<SSL_CTX> SetupCtx(const TestConfig *config) {
1037 bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(
David Benjamina7f333d2015-02-09 02:37:18 -05001038 config->is_dtls ? DTLS_method() : TLS_method()));
1039 if (!ssl_ctx) {
1040 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -04001041 }
1042
Adam Langleyd519bf62016-12-12 11:16:44 -08001043 SSL_CTX_set0_buffer_pool(ssl_ctx.get(), g_pool);
1044
David Benjamin2dc02042016-09-19 19:57:37 -04001045 // Enable TLS 1.3 for tests.
1046 if (!config->is_dtls &&
David Benjamine4706902016-09-20 15:12:23 -04001047 !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION)) {
David Benjamin2dc02042016-09-19 19:57:37 -04001048 return nullptr;
Nick Harper1fd39d82016-06-14 18:14:35 -07001049 }
1050
Adam Langleycef75832015-09-03 14:51:12 -07001051 std::string cipher_list = "ALL";
1052 if (!config->cipher.empty()) {
1053 cipher_list = config->cipher;
1054 SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
1055 }
Matthew Braithwaitea57dcfb2017-02-17 22:08:23 -08001056 if (!SSL_CTX_set_strict_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
Adam Langleycef75832015-09-03 14:51:12 -07001057 return nullptr;
1058 }
1059
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001060 bssl::UniquePtr<DH> dh(DH_get_2048_256(NULL));
David Benjaminb7c5e842016-03-28 09:59:10 -04001061 if (!dh) {
1062 return nullptr;
1063 }
Adam Langleyc4f25ce2015-11-26 16:39:08 -08001064
1065 if (config->use_sparse_dh_prime) {
1066 // This prime number is 2^1024 + 643 – a value just above a power of two.
1067 // Because of its form, values modulo it are essentially certain to be one
1068 // byte shorter. This is used to test padding of these values.
1069 if (BN_hex2bn(
1070 &dh->p,
1071 "1000000000000000000000000000000000000000000000000000000000000000"
1072 "0000000000000000000000000000000000000000000000000000000000000000"
1073 "0000000000000000000000000000000000000000000000000000000000000000"
1074 "0000000000000000000000000000000000000000000000000000000000000028"
1075 "3") == 0 ||
1076 !BN_set_word(dh->g, 2)) {
1077 return nullptr;
1078 }
David Benjamine66148a2016-02-02 14:14:36 -05001079 BN_free(dh->q);
1080 dh->q = NULL;
Adam Langleyc4f25ce2015-11-26 16:39:08 -08001081 dh->priv_length = 0;
1082 }
1083
David Benjaminb7c5e842016-03-28 09:59:10 -04001084 if (!SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
David Benjamina7f333d2015-02-09 02:37:18 -05001085 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -04001086 }
1087
David Benjamin1b8b6912015-02-09 04:28:16 -05001088 if (config->async && config->is_server) {
1089 // Disable the internal session cache. To test asynchronous session lookup,
1090 // we use an external session cache.
1091 SSL_CTX_set_session_cache_mode(
1092 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -05001093 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -05001094 } else {
1095 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
1096 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04001097
David Benjamind4c2bce2015-10-17 12:28:18 -04001098 SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback);
David Benjamin8f2c20e2014-07-09 09:30:38 -04001099
David Benjaminacb6dcc2016-03-10 09:15:01 -05001100 if (config->use_old_client_cert_callback) {
1101 SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback);
1102 }
1103
David Benjamin1f5f62b2014-07-12 16:18:02 -04001104 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -05001105 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -04001106 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001107 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -05001108 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -04001109 }
1110
David Benjamin594e7d22016-03-17 17:49:56 -04001111 if (!config->select_alpn.empty() || config->decline_alpn) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001112 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -04001113 }
David Benjamin1f5f62b2014-07-12 16:18:02 -04001114
David Benjaminc273d2c2015-02-09 12:59:46 -05001115 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -04001116
David Benjamin1b22f852016-10-27 16:36:32 -04001117 SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback);
David Benjamin377fc312015-01-26 00:22:12 -05001118
David Benjamin87e4acd2015-04-02 19:57:35 -04001119 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -04001120 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -04001121
David Benjamind98452d2015-06-16 14:16:23 -04001122 if (config->use_ticket_callback) {
1123 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
1124 }
1125
Adam Langley09505632015-07-30 18:10:13 -07001126 if (config->enable_client_custom_extension &&
1127 !SSL_CTX_add_client_custom_ext(
1128 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
1129 CustomExtensionFreeCallback, kCustomExtensionAddArg,
1130 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
1131 return nullptr;
1132 }
1133
1134 if (config->enable_server_custom_extension &&
1135 !SSL_CTX_add_server_custom_ext(
1136 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
1137 CustomExtensionFreeCallback, kCustomExtensionAddArg,
1138 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
1139 return nullptr;
1140 }
1141
Paul Lietar8f1c2682015-08-18 12:21:54 +01001142 if (config->verify_fail) {
1143 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
1144 } else {
1145 SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
1146 }
1147
Paul Lietar4fac72e2015-09-09 13:44:55 +01001148 if (!config->signed_cert_timestamps.empty() &&
1149 !SSL_CTX_set_signed_cert_timestamp_list(
1150 ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(),
1151 config->signed_cert_timestamps.size())) {
1152 return nullptr;
1153 }
1154
Adam Langley2ff79332017-02-28 13:45:39 -08001155 if (!config->use_client_ca_list.empty()) {
1156 if (config->use_client_ca_list == "<NULL>") {
1157 SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr);
1158 } else {
1159 bssl::UniquePtr<STACK_OF(X509_NAME)> names =
1160 DecodeHexX509Names(config->use_client_ca_list);
1161 SSL_CTX_set_client_CA_list(ssl_ctx.get(), names.release());
1162 }
David Benjamin2f8935d2016-07-13 19:47:39 -04001163 }
1164
David Benjamin65ac9972016-09-02 21:35:25 -04001165 if (config->enable_grease) {
1166 SSL_CTX_set_grease_enabled(ssl_ctx.get(), 1);
1167 }
1168
David Benjamin8b176712016-10-27 21:51:24 -04001169 if (!config->expected_server_name.empty()) {
1170 SSL_CTX_set_tlsext_servername_callback(ssl_ctx.get(), ServerNameCallback);
1171 }
1172
David Benjamin4199b0d2016-11-01 13:58:25 -04001173 if (!config->ticket_key.empty() &&
1174 !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), config->ticket_key.data(),
1175 config->ticket_key.size())) {
1176 return nullptr;
1177 }
1178
David Benjamin6f600d62016-12-21 16:06:54 -05001179 if (config->enable_short_header) {
1180 SSL_CTX_set_short_header_enabled(ssl_ctx.get(), 1);
1181 }
1182
Steven Valdez08b65f42016-12-07 15:29:45 -05001183 if (config->enable_early_data) {
1184 SSL_CTX_set_early_data_enabled(ssl_ctx.get(), 1);
1185 }
1186
David Benjamin1d5c83e2014-07-22 19:20:02 -04001187 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001188}
1189
David Benjamin40f101b2015-02-20 11:23:42 -05001190// RetryAsync is called after a failed operation on |ssl| with return code
1191// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -04001192// event and returns true. Otherwise it returns false.
1193static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -04001194 // No error; don't retry.
1195 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001196 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001197 }
David Benjamin83f90402015-01-27 01:09:43 -05001198
David Benjamin6c2563e2015-04-03 03:47:47 -04001199 TestState *test_state = GetTestState(ssl);
Matt Braithwaite6278e242016-06-14 08:18:22 -07001200 assert(GetTestConfig(ssl)->async);
David Benjamin83f90402015-01-27 01:09:43 -05001201
David Benjamin585d7a42016-06-02 14:58:00 -04001202 if (test_state->packeted_bio != nullptr &&
1203 PacketedBioAdvanceClock(test_state->packeted_bio)) {
David Benjamin13e81fc2015-11-02 17:16:13 -05001204 // The DTLS retransmit logic silently ignores write failures. So the test
1205 // may progress, allow writes through synchronously.
David Benjamin585d7a42016-06-02 14:58:00 -04001206 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
David Benjamin13e81fc2015-11-02 17:16:13 -05001207 int timeout_ret = DTLSv1_handle_timeout(ssl);
David Benjamin585d7a42016-06-02 14:58:00 -04001208 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
David Benjamin13e81fc2015-11-02 17:16:13 -05001209
1210 if (timeout_ret < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -04001211 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001212 return false;
David Benjamin83f90402015-01-27 01:09:43 -05001213 }
David Benjamin40f101b2015-02-20 11:23:42 -05001214 return true;
David Benjamin83f90402015-01-27 01:09:43 -05001215 }
1216
David Benjamin43ec06f2014-08-05 02:28:57 -04001217 // See if we needed to read or write more. If so, allow one byte through on
1218 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -05001219 switch (SSL_get_error(ssl, ret)) {
1220 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -04001221 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -05001222 return true;
David Benjamind9e07012015-02-09 03:04:34 -05001223 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -04001224 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -05001225 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -05001226 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001227 bssl::UniquePtr<EVP_PKEY> pkey =
1228 LoadPrivateKey(GetTestConfig(ssl)->send_channel_id);
David Benjamin9d0847a2015-02-16 03:57:55 -05001229 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -05001230 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -05001231 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001232 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -05001233 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -05001234 }
David Benjamin41fdbcd2015-02-09 03:13:35 -05001235 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -04001236 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -05001237 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -05001238 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -04001239 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -05001240 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -05001241 case SSL_ERROR_PENDING_CERTIFICATE:
Alessandro Ghedini958346a2016-12-20 19:42:15 +00001242 test_state->early_callback_ready = true;
1243 return true;
David Benjaminb4d65fd2015-05-29 17:11:21 -04001244 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001245 test_state->private_key_retries++;
David Benjaminb4d65fd2015-05-29 17:11:21 -04001246 return true;
David Benjamind9e07012015-02-09 03:04:34 -05001247 default:
David Benjamin40f101b2015-02-20 11:23:42 -05001248 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001249 }
David Benjamin025b3d32014-07-01 19:53:04 -04001250}
1251
David Benjamin6c2563e2015-04-03 03:47:47 -04001252// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
1253// the result value of the final |SSL_read| call.
1254static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001255 const TestConfig *config = GetTestConfig(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -05001256 TestState *test_state = GetTestState(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001257 int ret;
1258 do {
David Benjamin13e81fc2015-11-02 17:16:13 -05001259 if (config->async) {
1260 // The DTLS retransmit logic silently ignores write failures. So the test
1261 // may progress, allow writes through synchronously. |SSL_read| may
1262 // trigger a retransmit, so disconnect the write quota.
1263 AsyncBioEnforceWriteQuota(test_state->async_bio, false);
1264 }
David Benjaminf3fbade2016-09-19 13:08:16 -04001265 ret = config->peek_then_read ? SSL_peek(ssl, out, max_out)
1266 : SSL_read(ssl, out, max_out);
David Benjamin13e81fc2015-11-02 17:16:13 -05001267 if (config->async) {
1268 AsyncBioEnforceWriteQuota(test_state->async_bio, true);
1269 }
David Benjamin7bb1d292016-11-01 19:45:06 -04001270
1271 // Run the exporter after each read. This is to test that the exporter fails
1272 // during a renegotiation.
1273 if (config->use_exporter_between_reads) {
1274 uint8_t buf;
1275 if (!SSL_export_keying_material(ssl, &buf, 1, NULL, 0, NULL, 0, 0)) {
1276 fprintf(stderr, "failed to export keying material\n");
1277 return -1;
1278 }
1279 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001280 } while (config->async && RetryAsync(ssl, ret));
David Benjaminf3fbade2016-09-19 13:08:16 -04001281
1282 if (config->peek_then_read && ret > 0) {
1283 std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]);
1284
1285 // SSL_peek should synchronously return the same data.
1286 int ret2 = SSL_peek(ssl, buf.get(), ret);
1287 if (ret2 != ret ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001288 OPENSSL_memcmp(buf.get(), out, ret) != 0) {
David Benjaminf3fbade2016-09-19 13:08:16 -04001289 fprintf(stderr, "First and second SSL_peek did not match.\n");
1290 return -1;
1291 }
1292
1293 // SSL_read should synchronously return the same data and consume it.
1294 ret2 = SSL_read(ssl, buf.get(), ret);
1295 if (ret2 != ret ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001296 OPENSSL_memcmp(buf.get(), out, ret) != 0) {
David Benjaminf3fbade2016-09-19 13:08:16 -04001297 fprintf(stderr, "SSL_peek and SSL_read did not match.\n");
1298 return -1;
1299 }
1300 }
1301
David Benjamin6c2563e2015-04-03 03:47:47 -04001302 return ret;
1303}
1304
1305// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
1306// operations. It returns the result of the final |SSL_write| call.
1307static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001308 const TestConfig *config = GetTestConfig(ssl);
David Benjamin6c2563e2015-04-03 03:47:47 -04001309 int ret;
1310 do {
1311 ret = SSL_write(ssl, in, in_len);
1312 if (ret > 0) {
1313 in += ret;
1314 in_len -= ret;
1315 }
1316 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
1317 return ret;
1318}
1319
David Benjamin30789da2015-08-29 22:56:45 -04001320// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
1321// returns the result of the final |SSL_shutdown| call.
1322static int DoShutdown(SSL *ssl) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001323 const TestConfig *config = GetTestConfig(ssl);
David Benjamin30789da2015-08-29 22:56:45 -04001324 int ret;
1325 do {
1326 ret = SSL_shutdown(ssl);
1327 } while (config->async && RetryAsync(ssl, ret));
1328 return ret;
1329}
1330
David Benjamin1d4f4c02016-07-26 18:03:08 -04001331// DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous
1332// operations. It returns the result of the final |SSL_send_fatal_alert| call.
1333static int DoSendFatalAlert(SSL *ssl, uint8_t alert) {
1334 const TestConfig *config = GetTestConfig(ssl);
1335 int ret;
1336 do {
1337 ret = SSL_send_fatal_alert(ssl, alert);
1338 } while (config->async && RetryAsync(ssl, ret));
1339 return ret;
1340}
1341
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001342static uint16_t GetProtocolVersion(const SSL *ssl) {
1343 uint16_t version = SSL_version(ssl);
1344 if (!SSL_is_dtls(ssl)) {
1345 return version;
1346 }
1347 return 0x0201 + ~version;
1348}
1349
David Benjamin91eab5c2015-06-18 18:35:46 -04001350// CheckHandshakeProperties checks, immediately after |ssl| completes its
1351// initial handshake (or False Starts), whether all the properties are
1352// consistent with the test configuration and invariants.
1353static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
David Benjamin7e7a82d2016-05-20 20:12:42 -04001354 const TestConfig *config = GetTestConfig(ssl);
David Benjamin91eab5c2015-06-18 18:35:46 -04001355
1356 if (SSL_get_current_cipher(ssl) == nullptr) {
1357 fprintf(stderr, "null cipher after handshake\n");
1358 return false;
1359 }
1360
1361 if (is_resume &&
1362 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
1363 fprintf(stderr, "session was%s reused\n",
1364 SSL_session_reused(ssl) ? "" : " not");
1365 return false;
1366 }
1367
1368 bool expect_handshake_done = is_resume || !config->false_start;
1369 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
1370 fprintf(stderr, "handshake was%s completed\n",
1371 GetTestState(ssl)->handshake_done ? "" : " not");
1372 return false;
1373 }
1374
David Benjaminba4594a2015-06-18 18:36:15 -04001375 if (expect_handshake_done && !config->is_server) {
1376 bool expect_new_session =
1377 !config->expect_no_session &&
Steven Valdez143e8b32016-07-11 13:19:03 -04001378 (!SSL_session_reused(ssl) || config->expect_ticket_renewal) &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001379 // Session tickets are sent post-handshake in TLS 1.3.
1380 GetProtocolVersion(ssl) < TLS1_3_VERSION;
David Benjaminba4594a2015-06-18 18:36:15 -04001381 if (expect_new_session != GetTestState(ssl)->got_new_session) {
1382 fprintf(stderr,
David Benjamindd6fed92015-10-23 17:41:12 -04001383 "new session was%s cached, but we expected the opposite\n",
David Benjaminba4594a2015-06-18 18:36:15 -04001384 GetTestState(ssl)->got_new_session ? "" : " not");
1385 return false;
1386 }
1387 }
1388
David Benjaminb5c58db2017-01-28 01:39:29 -05001389 if (!is_resume) {
1390 if (config->expect_session_id && !GetTestState(ssl)->got_new_session) {
1391 fprintf(stderr, "session was not cached on the server.\n");
1392 return false;
1393 }
1394 if (config->expect_no_session_id && GetTestState(ssl)->got_new_session) {
1395 fprintf(stderr, "session was unexpectedly cached on the server.\n");
1396 return false;
1397 }
1398 }
1399
David Benjamin91eab5c2015-06-18 18:35:46 -04001400 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
1401 fprintf(stderr, "early callback not called\n");
1402 return false;
1403 }
1404
1405 if (!config->expected_server_name.empty()) {
1406 const char *server_name =
1407 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin8b176712016-10-27 21:51:24 -04001408 if (server_name == nullptr ||
1409 server_name != config->expected_server_name) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001410 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
1411 server_name, config->expected_server_name.c_str());
1412 return false;
1413 }
1414 }
1415
David Benjamin91eab5c2015-06-18 18:35:46 -04001416 if (!config->expected_next_proto.empty()) {
1417 const uint8_t *next_proto;
1418 unsigned next_proto_len;
1419 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
1420 if (next_proto_len != config->expected_next_proto.size() ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001421 OPENSSL_memcmp(next_proto, config->expected_next_proto.data(),
1422 next_proto_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001423 fprintf(stderr, "negotiated next proto mismatch\n");
1424 return false;
1425 }
1426 }
1427
1428 if (!config->expected_alpn.empty()) {
1429 const uint8_t *alpn_proto;
1430 unsigned alpn_proto_len;
1431 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
1432 if (alpn_proto_len != config->expected_alpn.size() ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001433 OPENSSL_memcmp(alpn_proto, config->expected_alpn.data(),
1434 alpn_proto_len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001435 fprintf(stderr, "negotiated alpn proto mismatch\n");
1436 return false;
1437 }
1438 }
1439
1440 if (!config->expected_channel_id.empty()) {
1441 uint8_t channel_id[64];
1442 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
1443 fprintf(stderr, "no channel id negotiated\n");
1444 return false;
1445 }
1446 if (config->expected_channel_id.size() != 64 ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001447 OPENSSL_memcmp(config->expected_channel_id.data(), channel_id, 64) !=
1448 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001449 fprintf(stderr, "channel id mismatch\n");
1450 return false;
1451 }
1452 }
1453
David Benjamind2610042017-01-03 10:49:28 -05001454 if (config->expect_extended_master_secret && !SSL_get_extms_support(ssl)) {
1455 fprintf(stderr, "No EMS for connection when expected\n");
1456 return false;
1457 }
1458
1459 if (config->expect_secure_renegotiation &&
1460 !SSL_get_secure_renegotiation_support(ssl)) {
1461 fprintf(stderr, "No secure renegotiation for connection when expected\n");
1462 return false;
1463 }
1464
1465 if (config->expect_no_secure_renegotiation &&
1466 SSL_get_secure_renegotiation_support(ssl)) {
1467 fprintf(stderr,
1468 "Secure renegotiation unexpectedly negotiated for connection\n");
1469 return false;
David Benjamin91eab5c2015-06-18 18:35:46 -04001470 }
1471
1472 if (!config->expected_ocsp_response.empty()) {
1473 const uint8_t *data;
1474 size_t len;
1475 SSL_get0_ocsp_response(ssl, &data, &len);
1476 if (config->expected_ocsp_response.size() != len ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001477 OPENSSL_memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001478 fprintf(stderr, "OCSP response mismatch\n");
1479 return false;
1480 }
1481 }
1482
1483 if (!config->expected_signed_cert_timestamps.empty()) {
1484 const uint8_t *data;
1485 size_t len;
1486 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
1487 if (config->expected_signed_cert_timestamps.size() != len ||
David Benjamin17cf2cb2016-12-13 01:07:13 -05001488 OPENSSL_memcmp(config->expected_signed_cert_timestamps.data(), data,
1489 len) != 0) {
David Benjamin91eab5c2015-06-18 18:35:46 -04001490 fprintf(stderr, "SCT list mismatch\n");
1491 return false;
1492 }
1493 }
1494
Paul Lietar8f1c2682015-08-18 12:21:54 +01001495 if (config->expect_verify_result) {
1496 int expected_verify_result = config->verify_fail ?
1497 X509_V_ERR_APPLICATION_VERIFICATION :
1498 X509_V_OK;
1499
1500 if (SSL_get_verify_result(ssl) != expected_verify_result) {
1501 fprintf(stderr, "Wrong certificate verification result\n");
1502 return false;
1503 }
1504 }
1505
Nick Harper60edffd2016-06-21 15:19:24 -07001506 if (config->expect_peer_signature_algorithm != 0 &&
1507 config->expect_peer_signature_algorithm !=
1508 SSL_get_peer_signature_algorithm(ssl)) {
1509 fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n",
1510 SSL_get_peer_signature_algorithm(ssl),
1511 config->expect_peer_signature_algorithm);
David Benjamin6e807652015-11-02 12:02:20 -05001512 return false;
1513 }
1514
David Benjamin8a55ce42016-12-11 03:03:42 -05001515 int expect_curve_id = config->expect_curve_id;
1516 if (is_resume && config->expect_resume_curve_id != 0) {
1517 expect_curve_id = config->expect_resume_curve_id;
1518 }
1519 if (expect_curve_id != 0) {
David Benjamin9e68f192016-06-30 14:55:33 -04001520 uint16_t curve_id = SSL_get_curve_id(ssl);
David Benjamin8a55ce42016-12-11 03:03:42 -05001521 if (static_cast<uint16_t>(expect_curve_id) != curve_id) {
David Benjamin9e68f192016-06-30 14:55:33 -04001522 fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id,
David Benjamin8a55ce42016-12-11 03:03:42 -05001523 static_cast<uint16_t>(expect_curve_id));
David Benjamin9e68f192016-06-30 14:55:33 -04001524 return false;
1525 }
1526 }
1527
David Benjaminabbbee12016-10-31 19:20:42 -04001528 uint16_t cipher_id =
1529 static_cast<uint16_t>(SSL_CIPHER_get_id(SSL_get_current_cipher(ssl)));
1530 if (config->expect_cipher_aes != 0 &&
1531 EVP_has_aes_hardware() &&
1532 static_cast<uint16_t>(config->expect_cipher_aes) != cipher_id) {
1533 fprintf(stderr, "Cipher ID was %04x, wanted %04x (has AES hardware)\n",
1534 cipher_id, static_cast<uint16_t>(config->expect_cipher_aes));
1535 return false;
1536 }
1537
1538 if (config->expect_cipher_no_aes != 0 &&
1539 !EVP_has_aes_hardware() &&
1540 static_cast<uint16_t>(config->expect_cipher_no_aes) != cipher_id) {
1541 fprintf(stderr, "Cipher ID was %04x, wanted %04x (no AES hardware)\n",
1542 cipher_id, static_cast<uint16_t>(config->expect_cipher_no_aes));
1543 return false;
1544 }
1545
1546
David Benjaminbb9e36e2016-08-03 14:14:47 -04001547 if (!config->psk.empty()) {
1548 if (SSL_get_peer_cert_chain(ssl) != nullptr) {
1549 fprintf(stderr, "Received peer certificate on a PSK cipher.\n");
1550 return false;
1551 }
1552 } else if (!config->is_server || config->require_any_client_certificate) {
1553 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
1554 fprintf(stderr, "Received no peer certificate but expected one.\n");
David Benjamin91eab5c2015-06-18 18:35:46 -04001555 return false;
1556 }
1557 }
David Benjaminbb9e36e2016-08-03 14:14:47 -04001558
David Benjamin2c516452016-11-15 10:16:54 +09001559 if (!config->expect_peer_cert_file.empty()) {
1560 bssl::UniquePtr<X509> expect_leaf;
1561 bssl::UniquePtr<STACK_OF(X509)> expect_chain;
1562 if (!LoadCertificate(&expect_leaf, &expect_chain,
1563 config->expect_peer_cert_file)) {
1564 return false;
1565 }
1566
1567 // For historical reasons, clients report a chain with a leaf and servers
1568 // without.
1569 if (!config->is_server) {
1570 if (!sk_X509_insert(expect_chain.get(), expect_leaf.get(), 0)) {
1571 return false;
1572 }
1573 X509_up_ref(expect_leaf.get()); // sk_X509_push takes ownership.
1574 }
1575
1576 bssl::UniquePtr<X509> leaf(SSL_get_peer_certificate(ssl));
1577 STACK_OF(X509) *chain = SSL_get_peer_cert_chain(ssl);
1578 if (X509_cmp(leaf.get(), expect_leaf.get()) != 0) {
1579 fprintf(stderr, "Received a different leaf certificate than expected.\n");
1580 return false;
1581 }
1582
1583 if (sk_X509_num(chain) != sk_X509_num(expect_chain.get())) {
1584 fprintf(stderr, "Received a chain of length %zu instead of %zu.\n",
1585 sk_X509_num(chain), sk_X509_num(expect_chain.get()));
1586 return false;
1587 }
1588
1589 for (size_t i = 0; i < sk_X509_num(chain); i++) {
1590 if (X509_cmp(sk_X509_value(chain, i),
1591 sk_X509_value(expect_chain.get(), i)) != 0) {
1592 fprintf(stderr, "Chain certificate %zu did not match.\n",
1593 i + 1);
1594 return false;
1595 }
1596 }
1597 }
1598
David Benjaminbbaf3672016-11-17 10:53:09 +09001599 bool expected_sha256_client_cert = config->expect_sha256_client_cert_initial;
1600 if (is_resume) {
1601 expected_sha256_client_cert = config->expect_sha256_client_cert_resume;
1602 }
1603
1604 if (SSL_get_session(ssl)->peer_sha256_valid != expected_sha256_client_cert) {
1605 fprintf(stderr,
1606 "Unexpected SHA-256 client cert state: expected:%d is_resume:%d.\n",
1607 expected_sha256_client_cert, is_resume);
1608 return false;
1609 }
1610
1611 if (expected_sha256_client_cert &&
Adam Langley46db7af2017-02-01 15:49:37 -08001612 SSL_get_session(ssl)->certs != nullptr) {
David Benjaminbbaf3672016-11-17 10:53:09 +09001613 fprintf(stderr, "Have both client cert and SHA-256 hash: is_resume:%d.\n",
1614 is_resume);
1615 return false;
1616 }
1617
David Benjamin91eab5c2015-06-18 18:35:46 -04001618 return true;
1619}
1620
David Benjamin87c8a642015-02-21 01:54:29 -05001621// DoExchange runs a test SSL exchange against the peer. On success, it returns
1622// true and sets |*out_session| to the negotiated SSL session. If the test is a
1623// resumption attempt, |is_resume| is true and |session| is the session from the
1624// previous exchange.
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001625static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session,
1626 SSL_CTX *ssl_ctx, const TestConfig *config,
1627 bool is_resume, SSL_SESSION *session) {
1628 bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx));
David Benjamina7f333d2015-02-09 02:37:18 -05001629 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -05001630 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001631 }
1632
David Benjamin7e7a82d2016-05-20 20:12:42 -04001633 if (!SetTestConfig(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -05001634 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -05001635 return false;
Adam Langley69a01602014-11-17 17:26:55 -08001636 }
David Benjamin5a593af2014-08-11 19:51:50 -04001637
Adam Langley5f0efe02015-02-20 13:03:16 -08001638 if (config->fallback_scsv &&
1639 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
1640 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001641 }
David Benjamina0486782016-10-06 19:11:32 -04001642 // Install the certificate synchronously if nothing else will handle it.
1643 if (!config->use_early_callback &&
1644 !config->use_old_client_cert_callback &&
1645 !config->async &&
1646 !InstallCertificate(ssl.get())) {
1647 return false;
David Benjamin5a593af2014-08-11 19:51:50 -04001648 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001649 if (!config->use_old_client_cert_callback) {
1650 SSL_set_cert_cb(ssl.get(), CertCallback, nullptr);
1651 }
David Benjamin5a593af2014-08-11 19:51:50 -04001652 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -05001653 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
Paul Lietar8f1c2682015-08-18 12:21:54 +01001654 NULL);
1655 }
1656 if (config->verify_peer) {
1657 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
David Benjamin5a593af2014-08-11 19:51:50 -04001658 }
1659 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -05001660 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -04001661 }
1662 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -05001663 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -04001664 }
1665 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -05001666 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -04001667 }
Steven Valdez4f94b1c2016-05-24 12:31:07 -04001668 if (config->no_tls13) {
1669 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3);
1670 }
David Benjamin5a593af2014-08-11 19:51:50 -04001671 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -05001672 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -04001673 }
1674 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -05001675 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -04001676 }
1677 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -05001678 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -04001679 }
1680 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -05001681 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -04001682 }
Steven Valdez143e8b32016-07-11 13:19:03 -04001683 if (!config->expected_channel_id.empty() ||
1684 config->enable_channel_id) {
David Benjamineebd3c82016-12-06 17:43:58 -05001685 SSL_set_tls_channel_id_enabled(ssl.get(), 1);
David Benjamina08e49d2014-08-24 01:46:07 -04001686 }
1687 if (!config->send_channel_id.empty()) {
David Benjamineebd3c82016-12-06 17:43:58 -05001688 SSL_set_tls_channel_id_enabled(ssl.get(), 1);
David Benjamind9e07012015-02-09 03:04:34 -05001689 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001690 // The async case will be supplied by |ChannelIdCallback|.
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001691 bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(config->send_channel_id);
David Benjamind9e07012015-02-09 03:04:34 -05001692 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001693 return false;
David Benjamind9e07012015-02-09 03:04:34 -05001694 }
David Benjamina08e49d2014-08-24 01:46:07 -04001695 }
David Benjamina08e49d2014-08-24 01:46:07 -04001696 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001697 if (!config->host_name.empty() &&
1698 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001699 return false;
David Benjamine78bfde2014-09-06 12:45:15 -04001700 }
David Benjamin9d0847a2015-02-16 03:57:55 -05001701 if (!config->advertise_alpn.empty() &&
1702 SSL_set_alpn_protos(ssl.get(),
1703 (const uint8_t *)config->advertise_alpn.data(),
1704 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001705 return false;
David Benjaminae2888f2014-09-06 12:58:58 -04001706 }
David Benjamin48cae082014-10-27 01:06:24 -04001707 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001708 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
1709 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -04001710 }
David Benjamin61f95272014-11-25 01:55:35 -05001711 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001712 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001713 return false;
David Benjamin48cae082014-10-27 01:06:24 -04001714 }
David Benjamin61f95272014-11-25 01:55:35 -05001715 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -05001716 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -05001717 return false;
David Benjamin61f95272014-11-25 01:55:35 -05001718 }
David Benjamin26e1ff32017-02-14 20:13:00 -05001719 if (config->enable_ocsp_stapling) {
1720 SSL_enable_ocsp_stapling(ssl.get());
David Benjamin61f95272014-11-25 01:55:35 -05001721 }
David Benjamin26e1ff32017-02-14 20:13:00 -05001722 if (config->enable_signed_cert_timestamps) {
1723 SSL_enable_signed_cert_timestamps(ssl.get());
David Benjaminca6c8262014-11-15 19:06:08 -05001724 }
David Benjamin2dc02042016-09-19 19:57:37 -04001725 if (config->min_version != 0 &&
David Benjamine4706902016-09-20 15:12:23 -04001726 !SSL_set_min_proto_version(ssl.get(), (uint16_t)config->min_version)) {
David Benjamin2dc02042016-09-19 19:57:37 -04001727 return false;
David Benjamin1eb367c2014-12-12 18:17:51 -05001728 }
David Benjamin2dc02042016-09-19 19:57:37 -04001729 if (config->max_version != 0 &&
David Benjamine4706902016-09-20 15:12:23 -04001730 !SSL_set_max_proto_version(ssl.get(), (uint16_t)config->max_version)) {
David Benjamin2dc02042016-09-19 19:57:37 -04001731 return false;
David Benjamin1eb367c2014-12-12 18:17:51 -05001732 }
David Benjamin13be1de2015-01-11 16:29:36 -05001733 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001734 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1735 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001736 }
Adam Langley524e7172015-02-20 16:04:00 -08001737 if (config->install_ddos_callback) {
1738 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1739 }
David Benjamin1d5ef3b2015-10-12 19:54:18 -04001740 if (config->renegotiate_once) {
1741 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once);
1742 }
1743 if (config->renegotiate_freely) {
1744 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely);
David Benjaminb16346b2015-04-08 19:16:58 -04001745 }
Adam Langley27a0d082015-11-03 13:34:10 -08001746 if (config->renegotiate_ignore) {
1747 SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore);
1748 }
David Benjamin30789da2015-08-29 22:56:45 -04001749 if (!config->check_close_notify) {
1750 SSL_set_quiet_shutdown(ssl.get(), 1);
1751 }
David Benjamin99fdfb92015-11-02 12:11:35 -05001752 if (config->p384_only) {
1753 int nid = NID_secp384r1;
1754 if (!SSL_set1_curves(ssl.get(), &nid, 1)) {
1755 return false;
1756 }
1757 }
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001758 if (config->enable_all_curves) {
1759 static const int kAllCurves[] = {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001760 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519,
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001761 };
1762 if (!SSL_set1_curves(ssl.get(), kAllCurves,
Steven Valdezcb966542016-08-17 16:56:14 -04001763 OPENSSL_ARRAY_SIZE(kAllCurves))) {
David Benjamin8c2b3bf2015-12-18 20:55:44 -05001764 return false;
1765 }
1766 }
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -07001767 if (config->initial_timeout_duration_ms > 0) {
1768 DTLSv1_set_initial_timeout_duration(ssl.get(),
1769 config->initial_timeout_duration_ms);
1770 }
David Benjamina252b342016-09-26 19:57:53 -04001771 if (config->max_cert_list > 0) {
1772 SSL_set_max_cert_list(ssl.get(), config->max_cert_list);
1773 }
David Benjaminbbaf3672016-11-17 10:53:09 +09001774 if (!is_resume && config->retain_only_sha256_client_cert_initial) {
1775 SSL_set_retain_only_sha256_of_client_certs(ssl.get(), 1);
1776 }
1777 if (is_resume && config->retain_only_sha256_client_cert_resume) {
1778 SSL_set_retain_only_sha256_of_client_certs(ssl.get(), 1);
1779 }
David Benjamine3fbb362017-01-06 16:19:28 -05001780 if (config->max_send_fragment > 0) {
1781 SSL_set_max_send_fragment(ssl.get(), config->max_send_fragment);
1782 }
David Benjamin025b3d32014-07-01 19:53:04 -04001783
David Benjamin87c8a642015-02-21 01:54:29 -05001784 int sock = Connect(config->port);
1785 if (sock == -1) {
1786 return false;
1787 }
1788 SocketCloser closer(sock);
1789
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001790 bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001791 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001792 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001793 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001794 if (config->is_dtls) {
David Benjamin11c82892017-02-23 20:40:31 -05001795 bssl::UniquePtr<BIO> packeted = PacketedBioCreate(&g_clock);
David Benjaminb7c5e842016-03-28 09:59:10 -04001796 if (!packeted) {
1797 return false;
1798 }
David Benjamin585d7a42016-06-02 14:58:00 -04001799 GetTestState(ssl.get())->packeted_bio = packeted.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001800 BIO_push(packeted.get(), bio.release());
1801 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001802 }
David Benjamin5a593af2014-08-11 19:51:50 -04001803 if (config->async) {
Matt Braithwaited17d74d2016-08-17 20:10:28 -07001804 bssl::UniquePtr<BIO> async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001805 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjaminb7c5e842016-03-28 09:59:10 -04001806 if (!async_scoped) {
1807 return false;
1808 }
David Benjamina7f333d2015-02-09 02:37:18 -05001809 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001810 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001811 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001812 }
David Benjamina7f333d2015-02-09 02:37:18 -05001813 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1814 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001815
David Benjamin1d5c83e2014-07-22 19:20:02 -04001816 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001817 if (!config->is_server) {
1818 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001819 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001820 }
1821 } else if (config->async) {
1822 // The internal session cache is disabled, so install the session
1823 // manually.
David Benjaminb9195402016-08-05 10:51:43 -04001824 SSL_SESSION_up_ref(session);
1825 GetTestState(ssl.get())->pending_session.reset(session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001826 }
1827 }
1828
David Benjamina07c0fc2015-05-13 13:19:42 -04001829 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1830 fprintf(stderr, "non-null cipher before handshake\n");
1831 return false;
1832 }
1833
David Benjamin1d5c83e2014-07-22 19:20:02 -04001834 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001835 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001836 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001837 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001838 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001839 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001840 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001841 } else {
1842 do {
1843 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001844 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001845 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001846 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001847 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001848 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001849 if (ret != 1 ||
1850 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001851 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001852 }
David Benjamin025b3d32014-07-01 19:53:04 -04001853
David Benjaminba4594a2015-06-18 18:36:15 -04001854 // Reset the state to assert later that the callback isn't called in
1855 // renegotations.
1856 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001857 }
1858
David Benjaminc565ebb2015-04-03 04:06:36 -04001859 if (config->export_keying_material > 0) {
1860 std::vector<uint8_t> result(
1861 static_cast<size_t>(config->export_keying_material));
1862 if (!SSL_export_keying_material(
1863 ssl.get(), result.data(), result.size(),
1864 config->export_label.data(), config->export_label.size(),
1865 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1866 config->export_context.size(), config->use_export_context)) {
1867 fprintf(stderr, "failed to export keying material\n");
1868 return false;
1869 }
1870 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1871 return false;
1872 }
1873 }
1874
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001875 if (config->tls_unique) {
1876 uint8_t tls_unique[16];
1877 size_t tls_unique_len;
1878 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1879 sizeof(tls_unique))) {
1880 fprintf(stderr, "failed to get tls-unique\n");
1881 return false;
1882 }
1883
1884 if (tls_unique_len != 12) {
1885 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1886 static_cast<unsigned>(tls_unique_len));
1887 return false;
1888 }
1889
1890 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1891 return false;
1892 }
1893 }
1894
David Benjamin1d4f4c02016-07-26 18:03:08 -04001895 if (config->send_alert) {
1896 if (DoSendFatalAlert(ssl.get(), SSL_AD_DECOMPRESSION_FAILURE) < 0) {
1897 return false;
1898 }
1899 return true;
1900 }
1901
David Benjamin5a593af2014-08-11 19:51:50 -04001902 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001903 if (config->is_dtls) {
1904 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001905 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001906 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001907 // This mode writes a number of different record sizes in an attempt to
1908 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001909 static const size_t kBufLen = 32769;
1910 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
David Benjamin17cf2cb2016-12-13 01:07:13 -05001911 OPENSSL_memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001912 static const size_t kRecordSizes[] = {
1913 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
Steven Valdezcb966542016-08-17 16:56:14 -04001914 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001915 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001916 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001917 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001918 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001919 }
Adam Langleybc949292015-06-18 21:32:44 -07001920 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001921 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001922 }
1923 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001924 } else {
David Benjamina1eaba12017-01-01 23:19:22 -05001925 if (config->read_with_unfinished_write) {
1926 if (!config->async) {
1927 fprintf(stderr, "-read-with-unfinished-write requires -async.\n");
1928 return false;
1929 }
1930
1931 int write_ret = SSL_write(ssl.get(),
1932 reinterpret_cast<const uint8_t *>("unfinished"), 10);
1933 if (SSL_get_error(ssl.get(), write_ret) != SSL_ERROR_WANT_WRITE) {
1934 fprintf(stderr, "Failed to leave unfinished write.\n");
1935 return false;
1936 }
1937 }
David Benjamine58c4f52014-08-24 03:47:07 -04001938 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001939 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1940 5) < 0) {
1941 return false;
1942 }
David Benjamine58c4f52014-08-24 03:47:07 -04001943 }
David Benjamin30789da2015-08-29 22:56:45 -04001944 if (!config->shim_shuts_down) {
1945 for (;;) {
David Benjamin2c99d282015-09-01 10:23:00 -04001946 // Read only 512 bytes at a time in TLS to ensure records may be
1947 // returned in multiple reads.
David Benjamine3fbb362017-01-06 16:19:28 -05001948 size_t read_size = config->is_dtls ? 16384 : 512;
1949 if (config->read_size > 0) {
1950 read_size = config->read_size;
1951 }
1952 std::unique_ptr<uint8_t[]> buf(new uint8_t[read_size]);
1953
1954 int n = DoRead(ssl.get(), buf.get(), read_size);
David Benjamin30789da2015-08-29 22:56:45 -04001955 int err = SSL_get_error(ssl.get(), n);
1956 if (err == SSL_ERROR_ZERO_RETURN ||
1957 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1958 if (n != 0) {
1959 fprintf(stderr, "Invalid SSL_get_error output\n");
1960 return false;
1961 }
1962 // Stop on either clean or unclean shutdown.
1963 break;
1964 } else if (err != SSL_ERROR_NONE) {
1965 if (n > 0) {
1966 fprintf(stderr, "Invalid SSL_get_error output\n");
1967 return false;
1968 }
1969 return false;
1970 }
1971 // Successfully read data.
1972 if (n <= 0) {
David Benjamin9a38e922015-01-22 16:06:11 -05001973 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001974 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001975 }
David Benjamin30789da2015-08-29 22:56:45 -04001976
1977 // After a successful read, with or without False Start, the handshake
1978 // must be complete.
1979 if (!GetTestState(ssl.get())->handshake_done) {
1980 fprintf(stderr, "handshake was not completed after SSL_read\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001981 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001982 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001983
David Benjamin30789da2015-08-29 22:56:45 -04001984 for (int i = 0; i < n; i++) {
1985 buf[i] ^= 0xff;
1986 }
Adam Langleya0a8dc22015-09-09 10:22:00 -07001987 if (WriteAll(ssl.get(), buf.get(), n) < 0) {
David Benjamin30789da2015-08-29 22:56:45 -04001988 return false;
1989 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001990 }
1991 }
David Benjamin025b3d32014-07-01 19:53:04 -04001992 }
1993
David Benjaminba4594a2015-06-18 18:36:15 -04001994 if (!config->is_server && !config->false_start &&
1995 !config->implicit_handshake &&
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001996 // Session tickets are sent post-handshake in TLS 1.3.
1997 GetProtocolVersion(ssl.get()) < TLS1_3_VERSION &&
David Benjaminba4594a2015-06-18 18:36:15 -04001998 GetTestState(ssl.get())->got_new_session) {
1999 fprintf(stderr, "new session was established after the handshake\n");
2000 return false;
2001 }
2002
Steven Valdez1e6f11a2016-07-27 11:10:52 -04002003 if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) {
2004 bool expect_new_session =
2005 !config->expect_no_session && !config->shim_shuts_down;
2006 if (expect_new_session != GetTestState(ssl.get())->got_new_session) {
2007 fprintf(stderr,
2008 "new session was%s cached, but we expected the opposite\n",
2009 GetTestState(ssl.get())->got_new_session ? "" : " not");
2010 return false;
2011 }
Steven Valdez08b65f42016-12-07 15:29:45 -05002012
2013 if (expect_new_session) {
2014 bool got_early_data_info =
2015 GetTestState(ssl.get())->new_session->ticket_max_early_data != 0;
2016 if (config->expect_early_data_info != got_early_data_info) {
2017 fprintf(
2018 stderr,
2019 "new session did%s include ticket_early_data_info, but we expected "
2020 "the opposite\n",
2021 got_early_data_info ? "" : " not");
2022 return false;
2023 }
2024 }
Steven Valdez1e6f11a2016-07-27 11:10:52 -04002025 }
2026
David Benjamin1d5c83e2014-07-22 19:20:02 -04002027 if (out_session) {
Steven Valdez4aa154e2016-07-29 14:32:55 -04002028 *out_session = std::move(GetTestState(ssl.get())->new_session);
David Benjamin1d5c83e2014-07-22 19:20:02 -04002029 }
2030
David Benjamin30789da2015-08-29 22:56:45 -04002031 ret = DoShutdown(ssl.get());
2032
2033 if (config->shim_shuts_down && config->check_close_notify) {
2034 // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
2035 // it returns zero when our close_notify is sent, then one when the peer's
2036 // is received.
2037 if (ret != 0) {
2038 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
2039 return false;
2040 }
2041 ret = DoShutdown(ssl.get());
2042 }
2043
2044 if (ret != 1) {
2045 fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
2046 return false;
2047 }
2048
David Benjamin324dce42015-10-12 19:49:00 -04002049 if (SSL_total_renegotiations(ssl.get()) !=
2050 config->expect_total_renegotiations) {
2051 fprintf(stderr, "Expected %d renegotiations, got %d\n",
2052 config->expect_total_renegotiations,
2053 SSL_total_renegotiations(ssl.get()));
2054 return false;
2055 }
2056
David Benjamin40f101b2015-02-20 11:23:42 -05002057 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04002058}
David Benjamin1d5c83e2014-07-22 19:20:02 -04002059
David Benjaminff3a1492016-03-02 10:12:06 -05002060class StderrDelimiter {
2061 public:
2062 ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
2063};
2064
David Benjaminaac1e2d2016-12-06 22:35:41 -05002065int main(int argc, char **argv) {
David Benjaminff3a1492016-03-02 10:12:06 -05002066 // To distinguish ASan's output from ours, add a trailing message to stderr.
2067 // Anything following this line will be considered an error.
2068 StderrDelimiter delimiter;
2069
David Benjamin87c8a642015-02-21 01:54:29 -05002070#if defined(OPENSSL_WINDOWS)
2071 /* Initialize Winsock. */
2072 WORD wsa_version = MAKEWORD(2, 2);
2073 WSADATA wsa_data;
2074 int wsa_err = WSAStartup(wsa_version, &wsa_data);
2075 if (wsa_err != 0) {
2076 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
2077 return 1;
2078 }
2079 if (wsa_data.wVersion != wsa_version) {
2080 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
2081 return 1;
2082 }
2083#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04002084 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07002085#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04002086
David Benjamin7a1eefd2015-10-17 23:39:22 -04002087 CRYPTO_library_init();
David Benjamind9e07012015-02-09 03:04:34 -05002088 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05002089 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04002090 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05002091 return 1;
2092 }
David Benjamin5a593af2014-08-11 19:51:50 -04002093
2094 TestConfig config;
2095 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05002096 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04002097 }
2098
Adam Langleyd519bf62016-12-12 11:16:44 -08002099 g_pool = CRYPTO_BUFFER_POOL_new();
2100
David Benjamin1b22f852016-10-27 16:36:32 -04002101 // Some code treats the zero time special, so initialize the clock to a
2102 // non-zero time.
2103 g_clock.tv_sec = 1234;
2104 g_clock.tv_usec = 1234;
2105
Matt Braithwaited17d74d2016-08-17 20:10:28 -07002106 bssl::UniquePtr<SSL_CTX> ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05002107 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10002108 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04002109 return 1;
2110 }
2111
Matt Braithwaited17d74d2016-08-17 20:10:28 -07002112 bssl::UniquePtr<SSL_SESSION> session;
David Benjamin46662482016-08-17 00:51:00 -04002113 for (int i = 0; i < config.resume_count + 1; i++) {
2114 bool is_resume = i > 0;
2115 if (is_resume && !config.is_server && !session) {
2116 fprintf(stderr, "No session to offer.\n");
2117 return 1;
2118 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04002119
Matt Braithwaited17d74d2016-08-17 20:10:28 -07002120 bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session);
David Benjamin46662482016-08-17 00:51:00 -04002121 if (!DoExchange(&session, ssl_ctx.get(), &config, is_resume,
2122 offer_session.get())) {
2123 fprintf(stderr, "Connection %d failed.\n", i + 1);
2124 ERR_print_errors_fp(stderr);
2125 return 1;
2126 }
Steven Valdeza833c352016-11-01 13:39:36 -04002127
2128 if (config.resumption_delay != 0) {
2129 g_clock.tv_sec += config.resumption_delay;
2130 }
David Benjamin1d5c83e2014-07-22 19:20:02 -04002131 }
2132
David Benjamina7f333d2015-02-09 02:37:18 -05002133 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04002134}