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