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