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