blob: 9f9fac37be4a9a0d4e9a8a44eb60bd6cc5d10804 [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 }
426 if (!config->psk_identity.empty()) {
427 if (!SSL_use_psk_identity_hint(ssl, config->psk_identity.c_str())) {
428 BIO_print_errors_fp(stdout);
429 return 1;
430 }
431 }
David Benjaminca6c8262014-11-15 19:06:08 -0500432 if (!config->srtp_profiles.empty()) {
433 if (!SSL_set_srtp_profiles(ssl, config->srtp_profiles.c_str())) {
434 BIO_print_errors_fp(stdout);
435 return 1;
436 }
437 }
David Benjamin025b3d32014-07-01 19:53:04 -0400438
David Benjamin43ec06f2014-08-05 02:28:57 -0400439 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
440 if (bio == NULL) {
441 BIO_print_errors_fp(stdout);
442 return 1;
443 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400444 if (config->is_dtls) {
445 BIO *packeted = packeted_bio_create();
446 BIO_push(packeted, bio);
447 bio = packeted;
448 }
David Benjamin5a593af2014-08-11 19:51:50 -0400449 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400450 BIO *async =
451 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400452 BIO_push(async, bio);
453 bio = async;
454 }
455 SSL_set_bio(ssl, bio, bio);
456
David Benjamin1d5c83e2014-07-22 19:20:02 -0400457 if (session != NULL) {
458 if (SSL_set_session(ssl, session) != 1) {
459 fprintf(stderr, "failed to set session\n");
460 return 2;
461 }
462 }
463
464 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400465 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400466 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400467 ret = SSL_accept(ssl);
468 } else {
469 ret = SSL_connect(ssl);
470 }
David Benjamin5a593af2014-08-11 19:51:50 -0400471 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400472 if (ret != 1) {
473 SSL_free(ssl);
474 BIO_print_errors_fp(stdout);
475 return 2;
476 }
477
David Benjamin01fe8202014-09-24 15:21:44 -0400478 if (is_resume && (SSL_session_reused(ssl) == config->expect_session_miss)) {
479 fprintf(stderr, "session was%s reused\n",
480 SSL_session_reused(ssl) ? "" : " not");
David Benjamin1d5c83e2014-07-22 19:20:02 -0400481 return 2;
482 }
483
David Benjamin5a593af2014-08-11 19:51:50 -0400484 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400485 const char *server_name =
486 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400487 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400488 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400489 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400490 return 2;
491 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400492
493 if (!early_callback_called) {
494 fprintf(stderr, "early callback not called\n");
495 return 2;
496 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400497 }
498
David Benjamin5a593af2014-08-11 19:51:50 -0400499 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400500 uint8_t *certificate_types;
501 int num_certificate_types =
David Benjamina08e49d2014-08-24 01:46:07 -0400502 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400503 if (num_certificate_types !=
504 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400505 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400506 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400507 num_certificate_types) != 0) {
508 fprintf(stderr, "certificate types mismatch\n");
509 return 2;
510 }
511 }
512
David Benjamin5a593af2014-08-11 19:51:50 -0400513 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400514 const uint8_t *next_proto;
515 unsigned next_proto_len;
516 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400517 if (next_proto_len != config->expected_next_proto.size() ||
518 memcmp(next_proto, config->expected_next_proto.data(),
519 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400520 fprintf(stderr, "negotiated next proto mismatch\n");
521 return 2;
522 }
523 }
524
David Benjaminae2888f2014-09-06 12:58:58 -0400525 if (!config->expected_alpn.empty()) {
526 const uint8_t *alpn_proto;
527 unsigned alpn_proto_len;
528 SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
529 if (alpn_proto_len != config->expected_alpn.size() ||
530 memcmp(alpn_proto, config->expected_alpn.data(),
531 alpn_proto_len) != 0) {
532 fprintf(stderr, "negotiated alpn proto mismatch\n");
533 return 2;
534 }
535 }
536
David Benjamina08e49d2014-08-24 01:46:07 -0400537 if (!config->expected_channel_id.empty()) {
538 uint8_t channel_id[64];
539 if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) {
540 fprintf(stderr, "no channel id negotiated\n");
541 return 2;
542 }
543 if (config->expected_channel_id.size() != 64 ||
544 memcmp(config->expected_channel_id.data(),
545 channel_id, 64) != 0) {
546 fprintf(stderr, "channel id mismatch\n");
547 return 2;
548 }
549 }
550
Adam Langley75712922014-10-10 16:23:43 -0700551 if (config->expect_extended_master_secret) {
552 if (!ssl->session->extended_master_secret) {
553 fprintf(stderr, "No EMS for session when expected");
554 return 2;
555 }
556 }
557
Adam Langley2ae77d22014-10-28 17:29:33 -0700558 if (config->renegotiate) {
559 if (config->async) {
560 fprintf(stderr, "--renegotiate is not supported with --async.\n");
561 return 2;
562 }
563
564 SSL_renegotiate(ssl);
565
566 ret = SSL_do_handshake(ssl);
567 if (ret != 1) {
568 SSL_free(ssl);
569 BIO_print_errors_fp(stdout);
570 return 2;
571 }
572
573 SSL_set_state(ssl, SSL_ST_ACCEPT);
574 ret = SSL_do_handshake(ssl);
575 if (ret != 1) {
576 SSL_free(ssl);
577 BIO_print_errors_fp(stdout);
578 return 2;
579 }
580 }
581
David Benjamin5a593af2014-08-11 19:51:50 -0400582 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400583 if (config->is_dtls) {
584 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
585 return 6;
586 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700587 // This mode writes a number of different record sizes in an attempt to
588 // trip up the CBC record splitting code.
589 uint8_t buf[32769];
590 memset(buf, 0x42, sizeof(buf));
591 static const size_t kRecordSizes[] = {
592 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
593 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
594 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400595 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700596 const size_t len = kRecordSizes[i];
597 size_t off = 0;
598
599 if (len > sizeof(buf)) {
600 fprintf(stderr, "Bad kRecordSizes value.\n");
601 return 5;
602 }
603
David Benjamin43ec06f2014-08-05 02:28:57 -0400604 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700605 w = SSL_write(ssl, buf + off, len - off);
606 if (w > 0) {
607 off += (size_t) w;
608 }
David Benjamin5a593af2014-08-11 19:51:50 -0400609 } while ((config->async && retry_async(ssl, w, bio)) ||
610 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700611
612 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400613 SSL_free(ssl);
614 BIO_print_errors_fp(stdout);
615 return 4;
616 }
617 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700618 } else {
David Benjamine58c4f52014-08-24 03:47:07 -0400619 if (config->shim_writes_first) {
620 int w;
621 do {
622 w = SSL_write(ssl, "hello", 5);
623 } while (config->async && retry_async(ssl, w, bio));
624 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700625 for (;;) {
626 uint8_t buf[512];
627 int n;
628 do {
629 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400630 } while (config->async && retry_async(ssl, n, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700631 if (n < 0) {
632 SSL_free(ssl);
633 BIO_print_errors_fp(stdout);
634 return 3;
635 } else if (n == 0) {
636 break;
637 } else {
638 for (int i = 0; i < n; i++) {
639 buf[i] ^= 0xff;
640 }
641 int w;
642 do {
643 w = SSL_write(ssl, buf, n);
David Benjamin5a593af2014-08-11 19:51:50 -0400644 } while (config->async && retry_async(ssl, w, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700645 if (w != n) {
646 SSL_free(ssl);
647 BIO_print_errors_fp(stdout);
648 return 4;
649 }
650 }
651 }
David Benjamin025b3d32014-07-01 19:53:04 -0400652 }
653
David Benjamin1d5c83e2014-07-22 19:20:02 -0400654 if (out_session) {
655 *out_session = SSL_get1_session(ssl);
656 }
657
658 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400659 SSL_free(ssl);
660 return 0;
661}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400662
663int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700664#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400665 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700666#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400667
David Benjamin5a593af2014-08-11 19:51:50 -0400668 if (!SSL_library_init()) {
669 return 1;
670 }
671 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
672
673 TestConfig config;
674 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400675 return usage(argv[0]);
676 }
677
David Benjamin5a593af2014-08-11 19:51:50 -0400678 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400679 if (ssl_ctx == NULL) {
680 BIO_print_errors_fp(stdout);
681 return 1;
682 }
683
Adam Langleye7bf2812014-08-19 11:36:45 -0700684 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400685 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400686 ssl_ctx, &config,
687 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400688 3 /* fd */, NULL /* session */);
689 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700690 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400691 }
692
David Benjamin5a593af2014-08-11 19:51:50 -0400693 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700694 ret = do_exchange(NULL,
695 ssl_ctx, &config,
696 true /* is_resume */,
697 4 /* fd */,
698 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400699 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700700 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400701 }
702 }
703
Adam Langleye7bf2812014-08-19 11:36:45 -0700704 ret = 0;
705
706out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400707 SSL_SESSION_free(session);
708 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700709 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400710}