David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1 | /* 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 Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 15 | #include <openssl/base.h> |
| 16 | |
| 17 | #if !defined(OPENSSL_WINDOWS) |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 18 | #include <arpa/inet.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 19 | #include <netinet/in.h> |
| 20 | #include <signal.h> |
| 21 | #include <sys/socket.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 22 | #include <unistd.h> |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #include <sys/types.h> |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 26 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 27 | #include <openssl/bio.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 28 | #include <openssl/bytestring.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 29 | #include <openssl/ssl.h> |
| 30 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 31 | #include "async_bio.h" |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 32 | #include "packeted_bio.h" |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 33 | #include "test_config.h" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 34 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 35 | static int usage(const char *program) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 36 | fprintf(stderr, "Usage: %s [flags...]\n", |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 37 | program); |
| 38 | return 1; |
| 39 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 40 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 41 | static int g_ex_data_index = 0; |
| 42 | |
| 43 | static void SetConfigPtr(SSL *ssl, const TestConfig *config) { |
| 44 | SSL_set_ex_data(ssl, g_ex_data_index, (void *)config); |
| 45 | } |
| 46 | |
| 47 | static const TestConfig *GetConfigPtr(SSL *ssl) { |
| 48 | return (const TestConfig *)SSL_get_ex_data(ssl, g_ex_data_index); |
| 49 | } |
| 50 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 51 | static int early_callback_called = 0; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 52 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 53 | static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 54 | early_callback_called = 1; |
| 55 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 56 | const TestConfig *config = GetConfigPtr(ctx->ssl); |
| 57 | |
| 58 | if (config->expected_server_name.empty()) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 59 | return 1; |
| 60 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 61 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 62 | const uint8_t *extension_data; |
| 63 | size_t extension_len; |
| 64 | CBS extension, server_name_list, host_name; |
| 65 | uint8_t name_type; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 66 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 67 | 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 Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 73 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 74 | 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 Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 85 | if (!CBS_mem_equal(&host_name, |
| 86 | (const uint8_t*)config->expected_server_name.data(), |
| 87 | config->expected_server_name.size())) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 88 | fprintf(stderr, "Server name mismatch.\n"); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return 1; |
| 92 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 93 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 94 | static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) { |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 98 | static int next_protos_advertised_callback(SSL *ssl, |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 99 | const uint8_t **out, |
| 100 | unsigned int *out_len, |
| 101 | void *arg) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 102 | const TestConfig *config = GetConfigPtr(ssl); |
| 103 | if (config->advertise_npn.empty()) |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 104 | return SSL_TLSEXT_ERR_NOACK; |
| 105 | |
| 106 | // TODO(davidben): Support passing byte strings with NULs to the |
| 107 | // test shim. |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 108 | *out = (const uint8_t*)config->advertise_npn.data(); |
| 109 | *out_len = config->advertise_npn.size(); |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 110 | return SSL_TLSEXT_ERR_OK; |
| 111 | } |
| 112 | |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 113 | static 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 Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 119 | const TestConfig *config = GetConfigPtr(ssl); |
| 120 | if (config->select_next_proto.empty()) |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 121 | return SSL_TLSEXT_ERR_NOACK; |
| 122 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 123 | *out = (uint8_t*)config->select_next_proto.data(); |
| 124 | *outlen = config->select_next_proto.size(); |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 125 | return SSL_TLSEXT_ERR_OK; |
| 126 | } |
| 127 | |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 128 | static int cookie_generate_callback(SSL *ssl, uint8_t *cookie, size_t *cookie_len) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 129 | *cookie_len = 32; |
| 130 | memset(cookie, 42, *cookie_len); |
| 131 | return 1; |
| 132 | } |
| 133 | |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 134 | static int cookie_verify_callback(SSL *ssl, const uint8_t *cookie, size_t cookie_len) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 135 | 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 Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 148 | static SSL_CTX *setup_ctx(const TestConfig *config) { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 149 | SSL_CTX *ssl_ctx = NULL; |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 150 | DH *dh = NULL; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 151 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 152 | 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 Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 171 | if (ssl_ctx == NULL) { |
| 172 | goto err; |
| 173 | } |
| 174 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 175 | 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 Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 183 | 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 Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 191 | dh = DH_get_2048_256(NULL); |
| 192 | if (!SSL_CTX_set_tmp_dh(ssl_ctx, dh)) { |
| 193 | goto err; |
| 194 | } |
| 195 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 196 | SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH); |
| 197 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 198 | ssl_ctx->select_certificate_cb = select_certificate_callback; |
| 199 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 200 | SSL_CTX_set_next_protos_advertised_cb( |
| 201 | ssl_ctx, next_protos_advertised_callback, NULL); |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 202 | SSL_CTX_set_next_proto_select_cb( |
| 203 | ssl_ctx, next_proto_select_callback, NULL); |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 204 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 205 | 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 Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 208 | DH_free(dh); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 209 | return ssl_ctx; |
| 210 | |
| 211 | err: |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 212 | if (dh != NULL) { |
| 213 | DH_free(dh); |
| 214 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 215 | if (ssl_ctx != NULL) { |
| 216 | SSL_CTX_free(ssl_ctx); |
| 217 | } |
| 218 | return NULL; |
| 219 | } |
| 220 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 221 | static int retry_async(SSL *ssl, int ret, BIO *bio) { |
| 222 | // No error; don't retry. |
| 223 | if (ret >= 0) { |
| 224 | return 0; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 225 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 226 | // 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 Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 235 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 236 | return 0; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 237 | } |
| 238 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 239 | static int do_exchange(SSL_SESSION **out_session, |
| 240 | SSL_CTX *ssl_ctx, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 241 | const TestConfig *config, |
| 242 | bool is_resume, |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 243 | int fd, |
| 244 | SSL_SESSION *session) { |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 245 | early_callback_called = 0; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 246 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 247 | SSL *ssl = SSL_new(ssl_ctx); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 248 | if (ssl == NULL) { |
| 249 | BIO_print_errors_fp(stdout); |
| 250 | return 1; |
| 251 | } |
| 252 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 253 | SetConfigPtr(ssl, config); |
| 254 | |
| 255 | if (config->fallback_scsv) { |
| 256 | if (!SSL_enable_fallback_scsv(ssl)) { |
| 257 | BIO_print_errors_fp(stdout); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 258 | return 1; |
| 259 | } |
| 260 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 261 | 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 Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 300 | if (config->cookie_exchange) { |
| 301 | SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); |
| 302 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 303 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 304 | BIO *bio = BIO_new_fd(fd, 1 /* take ownership */); |
| 305 | if (bio == NULL) { |
| 306 | BIO_print_errors_fp(stdout); |
| 307 | return 1; |
| 308 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 309 | if (config->is_dtls) { |
| 310 | BIO *packeted = packeted_bio_create(); |
| 311 | BIO_push(packeted, bio); |
| 312 | bio = packeted; |
| 313 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 314 | if (config->async) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 315 | BIO *async = |
| 316 | config->is_dtls ? async_bio_create_datagram() : async_bio_create(); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 317 | BIO_push(async, bio); |
| 318 | bio = async; |
| 319 | } |
| 320 | SSL_set_bio(ssl, bio, bio); |
| 321 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 322 | 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 Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 330 | do { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 331 | if (config->is_server) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 332 | ret = SSL_accept(ssl); |
| 333 | } else { |
| 334 | ret = SSL_connect(ssl); |
| 335 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 336 | } while (config->async && retry_async(ssl, ret, bio)); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 337 | if (ret != 1) { |
| 338 | SSL_free(ssl); |
| 339 | BIO_print_errors_fp(stdout); |
| 340 | return 2; |
| 341 | } |
| 342 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 343 | if (is_resume && !SSL_session_reused(ssl)) { |
| 344 | fprintf(stderr, "session was not reused\n"); |
| 345 | return 2; |
| 346 | } |
| 347 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 348 | if (!config->expected_server_name.empty()) { |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 349 | const char *server_name = |
| 350 | SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 351 | if (server_name != config->expected_server_name) { |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 352 | fprintf(stderr, "servername mismatch (got %s; want %s)\n", |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 353 | server_name, config->expected_server_name.c_str()); |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 354 | return 2; |
| 355 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 356 | |
| 357 | if (!early_callback_called) { |
| 358 | fprintf(stderr, "early callback not called\n"); |
| 359 | return 2; |
| 360 | } |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 361 | } |
| 362 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 363 | if (!config->expected_certificate_types.empty()) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 364 | uint8_t *certificate_types; |
| 365 | int num_certificate_types = |
| 366 | SSL_get0_certificate_types(ssl, &certificate_types); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 367 | if (num_certificate_types != |
| 368 | (int)config->expected_certificate_types.size() || |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 369 | memcmp(certificate_types, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 370 | config->expected_certificate_types.data(), |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 371 | num_certificate_types) != 0) { |
| 372 | fprintf(stderr, "certificate types mismatch\n"); |
| 373 | return 2; |
| 374 | } |
| 375 | } |
| 376 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 377 | if (!config->expected_next_proto.empty()) { |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 378 | const uint8_t *next_proto; |
| 379 | unsigned next_proto_len; |
| 380 | SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 381 | if (next_proto_len != config->expected_next_proto.size() || |
| 382 | memcmp(next_proto, config->expected_next_proto.data(), |
| 383 | next_proto_len) != 0) { |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 384 | fprintf(stderr, "negotiated next proto mismatch\n"); |
| 385 | return 2; |
| 386 | } |
| 387 | } |
| 388 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 389 | if (config->write_different_record_sizes) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 390 | if (config->is_dtls) { |
| 391 | fprintf(stderr, "write_different_record_sizes not supported for DTLS\n"); |
| 392 | return 6; |
| 393 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 394 | // 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 Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 402 | int w; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 403 | 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 Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 411 | do { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 412 | w = SSL_write(ssl, buf + off, len - off); |
| 413 | if (w > 0) { |
| 414 | off += (size_t) w; |
| 415 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 416 | } while ((config->async && retry_async(ssl, w, bio)) || |
| 417 | (w > 0 && off < len)); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 418 | |
| 419 | if (w < 0 || off != len) { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 420 | SSL_free(ssl); |
| 421 | BIO_print_errors_fp(stdout); |
| 422 | return 4; |
| 423 | } |
| 424 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 425 | } else { |
| 426 | for (;;) { |
| 427 | uint8_t buf[512]; |
| 428 | int n; |
| 429 | do { |
| 430 | n = SSL_read(ssl, buf, sizeof(buf)); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 431 | } while (config->async && retry_async(ssl, n, bio)); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 432 | 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 Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 445 | } while (config->async && retry_async(ssl, w, bio)); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 446 | if (w != n) { |
| 447 | SSL_free(ssl); |
| 448 | BIO_print_errors_fp(stdout); |
| 449 | return 4; |
| 450 | } |
| 451 | } |
| 452 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 453 | } |
| 454 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 455 | if (out_session) { |
| 456 | *out_session = SSL_get1_session(ssl); |
| 457 | } |
| 458 | |
| 459 | SSL_shutdown(ssl); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 460 | SSL_free(ssl); |
| 461 | return 0; |
| 462 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 463 | |
| 464 | int main(int argc, char **argv) { |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 465 | #if !defined(OPENSSL_WINDOWS) |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 466 | signal(SIGPIPE, SIG_IGN); |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 467 | #endif |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 468 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 469 | 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 Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 476 | return usage(argv[0]); |
| 477 | } |
| 478 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 479 | SSL_CTX *ssl_ctx = setup_ctx(&config); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 480 | if (ssl_ctx == NULL) { |
| 481 | BIO_print_errors_fp(stdout); |
| 482 | return 1; |
| 483 | } |
| 484 | |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 485 | SSL_SESSION *session = NULL; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 486 | int ret = do_exchange(&session, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 487 | ssl_ctx, &config, |
| 488 | false /* is_resume */, |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 489 | 3 /* fd */, NULL /* session */); |
| 490 | if (ret != 0) { |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 491 | goto out; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 492 | } |
| 493 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 494 | if (config.resume) { |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 495 | ret = do_exchange(NULL, |
| 496 | ssl_ctx, &config, |
| 497 | true /* is_resume */, |
| 498 | 4 /* fd */, |
| 499 | config.is_server ? NULL : session); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 500 | if (ret != 0) { |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 501 | goto out; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 505 | ret = 0; |
| 506 | |
| 507 | out: |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 508 | SSL_SESSION_free(session); |
| 509 | SSL_CTX_free(ssl_ctx); |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 510 | return ret; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 511 | } |