blob: 3d78c1cc3c0ee6e1bd364700c06b6ae25d8ab46e [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
25#include <sys/types.h>
David Benjamin025b3d32014-07-01 19:53:04 -040026
David Benjamin025b3d32014-07-01 19:53:04 -040027#include <openssl/bio.h>
David Benjamin48cae082014-10-27 01:06:24 -040028#include <openssl/buf.h>
David Benjamin8f2c20e2014-07-09 09:30:38 -040029#include <openssl/bytestring.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040030#include <openssl/ssl.h>
31
David Benjamin43ec06f2014-08-05 02:28:57 -040032#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040033#include "packeted_bio.h"
David Benjamin5a593af2014-08-11 19:51:50 -040034#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040035
David Benjamin1d5c83e2014-07-22 19:20:02 -040036static int usage(const char *program) {
David Benjamin5a593af2014-08-11 19:51:50 -040037 fprintf(stderr, "Usage: %s [flags...]\n",
David Benjamin1d5c83e2014-07-22 19:20:02 -040038 program);
39 return 1;
40}
David Benjamin025b3d32014-07-01 19:53:04 -040041
David Benjamin5a593af2014-08-11 19:51:50 -040042static int g_ex_data_index = 0;
43
Adam Langley69a01602014-11-17 17:26:55 -080044static bool SetConfigPtr(SSL *ssl, const TestConfig *config) {
45 return SSL_set_ex_data(ssl, g_ex_data_index, (void *)config) == 1;
David Benjamin5a593af2014-08-11 19:51:50 -040046}
47
48static const TestConfig *GetConfigPtr(SSL *ssl) {
49 return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index);
50}
51
David Benjamina08e49d2014-08-24 01:46:07 -040052static EVP_PKEY *LoadPrivateKey(const std::string &file) {
53 BIO *bio = BIO_new(BIO_s_file());
54 if (bio == NULL) {
55 return NULL;
56 }
57 if (!BIO_read_filename(bio, file.c_str())) {
58 BIO_free(bio);
59 return NULL;
60 }
61 EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
62 BIO_free(bio);
63 return pkey;
64}
65
David Benjamin1f5f62b2014-07-12 16:18:02 -040066static int early_callback_called = 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -040067
David Benjamin1f5f62b2014-07-12 16:18:02 -040068static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin8f2c20e2014-07-09 09:30:38 -040069 early_callback_called = 1;
70
David Benjamin5a593af2014-08-11 19:51:50 -040071 const TestConfig *config = GetConfigPtr(ctx->ssl);
72
73 if (config->expected_server_name.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -040074 return 1;
75 }
David Benjamin8f2c20e2014-07-09 09:30:38 -040076
David Benjamin7b030512014-07-08 17:30:11 -040077 const uint8_t *extension_data;
78 size_t extension_len;
79 CBS extension, server_name_list, host_name;
80 uint8_t name_type;
David Benjamin8f2c20e2014-07-09 09:30:38 -040081
David Benjamin7b030512014-07-08 17:30:11 -040082 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
83 &extension_data,
84 &extension_len)) {
85 fprintf(stderr, "Could not find server_name extension.\n");
86 return -1;
87 }
David Benjamin8f2c20e2014-07-09 09:30:38 -040088
David Benjamin7b030512014-07-08 17:30:11 -040089 CBS_init(&extension, extension_data, extension_len);
90 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
91 CBS_len(&extension) != 0 ||
92 !CBS_get_u8(&server_name_list, &name_type) ||
93 name_type != TLSEXT_NAMETYPE_host_name ||
94 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
95 CBS_len(&server_name_list) != 0) {
96 fprintf(stderr, "Could not decode server_name extension.\n");
97 return -1;
98 }
99
David Benjamin5a593af2014-08-11 19:51:50 -0400100 if (!CBS_mem_equal(&host_name,
101 (const uint8_t*)config->expected_server_name.data(),
102 config->expected_server_name.size())) {
David Benjamin7b030512014-07-08 17:30:11 -0400103 fprintf(stderr, "Server name mismatch.\n");
David Benjamin8f2c20e2014-07-09 09:30:38 -0400104 }
105
106 return 1;
107}
David Benjamin025b3d32014-07-01 19:53:04 -0400108
David Benjamin1f5f62b2014-07-12 16:18:02 -0400109static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -0400110 return 1;
111}
112
David Benjamin1f5f62b2014-07-12 16:18:02 -0400113static int next_protos_advertised_callback(SSL *ssl,
David Benjamin7e3305e2014-07-28 14:52:32 -0400114 const uint8_t **out,
115 unsigned int *out_len,
116 void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400117 const TestConfig *config = GetConfigPtr(ssl);
118 if (config->advertise_npn.empty())
David Benjamin1f5f62b2014-07-12 16:18:02 -0400119 return SSL_TLSEXT_ERR_NOACK;
120
David Benjamin5a593af2014-08-11 19:51:50 -0400121 *out = (const uint8_t*)config->advertise_npn.data();
122 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400123 return SSL_TLSEXT_ERR_OK;
124}
125
David Benjamin7e3305e2014-07-28 14:52:32 -0400126static int next_proto_select_callback(SSL* ssl,
127 uint8_t** out,
128 uint8_t* outlen,
129 const uint8_t* in,
130 unsigned inlen,
131 void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400132 const TestConfig *config = GetConfigPtr(ssl);
133 if (config->select_next_proto.empty())
David Benjamin7e3305e2014-07-28 14:52:32 -0400134 return SSL_TLSEXT_ERR_NOACK;
135
David Benjamin5a593af2014-08-11 19:51:50 -0400136 *out = (uint8_t*)config->select_next_proto.data();
137 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400138 return SSL_TLSEXT_ERR_OK;
139}
140
David Benjaminae2888f2014-09-06 12:58:58 -0400141static int alpn_select_callback(SSL* ssl,
142 const uint8_t** out,
143 uint8_t* outlen,
144 const uint8_t* in,
145 unsigned inlen,
146 void* arg) {
147 const TestConfig *config = GetConfigPtr(ssl);
148 if (config->select_alpn.empty())
149 return SSL_TLSEXT_ERR_NOACK;
150
151 if (!config->expected_advertised_alpn.empty() &&
152 (config->expected_advertised_alpn.size() != inlen ||
153 memcmp(config->expected_advertised_alpn.data(),
154 in, inlen) != 0)) {
155 fprintf(stderr, "bad ALPN select callback inputs\n");
156 exit(1);
157 }
158
159 *out = (const uint8_t*)config->select_alpn.data();
160 *outlen = config->select_alpn.size();
161 return SSL_TLSEXT_ERR_OK;
162}
163
David Benjaminfb4ea282014-08-15 13:38:15 -0400164static int cookie_generate_callback(SSL *ssl, uint8_t *cookie, size_t *cookie_len) {
David Benjamin8c881532014-11-26 00:29:45 -0500165 if (*cookie_len < 32) {
166 fprintf(stderr, "Insufficient space for cookie\n");
167 return 0;
168 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400169 *cookie_len = 32;
170 memset(cookie, 42, *cookie_len);
171 return 1;
172}
173
David Benjaminfb4ea282014-08-15 13:38:15 -0400174static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400175 if (cookie_len != 32) {
176 fprintf(stderr, "Cookie length mismatch.\n");
177 return 0;
178 }
179 for (size_t i = 0; i < cookie_len; i++) {
180 if (cookie[i] != 42) {
181 fprintf(stderr, "Cookie mismatch.\n");
182 return 0;
183 }
184 }
185 return 1;
186}
187
David Benjamin48cae082014-10-27 01:06:24 -0400188static unsigned psk_client_callback(SSL *ssl, const char *hint,
189 char *out_identity,
190 unsigned max_identity_len,
191 uint8_t *out_psk, unsigned max_psk_len) {
192 const TestConfig *config = GetConfigPtr(ssl);
193
194 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
195 fprintf(stderr, "Server PSK hint did not match.\n");
196 return 0;
197 }
198
199 // Account for the trailing '\0' for the identity.
200 if (config->psk_identity.size() >= max_identity_len ||
201 config->psk.size() > max_psk_len) {
202 fprintf(stderr, "PSK buffers too small\n");
203 return 0;
204 }
205
206 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
207 max_identity_len);
208 memcpy(out_psk, config->psk.data(), config->psk.size());
209 return config->psk.size();
210}
211
212static unsigned psk_server_callback(SSL *ssl, const char *identity,
213 uint8_t *out_psk, unsigned max_psk_len) {
214 const TestConfig *config = GetConfigPtr(ssl);
215
216 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
217 fprintf(stderr, "Client PSK identity did not match.\n");
218 return 0;
219 }
220
221 if (config->psk.size() > max_psk_len) {
222 fprintf(stderr, "PSK buffers too small\n");
223 return 0;
224 }
225
226 memcpy(out_psk, config->psk.data(), config->psk.size());
227 return config->psk.size();
228}
229
David Benjamin5a593af2014-08-11 19:51:50 -0400230static SSL_CTX *setup_ctx(const TestConfig *config) {
David Benjamin025b3d32014-07-01 19:53:04 -0400231 SSL_CTX *ssl_ctx = NULL;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400232 DH *dh = NULL;
David Benjamin025b3d32014-07-01 19:53:04 -0400233
David Benjamincde8aba2014-11-23 15:47:20 -0500234 ssl_ctx = SSL_CTX_new(config->is_dtls ? DTLS_method() : SSLv23_method());
David Benjamin025b3d32014-07-01 19:53:04 -0400235 if (ssl_ctx == NULL) {
236 goto err;
237 }
238
David Benjamin6fd297b2014-08-11 18:43:38 -0400239 if (config->is_dtls) {
240 // DTLS needs read-ahead to function on a datagram BIO.
241 //
242 // TODO(davidben): this should not be necessary. DTLS code should only
243 // expect a datagram BIO.
244 SSL_CTX_set_read_ahead(ssl_ctx, 1);
245 }
246
David Benjamin025b3d32014-07-01 19:53:04 -0400247 if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
248 goto err;
249 }
250
251 if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
252 goto err;
253 }
254
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400255 dh = DH_get_2048_256(NULL);
Adam Langley69a01602014-11-17 17:26:55 -0800256 if (dh == NULL ||
257 !SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400258 goto err;
259 }
260
David Benjamin1d5c83e2014-07-22 19:20:02 -0400261 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
262
David Benjamin8f2c20e2014-07-09 09:30:38 -0400263 ssl_ctx->select_certificate_cb = select_certificate_callback;
264
David Benjamin1f5f62b2014-07-12 16:18:02 -0400265 SSL_CTX_set_next_protos_advertised_cb(
266 ssl_ctx, next_protos_advertised_callback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400267 if (!config->select_next_proto.empty()) {
268 SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_callback, NULL);
269 }
270
271 if (!config->select_alpn.empty()) {
272 SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_callback, NULL);
273 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400274
David Benjamin6fd297b2014-08-11 18:43:38 -0400275 SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
276 SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
277
David Benjamina08e49d2014-08-24 01:46:07 -0400278 ssl_ctx->tlsext_channel_id_enabled_new = 1;
279
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400280 DH_free(dh);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400281 return ssl_ctx;
282
283 err:
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400284 if (dh != NULL) {
285 DH_free(dh);
286 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400287 if (ssl_ctx != NULL) {
288 SSL_CTX_free(ssl_ctx);
289 }
290 return NULL;
291}
292
David Benjamin43ec06f2014-08-05 02:28:57 -0400293static int retry_async(SSL *ssl, int ret, BIO *bio) {
294 // No error; don't retry.
295 if (ret >= 0) {
296 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400297 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400298 // See if we needed to read or write more. If so, allow one byte through on
299 // the appropriate end to maximally stress the state machine.
300 int err = SSL_get_error(ssl, ret);
301 if (err == SSL_ERROR_WANT_READ) {
302 async_bio_allow_read(bio, 1);
303 return 1;
304 } else if (err == SSL_ERROR_WANT_WRITE) {
305 async_bio_allow_write(bio, 1);
306 return 1;
David Benjamin025b3d32014-07-01 19:53:04 -0400307 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400308 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400309}
310
David Benjamin1d5c83e2014-07-22 19:20:02 -0400311static int do_exchange(SSL_SESSION **out_session,
312 SSL_CTX *ssl_ctx,
David Benjamin5a593af2014-08-11 19:51:50 -0400313 const TestConfig *config,
314 bool is_resume,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400315 int fd,
316 SSL_SESSION *session) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400317 early_callback_called = 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400318
David Benjamin43ec06f2014-08-05 02:28:57 -0400319 SSL *ssl = SSL_new(ssl_ctx);
David Benjamin025b3d32014-07-01 19:53:04 -0400320 if (ssl == NULL) {
321 BIO_print_errors_fp(stdout);
322 return 1;
323 }
324
Adam Langley69a01602014-11-17 17:26:55 -0800325 if (!SetConfigPtr(ssl, config)) {
326 BIO_print_errors_fp(stdout);
327 return 1;
328 }
David Benjamin5a593af2014-08-11 19:51:50 -0400329
330 if (config->fallback_scsv) {
331 if (!SSL_enable_fallback_scsv(ssl)) {
332 BIO_print_errors_fp(stdout);
David Benjamin025b3d32014-07-01 19:53:04 -0400333 return 1;
334 }
335 }
David Benjamin5a593af2014-08-11 19:51:50 -0400336 if (!config->key_file.empty()) {
337 if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
338 SSL_FILETYPE_PEM)) {
339 BIO_print_errors_fp(stdout);
340 return 1;
341 }
342 }
343 if (!config->cert_file.empty()) {
344 if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
345 SSL_FILETYPE_PEM)) {
346 BIO_print_errors_fp(stdout);
347 return 1;
348 }
349 }
350 if (config->require_any_client_certificate) {
351 SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
352 skip_verify);
353 }
354 if (config->false_start) {
355 SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
356 }
357 if (config->cbc_record_splitting) {
358 SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
359 }
360 if (config->partial_write) {
361 SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
362 }
363 if (config->no_tls12) {
364 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
365 }
366 if (config->no_tls11) {
367 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
368 }
369 if (config->no_tls1) {
370 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
371 }
372 if (config->no_ssl3) {
373 SSL_set_options(ssl, SSL_OP_NO_SSLv3);
374 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400375 if (config->cookie_exchange) {
376 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
377 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400378 if (config->tls_d5_bug) {
379 SSL_set_options(ssl, SSL_OP_TLS_D5_BUG);
380 }
David Benjaminca6554b2014-11-08 12:31:52 -0500381 if (config->allow_unsafe_legacy_renegotiation) {
382 SSL_set_options(ssl, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
383 }
David Benjamina08e49d2014-08-24 01:46:07 -0400384 if (!config->expected_channel_id.empty()) {
385 SSL_enable_tls_channel_id(ssl);
386 }
387 if (!config->send_channel_id.empty()) {
388 EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id);
389 if (pkey == NULL) {
390 BIO_print_errors_fp(stdout);
391 return 1;
392 }
393 SSL_enable_tls_channel_id(ssl);
394 if (!SSL_set1_tls_channel_id(ssl, pkey)) {
395 EVP_PKEY_free(pkey);
396 BIO_print_errors_fp(stdout);
397 return 1;
398 }
399 EVP_PKEY_free(pkey);
400 }
David Benjamine78bfde2014-09-06 12:45:15 -0400401 if (!config->host_name.empty()) {
402 SSL_set_tlsext_host_name(ssl, config->host_name.c_str());
403 }
David Benjaminae2888f2014-09-06 12:58:58 -0400404 if (!config->advertise_alpn.empty()) {
405 SSL_set_alpn_protos(ssl, (const uint8_t *)config->advertise_alpn.data(),
406 config->advertise_alpn.size());
407 }
David Benjamin48cae082014-10-27 01:06:24 -0400408 if (!config->psk.empty()) {
409 SSL_set_psk_client_callback(ssl, psk_client_callback);
410 SSL_set_psk_server_callback(ssl, psk_server_callback);
411 }
David Benjamin61f95272014-11-25 01:55:35 -0500412 if (!config->psk_identity.empty() &&
413 !SSL_use_psk_identity_hint(ssl, config->psk_identity.c_str())) {
414 BIO_print_errors_fp(stdout);
415 return 1;
David Benjamin48cae082014-10-27 01:06:24 -0400416 }
David Benjamin61f95272014-11-25 01:55:35 -0500417 if (!config->srtp_profiles.empty() &&
418 !SSL_set_srtp_profiles(ssl, config->srtp_profiles.c_str())) {
419 BIO_print_errors_fp(stdout);
420 return 1;
421 }
422 if (config->enable_ocsp_stapling &&
423 !SSL_enable_ocsp_stapling(ssl)) {
424 BIO_print_errors_fp(stdout);
425 return 1;
426 }
427 if (config->enable_signed_cert_timestamps &&
428 !SSL_enable_signed_cert_timestamps(ssl)) {
429 BIO_print_errors_fp(stdout);
430 return 1;
David Benjaminca6c8262014-11-15 19:06:08 -0500431 }
Feng Lu41aa3252014-11-21 22:47:56 -0800432 SSL_enable_fastradio_padding(ssl, config->fastradio_padding);
David Benjamin1eb367c2014-12-12 18:17:51 -0500433 if (config->min_version != 0) {
434 SSL_set_min_version(ssl, (uint16_t)config->min_version);
435 }
436 if (config->max_version != 0) {
437 SSL_set_max_version(ssl, (uint16_t)config->max_version);
438 }
David Benjamin025b3d32014-07-01 19:53:04 -0400439
David Benjamin43ec06f2014-08-05 02:28:57 -0400440 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
441 if (bio == NULL) {
442 BIO_print_errors_fp(stdout);
443 return 1;
444 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400445 if (config->is_dtls) {
446 BIO *packeted = packeted_bio_create();
447 BIO_push(packeted, bio);
448 bio = packeted;
449 }
David Benjamin5a593af2014-08-11 19:51:50 -0400450 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400451 BIO *async =
452 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400453 BIO_push(async, bio);
454 bio = async;
455 }
456 SSL_set_bio(ssl, bio, bio);
457
David Benjamin1d5c83e2014-07-22 19:20:02 -0400458 if (session != NULL) {
459 if (SSL_set_session(ssl, session) != 1) {
460 fprintf(stderr, "failed to set session\n");
461 return 2;
462 }
463 }
464
465 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400466 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400467 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400468 ret = SSL_accept(ssl);
469 } else {
470 ret = SSL_connect(ssl);
471 }
David Benjamin5a593af2014-08-11 19:51:50 -0400472 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400473 if (ret != 1) {
474 SSL_free(ssl);
475 BIO_print_errors_fp(stdout);
476 return 2;
477 }
478
David Benjamind8a3e782014-12-03 17:56:39 -0500479 if (is_resume && (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
David Benjamin01fe8202014-09-24 15:21:44 -0400480 fprintf(stderr, "session was%s reused\n",
481 SSL_session_reused(ssl) ? "" : " not");
David Benjamin1d5c83e2014-07-22 19:20:02 -0400482 return 2;
483 }
484
David Benjamin5a593af2014-08-11 19:51:50 -0400485 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400486 const char *server_name =
487 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400488 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400489 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400490 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400491 return 2;
492 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400493
494 if (!early_callback_called) {
495 fprintf(stderr, "early callback not called\n");
496 return 2;
497 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400498 }
499
David Benjamin5a593af2014-08-11 19:51:50 -0400500 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400501 uint8_t *certificate_types;
502 int num_certificate_types =
David Benjamina08e49d2014-08-24 01:46:07 -0400503 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400504 if (num_certificate_types !=
505 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400506 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400507 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400508 num_certificate_types) != 0) {
509 fprintf(stderr, "certificate types mismatch\n");
510 return 2;
511 }
512 }
513
David Benjamin5a593af2014-08-11 19:51:50 -0400514 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400515 const uint8_t *next_proto;
516 unsigned next_proto_len;
517 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400518 if (next_proto_len != config->expected_next_proto.size() ||
519 memcmp(next_proto, config->expected_next_proto.data(),
520 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400521 fprintf(stderr, "negotiated next proto mismatch\n");
522 return 2;
523 }
524 }
525
David Benjaminae2888f2014-09-06 12:58:58 -0400526 if (!config->expected_alpn.empty()) {
527 const uint8_t *alpn_proto;
528 unsigned alpn_proto_len;
529 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
530 if (alpn_proto_len != config->expected_alpn.size() ||
531 memcmp(alpn_proto, config->expected_alpn.data(),
532 alpn_proto_len) != 0) {
533 fprintf(stderr, "negotiated alpn proto mismatch\n");
534 return 2;
535 }
536 }
537
David Benjamina08e49d2014-08-24 01:46:07 -0400538 if (!config->expected_channel_id.empty()) {
539 uint8_t channel_id[64];
540 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
541 fprintf(stderr, "no channel id negotiated\n");
542 return 2;
543 }
544 if (config->expected_channel_id.size() != 64 ||
545 memcmp(config->expected_channel_id.data(),
546 channel_id, 64) != 0) {
547 fprintf(stderr, "channel id mismatch\n");
548 return 2;
549 }
550 }
551
Adam Langley75712922014-10-10 16:23:43 -0700552 if (config->expect_extended_master_secret) {
553 if (!ssl->session->extended_master_secret) {
554 fprintf(stderr, "No EMS for session when expected");
555 return 2;
556 }
557 }
558
David Benjamin61f95272014-11-25 01:55:35 -0500559 if (!config->expected_ocsp_response.empty()) {
560 const uint8_t *data;
561 size_t len;
562 SSL_get0_ocsp_response(ssl, &data, &len);
563 if (config->expected_ocsp_response.size() != len ||
564 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
565 fprintf(stderr, "OCSP response mismatch\n");
566 return 2;
567 }
568 }
569
570 if (!config->expected_signed_cert_timestamps.empty()) {
571 const uint8_t *data;
572 size_t len;
573 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
574 if (config->expected_signed_cert_timestamps.size() != len ||
575 memcmp(config->expected_signed_cert_timestamps.data(),
576 data, len) != 0) {
577 fprintf(stderr, "SCT list mismatch\n");
578 return 2;
579 }
580 }
581
Adam Langley2ae77d22014-10-28 17:29:33 -0700582 if (config->renegotiate) {
583 if (config->async) {
584 fprintf(stderr, "--renegotiate is not supported with --async.\n");
585 return 2;
586 }
587
588 SSL_renegotiate(ssl);
589
590 ret = SSL_do_handshake(ssl);
591 if (ret != 1) {
592 SSL_free(ssl);
593 BIO_print_errors_fp(stdout);
594 return 2;
595 }
596
597 SSL_set_state(ssl, SSL_ST_ACCEPT);
598 ret = SSL_do_handshake(ssl);
599 if (ret != 1) {
600 SSL_free(ssl);
601 BIO_print_errors_fp(stdout);
602 return 2;
603 }
604 }
605
David Benjamin5a593af2014-08-11 19:51:50 -0400606 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400607 if (config->is_dtls) {
608 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
609 return 6;
610 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700611 // This mode writes a number of different record sizes in an attempt to
612 // trip up the CBC record splitting code.
613 uint8_t buf[32769];
614 memset(buf, 0x42, sizeof(buf));
615 static const size_t kRecordSizes[] = {
616 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
617 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
618 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400619 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700620 const size_t len = kRecordSizes[i];
621 size_t off = 0;
622
623 if (len > sizeof(buf)) {
624 fprintf(stderr, "Bad kRecordSizes value.\n");
625 return 5;
626 }
627
David Benjamin43ec06f2014-08-05 02:28:57 -0400628 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700629 w = SSL_write(ssl, buf + off, len - off);
630 if (w > 0) {
631 off += (size_t) w;
632 }
David Benjamin5a593af2014-08-11 19:51:50 -0400633 } while ((config->async && retry_async(ssl, w, bio)) ||
634 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700635
636 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400637 SSL_free(ssl);
638 BIO_print_errors_fp(stdout);
639 return 4;
640 }
641 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700642 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400643 if (config->shim_writes_first) {
644 int w;
645 do {
646 w = SSL_write(ssl, "hello", 5);
647 } while (config->async && retry_async(ssl, w, bio));
648 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700649 for (;;) {
650 uint8_t buf[512];
651 int n;
652 do {
653 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400654 } while (config->async && retry_async(ssl, n, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700655 if (n < 0) {
656 SSL_free(ssl);
657 BIO_print_errors_fp(stdout);
658 return 3;
659 } else if (n == 0) {
660 break;
661 } else {
662 for (int i = 0; i < n; i++) {
663 buf[i] ^= 0xff;
664 }
665 int w;
666 do {
667 w = SSL_write(ssl, buf, n);
David Benjamin5a593af2014-08-11 19:51:50 -0400668 } while (config->async && retry_async(ssl, w, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700669 if (w != n) {
670 SSL_free(ssl);
671 BIO_print_errors_fp(stdout);
672 return 4;
673 }
674 }
675 }
David Benjamin025b3d32014-07-01 19:53:04 -0400676 }
677
David Benjamin1d5c83e2014-07-22 19:20:02 -0400678 if (out_session) {
679 *out_session = SSL_get1_session(ssl);
680 }
681
682 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400683 SSL_free(ssl);
684 return 0;
685}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400686
687int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700688#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400689 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700690#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400691
David Benjamin5a593af2014-08-11 19:51:50 -0400692 if (!SSL_library_init()) {
693 return 1;
694 }
695 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjaminae3e4872014-11-18 21:52:26 -0500696 if (g_ex_data_index < 0) {
697 return 1;
698 }
David Benjamin5a593af2014-08-11 19:51:50 -0400699
700 TestConfig config;
701 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400702 return usage(argv[0]);
703 }
704
David Benjamin5a593af2014-08-11 19:51:50 -0400705 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400706 if (ssl_ctx == NULL) {
707 BIO_print_errors_fp(stdout);
708 return 1;
709 }
710
Adam Langleye7bf2812014-08-19 11:36:45 -0700711 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400712 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400713 ssl_ctx, &config,
714 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400715 3 /* fd */, NULL /* session */);
716 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700717 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400718 }
719
David Benjamin5a593af2014-08-11 19:51:50 -0400720 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700721 ret = do_exchange(NULL,
722 ssl_ctx, &config,
723 true /* is_resume */,
724 4 /* fd */,
725 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400726 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700727 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400728 }
729 }
730
Adam Langleye7bf2812014-08-19 11:36:45 -0700731 ret = 0;
732
733out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400734 SSL_SESSION_free(session);
735 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700736 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400737}