1d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Copyright (c) 2014, Google Inc.
2d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
3d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Permission to use, copy, modify, and/or distribute this software for any
4d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * purpose with or without fee is hereby granted, provided that the above
5d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * copyright notice and this permission notice appear in all copies.
6d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
7d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
15d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/base.h>
16d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
17d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#if !defined(OPENSSL_WINDOWS)
18d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <arpa/inet.h>
19d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <netinet/in.h>
20e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <netinet/tcp.h>
21d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <signal.h>
22d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <sys/socket.h>
23e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <sys/types.h>
24d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <unistd.h>
25e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#else
26e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <io.h>
27e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#pragma warning(push, 3)
28e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <winsock2.h>
29e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <ws2tcpip.h>
30e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#pragma warning(pop)
31e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
32e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#pragma comment(lib, "Ws2_32.lib")
33d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#endif
34d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
35d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <string.h>
36d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <sys/types.h>
37d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
38d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/bio.h>
39d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/buf.h>
40d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/bytestring.h>
41e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <openssl/err.h>
42d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/ssl.h>
43d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
44e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <memory>
45e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include <vector>
46e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
47e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include "../../crypto/test/scoped_types.h"
48d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include "async_bio.h"
49d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include "packeted_bio.h"
50e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#include "scoped_types.h"
51d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include "test_config.h"
52d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
53e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
54e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#if !defined(OPENSSL_WINDOWS)
55e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int closesocket(int sock) {
56e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return close(sock);
57e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
58e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
59e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic void PrintSocketError(const char *func) {
60e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  perror(func);
61e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
62e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#else
63e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic void PrintSocketError(const char *func) {
64e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
65e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
66e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#endif
67e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
68e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int Usage(const char *program) {
69e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  fprintf(stderr, "Usage: %s [flags...]\n", program);
70d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return 1;
71d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
72d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
73e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystruct TestState {
74e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  TestState() {
75e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // MSVC cannot initialize these inline.
76e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    memset(&clock, 0, sizeof(clock));
77e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    memset(&clock_delta, 0, sizeof(clock_delta));
78e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
79e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
80e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  // async_bio is async BIO which pauses reads and writes.
81e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  BIO *async_bio = nullptr;
82e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  // clock is the current time for the SSL connection.
83e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  timeval clock;
84e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  // clock_delta is how far the clock advanced in the most recent failed
85e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  // |BIO_read|.
86e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  timeval clock_delta;
87e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedEVP_PKEY channel_id;
88e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  bool cert_ready = false;
89e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedSSL_SESSION session;
90e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedSSL_SESSION pending_session;
91e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  bool early_callback_called = false;
92e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  bool handshake_done = false;
93e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley};
94e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
95e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
96e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley			    int index, long argl, void *argp) {
97e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  delete ((TestState *)ptr);
98e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
99e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
100e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int g_config_index = 0;
101e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int g_state_index = 0;
102d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
103d9e397b599b13d642138480a28c14db7a136bf0Adam Langleystatic bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
104e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
105d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
106d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
107e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic const TestConfig *GetConfigPtr(const SSL *ssl) {
108e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
109d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
110d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
111e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
112e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
113e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    async.release();
114e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return true;
115d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
116e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return false;
117e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
118e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
119e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic TestState *GetTestState(const SSL *ssl) {
120e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return (TestState *)SSL_get_ex_data(ssl, g_state_index);
121e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
122e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
123e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
124e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedBIO bio(BIO_new(BIO_s_file()));
125e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
126e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return nullptr;
127d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
128e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
129d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return pkey;
130d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
131d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
132e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic bool InstallCertificate(SSL *ssl) {
133e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
134e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->key_file.empty() &&
135e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
136e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                               SSL_FILETYPE_PEM)) {
137e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
138e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
139e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->cert_file.empty() &&
140e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
141e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                SSL_FILETYPE_PEM)) {
142e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
143e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
144e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return true;
145e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
146d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
147e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
148d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  const TestConfig *config = GetConfigPtr(ctx->ssl);
149e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  GetTestState(ctx->ssl)->early_callback_called = true;
150d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
151e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->expected_server_name.empty()) {
152e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    const uint8_t *extension_data;
153e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    size_t extension_len;
154e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    CBS extension, server_name_list, host_name;
155e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    uint8_t name_type;
156e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
157e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
158e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                              &extension_data,
159e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                              &extension_len)) {
160e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "Could not find server_name extension.\n");
161e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return -1;
162e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
163d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
164e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    CBS_init(&extension, extension_data, extension_len);
165e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
166e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        CBS_len(&extension) != 0 ||
167e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        !CBS_get_u8(&server_name_list, &name_type) ||
168e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        name_type != TLSEXT_NAMETYPE_host_name ||
169e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
170e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        CBS_len(&server_name_list) != 0) {
171e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "Could not decode server_name extension.\n");
172e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return -1;
173e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
174d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
175e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!CBS_mem_equal(&host_name,
176e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                       (const uint8_t*)config->expected_server_name.data(),
177e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                       config->expected_server_name.size())) {
178e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "Server name mismatch.\n");
179e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
180d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
181d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
182e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->fail_early_callback) {
183d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return -1;
184d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
185d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
186e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  // Install the certificate in the early callback.
187e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->use_early_callback) {
188e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (config->async) {
189e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // Install the certificate asynchronously.
190e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return 0;
191e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
192e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!InstallCertificate(ctx->ssl)) {
193e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return -1;
194e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
195d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
196d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return 1;
197d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
198d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
199e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
200d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return 1;
201d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
202d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
203e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
204e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                        unsigned int *out_len, void *arg) {
205d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
206e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->advertise_npn.empty()) {
207d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return SSL_TLSEXT_ERR_NOACK;
208e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
209d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
210d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  *out = (const uint8_t*)config->advertise_npn.data();
211d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  *out_len = config->advertise_npn.size();
212d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return SSL_TLSEXT_ERR_OK;
213d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
214d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
215e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
216e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                   const uint8_t* in, unsigned inlen, void* arg) {
217d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
218e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->select_next_proto.empty()) {
219d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return SSL_TLSEXT_ERR_NOACK;
220e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
221d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
222d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  *out = (uint8_t*)config->select_next_proto.data();
223d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  *outlen = config->select_next_proto.size();
224d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return SSL_TLSEXT_ERR_OK;
225d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
226d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
227e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
228e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                              const uint8_t* in, unsigned inlen, void* arg) {
229d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
230e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->select_alpn.empty()) {
231d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return SSL_TLSEXT_ERR_NOACK;
232e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
233d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
234d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->expected_advertised_alpn.empty() &&
235d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      (config->expected_advertised_alpn.size() != inlen ||
236d9e397b599b13d642138480a28c14db7a136bf0Adam Langley       memcmp(config->expected_advertised_alpn.data(),
237d9e397b599b13d642138480a28c14db7a136bf0Adam Langley              in, inlen) != 0)) {
238d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    fprintf(stderr, "bad ALPN select callback inputs\n");
239d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    exit(1);
240d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
241d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
242d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  *out = (const uint8_t*)config->select_alpn.data();
243d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  *outlen = config->select_alpn.size();
244d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return SSL_TLSEXT_ERR_OK;
245d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
246d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
247e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic unsigned PskClientCallback(SSL *ssl, const char *hint,
248e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                  char *out_identity,
249e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                  unsigned max_identity_len,
250e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                  uint8_t *out_psk, unsigned max_psk_len) {
251d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
252d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
253d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
254d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    fprintf(stderr, "Server PSK hint did not match.\n");
255d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 0;
256d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
257d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
258d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  // Account for the trailing '\0' for the identity.
259d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->psk_identity.size() >= max_identity_len ||
260d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      config->psk.size() > max_psk_len) {
261d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    fprintf(stderr, "PSK buffers too small\n");
262d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 0;
263d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
264d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
265d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  BUF_strlcpy(out_identity, config->psk_identity.c_str(),
266d9e397b599b13d642138480a28c14db7a136bf0Adam Langley              max_identity_len);
267d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  memcpy(out_psk, config->psk.data(), config->psk.size());
268d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return config->psk.size();
269d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
270d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
271e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic unsigned PskServerCallback(SSL *ssl, const char *identity,
272e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                  uint8_t *out_psk, unsigned max_psk_len) {
273d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
274d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
275d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (strcmp(identity, config->psk_identity.c_str()) != 0) {
276d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    fprintf(stderr, "Client PSK identity did not match.\n");
277d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 0;
278d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
279d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
280d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->psk.size() > max_psk_len) {
281d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    fprintf(stderr, "PSK buffers too small\n");
282d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 0;
283d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
284d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
285d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  memcpy(out_psk, config->psk.data(), config->psk.size());
286d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  return config->psk.size();
287d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
288d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
289e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
290e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  *out_clock = GetTestState(ssl)->clock;
291e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
292d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
293e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
294e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  *out_pkey = GetTestState(ssl)->channel_id.release();
295e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
296e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
297e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int CertCallback(SSL *ssl, void *arg) {
298e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!GetTestState(ssl)->cert_ready) {
299e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return -1;
300e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
301e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!InstallCertificate(ssl)) {
302e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return 0;
303e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
304e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return 1;
305e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
306e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
307e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
308e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                       int *copy) {
309e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  TestState *async_state = GetTestState(ssl);
310e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (async_state->session) {
311e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    *copy = 0;
312e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return async_state->session.release();
313e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  } else if (async_state->pending_session) {
314e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return SSL_magic_pending_session_ptr();
315e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  } else {
316e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return NULL;
317e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
318e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
319e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
320e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
321e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  const TestConfig *config = GetConfigPtr(early_context->ssl);
322e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  static int callback_num = 0;
323e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
324e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  callback_num++;
325e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->fail_ddos_callback ||
326e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      (config->fail_second_ddos_callback && callback_num == 2)) {
327e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return 0;
328e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
329e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return 1;
330e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
331e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
332e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic void InfoCallback(const SSL *ssl, int type, int val) {
333e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (type == SSL_CB_HANDSHAKE_DONE) {
334e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (GetConfigPtr(ssl)->handshake_never_done) {
335e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "handshake completed\n");
336e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // Abort before any expected error code is printed, to ensure the overall
337e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // test fails.
338e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      abort();
339e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
340e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    GetTestState(ssl)->handshake_done = true;
341e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
342e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
343e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
344e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// Connect returns a new socket connected to localhost on |port| or -1 on
345e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// error.
346e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int Connect(uint16_t port) {
347e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  int sock = socket(AF_INET, SOCK_STREAM, 0);
348e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (sock == -1) {
349e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    PrintSocketError("socket");
350e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return -1;
351e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
352e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  int nodelay = 1;
353e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
354e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
355e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    PrintSocketError("setsockopt");
356e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    closesocket(sock);
357e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return -1;
358e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
359e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  sockaddr_in sin;
360e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  memset(&sin, 0, sizeof(sin));
361e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  sin.sin_family = AF_INET;
362e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  sin.sin_port = htons(port);
363e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
364e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    PrintSocketError("inet_pton");
365e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    closesocket(sock);
366e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return -1;
367e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
368e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
369e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley              sizeof(sin)) != 0) {
370e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    PrintSocketError("connect");
371e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    closesocket(sock);
372e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return -1;
373e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
374e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return sock;
375e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
376e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
377e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleyclass SocketCloser {
378e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley public:
379e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  explicit SocketCloser(int sock) : sock_(sock) {}
380e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ~SocketCloser() {
381e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // Half-close and drain the socket before releasing it. This seems to be
382e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // necessary for graceful shutdown on Windows. It will also avoid write
383e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // failures in the test runner.
384e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#if defined(OPENSSL_WINDOWS)
385e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    shutdown(sock_, SD_SEND);
386e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#else
387e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    shutdown(sock_, SHUT_WR);
388e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#endif
389e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    while (true) {
390e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      char buf[1024];
391e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
392e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        break;
393e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
394e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
395e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    closesocket(sock_);
396e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
397e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
398e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley private:
399e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  const int sock_;
400e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley};
401e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
402e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic ScopedSSL_CTX SetupCtx(const TestConfig *config) {
403e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
404e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      config->is_dtls ? DTLS_method() : TLS_method()));
405e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!ssl_ctx) {
406e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return nullptr;
407d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
408d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
409e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
410e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return nullptr;
411d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
412d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
413e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedDH dh(DH_get_2048_256(NULL));
414e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
415e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return nullptr;
416d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
417d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
418e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->async && config->is_server) {
419e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // Disable the internal session cache. To test asynchronous session lookup,
420e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // we use an external session cache.
421e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_CTX_set_session_cache_mode(
422e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
423e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
424e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  } else {
425e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
426d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
427d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
428e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ssl_ctx->select_certificate_cb = SelectCertificateCallback;
429d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
430d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  SSL_CTX_set_next_protos_advertised_cb(
431e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
432d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->select_next_proto.empty()) {
433e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
434e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                                     NULL);
435d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
436d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
437d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->select_alpn.empty()) {
438e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
439d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
440d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
441d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  ssl_ctx->tlsext_channel_id_enabled_new = 1;
442e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
443d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
444e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ssl_ctx->current_time_cb = CurrentTimeCallback;
445d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
446e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
447e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
448e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return ssl_ctx;
449d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
450d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
451e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// RetryAsync is called after a failed operation on |ssl| with return code
452e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// |ret|. If the operation should be retried, it simulates one asynchronous
453e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// event and returns true. Otherwise it returns false.
454e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic bool RetryAsync(SSL *ssl, int ret) {
455d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  // No error; don't retry.
456d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (ret >= 0) {
457e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
458e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
459e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
460e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  TestState *test_state = GetTestState(ssl);
461e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (test_state->clock_delta.tv_usec != 0 ||
462e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      test_state->clock_delta.tv_sec != 0) {
463e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    // Process the timeout and retry.
464e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
465e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
466e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    test_state->clock.tv_usec %= 1000000;
467e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
468e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
469e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
470e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (DTLSv1_handle_timeout(ssl) < 0) {
471e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "Error retransmitting.\n");
472e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
473e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
474e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return true;
475d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
476e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
477d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  // See if we needed to read or write more. If so, allow one byte through on
478d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  // the appropriate end to maximally stress the state machine.
479e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  switch (SSL_get_error(ssl, ret)) {
480e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    case SSL_ERROR_WANT_READ:
481e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      AsyncBioAllowRead(test_state->async_bio, 1);
482e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return true;
483e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    case SSL_ERROR_WANT_WRITE:
484e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      AsyncBioAllowWrite(test_state->async_bio, 1);
485e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return true;
486e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
487e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
488e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (!pkey) {
489e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
490e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
491e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      test_state->channel_id = std::move(pkey);
492e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return true;
493e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
494e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    case SSL_ERROR_WANT_X509_LOOKUP:
495e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      test_state->cert_ready = true;
496e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return true;
497e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    case SSL_ERROR_PENDING_SESSION:
498e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      test_state->session = std::move(test_state->pending_session);
499e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return true;
500e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    case SSL_ERROR_PENDING_CERTIFICATE:
501e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // The handshake will resume without a second call to the early callback.
502e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return InstallCertificate(ssl);
503e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    default:
504e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
505d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
506d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
507d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
508e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
509e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// the result value of the final |SSL_read| call.
510e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
511e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
512e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  int ret;
513e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  do {
514e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ret = SSL_read(ssl, out, max_out);
515e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  } while (config->async && RetryAsync(ssl, ret));
516e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return ret;
517e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
518d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
519e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
520e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// operations. It returns the result of the final |SSL_write| call.
521e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
522e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  const TestConfig *config = GetConfigPtr(ssl);
523e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  int ret;
524e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  do {
525e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ret = SSL_write(ssl, in, in_len);
526e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (ret > 0) {
527e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      in += ret;
528e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      in_len -= ret;
529e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
530e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
531e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return ret;
532e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley}
533d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
534e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// DoExchange runs a test SSL exchange against the peer. On success, it returns
535e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// true and sets |*out_session| to the negotiated SSL session. If the test is a
536e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// resumption attempt, |is_resume| is true and |session| is the session from the
537e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley// previous exchange.
538e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langleystatic bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
539e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                       const TestConfig *config, bool is_resume,
540e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                       SSL_SESSION *session) {
541e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedSSL ssl(SSL_new(ssl_ctx));
542e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!ssl) {
543e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
544d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
545d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
546e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!SetConfigPtr(ssl.get(), config) ||
547e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
548e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
549d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
550e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
551e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->fallback_scsv &&
552e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
553e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
554d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
555e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->use_early_callback) {
556e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (config->async) {
557e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
558e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
559e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    } else if (!InstallCertificate(ssl.get())) {
560e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
561d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
562d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
563d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->require_any_client_certificate) {
564e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
565e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                   SkipVerify);
566d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
567d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->false_start) {
568e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
569d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
570d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->cbc_record_splitting) {
571e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
572d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
573d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->partial_write) {
574e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
575d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
576d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->no_tls12) {
577e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
578d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
579d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->no_tls11) {
580e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
581d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
582d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->no_tls1) {
583e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
584d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
585d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->no_ssl3) {
586e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
587d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
588d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->tls_d5_bug) {
589e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
590d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
591d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->allow_unsafe_legacy_renegotiation) {
592e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
593d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
59453b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  if (config->no_legacy_server_connect) {
59553b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
59653b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  }
597d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->expected_channel_id.empty()) {
598e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_enable_tls_channel_id(ssl.get());
599d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
600d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->send_channel_id.empty()) {
601e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_enable_tls_channel_id(ssl.get());
602e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->async) {
603e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // The async case will be supplied by |ChannelIdCallback|.
604e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
605e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
606e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
607e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
608d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
609d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
610e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->host_name.empty() &&
611e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
612e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
613d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
614e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->advertise_alpn.empty() &&
615e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_set_alpn_protos(ssl.get(),
616e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                          (const uint8_t *)config->advertise_alpn.data(),
617e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                          config->advertise_alpn.size()) != 0) {
618e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
619d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
620d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->psk.empty()) {
621e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
622e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
623d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
624d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->psk_identity.empty() &&
625e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
626e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
627d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
628d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!config->srtp_profiles.empty() &&
629e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
630e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
631d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
632d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->enable_ocsp_stapling &&
633e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_enable_ocsp_stapling(ssl.get())) {
634e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
635d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
636d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->enable_signed_cert_timestamps &&
637e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_enable_signed_cert_timestamps(ssl.get())) {
638e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
639d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
640e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding);
641d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->min_version != 0) {
642e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
643d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
644d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->max_version != 0) {
645e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
646d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
647d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->mtu != 0) {
648e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
649e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_set_mtu(ssl.get(), config->mtu);
650e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
651e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->install_ddos_callback) {
652e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
653e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
654e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!config->cipher.empty() &&
655e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
656e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
657e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
65853b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  if (!config->reject_peer_renegotiations) {
65953b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    /* Renegotiations are disabled by default. */
66053b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    SSL_set_reject_peer_renegotiations(ssl.get(), 0);
661d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
662d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
663e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  int sock = Connect(config->port);
664e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (sock == -1) {
665e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
666e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
667e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  SocketCloser closer(sock);
668e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
669e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
670e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!bio) {
671e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return false;
672d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
673d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->is_dtls) {
674e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ScopedBIO packeted =
675e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
676e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    BIO_push(packeted.get(), bio.release());
677e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    bio = std::move(packeted);
678d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
679d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->async) {
680e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ScopedBIO async_scoped =
681e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
682e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    BIO_push(async_scoped.get(), bio.release());
683e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    GetTestState(ssl.get())->async_bio = async_scoped.get();
684e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    bio = std::move(async_scoped);
685d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
686e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  SSL_set_bio(ssl.get(), bio.get(), bio.get());
687e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  bio.release();  // SSL_set_bio takes ownership.
688d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
689d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (session != NULL) {
690e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->is_server) {
691e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (SSL_set_session(ssl.get(), session) != 1) {
692e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
693e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
694e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    } else if (config->async) {
695e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // The internal session cache is disabled, so install the session
696e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // manually.
697e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      GetTestState(ssl.get())->pending_session.reset(
698e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          SSL_SESSION_up_ref(session));
699d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
700d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
701d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
70253b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  if (SSL_get_current_cipher(ssl.get()) != nullptr) {
70353b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    fprintf(stderr, "non-null cipher before handshake\n");
70453b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    return false;
70553b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  }
70653b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley
707d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  int ret;
708e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->implicit_handshake) {
709d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    if (config->is_server) {
710e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_set_accept_state(ssl.get());
711d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    } else {
712e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_set_connect_state(ssl.get());
713e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
714e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  } else {
715e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    do {
716e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (config->is_server) {
717e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        ret = SSL_accept(ssl.get());
718e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      } else {
719e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        ret = SSL_connect(ssl.get());
720e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
721e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    } while (config->async && RetryAsync(ssl.get(), ret));
722e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (ret != 1) {
723e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
724d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
725d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
72653b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    if (SSL_get_current_cipher(ssl.get()) == nullptr) {
72753b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      fprintf(stderr, "null cipher after handshake\n");
72853b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      return false;
72953b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    }
73053b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley
731e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (is_resume &&
732e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        (!!SSL_session_reused(ssl.get()) == config->expect_session_miss)) {
733e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "session was%s reused\n",
734e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley              SSL_session_reused(ssl.get()) ? "" : " not");
735e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
736e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
737d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
738e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    bool expect_handshake_done = is_resume || !config->false_start;
739e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (expect_handshake_done != GetTestState(ssl.get())->handshake_done) {
740e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "handshake was%s completed\n",
741e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley              GetTestState(ssl.get())->handshake_done ? "" : " not");
742e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
743d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
744d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
745e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (config->is_server && !GetTestState(ssl.get())->early_callback_called) {
746d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      fprintf(stderr, "early callback not called\n");
747e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
748d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
749d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
750e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_server_name.empty()) {
751e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      const char *server_name =
752e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        SSL_get_servername(ssl.get(), TLSEXT_NAMETYPE_host_name);
753e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (server_name != config->expected_server_name) {
754e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "servername mismatch (got %s; want %s)\n",
755e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                server_name, config->expected_server_name.c_str());
756e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
757e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
758d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
759d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
760e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_certificate_types.empty()) {
761e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      uint8_t *certificate_types;
762e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      int num_certificate_types =
763e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        SSL_get0_certificate_types(ssl.get(), &certificate_types);
764e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (num_certificate_types !=
765e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          (int)config->expected_certificate_types.size() ||
766e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          memcmp(certificate_types,
767e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                 config->expected_certificate_types.data(),
768e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                 num_certificate_types) != 0) {
769e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "certificate types mismatch\n");
770e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
771e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
772d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
773d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
774e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_next_proto.empty()) {
775e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      const uint8_t *next_proto;
776e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      unsigned next_proto_len;
777e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_get0_next_proto_negotiated(ssl.get(), &next_proto, &next_proto_len);
778e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (next_proto_len != config->expected_next_proto.size() ||
779e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          memcmp(next_proto, config->expected_next_proto.data(),
780e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                 next_proto_len) != 0) {
781e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "negotiated next proto mismatch\n");
782e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
783e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
784d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
785d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
786e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_alpn.empty()) {
787e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      const uint8_t *alpn_proto;
788e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      unsigned alpn_proto_len;
789e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_get0_alpn_selected(ssl.get(), &alpn_proto, &alpn_proto_len);
790e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (alpn_proto_len != config->expected_alpn.size() ||
791e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          memcmp(alpn_proto, config->expected_alpn.data(),
792e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                 alpn_proto_len) != 0) {
793e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "negotiated alpn proto mismatch\n");
794e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
795e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
796d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
797e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
798e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_channel_id.empty()) {
799e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      uint8_t channel_id[64];
800e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (!SSL_get_tls_channel_id(ssl.get(), channel_id, sizeof(channel_id))) {
801e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "no channel id negotiated\n");
802e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
803e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
804e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (config->expected_channel_id.size() != 64 ||
805e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          memcmp(config->expected_channel_id.data(),
806e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                 channel_id, 64) != 0) {
807e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "channel id mismatch\n");
808e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
809e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
810d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
811d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
812e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (config->expect_extended_master_secret) {
813e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (!ssl->session->extended_master_secret) {
814e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "No EMS for session when expected");
815e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
816e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
817d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
818d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
819e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_ocsp_response.empty()) {
820e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      const uint8_t *data;
821e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      size_t len;
822e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_get0_ocsp_response(ssl.get(), &data, &len);
823e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (config->expected_ocsp_response.size() != len ||
824e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
825e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "OCSP response mismatch\n");
826e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
827e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
828d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
829d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
830e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!config->expected_signed_cert_timestamps.empty()) {
831e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      const uint8_t *data;
832e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      size_t len;
833e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      SSL_get0_signed_cert_timestamp_list(ssl.get(), &data, &len);
834e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (config->expected_signed_cert_timestamps.size() != len ||
835e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          memcmp(config->expected_signed_cert_timestamps.data(),
836e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                 data, len) != 0) {
837e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "SCT list mismatch\n");
838e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
839e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
840d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
841d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
842d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
843e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config->export_keying_material > 0) {
844e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    std::vector<uint8_t> result(
845e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        static_cast<size_t>(config->export_keying_material));
846e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (!SSL_export_keying_material(
847e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley            ssl.get(), result.data(), result.size(),
848e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley            config->export_label.data(), config->export_label.size(),
849e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley            reinterpret_cast<const uint8_t*>(config->export_context.data()),
850e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley            config->export_context.size(), config->use_export_context)) {
851e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      fprintf(stderr, "failed to export keying material\n");
852e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
853e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    }
854e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
855e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
856d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
857d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
858d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
85953b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  if (config->tls_unique) {
86053b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    uint8_t tls_unique[16];
86153b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    size_t tls_unique_len;
86253b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
86353b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley                            sizeof(tls_unique))) {
86453b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      fprintf(stderr, "failed to get tls-unique\n");
86553b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      return false;
86653b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    }
86753b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley
86853b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    if (tls_unique_len != 12) {
86953b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
87053b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley              static_cast<unsigned>(tls_unique_len));
87153b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      return false;
87253b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    }
87353b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley
87453b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
87553b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley      return false;
87653b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley    }
87753b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley  }
87853b272a2813a0b11f107d77100ff8805ada8fbd2Adam Langley
879d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (config->write_different_record_sizes) {
880d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    if (config->is_dtls) {
881d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
882e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      return false;
883d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
884d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    // This mode writes a number of different record sizes in an attempt to
885d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    // trip up the CBC record splitting code.
886d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    uint8_t buf[32769];
887d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    memset(buf, 0x42, sizeof(buf));
888d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    static const size_t kRecordSizes[] = {
889d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
890d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
891d9e397b599b13d642138480a28c14db7a136bf0Adam Langley         i++) {
892d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      const size_t len = kRecordSizes[i];
893d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      if (len > sizeof(buf)) {
894d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        fprintf(stderr, "Bad kRecordSizes value.\n");
895e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
896d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      }
897e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (WriteAll(ssl.get(), buf, len) < 0) {
898e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
899d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      }
900d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
901d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  } else {
902d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    if (config->shim_writes_first) {
903e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
904e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                   5) < 0) {
905e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
906e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
907d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
908d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    for (;;) {
909d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      uint8_t buf[512];
910e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      int n = DoRead(ssl.get(), buf, sizeof(buf));
911e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      int err = SSL_get_error(ssl.get(), n);
912d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      if (err == SSL_ERROR_ZERO_RETURN ||
913d9e397b599b13d642138480a28c14db7a136bf0Adam Langley          (n == 0 && err == SSL_ERROR_SYSCALL)) {
914d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        if (n != 0) {
915d9e397b599b13d642138480a28c14db7a136bf0Adam Langley          fprintf(stderr, "Invalid SSL_get_error output\n");
916e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          return false;
917d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        }
918e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        // Accept shutdowns with or without close_notify.
919e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        // TODO(davidben): Write tests which distinguish these two cases.
920d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        break;
921d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      } else if (err != SSL_ERROR_NONE) {
922d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        if (n > 0) {
923d9e397b599b13d642138480a28c14db7a136bf0Adam Langley          fprintf(stderr, "Invalid SSL_get_error output\n");
924e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley          return false;
925d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        }
926e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
927d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      }
928e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // Successfully read data.
929d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      if (n <= 0) {
930d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        fprintf(stderr, "Invalid SSL_get_error output\n");
931e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
932d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      }
933e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
934e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // After a successful read, with or without False Start, the handshake
935e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      // must be complete.
936e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (!GetTestState(ssl.get())->handshake_done) {
937e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        fprintf(stderr, "handshake was not completed after SSL_read\n");
938e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
939e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      }
940e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley
941d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      for (int i = 0; i < n; i++) {
942d9e397b599b13d642138480a28c14db7a136bf0Adam Langley        buf[i] ^= 0xff;
943d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      }
944e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      if (WriteAll(ssl.get(), buf, n) < 0) {
945e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley        return false;
946d9e397b599b13d642138480a28c14db7a136bf0Adam Langley      }
947d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    }
948d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
949d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
950d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (out_session) {
951e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    out_session->reset(SSL_get1_session(ssl.get()));
952d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
953d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
954e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  SSL_shutdown(ssl.get());
955e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return true;
956d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
957d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
958d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint main(int argc, char **argv) {
959e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#if defined(OPENSSL_WINDOWS)
960e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  /* Initialize Winsock. */
961e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  WORD wsa_version = MAKEWORD(2, 2);
962e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  WSADATA wsa_data;
963e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  int wsa_err = WSAStartup(wsa_version, &wsa_data);
964e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (wsa_err != 0) {
965e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
966e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return 1;
967e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
968e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (wsa_data.wVersion != wsa_version) {
969e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
970e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return 1;
971e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  }
972e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley#else
973d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  signal(SIGPIPE, SIG_IGN);
974d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#endif
975d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
976d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!SSL_library_init()) {
977d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 1;
978d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
979e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
980e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
981e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (g_config_index < 0 || g_state_index < 0) {
982d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 1;
983d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
984d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
985d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  TestConfig config;
986d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  if (!ParseConfig(argc - 1, argv + 1, &config)) {
987e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return Usage(argv[0]);
988d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
989d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
990e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
991e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!ssl_ctx) {
992e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ERR_print_errors_fp(stderr);
993d9e397b599b13d642138480a28c14db7a136bf0Adam Langley    return 1;
994d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
995d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
996e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  ScopedSSL_SESSION session;
997e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
998e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                  NULL /* session */)) {
999e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ERR_print_errors_fp(stderr);
1000e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return 1;
1001d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
1002d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
1003e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  if (config.resume &&
1004e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley      !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
1005e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley                  session.get())) {
1006e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    ERR_print_errors_fp(stderr);
1007e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley    return 1;
1008d9e397b599b13d642138480a28c14db7a136bf0Adam Langley  }
1009d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
1010e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5Adam Langley  return 0;
1011d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
1012