blob: 19355afdc373fd32d94f5895d25eeebea55460e9 [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 Benjamin82c9e902014-12-12 15:55:27 -0500234 ssl_ctx = SSL_CTX_new(config->is_dtls ? DTLS_method() : TLS_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 Benjamin13be1de2015-01-11 16:29:36 -0500439 if (config->mtu != 0) {
440 SSL_set_options(ssl, SSL_OP_NO_QUERY_MTU);
441 SSL_set_mtu(ssl, config->mtu);
442 }
David Benjamin025b3d32014-07-01 19:53:04 -0400443
David Benjamin43ec06f2014-08-05 02:28:57 -0400444 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
445 if (bio == NULL) {
446 BIO_print_errors_fp(stdout);
447 return 1;
448 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400449 if (config->is_dtls) {
450 BIO *packeted = packeted_bio_create();
451 BIO_push(packeted, bio);
452 bio = packeted;
453 }
David Benjamin5a593af2014-08-11 19:51:50 -0400454 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400455 BIO *async =
456 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400457 BIO_push(async, bio);
458 bio = async;
459 }
460 SSL_set_bio(ssl, bio, bio);
461
David Benjamin1d5c83e2014-07-22 19:20:02 -0400462 if (session != NULL) {
463 if (SSL_set_session(ssl, session) != 1) {
464 fprintf(stderr, "failed to set session\n");
465 return 2;
466 }
467 }
468
469 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400470 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400471 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400472 ret = SSL_accept(ssl);
473 } else {
474 ret = SSL_connect(ssl);
475 }
David Benjamin5a593af2014-08-11 19:51:50 -0400476 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400477 if (ret != 1) {
478 SSL_free(ssl);
479 BIO_print_errors_fp(stdout);
480 return 2;
481 }
482
David Benjamind8a3e782014-12-03 17:56:39 -0500483 if (is_resume && (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
David Benjamin01fe8202014-09-24 15:21:44 -0400484 fprintf(stderr, "session was%s reused\n",
485 SSL_session_reused(ssl) ? "" : " not");
David Benjamin1d5c83e2014-07-22 19:20:02 -0400486 return 2;
487 }
488
David Benjamin5a593af2014-08-11 19:51:50 -0400489 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400490 const char *server_name =
491 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400492 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400493 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400494 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400495 return 2;
496 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400497
498 if (!early_callback_called) {
499 fprintf(stderr, "early callback not called\n");
500 return 2;
501 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400502 }
503
David Benjamin5a593af2014-08-11 19:51:50 -0400504 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400505 uint8_t *certificate_types;
506 int num_certificate_types =
David Benjamina08e49d2014-08-24 01:46:07 -0400507 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400508 if (num_certificate_types !=
509 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400510 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400511 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400512 num_certificate_types) != 0) {
513 fprintf(stderr, "certificate types mismatch\n");
514 return 2;
515 }
516 }
517
David Benjamin5a593af2014-08-11 19:51:50 -0400518 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400519 const uint8_t *next_proto;
520 unsigned next_proto_len;
521 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400522 if (next_proto_len != config->expected_next_proto.size() ||
523 memcmp(next_proto, config->expected_next_proto.data(),
524 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400525 fprintf(stderr, "negotiated next proto mismatch\n");
526 return 2;
527 }
528 }
529
David Benjaminae2888f2014-09-06 12:58:58 -0400530 if (!config->expected_alpn.empty()) {
531 const uint8_t *alpn_proto;
532 unsigned alpn_proto_len;
533 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
534 if (alpn_proto_len != config->expected_alpn.size() ||
535 memcmp(alpn_proto, config->expected_alpn.data(),
536 alpn_proto_len) != 0) {
537 fprintf(stderr, "negotiated alpn proto mismatch\n");
538 return 2;
539 }
540 }
541
David Benjamina08e49d2014-08-24 01:46:07 -0400542 if (!config->expected_channel_id.empty()) {
543 uint8_t channel_id[64];
544 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
545 fprintf(stderr, "no channel id negotiated\n");
546 return 2;
547 }
548 if (config->expected_channel_id.size() != 64 ||
549 memcmp(config->expected_channel_id.data(),
550 channel_id, 64) != 0) {
551 fprintf(stderr, "channel id mismatch\n");
552 return 2;
553 }
554 }
555
Adam Langley75712922014-10-10 16:23:43 -0700556 if (config->expect_extended_master_secret) {
557 if (!ssl->session->extended_master_secret) {
558 fprintf(stderr, "No EMS for session when expected");
559 return 2;
560 }
561 }
562
David Benjamin61f95272014-11-25 01:55:35 -0500563 if (!config->expected_ocsp_response.empty()) {
564 const uint8_t *data;
565 size_t len;
566 SSL_get0_ocsp_response(ssl, &data, &len);
567 if (config->expected_ocsp_response.size() != len ||
568 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
569 fprintf(stderr, "OCSP response mismatch\n");
570 return 2;
571 }
572 }
573
574 if (!config->expected_signed_cert_timestamps.empty()) {
575 const uint8_t *data;
576 size_t len;
577 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
578 if (config->expected_signed_cert_timestamps.size() != len ||
579 memcmp(config->expected_signed_cert_timestamps.data(),
580 data, len) != 0) {
581 fprintf(stderr, "SCT list mismatch\n");
582 return 2;
583 }
584 }
585
Adam Langley2ae77d22014-10-28 17:29:33 -0700586 if (config->renegotiate) {
587 if (config->async) {
588 fprintf(stderr, "--renegotiate is not supported with --async.\n");
589 return 2;
590 }
591
592 SSL_renegotiate(ssl);
593
594 ret = SSL_do_handshake(ssl);
595 if (ret != 1) {
596 SSL_free(ssl);
597 BIO_print_errors_fp(stdout);
598 return 2;
599 }
600
601 SSL_set_state(ssl, SSL_ST_ACCEPT);
602 ret = SSL_do_handshake(ssl);
603 if (ret != 1) {
604 SSL_free(ssl);
605 BIO_print_errors_fp(stdout);
606 return 2;
607 }
608 }
609
David Benjamin5a593af2014-08-11 19:51:50 -0400610 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400611 if (config->is_dtls) {
612 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
613 return 6;
614 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700615 // This mode writes a number of different record sizes in an attempt to
616 // trip up the CBC record splitting code.
617 uint8_t buf[32769];
618 memset(buf, 0x42, sizeof(buf));
619 static const size_t kRecordSizes[] = {
620 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
621 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
622 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400623 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700624 const size_t len = kRecordSizes[i];
625 size_t off = 0;
626
627 if (len > sizeof(buf)) {
628 fprintf(stderr, "Bad kRecordSizes value.\n");
629 return 5;
630 }
631
David Benjamin43ec06f2014-08-05 02:28:57 -0400632 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700633 w = SSL_write(ssl, buf + off, len - off);
634 if (w > 0) {
635 off += (size_t) w;
636 }
David Benjamin5a593af2014-08-11 19:51:50 -0400637 } while ((config->async && retry_async(ssl, w, bio)) ||
638 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700639
640 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400641 SSL_free(ssl);
642 BIO_print_errors_fp(stdout);
643 return 4;
644 }
645 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700646 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400647 if (config->shim_writes_first) {
648 int w;
649 do {
650 w = SSL_write(ssl, "hello", 5);
651 } while (config->async && retry_async(ssl, w, bio));
652 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700653 for (;;) {
654 uint8_t buf[512];
655 int n;
656 do {
657 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400658 } while (config->async && retry_async(ssl, n, bio));
David Benjamin9a38e922015-01-22 16:06:11 -0500659 int err = SSL_get_error(ssl, n);
660 if (err == SSL_ERROR_ZERO_RETURN ||
661 (n == 0 && err == SSL_ERROR_SYSCALL)) {
662 if (n != 0) {
663 fprintf(stderr, "Invalid SSL_get_error output\n");
664 return 3;
665 }
666 /* Accept shutdowns with or without close_notify.
667 * TODO(davidben): Write tests which distinguish these two cases. */
668 break;
669 } else if (err != SSL_ERROR_NONE) {
670 if (n > 0) {
671 fprintf(stderr, "Invalid SSL_get_error output\n");
672 return 3;
673 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700674 SSL_free(ssl);
675 BIO_print_errors_fp(stdout);
676 return 3;
David Benjamin9a38e922015-01-22 16:06:11 -0500677 }
678 /* Successfully read data. */
679 if (n <= 0) {
680 fprintf(stderr, "Invalid SSL_get_error output\n");
681 return 3;
682 }
683 for (int i = 0; i < n; i++) {
684 buf[i] ^= 0xff;
685 }
686 int w;
687 do {
688 w = SSL_write(ssl, buf, n);
689 } while (config->async && retry_async(ssl, w, bio));
690 if (w != n) {
691 SSL_free(ssl);
692 BIO_print_errors_fp(stdout);
693 return 4;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700694 }
695 }
David Benjamin025b3d32014-07-01 19:53:04 -0400696 }
697
David Benjamin1d5c83e2014-07-22 19:20:02 -0400698 if (out_session) {
699 *out_session = SSL_get1_session(ssl);
700 }
701
702 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400703 SSL_free(ssl);
704 return 0;
705}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400706
707int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700708#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400709 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700710#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400711
David Benjamin5a593af2014-08-11 19:51:50 -0400712 if (!SSL_library_init()) {
713 return 1;
714 }
715 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjaminae3e4872014-11-18 21:52:26 -0500716 if (g_ex_data_index < 0) {
717 return 1;
718 }
David Benjamin5a593af2014-08-11 19:51:50 -0400719
720 TestConfig config;
721 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400722 return usage(argv[0]);
723 }
724
David Benjamin5a593af2014-08-11 19:51:50 -0400725 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400726 if (ssl_ctx == NULL) {
727 BIO_print_errors_fp(stdout);
728 return 1;
729 }
730
Adam Langleye7bf2812014-08-19 11:36:45 -0700731 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400732 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400733 ssl_ctx, &config,
734 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400735 3 /* fd */, NULL /* session */);
736 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700737 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400738 }
739
David Benjamin5a593af2014-08-11 19:51:50 -0400740 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700741 ret = do_exchange(NULL,
742 ssl_ctx, &config,
743 true /* is_resume */,
744 4 /* fd */,
745 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400746 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700747 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400748 }
749 }
750
Adam Langleye7bf2812014-08-19 11:36:45 -0700751 ret = 0;
752
753out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400754 SSL_SESSION_free(session);
755 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700756 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400757}