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