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