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 | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame^] | 51 | static EVP_PKEY *LoadPrivateKey(const std::string &file) { |
| 52 | BIO *bio = BIO_new(BIO_s_file()); |
| 53 | if (bio == NULL) { |
| 54 | return NULL; |
| 55 | } |
| 56 | if (!BIO_read_filename(bio, file.c_str())) { |
| 57 | BIO_free(bio); |
| 58 | return NULL; |
| 59 | } |
| 60 | EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
| 61 | BIO_free(bio); |
| 62 | return pkey; |
| 63 | } |
| 64 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 65 | static int early_callback_called = 0; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 66 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 67 | static int select_certificate_callback(const struct ssl_early_callback_ctx *ctx) { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 68 | early_callback_called = 1; |
| 69 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 70 | const TestConfig *config = GetConfigPtr(ctx->ssl); |
| 71 | |
| 72 | if (config->expected_server_name.empty()) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 73 | return 1; |
| 74 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 75 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 76 | const uint8_t *extension_data; |
| 77 | size_t extension_len; |
| 78 | CBS extension, server_name_list, host_name; |
| 79 | uint8_t name_type; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 80 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 81 | if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 82 | &extension_data, |
| 83 | &extension_len)) { |
| 84 | fprintf(stderr, "Could not find server_name extension.\n"); |
| 85 | return -1; |
| 86 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 87 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 88 | CBS_init(&extension, extension_data, extension_len); |
| 89 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) || |
| 90 | CBS_len(&extension) != 0 || |
| 91 | !CBS_get_u8(&server_name_list, &name_type) || |
| 92 | name_type != TLSEXT_NAMETYPE_host_name || |
| 93 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 94 | CBS_len(&server_name_list) != 0) { |
| 95 | fprintf(stderr, "Could not decode server_name extension.\n"); |
| 96 | return -1; |
| 97 | } |
| 98 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 99 | if (!CBS_mem_equal(&host_name, |
| 100 | (const uint8_t*)config->expected_server_name.data(), |
| 101 | config->expected_server_name.size())) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 102 | fprintf(stderr, "Server name mismatch.\n"); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return 1; |
| 106 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 107 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 108 | static int skip_verify(int preverify_ok, X509_STORE_CTX *store_ctx) { |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 112 | static int next_protos_advertised_callback(SSL *ssl, |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 113 | const uint8_t **out, |
| 114 | unsigned int *out_len, |
| 115 | void *arg) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 116 | const TestConfig *config = GetConfigPtr(ssl); |
| 117 | if (config->advertise_npn.empty()) |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 118 | return SSL_TLSEXT_ERR_NOACK; |
| 119 | |
| 120 | // TODO(davidben): Support passing byte strings with NULs to the |
| 121 | // test shim. |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 122 | *out = (const uint8_t*)config->advertise_npn.data(); |
| 123 | *out_len = config->advertise_npn.size(); |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 124 | return SSL_TLSEXT_ERR_OK; |
| 125 | } |
| 126 | |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 127 | static int next_proto_select_callback(SSL* ssl, |
| 128 | uint8_t** out, |
| 129 | uint8_t* outlen, |
| 130 | const uint8_t* in, |
| 131 | unsigned inlen, |
| 132 | void* arg) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 133 | const TestConfig *config = GetConfigPtr(ssl); |
| 134 | if (config->select_next_proto.empty()) |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 135 | return SSL_TLSEXT_ERR_NOACK; |
| 136 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 137 | *out = (uint8_t*)config->select_next_proto.data(); |
| 138 | *outlen = config->select_next_proto.size(); |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 139 | return SSL_TLSEXT_ERR_OK; |
| 140 | } |
| 141 | |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 142 | 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] | 143 | *cookie_len = 32; |
| 144 | memset(cookie, 42, *cookie_len); |
| 145 | return 1; |
| 146 | } |
| 147 | |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 148 | 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] | 149 | if (cookie_len != 32) { |
| 150 | fprintf(stderr, "Cookie length mismatch.\n"); |
| 151 | return 0; |
| 152 | } |
| 153 | for (size_t i = 0; i < cookie_len; i++) { |
| 154 | if (cookie[i] != 42) { |
| 155 | fprintf(stderr, "Cookie mismatch.\n"); |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | return 1; |
| 160 | } |
| 161 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 162 | static SSL_CTX *setup_ctx(const TestConfig *config) { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 163 | SSL_CTX *ssl_ctx = NULL; |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 164 | DH *dh = NULL; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 165 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 166 | const SSL_METHOD *method; |
| 167 | if (config->is_dtls) { |
| 168 | // TODO(davidben): Get DTLS 1.2 working and test the version negotiation |
| 169 | // codepath. This doesn't currently work because |
| 170 | // - Session resumption is broken: https://crbug.com/403378 |
| 171 | // - DTLS hasn't been updated for EVP_AEAD. |
| 172 | if (config->is_server) { |
| 173 | method = DTLSv1_server_method(); |
| 174 | } else { |
| 175 | method = DTLSv1_client_method(); |
| 176 | } |
| 177 | } else { |
| 178 | if (config->is_server) { |
| 179 | method = SSLv23_server_method(); |
| 180 | } else { |
| 181 | method = SSLv23_client_method(); |
| 182 | } |
| 183 | } |
| 184 | ssl_ctx = SSL_CTX_new(method); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 185 | if (ssl_ctx == NULL) { |
| 186 | goto err; |
| 187 | } |
| 188 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 189 | if (config->is_dtls) { |
| 190 | // DTLS needs read-ahead to function on a datagram BIO. |
| 191 | // |
| 192 | // TODO(davidben): this should not be necessary. DTLS code should only |
| 193 | // expect a datagram BIO. |
| 194 | SSL_CTX_set_read_ahead(ssl_ctx, 1); |
| 195 | } |
| 196 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 197 | if (!SSL_CTX_set_ecdh_auto(ssl_ctx, 1)) { |
| 198 | goto err; |
| 199 | } |
| 200 | |
| 201 | if (!SSL_CTX_set_cipher_list(ssl_ctx, "ALL")) { |
| 202 | goto err; |
| 203 | } |
| 204 | |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 205 | dh = DH_get_2048_256(NULL); |
| 206 | if (!SSL_CTX_set_tmp_dh(ssl_ctx, dh)) { |
| 207 | goto err; |
| 208 | } |
| 209 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 210 | SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH); |
| 211 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 212 | ssl_ctx->select_certificate_cb = select_certificate_callback; |
| 213 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 214 | SSL_CTX_set_next_protos_advertised_cb( |
| 215 | ssl_ctx, next_protos_advertised_callback, NULL); |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 216 | SSL_CTX_set_next_proto_select_cb( |
| 217 | ssl_ctx, next_proto_select_callback, NULL); |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 218 | |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 219 | SSL_CTX_set_cookie_generate_cb(ssl_ctx, cookie_generate_callback); |
| 220 | SSL_CTX_set_cookie_verify_cb(ssl_ctx, cookie_verify_callback); |
| 221 | |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame^] | 222 | ssl_ctx->tlsext_channel_id_enabled_new = 1; |
| 223 | |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 224 | DH_free(dh); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 225 | return ssl_ctx; |
| 226 | |
| 227 | err: |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 228 | if (dh != NULL) { |
| 229 | DH_free(dh); |
| 230 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 231 | if (ssl_ctx != NULL) { |
| 232 | SSL_CTX_free(ssl_ctx); |
| 233 | } |
| 234 | return NULL; |
| 235 | } |
| 236 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 237 | static int retry_async(SSL *ssl, int ret, BIO *bio) { |
| 238 | // No error; don't retry. |
| 239 | if (ret >= 0) { |
| 240 | return 0; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 241 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 242 | // See if we needed to read or write more. If so, allow one byte through on |
| 243 | // the appropriate end to maximally stress the state machine. |
| 244 | int err = SSL_get_error(ssl, ret); |
| 245 | if (err == SSL_ERROR_WANT_READ) { |
| 246 | async_bio_allow_read(bio, 1); |
| 247 | return 1; |
| 248 | } else if (err == SSL_ERROR_WANT_WRITE) { |
| 249 | async_bio_allow_write(bio, 1); |
| 250 | return 1; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 251 | } |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 252 | return 0; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 253 | } |
| 254 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 255 | static int do_exchange(SSL_SESSION **out_session, |
| 256 | SSL_CTX *ssl_ctx, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 257 | const TestConfig *config, |
| 258 | bool is_resume, |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 259 | int fd, |
| 260 | SSL_SESSION *session) { |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 261 | early_callback_called = 0; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 262 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 263 | SSL *ssl = SSL_new(ssl_ctx); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 264 | if (ssl == NULL) { |
| 265 | BIO_print_errors_fp(stdout); |
| 266 | return 1; |
| 267 | } |
| 268 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 269 | SetConfigPtr(ssl, config); |
| 270 | |
| 271 | if (config->fallback_scsv) { |
| 272 | if (!SSL_enable_fallback_scsv(ssl)) { |
| 273 | BIO_print_errors_fp(stdout); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 274 | return 1; |
| 275 | } |
| 276 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 277 | if (!config->key_file.empty()) { |
| 278 | if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(), |
| 279 | SSL_FILETYPE_PEM)) { |
| 280 | BIO_print_errors_fp(stdout); |
| 281 | return 1; |
| 282 | } |
| 283 | } |
| 284 | if (!config->cert_file.empty()) { |
| 285 | if (!SSL_use_certificate_file(ssl, config->cert_file.c_str(), |
| 286 | SSL_FILETYPE_PEM)) { |
| 287 | BIO_print_errors_fp(stdout); |
| 288 | return 1; |
| 289 | } |
| 290 | } |
| 291 | if (config->require_any_client_certificate) { |
| 292 | SSL_set_verify(ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 293 | skip_verify); |
| 294 | } |
| 295 | if (config->false_start) { |
| 296 | SSL_set_mode(ssl, SSL_MODE_HANDSHAKE_CUTTHROUGH); |
| 297 | } |
| 298 | if (config->cbc_record_splitting) { |
| 299 | SSL_set_mode(ssl, SSL_MODE_CBC_RECORD_SPLITTING); |
| 300 | } |
| 301 | if (config->partial_write) { |
| 302 | SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); |
| 303 | } |
| 304 | if (config->no_tls12) { |
| 305 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_2); |
| 306 | } |
| 307 | if (config->no_tls11) { |
| 308 | SSL_set_options(ssl, SSL_OP_NO_TLSv1_1); |
| 309 | } |
| 310 | if (config->no_tls1) { |
| 311 | SSL_set_options(ssl, SSL_OP_NO_TLSv1); |
| 312 | } |
| 313 | if (config->no_ssl3) { |
| 314 | SSL_set_options(ssl, SSL_OP_NO_SSLv3); |
| 315 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 316 | if (config->cookie_exchange) { |
| 317 | SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE); |
| 318 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame^] | 319 | if (!config->expected_channel_id.empty()) { |
| 320 | SSL_enable_tls_channel_id(ssl); |
| 321 | } |
| 322 | if (!config->send_channel_id.empty()) { |
| 323 | EVP_PKEY *pkey = LoadPrivateKey(config->send_channel_id); |
| 324 | if (pkey == NULL) { |
| 325 | BIO_print_errors_fp(stdout); |
| 326 | return 1; |
| 327 | } |
| 328 | SSL_enable_tls_channel_id(ssl); |
| 329 | if (!SSL_set1_tls_channel_id(ssl, pkey)) { |
| 330 | EVP_PKEY_free(pkey); |
| 331 | BIO_print_errors_fp(stdout); |
| 332 | return 1; |
| 333 | } |
| 334 | EVP_PKEY_free(pkey); |
| 335 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 336 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 337 | BIO *bio = BIO_new_fd(fd, 1 /* take ownership */); |
| 338 | if (bio == NULL) { |
| 339 | BIO_print_errors_fp(stdout); |
| 340 | return 1; |
| 341 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 342 | if (config->is_dtls) { |
| 343 | BIO *packeted = packeted_bio_create(); |
| 344 | BIO_push(packeted, bio); |
| 345 | bio = packeted; |
| 346 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 347 | if (config->async) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 348 | BIO *async = |
| 349 | config->is_dtls ? async_bio_create_datagram() : async_bio_create(); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 350 | BIO_push(async, bio); |
| 351 | bio = async; |
| 352 | } |
| 353 | SSL_set_bio(ssl, bio, bio); |
| 354 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 355 | if (session != NULL) { |
| 356 | if (SSL_set_session(ssl, session) != 1) { |
| 357 | fprintf(stderr, "failed to set session\n"); |
| 358 | return 2; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | int ret; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 363 | do { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 364 | if (config->is_server) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 365 | ret = SSL_accept(ssl); |
| 366 | } else { |
| 367 | ret = SSL_connect(ssl); |
| 368 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 369 | } while (config->async && retry_async(ssl, ret, bio)); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 370 | if (ret != 1) { |
| 371 | SSL_free(ssl); |
| 372 | BIO_print_errors_fp(stdout); |
| 373 | return 2; |
| 374 | } |
| 375 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 376 | if (is_resume && !SSL_session_reused(ssl)) { |
| 377 | fprintf(stderr, "session was not reused\n"); |
| 378 | return 2; |
| 379 | } |
| 380 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 381 | if (!config->expected_server_name.empty()) { |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 382 | const char *server_name = |
| 383 | SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 384 | if (server_name != config->expected_server_name) { |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 385 | fprintf(stderr, "servername mismatch (got %s; want %s)\n", |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 386 | server_name, config->expected_server_name.c_str()); |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 387 | return 2; |
| 388 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 389 | |
| 390 | if (!early_callback_called) { |
| 391 | fprintf(stderr, "early callback not called\n"); |
| 392 | return 2; |
| 393 | } |
David Benjamin | 197b3ab | 2014-07-02 18:37:33 -0400 | [diff] [blame] | 394 | } |
| 395 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 396 | if (!config->expected_certificate_types.empty()) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 397 | uint8_t *certificate_types; |
| 398 | int num_certificate_types = |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame^] | 399 | SSL_get0_certificate_types(ssl, &certificate_types); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 400 | if (num_certificate_types != |
| 401 | (int)config->expected_certificate_types.size() || |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 402 | memcmp(certificate_types, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 403 | config->expected_certificate_types.data(), |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 404 | num_certificate_types) != 0) { |
| 405 | fprintf(stderr, "certificate types mismatch\n"); |
| 406 | return 2; |
| 407 | } |
| 408 | } |
| 409 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 410 | if (!config->expected_next_proto.empty()) { |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 411 | const uint8_t *next_proto; |
| 412 | unsigned next_proto_len; |
| 413 | SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 414 | if (next_proto_len != config->expected_next_proto.size() || |
| 415 | memcmp(next_proto, config->expected_next_proto.data(), |
| 416 | next_proto_len) != 0) { |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 417 | fprintf(stderr, "negotiated next proto mismatch\n"); |
| 418 | return 2; |
| 419 | } |
| 420 | } |
| 421 | |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame^] | 422 | if (!config->expected_channel_id.empty()) { |
| 423 | uint8_t channel_id[64]; |
| 424 | if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) { |
| 425 | fprintf(stderr, "no channel id negotiated\n"); |
| 426 | return 2; |
| 427 | } |
| 428 | if (config->expected_channel_id.size() != 64 || |
| 429 | memcmp(config->expected_channel_id.data(), |
| 430 | channel_id, 64) != 0) { |
| 431 | fprintf(stderr, "channel id mismatch\n"); |
| 432 | return 2; |
| 433 | } |
| 434 | } |
| 435 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 436 | if (config->write_different_record_sizes) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 437 | if (config->is_dtls) { |
| 438 | fprintf(stderr, "write_different_record_sizes not supported for DTLS\n"); |
| 439 | return 6; |
| 440 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 441 | // This mode writes a number of different record sizes in an attempt to |
| 442 | // trip up the CBC record splitting code. |
| 443 | uint8_t buf[32769]; |
| 444 | memset(buf, 0x42, sizeof(buf)); |
| 445 | static const size_t kRecordSizes[] = { |
| 446 | 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769}; |
| 447 | for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]); |
| 448 | i++) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 449 | int w; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 450 | const size_t len = kRecordSizes[i]; |
| 451 | size_t off = 0; |
| 452 | |
| 453 | if (len > sizeof(buf)) { |
| 454 | fprintf(stderr, "Bad kRecordSizes value.\n"); |
| 455 | return 5; |
| 456 | } |
| 457 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 458 | do { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 459 | w = SSL_write(ssl, buf + off, len - off); |
| 460 | if (w > 0) { |
| 461 | off += (size_t) w; |
| 462 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 463 | } while ((config->async && retry_async(ssl, w, bio)) || |
| 464 | (w > 0 && off < len)); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 465 | |
| 466 | if (w < 0 || off != len) { |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 467 | SSL_free(ssl); |
| 468 | BIO_print_errors_fp(stdout); |
| 469 | return 4; |
| 470 | } |
| 471 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 472 | } else { |
| 473 | for (;;) { |
| 474 | uint8_t buf[512]; |
| 475 | int n; |
| 476 | do { |
| 477 | n = SSL_read(ssl, buf, sizeof(buf)); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 478 | } while (config->async && retry_async(ssl, n, bio)); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 479 | if (n < 0) { |
| 480 | SSL_free(ssl); |
| 481 | BIO_print_errors_fp(stdout); |
| 482 | return 3; |
| 483 | } else if (n == 0) { |
| 484 | break; |
| 485 | } else { |
| 486 | for (int i = 0; i < n; i++) { |
| 487 | buf[i] ^= 0xff; |
| 488 | } |
| 489 | int w; |
| 490 | do { |
| 491 | w = SSL_write(ssl, buf, n); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 492 | } while (config->async && retry_async(ssl, w, bio)); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 493 | if (w != n) { |
| 494 | SSL_free(ssl); |
| 495 | BIO_print_errors_fp(stdout); |
| 496 | return 4; |
| 497 | } |
| 498 | } |
| 499 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 500 | } |
| 501 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 502 | if (out_session) { |
| 503 | *out_session = SSL_get1_session(ssl); |
| 504 | } |
| 505 | |
| 506 | SSL_shutdown(ssl); |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 507 | SSL_free(ssl); |
| 508 | return 0; |
| 509 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 510 | |
| 511 | int main(int argc, char **argv) { |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 512 | #if !defined(OPENSSL_WINDOWS) |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 513 | signal(SIGPIPE, SIG_IGN); |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 514 | #endif |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 515 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 516 | if (!SSL_library_init()) { |
| 517 | return 1; |
| 518 | } |
| 519 | g_ex_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 520 | |
| 521 | TestConfig config; |
| 522 | if (!ParseConfig(argc - 1, argv + 1, &config)) { |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 523 | return usage(argv[0]); |
| 524 | } |
| 525 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 526 | SSL_CTX *ssl_ctx = setup_ctx(&config); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 527 | if (ssl_ctx == NULL) { |
| 528 | BIO_print_errors_fp(stdout); |
| 529 | return 1; |
| 530 | } |
| 531 | |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 532 | SSL_SESSION *session = NULL; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 533 | int ret = do_exchange(&session, |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 534 | ssl_ctx, &config, |
| 535 | false /* is_resume */, |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 536 | 3 /* fd */, NULL /* session */); |
| 537 | if (ret != 0) { |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 538 | goto out; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 539 | } |
| 540 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 541 | if (config.resume) { |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 542 | ret = do_exchange(NULL, |
| 543 | ssl_ctx, &config, |
| 544 | true /* is_resume */, |
| 545 | 4 /* fd */, |
| 546 | config.is_server ? NULL : session); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 547 | if (ret != 0) { |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 548 | goto out; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 552 | ret = 0; |
| 553 | |
| 554 | out: |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 555 | SSL_SESSION_free(session); |
| 556 | SSL_CTX_free(ssl_ctx); |
Adam Langley | e7bf281 | 2014-08-19 11:36:45 -0700 | [diff] [blame] | 557 | return ret; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 558 | } |