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