blob: c976e7c28ea84674e894246557f7b78a862f6ecd [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 Benjamin8f2c20e2014-07-09 09:30:38 -040028#include <openssl/bytestring.h>
David Benjamin1d5c83e2014-07-22 19:20:02 -040029#include <openssl/ssl.h>
30
David Benjamin43ec06f2014-08-05 02:28:57 -040031#include "async_bio.h"
David Benjamin6fd297b2014-08-11 18:43:38 -040032#include "packeted_bio.h"
David Benjamin5a593af2014-08-11 19:51:50 -040033#include "test_config.h"
David Benjamin43ec06f2014-08-05 02:28:57 -040034
David Benjamin1d5c83e2014-07-22 19:20:02 -040035static int usage(const char *program) {
David Benjamin5a593af2014-08-11 19:51:50 -040036 fprintf(stderr, "Usage: %s [flags...]\n",
David Benjamin1d5c83e2014-07-22 19:20:02 -040037 program);
38 return 1;
39}
David Benjamin025b3d32014-07-01 19:53:04 -040040
David Benjamin5a593af2014-08-11 19:51:50 -040041static int g_ex_data_index = 0;
42
43static void SetConfigPtr(SSL *ssl, const TestConfig *config) {
44 SSL_set_ex_data(ssl, g_ex_data_index, (void *)config);
45}
46
47static const TestConfig *GetConfigPtr(SSL *ssl) {
48 return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index);
49}
50
David Benjamin1f5f62b2014-07-12 16:18:02 -040051static int early_callback_called = 0;
David Benjamin8f2c20e2014-07-09 09:30:38 -040052
David Benjamin1f5f62b2014-07-12 16:18:02 -040053static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
David Benjamin8f2c20e2014-07-09 09:30:38 -040054 early_callback_called = 1;
55
David Benjamin5a593af2014-08-11 19:51:50 -040056 const TestConfig *config = GetConfigPtr(ctx->ssl);
57
58 if (config->expected_server_name.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -040059 return 1;
60 }
David Benjamin8f2c20e2014-07-09 09:30:38 -040061
David Benjamin7b030512014-07-08 17:30:11 -040062 const uint8_t *extension_data;
63 size_t extension_len;
64 CBS extension, server_name_list, host_name;
65 uint8_t name_type;
David Benjamin8f2c20e2014-07-09 09:30:38 -040066
David Benjamin7b030512014-07-08 17:30:11 -040067 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
68 &extension_data,
69 &extension_len)) {
70 fprintf(stderr, "Could not find server_name extension.\n");
71 return -1;
72 }
David Benjamin8f2c20e2014-07-09 09:30:38 -040073
David Benjamin7b030512014-07-08 17:30:11 -040074 CBS_init(&extension, extension_data, extension_len);
75 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
76 CBS_len(&extension) != 0 ||
77 !CBS_get_u8(&server_name_list, &name_type) ||
78 name_type != TLSEXT_NAMETYPE_host_name ||
79 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
80 CBS_len(&server_name_list) != 0) {
81 fprintf(stderr, "Could not decode server_name extension.\n");
82 return -1;
83 }
84
David Benjamin5a593af2014-08-11 19:51:50 -040085 if (!CBS_mem_equal(&host_name,
86 (const uint8_t*)config->expected_server_name.data(),
87 config->expected_server_name.size())) {
David Benjamin7b030512014-07-08 17:30:11 -040088 fprintf(stderr, "Server name mismatch.\n");
David Benjamin8f2c20e2014-07-09 09:30:38 -040089 }
90
91 return 1;
92}
David Benjamin025b3d32014-07-01 19:53:04 -040093
David Benjamin1f5f62b2014-07-12 16:18:02 -040094static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
David Benjamin67666e72014-07-12 15:47:52 -040095 return 1;
96}
97
David Benjamin1f5f62b2014-07-12 16:18:02 -040098static int next_protos_advertised_callback(SSL *ssl,
David Benjamin7e3305e2014-07-28 14:52:32 -040099 const uint8_t **out,
100 unsigned int *out_len,
101 void *arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400102 const TestConfig *config = GetConfigPtr(ssl);
103 if (config->advertise_npn.empty())
David Benjamin1f5f62b2014-07-12 16:18:02 -0400104 return SSL_TLSEXT_ERR_NOACK;
105
106 // TODO(davidben): Support passing byte strings with NULs to the
107 // test shim.
David Benjamin5a593af2014-08-11 19:51:50 -0400108 *out = (const uint8_t*)config->advertise_npn.data();
109 *out_len = config->advertise_npn.size();
David Benjamin1f5f62b2014-07-12 16:18:02 -0400110 return SSL_TLSEXT_ERR_OK;
111}
112
David Benjamin7e3305e2014-07-28 14:52:32 -0400113static int next_proto_select_callback(SSL* ssl,
114 uint8_t** out,
115 uint8_t* outlen,
116 const uint8_t* in,
117 unsigned inlen,
118 void* arg) {
David Benjamin5a593af2014-08-11 19:51:50 -0400119 const TestConfig *config = GetConfigPtr(ssl);
120 if (config->select_next_proto.empty())
David Benjamin7e3305e2014-07-28 14:52:32 -0400121 return SSL_TLSEXT_ERR_NOACK;
122
David Benjamin5a593af2014-08-11 19:51:50 -0400123 *out = (uint8_t*)config->select_next_proto.data();
124 *outlen = config->select_next_proto.size();
David Benjamin7e3305e2014-07-28 14:52:32 -0400125 return SSL_TLSEXT_ERR_OK;
126}
127
David Benjaminfb4ea282014-08-15 13:38:15 -0400128static int cookie_generate_callback(SSL *ssl, uint8_t *cookie, size_t *cookie_len) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400129 *cookie_len = 32;
130 memset(cookie, 42, *cookie_len);
131 return 1;
132}
133
David Benjaminfb4ea282014-08-15 13:38:15 -0400134static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400135 if (cookie_len != 32) {
136 fprintf(stderr, "Cookie length mismatch.\n");
137 return 0;
138 }
139 for (size_t i = 0; i < cookie_len; i++) {
140 if (cookie[i] != 42) {
141 fprintf(stderr, "Cookie mismatch.\n");
142 return 0;
143 }
144 }
145 return 1;
146}
147
David Benjamin5a593af2014-08-11 19:51:50 -0400148static SSL_CTX *setup_ctx(const TestConfig *config) {
David Benjamin025b3d32014-07-01 19:53:04 -0400149 SSL_CTX *ssl_ctx = NULL;
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400150 DH *dh = NULL;
David Benjamin025b3d32014-07-01 19:53:04 -0400151
David Benjamin6fd297b2014-08-11 18:43:38 -0400152 const SSL_METHOD *method;
153 if (config->is_dtls) {
154 // TODO(davidben): Get DTLS 1.2 working and test the version negotiation
155 // codepath. This doesn't currently work because
156 // - Session resumption is broken: https://crbug.com/403378
157 // - DTLS hasn't been updated for EVP_AEAD.
158 if (config->is_server) {
159 method = DTLSv1_server_method();
160 } else {
161 method = DTLSv1_client_method();
162 }
163 } else {
164 if (config->is_server) {
165 method = SSLv23_server_method();
166 } else {
167 method = SSLv23_client_method();
168 }
169 }
170 ssl_ctx = SSL_CTX_new(method);
David Benjamin025b3d32014-07-01 19:53:04 -0400171 if (ssl_ctx == NULL) {
172 goto err;
173 }
174
David Benjamin6fd297b2014-08-11 18:43:38 -0400175 if (config->is_dtls) {
176 // DTLS needs read-ahead to function on a datagram BIO.
177 //
178 // TODO(davidben): this should not be necessary. DTLS code should only
179 // expect a datagram BIO.
180 SSL_CTX_set_read_ahead(ssl_ctx, 1);
181 }
182
David Benjamin025b3d32014-07-01 19:53:04 -0400183 if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
184 goto err;
185 }
186
187 if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
188 goto err;
189 }
190
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400191 dh = DH_get_2048_256(NULL);
192 if (!SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
193 goto err;
194 }
195
David Benjamin1d5c83e2014-07-22 19:20:02 -0400196 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
197
David Benjamin8f2c20e2014-07-09 09:30:38 -0400198 ssl_ctx->select_certificate_cb = select_certificate_callback;
199
David Benjamin1f5f62b2014-07-12 16:18:02 -0400200 SSL_CTX_set_next_protos_advertised_cb(
201 ssl_ctx, next_protos_advertised_callback, NULL);
David Benjamin7e3305e2014-07-28 14:52:32 -0400202 SSL_CTX_set_next_proto_select_cb(
203 ssl_ctx, next_proto_select_callback, NULL);
David Benjamin1f5f62b2014-07-12 16:18:02 -0400204
David Benjamin6fd297b2014-08-11 18:43:38 -0400205 SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback);
206 SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback);
207
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400208 DH_free(dh);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400209 return ssl_ctx;
210
211 err:
David Benjaminf4e5c4e2014-08-02 17:35:45 -0400212 if (dh != NULL) {
213 DH_free(dh);
214 }
David Benjamin1d5c83e2014-07-22 19:20:02 -0400215 if (ssl_ctx != NULL) {
216 SSL_CTX_free(ssl_ctx);
217 }
218 return NULL;
219}
220
David Benjamin43ec06f2014-08-05 02:28:57 -0400221static int retry_async(SSL *ssl, int ret, BIO *bio) {
222 // No error; don't retry.
223 if (ret >= 0) {
224 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400225 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400226 // See if we needed to read or write more. If so, allow one byte through on
227 // the appropriate end to maximally stress the state machine.
228 int err = SSL_get_error(ssl, ret);
229 if (err == SSL_ERROR_WANT_READ) {
230 async_bio_allow_read(bio, 1);
231 return 1;
232 } else if (err == SSL_ERROR_WANT_WRITE) {
233 async_bio_allow_write(bio, 1);
234 return 1;
David Benjamin025b3d32014-07-01 19:53:04 -0400235 }
David Benjamin43ec06f2014-08-05 02:28:57 -0400236 return 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400237}
238
David Benjamin1d5c83e2014-07-22 19:20:02 -0400239static int do_exchange(SSL_SESSION **out_session,
240 SSL_CTX *ssl_ctx,
David Benjamin5a593af2014-08-11 19:51:50 -0400241 const TestConfig *config,
242 bool is_resume,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400243 int fd,
244 SSL_SESSION *session) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400245 early_callback_called = 0;
David Benjamin025b3d32014-07-01 19:53:04 -0400246
David Benjamin43ec06f2014-08-05 02:28:57 -0400247 SSL *ssl = SSL_new(ssl_ctx);
David Benjamin025b3d32014-07-01 19:53:04 -0400248 if (ssl == NULL) {
249 BIO_print_errors_fp(stdout);
250 return 1;
251 }
252
David Benjamin5a593af2014-08-11 19:51:50 -0400253 SetConfigPtr(ssl, config);
254
255 if (config->fallback_scsv) {
256 if (!SSL_enable_fallback_scsv(ssl)) {
257 BIO_print_errors_fp(stdout);
David Benjamin025b3d32014-07-01 19:53:04 -0400258 return 1;
259 }
260 }
David Benjamin5a593af2014-08-11 19:51:50 -0400261 if (!config->key_file.empty()) {
262 if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(),
263 SSL_FILETYPE_PEM)) {
264 BIO_print_errors_fp(stdout);
265 return 1;
266 }
267 }
268 if (!config->cert_file.empty()) {
269 if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(),
270 SSL_FILETYPE_PEM)) {
271 BIO_print_errors_fp(stdout);
272 return 1;
273 }
274 }
275 if (config->require_any_client_certificate) {
276 SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
277 skip_verify);
278 }
279 if (config->false_start) {
280 SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
281 }
282 if (config->cbc_record_splitting) {
283 SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
284 }
285 if (config->partial_write) {
286 SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
287 }
288 if (config->no_tls12) {
289 SSL_set_options(ssl, SSL_OP_NO_TLSv1_2);
290 }
291 if (config->no_tls11) {
292 SSL_set_options(ssl, SSL_OP_NO_TLSv1_1);
293 }
294 if (config->no_tls1) {
295 SSL_set_options(ssl, SSL_OP_NO_TLSv1);
296 }
297 if (config->no_ssl3) {
298 SSL_set_options(ssl, SSL_OP_NO_SSLv3);
299 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400300 if (config->cookie_exchange) {
301 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
302 }
David Benjamin025b3d32014-07-01 19:53:04 -0400303
David Benjamin43ec06f2014-08-05 02:28:57 -0400304 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
305 if (bio == NULL) {
306 BIO_print_errors_fp(stdout);
307 return 1;
308 }
David Benjamin6fd297b2014-08-11 18:43:38 -0400309 if (config->is_dtls) {
310 BIO *packeted = packeted_bio_create();
311 BIO_push(packeted, bio);
312 bio = packeted;
313 }
David Benjamin5a593af2014-08-11 19:51:50 -0400314 if (config->async) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400315 BIO *async =
316 config->is_dtls ? async_bio_create_datagram() : async_bio_create();
David Benjamin43ec06f2014-08-05 02:28:57 -0400317 BIO_push(async, bio);
318 bio = async;
319 }
320 SSL_set_bio(ssl, bio, bio);
321
David Benjamin1d5c83e2014-07-22 19:20:02 -0400322 if (session != NULL) {
323 if (SSL_set_session(ssl, session) != 1) {
324 fprintf(stderr, "failed to set session\n");
325 return 2;
326 }
327 }
328
329 int ret;
David Benjamin43ec06f2014-08-05 02:28:57 -0400330 do {
David Benjamin5a593af2014-08-11 19:51:50 -0400331 if (config->is_server) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400332 ret = SSL_accept(ssl);
333 } else {
334 ret = SSL_connect(ssl);
335 }
David Benjamin5a593af2014-08-11 19:51:50 -0400336 } while (config->async && retry_async(ssl, ret, bio));
David Benjamin025b3d32014-07-01 19:53:04 -0400337 if (ret != 1) {
338 SSL_free(ssl);
339 BIO_print_errors_fp(stdout);
340 return 2;
341 }
342
David Benjamin1d5c83e2014-07-22 19:20:02 -0400343 if (is_resume && !SSL_session_reused(ssl)) {
344 fprintf(stderr, "session was not reused\n");
345 return 2;
346 }
347
David Benjamin5a593af2014-08-11 19:51:50 -0400348 if (!config->expected_server_name.empty()) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400349 const char *server_name =
350 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
David Benjamin5a593af2014-08-11 19:51:50 -0400351 if (server_name != config->expected_server_name) {
David Benjamin197b3ab2014-07-02 18:37:33 -0400352 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
David Benjamin5a593af2014-08-11 19:51:50 -0400353 server_name, config->expected_server_name.c_str());
David Benjamin197b3ab2014-07-02 18:37:33 -0400354 return 2;
355 }
David Benjamin8f2c20e2014-07-09 09:30:38 -0400356
357 if (!early_callback_called) {
358 fprintf(stderr, "early callback not called\n");
359 return 2;
360 }
David Benjamin197b3ab2014-07-02 18:37:33 -0400361 }
362
David Benjamin5a593af2014-08-11 19:51:50 -0400363 if (!config->expected_certificate_types.empty()) {
David Benjamin7b030512014-07-08 17:30:11 -0400364 uint8_t *certificate_types;
365 int num_certificate_types =
366 SSL_get0_certificate_types(ssl, &certificate_types);
David Benjamin5a593af2014-08-11 19:51:50 -0400367 if (num_certificate_types !=
368 (int)config->expected_certificate_types.size() ||
David Benjamin7b030512014-07-08 17:30:11 -0400369 memcmp(certificate_types,
David Benjamin5a593af2014-08-11 19:51:50 -0400370 config->expected_certificate_types.data(),
David Benjamin7b030512014-07-08 17:30:11 -0400371 num_certificate_types) != 0) {
372 fprintf(stderr, "certificate types mismatch\n");
373 return 2;
374 }
375 }
376
David Benjamin5a593af2014-08-11 19:51:50 -0400377 if (!config->expected_next_proto.empty()) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400378 const uint8_t *next_proto;
379 unsigned next_proto_len;
380 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
David Benjamin5a593af2014-08-11 19:51:50 -0400381 if (next_proto_len != config->expected_next_proto.size() ||
382 memcmp(next_proto, config->expected_next_proto.data(),
383 next_proto_len) != 0) {
David Benjamin1f5f62b2014-07-12 16:18:02 -0400384 fprintf(stderr, "negotiated next proto mismatch\n");
385 return 2;
386 }
387 }
388
David Benjamin5a593af2014-08-11 19:51:50 -0400389 if (config->write_different_record_sizes) {
David Benjamin6fd297b2014-08-11 18:43:38 -0400390 if (config->is_dtls) {
391 fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
392 return 6;
393 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700394 // This mode writes a number of different record sizes in an attempt to
395 // trip up the CBC record splitting code.
396 uint8_t buf[32769];
397 memset(buf, 0x42, sizeof(buf));
398 static const size_t kRecordSizes[] = {
399 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
400 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
401 i++) {
David Benjamin43ec06f2014-08-05 02:28:57 -0400402 int w;
Kenny Root7fdeaf12014-08-05 15:23:37 -0700403 const size_t len = kRecordSizes[i];
404 size_t off = 0;
405
406 if (len > sizeof(buf)) {
407 fprintf(stderr, "Bad kRecordSizes value.\n");
408 return 5;
409 }
410
David Benjamin43ec06f2014-08-05 02:28:57 -0400411 do {
Kenny Root7fdeaf12014-08-05 15:23:37 -0700412 w = SSL_write(ssl, buf + off, len - off);
413 if (w > 0) {
414 off += (size_t) w;
415 }
David Benjamin5a593af2014-08-11 19:51:50 -0400416 } while ((config->async && retry_async(ssl, w, bio)) ||
417 (w > 0 && off < len));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700418
419 if (w < 0 || off != len) {
David Benjamin025b3d32014-07-01 19:53:04 -0400420 SSL_free(ssl);
421 BIO_print_errors_fp(stdout);
422 return 4;
423 }
424 }
Kenny Root7fdeaf12014-08-05 15:23:37 -0700425 } else {
426 for (;;) {
427 uint8_t buf[512];
428 int n;
429 do {
430 n = SSL_read(ssl, buf, sizeof(buf));
David Benjamin5a593af2014-08-11 19:51:50 -0400431 } while (config->async && retry_async(ssl, n, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700432 if (n < 0) {
433 SSL_free(ssl);
434 BIO_print_errors_fp(stdout);
435 return 3;
436 } else if (n == 0) {
437 break;
438 } else {
439 for (int i = 0; i < n; i++) {
440 buf[i] ^= 0xff;
441 }
442 int w;
443 do {
444 w = SSL_write(ssl, buf, n);
David Benjamin5a593af2014-08-11 19:51:50 -0400445 } while (config->async && retry_async(ssl, w, bio));
Kenny Root7fdeaf12014-08-05 15:23:37 -0700446 if (w != n) {
447 SSL_free(ssl);
448 BIO_print_errors_fp(stdout);
449 return 4;
450 }
451 }
452 }
David Benjamin025b3d32014-07-01 19:53:04 -0400453 }
454
David Benjamin1d5c83e2014-07-22 19:20:02 -0400455 if (out_session) {
456 *out_session = SSL_get1_session(ssl);
457 }
458
459 SSL_shutdown(ssl);
David Benjamin025b3d32014-07-01 19:53:04 -0400460 SSL_free(ssl);
461 return 0;
462}
David Benjamin1d5c83e2014-07-22 19:20:02 -0400463
464int main(int argc, char **argv) {
Adam Langleyded93582014-07-31 15:23:51 -0700465#if !defined(OPENSSL_WINDOWS)
David Benjamin1d5c83e2014-07-22 19:20:02 -0400466 signal(SIGPIPE, SIG_IGN);
Adam Langleyded93582014-07-31 15:23:51 -0700467#endif
David Benjamin1d5c83e2014-07-22 19:20:02 -0400468
David Benjamin5a593af2014-08-11 19:51:50 -0400469 if (!SSL_library_init()) {
470 return 1;
471 }
472 g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
473
474 TestConfig config;
475 if (!ParseConfig(argc - 1, argv + 1, &config)) {
David Benjamin1d5c83e2014-07-22 19:20:02 -0400476 return usage(argv[0]);
477 }
478
David Benjamin5a593af2014-08-11 19:51:50 -0400479 SSL_CTX *ssl_ctx = setup_ctx(&config);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400480 if (ssl_ctx == NULL) {
481 BIO_print_errors_fp(stdout);
482 return 1;
483 }
484
Adam Langleye7bf2812014-08-19 11:36:45 -0700485 SSL_SESSION *session = NULL;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400486 int ret = do_exchange(&session,
David Benjamin5a593af2014-08-11 19:51:50 -0400487 ssl_ctx, &config,
488 false /* is_resume */,
David Benjamin1d5c83e2014-07-22 19:20:02 -0400489 3 /* fd */, NULL /* session */);
490 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700491 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400492 }
493
David Benjamin5a593af2014-08-11 19:51:50 -0400494 if (config.resume) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700495 ret = do_exchange(NULL,
496 ssl_ctx, &config,
497 true /* is_resume */,
498 4 /* fd */,
499 config.is_server ? NULL : session);
David Benjamin1d5c83e2014-07-22 19:20:02 -0400500 if (ret != 0) {
Adam Langleye7bf2812014-08-19 11:36:45 -0700501 goto out;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400502 }
503 }
504
Adam Langleye7bf2812014-08-19 11:36:45 -0700505 ret = 0;
506
507out:
David Benjamin1d5c83e2014-07-22 19:20:02 -0400508 SSL_SESSION_free(session);
509 SSL_CTX_free(ssl_ctx);
Adam Langleye7bf2812014-08-19 11:36:45 -0700510 return ret;
David Benjamin1d5c83e2014-07-22 19:20:02 -0400511}