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