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