blob: 5fd97ca9edebbcf12138a07103fd0e2a9bdc3b6b [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
Adam Langleyded93582014-07-31 15:23:51 -070015#include <openssl/base.h>
16
17#if !defined(OPENSSL_WINDOWS)
David Benjamin025b3d32014-07-01 19:53:04 -040018#include <arpa/inet.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040019#include <netinet/in.h>
David Benjamin87c8a642015-02-21 01:54:29 -050020#include <netinet/tcp.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040021#include <signal.h>
22#include <sys/socket.h>
David Benjamin4d2e7ce2015-05-08 13:29:45 -040023#include <sys/types.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040024#include <unistd.h>
David Benjamin87c8a642015-02-21 01:54:29 -050025#else
26#include <io.h>
27#pragma warning(push, 3)
Adam Langley3e719312015-03-20 16:32:23 -070028#include <winsock2.h>
29#include <ws2tcpip.h>
David Benjamin87c8a642015-02-21 01:54:29 -050030#pragma warning(pop)
31
32#pragma comment(lib, "Ws2_32.lib")
Adam Langleyded93582014-07-31 15:23:51 -070033#endif
34
Adam Langley2b2d66d2015-01-30 17:08:37 -080035#include <string.h>
Adam Langleyded93582014-07-31 15:23:51 -070036#include <sys/types.h>
David Benjamin025b3d32014-07-01 19:53:04 -040037
David Benjamin025b3d32014-07-01 19:53:04 -040038#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040039#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040040#include <openssl/bytestring.h>
David Benjamind98452d2015-06-16 14:16:23 -040041#include <openssl/cipher.h>
Brian Smith83a82982015-04-09 16:21:10 -100042#include <openssl/err.h>
David Benjamind98452d2015-06-16 14:16:23 -040043#include <openssl/hmac.h>
44#include <openssl/rand.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040045#include <openssl/ssl.h>
46
David Benjamin45fb1be2015-03-22 16:31:27 -040047#include <memory>
David Benjaminc565ebb2015-04-03 04:06:36 -040048#include <vector>
David Benjamin45fb1be2015-03-22 16:31:27 -040049
50#include "../../crypto/test/scoped_types.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040051#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040052#include "packeted_bio.h"
David Benjamina7f333d2015-02-09 02:37:18 -050053#include "scoped_types.h"
David Benjamin5a593af2014-08-11 19:51:50 -040054#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040055
David Benjamin87c8a642015-02-21 01:54:29 -050056
57#if !defined(OPENSSL_WINDOWS)
58static int closesocket(int sock) {
59 return close(sock);
60}
61
62static void PrintSocketError(const char *func) {
63 perror(func);
64}
65#else
66static void PrintSocketError(const char *func) {
67 fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
68}
69#endif
70
David Benjaminc273d2c2015-02-09 12:59:46 -050071static int Usage(const char *program) {
David Benjamina7f333d2015-02-09 02:37:18 -050072 fprintf(stderr, "Usage: %s [flags...]\n", program);
David Benjamin1d5c83e2014-07-22 19:20:02 -040073 return 1;
74}
David Benjamin025b3d32014-07-01 19:53:04 -040075
David Benjamin2d445c02015-02-09 13:03:50 -050076struct TestState {
David Benjaminff9c74f2015-04-06 20:17:56 -040077 TestState() {
78 // MSVC cannot initialize these inline.
79 memset(&clock, 0, sizeof(clock));
80 memset(&clock_delta, 0, sizeof(clock_delta));
81 }
82
David Benjamin6c2563e2015-04-03 03:47:47 -040083 // async_bio is async BIO which pauses reads and writes.
84 BIO *async_bio = nullptr;
85 // clock is the current time for the SSL connection.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040086 timeval clock;
David Benjamin6c2563e2015-04-03 03:47:47 -040087 // clock_delta is how far the clock advanced in the most recent failed
88 // |BIO_read|.
David Benjamin4d2e7ce2015-05-08 13:29:45 -040089 timeval clock_delta;
David Benjamind9e07012015-02-09 03:04:34 -050090 ScopedEVP_PKEY channel_id;
David Benjamin0d4db502015-03-23 18:46:05 -040091 bool cert_ready = false;
David Benjamin1b8b6912015-02-09 04:28:16 -050092 ScopedSSL_SESSION session;
93 ScopedSSL_SESSION pending_session;
David Benjamin0d4db502015-03-23 18:46:05 -040094 bool early_callback_called = false;
David Benjamin87e4acd2015-04-02 19:57:35 -040095 bool handshake_done = false;
David Benjaminb4d65fd2015-05-29 17:11:21 -040096 // private_key is the underlying private key used when testing custom keys.
97 ScopedEVP_PKEY private_key;
98 std::vector<uint8_t> signature;
99 // signature_retries is the number of times an asynchronous sign operation has
100 // been retried.
101 unsigned signature_retries = 0;
David Benjaminba4594a2015-06-18 18:36:15 -0400102 bool got_new_session = false;
David Benjamind9e07012015-02-09 03:04:34 -0500103};
104
David Benjamin2d445c02015-02-09 13:03:50 -0500105static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
Adam Langley09505632015-07-30 18:10:13 -0700106 int index, long argl, void *argp) {
David Benjamin2d445c02015-02-09 13:03:50 -0500107 delete ((TestState *)ptr);
David Benjamind9e07012015-02-09 03:04:34 -0500108}
109
110static int g_config_index = 0;
David Benjamin2d445c02015-02-09 13:03:50 -0500111static int g_state_index = 0;
David Benjamin5a593af2014-08-11 19:51:50 -0400112
Adam Langley69a01602014-11-17 17:26:55 -0800113static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
David Benjamind9e07012015-02-09 03:04:34 -0500114 return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -0400115}
116
David Benjamin87e4acd2015-04-02 19:57:35 -0400117static const TestConfig *GetConfigPtr(const SSL *ssl) {
David Benjamind9e07012015-02-09 03:04:34 -0500118 return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
David Benjamin5a593af2014-08-11 19:51:50 -0400119}
120
David Benjamin2d445c02015-02-09 13:03:50 -0500121static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) {
122 if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) {
David Benjamind9e07012015-02-09 03:04:34 -0500123 async.release();
124 return true;
125 }
126 return false;
127}
128
David Benjamin87e4acd2015-04-02 19:57:35 -0400129static TestState *GetTestState(const SSL *ssl) {
David Benjamin2d445c02015-02-09 13:03:50 -0500130 return (TestState *)SSL_get_ex_data(ssl, g_state_index);
David Benjamin83f90402015-01-27 01:09:43 -0500131}
132
David Benjamina7f333d2015-02-09 02:37:18 -0500133static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
134 ScopedBIO bio(BIO_new(BIO_s_file()));
135 if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
136 return nullptr;
David Benjamina08e49d2014-08-24 01:46:07 -0400137 }
David Benjamina7f333d2015-02-09 02:37:18 -0500138 ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
David Benjamina08e49d2014-08-24 01:46:07 -0400139 return pkey;
140}
141
David Benjaminb4d65fd2015-05-29 17:11:21 -0400142static int AsyncPrivateKeyType(SSL *ssl) {
143 return EVP_PKEY_id(GetTestState(ssl)->private_key.get());
144}
145
146static int AsyncPrivateKeySupportsDigest(SSL *ssl, const EVP_MD *md) {
147 return EVP_PKEY_supports_digest(GetTestState(ssl)->private_key.get(), md);
148}
149
150static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) {
151 return EVP_PKEY_size(GetTestState(ssl)->private_key.get());
152}
153
154static ssl_private_key_result_t AsyncPrivateKeySign(
155 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
156 const EVP_MD *md, const uint8_t *in, size_t in_len) {
157 TestState *test_state = GetTestState(ssl);
158 if (!test_state->signature.empty()) {
159 fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n");
160 abort();
161 }
162
163 ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(test_state->private_key.get(),
164 nullptr));
165 if (!ctx) {
166 return ssl_private_key_failure;
167 }
168
169 // Write the signature into |test_state|.
170 size_t len = 0;
171 if (!EVP_PKEY_sign_init(ctx.get()) ||
172 !EVP_PKEY_CTX_set_signature_md(ctx.get(), md) ||
173 !EVP_PKEY_sign(ctx.get(), nullptr, &len, in, in_len)) {
174 return ssl_private_key_failure;
175 }
176 test_state->signature.resize(len);
177 if (!EVP_PKEY_sign(ctx.get(), bssl::vector_data(&test_state->signature), &len,
178 in, in_len)) {
179 return ssl_private_key_failure;
180 }
181 test_state->signature.resize(len);
182
183 // The signature will be released asynchronously in |AsyncPrivateKeySignComplete|.
184 return ssl_private_key_retry;
185}
186
187static ssl_private_key_result_t AsyncPrivateKeySignComplete(
188 SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
189 TestState *test_state = GetTestState(ssl);
190 if (test_state->signature.empty()) {
191 fprintf(stderr,
192 "AsyncPrivateKeySignComplete called without operation pending.\n");
193 abort();
194 }
195
196 if (test_state->signature_retries < 2) {
197 // Only return the signature on the second attempt, to test both incomplete
198 // |sign| and |sign_complete|.
199 return ssl_private_key_retry;
200 }
201
202 if (max_out < test_state->signature.size()) {
203 fprintf(stderr, "Output buffer too small.\n");
204 return ssl_private_key_failure;
205 }
206 memcpy(out, bssl::vector_data(&test_state->signature),
207 test_state->signature.size());
nagendra modadugu601448a2015-07-24 09:31:31 -0700208 *out_len = test_state->signature.size();
David Benjaminb4d65fd2015-05-29 17:11:21 -0400209
210 test_state->signature.clear();
211 test_state->signature_retries = 0;
212 return ssl_private_key_success;
213}
214
215static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = {
216 AsyncPrivateKeyType,
217 AsyncPrivateKeySupportsDigest,
218 AsyncPrivateKeyMaxSignatureLen,
219 AsyncPrivateKeySign,
220 AsyncPrivateKeySignComplete,
221};
222
David Benjamin41fdbcd2015-02-09 03:13:35 -0500223static bool InstallCertificate(SSL *ssl) {
224 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400225 TestState *test_state = GetTestState(ssl);
226 if (!config->key_file.empty()) {
227 if (config->use_async_private_key) {
228 test_state->private_key = LoadPrivateKey(config->key_file.c_str());
229 if (!test_state->private_key) {
230 return false;
231 }
232 SSL_set_private_key_method(ssl, &g_async_private_key_method);
233 } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
234 SSL_FILETYPE_PEM)) {
235 return false;
236 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500237 }
238 if (!config->cert_file.empty() &&
239 !SSL_use_certificate_file(ssl, config->cert_file.c_str(),
240 SSL_FILETYPE_PEM)) {
241 return false;
242 }
243 return true;
244}
245
David Benjaminc273d2c2015-02-09 12:59:46 -0500246static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin5a593af2014-08-11 19:51:50 -0400247 const TestConfig *config = GetConfigPtr(ctx->ssl);
David Benjamin2d445c02015-02-09 13:03:50 -0500248 GetTestState(ctx->ssl)->early_callback_called = true;
David Benjamin5a593af2014-08-11 19:51:50 -0400249
David Benjamin6f5c0f42015-02-24 01:23:21 -0500250 if (!config->expected_server_name.empty()) {
251 const uint8_t *extension_data;
252 size_t extension_len;
253 CBS extension, server_name_list, host_name;
254 uint8_t name_type;
255
256 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
257 &extension_data,
258 &extension_len)) {
259 fprintf(stderr, "Could not find server_name extension.\n");
260 return -1;
261 }
262
263 CBS_init(&extension, extension_data, extension_len);
264 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
265 CBS_len(&extension) != 0 ||
266 !CBS_get_u8(&server_name_list, &name_type) ||
267 name_type != TLSEXT_NAMETYPE_host_name ||
268 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
269 CBS_len(&server_name_list) != 0) {
270 fprintf(stderr, "Could not decode server_name extension.\n");
271 return -1;
272 }
273
274 if (!CBS_mem_equal(&host_name,
275 (const uint8_t*)config->expected_server_name.data(),
276 config->expected_server_name.size())) {
277 fprintf(stderr, "Server name mismatch.\n");
278 }
David Benjamin7b030512014-07-08 17:30:11 -0400279 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400280
David Benjamin6f5c0f42015-02-24 01:23:21 -0500281 if (config->fail_early_callback) {
David Benjamin7b030512014-07-08 17:30:11 -0400282 return -1;
283 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400284
David Benjamin6f5c0f42015-02-24 01:23:21 -0500285 // Install the certificate in the early callback.
286 if (config->use_early_callback) {
287 if (config->async) {
288 // Install the certificate asynchronously.
289 return 0;
290 }
291 if (!InstallCertificate(ctx->ssl)) {
292 return -1;
293 }
David Benjamin7b030512014-07-08 17:30:11 -0400294 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400295 return 1;
296}
David Benjamin025b3d32014-07-01 19:53:04 -0400297
David Benjaminc273d2c2015-02-09 12:59:46 -0500298static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400299 return 1;
300}
301
David Benjaminc273d2c2015-02-09 12:59:46 -0500302static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
303 unsigned int *out_len, void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400304 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500305 if (config->advertise_npn.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400306 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500307 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400308
David Benjamin5a593af2014-08-11 19:51:50 -0400309 *out = (const uint8_t*)config->advertise_npn.data();
310 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400311 return SSL_TLSEXT_ERR_OK;
312}
313
David Benjaminc273d2c2015-02-09 12:59:46 -0500314static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
David Benjamin8b368412015-03-14 01:54:17 -0400315 const uint8_t* in, unsigned inlen, void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400316 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500317 if (config->select_next_proto.empty()) {
David Benjamin7e3305e2014-07-28 14:52:32 -0400318 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500319 }
David Benjamin7e3305e2014-07-28 14:52:32 -0400320
David Benjamin5a593af2014-08-11 19:51:50 -0400321 *out = (uint8_t*)config->select_next_proto.data();
322 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400323 return SSL_TLSEXT_ERR_OK;
324}
325
David Benjaminc273d2c2015-02-09 12:59:46 -0500326static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
327 const uint8_t* in, unsigned inlen, void* arg) {
David Benjaminae2888f2014-09-06 12:58:58 -0400328 const TestConfig *config = GetConfigPtr(ssl);
David Benjaminc273d2c2015-02-09 12:59:46 -0500329 if (config->select_alpn.empty()) {
David Benjaminae2888f2014-09-06 12:58:58 -0400330 return SSL_TLSEXT_ERR_NOACK;
David Benjaminc273d2c2015-02-09 12:59:46 -0500331 }
David Benjaminae2888f2014-09-06 12:58:58 -0400332
333 if (!config->expected_advertised_alpn.empty() &&
334 (config->expected_advertised_alpn.size() != inlen ||
335 memcmp(config->expected_advertised_alpn.data(),
336 in, inlen) != 0)) {
337 fprintf(stderr, "bad ALPN select callback inputs\n");
338 exit(1);
339 }
340
341 *out = (const uint8_t*)config->select_alpn.data();
342 *outlen = config->select_alpn.size();
343 return SSL_TLSEXT_ERR_OK;
344}
345
David Benjaminc273d2c2015-02-09 12:59:46 -0500346static unsigned PskClientCallback(SSL *ssl, const char *hint,
347 char *out_identity,
348 unsigned max_identity_len,
349 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400350 const TestConfig *config = GetConfigPtr(ssl);
351
352 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
353 fprintf(stderr, "Server PSK hint did not match.\n");
354 return 0;
355 }
356
357 // Account for the trailing '\0' for the identity.
358 if (config->psk_identity.size() >= max_identity_len ||
359 config->psk.size() > max_psk_len) {
360 fprintf(stderr, "PSK buffers too small\n");
361 return 0;
362 }
363
364 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
365 max_identity_len);
366 memcpy(out_psk, config->psk.data(), config->psk.size());
367 return config->psk.size();
368}
369
David Benjaminc273d2c2015-02-09 12:59:46 -0500370static unsigned PskServerCallback(SSL *ssl, const char *identity,
371 uint8_t *out_psk, unsigned max_psk_len) {
David Benjamin48cae082014-10-27 01:06:24 -0400372 const TestConfig *config = GetConfigPtr(ssl);
373
374 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
375 fprintf(stderr, "Client PSK identity did not match.\n");
376 return 0;
377 }
378
379 if (config->psk.size() > max_psk_len) {
380 fprintf(stderr, "PSK buffers too small\n");
381 return 0;
382 }
383
384 memcpy(out_psk, config->psk.data(), config->psk.size());
385 return config->psk.size();
386}
387
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400388static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
David Benjamin6c2563e2015-04-03 03:47:47 -0400389 *out_clock = GetTestState(ssl)->clock;
David Benjamin377fc312015-01-26 00:22:12 -0500390}
391
David Benjaminc273d2c2015-02-09 12:59:46 -0500392static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) {
David Benjamin2d445c02015-02-09 13:03:50 -0500393 *out_pkey = GetTestState(ssl)->channel_id.release();
David Benjamind9e07012015-02-09 03:04:34 -0500394}
395
David Benjaminc273d2c2015-02-09 12:59:46 -0500396static int CertCallback(SSL *ssl, void *arg) {
David Benjamin2d445c02015-02-09 13:03:50 -0500397 if (!GetTestState(ssl)->cert_ready) {
David Benjamin41fdbcd2015-02-09 03:13:35 -0500398 return -1;
399 }
400 if (!InstallCertificate(ssl)) {
401 return 0;
402 }
403 return 1;
404}
405
David Benjaminc273d2c2015-02-09 12:59:46 -0500406static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len,
407 int *copy) {
David Benjamin2d445c02015-02-09 13:03:50 -0500408 TestState *async_state = GetTestState(ssl);
David Benjamin1b8b6912015-02-09 04:28:16 -0500409 if (async_state->session) {
410 *copy = 0;
411 return async_state->session.release();
412 } else if (async_state->pending_session) {
413 return SSL_magic_pending_session_ptr();
414 } else {
415 return NULL;
416 }
417}
418
Adam Langley524e7172015-02-20 16:04:00 -0800419static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) {
420 const TestConfig *config = GetConfigPtr(early_context->ssl);
421 static int callback_num = 0;
422
423 callback_num++;
424 if (config->fail_ddos_callback ||
425 (config->fail_second_ddos_callback && callback_num == 2)) {
426 return 0;
427 }
428 return 1;
429}
430
David Benjamin87e4acd2015-04-02 19:57:35 -0400431static void InfoCallback(const SSL *ssl, int type, int val) {
432 if (type == SSL_CB_HANDSHAKE_DONE) {
433 if (GetConfigPtr(ssl)->handshake_never_done) {
434 fprintf(stderr, "handshake completed\n");
435 // Abort before any expected error code is printed, to ensure the overall
436 // test fails.
437 abort();
438 }
439 GetTestState(ssl)->handshake_done = true;
440 }
441}
442
David Benjaminba4594a2015-06-18 18:36:15 -0400443static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
444 GetTestState(ssl)->got_new_session = true;
445 // BoringSSL passes a reference to |session|.
446 SSL_SESSION_free(session);
447 return 1;
448}
449
David Benjamind98452d2015-06-16 14:16:23 -0400450static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
451 EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
452 int encrypt) {
453 // This is just test code, so use the all-zeros key.
454 static const uint8_t kZeros[16] = {0};
455
456 if (encrypt) {
457 memcpy(key_name, kZeros, sizeof(kZeros));
458 RAND_bytes(iv, 16);
459 } else if (memcmp(key_name, kZeros, 16) != 0) {
460 return 0;
461 }
462
463 if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
464 !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
465 return -1;
466 }
467
468 if (!encrypt) {
469 return GetConfigPtr(ssl)->renew_ticket ? 2 : 1;
470 }
471 return 1;
472}
473
Adam Langley09505632015-07-30 18:10:13 -0700474// kCustomExtensionValue is the extension value that the custom extension
475// callbacks will add.
Adam Langleyc5b23a12015-07-30 18:19:26 -0700476static const uint16_t kCustomExtensionValue = 1234;
Adam Langley09505632015-07-30 18:10:13 -0700477static void *const kCustomExtensionAddArg =
478 reinterpret_cast<void *>(kCustomExtensionValue);
479static void *const kCustomExtensionParseArg =
480 reinterpret_cast<void *>(kCustomExtensionValue + 1);
481static const char kCustomExtensionContents[] = "custom extension";
482
483static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
484 const uint8_t **out, size_t *out_len,
485 int *out_alert_value, void *add_arg) {
486 if (extension_value != kCustomExtensionValue ||
487 add_arg != kCustomExtensionAddArg) {
488 abort();
489 }
490
491 if (GetConfigPtr(ssl)->custom_extension_skip) {
492 return 0;
493 }
494 if (GetConfigPtr(ssl)->custom_extension_fail_add) {
495 return -1;
496 }
497
498 *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
499 *out_len = sizeof(kCustomExtensionContents) - 1;
500
501 return 1;
502}
503
504static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
505 const uint8_t *out, void *add_arg) {
506 if (extension_value != kCustomExtensionValue ||
507 add_arg != kCustomExtensionAddArg ||
508 out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
509 abort();
510 }
511}
512
513static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
514 const uint8_t *contents,
515 size_t contents_len,
516 int *out_alert_value, void *parse_arg) {
517 if (extension_value != kCustomExtensionValue ||
518 parse_arg != kCustomExtensionParseArg) {
519 abort();
520 }
521
522 if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
523 memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
524 *out_alert_value = SSL_AD_DECODE_ERROR;
525 return 0;
526 }
527
528 return 1;
529}
530
David Benjamin87c8a642015-02-21 01:54:29 -0500531// Connect returns a new socket connected to localhost on |port| or -1 on
532// error.
533static int Connect(uint16_t port) {
534 int sock = socket(AF_INET, SOCK_STREAM, 0);
535 if (sock == -1) {
536 PrintSocketError("socket");
537 return -1;
538 }
539 int nodelay = 1;
540 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
541 reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
542 PrintSocketError("setsockopt");
543 closesocket(sock);
544 return -1;
545 }
546 sockaddr_in sin;
547 memset(&sin, 0, sizeof(sin));
548 sin.sin_family = AF_INET;
549 sin.sin_port = htons(port);
550 if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
551 PrintSocketError("inet_pton");
552 closesocket(sock);
553 return -1;
554 }
555 if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
556 sizeof(sin)) != 0) {
557 PrintSocketError("connect");
558 closesocket(sock);
559 return -1;
560 }
561 return sock;
562}
563
564class SocketCloser {
565 public:
566 explicit SocketCloser(int sock) : sock_(sock) {}
567 ~SocketCloser() {
568 // Half-close and drain the socket before releasing it. This seems to be
569 // necessary for graceful shutdown on Windows. It will also avoid write
570 // failures in the test runner.
571#if defined(OPENSSL_WINDOWS)
572 shutdown(sock_, SD_SEND);
573#else
574 shutdown(sock_, SHUT_WR);
575#endif
576 while (true) {
577 char buf[1024];
578 if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
579 break;
580 }
581 }
582 closesocket(sock_);
583 }
584
585 private:
586 const int sock_;
587};
588
David Benjaminc273d2c2015-02-09 12:59:46 -0500589static ScopedSSL_CTX SetupCtx(const TestConfig *config) {
David Benjamina7f333d2015-02-09 02:37:18 -0500590 ScopedSSL_CTX ssl_ctx(SSL_CTX_new(
591 config->is_dtls ? DTLS_method() : TLS_method()));
592 if (!ssl_ctx) {
593 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400594 }
595
David Benjamina7f333d2015-02-09 02:37:18 -0500596 if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) {
597 return nullptr;
David Benjamin025b3d32014-07-01 19:53:04 -0400598 }
599
David Benjamina7f333d2015-02-09 02:37:18 -0500600 ScopedDH dh(DH_get_2048_256(NULL));
601 if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
602 return nullptr;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400603 }
604
David Benjamin1b8b6912015-02-09 04:28:16 -0500605 if (config->async && config->is_server) {
606 // Disable the internal session cache. To test asynchronous session lookup,
607 // we use an external session cache.
608 SSL_CTX_set_session_cache_mode(
609 ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL);
David Benjaminc273d2c2015-02-09 12:59:46 -0500610 SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback);
David Benjamin1b8b6912015-02-09 04:28:16 -0500611 } else {
612 SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
613 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400614
David Benjaminc273d2c2015-02-09 12:59:46 -0500615 ssl_ctx->select_certificate_cb = SelectCertificateCallback;
David Benjamin8f2c20e2014-07-09 09:30:38 -0400616
David Benjamin1f5f62b2014-07-12 16:18:02 -0400617 SSL_CTX_set_next_protos_advertised_cb(
David Benjaminc273d2c2015-02-09 12:59:46 -0500618 ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400619 if (!config->select_next_proto.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500620 SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
David Benjamina7f333d2015-02-09 02:37:18 -0500621 NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400622 }
623
624 if (!config->select_alpn.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500625 SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400626 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400627
Adam Langley49c7af12015-07-10 14:33:46 -0700628 SSL_CTX_enable_tls_channel_id(ssl_ctx.get());
David Benjaminc273d2c2015-02-09 12:59:46 -0500629 SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback);
David Benjamina08e49d2014-08-24 01:46:07 -0400630
David Benjaminc273d2c2015-02-09 12:59:46 -0500631 ssl_ctx->current_time_cb = CurrentTimeCallback;
David Benjamin377fc312015-01-26 00:22:12 -0500632
David Benjamin87e4acd2015-04-02 19:57:35 -0400633 SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
David Benjaminba4594a2015-06-18 18:36:15 -0400634 SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
David Benjamin87e4acd2015-04-02 19:57:35 -0400635
David Benjamind98452d2015-06-16 14:16:23 -0400636 if (config->use_ticket_callback) {
637 SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
638 }
639
Adam Langley09505632015-07-30 18:10:13 -0700640 if (config->enable_client_custom_extension &&
641 !SSL_CTX_add_client_custom_ext(
642 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
643 CustomExtensionFreeCallback, kCustomExtensionAddArg,
644 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
645 return nullptr;
646 }
647
648 if (config->enable_server_custom_extension &&
649 !SSL_CTX_add_server_custom_ext(
650 ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
651 CustomExtensionFreeCallback, kCustomExtensionAddArg,
652 CustomExtensionParseCallback, kCustomExtensionParseArg)) {
653 return nullptr;
654 }
655
David Benjamin1d5c83e2014-07-22 19:20:02 -0400656 return ssl_ctx;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400657}
658
David Benjamin40f101b2015-02-20 11:23:42 -0500659// RetryAsync is called after a failed operation on |ssl| with return code
660// |ret|. If the operation should be retried, it simulates one asynchronous
David Benjamin6c2563e2015-04-03 03:47:47 -0400661// event and returns true. Otherwise it returns false.
662static bool RetryAsync(SSL *ssl, int ret) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400663 // No error; don't retry.
664 if (ret >= 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500665 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400666 }
David Benjamin83f90402015-01-27 01:09:43 -0500667
David Benjamin6c2563e2015-04-03 03:47:47 -0400668 TestState *test_state = GetTestState(ssl);
669 if (test_state->clock_delta.tv_usec != 0 ||
670 test_state->clock_delta.tv_sec != 0) {
David Benjamin83f90402015-01-27 01:09:43 -0500671 // Process the timeout and retry.
David Benjamin6c2563e2015-04-03 03:47:47 -0400672 test_state->clock.tv_usec += test_state->clock_delta.tv_usec;
673 test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000;
674 test_state->clock.tv_usec %= 1000000;
675 test_state->clock.tv_sec += test_state->clock_delta.tv_sec;
676 memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta));
David Benjamin83f90402015-01-27 01:09:43 -0500677
678 if (DTLSv1_handle_timeout(ssl) < 0) {
David Benjaminc565ebb2015-04-03 04:06:36 -0400679 fprintf(stderr, "Error retransmitting.\n");
David Benjamin40f101b2015-02-20 11:23:42 -0500680 return false;
David Benjamin83f90402015-01-27 01:09:43 -0500681 }
David Benjamin40f101b2015-02-20 11:23:42 -0500682 return true;
David Benjamin83f90402015-01-27 01:09:43 -0500683 }
684
David Benjamin43ec06f2014-08-05 02:28:57 -0400685 // See if we needed to read or write more. If so, allow one byte through on
686 // the appropriate end to maximally stress the state machine.
David Benjamind9e07012015-02-09 03:04:34 -0500687 switch (SSL_get_error(ssl, ret)) {
688 case SSL_ERROR_WANT_READ:
David Benjamin6c2563e2015-04-03 03:47:47 -0400689 AsyncBioAllowRead(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500690 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500691 case SSL_ERROR_WANT_WRITE:
David Benjamin6c2563e2015-04-03 03:47:47 -0400692 AsyncBioAllowWrite(test_state->async_bio, 1);
David Benjamin40f101b2015-02-20 11:23:42 -0500693 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500694 case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: {
695 ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id);
696 if (!pkey) {
David Benjamin40f101b2015-02-20 11:23:42 -0500697 return false;
David Benjamin9d0847a2015-02-16 03:57:55 -0500698 }
David Benjamin6c2563e2015-04-03 03:47:47 -0400699 test_state->channel_id = std::move(pkey);
David Benjamin40f101b2015-02-20 11:23:42 -0500700 return true;
David Benjamin9d0847a2015-02-16 03:57:55 -0500701 }
David Benjamin41fdbcd2015-02-09 03:13:35 -0500702 case SSL_ERROR_WANT_X509_LOOKUP:
David Benjamin6c2563e2015-04-03 03:47:47 -0400703 test_state->cert_ready = true;
David Benjamin40f101b2015-02-20 11:23:42 -0500704 return true;
David Benjamin1b8b6912015-02-09 04:28:16 -0500705 case SSL_ERROR_PENDING_SESSION:
David Benjamin6c2563e2015-04-03 03:47:47 -0400706 test_state->session = std::move(test_state->pending_session);
David Benjamin40f101b2015-02-20 11:23:42 -0500707 return true;
David Benjamin6f5c0f42015-02-24 01:23:21 -0500708 case SSL_ERROR_PENDING_CERTIFICATE:
709 // The handshake will resume without a second call to the early callback.
710 return InstallCertificate(ssl);
David Benjaminb4d65fd2015-05-29 17:11:21 -0400711 case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION:
712 test_state->signature_retries++;
713 return true;
David Benjamind9e07012015-02-09 03:04:34 -0500714 default:
David Benjamin40f101b2015-02-20 11:23:42 -0500715 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400716 }
David Benjamin025b3d32014-07-01 19:53:04 -0400717}
718
David Benjamin6c2563e2015-04-03 03:47:47 -0400719// DoRead reads from |ssl|, resolving any asynchronous operations. It returns
720// the result value of the final |SSL_read| call.
721static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
722 const TestConfig *config = GetConfigPtr(ssl);
723 int ret;
724 do {
725 ret = SSL_read(ssl, out, max_out);
726 } while (config->async && RetryAsync(ssl, ret));
727 return ret;
728}
729
730// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
731// operations. It returns the result of the final |SSL_write| call.
732static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
733 const TestConfig *config = GetConfigPtr(ssl);
734 int ret;
735 do {
736 ret = SSL_write(ssl, in, in_len);
737 if (ret > 0) {
738 in += ret;
739 in_len -= ret;
740 }
741 } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
742 return ret;
743}
744
David Benjamin91eab5c2015-06-18 18:35:46 -0400745// CheckHandshakeProperties checks, immediately after |ssl| completes its
746// initial handshake (or False Starts), whether all the properties are
747// consistent with the test configuration and invariants.
748static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
749 const TestConfig *config = GetConfigPtr(ssl);
750
751 if (SSL_get_current_cipher(ssl) == nullptr) {
752 fprintf(stderr, "null cipher after handshake\n");
753 return false;
754 }
755
756 if (is_resume &&
757 (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
758 fprintf(stderr, "session was%s reused\n",
759 SSL_session_reused(ssl) ? "" : " not");
760 return false;
761 }
762
763 bool expect_handshake_done = is_resume || !config->false_start;
764 if (expect_handshake_done != GetTestState(ssl)->handshake_done) {
765 fprintf(stderr, "handshake was%s completed\n",
766 GetTestState(ssl)->handshake_done ? "" : " not");
767 return false;
768 }
769
David Benjaminba4594a2015-06-18 18:36:15 -0400770 if (expect_handshake_done && !config->is_server) {
771 bool expect_new_session =
772 !config->expect_no_session &&
773 (!SSL_session_reused(ssl) || config->expect_ticket_renewal);
774 if (expect_new_session != GetTestState(ssl)->got_new_session) {
775 fprintf(stderr,
776 "new session was%s established, but we expected the opposite\n",
777 GetTestState(ssl)->got_new_session ? "" : " not");
778 return false;
779 }
780 }
781
David Benjamin91eab5c2015-06-18 18:35:46 -0400782 if (config->is_server && !GetTestState(ssl)->early_callback_called) {
783 fprintf(stderr, "early callback not called\n");
784 return false;
785 }
786
787 if (!config->expected_server_name.empty()) {
788 const char *server_name =
789 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
790 if (server_name != config->expected_server_name) {
791 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
792 server_name, config->expected_server_name.c_str());
793 return false;
794 }
795 }
796
797 if (!config->expected_certificate_types.empty()) {
798 uint8_t *certificate_types;
799 int num_certificate_types =
800 SSL_get0_certificate_types(ssl, &certificate_types);
801 if (num_certificate_types !=
802 (int)config->expected_certificate_types.size() ||
803 memcmp(certificate_types,
804 config->expected_certificate_types.data(),
805 num_certificate_types) != 0) {
806 fprintf(stderr, "certificate types mismatch\n");
807 return false;
808 }
809 }
810
811 if (!config->expected_next_proto.empty()) {
812 const uint8_t *next_proto;
813 unsigned next_proto_len;
814 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
815 if (next_proto_len != config->expected_next_proto.size() ||
816 memcmp(next_proto, config->expected_next_proto.data(),
817 next_proto_len) != 0) {
818 fprintf(stderr, "negotiated next proto mismatch\n");
819 return false;
820 }
821 }
822
823 if (!config->expected_alpn.empty()) {
824 const uint8_t *alpn_proto;
825 unsigned alpn_proto_len;
826 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
827 if (alpn_proto_len != config->expected_alpn.size() ||
828 memcmp(alpn_proto, config->expected_alpn.data(),
829 alpn_proto_len) != 0) {
830 fprintf(stderr, "negotiated alpn proto mismatch\n");
831 return false;
832 }
833 }
834
835 if (!config->expected_channel_id.empty()) {
836 uint8_t channel_id[64];
837 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
838 fprintf(stderr, "no channel id negotiated\n");
839 return false;
840 }
841 if (config->expected_channel_id.size() != 64 ||
842 memcmp(config->expected_channel_id.data(),
843 channel_id, 64) != 0) {
844 fprintf(stderr, "channel id mismatch\n");
845 return false;
846 }
847 }
848
849 if (config->expect_extended_master_secret) {
850 if (!ssl->session->extended_master_secret) {
851 fprintf(stderr, "No EMS for session when expected");
852 return false;
853 }
854 }
855
856 if (!config->expected_ocsp_response.empty()) {
857 const uint8_t *data;
858 size_t len;
859 SSL_get0_ocsp_response(ssl, &data, &len);
860 if (config->expected_ocsp_response.size() != len ||
861 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
862 fprintf(stderr, "OCSP response mismatch\n");
863 return false;
864 }
865 }
866
867 if (!config->expected_signed_cert_timestamps.empty()) {
868 const uint8_t *data;
869 size_t len;
870 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
871 if (config->expected_signed_cert_timestamps.size() != len ||
872 memcmp(config->expected_signed_cert_timestamps.data(),
873 data, len) != 0) {
874 fprintf(stderr, "SCT list mismatch\n");
875 return false;
876 }
877 }
878
879 if (!config->is_server) {
880 /* Clients should expect a peer certificate chain iff this was not a PSK
881 * cipher suite. */
882 if (config->psk.empty()) {
883 if (SSL_get_peer_cert_chain(ssl) == nullptr) {
884 fprintf(stderr, "Missing peer certificate chain!\n");
885 return false;
886 }
887 } else if (SSL_get_peer_cert_chain(ssl) != nullptr) {
888 fprintf(stderr, "Unexpected peer certificate chain!\n");
889 return false;
890 }
891 }
892 return true;
893}
894
David Benjamin87c8a642015-02-21 01:54:29 -0500895// DoExchange runs a test SSL exchange against the peer. On success, it returns
896// true and sets |*out_session| to the negotiated SSL session. If the test is a
897// resumption attempt, |is_resume| is true and |session| is the session from the
898// previous exchange.
David Benjamin40f101b2015-02-20 11:23:42 -0500899static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx,
900 const TestConfig *config, bool is_resume,
David Benjamin87c8a642015-02-21 01:54:29 -0500901 SSL_SESSION *session) {
David Benjamina7f333d2015-02-09 02:37:18 -0500902 ScopedSSL ssl(SSL_new(ssl_ctx));
903 if (!ssl) {
David Benjamin40f101b2015-02-20 11:23:42 -0500904 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400905 }
906
David Benjamina7f333d2015-02-09 02:37:18 -0500907 if (!SetConfigPtr(ssl.get(), config) ||
David Benjamin2d445c02015-02-09 13:03:50 -0500908 !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
David Benjamin40f101b2015-02-20 11:23:42 -0500909 return false;
Adam Langley69a01602014-11-17 17:26:55 -0800910 }
David Benjamin5a593af2014-08-11 19:51:50 -0400911
Adam Langley5f0efe02015-02-20 13:03:16 -0800912 if (config->fallback_scsv &&
913 !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
914 return false;
David Benjamin025b3d32014-07-01 19:53:04 -0400915 }
David Benjamin6f5c0f42015-02-24 01:23:21 -0500916 if (!config->use_early_callback) {
917 if (config->async) {
918 // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client.
919 SSL_set_cert_cb(ssl.get(), CertCallback, NULL);
920 } else if (!InstallCertificate(ssl.get())) {
921 return false;
922 }
David Benjamin5a593af2014-08-11 19:51:50 -0400923 }
924 if (config->require_any_client_certificate) {
David Benjamina7f333d2015-02-09 02:37:18 -0500925 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
David Benjaminc273d2c2015-02-09 12:59:46 -0500926 SkipVerify);
David Benjamin5a593af2014-08-11 19:51:50 -0400927 }
928 if (config->false_start) {
David Benjamined7c4752015-02-16 19:16:46 -0500929 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START);
David Benjamin5a593af2014-08-11 19:51:50 -0400930 }
931 if (config->cbc_record_splitting) {
David Benjamina7f333d2015-02-09 02:37:18 -0500932 SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING);
David Benjamin5a593af2014-08-11 19:51:50 -0400933 }
934 if (config->partial_write) {
David Benjamina7f333d2015-02-09 02:37:18 -0500935 SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
David Benjamin5a593af2014-08-11 19:51:50 -0400936 }
937 if (config->no_tls12) {
David Benjamina7f333d2015-02-09 02:37:18 -0500938 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
David Benjamin5a593af2014-08-11 19:51:50 -0400939 }
940 if (config->no_tls11) {
David Benjamina7f333d2015-02-09 02:37:18 -0500941 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
David Benjamin5a593af2014-08-11 19:51:50 -0400942 }
943 if (config->no_tls1) {
David Benjamina7f333d2015-02-09 02:37:18 -0500944 SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
David Benjamin5a593af2014-08-11 19:51:50 -0400945 }
946 if (config->no_ssl3) {
David Benjamina7f333d2015-02-09 02:37:18 -0500947 SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
David Benjamin5a593af2014-08-11 19:51:50 -0400948 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400949 if (config->tls_d5_bug) {
David Benjamina7f333d2015-02-09 02:37:18 -0500950 SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG);
David Benjamin5c24a1d2014-08-31 00:59:27 -0400951 }
David Benjamincff0b902015-05-15 23:09:47 -0400952 if (config->no_legacy_server_connect) {
953 SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT);
954 }
David Benjamina08e49d2014-08-24 01:46:07 -0400955 if (!config->expected_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500956 SSL_enable_tls_channel_id(ssl.get());
David Benjamina08e49d2014-08-24 01:46:07 -0400957 }
958 if (!config->send_channel_id.empty()) {
David Benjamina7f333d2015-02-09 02:37:18 -0500959 SSL_enable_tls_channel_id(ssl.get());
David Benjamind9e07012015-02-09 03:04:34 -0500960 if (!config->async) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500961 // The async case will be supplied by |ChannelIdCallback|.
David Benjamind9e07012015-02-09 03:04:34 -0500962 ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id);
963 if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500964 return false;
David Benjamind9e07012015-02-09 03:04:34 -0500965 }
David Benjamina08e49d2014-08-24 01:46:07 -0400966 }
David Benjamina08e49d2014-08-24 01:46:07 -0400967 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500968 if (!config->host_name.empty() &&
969 !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500970 return false;
David Benjamine78bfde2014-09-06 12:45:15 -0400971 }
David Benjamin9d0847a2015-02-16 03:57:55 -0500972 if (!config->advertise_alpn.empty() &&
973 SSL_set_alpn_protos(ssl.get(),
974 (const uint8_t *)config->advertise_alpn.data(),
975 config->advertise_alpn.size()) != 0) {
David Benjamin40f101b2015-02-20 11:23:42 -0500976 return false;
David Benjaminae2888f2014-09-06 12:58:58 -0400977 }
David Benjamin48cae082014-10-27 01:06:24 -0400978 if (!config->psk.empty()) {
David Benjaminc273d2c2015-02-09 12:59:46 -0500979 SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
980 SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
David Benjamin48cae082014-10-27 01:06:24 -0400981 }
David Benjamin61f95272014-11-25 01:55:35 -0500982 if (!config->psk_identity.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500983 !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500984 return false;
David Benjamin48cae082014-10-27 01:06:24 -0400985 }
David Benjamin61f95272014-11-25 01:55:35 -0500986 if (!config->srtp_profiles.empty() &&
David Benjamina7f333d2015-02-09 02:37:18 -0500987 !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500988 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500989 }
990 if (config->enable_ocsp_stapling &&
David Benjamina7f333d2015-02-09 02:37:18 -0500991 !SSL_enable_ocsp_stapling(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500992 return false;
David Benjamin61f95272014-11-25 01:55:35 -0500993 }
994 if (config->enable_signed_cert_timestamps &&
David Benjamina7f333d2015-02-09 02:37:18 -0500995 !SSL_enable_signed_cert_timestamps(ssl.get())) {
David Benjamin40f101b2015-02-20 11:23:42 -0500996 return false;
David Benjaminca6c8262014-11-15 19:06:08 -0500997 }
David Benjamin1eb367c2014-12-12 18:17:51 -0500998 if (config->min_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -0500999 SSL_set_min_version(ssl.get(), (uint16_t)config->min_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001000 }
1001 if (config->max_version != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001002 SSL_set_max_version(ssl.get(), (uint16_t)config->max_version);
David Benjamin1eb367c2014-12-12 18:17:51 -05001003 }
David Benjamin13be1de2015-01-11 16:29:36 -05001004 if (config->mtu != 0) {
David Benjamina7f333d2015-02-09 02:37:18 -05001005 SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
1006 SSL_set_mtu(ssl.get(), config->mtu);
David Benjamin13be1de2015-01-11 16:29:36 -05001007 }
Adam Langley524e7172015-02-20 16:04:00 -08001008 if (config->install_ddos_callback) {
1009 SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback);
1010 }
David Benjamin67d1fb52015-03-16 15:16:23 -04001011 if (!config->cipher.empty() &&
1012 !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) {
1013 return false;
1014 }
David Benjamin897e5e02015-05-12 17:03:54 -04001015 if (!config->reject_peer_renegotiations) {
1016 /* Renegotiations are disabled by default. */
1017 SSL_set_reject_peer_renegotiations(ssl.get(), 0);
David Benjaminb16346b2015-04-08 19:16:58 -04001018 }
David Benjamin025b3d32014-07-01 19:53:04 -04001019
David Benjamin87c8a642015-02-21 01:54:29 -05001020 int sock = Connect(config->port);
1021 if (sock == -1) {
1022 return false;
1023 }
1024 SocketCloser closer(sock);
1025
1026 ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE));
David Benjamina7f333d2015-02-09 02:37:18 -05001027 if (!bio) {
David Benjamin40f101b2015-02-20 11:23:42 -05001028 return false;
David Benjamin43ec06f2014-08-05 02:28:57 -04001029 }
David Benjamin6fd297b2014-08-11 18:43:38 -04001030 if (config->is_dtls) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001031 ScopedBIO packeted =
1032 PacketedBioCreate(&GetTestState(ssl.get())->clock_delta);
David Benjamina7f333d2015-02-09 02:37:18 -05001033 BIO_push(packeted.get(), bio.release());
1034 bio = std::move(packeted);
David Benjamin6fd297b2014-08-11 18:43:38 -04001035 }
David Benjamin5a593af2014-08-11 19:51:50 -04001036 if (config->async) {
David Benjamina7f333d2015-02-09 02:37:18 -05001037 ScopedBIO async_scoped =
David Benjaminc273d2c2015-02-09 12:59:46 -05001038 config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
David Benjamina7f333d2015-02-09 02:37:18 -05001039 BIO_push(async_scoped.get(), bio.release());
David Benjamin6c2563e2015-04-03 03:47:47 -04001040 GetTestState(ssl.get())->async_bio = async_scoped.get();
David Benjamina7f333d2015-02-09 02:37:18 -05001041 bio = std::move(async_scoped);
David Benjamin43ec06f2014-08-05 02:28:57 -04001042 }
David Benjamina7f333d2015-02-09 02:37:18 -05001043 SSL_set_bio(ssl.get(), bio.get(), bio.get());
1044 bio.release(); // SSL_set_bio takes ownership.
David Benjamin43ec06f2014-08-05 02:28:57 -04001045
David Benjamin1d5c83e2014-07-22 19:20:02 -04001046 if (session != NULL) {
David Benjamin1b8b6912015-02-09 04:28:16 -05001047 if (!config->is_server) {
1048 if (SSL_set_session(ssl.get(), session) != 1) {
David Benjamin40f101b2015-02-20 11:23:42 -05001049 return false;
David Benjamin1b8b6912015-02-09 04:28:16 -05001050 }
1051 } else if (config->async) {
1052 // The internal session cache is disabled, so install the session
1053 // manually.
David Benjamin2d445c02015-02-09 13:03:50 -05001054 GetTestState(ssl.get())->pending_session.reset(
David Benjamin1b8b6912015-02-09 04:28:16 -05001055 SSL_SESSION_up_ref(session));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001056 }
1057 }
1058
David Benjamina07c0fc2015-05-13 13:19:42 -04001059 if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1060 fprintf(stderr, "non-null cipher before handshake\n");
1061 return false;
1062 }
1063
David Benjamin1d5c83e2014-07-22 19:20:02 -04001064 int ret;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001065 if (config->implicit_handshake) {
David Benjamin5a593af2014-08-11 19:51:50 -04001066 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001067 SSL_set_accept_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001068 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001069 SSL_set_connect_state(ssl.get());
David Benjamin43ec06f2014-08-05 02:28:57 -04001070 }
David Benjamine0e7d0d2015-02-08 19:33:25 -05001071 } else {
1072 do {
1073 if (config->is_server) {
David Benjamina7f333d2015-02-09 02:37:18 -05001074 ret = SSL_accept(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001075 } else {
David Benjamina7f333d2015-02-09 02:37:18 -05001076 ret = SSL_connect(ssl.get());
David Benjamine0e7d0d2015-02-08 19:33:25 -05001077 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001078 } while (config->async && RetryAsync(ssl.get(), ret));
David Benjamin91eab5c2015-06-18 18:35:46 -04001079 if (ret != 1 ||
1080 !CheckHandshakeProperties(ssl.get(), is_resume)) {
David Benjamin40f101b2015-02-20 11:23:42 -05001081 return false;
David Benjamine0e7d0d2015-02-08 19:33:25 -05001082 }
David Benjamin025b3d32014-07-01 19:53:04 -04001083
David Benjaminba4594a2015-06-18 18:36:15 -04001084 // Reset the state to assert later that the callback isn't called in
1085 // renegotations.
1086 GetTestState(ssl.get())->got_new_session = false;
David Benjamin61f95272014-11-25 01:55:35 -05001087 }
1088
David Benjaminc565ebb2015-04-03 04:06:36 -04001089 if (config->export_keying_material > 0) {
1090 std::vector<uint8_t> result(
1091 static_cast<size_t>(config->export_keying_material));
1092 if (!SSL_export_keying_material(
1093 ssl.get(), result.data(), result.size(),
1094 config->export_label.data(), config->export_label.size(),
1095 reinterpret_cast<const uint8_t*>(config->export_context.data()),
1096 config->export_context.size(), config->use_export_context)) {
1097 fprintf(stderr, "failed to export keying material\n");
1098 return false;
1099 }
1100 if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1101 return false;
1102 }
1103 }
1104
Adam Langleyaf0e32c2015-06-03 09:57:23 -07001105 if (config->tls_unique) {
1106 uint8_t tls_unique[16];
1107 size_t tls_unique_len;
1108 if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len,
1109 sizeof(tls_unique))) {
1110 fprintf(stderr, "failed to get tls-unique\n");
1111 return false;
1112 }
1113
1114 if (tls_unique_len != 12) {
1115 fprintf(stderr, "expected 12 bytes of tls-unique but got %u",
1116 static_cast<unsigned>(tls_unique_len));
1117 return false;
1118 }
1119
1120 if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) {
1121 return false;
1122 }
1123 }
1124
David Benjamin5a593af2014-08-11 19:51:50 -04001125 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -04001126 if (config->is_dtls) {
1127 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001128 return false;
David Benjamin6fd297b2014-08-11 18:43:38 -04001129 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001130 // This mode writes a number of different record sizes in an attempt to
1131 // trip up the CBC record splitting code.
Adam Langleybc949292015-06-18 21:32:44 -07001132 static const size_t kBufLen = 32769;
1133 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1134 memset(buf.get(), 0x42, kBufLen);
Kenny Root7fdeaf12014-08-05 15:23:37 -07001135 static const size_t kRecordSizes[] = {
1136 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1137 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
1138 i++) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001139 const size_t len = kRecordSizes[i];
Adam Langleybc949292015-06-18 21:32:44 -07001140 if (len > kBufLen) {
Kenny Root7fdeaf12014-08-05 15:23:37 -07001141 fprintf(stderr, "Bad kRecordSizes value.\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001142 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001143 }
Adam Langleybc949292015-06-18 21:32:44 -07001144 if (WriteAll(ssl.get(), buf.get(), len) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001145 return false;
David Benjamin025b3d32014-07-01 19:53:04 -04001146 }
1147 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001148 } else {
David Benjamine58c4f52014-08-24 03:47:07 -04001149 if (config->shim_writes_first) {
David Benjamin6c2563e2015-04-03 03:47:47 -04001150 if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1151 5) < 0) {
1152 return false;
1153 }
David Benjamine58c4f52014-08-24 03:47:07 -04001154 }
Kenny Root7fdeaf12014-08-05 15:23:37 -07001155 for (;;) {
1156 uint8_t buf[512];
David Benjamin6c2563e2015-04-03 03:47:47 -04001157 int n = DoRead(ssl.get(), buf, sizeof(buf));
David Benjamina7f333d2015-02-09 02:37:18 -05001158 int err = SSL_get_error(ssl.get(), n);
David Benjamin9a38e922015-01-22 16:06:11 -05001159 if (err == SSL_ERROR_ZERO_RETURN ||
1160 (n == 0 && err == SSL_ERROR_SYSCALL)) {
1161 if (n != 0) {
1162 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001163 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001164 }
David Benjaminc273d2c2015-02-09 12:59:46 -05001165 // Accept shutdowns with or without close_notify.
1166 // TODO(davidben): Write tests which distinguish these two cases.
David Benjamin9a38e922015-01-22 16:06:11 -05001167 break;
1168 } else if (err != SSL_ERROR_NONE) {
1169 if (n > 0) {
1170 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001171 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001172 }
David Benjamin40f101b2015-02-20 11:23:42 -05001173 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001174 }
David Benjaminc273d2c2015-02-09 12:59:46 -05001175 // Successfully read data.
David Benjamin9a38e922015-01-22 16:06:11 -05001176 if (n <= 0) {
1177 fprintf(stderr, "Invalid SSL_get_error output\n");
David Benjamin40f101b2015-02-20 11:23:42 -05001178 return false;
David Benjamin9a38e922015-01-22 16:06:11 -05001179 }
David Benjamin87e4acd2015-04-02 19:57:35 -04001180
1181 // After a successful read, with or without False Start, the handshake
1182 // must be complete.
1183 if (!GetTestState(ssl.get())->handshake_done) {
1184 fprintf(stderr, "handshake was not completed after SSL_read\n");
1185 return false;
1186 }
1187
David Benjamin9a38e922015-01-22 16:06:11 -05001188 for (int i = 0; i < n; i++) {
1189 buf[i] ^= 0xff;
1190 }
David Benjamin6c2563e2015-04-03 03:47:47 -04001191 if (WriteAll(ssl.get(), buf, n) < 0) {
David Benjamin40f101b2015-02-20 11:23:42 -05001192 return false;
Kenny Root7fdeaf12014-08-05 15:23:37 -07001193 }
1194 }
David Benjamin025b3d32014-07-01 19:53:04 -04001195 }
1196
David Benjaminba4594a2015-06-18 18:36:15 -04001197 if (!config->is_server && !config->false_start &&
1198 !config->implicit_handshake &&
1199 GetTestState(ssl.get())->got_new_session) {
1200 fprintf(stderr, "new session was established after the handshake\n");
1201 return false;
1202 }
1203
David Benjamin1d5c83e2014-07-22 19:20:02 -04001204 if (out_session) {
David Benjamina7f333d2015-02-09 02:37:18 -05001205 out_session->reset(SSL_get1_session(ssl.get()));
David Benjamin1d5c83e2014-07-22 19:20:02 -04001206 }
1207
David Benjamina7f333d2015-02-09 02:37:18 -05001208 SSL_shutdown(ssl.get());
David Benjamin40f101b2015-02-20 11:23:42 -05001209 return true;
David Benjamin025b3d32014-07-01 19:53:04 -04001210}
David Benjamin1d5c83e2014-07-22 19:20:02 -04001211
1212int main(int argc, char **argv) {
David Benjamin87c8a642015-02-21 01:54:29 -05001213#if defined(OPENSSL_WINDOWS)
1214 /* Initialize Winsock. */
1215 WORD wsa_version = MAKEWORD(2, 2);
1216 WSADATA wsa_data;
1217 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1218 if (wsa_err != 0) {
1219 fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1220 return 1;
1221 }
1222 if (wsa_data.wVersion != wsa_version) {
1223 fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1224 return 1;
1225 }
1226#else
David Benjamin1d5c83e2014-07-22 19:20:02 -04001227 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -07001228#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -04001229
David Benjamin5a593af2014-08-11 19:51:50 -04001230 if (!SSL_library_init()) {
1231 return 1;
1232 }
David Benjamind9e07012015-02-09 03:04:34 -05001233 g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjamin2d445c02015-02-09 13:03:50 -05001234 g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
David Benjamin6c2563e2015-04-03 03:47:47 -04001235 if (g_config_index < 0 || g_state_index < 0) {
David Benjaminae3e4872014-11-18 21:52:26 -05001236 return 1;
1237 }
David Benjamin5a593af2014-08-11 19:51:50 -04001238
1239 TestConfig config;
1240 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjaminc273d2c2015-02-09 12:59:46 -05001241 return Usage(argv[0]);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001242 }
1243
David Benjaminc273d2c2015-02-09 12:59:46 -05001244 ScopedSSL_CTX ssl_ctx = SetupCtx(&config);
David Benjamina7f333d2015-02-09 02:37:18 -05001245 if (!ssl_ctx) {
Brian Smith83a82982015-04-09 16:21:10 -10001246 ERR_print_errors_fp(stderr);
David Benjamin1d5c83e2014-07-22 19:20:02 -04001247 return 1;
1248 }
1249
David Benjamina7f333d2015-02-09 02:37:18 -05001250 ScopedSSL_SESSION session;
David Benjamin40f101b2015-02-20 11:23:42 -05001251 if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001252 NULL /* session */)) {
Brian Smith83a82982015-04-09 16:21:10 -10001253 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001254 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001255 }
1256
David Benjamin40f101b2015-02-20 11:23:42 -05001257 if (config.resume &&
1258 !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */,
David Benjamin87c8a642015-02-21 01:54:29 -05001259 session.get())) {
Brian Smith83a82982015-04-09 16:21:10 -10001260 ERR_print_errors_fp(stderr);
David Benjamin40f101b2015-02-20 11:23:42 -05001261 return 1;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001262 }
1263
David Benjamina7f333d2015-02-09 02:37:18 -05001264 return 0;
David Benjamin1d5c83e2014-07-22 19:20:02 -04001265}