blob: a4dd06d7962ebf3196550c73f8227e3fe153afad [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 Benjamin6fd297b2014-08-11 18:43:38 -0400165 *cookie_len = 32;
166 memset(cookie, 42, *cookie_len);
167 return 1;
168}
169
David Benjaminfb4ea282014-08-15 13:38:15 -0400170static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400171 if (cookie_len != 32) {
172 fprintf(stderr, "Cookie length mismatch.\n");
173 return 0;
174 }
175 for (size_t i = 0; i < cookie_len; i++) {
176 if (cookie[i] != 42) {
177 fprintf(stderr, "Cookie mismatch.\n");
178 return 0;
179 }
180 }
181 return 1;
182}
183
David Benjamin48cae082014-10-27 01:06:24 -0400184static unsigned psk_client_callback(SSL *ssl, const char *hint,
185 char *out_identity,
186 unsigned max_identity_len,
187 uint8_t *out_psk, unsigned max_psk_len) {
188 const TestConfig *config = GetConfigPtr(ssl);
189
190 if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) {
191 fprintf(stderr, "Server PSK hint did not match.\n");
192 return 0;
193 }
194
195 // Account for the trailing '\0' for the identity.
196 if (config->psk_identity.size() >= max_identity_len ||
197 config->psk.size() > max_psk_len) {
198 fprintf(stderr, "PSK buffers too small\n");
199 return 0;
200 }
201
202 BUF_strlcpy(out_identity, config->psk_identity.c_str(),
203 max_identity_len);
204 memcpy(out_psk, config->psk.data(), config->psk.size());
205 return config->psk.size();
206}
207
208static unsigned psk_server_callback(SSL *ssl, const char *identity,
209 uint8_t *out_psk, unsigned max_psk_len) {
210 const TestConfig *config = GetConfigPtr(ssl);
211
212 if (strcmp(identity, config->psk_identity.c_str()) != 0) {
213 fprintf(stderr, "Client PSK identity did not match.\n");
214 return 0;
215 }
216
217 if (config->psk.size() > max_psk_len) {
218 fprintf(stderr, "PSK buffers too small\n");
219 return 0;
220 }
221
222 memcpy(out_psk, config->psk.data(), config->psk.size());
223 return config->psk.size();
224}
225
David Benjamin5a593af2014-08-11 19:51:50 -0400226static SSL_CTX *setup_ctx(const TestConfig *config) {
David Benjamin025b3d32014-07-01 19:53:04 -0400227 SSL_CTX *ssl_ctx = NULL;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400228 DH *dh = NULL;
David Benjamin025b3d32014-07-01 19:53:04 -0400229
David Benjamin6fd297b2014-08-11 18:43:38 -0400230 const SSL_METHOD *method;
231 if (config->is_dtls) {
232 // TODO(davidben): Get DTLS 1.2 working and test the version negotiation
233 // codepath. This doesn't currently work because
234 // - Session resumption is broken: https://crbug.com/403378
235 // - DTLS hasn't been updated for EVP_AEAD.
236 if (config->is_server) {
237 method = DTLSv1_server_method();
238 } else {
239 method = DTLSv1_client_method();
240 }
241 } else {
242 if (config->is_server) {
243 method = SSLv23_server_method();
244 } else {
245 method = SSLv23_client_method();
246 }
247 }
248 ssl_ctx = SSL_CTX_new(method);
David Benjamin025b3d32014-07-01 19:53:04 -0400249 if (ssl_ctx == NULL) {
250 goto err;
251 }
252
David Benjamin6fd297b2014-08-11 18:43:38 -0400253 if (config->is_dtls) {
254 // DTLS needs read-ahead to function on a datagram BIO.
255 //
256 // TODO(davidben): this should not be necessary. DTLS code should only
257 // expect a datagram BIO.
258 SSL_CTX_set_read_ahead(ssl_ctx, 1);
259 }
260
David Benjamin025b3d32014-07-01 19:53:04 -0400261 if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
262 goto err;
263 }
264
265 if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
266 goto err;
267 }
268
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400269 dh = DH_get_2048_256(NULL);
Adam Langley69a01602014-11-17 17:26:55 -0800270 if (dh == NULL ||
271 !SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400272 goto err;
273 }
274
David Benjamin1d5c83e2014-07-22 19:20:02 -0400275 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
276
David Benjamin8f2c20e2014-07-09 09:30:38 -0400277 ssl_ctx->select_certificate_cb = select_certificate_callback;
278
David Benjamin1f5f62b2014-07-12 16:18:02 -0400279 SSL_CTX_set_next_protos_advertised_cb(
280 ssl_ctx, next_protos_advertised_callback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400281 if (!config->select_next_proto.empty()) {
282 SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_callback, NULL);
283 }
284
285 if (!config->select_alpn.empty()) {
286 SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_callback, NULL);
287 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400288
David Benjamin6fd297b2014-08-11 18:43:38 -0400289 SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
290 SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
291
David Benjamina08e49d2014-08-24 01:46:07 -0400292 ssl_ctx->tlsext_channel_id_enabled_new = 1;
293
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400294 DH_free(dh);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400295 return ssl_ctx;
296
297 err:
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400298 if (dh != NULL) {
299 DH_free(dh);
300 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400301 if (ssl_ctx != NULL) {
302 SSL_CTX_free(ssl_ctx);
303 }
304 return NULL;
305}
306
David Benjamin43ec06f2014-08-05 02:28:57 -0400307static int retry_async(SSL *ssl, int ret, BIO *bio) {
308 // No error; don't retry.
309 if (ret >= 0) {
310 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400311 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400312 // See if we needed to read or write more. If so, allow one byte through on
313 // the appropriate end to maximally stress the state machine.
314 int err = SSL_get_error(ssl, ret);
315 if (err == SSL_ERROR_WANT_READ) {
316 async_bio_allow_read(bio, 1);
317 return 1;
318 } else if (err == SSL_ERROR_WANT_WRITE) {
319 async_bio_allow_write(bio, 1);
320 return 1;
David Benjamin025b3d32014-07-01 19:53:04 -0400321 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400322 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400323}
324
David Benjamin1d5c83e2014-07-22 19:20:02 -0400325static int do_exchange(SSL_SESSION **out_session,
326 SSL_CTX *ssl_ctx,
David Benjamin5a593af2014-08-11 19:51:50 -0400327 const TestConfig *config,
328 bool is_resume,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400329 int fd,
330 SSL_SESSION *session) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400331 early_callback_called = 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400332
David Benjamin43ec06f2014-08-05 02:28:57 -0400333 SSL *ssl = SSL_new(ssl_ctx);
David Benjamin025b3d32014-07-01 19:53:04 -0400334 if (ssl == NULL) {
335 BIO_print_errors_fp(stdout);
336 return 1;
337 }
338
Adam Langley69a01602014-11-17 17:26:55 -0800339 if (!SetConfigPtr(ssl, config)) {
340 BIO_print_errors_fp(stdout);
341 return 1;
342 }
David Benjamin5a593af2014-08-11 19:51:50 -0400343
344 if (config->fallback_scsv) {
345 if (!SSL_enable_fallback_scsv(ssl)) {
346 BIO_print_errors_fp(stdout);
David Benjamin025b3d32014-07-01 19:53:04 -0400347 return 1;
348 }
349 }
David Benjamin5a593af2014-08-11 19:51:50 -0400350 if (!config->key_file.empty()) {
351 if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
352 SSL_FILETYPE_PEM)) {
353 BIO_print_errors_fp(stdout);
354 return 1;
355 }
356 }
357 if (!config->cert_file.empty()) {
358 if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
359 SSL_FILETYPE_PEM)) {
360 BIO_print_errors_fp(stdout);
361 return 1;
362 }
363 }
364 if (config->require_any_client_certificate) {
365 SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
366 skip_verify);
367 }
368 if (config->false_start) {
369 SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
370 }
371 if (config->cbc_record_splitting) {
372 SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
373 }
374 if (config->partial_write) {
375 SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
376 }
377 if (config->no_tls12) {
378 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
379 }
380 if (config->no_tls11) {
381 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
382 }
383 if (config->no_tls1) {
384 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
385 }
386 if (config->no_ssl3) {
387 SSL_set_options(ssl, SSL_OP_NO_SSLv3);
388 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400389 if (config->cookie_exchange) {
390 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
391 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400392 if (config->tls_d5_bug) {
393 SSL_set_options(ssl, SSL_OP_TLS_D5_BUG);
394 }
David Benjaminca6554b2014-11-08 12:31:52 -0500395 if (config->allow_unsafe_legacy_renegotiation) {
396 SSL_set_options(ssl, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
397 }
David Benjamina08e49d2014-08-24 01:46:07 -0400398 if (!config->expected_channel_id.empty()) {
399 SSL_enable_tls_channel_id(ssl);
400 }
401 if (!config->send_channel_id.empty()) {
402 EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id);
403 if (pkey == NULL) {
404 BIO_print_errors_fp(stdout);
405 return 1;
406 }
407 SSL_enable_tls_channel_id(ssl);
408 if (!SSL_set1_tls_channel_id(ssl, pkey)) {
409 EVP_PKEY_free(pkey);
410 BIO_print_errors_fp(stdout);
411 return 1;
412 }
413 EVP_PKEY_free(pkey);
414 }
David Benjamine78bfde2014-09-06 12:45:15 -0400415 if (!config->host_name.empty()) {
416 SSL_set_tlsext_host_name(ssl, config->host_name.c_str());
417 }
David Benjaminae2888f2014-09-06 12:58:58 -0400418 if (!config->advertise_alpn.empty()) {
419 SSL_set_alpn_protos(ssl, (const uint8_t *)config->advertise_alpn.data(),
420 config->advertise_alpn.size());
421 }
David Benjamin48cae082014-10-27 01:06:24 -0400422 if (!config->psk.empty()) {
423 SSL_set_psk_client_callback(ssl, psk_client_callback);
424 SSL_set_psk_server_callback(ssl, psk_server_callback);
425 }
David Benjamin61f95272014-11-25 01:55:35 -0500426 if (!config->psk_identity.empty() &&
427 !SSL_use_psk_identity_hint(ssl, config->psk_identity.c_str())) {
428 BIO_print_errors_fp(stdout);
429 return 1;
David Benjamin48cae082014-10-27 01:06:24 -0400430 }
David Benjamin61f95272014-11-25 01:55:35 -0500431 if (!config->srtp_profiles.empty() &&
432 !SSL_set_srtp_profiles(ssl, config->srtp_profiles.c_str())) {
433 BIO_print_errors_fp(stdout);
434 return 1;
435 }
436 if (config->enable_ocsp_stapling &&
437 !SSL_enable_ocsp_stapling(ssl)) {
438 BIO_print_errors_fp(stdout);
439 return 1;
440 }
441 if (config->enable_signed_cert_timestamps &&
442 !SSL_enable_signed_cert_timestamps(ssl)) {
443 BIO_print_errors_fp(stdout);
444 return 1;
David Benjaminca6c8262014-11-15 19:06:08 -0500445 }
David Benjamin025b3d32014-07-01 19:53:04 -0400446
David Benjamin43ec06f2014-08-05 02:28:57 -0400447 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
448 if (bio == NULL) {
449 BIO_print_errors_fp(stdout);
450 return 1;
451 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400452 if (config->is_dtls) {
453 BIO *packeted = packeted_bio_create();
454 BIO_push(packeted, bio);
455 bio = packeted;
456 }
David Benjamin5a593af2014-08-11 19:51:50 -0400457 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400458 BIO *async =
459 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400460 BIO_push(async, bio);
461 bio = async;
462 }
463 SSL_set_bio(ssl, bio, bio);
464
David Benjamin1d5c83e2014-07-22 19:20:02 -0400465 if (session != NULL) {
466 if (SSL_set_session(ssl, session) != 1) {
467 fprintf(stderr, "failed to set session\n");
468 return 2;
469 }
470 }
471
472 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400473 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400474 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400475 ret = SSL_accept(ssl);
476 } else {
477 ret = SSL_connect(ssl);
478 }
David Benjamin5a593af2014-08-11 19:51:50 -0400479 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400480 if (ret != 1) {
481 SSL_free(ssl);
482 BIO_print_errors_fp(stdout);
483 return 2;
484 }
485
David Benjamin01fe8202014-09-24 15:21:44 -0400486 if (is_resume && (SSL_session_reused(ssl) == config->expect_session_miss)) {
487 fprintf(stderr, "session was%s reused\n",
488 SSL_session_reused(ssl) ? "" : " not");
David Benjamin1d5c83e2014-07-22 19:20:02 -0400489 return 2;
490 }
491
David Benjamin5a593af2014-08-11 19:51:50 -0400492 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400493 const char *server_name =
494 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400495 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400496 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400497 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400498 return 2;
499 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400500
501 if (!early_callback_called) {
502 fprintf(stderr, "early callback not called\n");
503 return 2;
504 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400505 }
506
David Benjamin5a593af2014-08-11 19:51:50 -0400507 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400508 uint8_t *certificate_types;
509 int num_certificate_types =
David Benjamina08e49d2014-08-24 01:46:07 -0400510 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400511 if (num_certificate_types !=
512 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400513 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400514 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400515 num_certificate_types) != 0) {
516 fprintf(stderr, "certificate types mismatch\n");
517 return 2;
518 }
519 }
520
David Benjamin5a593af2014-08-11 19:51:50 -0400521 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400522 const uint8_t *next_proto;
523 unsigned next_proto_len;
524 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400525 if (next_proto_len != config->expected_next_proto.size() ||
526 memcmp(next_proto, config->expected_next_proto.data(),
527 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400528 fprintf(stderr, "negotiated next proto mismatch\n");
529 return 2;
530 }
531 }
532
David Benjaminae2888f2014-09-06 12:58:58 -0400533 if (!config->expected_alpn.empty()) {
534 const uint8_t *alpn_proto;
535 unsigned alpn_proto_len;
536 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
537 if (alpn_proto_len != config->expected_alpn.size() ||
538 memcmp(alpn_proto, config->expected_alpn.data(),
539 alpn_proto_len) != 0) {
540 fprintf(stderr, "negotiated alpn proto mismatch\n");
541 return 2;
542 }
543 }
544
David Benjamina08e49d2014-08-24 01:46:07 -0400545 if (!config->expected_channel_id.empty()) {
546 uint8_t channel_id[64];
547 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
548 fprintf(stderr, "no channel id negotiated\n");
549 return 2;
550 }
551 if (config->expected_channel_id.size() != 64 ||
552 memcmp(config->expected_channel_id.data(),
553 channel_id, 64) != 0) {
554 fprintf(stderr, "channel id mismatch\n");
555 return 2;
556 }
557 }
558
Adam Langley75712922014-10-10 16:23:43 -0700559 if (config->expect_extended_master_secret) {
560 if (!ssl->session->extended_master_secret) {
561 fprintf(stderr, "No EMS for session when expected");
562 return 2;
563 }
564 }
565
David Benjamin61f95272014-11-25 01:55:35 -0500566 if (!config->expected_ocsp_response.empty()) {
567 const uint8_t *data;
568 size_t len;
569 SSL_get0_ocsp_response(ssl, &data, &len);
570 if (config->expected_ocsp_response.size() != len ||
571 memcmp(config->expected_ocsp_response.data(), data, len) != 0) {
572 fprintf(stderr, "OCSP response mismatch\n");
573 return 2;
574 }
575 }
576
577 if (!config->expected_signed_cert_timestamps.empty()) {
578 const uint8_t *data;
579 size_t len;
580 SSL_get0_signed_cert_timestamp_list(ssl, &data, &len);
581 if (config->expected_signed_cert_timestamps.size() != len ||
582 memcmp(config->expected_signed_cert_timestamps.data(),
583 data, len) != 0) {
584 fprintf(stderr, "SCT list mismatch\n");
585 return 2;
586 }
587 }
588
Adam Langley2ae77d22014-10-28 17:29:33 -0700589 if (config->renegotiate) {
590 if (config->async) {
591 fprintf(stderr, "--renegotiate is not supported with --async.\n");
592 return 2;
593 }
594
595 SSL_renegotiate(ssl);
596
597 ret = SSL_do_handshake(ssl);
598 if (ret != 1) {
599 SSL_free(ssl);
600 BIO_print_errors_fp(stdout);
601 return 2;
602 }
603
604 SSL_set_state(ssl, SSL_ST_ACCEPT);
605 ret = SSL_do_handshake(ssl);
606 if (ret != 1) {
607 SSL_free(ssl);
608 BIO_print_errors_fp(stdout);
609 return 2;
610 }
611 }
612
David Benjamin5a593af2014-08-11 19:51:50 -0400613 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400614 if (config->is_dtls) {
615 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
616 return 6;
617 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700618 // This mode writes a number of different record sizes in an attempt to
619 // trip up the CBC record splitting code.
620 uint8_t buf[32769];
621 memset(buf, 0x42, sizeof(buf));
622 static const size_t kRecordSizes[] = {
623 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
624 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
625 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400626 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700627 const size_t len = kRecordSizes[i];
628 size_t off = 0;
629
630 if (len > sizeof(buf)) {
631 fprintf(stderr, "Bad kRecordSizes value.\n");
632 return 5;
633 }
634
David Benjamin43ec06f2014-08-05 02:28:57 -0400635 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700636 w = SSL_write(ssl, buf + off, len - off);
637 if (w > 0) {
638 off += (size_t) w;
639 }
David Benjamin5a593af2014-08-11 19:51:50 -0400640 } while ((config->async && retry_async(ssl, w, bio)) ||
641 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700642
643 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400644 SSL_free(ssl);
645 BIO_print_errors_fp(stdout);
646 return 4;
647 }
648 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700649 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400650 if (config->shim_writes_first) {
651 int w;
652 do {
653 w = SSL_write(ssl, "hello", 5);
654 } while (config->async && retry_async(ssl, w, bio));
655 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700656 for (;;) {
657 uint8_t buf[512];
658 int n;
659 do {
660 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400661 } while (config->async && retry_async(ssl, n, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700662 if (n < 0) {
663 SSL_free(ssl);
664 BIO_print_errors_fp(stdout);
665 return 3;
666 } else if (n == 0) {
667 break;
668 } else {
669 for (int i = 0; i < n; i++) {
670 buf[i] ^= 0xff;
671 }
672 int w;
673 do {
674 w = SSL_write(ssl, buf, n);
David Benjamin5a593af2014-08-11 19:51:50 -0400675 } while (config->async && retry_async(ssl, w, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700676 if (w != n) {
677 SSL_free(ssl);
678 BIO_print_errors_fp(stdout);
679 return 4;
680 }
681 }
682 }
David Benjamin025b3d32014-07-01 19:53:04 -0400683 }
684
David Benjamin1d5c83e2014-07-22 19:20:02 -0400685 if (out_session) {
686 *out_session = SSL_get1_session(ssl);
687 }
688
689 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400690 SSL_free(ssl);
691 return 0;
692}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400693
694int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700695#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400696 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700697#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400698
David Benjamin5a593af2014-08-11 19:51:50 -0400699 if (!SSL_library_init()) {
700 return 1;
701 }
702 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
David Benjaminae3e4872014-11-18 21:52:26 -0500703 if (g_ex_data_index < 0) {
704 return 1;
705 }
David Benjamin5a593af2014-08-11 19:51:50 -0400706
707 TestConfig config;
708 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400709 return usage(argv[0]);
710 }
711
David Benjamin5a593af2014-08-11 19:51:50 -0400712 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400713 if (ssl_ctx == NULL) {
714 BIO_print_errors_fp(stdout);
715 return 1;
716 }
717
Adam Langleye7bf2812014-08-19 11:36:45 -0700718 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400719 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400720 ssl_ctx, &config,
721 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400722 3 /* fd */, NULL /* session */);
723 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700724 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400725 }
726
David Benjamin5a593af2014-08-11 19:51:50 -0400727 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700728 ret = do_exchange(NULL,
729 ssl_ctx, &config,
730 true /* is_resume */,
731 4 /* fd */,
732 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400733 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700734 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400735 }
736 }
737
Adam Langleye7bf2812014-08-19 11:36:45 -0700738 ret = 0;
739
740out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400741 SSL_SESSION_free(session);
742 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700743 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400744}