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