blob: 37891b99d3597d7fdee9f036b5d50cbc95f544bf [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>
20#include <signal.h>
21#include <sys/socket.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040022#include <unistd.h>
Adam Langleyded93582014-07-31 15:23:51 -070023#endif
24
Adam Langley2b2d66d2015-01-30 17:08:37 -080025#include <string.h>
Adam Langleyded93582014-07-31 15:23:51 -070026#include <sys/types.h>
David Benjamin025b3d32014-07-01 19:53:04 -040027
David Benjamin025b3d32014-07-01 19:53:04 -040028#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040029#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040030#include <openssl/bytestring.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040031#include <openssl/ssl.h>
32
David Benjamin43ec06f2014-08-05 02:28:57 -040033#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040034#include "packeted_bio.h"
David Benjamin5a593af2014-08-11 19:51:50 -040035#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040036
David Benjamin1d5c83e2014-07-22 19:20:02 -040037static int usage(const char *program) {
David Benjamin5a593af2014-08-11 19:51:50 -040038 fprintf(stderr, "Usage: %s [flags...]\n",
David Benjamin1d5c83e2014-07-22 19:20:02 -040039 program);
40 return 1;
41}
David Benjamin025b3d32014-07-01 19:53:04 -040042
David Benjamin5a593af2014-08-11 19:51:50 -040043static int g_ex_data_index = 0;
44
Adam Langley69a01602014-11-17 17:26:55 -080045static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
46 return SSL_set_ex_data(ssl, g_ex_data_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -040047}
48
49static const TestConfig *GetConfigPtr(SSL *ssl) {
50 return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index);
51}
52
David Benjamina08e49d2014-08-24 01:46:07 -040053static EVP_PKEY *LoadPrivateKey(const std::string &file) {
54 BIO *bio = BIO_new(BIO_s_file());
55 if (bio == NULL) {
56 return NULL;
57 }
58 if (!BIO_read_filename(bio, file.c_str())) {
59 BIO_free(bio);
60 return NULL;
61 }
62 EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
63 BIO_free(bio);
64 return pkey;
65}
66
David Benjamin1f5f62b2014-07-12 16:18:02 -040067static int early_callback_called = 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -040068
David Benjamin1f5f62b2014-07-12 16:18:02 -040069static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin8f2c20e2014-07-09 09:30:38 -040070 early_callback_called = 1;
71
David Benjamin5a593af2014-08-11 19:51:50 -040072 const TestConfig *config = GetConfigPtr(ctx->ssl);
73
74 if (config->expected_server_name.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -040075 return 1;
76 }
David Benjamin8f2c20e2014-07-09 09:30:38 -040077
David Benjamin7b030512014-07-08 17:30:11 -040078 const uint8_t *extension_data;
79 size_t extension_len;
80 CBS extension, server_name_list, host_name;
81 uint8_t name_type;
David Benjamin8f2c20e2014-07-09 09:30:38 -040082
David Benjamin7b030512014-07-08 17:30:11 -040083 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
84 &extension_data,
85 &extension_len)) {
86 fprintf(stderr, "Could not find server_name extension.\n");
87 return -1;
88 }
David Benjamin8f2c20e2014-07-09 09:30:38 -040089
David Benjamin7b030512014-07-08 17:30:11 -040090 CBS_init(&extension, extension_data, extension_len);
91 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
92 CBS_len(&extension) != 0 ||
93 !CBS_get_u8(&server_name_list, &name_type) ||
94 name_type != TLSEXT_NAMETYPE_host_name ||
95 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
96 CBS_len(&server_name_list) != 0) {
97 fprintf(stderr, "Could not decode server_name extension.\n");
98 return -1;
99 }
100
David Benjamin5a593af2014-08-11 19:51:50 -0400101 if (!CBS_mem_equal(&host_name,
102 (const uint8_t*)config->expected_server_name.data(),
103 config->expected_server_name.size())) {
David Benjamin7b030512014-07-08 17:30:11 -0400104 fprintf(stderr, "Server name mismatch.\n");
David Benjamin8f2c20e2014-07-09 09:30:38 -0400105 }
106
107 return 1;
108}
David Benjamin025b3d32014-07-01 19:53:04 -0400109
David Benjamin1f5f62b2014-07-12 16:18:02 -0400110static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400111 return 1;
112}
113
David Benjamin1f5f62b2014-07-12 16:18:02 -0400114static int next_protos_advertised_callback(SSL *ssl,
David Benjamin7e3305e2014-07-28 14:52:32 -0400115 const uint8_t **out,
116 unsigned int *out_len,
117 void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400118 const TestConfig *config = GetConfigPtr(ssl);
119 if (config->advertise_npn.empty())
David Benjamin1f5f62b2014-07-12 16:18:02 -0400120 return SSL_TLSEXT_ERR_NOACK;
121
David Benjamin5a593af2014-08-11 19:51:50 -0400122 *out = (const uint8_t*)config->advertise_npn.data();
123 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400124 return SSL_TLSEXT_ERR_OK;
125}
126
David Benjamin7e3305e2014-07-28 14:52:32 -0400127static int next_proto_select_callback(SSL* ssl,
128 uint8_t** out,
129 uint8_t* outlen,
130 const uint8_t* in,
131 unsigned inlen,
132 void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400133 const TestConfig *config = GetConfigPtr(ssl);
134 if (config->select_next_proto.empty())
David Benjamin7e3305e2014-07-28 14:52:32 -0400135 return SSL_TLSEXT_ERR_NOACK;
136
David Benjamin5a593af2014-08-11 19:51:50 -0400137 *out = (uint8_t*)config->select_next_proto.data();
138 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400139 return SSL_TLSEXT_ERR_OK;
140}
141
David Benjaminae2888f2014-09-06 12:58:58 -0400142static int alpn_select_callback(SSL* ssl,
143 const uint8_t** out,
144 uint8_t* outlen,
145 const uint8_t* in,
146 unsigned inlen,
147 void* arg) {
148 const TestConfig *config = GetConfigPtr(ssl);
149 if (config->select_alpn.empty())
150 return SSL_TLSEXT_ERR_NOACK;
151
152 if (!config->expected_advertised_alpn.empty() &&
153 (config->expected_advertised_alpn.size() != inlen ||
154 memcmp(config->expected_advertised_alpn.data(),
155 in, inlen) != 0)) {
156 fprintf(stderr, "bad ALPN select callback inputs\n");
157 exit(1);
158 }
159
160 *out = (const uint8_t*)config->select_alpn.data();
161 *outlen = config->select_alpn.size();
162 return SSL_TLSEXT_ERR_OK;
163}
164
David Benjaminfb4ea282014-08-15 13:38:15 -0400165static int cookie_generate_callback(SSL *ssl, uint8_t *cookie, size_t *cookie_len) {
David Benjamin8c881532014-11-26 00:29:45 -0500166 if (*cookie_len < 32) {
167 fprintf(stderr, "Insufficient space for cookie\n");
168 return 0;
169 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400170 *cookie_len = 32;
171 memset(cookie, 42, *cookie_len);
172 return 1;
173}
174
David Benjaminfb4ea282014-08-15 13:38:15 -0400175static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400176 if (cookie_len != 32) {
177 fprintf(stderr, "Cookie length mismatch.\n");
178 return 0;
179 }
180 for (size_t i = 0; i < cookie_len; i++) {
181 if (cookie[i] != 42) {
182 fprintf(stderr, "Cookie mismatch.\n");
183 return 0;
184 }
185 }
186 return 1;
187}
188
David Benjamin48cae082014-10-27 01:06:24 -0400189static unsigned psk_client_callback(SSL *ssl, const char *hint,
190 char *out_identity,
191 unsigned max_identity_len,
192 uint8_t *out_psk, unsigned max_psk_len) {
193 const TestConfig *config = GetConfigPtr(ssl);
194
195 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
196 fprintf(stderr, "Server PSK hint did not match.\n");
197 return 0;
198 }
199
200 // Account for the trailing '\0' for the identity.
201 if (config->psk_identity.size() >= max_identity_len ||
202 config->psk.size() > max_psk_len) {
203 fprintf(stderr, "PSK buffers too small\n");
204 return 0;
205 }
206
207 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
208 max_identity_len);
209 memcpy(out_psk, config->psk.data(), config->psk.size());
210 return config->psk.size();
211}
212
213static unsigned psk_server_callback(SSL *ssl, const char *identity,
214 uint8_t *out_psk, unsigned max_psk_len) {
215 const TestConfig *config = GetConfigPtr(ssl);
216
217 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
218 fprintf(stderr, "Client PSK identity did not match.\n");
219 return 0;
220 }
221
222 if (config->psk.size() > max_psk_len) {
223 fprintf(stderr, "PSK buffers too small\n");
224 return 0;
225 }
226
227 memcpy(out_psk, config->psk.data(), config->psk.size());
228 return config->psk.size();
229}
230
David Benjamin5a593af2014-08-11 19:51:50 -0400231static SSL_CTX *setup_ctx(const TestConfig *config) {
David Benjamin025b3d32014-07-01 19:53:04 -0400232 SSL_CTX *ssl_ctx = NULL;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400233 DH *dh = NULL;
David Benjamin025b3d32014-07-01 19:53:04 -0400234
David Benjamin82c9e902014-12-12 15:55:27 -0500235 ssl_ctx = SSL_CTX_new(config->is_dtls ? DTLS_method() : TLS_method());
David Benjamin025b3d32014-07-01 19:53:04 -0400236 if (ssl_ctx == NULL) {
237 goto err;
238 }
239
David Benjamin6fd297b2014-08-11 18:43:38 -0400240 if (config->is_dtls) {
241 // DTLS needs read-ahead to function on a datagram BIO.
242 //
243 // TODO(davidben): this should not be necessary. DTLS code should only
244 // expect a datagram BIO.
245 SSL_CTX_set_read_ahead(ssl_ctx, 1);
246 }
247
David Benjamin025b3d32014-07-01 19:53:04 -0400248 if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
249 goto err;
250 }
251
252 if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
253 goto err;
254 }
255
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400256 dh = DH_get_2048_256(NULL);
Adam Langley69a01602014-11-17 17:26:55 -0800257 if (dh == NULL ||
258 !SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400259 goto err;
260 }
261
David Benjamin1d5c83e2014-07-22 19:20:02 -0400262 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
263
David Benjamin8f2c20e2014-07-09 09:30:38 -0400264 ssl_ctx->select_certificate_cb = select_certificate_callback;
265
David Benjamin1f5f62b2014-07-12 16:18:02 -0400266 SSL_CTX_set_next_protos_advertised_cb(
267 ssl_ctx, next_protos_advertised_callback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400268 if (!config->select_next_proto.empty()) {
269 SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_callback, NULL);
270 }
271
272 if (!config->select_alpn.empty()) {
273 SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_callback, NULL);
274 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400275
David Benjamin6fd297b2014-08-11 18:43:38 -0400276 SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
277 SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
278
David Benjamina08e49d2014-08-24 01:46:07 -0400279 ssl_ctx->tlsext_channel_id_enabled_new = 1;
280
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400281 DH_free(dh);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400282 return ssl_ctx;
283
284 err:
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400285 if (dh != NULL) {
286 DH_free(dh);
287 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400288 if (ssl_ctx != NULL) {
289 SSL_CTX_free(ssl_ctx);
290 }
291 return NULL;
292}
293
David Benjamin43ec06f2014-08-05 02:28:57 -0400294static int retry_async(SSL *ssl, int ret, BIO *bio) {
295 // No error; don't retry.
296 if (ret >= 0) {
297 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400298 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400299 // See if we needed to read or write more. If so, allow one byte through on
300 // the appropriate end to maximally stress the state machine.
301 int err = SSL_get_error(ssl, ret);
302 if (err == SSL_ERROR_WANT_READ) {
303 async_bio_allow_read(bio, 1);
304 return 1;
305 } else if (err == SSL_ERROR_WANT_WRITE) {
306 async_bio_allow_write(bio, 1);
307 return 1;
David Benjamin025b3d32014-07-01 19:53:04 -0400308 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400309 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400310}
311
David Benjamin1d5c83e2014-07-22 19:20:02 -0400312static int do_exchange(SSL_SESSION **out_session,
313 SSL_CTX *ssl_ctx,
David Benjamin5a593af2014-08-11 19:51:50 -0400314 const TestConfig *config,
315 bool is_resume,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400316 int fd,
317 SSL_SESSION *session) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400318 early_callback_called = 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400319
David Benjamin43ec06f2014-08-05 02:28:57 -0400320 SSL *ssl = SSL_new(ssl_ctx);
David Benjamin025b3d32014-07-01 19:53:04 -0400321 if (ssl == NULL) {
322 BIO_print_errors_fp(stdout);
323 return 1;
324 }
325
Adam Langley69a01602014-11-17 17:26:55 -0800326 if (!SetConfigPtr(ssl, config)) {
327 BIO_print_errors_fp(stdout);
328 return 1;
329 }
David Benjamin5a593af2014-08-11 19:51:50 -0400330
331 if (config->fallback_scsv) {
332 if (!SSL_enable_fallback_scsv(ssl)) {
333 BIO_print_errors_fp(stdout);
David Benjamin025b3d32014-07-01 19:53:04 -0400334 return 1;
335 }
336 }
David Benjamin5a593af2014-08-11 19:51:50 -0400337 if (!config->key_file.empty()) {
338 if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
339 SSL_FILETYPE_PEM)) {
340 BIO_print_errors_fp(stdout);
341 return 1;
342 }
343 }
344 if (!config->cert_file.empty()) {
345 if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
346 SSL_FILETYPE_PEM)) {
347 BIO_print_errors_fp(stdout);
348 return 1;
349 }
350 }
351 if (config->require_any_client_certificate) {
352 SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
353 skip_verify);
354 }
355 if (config->false_start) {
356 SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
357 }
358 if (config->cbc_record_splitting) {
359 SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
360 }
361 if (config->partial_write) {
362 SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
363 }
364 if (config->no_tls12) {
365 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
366 }
367 if (config->no_tls11) {
368 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
369 }
370 if (config->no_tls1) {
371 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
372 }
373 if (config->no_ssl3) {
374 SSL_set_options(ssl, SSL_OP_NO_SSLv3);
375 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400376 if (config->cookie_exchange) {
377 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
378 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400379 if (config->tls_d5_bug) {
380 SSL_set_options(ssl, SSL_OP_TLS_D5_BUG);
381 }
David Benjaminca6554b2014-11-08 12:31:52 -0500382 if (config->allow_unsafe_legacy_renegotiation) {
383 SSL_set_options(ssl, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
384 }
David Benjamina08e49d2014-08-24 01:46:07 -0400385 if (!config->expected_channel_id.empty()) {
386 SSL_enable_tls_channel_id(ssl);
387 }
388 if (!config->send_channel_id.empty()) {
389 EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id);
390 if (pkey == NULL) {
391 BIO_print_errors_fp(stdout);
392 return 1;
393 }
394 SSL_enable_tls_channel_id(ssl);
395 if (!SSL_set1_tls_channel_id(ssl, pkey)) {
396 EVP_PKEY_free(pkey);
397 BIO_print_errors_fp(stdout);
398 return 1;
399 }
400 EVP_PKEY_free(pkey);
401 }
David Benjamine78bfde2014-09-06 12:45:15 -0400402 if (!config->host_name.empty()) {
403 SSL_set_tlsext_host_name(ssl, config->host_name.c_str());
404 }
David Benjaminae2888f2014-09-06 12:58:58 -0400405 if (!config->advertise_alpn.empty()) {
406 SSL_set_alpn_protos(ssl, (const uint8_t *)config->advertise_alpn.data(),
407 config->advertise_alpn.size());
408 }
David Benjamin48cae082014-10-27 01:06:24 -0400409 if (!config->psk.empty()) {
410 SSL_set_psk_client_callback(ssl, psk_client_callback);
411 SSL_set_psk_server_callback(ssl, psk_server_callback);
412 }
David Benjamin61f95272014-11-25 01:55:35 -0500413 if (!config->psk_identity.empty() &&
414 !SSL_use_psk_identity_hint(ssl, config->psk_identity.c_str())) {
415 BIO_print_errors_fp(stdout);
416 return 1;
David Benjamin48cae082014-10-27 01:06:24 -0400417 }
David Benjamin61f95272014-11-25 01:55:35 -0500418 if (!config->srtp_profiles.empty() &&
419 !SSL_set_srtp_profiles(ssl, config->srtp_profiles.c_str())) {
420 BIO_print_errors_fp(stdout);
421 return 1;
422 }
423 if (config->enable_ocsp_stapling &&
424 !SSL_enable_ocsp_stapling(ssl)) {
425 BIO_print_errors_fp(stdout);
426 return 1;
427 }
428 if (config->enable_signed_cert_timestamps &&
429 !SSL_enable_signed_cert_timestamps(ssl)) {
430 BIO_print_errors_fp(stdout);
431 return 1;
David Benjaminca6c8262014-11-15 19:06:08 -0500432 }
Feng Lu41aa3252014-11-21 22:47:56 -0800433 SSL_enable_fastradio_padding(ssl, config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500434 if (config->min_version != 0) {
435 SSL_set_min_version(ssl, (uint16_t)config->min_version);
436 }
437 if (config->max_version != 0) {
438 SSL_set_max_version(ssl, (uint16_t)config->max_version);
439 }
David Benjamin13be1de2015-01-11 16:29:36 -0500440 if (config->mtu != 0) {
441 SSL_set_options(ssl, SSL_OP_NO_QUERY_MTU);
442 SSL_set_mtu(ssl, config->mtu);
443 }
David Benjamin025b3d32014-07-01 19:53:04 -0400444
David Benjamin43ec06f2014-08-05 02:28:57 -0400445 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
446 if (bio == NULL) {
447 BIO_print_errors_fp(stdout);
448 return 1;
449 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400450 if (config->is_dtls) {
451 BIO *packeted = packeted_bio_create();
452 BIO_push(packeted, bio);
453 bio = packeted;
454 }
David Benjamin5a593af2014-08-11 19:51:50 -0400455 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400456 BIO *async =
457 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400458 BIO_push(async, bio);
459 bio = async;
460 }
461 SSL_set_bio(ssl, bio, bio);
462
David Benjamin1d5c83e2014-07-22 19:20:02 -0400463 if (session != NULL) {
464 if (SSL_set_session(ssl, session) != 1) {
465 fprintf(stderr, "failed to set session\n");
466 return 2;
467 }
468 }
469
470 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400471 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400472 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400473 ret = SSL_accept(ssl);
474 } else {
475 ret = SSL_connect(ssl);
476 }
David Benjamin5a593af2014-08-11 19:51:50 -0400477 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400478 if (ret != 1) {
479 SSL_free(ssl);
480 BIO_print_errors_fp(stdout);
481 return 2;
482 }
483
David Benjamind8a3e782014-12-03 17:56:39 -0500484 if (is_resume && (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
David Benjamin01fe8202014-09-24 15:21:44 -0400485 fprintf(stderr, "session was%s reused\n",
486 SSL_session_reused(ssl) ? "" : " not");
David Benjamin1d5c83e2014-07-22 19:20:02 -0400487 return 2;
488 }
489
David Benjamin5a593af2014-08-11 19:51:50 -0400490 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400491 const char *server_name =
492 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400493 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400494 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400495 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400496 return 2;
497 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400498
499 if (!early_callback_called) {
500 fprintf(stderr, "early callback not called\n");
501 return 2;
502 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400503 }
504
David Benjamin5a593af2014-08-11 19:51:50 -0400505 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400506 uint8_t *certificate_types;
507 int num_certificate_types =
David Benjamina08e49d2014-08-24 01:46:07 -0400508 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400509 if (num_certificate_types !=
510 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400511 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400512 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400513 num_certificate_types) != 0) {
514 fprintf(stderr, "certificate types mismatch\n");
515 return 2;
516 }
517 }
518
David Benjamin5a593af2014-08-11 19:51:50 -0400519 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400520 const uint8_t *next_proto;
521 unsigned next_proto_len;
522 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400523 if (next_proto_len != config->expected_next_proto.size() ||
524 memcmp(next_proto, config->expected_next_proto.data(),
525 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400526 fprintf(stderr, "negotiated next proto mismatch\n");
527 return 2;
528 }
529 }
530
David Benjaminae2888f2014-09-06 12:58:58 -0400531 if (!config->expected_alpn.empty()) {
532 const uint8_t *alpn_proto;
533 unsigned alpn_proto_len;
534 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
535 if (alpn_proto_len != config->expected_alpn.size() ||
536 memcmp(alpn_proto, config->expected_alpn.data(),
537 alpn_proto_len) != 0) {
538 fprintf(stderr, "negotiated alpn proto mismatch\n");
539 return 2;
540 }
541 }
542
David Benjamina08e49d2014-08-24 01:46:07 -0400543 if (!config->expected_channel_id.empty()) {
544 uint8_t channel_id[64];
545 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
546 fprintf(stderr, "no channel id negotiated\n");
547 return 2;
548 }
549 if (config->expected_channel_id.size() != 64 ||
550 memcmp(config->expected_channel_id.data(),
551 channel_id, 64) != 0) {
552 fprintf(stderr, "channel id mismatch\n");
553 return 2;
554 }
555 }
556
Adam Langley75712922014-10-10 16:23:43 -0700557 if (config->expect_extended_master_secret) {
558 if (!ssl->session->extended_master_secret) {
559 fprintf(stderr, "No EMS for session when expected");
560 return 2;
561 }
562 }
563
David Benjamin61f95272014-11-25 01:55:35 -0500564 if (!config->expected_ocsp_response.empty()) {
565 const uint8_t *data;
566 size_t len;
567 SSL_get0_ocsp_response(ssl, &data, &len);
568 if (config->expected_ocsp_response.size() != len ||
569 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
570 fprintf(stderr, "OCSP response mismatch\n");
571 return 2;
572 }
573 }
574
575 if (!config->expected_signed_cert_timestamps.empty()) {
576 const uint8_t *data;
577 size_t len;
578 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
579 if (config->expected_signed_cert_timestamps.size() != len ||
580 memcmp(config->expected_signed_cert_timestamps.data(),
581 data, len) != 0) {
582 fprintf(stderr, "SCT list mismatch\n");
583 return 2;
584 }
585 }
586
Adam Langley2ae77d22014-10-28 17:29:33 -0700587 if (config->renegotiate) {
588 if (config->async) {
589 fprintf(stderr, "--renegotiate is not supported with --async.\n");
590 return 2;
591 }
592
593 SSL_renegotiate(ssl);
594
595 ret = SSL_do_handshake(ssl);
596 if (ret != 1) {
597 SSL_free(ssl);
598 BIO_print_errors_fp(stdout);
599 return 2;
600 }
601
602 SSL_set_state(ssl, SSL_ST_ACCEPT);
603 ret = SSL_do_handshake(ssl);
604 if (ret != 1) {
605 SSL_free(ssl);
606 BIO_print_errors_fp(stdout);
607 return 2;
608 }
609 }
610
David Benjamin5a593af2014-08-11 19:51:50 -0400611 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400612 if (config->is_dtls) {
613 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
614 return 6;
615 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700616 // This mode writes a number of different record sizes in an attempt to
617 // trip up the CBC record splitting code.
618 uint8_t buf[32769];
619 memset(buf, 0x42, sizeof(buf));
620 static const size_t kRecordSizes[] = {
621 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
622 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
623 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400624 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700625 const size_t len = kRecordSizes[i];
626 size_t off = 0;
627
628 if (len > sizeof(buf)) {
629 fprintf(stderr, "Bad kRecordSizes value.\n");
630 return 5;
631 }
632
David Benjamin43ec06f2014-08-05 02:28:57 -0400633 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700634 w = SSL_write(ssl, buf + off, len - off);
635 if (w > 0) {
636 off += (size_t) w;
637 }
David Benjamin5a593af2014-08-11 19:51:50 -0400638 } while ((config->async && retry_async(ssl, w, bio)) ||
639 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700640
641 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400642 SSL_free(ssl);
643 BIO_print_errors_fp(stdout);
644 return 4;
645 }
646 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700647 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400648 if (config->shim_writes_first) {
649 int w;
650 do {
651 w = SSL_write(ssl, "hello", 5);
652 } while (config->async && retry_async(ssl, w, bio));
653 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700654 for (;;) {
655 uint8_t buf[512];
656 int n;
657 do {
658 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400659 } while (config->async && retry_async(ssl, n, bio));
David Benjamin9a38e922015-01-22 16:06:11 -0500660 int err = SSL_get_error(ssl, n);
661 if (err == SSL_ERROR_ZERO_RETURN ||
662 (n == 0 && err == SSL_ERROR_SYSCALL)) {
663 if (n != 0) {
664 fprintf(stderr, "Invalid SSL_get_error output\n");
665 return 3;
666 }
667 /* Accept shutdowns with or without close_notify.
668 * TODO(davidben): Write tests which distinguish these two cases. */
669 break;
670 } else if (err != SSL_ERROR_NONE) {
671 if (n > 0) {
672 fprintf(stderr, "Invalid SSL_get_error output\n");
673 return 3;
674 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700675 SSL_free(ssl);
676 BIO_print_errors_fp(stdout);
677 return 3;
David Benjamin9a38e922015-01-22 16:06:11 -0500678 }
679 /* Successfully read data. */
680 if (n <= 0) {
681 fprintf(stderr, "Invalid SSL_get_error output\n");
682 return 3;
683 }
684 for (int i = 0; i < n; i++) {
685 buf[i] ^= 0xff;
686 }
687 int w;
688 do {
689 w = SSL_write(ssl, buf, n);
690 } while (config->async && retry_async(ssl, w, bio));
691 if (w != n) {
692 SSL_free(ssl);
693 BIO_print_errors_fp(stdout);
694 return 4;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700695 }
696 }
David Benjamin025b3d32014-07-01 19:53:04 -0400697 }
698
David Benjamin1d5c83e2014-07-22 19:20:02 -0400699 if (out_session) {
700 *out_session = SSL_get1_session(ssl);
701 }
702
703 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400704 SSL_free(ssl);
705 return 0;
706}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400707
708int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700709#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400710 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700711#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400712
David Benjamin5a593af2014-08-11 19:51:50 -0400713 if (!SSL_library_init()) {
714 return 1;
715 }
716 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjaminae3e4872014-11-18 21:52:26 -0500717 if (g_ex_data_index < 0) {
718 return 1;
719 }
David Benjamin5a593af2014-08-11 19:51:50 -0400720
721 TestConfig config;
722 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400723 return usage(argv[0]);
724 }
725
David Benjamin5a593af2014-08-11 19:51:50 -0400726 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400727 if (ssl_ctx == NULL) {
728 BIO_print_errors_fp(stdout);
729 return 1;
730 }
731
Adam Langleye7bf2812014-08-19 11:36:45 -0700732 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400733 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400734 ssl_ctx, &config,
735 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400736 3 /* fd */, NULL /* session */);
737 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700738 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400739 }
740
David Benjamin5a593af2014-08-11 19:51:50 -0400741 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700742 ret = do_exchange(NULL,
743 ssl_ctx, &config,
744 true /* is_resume */,
745 4 /* fd */,
746 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400747 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700748 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400749 }
750 }
751
Adam Langleye7bf2812014-08-19 11:36:45 -0700752 ret = 0;
753
754out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400755 SSL_SESSION_free(session);
756 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700757 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400758}