1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
22 #include <openssl/bio.h>
23 #include <openssl/bytestring.h>
24 #include <openssl/ssl.h>
26 #include "async_bio.h"
28 static int usage(const char *program) {
29 fprintf(stderr, "Usage: %s (client|server) (normal|resume) [flags...]\n",
34 static const char *expected_server_name = NULL;
35 static int early_callback_called = 0;
37 static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) {
38 early_callback_called = 1;
40 if (!expected_server_name) {
44 const uint8_t *extension_data;
46 CBS extension, server_name_list, host_name;
49 if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
52 fprintf(stderr, "Could not find server_name extension.\n");
56 CBS_init(&extension, extension_data, extension_len);
57 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
58 CBS_len(&extension) != 0 ||
59 !CBS_get_u8(&server_name_list, &name_type) ||
60 name_type != TLSEXT_NAMETYPE_host_name ||
61 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
62 CBS_len(&server_name_list) != 0) {
63 fprintf(stderr, "Could not decode server_name extension.\n");
67 if (!CBS_mem_equal(&host_name, (const uint8_t*)expected_server_name,
68 strlen(expected_server_name))) {
69 fprintf(stderr, "Server name mismatch.\n");
75 static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) {
79 static const char *advertise_npn = NULL;
81 static int next_protos_advertised_callback(SSL *ssl,
83 unsigned int *out_len,
86 return SSL_TLSEXT_ERR_NOACK;
88 // TODO(davidben): Support passing byte strings with NULs to the
90 *out = (const uint8_t*)advertise_npn;
91 *out_len = strlen(advertise_npn);
92 return SSL_TLSEXT_ERR_OK;
95 static const char *select_next_proto = NULL;
97 static int next_proto_select_callback(SSL* ssl,
103 if (!select_next_proto)
104 return SSL_TLSEXT_ERR_NOACK;
106 *out = (uint8_t*)select_next_proto;
107 *outlen = strlen(select_next_proto);
108 return SSL_TLSEXT_ERR_OK;
111 static SSL_CTX *setup_ctx(int is_server) {
112 if (!SSL_library_init()) {
116 SSL_CTX *ssl_ctx = NULL;
119 ssl_ctx = SSL_CTX_new(
120 is_server ? SSLv23_server_method() : SSLv23_client_method());
121 if (ssl_ctx == NULL) {
125 if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) {
129 if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) {
133 dh = DH_get_2048_256(NULL);
134 if (!SSL_CTX_set_tmp_dh(ssl_ctx, dh)) {
138 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
140 ssl_ctx->select_certificate_cb = select_certificate_callback;
142 SSL_CTX_set_next_protos_advertised_cb(
143 ssl_ctx, next_protos_advertised_callback, NULL);
144 SSL_CTX_set_next_proto_select_cb(
145 ssl_ctx, next_proto_select_callback, NULL);
154 if (ssl_ctx != NULL) {
155 SSL_CTX_free(ssl_ctx);
160 static int retry_async(SSL *ssl, int ret, BIO *bio) {
161 // No error; don't retry.
165 // See if we needed to read or write more. If so, allow one byte through on
166 // the appropriate end to maximally stress the state machine.
167 int err = SSL_get_error(ssl, ret);
168 if (err == SSL_ERROR_WANT_READ) {
169 async_bio_allow_read(bio, 1);
171 } else if (err == SSL_ERROR_WANT_WRITE) {
172 async_bio_allow_write(bio, 1);
178 static int do_exchange(SSL_SESSION **out_session,
185 SSL_SESSION *session) {
186 bool async = false, write_different_record_sizes = false;
187 const char *expected_certificate_types = NULL;
188 const char *expected_next_proto = NULL;
189 expected_server_name = NULL;
190 early_callback_called = 0;
191 advertise_npn = NULL;
193 SSL *ssl = SSL_new(ssl_ctx);
195 BIO_print_errors_fp(stdout);
199 for (int i = 0; i < argc; i++) {
200 if (strcmp(argv[i], "-fallback-scsv") == 0) {
201 if (!SSL_enable_fallback_scsv(ssl)) {
202 BIO_print_errors_fp(stdout);
205 } else if (strcmp(argv[i], "-key-file") == 0) {
208 fprintf(stderr, "Missing parameter\n");
211 if (!SSL_use_PrivateKey_file(ssl, argv[i], SSL_FILETYPE_PEM)) {
212 BIO_print_errors_fp(stdout);
215 } else if (strcmp(argv[i], "-cert-file") == 0) {
218 fprintf(stderr, "Missing parameter\n");
221 if (!SSL_use_certificate_file(ssl, argv[i], SSL_FILETYPE_PEM)) {
222 BIO_print_errors_fp(stdout);
225 } else if (strcmp(argv[i], "-expect-server-name") == 0) {
228 fprintf(stderr, "Missing parameter\n");
231 expected_server_name = argv[i];
232 } else if (strcmp(argv[i], "-expect-certificate-types") == 0) {
235 fprintf(stderr, "Missing parameter\n");
238 // Conveniently, 00 is not a certificate type.
239 expected_certificate_types = argv[i];
240 } else if (strcmp(argv[i], "-require-any-client-certificate") == 0) {
241 SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
243 } else if (strcmp(argv[i], "-advertise-npn") == 0) {
246 fprintf(stderr, "Missing parameter\n");
249 advertise_npn = argv[i];
250 } else if (strcmp(argv[i], "-expect-next-proto") == 0) {
253 fprintf(stderr, "Missing parameter\n");
256 expected_next_proto = argv[i];
257 } else if (strcmp(argv[i], "-false-start") == 0) {
258 SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH);
259 } else if (strcmp(argv[i], "-select-next-proto") == 0) {
262 fprintf(stderr, "Missing parameter\n");
265 select_next_proto = argv[i];
266 } else if (strcmp(argv[i], "-async") == 0) {
268 } else if (strcmp(argv[i], "-write-different-record-sizes") == 0) {
269 write_different_record_sizes = true;
270 } else if (strcmp(argv[i], "-cbc-record-splitting") == 0) {
271 SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING);
272 } else if (strcmp(argv[i], "-partial-write") == 0) {
273 SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
275 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
280 BIO *bio = BIO_new_fd(fd, 1 /* take ownership */);
282 BIO_print_errors_fp(stdout);
286 BIO *async = async_bio_create();
287 BIO_push(async, bio);
290 SSL_set_bio(ssl, bio, bio);
292 if (session != NULL) {
293 if (SSL_set_session(ssl, session) != 1) {
294 fprintf(stderr, "failed to set session\n");
302 ret = SSL_accept(ssl);
304 ret = SSL_connect(ssl);
306 } while (async && retry_async(ssl, ret, bio));
309 BIO_print_errors_fp(stdout);
313 if (is_resume && !SSL_session_reused(ssl)) {
314 fprintf(stderr, "session was not reused\n");
318 if (expected_server_name) {
319 const char *server_name =
320 SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
321 if (strcmp(server_name, expected_server_name) != 0) {
322 fprintf(stderr, "servername mismatch (got %s; want %s)\n",
323 server_name, expected_server_name);
327 if (!early_callback_called) {
328 fprintf(stderr, "early callback not called\n");
333 if (expected_certificate_types) {
334 uint8_t *certificate_types;
335 int num_certificate_types =
336 SSL_get0_certificate_types(ssl, &certificate_types);
337 if (num_certificate_types != (int)strlen(expected_certificate_types) ||
338 memcmp(certificate_types,
339 expected_certificate_types,
340 num_certificate_types) != 0) {
341 fprintf(stderr, "certificate types mismatch\n");
346 if (expected_next_proto) {
347 const uint8_t *next_proto;
348 unsigned next_proto_len;
349 SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
350 if (next_proto_len != strlen(expected_next_proto) ||
351 memcmp(next_proto, expected_next_proto, next_proto_len) != 0) {
352 fprintf(stderr, "negotiated next proto mismatch\n");
357 if (write_different_record_sizes) {
358 // This mode writes a number of different record sizes in an attempt to
359 // trip up the CBC record splitting code.
361 memset(buf, 0x42, sizeof(buf));
362 static const size_t kRecordSizes[] = {
363 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
364 for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]);
367 const size_t len = kRecordSizes[i];
370 if (len > sizeof(buf)) {
371 fprintf(stderr, "Bad kRecordSizes value.\n");
376 w = SSL_write(ssl, buf + off, len - off);
380 } while ((async && retry_async(ssl, w, bio)) || (w > 0 && off < len));
382 if (w < 0 || off != len) {
384 BIO_print_errors_fp(stdout);
393 n = SSL_read(ssl, buf, sizeof(buf));
394 } while (async && retry_async(ssl, n, bio));
397 BIO_print_errors_fp(stdout);
402 for (int i = 0; i < n; i++) {
407 w = SSL_write(ssl, buf, n);
408 } while (async && retry_async(ssl, w, bio));
411 BIO_print_errors_fp(stdout);
419 *out_session = SSL_get1_session(ssl);
427 int main(int argc, char **argv) {
428 int is_server, resume;
430 signal(SIGPIPE, SIG_IGN);
433 return usage(argv[0]);
436 if (strcmp(argv[1], "client") == 0) {
438 } else if (strcmp(argv[1], "server") == 0) {
441 return usage(argv[0]);
444 if (strcmp(argv[2], "normal") == 0) {
446 } else if (strcmp(argv[2], "resume") == 0) {
449 return usage(argv[0]);
452 SSL_CTX *ssl_ctx = setup_ctx(is_server);
453 if (ssl_ctx == NULL) {
454 BIO_print_errors_fp(stdout);
458 SSL_SESSION *session;
459 int ret = do_exchange(&session,
462 is_server, 0 /* is_resume */,
463 3 /* fd */, NULL /* session */);
469 int ret = do_exchange(NULL,
470 ssl_ctx, argc - 3, argv + 3,
471 is_server, 1 /* is_resume */,
473 is_server ? NULL : session);
479 SSL_SESSION_free(session);
480 SSL_CTX_free(ssl_ctx);