blob: cdd62ff65f226782daa70385d3b264070e96f949 [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
44static void SetConfigPtr(SSL *ssl, const TestConfig *config) {
45 SSL_set_ex_data(ssl, g_ex_data_index, (void *)config);
46}
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);
270 if (!SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
271 goto err;
272 }
273
David Benjamin1d5c83e2014-07-22 19:20:02 -0400274 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
275
David Benjamin8f2c20e2014-07-09 09:30:38 -0400276 ssl_ctx->select_certificate_cb = select_certificate_callback;
277
David Benjamin1f5f62b2014-07-12 16:18:02 -0400278 SSL_CTX_set_next_protos_advertised_cb(
279 ssl_ctx, next_protos_advertised_callback, NULL);
David Benjaminae2888f2014-09-06 12:58:58 -0400280 if (!config->select_next_proto.empty()) {
281 SSL_CTX_set_next_proto_select_cb(ssl_ctx, next_proto_select_callback, NULL);
282 }
283
284 if (!config->select_alpn.empty()) {
285 SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_callback, NULL);
286 }
David Benjamin1f5f62b2014-07-12 16:18:02 -0400287
David Benjamin6fd297b2014-08-11 18:43:38 -0400288 SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
289 SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
290
David Benjamina08e49d2014-08-24 01:46:07 -0400291 ssl_ctx->tlsext_channel_id_enabled_new = 1;
292
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400293 DH_free(dh);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400294 return ssl_ctx;
295
296 err:
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400297 if (dh != NULL) {
298 DH_free(dh);
299 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400300 if (ssl_ctx != NULL) {
301 SSL_CTX_free(ssl_ctx);
302 }
303 return NULL;
304}
305
David Benjamin43ec06f2014-08-05 02:28:57 -0400306static int retry_async(SSL *ssl, int ret, BIO *bio) {
307 // No error; don't retry.
308 if (ret >= 0) {
309 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400310 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400311 // See if we needed to read or write more. If so, allow one byte through on
312 // the appropriate end to maximally stress the state machine.
313 int err = SSL_get_error(ssl, ret);
314 if (err == SSL_ERROR_WANT_READ) {
315 async_bio_allow_read(bio, 1);
316 return 1;
317 } else if (err == SSL_ERROR_WANT_WRITE) {
318 async_bio_allow_write(bio, 1);
319 return 1;
David Benjamin025b3d32014-07-01 19:53:04 -0400320 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400321 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400322}
323
David Benjamin1d5c83e2014-07-22 19:20:02 -0400324static int do_exchange(SSL_SESSION **out_session,
325 SSL_CTX *ssl_ctx,
David Benjamin5a593af2014-08-11 19:51:50 -0400326 const TestConfig *config,
327 bool is_resume,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400328 int fd,
329 SSL_SESSION *session) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400330 early_callback_called = 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400331
David Benjamin43ec06f2014-08-05 02:28:57 -0400332 SSL *ssl = SSL_new(ssl_ctx);
David Benjamin025b3d32014-07-01 19:53:04 -0400333 if (ssl == NULL) {
334 BIO_print_errors_fp(stdout);
335 return 1;
336 }
337
David Benjamin5a593af2014-08-11 19:51:50 -0400338 SetConfigPtr(ssl, config);
339
340 if (config->fallback_scsv) {
341 if (!SSL_enable_fallback_scsv(ssl)) {
342 BIO_print_errors_fp(stdout);
David Benjamin025b3d32014-07-01 19:53:04 -0400343 return 1;
344 }
345 }
David Benjamin5a593af2014-08-11 19:51:50 -0400346 if (!config->key_file.empty()) {
347 if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
348 SSL_FILETYPE_PEM)) {
349 BIO_print_errors_fp(stdout);
350 return 1;
351 }
352 }
353 if (!config->cert_file.empty()) {
354 if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
355 SSL_FILETYPE_PEM)) {
356 BIO_print_errors_fp(stdout);
357 return 1;
358 }
359 }
360 if (config->require_any_client_certificate) {
361 SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
362 skip_verify);
363 }
364 if (config->false_start) {
365 SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
366 }
367 if (config->cbc_record_splitting) {
368 SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
369 }
370 if (config->partial_write) {
371 SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
372 }
373 if (config->no_tls12) {
374 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
375 }
376 if (config->no_tls11) {
377 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
378 }
379 if (config->no_tls1) {
380 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
381 }
382 if (config->no_ssl3) {
383 SSL_set_options(ssl, SSL_OP_NO_SSLv3);
384 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400385 if (config->cookie_exchange) {
386 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
387 }
David Benjamin5c24a1d2014-08-31 00:59:27 -0400388 if (config->tls_d5_bug) {
389 SSL_set_options(ssl, SSL_OP_TLS_D5_BUG);
390 }
David Benjaminca6554b2014-11-08 12:31:52 -0500391 if (config->allow_unsafe_legacy_renegotiation) {
392 SSL_set_options(ssl, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
393 }
David Benjamina08e49d2014-08-24 01:46:07 -0400394 if (!config->expected_channel_id.empty()) {
395 SSL_enable_tls_channel_id(ssl);
396 }
397 if (!config->send_channel_id.empty()) {
398 EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id);
399 if (pkey == NULL) {
400 BIO_print_errors_fp(stdout);
401 return 1;
402 }
403 SSL_enable_tls_channel_id(ssl);
404 if (!SSL_set1_tls_channel_id(ssl, pkey)) {
405 EVP_PKEY_free(pkey);
406 BIO_print_errors_fp(stdout);
407 return 1;
408 }
409 EVP_PKEY_free(pkey);
410 }
David Benjamine78bfde2014-09-06 12:45:15 -0400411 if (!config->host_name.empty()) {
412 SSL_set_tlsext_host_name(ssl, config->host_name.c_str());
413 }
David Benjaminae2888f2014-09-06 12:58:58 -0400414 if (!config->advertise_alpn.empty()) {
415 SSL_set_alpn_protos(ssl, (const uint8_t *)config->advertise_alpn.data(),
416 config->advertise_alpn.size());
417 }
David Benjamin48cae082014-10-27 01:06:24 -0400418 if (!config->psk.empty()) {
419 SSL_set_psk_client_callback(ssl, psk_client_callback);
420 SSL_set_psk_server_callback(ssl, psk_server_callback);
421 }
422 if (!config->psk_identity.empty()) {
423 if (!SSL_use_psk_identity_hint(ssl, config->psk_identity.c_str())) {
424 BIO_print_errors_fp(stdout);
425 return 1;
426 }
427 }
David Benjaminca6c8262014-11-15 19:06:08 -0500428 if (!config->srtp_profiles.empty()) {
429 if (!SSL_set_srtp_profiles(ssl, config->srtp_profiles.c_str())) {
430 BIO_print_errors_fp(stdout);
431 return 1;
432 }
433 }
David Benjamin025b3d32014-07-01 19:53:04 -0400434
David Benjamin43ec06f2014-08-05 02:28:57 -0400435 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
436 if (bio == NULL) {
437 BIO_print_errors_fp(stdout);
438 return 1;
439 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400440 if (config->is_dtls) {
441 BIO *packeted = packeted_bio_create();
442 BIO_push(packeted, bio);
443 bio = packeted;
444 }
David Benjamin5a593af2014-08-11 19:51:50 -0400445 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400446 BIO *async =
447 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400448 BIO_push(async, bio);
449 bio = async;
450 }
451 SSL_set_bio(ssl, bio, bio);
452
David Benjamin1d5c83e2014-07-22 19:20:02 -0400453 if (session != NULL) {
454 if (SSL_set_session(ssl, session) != 1) {
455 fprintf(stderr, "failed to set session\n");
456 return 2;
457 }
458 }
459
460 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400461 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400462 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400463 ret = SSL_accept(ssl);
464 } else {
465 ret = SSL_connect(ssl);
466 }
David Benjamin5a593af2014-08-11 19:51:50 -0400467 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400468 if (ret != 1) {
469 SSL_free(ssl);
470 BIO_print_errors_fp(stdout);
471 return 2;
472 }
473
David Benjamin01fe8202014-09-24 15:21:44 -0400474 if (is_resume && (SSL_session_reused(ssl) == config->expect_session_miss)) {
475 fprintf(stderr, "session was%s reused\n",
476 SSL_session_reused(ssl) ? "" : " not");
David Benjamin1d5c83e2014-07-22 19:20:02 -0400477 return 2;
478 }
479
David Benjamin5a593af2014-08-11 19:51:50 -0400480 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400481 const char *server_name =
482 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400483 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400484 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400485 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400486 return 2;
487 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400488
489 if (!early_callback_called) {
490 fprintf(stderr, "early callback not called\n");
491 return 2;
492 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400493 }
494
David Benjamin5a593af2014-08-11 19:51:50 -0400495 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400496 uint8_t *certificate_types;
497 int num_certificate_types =
David Benjamina08e49d2014-08-24 01:46:07 -0400498 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400499 if (num_certificate_types !=
500 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400501 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400502 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400503 num_certificate_types) != 0) {
504 fprintf(stderr, "certificate types mismatch\n");
505 return 2;
506 }
507 }
508
David Benjamin5a593af2014-08-11 19:51:50 -0400509 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400510 const uint8_t *next_proto;
511 unsigned next_proto_len;
512 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400513 if (next_proto_len != config->expected_next_proto.size() ||
514 memcmp(next_proto, config->expected_next_proto.data(),
515 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400516 fprintf(stderr, "negotiated next proto mismatch\n");
517 return 2;
518 }
519 }
520
David Benjaminae2888f2014-09-06 12:58:58 -0400521 if (!config->expected_alpn.empty()) {
522 const uint8_t *alpn_proto;
523 unsigned alpn_proto_len;
524 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
525 if (alpn_proto_len != config->expected_alpn.size() ||
526 memcmp(alpn_proto, config->expected_alpn.data(),
527 alpn_proto_len) != 0) {
528 fprintf(stderr, "negotiated alpn proto mismatch\n");
529 return 2;
530 }
531 }
532
David Benjamina08e49d2014-08-24 01:46:07 -0400533 if (!config->expected_channel_id.empty()) {
534 uint8_t channel_id[64];
535 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
536 fprintf(stderr, "no channel id negotiated\n");
537 return 2;
538 }
539 if (config->expected_channel_id.size() != 64 ||
540 memcmp(config->expected_channel_id.data(),
541 channel_id, 64) != 0) {
542 fprintf(stderr, "channel id mismatch\n");
543 return 2;
544 }
545 }
546
Adam Langley75712922014-10-10 16:23:43 -0700547 if (config->expect_extended_master_secret) {
548 if (!ssl->session->extended_master_secret) {
549 fprintf(stderr, "No EMS for session when expected");
550 return 2;
551 }
552 }
553
Adam Langley2ae77d22014-10-28 17:29:33 -0700554 if (config->renegotiate) {
555 if (config->async) {
556 fprintf(stderr, "--renegotiate is not supported with --async.\n");
557 return 2;
558 }
559
560 SSL_renegotiate(ssl);
561
562 ret = SSL_do_handshake(ssl);
563 if (ret != 1) {
564 SSL_free(ssl);
565 BIO_print_errors_fp(stdout);
566 return 2;
567 }
568
569 SSL_set_state(ssl, SSL_ST_ACCEPT);
570 ret = SSL_do_handshake(ssl);
571 if (ret != 1) {
572 SSL_free(ssl);
573 BIO_print_errors_fp(stdout);
574 return 2;
575 }
576 }
577
David Benjamin5a593af2014-08-11 19:51:50 -0400578 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400579 if (config->is_dtls) {
580 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
581 return 6;
582 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700583 // This mode writes a number of different record sizes in an attempt to
584 // trip up the CBC record splitting code.
585 uint8_t buf[32769];
586 memset(buf, 0x42, sizeof(buf));
587 static const size_t kRecordSizes[] = {
588 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
589 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
590 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400591 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700592 const size_t len = kRecordSizes[i];
593 size_t off = 0;
594
595 if (len > sizeof(buf)) {
596 fprintf(stderr, "Bad kRecordSizes value.\n");
597 return 5;
598 }
599
David Benjamin43ec06f2014-08-05 02:28:57 -0400600 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700601 w = SSL_write(ssl, buf + off, len - off);
602 if (w > 0) {
603 off += (size_t) w;
604 }
David Benjamin5a593af2014-08-11 19:51:50 -0400605 } while ((config->async && retry_async(ssl, w, bio)) ||
606 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700607
608 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400609 SSL_free(ssl);
610 BIO_print_errors_fp(stdout);
611 return 4;
612 }
613 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700614 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400615 if (config->shim_writes_first) {
616 int w;
617 do {
618 w = SSL_write(ssl, "hello", 5);
619 } while (config->async && retry_async(ssl, w, bio));
620 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700621 for (;;) {
622 uint8_t buf[512];
623 int n;
624 do {
625 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400626 } while (config->async && retry_async(ssl, n, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700627 if (n < 0) {
628 SSL_free(ssl);
629 BIO_print_errors_fp(stdout);
630 return 3;
631 } else if (n == 0) {
632 break;
633 } else {
634 for (int i = 0; i < n; i++) {
635 buf[i] ^= 0xff;
636 }
637 int w;
638 do {
639 w = SSL_write(ssl, buf, n);
David Benjamin5a593af2014-08-11 19:51:50 -0400640 } while (config->async && retry_async(ssl, w, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700641 if (w != n) {
642 SSL_free(ssl);
643 BIO_print_errors_fp(stdout);
644 return 4;
645 }
646 }
647 }
David Benjamin025b3d32014-07-01 19:53:04 -0400648 }
649
David Benjamin1d5c83e2014-07-22 19:20:02 -0400650 if (out_session) {
651 *out_session = SSL_get1_session(ssl);
652 }
653
654 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400655 SSL_free(ssl);
656 return 0;
657}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400658
659int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700660#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400661 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700662#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400663
David Benjamin5a593af2014-08-11 19:51:50 -0400664 if (!SSL_library_init()) {
665 return 1;
666 }
667 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
668
669 TestConfig config;
670 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400671 return usage(argv[0]);
672 }
673
David Benjamin5a593af2014-08-11 19:51:50 -0400674 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400675 if (ssl_ctx == NULL) {
676 BIO_print_errors_fp(stdout);
677 return 1;
678 }
679
Adam Langleye7bf2812014-08-19 11:36:45 -0700680 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400681 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400682 ssl_ctx, &config,
683 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400684 3 /* fd */, NULL /* session */);
685 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700686 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400687 }
688
David Benjamin5a593af2014-08-11 19:51:50 -0400689 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700690 ret = do_exchange(NULL,
691 ssl_ctx, &config,
692 true /* is_resume */,
693 4 /* fd */,
694 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400695 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700696 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400697 }
698 }
699
Adam Langleye7bf2812014-08-19 11:36:45 -0700700 ret = 0;
701
702out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400703 SSL_SESSION_free(session);
704 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700705 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400706}