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> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 20 | #include <netinet/tcp.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 21 | #include <signal.h> |
| 22 | #include <sys/socket.h> |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 23 | #include <sys/types.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 24 | #include <unistd.h> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 25 | #else |
| 26 | #include <io.h> |
| 27 | #pragma warning(push, 3) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 28 | #include <winsock2.h> |
| 29 | #include <ws2tcpip.h> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 30 | #pragma warning(pop) |
| 31 | |
| 32 | #pragma comment(lib, "Ws2_32.lib") |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 33 | #endif |
| 34 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 35 | #include <string.h> |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 36 | #include <sys/types.h> |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 37 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 38 | #include <openssl/bio.h> |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 39 | #include <openssl/buf.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 40 | #include <openssl/bytestring.h> |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 41 | #include <openssl/err.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 42 | #include <openssl/ssl.h> |
| 43 | |
David Benjamin | 45fb1be | 2015-03-22 16:31:27 -0400 | [diff] [blame] | 44 | #include <memory> |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 45 | #include <vector> |
David Benjamin | 45fb1be | 2015-03-22 16:31:27 -0400 | [diff] [blame] | 46 | |
| 47 | #include "../../crypto/test/scoped_types.h" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 48 | #include "async_bio.h" |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 49 | #include "packeted_bio.h" |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 50 | #include "scoped_types.h" |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 51 | #include "test_config.h" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 52 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 53 | |
| 54 | #if !defined(OPENSSL_WINDOWS) |
| 55 | static int closesocket(int sock) { |
| 56 | return close(sock); |
| 57 | } |
| 58 | |
| 59 | static void PrintSocketError(const char *func) { |
| 60 | perror(func); |
| 61 | } |
| 62 | #else |
| 63 | static void PrintSocketError(const char *func) { |
| 64 | fprintf(stderr, "%s: %d\n", func, WSAGetLastError()); |
| 65 | } |
| 66 | #endif |
| 67 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 68 | static int Usage(const char *program) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 69 | fprintf(stderr, "Usage: %s [flags...]\n", program); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 70 | return 1; |
| 71 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 72 | |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 73 | struct TestState { |
David Benjamin | ff9c74f | 2015-04-06 20:17:56 -0400 | [diff] [blame] | 74 | TestState() { |
| 75 | // MSVC cannot initialize these inline. |
| 76 | memset(&clock, 0, sizeof(clock)); |
| 77 | memset(&clock_delta, 0, sizeof(clock_delta)); |
| 78 | } |
| 79 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 80 | // async_bio is async BIO which pauses reads and writes. |
| 81 | BIO *async_bio = nullptr; |
| 82 | // clock is the current time for the SSL connection. |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 83 | timeval clock; |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 84 | // clock_delta is how far the clock advanced in the most recent failed |
| 85 | // |BIO_read|. |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 86 | timeval clock_delta; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 87 | ScopedEVP_PKEY channel_id; |
David Benjamin | 0d4db50 | 2015-03-23 18:46:05 -0400 | [diff] [blame] | 88 | bool cert_ready = false; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 89 | ScopedSSL_SESSION session; |
| 90 | ScopedSSL_SESSION pending_session; |
David Benjamin | 0d4db50 | 2015-03-23 18:46:05 -0400 | [diff] [blame] | 91 | bool early_callback_called = false; |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 92 | bool handshake_done = false; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 93 | // private_key is the underlying private key used when testing custom keys. |
| 94 | ScopedEVP_PKEY private_key; |
| 95 | std::vector<uint8_t> signature; |
| 96 | // signature_retries is the number of times an asynchronous sign operation has |
| 97 | // been retried. |
| 98 | unsigned signature_retries = 0; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 101 | static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 102 | int index, long argl, void *argp) { |
| 103 | delete ((TestState *)ptr); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int g_config_index = 0; |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 107 | static int g_state_index = 0; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 108 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 109 | static bool SetConfigPtr(SSL *ssl, const TestConfig *config) { |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 110 | return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 111 | } |
| 112 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 113 | static const TestConfig *GetConfigPtr(const SSL *ssl) { |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 114 | return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 115 | } |
| 116 | |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 117 | static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> async) { |
| 118 | if (SSL_set_ex_data(ssl, g_state_index, (void *)async.get()) == 1) { |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 119 | async.release(); |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 125 | static TestState *GetTestState(const SSL *ssl) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 126 | return (TestState *)SSL_get_ex_data(ssl, g_state_index); |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 127 | } |
| 128 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 129 | static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) { |
| 130 | ScopedBIO bio(BIO_new(BIO_s_file())); |
| 131 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
| 132 | return nullptr; |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 133 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 134 | ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 135 | return pkey; |
| 136 | } |
| 137 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 138 | static int AsyncPrivateKeyType(SSL *ssl) { |
| 139 | return EVP_PKEY_id(GetTestState(ssl)->private_key.get()); |
| 140 | } |
| 141 | |
| 142 | static int AsyncPrivateKeySupportsDigest(SSL *ssl, const EVP_MD *md) { |
| 143 | return EVP_PKEY_supports_digest(GetTestState(ssl)->private_key.get(), md); |
| 144 | } |
| 145 | |
| 146 | static size_t AsyncPrivateKeyMaxSignatureLen(SSL *ssl) { |
| 147 | return EVP_PKEY_size(GetTestState(ssl)->private_key.get()); |
| 148 | } |
| 149 | |
| 150 | static ssl_private_key_result_t AsyncPrivateKeySign( |
| 151 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 152 | const EVP_MD *md, const uint8_t *in, size_t in_len) { |
| 153 | TestState *test_state = GetTestState(ssl); |
| 154 | if (!test_state->signature.empty()) { |
| 155 | fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n"); |
| 156 | abort(); |
| 157 | } |
| 158 | |
| 159 | ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(test_state->private_key.get(), |
| 160 | nullptr)); |
| 161 | if (!ctx) { |
| 162 | return ssl_private_key_failure; |
| 163 | } |
| 164 | |
| 165 | // Write the signature into |test_state|. |
| 166 | size_t len = 0; |
| 167 | if (!EVP_PKEY_sign_init(ctx.get()) || |
| 168 | !EVP_PKEY_CTX_set_signature_md(ctx.get(), md) || |
| 169 | !EVP_PKEY_sign(ctx.get(), nullptr, &len, in, in_len)) { |
| 170 | return ssl_private_key_failure; |
| 171 | } |
| 172 | test_state->signature.resize(len); |
| 173 | if (!EVP_PKEY_sign(ctx.get(), bssl::vector_data(&test_state->signature), &len, |
| 174 | in, in_len)) { |
| 175 | return ssl_private_key_failure; |
| 176 | } |
| 177 | test_state->signature.resize(len); |
| 178 | |
| 179 | // The signature will be released asynchronously in |AsyncPrivateKeySignComplete|. |
| 180 | return ssl_private_key_retry; |
| 181 | } |
| 182 | |
| 183 | static ssl_private_key_result_t AsyncPrivateKeySignComplete( |
| 184 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) { |
| 185 | TestState *test_state = GetTestState(ssl); |
| 186 | if (test_state->signature.empty()) { |
| 187 | fprintf(stderr, |
| 188 | "AsyncPrivateKeySignComplete called without operation pending.\n"); |
| 189 | abort(); |
| 190 | } |
| 191 | |
| 192 | if (test_state->signature_retries < 2) { |
| 193 | // Only return the signature on the second attempt, to test both incomplete |
| 194 | // |sign| and |sign_complete|. |
| 195 | return ssl_private_key_retry; |
| 196 | } |
| 197 | |
| 198 | if (max_out < test_state->signature.size()) { |
| 199 | fprintf(stderr, "Output buffer too small.\n"); |
| 200 | return ssl_private_key_failure; |
| 201 | } |
| 202 | memcpy(out, bssl::vector_data(&test_state->signature), |
| 203 | test_state->signature.size()); |
| 204 | |
| 205 | test_state->signature.clear(); |
| 206 | test_state->signature_retries = 0; |
| 207 | return ssl_private_key_success; |
| 208 | } |
| 209 | |
| 210 | static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = { |
| 211 | AsyncPrivateKeyType, |
| 212 | AsyncPrivateKeySupportsDigest, |
| 213 | AsyncPrivateKeyMaxSignatureLen, |
| 214 | AsyncPrivateKeySign, |
| 215 | AsyncPrivateKeySignComplete, |
| 216 | }; |
| 217 | |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 218 | static bool InstallCertificate(SSL *ssl) { |
| 219 | const TestConfig *config = GetConfigPtr(ssl); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 220 | TestState *test_state = GetTestState(ssl); |
| 221 | if (!config->key_file.empty()) { |
| 222 | if (config->use_async_private_key) { |
| 223 | test_state->private_key = LoadPrivateKey(config->key_file.c_str()); |
| 224 | if (!test_state->private_key) { |
| 225 | return false; |
| 226 | } |
| 227 | SSL_set_private_key_method(ssl, &g_async_private_key_method); |
| 228 | } else if (!SSL_use_PrivateKey_file(ssl, config->key_file.c_str(), |
| 229 | SSL_FILETYPE_PEM)) { |
| 230 | return false; |
| 231 | } |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 232 | } |
| 233 | if (!config->cert_file.empty() && |
| 234 | !SSL_use_certificate_file(ssl, config->cert_file.c_str(), |
| 235 | SSL_FILETYPE_PEM)) { |
| 236 | return false; |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 241 | static int SelectCertificateCallback(const struct ssl_early_callback_ctx *ctx) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 242 | const TestConfig *config = GetConfigPtr(ctx->ssl); |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 243 | GetTestState(ctx->ssl)->early_callback_called = true; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 244 | |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 245 | if (!config->expected_server_name.empty()) { |
| 246 | const uint8_t *extension_data; |
| 247 | size_t extension_len; |
| 248 | CBS extension, server_name_list, host_name; |
| 249 | uint8_t name_type; |
| 250 | |
| 251 | if (!SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 252 | &extension_data, |
| 253 | &extension_len)) { |
| 254 | fprintf(stderr, "Could not find server_name extension.\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | CBS_init(&extension, extension_data, extension_len); |
| 259 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) || |
| 260 | CBS_len(&extension) != 0 || |
| 261 | !CBS_get_u8(&server_name_list, &name_type) || |
| 262 | name_type != TLSEXT_NAMETYPE_host_name || |
| 263 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 264 | CBS_len(&server_name_list) != 0) { |
| 265 | fprintf(stderr, "Could not decode server_name extension.\n"); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | if (!CBS_mem_equal(&host_name, |
| 270 | (const uint8_t*)config->expected_server_name.data(), |
| 271 | config->expected_server_name.size())) { |
| 272 | fprintf(stderr, "Server name mismatch.\n"); |
| 273 | } |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 274 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 275 | |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 276 | if (config->fail_early_callback) { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 277 | return -1; |
| 278 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 279 | |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 280 | // Install the certificate in the early callback. |
| 281 | if (config->use_early_callback) { |
| 282 | if (config->async) { |
| 283 | // Install the certificate asynchronously. |
| 284 | return 0; |
| 285 | } |
| 286 | if (!InstallCertificate(ctx->ssl)) { |
| 287 | return -1; |
| 288 | } |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 289 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 290 | return 1; |
| 291 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 292 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 293 | static int SkipVerify(int preverify_ok, X509_STORE_CTX *store_ctx) { |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 294 | return 1; |
| 295 | } |
| 296 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 297 | static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out, |
| 298 | unsigned int *out_len, void *arg) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 299 | const TestConfig *config = GetConfigPtr(ssl); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 300 | if (config->advertise_npn.empty()) { |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 301 | return SSL_TLSEXT_ERR_NOACK; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 302 | } |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 303 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 304 | *out = (const uint8_t*)config->advertise_npn.data(); |
| 305 | *out_len = config->advertise_npn.size(); |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 306 | return SSL_TLSEXT_ERR_OK; |
| 307 | } |
| 308 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 309 | static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen, |
David Benjamin | 8b36841 | 2015-03-14 01:54:17 -0400 | [diff] [blame] | 310 | const uint8_t* in, unsigned inlen, void* arg) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 311 | const TestConfig *config = GetConfigPtr(ssl); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 312 | if (config->select_next_proto.empty()) { |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 313 | return SSL_TLSEXT_ERR_NOACK; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 314 | } |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 315 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 316 | *out = (uint8_t*)config->select_next_proto.data(); |
| 317 | *outlen = config->select_next_proto.size(); |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 318 | return SSL_TLSEXT_ERR_OK; |
| 319 | } |
| 320 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 321 | static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen, |
| 322 | const uint8_t* in, unsigned inlen, void* arg) { |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 323 | const TestConfig *config = GetConfigPtr(ssl); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 324 | if (config->select_alpn.empty()) { |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 325 | return SSL_TLSEXT_ERR_NOACK; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 326 | } |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 327 | |
| 328 | if (!config->expected_advertised_alpn.empty() && |
| 329 | (config->expected_advertised_alpn.size() != inlen || |
| 330 | memcmp(config->expected_advertised_alpn.data(), |
| 331 | in, inlen) != 0)) { |
| 332 | fprintf(stderr, "bad ALPN select callback inputs\n"); |
| 333 | exit(1); |
| 334 | } |
| 335 | |
| 336 | *out = (const uint8_t*)config->select_alpn.data(); |
| 337 | *outlen = config->select_alpn.size(); |
| 338 | return SSL_TLSEXT_ERR_OK; |
| 339 | } |
| 340 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 341 | static unsigned PskClientCallback(SSL *ssl, const char *hint, |
| 342 | char *out_identity, |
| 343 | unsigned max_identity_len, |
| 344 | uint8_t *out_psk, unsigned max_psk_len) { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 345 | const TestConfig *config = GetConfigPtr(ssl); |
| 346 | |
| 347 | if (strcmp(hint ? hint : "", config->psk_identity.c_str()) != 0) { |
| 348 | fprintf(stderr, "Server PSK hint did not match.\n"); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | // Account for the trailing '\0' for the identity. |
| 353 | if (config->psk_identity.size() >= max_identity_len || |
| 354 | config->psk.size() > max_psk_len) { |
| 355 | fprintf(stderr, "PSK buffers too small\n"); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | BUF_strlcpy(out_identity, config->psk_identity.c_str(), |
| 360 | max_identity_len); |
| 361 | memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 362 | return config->psk.size(); |
| 363 | } |
| 364 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 365 | static unsigned PskServerCallback(SSL *ssl, const char *identity, |
| 366 | uint8_t *out_psk, unsigned max_psk_len) { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 367 | const TestConfig *config = GetConfigPtr(ssl); |
| 368 | |
| 369 | if (strcmp(identity, config->psk_identity.c_str()) != 0) { |
| 370 | fprintf(stderr, "Client PSK identity did not match.\n"); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | if (config->psk.size() > max_psk_len) { |
| 375 | fprintf(stderr, "PSK buffers too small\n"); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 380 | return config->psk.size(); |
| 381 | } |
| 382 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 383 | static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 384 | *out_clock = GetTestState(ssl)->clock; |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 385 | } |
| 386 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 387 | static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 388 | *out_pkey = GetTestState(ssl)->channel_id.release(); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 389 | } |
| 390 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 391 | static int CertCallback(SSL *ssl, void *arg) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 392 | if (!GetTestState(ssl)->cert_ready) { |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | if (!InstallCertificate(ssl)) { |
| 396 | return 0; |
| 397 | } |
| 398 | return 1; |
| 399 | } |
| 400 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 401 | static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len, |
| 402 | int *copy) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 403 | TestState *async_state = GetTestState(ssl); |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 404 | if (async_state->session) { |
| 405 | *copy = 0; |
| 406 | return async_state->session.release(); |
| 407 | } else if (async_state->pending_session) { |
| 408 | return SSL_magic_pending_session_ptr(); |
| 409 | } else { |
| 410 | return NULL; |
| 411 | } |
| 412 | } |
| 413 | |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 414 | static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) { |
| 415 | const TestConfig *config = GetConfigPtr(early_context->ssl); |
| 416 | static int callback_num = 0; |
| 417 | |
| 418 | callback_num++; |
| 419 | if (config->fail_ddos_callback || |
| 420 | (config->fail_second_ddos_callback && callback_num == 2)) { |
| 421 | return 0; |
| 422 | } |
| 423 | return 1; |
| 424 | } |
| 425 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 426 | static void InfoCallback(const SSL *ssl, int type, int val) { |
| 427 | if (type == SSL_CB_HANDSHAKE_DONE) { |
| 428 | if (GetConfigPtr(ssl)->handshake_never_done) { |
| 429 | fprintf(stderr, "handshake completed\n"); |
| 430 | // Abort before any expected error code is printed, to ensure the overall |
| 431 | // test fails. |
| 432 | abort(); |
| 433 | } |
| 434 | GetTestState(ssl)->handshake_done = true; |
| 435 | } |
| 436 | } |
| 437 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 438 | // Connect returns a new socket connected to localhost on |port| or -1 on |
| 439 | // error. |
| 440 | static int Connect(uint16_t port) { |
| 441 | int sock = socket(AF_INET, SOCK_STREAM, 0); |
| 442 | if (sock == -1) { |
| 443 | PrintSocketError("socket"); |
| 444 | return -1; |
| 445 | } |
| 446 | int nodelay = 1; |
| 447 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 448 | reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) { |
| 449 | PrintSocketError("setsockopt"); |
| 450 | closesocket(sock); |
| 451 | return -1; |
| 452 | } |
| 453 | sockaddr_in sin; |
| 454 | memset(&sin, 0, sizeof(sin)); |
| 455 | sin.sin_family = AF_INET; |
| 456 | sin.sin_port = htons(port); |
| 457 | if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) { |
| 458 | PrintSocketError("inet_pton"); |
| 459 | closesocket(sock); |
| 460 | return -1; |
| 461 | } |
| 462 | if (connect(sock, reinterpret_cast<const sockaddr*>(&sin), |
| 463 | sizeof(sin)) != 0) { |
| 464 | PrintSocketError("connect"); |
| 465 | closesocket(sock); |
| 466 | return -1; |
| 467 | } |
| 468 | return sock; |
| 469 | } |
| 470 | |
| 471 | class SocketCloser { |
| 472 | public: |
| 473 | explicit SocketCloser(int sock) : sock_(sock) {} |
| 474 | ~SocketCloser() { |
| 475 | // Half-close and drain the socket before releasing it. This seems to be |
| 476 | // necessary for graceful shutdown on Windows. It will also avoid write |
| 477 | // failures in the test runner. |
| 478 | #if defined(OPENSSL_WINDOWS) |
| 479 | shutdown(sock_, SD_SEND); |
| 480 | #else |
| 481 | shutdown(sock_, SHUT_WR); |
| 482 | #endif |
| 483 | while (true) { |
| 484 | char buf[1024]; |
| 485 | if (recv(sock_, buf, sizeof(buf), 0) <= 0) { |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | closesocket(sock_); |
| 490 | } |
| 491 | |
| 492 | private: |
| 493 | const int sock_; |
| 494 | }; |
| 495 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 496 | static ScopedSSL_CTX SetupCtx(const TestConfig *config) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 497 | ScopedSSL_CTX ssl_ctx(SSL_CTX_new( |
| 498 | config->is_dtls ? DTLS_method() : TLS_method())); |
| 499 | if (!ssl_ctx) { |
| 500 | return nullptr; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 501 | } |
| 502 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 503 | if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), "ALL")) { |
| 504 | return nullptr; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 505 | } |
| 506 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 507 | ScopedDH dh(DH_get_2048_256(NULL)); |
| 508 | if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) { |
| 509 | return nullptr; |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 510 | } |
| 511 | |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 512 | if (config->async && config->is_server) { |
| 513 | // Disable the internal session cache. To test asynchronous session lookup, |
| 514 | // we use an external session cache. |
| 515 | SSL_CTX_set_session_cache_mode( |
| 516 | ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 517 | SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback); |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 518 | } else { |
| 519 | SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 520 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 521 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 522 | ssl_ctx->select_certificate_cb = SelectCertificateCallback; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 523 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 524 | SSL_CTX_set_next_protos_advertised_cb( |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 525 | ssl_ctx.get(), NextProtosAdvertisedCallback, NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 526 | if (!config->select_next_proto.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 527 | SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback, |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 528 | NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (!config->select_alpn.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 532 | SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 533 | } |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 534 | |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 535 | ssl_ctx->tlsext_channel_id_enabled_new = 1; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 536 | SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 537 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 538 | ssl_ctx->current_time_cb = CurrentTimeCallback; |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 539 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 540 | SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback); |
| 541 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 542 | return ssl_ctx; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 543 | } |
| 544 | |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 545 | // RetryAsync is called after a failed operation on |ssl| with return code |
| 546 | // |ret|. If the operation should be retried, it simulates one asynchronous |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 547 | // event and returns true. Otherwise it returns false. |
| 548 | static bool RetryAsync(SSL *ssl, int ret) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 549 | // No error; don't retry. |
| 550 | if (ret >= 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 551 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 552 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 553 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 554 | TestState *test_state = GetTestState(ssl); |
| 555 | if (test_state->clock_delta.tv_usec != 0 || |
| 556 | test_state->clock_delta.tv_sec != 0) { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 557 | // Process the timeout and retry. |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 558 | test_state->clock.tv_usec += test_state->clock_delta.tv_usec; |
| 559 | test_state->clock.tv_sec += test_state->clock.tv_usec / 1000000; |
| 560 | test_state->clock.tv_usec %= 1000000; |
| 561 | test_state->clock.tv_sec += test_state->clock_delta.tv_sec; |
| 562 | memset(&test_state->clock_delta, 0, sizeof(test_state->clock_delta)); |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 563 | |
| 564 | if (DTLSv1_handle_timeout(ssl) < 0) { |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 565 | fprintf(stderr, "Error retransmitting.\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 566 | return false; |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 567 | } |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 568 | return true; |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 569 | } |
| 570 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 571 | // See if we needed to read or write more. If so, allow one byte through on |
| 572 | // the appropriate end to maximally stress the state machine. |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 573 | switch (SSL_get_error(ssl, ret)) { |
| 574 | case SSL_ERROR_WANT_READ: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 575 | AsyncBioAllowRead(test_state->async_bio, 1); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 576 | return true; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 577 | case SSL_ERROR_WANT_WRITE: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 578 | AsyncBioAllowWrite(test_state->async_bio, 1); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 579 | return true; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 580 | case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: { |
| 581 | ScopedEVP_PKEY pkey = LoadPrivateKey(GetConfigPtr(ssl)->send_channel_id); |
| 582 | if (!pkey) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 583 | return false; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 584 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 585 | test_state->channel_id = std::move(pkey); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 586 | return true; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 587 | } |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 588 | case SSL_ERROR_WANT_X509_LOOKUP: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 589 | test_state->cert_ready = true; |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 590 | return true; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 591 | case SSL_ERROR_PENDING_SESSION: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 592 | test_state->session = std::move(test_state->pending_session); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 593 | return true; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 594 | case SSL_ERROR_PENDING_CERTIFICATE: |
| 595 | // The handshake will resume without a second call to the early callback. |
| 596 | return InstallCertificate(ssl); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 597 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
| 598 | test_state->signature_retries++; |
| 599 | return true; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 600 | default: |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 601 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 602 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 603 | } |
| 604 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 605 | // DoRead reads from |ssl|, resolving any asynchronous operations. It returns |
| 606 | // the result value of the final |SSL_read| call. |
| 607 | static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) { |
| 608 | const TestConfig *config = GetConfigPtr(ssl); |
| 609 | int ret; |
| 610 | do { |
| 611 | ret = SSL_read(ssl, out, max_out); |
| 612 | } while (config->async && RetryAsync(ssl, ret)); |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | // WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous |
| 617 | // operations. It returns the result of the final |SSL_write| call. |
| 618 | static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) { |
| 619 | const TestConfig *config = GetConfigPtr(ssl); |
| 620 | int ret; |
| 621 | do { |
| 622 | ret = SSL_write(ssl, in, in_len); |
| 623 | if (ret > 0) { |
| 624 | in += ret; |
| 625 | in_len -= ret; |
| 626 | } |
| 627 | } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0)); |
| 628 | return ret; |
| 629 | } |
| 630 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame^] | 631 | // CheckHandshakeProperties checks, immediately after |ssl| completes its |
| 632 | // initial handshake (or False Starts), whether all the properties are |
| 633 | // consistent with the test configuration and invariants. |
| 634 | static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) { |
| 635 | const TestConfig *config = GetConfigPtr(ssl); |
| 636 | |
| 637 | if (SSL_get_current_cipher(ssl) == nullptr) { |
| 638 | fprintf(stderr, "null cipher after handshake\n"); |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | if (is_resume && |
| 643 | (!!SSL_session_reused(ssl) == config->expect_session_miss)) { |
| 644 | fprintf(stderr, "session was%s reused\n", |
| 645 | SSL_session_reused(ssl) ? "" : " not"); |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | bool expect_handshake_done = is_resume || !config->false_start; |
| 650 | if (expect_handshake_done != GetTestState(ssl)->handshake_done) { |
| 651 | fprintf(stderr, "handshake was%s completed\n", |
| 652 | GetTestState(ssl)->handshake_done ? "" : " not"); |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | if (config->is_server && !GetTestState(ssl)->early_callback_called) { |
| 657 | fprintf(stderr, "early callback not called\n"); |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | if (!config->expected_server_name.empty()) { |
| 662 | const char *server_name = |
| 663 | SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 664 | if (server_name != config->expected_server_name) { |
| 665 | fprintf(stderr, "servername mismatch (got %s; want %s)\n", |
| 666 | server_name, config->expected_server_name.c_str()); |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (!config->expected_certificate_types.empty()) { |
| 672 | uint8_t *certificate_types; |
| 673 | int num_certificate_types = |
| 674 | SSL_get0_certificate_types(ssl, &certificate_types); |
| 675 | if (num_certificate_types != |
| 676 | (int)config->expected_certificate_types.size() || |
| 677 | memcmp(certificate_types, |
| 678 | config->expected_certificate_types.data(), |
| 679 | num_certificate_types) != 0) { |
| 680 | fprintf(stderr, "certificate types mismatch\n"); |
| 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (!config->expected_next_proto.empty()) { |
| 686 | const uint8_t *next_proto; |
| 687 | unsigned next_proto_len; |
| 688 | SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
| 689 | if (next_proto_len != config->expected_next_proto.size() || |
| 690 | memcmp(next_proto, config->expected_next_proto.data(), |
| 691 | next_proto_len) != 0) { |
| 692 | fprintf(stderr, "negotiated next proto mismatch\n"); |
| 693 | return false; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (!config->expected_alpn.empty()) { |
| 698 | const uint8_t *alpn_proto; |
| 699 | unsigned alpn_proto_len; |
| 700 | SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len); |
| 701 | if (alpn_proto_len != config->expected_alpn.size() || |
| 702 | memcmp(alpn_proto, config->expected_alpn.data(), |
| 703 | alpn_proto_len) != 0) { |
| 704 | fprintf(stderr, "negotiated alpn proto mismatch\n"); |
| 705 | return false; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (!config->expected_channel_id.empty()) { |
| 710 | uint8_t channel_id[64]; |
| 711 | if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) { |
| 712 | fprintf(stderr, "no channel id negotiated\n"); |
| 713 | return false; |
| 714 | } |
| 715 | if (config->expected_channel_id.size() != 64 || |
| 716 | memcmp(config->expected_channel_id.data(), |
| 717 | channel_id, 64) != 0) { |
| 718 | fprintf(stderr, "channel id mismatch\n"); |
| 719 | return false; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | if (config->expect_extended_master_secret) { |
| 724 | if (!ssl->session->extended_master_secret) { |
| 725 | fprintf(stderr, "No EMS for session when expected"); |
| 726 | return false; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | if (!config->expected_ocsp_response.empty()) { |
| 731 | const uint8_t *data; |
| 732 | size_t len; |
| 733 | SSL_get0_ocsp_response(ssl, &data, &len); |
| 734 | if (config->expected_ocsp_response.size() != len || |
| 735 | memcmp(config->expected_ocsp_response.data(), data, len) != 0) { |
| 736 | fprintf(stderr, "OCSP response mismatch\n"); |
| 737 | return false; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | if (!config->expected_signed_cert_timestamps.empty()) { |
| 742 | const uint8_t *data; |
| 743 | size_t len; |
| 744 | SSL_get0_signed_cert_timestamp_list(ssl, &data, &len); |
| 745 | if (config->expected_signed_cert_timestamps.size() != len || |
| 746 | memcmp(config->expected_signed_cert_timestamps.data(), |
| 747 | data, len) != 0) { |
| 748 | fprintf(stderr, "SCT list mismatch\n"); |
| 749 | return false; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (!config->is_server) { |
| 754 | /* Clients should expect a peer certificate chain iff this was not a PSK |
| 755 | * cipher suite. */ |
| 756 | if (config->psk.empty()) { |
| 757 | if (SSL_get_peer_cert_chain(ssl) == nullptr) { |
| 758 | fprintf(stderr, "Missing peer certificate chain!\n"); |
| 759 | return false; |
| 760 | } |
| 761 | } else if (SSL_get_peer_cert_chain(ssl) != nullptr) { |
| 762 | fprintf(stderr, "Unexpected peer certificate chain!\n"); |
| 763 | return false; |
| 764 | } |
| 765 | } |
| 766 | return true; |
| 767 | } |
| 768 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 769 | // DoExchange runs a test SSL exchange against the peer. On success, it returns |
| 770 | // true and sets |*out_session| to the negotiated SSL session. If the test is a |
| 771 | // resumption attempt, |is_resume| is true and |session| is the session from the |
| 772 | // previous exchange. |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 773 | static bool DoExchange(ScopedSSL_SESSION *out_session, SSL_CTX *ssl_ctx, |
| 774 | const TestConfig *config, bool is_resume, |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 775 | SSL_SESSION *session) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 776 | ScopedSSL ssl(SSL_new(ssl_ctx)); |
| 777 | if (!ssl) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 778 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 779 | } |
| 780 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 781 | if (!SetConfigPtr(ssl.get(), config) || |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 782 | !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 783 | return false; |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 784 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 785 | |
Adam Langley | 5f0efe0 | 2015-02-20 13:03:16 -0800 | [diff] [blame] | 786 | if (config->fallback_scsv && |
| 787 | !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) { |
| 788 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 789 | } |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 790 | if (!config->use_early_callback) { |
| 791 | if (config->async) { |
| 792 | // TODO(davidben): Also test |s->ctx->client_cert_cb| on the client. |
| 793 | SSL_set_cert_cb(ssl.get(), CertCallback, NULL); |
| 794 | } else if (!InstallCertificate(ssl.get())) { |
| 795 | return false; |
| 796 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 797 | } |
| 798 | if (config->require_any_client_certificate) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 799 | SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 800 | SkipVerify); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 801 | } |
| 802 | if (config->false_start) { |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 803 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 804 | } |
| 805 | if (config->cbc_record_splitting) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 806 | SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 807 | } |
| 808 | if (config->partial_write) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 809 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 810 | } |
| 811 | if (config->no_tls12) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 812 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 813 | } |
| 814 | if (config->no_tls11) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 815 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 816 | } |
| 817 | if (config->no_tls1) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 818 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 819 | } |
| 820 | if (config->no_ssl3) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 821 | SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 822 | } |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 823 | if (config->tls_d5_bug) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 824 | SSL_set_options(ssl.get(), SSL_OP_TLS_D5_BUG); |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 825 | } |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 826 | if (config->allow_unsafe_legacy_renegotiation) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 827 | SSL_set_options(ssl.get(), SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION); |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 828 | } |
David Benjamin | cff0b90 | 2015-05-15 23:09:47 -0400 | [diff] [blame] | 829 | if (config->no_legacy_server_connect) { |
| 830 | SSL_clear_options(ssl.get(), SSL_OP_LEGACY_SERVER_CONNECT); |
| 831 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 832 | if (!config->expected_channel_id.empty()) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 833 | SSL_enable_tls_channel_id(ssl.get()); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 834 | } |
| 835 | if (!config->send_channel_id.empty()) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 836 | SSL_enable_tls_channel_id(ssl.get()); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 837 | if (!config->async) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 838 | // The async case will be supplied by |ChannelIdCallback|. |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 839 | ScopedEVP_PKEY pkey = LoadPrivateKey(config->send_channel_id); |
| 840 | if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 841 | return false; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 842 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 843 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 844 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 845 | if (!config->host_name.empty() && |
| 846 | !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 847 | return false; |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 848 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 849 | if (!config->advertise_alpn.empty() && |
| 850 | SSL_set_alpn_protos(ssl.get(), |
| 851 | (const uint8_t *)config->advertise_alpn.data(), |
| 852 | config->advertise_alpn.size()) != 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 853 | return false; |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 854 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 855 | if (!config->psk.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 856 | SSL_set_psk_client_callback(ssl.get(), PskClientCallback); |
| 857 | SSL_set_psk_server_callback(ssl.get(), PskServerCallback); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 858 | } |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 859 | if (!config->psk_identity.empty() && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 860 | !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 861 | return false; |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 862 | } |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 863 | if (!config->srtp_profiles.empty() && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 864 | !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 865 | return false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 866 | } |
| 867 | if (config->enable_ocsp_stapling && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 868 | !SSL_enable_ocsp_stapling(ssl.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 869 | return false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 870 | } |
| 871 | if (config->enable_signed_cert_timestamps && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 872 | !SSL_enable_signed_cert_timestamps(ssl.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 873 | return false; |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 874 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 875 | SSL_enable_fastradio_padding(ssl.get(), config->fastradio_padding); |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 876 | if (config->min_version != 0) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 877 | SSL_set_min_version(ssl.get(), (uint16_t)config->min_version); |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 878 | } |
| 879 | if (config->max_version != 0) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 880 | SSL_set_max_version(ssl.get(), (uint16_t)config->max_version); |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 881 | } |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 882 | if (config->mtu != 0) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 883 | SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU); |
| 884 | SSL_set_mtu(ssl.get(), config->mtu); |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 885 | } |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 886 | if (config->install_ddos_callback) { |
| 887 | SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback); |
| 888 | } |
David Benjamin | 67d1fb5 | 2015-03-16 15:16:23 -0400 | [diff] [blame] | 889 | if (!config->cipher.empty() && |
| 890 | !SSL_set_cipher_list(ssl.get(), config->cipher.c_str())) { |
| 891 | return false; |
| 892 | } |
David Benjamin | 897e5e0 | 2015-05-12 17:03:54 -0400 | [diff] [blame] | 893 | if (!config->reject_peer_renegotiations) { |
| 894 | /* Renegotiations are disabled by default. */ |
| 895 | SSL_set_reject_peer_renegotiations(ssl.get(), 0); |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 896 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 897 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 898 | int sock = Connect(config->port); |
| 899 | if (sock == -1) { |
| 900 | return false; |
| 901 | } |
| 902 | SocketCloser closer(sock); |
| 903 | |
| 904 | ScopedBIO bio(BIO_new_socket(sock, BIO_NOCLOSE)); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 905 | if (!bio) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 906 | return false; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 907 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 908 | if (config->is_dtls) { |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 909 | ScopedBIO packeted = |
| 910 | PacketedBioCreate(&GetTestState(ssl.get())->clock_delta); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 911 | BIO_push(packeted.get(), bio.release()); |
| 912 | bio = std::move(packeted); |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 913 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 914 | if (config->async) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 915 | ScopedBIO async_scoped = |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 916 | config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate(); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 917 | BIO_push(async_scoped.get(), bio.release()); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 918 | GetTestState(ssl.get())->async_bio = async_scoped.get(); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 919 | bio = std::move(async_scoped); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 920 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 921 | SSL_set_bio(ssl.get(), bio.get(), bio.get()); |
| 922 | bio.release(); // SSL_set_bio takes ownership. |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 923 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 924 | if (session != NULL) { |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 925 | if (!config->is_server) { |
| 926 | if (SSL_set_session(ssl.get(), session) != 1) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 927 | return false; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 928 | } |
| 929 | } else if (config->async) { |
| 930 | // The internal session cache is disabled, so install the session |
| 931 | // manually. |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 932 | GetTestState(ssl.get())->pending_session.reset( |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 933 | SSL_SESSION_up_ref(session)); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
David Benjamin | a07c0fc | 2015-05-13 13:19:42 -0400 | [diff] [blame] | 937 | if (SSL_get_current_cipher(ssl.get()) != nullptr) { |
| 938 | fprintf(stderr, "non-null cipher before handshake\n"); |
| 939 | return false; |
| 940 | } |
| 941 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 942 | int ret; |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 943 | if (config->implicit_handshake) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 944 | if (config->is_server) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 945 | SSL_set_accept_state(ssl.get()); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 946 | } else { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 947 | SSL_set_connect_state(ssl.get()); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 948 | } |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 949 | } else { |
| 950 | do { |
| 951 | if (config->is_server) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 952 | ret = SSL_accept(ssl.get()); |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 953 | } else { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 954 | ret = SSL_connect(ssl.get()); |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 955 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 956 | } while (config->async && RetryAsync(ssl.get(), ret)); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame^] | 957 | if (ret != 1 || |
| 958 | !CheckHandshakeProperties(ssl.get(), is_resume)) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 959 | return false; |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 960 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 961 | |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 962 | } |
| 963 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 964 | if (config->export_keying_material > 0) { |
| 965 | std::vector<uint8_t> result( |
| 966 | static_cast<size_t>(config->export_keying_material)); |
| 967 | if (!SSL_export_keying_material( |
| 968 | ssl.get(), result.data(), result.size(), |
| 969 | config->export_label.data(), config->export_label.size(), |
| 970 | reinterpret_cast<const uint8_t*>(config->export_context.data()), |
| 971 | config->export_context.size(), config->use_export_context)) { |
| 972 | fprintf(stderr, "failed to export keying material\n"); |
| 973 | return false; |
| 974 | } |
| 975 | if (WriteAll(ssl.get(), result.data(), result.size()) < 0) { |
| 976 | return false; |
| 977 | } |
| 978 | } |
| 979 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 980 | if (config->tls_unique) { |
| 981 | uint8_t tls_unique[16]; |
| 982 | size_t tls_unique_len; |
| 983 | if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len, |
| 984 | sizeof(tls_unique))) { |
| 985 | fprintf(stderr, "failed to get tls-unique\n"); |
| 986 | return false; |
| 987 | } |
| 988 | |
| 989 | if (tls_unique_len != 12) { |
| 990 | fprintf(stderr, "expected 12 bytes of tls-unique but got %u", |
| 991 | static_cast<unsigned>(tls_unique_len)); |
| 992 | return false; |
| 993 | } |
| 994 | |
| 995 | if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) { |
| 996 | return false; |
| 997 | } |
| 998 | } |
| 999 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1000 | if (config->write_different_record_sizes) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1001 | if (config->is_dtls) { |
| 1002 | fprintf(stderr, "write_different_record_sizes not supported for DTLS\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1003 | return false; |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1004 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1005 | // This mode writes a number of different record sizes in an attempt to |
| 1006 | // trip up the CBC record splitting code. |
| 1007 | uint8_t buf[32769]; |
| 1008 | memset(buf, 0x42, sizeof(buf)); |
| 1009 | static const size_t kRecordSizes[] = { |
| 1010 | 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769}; |
| 1011 | for (size_t i = 0; i < sizeof(kRecordSizes) / sizeof(kRecordSizes[0]); |
| 1012 | i++) { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1013 | const size_t len = kRecordSizes[i]; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1014 | if (len > sizeof(buf)) { |
| 1015 | fprintf(stderr, "Bad kRecordSizes value.\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1016 | return false; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1017 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1018 | if (WriteAll(ssl.get(), buf, len) < 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1019 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1020 | } |
| 1021 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1022 | } else { |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 1023 | if (config->shim_writes_first) { |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1024 | if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"), |
| 1025 | 5) < 0) { |
| 1026 | return false; |
| 1027 | } |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 1028 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1029 | for (;;) { |
| 1030 | uint8_t buf[512]; |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1031 | int n = DoRead(ssl.get(), buf, sizeof(buf)); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1032 | int err = SSL_get_error(ssl.get(), n); |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1033 | if (err == SSL_ERROR_ZERO_RETURN || |
| 1034 | (n == 0 && err == SSL_ERROR_SYSCALL)) { |
| 1035 | if (n != 0) { |
| 1036 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1037 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1038 | } |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1039 | // Accept shutdowns with or without close_notify. |
| 1040 | // TODO(davidben): Write tests which distinguish these two cases. |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1041 | break; |
| 1042 | } else if (err != SSL_ERROR_NONE) { |
| 1043 | if (n > 0) { |
| 1044 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1045 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1046 | } |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1047 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1048 | } |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1049 | // Successfully read data. |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1050 | if (n <= 0) { |
| 1051 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1052 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1053 | } |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 1054 | |
| 1055 | // After a successful read, with or without False Start, the handshake |
| 1056 | // must be complete. |
| 1057 | if (!GetTestState(ssl.get())->handshake_done) { |
| 1058 | fprintf(stderr, "handshake was not completed after SSL_read\n"); |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1062 | for (int i = 0; i < n; i++) { |
| 1063 | buf[i] ^= 0xff; |
| 1064 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1065 | if (WriteAll(ssl.get(), buf, n) < 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1066 | return false; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1071 | if (out_session) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1072 | out_session->reset(SSL_get1_session(ssl.get())); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1075 | SSL_shutdown(ssl.get()); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1076 | return true; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1077 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1078 | |
| 1079 | int main(int argc, char **argv) { |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1080 | #if defined(OPENSSL_WINDOWS) |
| 1081 | /* Initialize Winsock. */ |
| 1082 | WORD wsa_version = MAKEWORD(2, 2); |
| 1083 | WSADATA wsa_data; |
| 1084 | int wsa_err = WSAStartup(wsa_version, &wsa_data); |
| 1085 | if (wsa_err != 0) { |
| 1086 | fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); |
| 1087 | return 1; |
| 1088 | } |
| 1089 | if (wsa_data.wVersion != wsa_version) { |
| 1090 | fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); |
| 1091 | return 1; |
| 1092 | } |
| 1093 | #else |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1094 | signal(SIGPIPE, SIG_IGN); |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 1095 | #endif |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1096 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1097 | if (!SSL_library_init()) { |
| 1098 | return 1; |
| 1099 | } |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1100 | g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 1101 | g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1102 | if (g_config_index < 0 || g_state_index < 0) { |
David Benjamin | ae3e487 | 2014-11-18 21:52:26 -0500 | [diff] [blame] | 1103 | return 1; |
| 1104 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1105 | |
| 1106 | TestConfig config; |
| 1107 | if (!ParseConfig(argc - 1, argv + 1, &config)) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1108 | return Usage(argv[0]); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1109 | } |
| 1110 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1111 | ScopedSSL_CTX ssl_ctx = SetupCtx(&config); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1112 | if (!ssl_ctx) { |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 1113 | ERR_print_errors_fp(stderr); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1114 | return 1; |
| 1115 | } |
| 1116 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1117 | ScopedSSL_SESSION session; |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1118 | if (!DoExchange(&session, ssl_ctx.get(), &config, false /* is_resume */, |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1119 | NULL /* session */)) { |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 1120 | ERR_print_errors_fp(stderr); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1121 | return 1; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1122 | } |
| 1123 | |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1124 | if (config.resume && |
| 1125 | !DoExchange(NULL, ssl_ctx.get(), &config, true /* is_resume */, |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1126 | session.get())) { |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 1127 | ERR_print_errors_fp(stderr); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1128 | return 1; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1131 | return 0; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1132 | } |