David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
David Benjamin | 4cc36ad | 2015-12-19 14:23:26 -0500 | [diff] [blame] | 15 | #if !defined(__STDC_FORMAT_MACROS) |
| 16 | #define __STDC_FORMAT_MACROS |
| 17 | #endif |
| 18 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 19 | #include <openssl/base.h> |
| 20 | |
| 21 | #if !defined(OPENSSL_WINDOWS) |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 22 | #include <arpa/inet.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 23 | #include <netinet/in.h> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 24 | #include <netinet/tcp.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 25 | #include <signal.h> |
| 26 | #include <sys/socket.h> |
David Benjamin | 0abd6f2 | 2015-12-04 21:49:53 -0500 | [diff] [blame] | 27 | #include <sys/time.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 28 | #include <unistd.h> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 29 | #else |
| 30 | #include <io.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 31 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 32 | #include <winsock2.h> |
| 33 | #include <ws2tcpip.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 34 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 35 | |
| 36 | #pragma comment(lib, "Ws2_32.lib") |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 39 | #include <assert.h> |
David Benjamin | 4cc36ad | 2015-12-19 14:23:26 -0500 | [diff] [blame] | 40 | #include <inttypes.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 41 | #include <string.h> |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 42 | |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 43 | #include <openssl/bio.h> |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 44 | #include <openssl/buf.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 45 | #include <openssl/bytestring.h> |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 46 | #include <openssl/cipher.h> |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 47 | #include <openssl/crypto.h> |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 48 | #include <openssl/dh.h> |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 49 | #include <openssl/digest.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 | |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 555 | if (config->psk_identity.empty()) { |
| 556 | if (hint != nullptr) { |
| 557 | fprintf(stderr, "Server PSK hint was non-null.\n"); |
| 558 | return 0; |
| 559 | } |
| 560 | } else if (hint == nullptr || |
| 561 | strcmp(hint, config->psk_identity.c_str()) != 0) { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 562 | fprintf(stderr, "Server PSK hint did not match.\n"); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | // Account for the trailing '\0' for the identity. |
| 567 | if (config->psk_identity.size() >= max_identity_len || |
| 568 | config->psk.size() > max_psk_len) { |
| 569 | fprintf(stderr, "PSK buffers too small\n"); |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | BUF_strlcpy(out_identity, config->psk_identity.c_str(), |
| 574 | max_identity_len); |
| 575 | memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 576 | return config->psk.size(); |
| 577 | } |
| 578 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 579 | static unsigned PskServerCallback(SSL *ssl, const char *identity, |
| 580 | uint8_t *out_psk, unsigned max_psk_len) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 581 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 582 | |
| 583 | if (strcmp(identity, config->psk_identity.c_str()) != 0) { |
| 584 | fprintf(stderr, "Client PSK identity did not match.\n"); |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | if (config->psk.size() > max_psk_len) { |
| 589 | fprintf(stderr, "PSK buffers too small\n"); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 594 | return config->psk.size(); |
| 595 | } |
| 596 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 597 | static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 598 | *out_clock = PacketedBioGetClock(GetTestState(ssl)->packeted_bio); |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 599 | } |
| 600 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 601 | static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 602 | *out_pkey = GetTestState(ssl)->channel_id.release(); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 603 | } |
| 604 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 605 | static int CertCallback(SSL *ssl, void *arg) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame^] | 606 | const TestConfig *config = GetTestConfig(ssl); |
| 607 | |
| 608 | // Check the CertificateRequest metadata is as expected. |
| 609 | // |
| 610 | // TODO(davidben): Test |SSL_get_client_CA_list|. |
| 611 | if (!SSL_is_server(ssl) && |
| 612 | !config->expected_certificate_types.empty()) { |
| 613 | const uint8_t *certificate_types; |
| 614 | size_t certificate_types_len = |
| 615 | SSL_get0_certificate_types(ssl, &certificate_types); |
| 616 | if (certificate_types_len != config->expected_certificate_types.size() || |
| 617 | memcmp(certificate_types, |
| 618 | config->expected_certificate_types.data(), |
| 619 | certificate_types_len) != 0) { |
| 620 | fprintf(stderr, "certificate types mismatch\n"); |
| 621 | return 0; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | // The certificate will be installed via other means. |
| 626 | if (!config->async || config->use_early_callback || |
| 627 | config->use_old_client_cert_callback) { |
| 628 | return 1; |
| 629 | } |
| 630 | |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 631 | if (!GetTestState(ssl)->cert_ready) { |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 632 | return -1; |
| 633 | } |
| 634 | if (!InstallCertificate(ssl)) { |
| 635 | return 0; |
| 636 | } |
| 637 | return 1; |
| 638 | } |
| 639 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 640 | static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len, |
| 641 | int *copy) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 642 | TestState *async_state = GetTestState(ssl); |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 643 | if (async_state->session) { |
| 644 | *copy = 0; |
| 645 | return async_state->session.release(); |
| 646 | } else if (async_state->pending_session) { |
| 647 | return SSL_magic_pending_session_ptr(); |
| 648 | } else { |
| 649 | return NULL; |
| 650 | } |
| 651 | } |
| 652 | |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 653 | static int DDoSCallback(const struct ssl_early_callback_ctx *early_context) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 654 | const TestConfig *config = GetTestConfig(early_context->ssl); |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 655 | static int callback_num = 0; |
| 656 | |
| 657 | callback_num++; |
| 658 | if (config->fail_ddos_callback || |
| 659 | (config->fail_second_ddos_callback && callback_num == 2)) { |
| 660 | return 0; |
| 661 | } |
| 662 | return 1; |
| 663 | } |
| 664 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 665 | static void InfoCallback(const SSL *ssl, int type, int val) { |
| 666 | if (type == SSL_CB_HANDSHAKE_DONE) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 667 | if (GetTestConfig(ssl)->handshake_never_done) { |
David Benjamin | 7a4aaa4 | 2016-09-20 17:58:14 -0400 | [diff] [blame] | 668 | fprintf(stderr, "Handshake unexpectedly completed.\n"); |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 669 | // Abort before any expected error code is printed, to ensure the overall |
| 670 | // test fails. |
| 671 | abort(); |
| 672 | } |
| 673 | GetTestState(ssl)->handshake_done = true; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 674 | |
| 675 | // Callbacks may be called again on a new handshake. |
| 676 | GetTestState(ssl)->ticket_decrypt_done = false; |
| 677 | GetTestState(ssl)->alpn_select_done = false; |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 681 | static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) { |
| 682 | GetTestState(ssl)->got_new_session = true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 683 | GetTestState(ssl)->new_session.reset(session); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 684 | return 1; |
| 685 | } |
| 686 | |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 687 | static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 688 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 689 | int encrypt) { |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 690 | if (!encrypt) { |
| 691 | if (GetTestState(ssl)->ticket_decrypt_done) { |
| 692 | fprintf(stderr, "TicketKeyCallback called after completion.\n"); |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | GetTestState(ssl)->ticket_decrypt_done = true; |
| 697 | } |
| 698 | |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 699 | // This is just test code, so use the all-zeros key. |
| 700 | static const uint8_t kZeros[16] = {0}; |
| 701 | |
| 702 | if (encrypt) { |
| 703 | memcpy(key_name, kZeros, sizeof(kZeros)); |
| 704 | RAND_bytes(iv, 16); |
| 705 | } else if (memcmp(key_name, kZeros, 16) != 0) { |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) || |
| 710 | !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) { |
| 711 | return -1; |
| 712 | } |
| 713 | |
| 714 | if (!encrypt) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 715 | return GetTestConfig(ssl)->renew_ticket ? 2 : 1; |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 716 | } |
| 717 | return 1; |
| 718 | } |
| 719 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 720 | // kCustomExtensionValue is the extension value that the custom extension |
| 721 | // callbacks will add. |
Adam Langley | c5b23a1 | 2015-07-30 18:19:26 -0700 | [diff] [blame] | 722 | static const uint16_t kCustomExtensionValue = 1234; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 723 | static void *const kCustomExtensionAddArg = |
| 724 | reinterpret_cast<void *>(kCustomExtensionValue); |
| 725 | static void *const kCustomExtensionParseArg = |
| 726 | reinterpret_cast<void *>(kCustomExtensionValue + 1); |
| 727 | static const char kCustomExtensionContents[] = "custom extension"; |
| 728 | |
| 729 | static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value, |
| 730 | const uint8_t **out, size_t *out_len, |
| 731 | int *out_alert_value, void *add_arg) { |
| 732 | if (extension_value != kCustomExtensionValue || |
| 733 | add_arg != kCustomExtensionAddArg) { |
| 734 | abort(); |
| 735 | } |
| 736 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 737 | if (GetTestConfig(ssl)->custom_extension_skip) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 738 | return 0; |
| 739 | } |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 740 | if (GetTestConfig(ssl)->custom_extension_fail_add) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents); |
| 745 | *out_len = sizeof(kCustomExtensionContents) - 1; |
| 746 | |
| 747 | return 1; |
| 748 | } |
| 749 | |
| 750 | static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value, |
| 751 | const uint8_t *out, void *add_arg) { |
| 752 | if (extension_value != kCustomExtensionValue || |
| 753 | add_arg != kCustomExtensionAddArg || |
| 754 | out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) { |
| 755 | abort(); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value, |
| 760 | const uint8_t *contents, |
| 761 | size_t contents_len, |
| 762 | int *out_alert_value, void *parse_arg) { |
| 763 | if (extension_value != kCustomExtensionValue || |
| 764 | parse_arg != kCustomExtensionParseArg) { |
| 765 | abort(); |
| 766 | } |
| 767 | |
| 768 | if (contents_len != sizeof(kCustomExtensionContents) - 1 || |
| 769 | memcmp(contents, kCustomExtensionContents, contents_len) != 0) { |
| 770 | *out_alert_value = SSL_AD_DECODE_ERROR; |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | return 1; |
| 775 | } |
| 776 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 777 | // Connect returns a new socket connected to localhost on |port| or -1 on |
| 778 | // error. |
| 779 | static int Connect(uint16_t port) { |
| 780 | int sock = socket(AF_INET, SOCK_STREAM, 0); |
| 781 | if (sock == -1) { |
| 782 | PrintSocketError("socket"); |
| 783 | return -1; |
| 784 | } |
| 785 | int nodelay = 1; |
| 786 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 787 | reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) { |
| 788 | PrintSocketError("setsockopt"); |
| 789 | closesocket(sock); |
| 790 | return -1; |
| 791 | } |
| 792 | sockaddr_in sin; |
| 793 | memset(&sin, 0, sizeof(sin)); |
| 794 | sin.sin_family = AF_INET; |
| 795 | sin.sin_port = htons(port); |
| 796 | if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) { |
| 797 | PrintSocketError("inet_pton"); |
| 798 | closesocket(sock); |
| 799 | return -1; |
| 800 | } |
| 801 | if (connect(sock, reinterpret_cast<const sockaddr*>(&sin), |
| 802 | sizeof(sin)) != 0) { |
| 803 | PrintSocketError("connect"); |
| 804 | closesocket(sock); |
| 805 | return -1; |
| 806 | } |
| 807 | return sock; |
| 808 | } |
| 809 | |
| 810 | class SocketCloser { |
| 811 | public: |
| 812 | explicit SocketCloser(int sock) : sock_(sock) {} |
| 813 | ~SocketCloser() { |
| 814 | // Half-close and drain the socket before releasing it. This seems to be |
| 815 | // necessary for graceful shutdown on Windows. It will also avoid write |
| 816 | // failures in the test runner. |
| 817 | #if defined(OPENSSL_WINDOWS) |
| 818 | shutdown(sock_, SD_SEND); |
| 819 | #else |
| 820 | shutdown(sock_, SHUT_WR); |
| 821 | #endif |
| 822 | while (true) { |
| 823 | char buf[1024]; |
| 824 | if (recv(sock_, buf, sizeof(buf), 0) <= 0) { |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | closesocket(sock_); |
| 829 | } |
| 830 | |
| 831 | private: |
| 832 | const int sock_; |
| 833 | }; |
| 834 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 835 | static bssl::UniquePtr<SSL_CTX> SetupCtx(const TestConfig *config) { |
| 836 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new( |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 837 | config->is_dtls ? DTLS_method() : TLS_method())); |
| 838 | if (!ssl_ctx) { |
| 839 | return nullptr; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 840 | } |
| 841 | |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 842 | // Enable TLS 1.3 for tests. |
| 843 | if (!config->is_dtls && |
David Benjamin | e470690 | 2016-09-20 15:12:23 -0400 | [diff] [blame] | 844 | !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION)) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 845 | return nullptr; |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 848 | std::string cipher_list = "ALL"; |
| 849 | if (!config->cipher.empty()) { |
| 850 | cipher_list = config->cipher; |
| 851 | SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 852 | } |
| 853 | if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) { |
| 854 | return nullptr; |
| 855 | } |
| 856 | |
| 857 | if (!config->cipher_tls10.empty() && |
| 858 | !SSL_CTX_set_cipher_list_tls10(ssl_ctx.get(), |
| 859 | config->cipher_tls10.c_str())) { |
| 860 | return nullptr; |
| 861 | } |
| 862 | if (!config->cipher_tls11.empty() && |
| 863 | !SSL_CTX_set_cipher_list_tls11(ssl_ctx.get(), |
| 864 | config->cipher_tls11.c_str())) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 865 | return nullptr; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 866 | } |
| 867 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 868 | bssl::UniquePtr<DH> dh(DH_get_2048_256(NULL)); |
David Benjamin | b7c5e84 | 2016-03-28 09:59:10 -0400 | [diff] [blame] | 869 | if (!dh) { |
| 870 | return nullptr; |
| 871 | } |
Adam Langley | c4f25ce | 2015-11-26 16:39:08 -0800 | [diff] [blame] | 872 | |
| 873 | if (config->use_sparse_dh_prime) { |
| 874 | // This prime number is 2^1024 + 643 – a value just above a power of two. |
| 875 | // Because of its form, values modulo it are essentially certain to be one |
| 876 | // byte shorter. This is used to test padding of these values. |
| 877 | if (BN_hex2bn( |
| 878 | &dh->p, |
| 879 | "1000000000000000000000000000000000000000000000000000000000000000" |
| 880 | "0000000000000000000000000000000000000000000000000000000000000000" |
| 881 | "0000000000000000000000000000000000000000000000000000000000000000" |
| 882 | "0000000000000000000000000000000000000000000000000000000000000028" |
| 883 | "3") == 0 || |
| 884 | !BN_set_word(dh->g, 2)) { |
| 885 | return nullptr; |
| 886 | } |
David Benjamin | e66148a | 2016-02-02 14:14:36 -0500 | [diff] [blame] | 887 | BN_free(dh->q); |
| 888 | dh->q = NULL; |
Adam Langley | c4f25ce | 2015-11-26 16:39:08 -0800 | [diff] [blame] | 889 | dh->priv_length = 0; |
| 890 | } |
| 891 | |
David Benjamin | b7c5e84 | 2016-03-28 09:59:10 -0400 | [diff] [blame] | 892 | if (!SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 893 | return nullptr; |
David Benjamin | f4e5c4e | 2014-08-02 17:35:45 -0400 | [diff] [blame] | 894 | } |
| 895 | |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 896 | if (config->async && config->is_server) { |
| 897 | // Disable the internal session cache. To test asynchronous session lookup, |
| 898 | // we use an external session cache. |
| 899 | SSL_CTX_set_session_cache_mode( |
| 900 | ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 901 | SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback); |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 902 | } else { |
| 903 | SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 904 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 905 | |
David Benjamin | d4c2bce | 2015-10-17 12:28:18 -0400 | [diff] [blame] | 906 | SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 907 | |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 908 | if (config->use_old_client_cert_callback) { |
| 909 | SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback); |
| 910 | } |
| 911 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 912 | SSL_CTX_set_next_protos_advertised_cb( |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 913 | ssl_ctx.get(), NextProtosAdvertisedCallback, NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 914 | if (!config->select_next_proto.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 915 | SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback, |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 916 | NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 917 | } |
| 918 | |
David Benjamin | 594e7d2 | 2016-03-17 17:49:56 -0400 | [diff] [blame] | 919 | if (!config->select_alpn.empty() || config->decline_alpn) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 920 | SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 921 | } |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 922 | |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 923 | SSL_CTX_enable_tls_channel_id(ssl_ctx.get()); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 924 | SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 925 | |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 926 | if (config->is_dtls) { |
| 927 | SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback); |
| 928 | } |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 929 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 930 | SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 931 | SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback); |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 932 | |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 933 | if (config->use_ticket_callback) { |
| 934 | SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback); |
| 935 | } |
| 936 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 937 | if (config->enable_client_custom_extension && |
| 938 | !SSL_CTX_add_client_custom_ext( |
| 939 | ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback, |
| 940 | CustomExtensionFreeCallback, kCustomExtensionAddArg, |
| 941 | CustomExtensionParseCallback, kCustomExtensionParseArg)) { |
| 942 | return nullptr; |
| 943 | } |
| 944 | |
| 945 | if (config->enable_server_custom_extension && |
| 946 | !SSL_CTX_add_server_custom_ext( |
| 947 | ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback, |
| 948 | CustomExtensionFreeCallback, kCustomExtensionAddArg, |
| 949 | CustomExtensionParseCallback, kCustomExtensionParseArg)) { |
| 950 | return nullptr; |
| 951 | } |
| 952 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 953 | if (config->verify_fail) { |
| 954 | SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL); |
| 955 | } else { |
| 956 | SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL); |
| 957 | } |
| 958 | |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 959 | if (!config->signed_cert_timestamps.empty() && |
| 960 | !SSL_CTX_set_signed_cert_timestamp_list( |
| 961 | ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(), |
| 962 | config->signed_cert_timestamps.size())) { |
| 963 | return nullptr; |
| 964 | } |
| 965 | |
David Benjamin | 2f8935d | 2016-07-13 19:47:39 -0400 | [diff] [blame] | 966 | if (config->use_null_client_ca_list) { |
| 967 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr); |
| 968 | } |
| 969 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 970 | if (config->enable_grease) { |
| 971 | SSL_CTX_set_grease_enabled(ssl_ctx.get(), 1); |
| 972 | } |
| 973 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 974 | return ssl_ctx; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 975 | } |
| 976 | |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 977 | // RetryAsync is called after a failed operation on |ssl| with return code |
| 978 | // |ret|. If the operation should be retried, it simulates one asynchronous |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 979 | // event and returns true. Otherwise it returns false. |
| 980 | static bool RetryAsync(SSL *ssl, int ret) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 981 | // No error; don't retry. |
| 982 | if (ret >= 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 983 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 984 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 985 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 986 | TestState *test_state = GetTestState(ssl); |
Matt Braithwaite | 6278e24 | 2016-06-14 08:18:22 -0700 | [diff] [blame] | 987 | assert(GetTestConfig(ssl)->async); |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 988 | |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 989 | if (test_state->packeted_bio != nullptr && |
| 990 | PacketedBioAdvanceClock(test_state->packeted_bio)) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 991 | // The DTLS retransmit logic silently ignores write failures. So the test |
| 992 | // may progress, allow writes through synchronously. |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 993 | AsyncBioEnforceWriteQuota(test_state->async_bio, false); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 994 | int timeout_ret = DTLSv1_handle_timeout(ssl); |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 995 | AsyncBioEnforceWriteQuota(test_state->async_bio, true); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 996 | |
| 997 | if (timeout_ret < 0) { |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 998 | fprintf(stderr, "Error retransmitting.\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 999 | return false; |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1000 | } |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1001 | return true; |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1002 | } |
| 1003 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1004 | // See if we needed to read or write more. If so, allow one byte through on |
| 1005 | // the appropriate end to maximally stress the state machine. |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1006 | switch (SSL_get_error(ssl, ret)) { |
| 1007 | case SSL_ERROR_WANT_READ: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1008 | AsyncBioAllowRead(test_state->async_bio, 1); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1009 | return true; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1010 | case SSL_ERROR_WANT_WRITE: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1011 | AsyncBioAllowWrite(test_state->async_bio, 1); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1012 | return true; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1013 | case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1014 | bssl::UniquePtr<EVP_PKEY> pkey = |
| 1015 | LoadPrivateKey(GetTestConfig(ssl)->send_channel_id); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1016 | if (!pkey) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1017 | return false; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1018 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1019 | test_state->channel_id = std::move(pkey); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1020 | return true; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1021 | } |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 1022 | case SSL_ERROR_WANT_X509_LOOKUP: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1023 | test_state->cert_ready = true; |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1024 | return true; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1025 | case SSL_ERROR_PENDING_SESSION: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1026 | test_state->session = std::move(test_state->pending_session); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1027 | return true; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 1028 | case SSL_ERROR_PENDING_CERTIFICATE: |
| 1029 | // The handshake will resume without a second call to the early callback. |
| 1030 | return InstallCertificate(ssl); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1031 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1032 | test_state->private_key_retries++; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1033 | return true; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1034 | default: |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1035 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1036 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1037 | } |
| 1038 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1039 | // DoRead reads from |ssl|, resolving any asynchronous operations. It returns |
| 1040 | // the result value of the final |SSL_read| call. |
| 1041 | static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1042 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1043 | TestState *test_state = GetTestState(ssl); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1044 | int ret; |
| 1045 | do { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1046 | if (config->async) { |
| 1047 | // The DTLS retransmit logic silently ignores write failures. So the test |
| 1048 | // may progress, allow writes through synchronously. |SSL_read| may |
| 1049 | // trigger a retransmit, so disconnect the write quota. |
| 1050 | AsyncBioEnforceWriteQuota(test_state->async_bio, false); |
| 1051 | } |
David Benjamin | f3fbade | 2016-09-19 13:08:16 -0400 | [diff] [blame] | 1052 | ret = config->peek_then_read ? SSL_peek(ssl, out, max_out) |
| 1053 | : SSL_read(ssl, out, max_out); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1054 | if (config->async) { |
| 1055 | AsyncBioEnforceWriteQuota(test_state->async_bio, true); |
| 1056 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1057 | } while (config->async && RetryAsync(ssl, ret)); |
David Benjamin | f3fbade | 2016-09-19 13:08:16 -0400 | [diff] [blame] | 1058 | |
| 1059 | if (config->peek_then_read && ret > 0) { |
| 1060 | std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]); |
| 1061 | |
| 1062 | // SSL_peek should synchronously return the same data. |
| 1063 | int ret2 = SSL_peek(ssl, buf.get(), ret); |
| 1064 | if (ret2 != ret || |
| 1065 | memcmp(buf.get(), out, ret) != 0) { |
| 1066 | fprintf(stderr, "First and second SSL_peek did not match.\n"); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | // SSL_read should synchronously return the same data and consume it. |
| 1071 | ret2 = SSL_read(ssl, buf.get(), ret); |
| 1072 | if (ret2 != ret || |
| 1073 | memcmp(buf.get(), out, ret) != 0) { |
| 1074 | fprintf(stderr, "SSL_peek and SSL_read did not match.\n"); |
| 1075 | return -1; |
| 1076 | } |
| 1077 | } |
| 1078 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1079 | return ret; |
| 1080 | } |
| 1081 | |
| 1082 | // WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous |
| 1083 | // operations. It returns the result of the final |SSL_write| call. |
| 1084 | 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] | 1085 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1086 | int ret; |
| 1087 | do { |
| 1088 | ret = SSL_write(ssl, in, in_len); |
| 1089 | if (ret > 0) { |
| 1090 | in += ret; |
| 1091 | in_len -= ret; |
| 1092 | } |
| 1093 | } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0)); |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1097 | // DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It |
| 1098 | // returns the result of the final |SSL_shutdown| call. |
| 1099 | static int DoShutdown(SSL *ssl) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1100 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1101 | int ret; |
| 1102 | do { |
| 1103 | ret = SSL_shutdown(ssl); |
| 1104 | } while (config->async && RetryAsync(ssl, ret)); |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1108 | // DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous |
| 1109 | // operations. It returns the result of the final |SSL_send_fatal_alert| call. |
| 1110 | static int DoSendFatalAlert(SSL *ssl, uint8_t alert) { |
| 1111 | const TestConfig *config = GetTestConfig(ssl); |
| 1112 | int ret; |
| 1113 | do { |
| 1114 | ret = SSL_send_fatal_alert(ssl, alert); |
| 1115 | } while (config->async && RetryAsync(ssl, ret)); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1119 | static uint16_t GetProtocolVersion(const SSL *ssl) { |
| 1120 | uint16_t version = SSL_version(ssl); |
| 1121 | if (!SSL_is_dtls(ssl)) { |
| 1122 | return version; |
| 1123 | } |
| 1124 | return 0x0201 + ~version; |
| 1125 | } |
| 1126 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1127 | // CheckHandshakeProperties checks, immediately after |ssl| completes its |
| 1128 | // initial handshake (or False Starts), whether all the properties are |
| 1129 | // consistent with the test configuration and invariants. |
| 1130 | static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1131 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1132 | |
| 1133 | if (SSL_get_current_cipher(ssl) == nullptr) { |
| 1134 | fprintf(stderr, "null cipher after handshake\n"); |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | if (is_resume && |
| 1139 | (!!SSL_session_reused(ssl) == config->expect_session_miss)) { |
| 1140 | fprintf(stderr, "session was%s reused\n", |
| 1141 | SSL_session_reused(ssl) ? "" : " not"); |
| 1142 | return false; |
| 1143 | } |
| 1144 | |
| 1145 | bool expect_handshake_done = is_resume || !config->false_start; |
| 1146 | if (expect_handshake_done != GetTestState(ssl)->handshake_done) { |
| 1147 | fprintf(stderr, "handshake was%s completed\n", |
| 1148 | GetTestState(ssl)->handshake_done ? "" : " not"); |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1152 | if (expect_handshake_done && !config->is_server) { |
| 1153 | bool expect_new_session = |
| 1154 | !config->expect_no_session && |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1155 | (!SSL_session_reused(ssl) || config->expect_ticket_renewal) && |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1156 | // Session tickets are sent post-handshake in TLS 1.3. |
| 1157 | GetProtocolVersion(ssl) < TLS1_3_VERSION; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1158 | if (expect_new_session != GetTestState(ssl)->got_new_session) { |
| 1159 | fprintf(stderr, |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1160 | "new session was%s cached, but we expected the opposite\n", |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1161 | GetTestState(ssl)->got_new_session ? "" : " not"); |
| 1162 | return false; |
| 1163 | } |
| 1164 | } |
| 1165 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1166 | if (config->is_server && !GetTestState(ssl)->early_callback_called) { |
| 1167 | fprintf(stderr, "early callback not called\n"); |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | if (!config->expected_server_name.empty()) { |
| 1172 | const char *server_name = |
| 1173 | SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 1174 | if (server_name != config->expected_server_name) { |
| 1175 | fprintf(stderr, "servername mismatch (got %s; want %s)\n", |
| 1176 | server_name, config->expected_server_name.c_str()); |
| 1177 | return false; |
| 1178 | } |
| 1179 | } |
| 1180 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1181 | if (!config->expected_next_proto.empty()) { |
| 1182 | const uint8_t *next_proto; |
| 1183 | unsigned next_proto_len; |
| 1184 | SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
| 1185 | if (next_proto_len != config->expected_next_proto.size() || |
| 1186 | memcmp(next_proto, config->expected_next_proto.data(), |
| 1187 | next_proto_len) != 0) { |
| 1188 | fprintf(stderr, "negotiated next proto mismatch\n"); |
| 1189 | return false; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | if (!config->expected_alpn.empty()) { |
| 1194 | const uint8_t *alpn_proto; |
| 1195 | unsigned alpn_proto_len; |
| 1196 | SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len); |
| 1197 | if (alpn_proto_len != config->expected_alpn.size() || |
| 1198 | memcmp(alpn_proto, config->expected_alpn.data(), |
| 1199 | alpn_proto_len) != 0) { |
| 1200 | fprintf(stderr, "negotiated alpn proto mismatch\n"); |
| 1201 | return false; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | if (!config->expected_channel_id.empty()) { |
| 1206 | uint8_t channel_id[64]; |
| 1207 | if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) { |
| 1208 | fprintf(stderr, "no channel id negotiated\n"); |
| 1209 | return false; |
| 1210 | } |
| 1211 | if (config->expected_channel_id.size() != 64 || |
| 1212 | memcmp(config->expected_channel_id.data(), |
| 1213 | channel_id, 64) != 0) { |
| 1214 | fprintf(stderr, "channel id mismatch\n"); |
| 1215 | return false; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | if (config->expect_extended_master_secret) { |
David Benjamin | 8ac3571 | 2016-07-13 21:07:29 -0400 | [diff] [blame] | 1220 | if (!SSL_get_extms_support(ssl)) { |
| 1221 | fprintf(stderr, "No EMS for connection when expected"); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (!config->expected_ocsp_response.empty()) { |
| 1227 | const uint8_t *data; |
| 1228 | size_t len; |
| 1229 | SSL_get0_ocsp_response(ssl, &data, &len); |
| 1230 | if (config->expected_ocsp_response.size() != len || |
| 1231 | memcmp(config->expected_ocsp_response.data(), data, len) != 0) { |
| 1232 | fprintf(stderr, "OCSP response mismatch\n"); |
| 1233 | return false; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | if (!config->expected_signed_cert_timestamps.empty()) { |
| 1238 | const uint8_t *data; |
| 1239 | size_t len; |
| 1240 | SSL_get0_signed_cert_timestamp_list(ssl, &data, &len); |
| 1241 | if (config->expected_signed_cert_timestamps.size() != len || |
| 1242 | memcmp(config->expected_signed_cert_timestamps.data(), |
| 1243 | data, len) != 0) { |
| 1244 | fprintf(stderr, "SCT list mismatch\n"); |
| 1245 | return false; |
| 1246 | } |
| 1247 | } |
| 1248 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1249 | if (config->expect_verify_result) { |
| 1250 | int expected_verify_result = config->verify_fail ? |
| 1251 | X509_V_ERR_APPLICATION_VERIFICATION : |
| 1252 | X509_V_OK; |
| 1253 | |
| 1254 | if (SSL_get_verify_result(ssl) != expected_verify_result) { |
| 1255 | fprintf(stderr, "Wrong certificate verification result\n"); |
| 1256 | return false; |
| 1257 | } |
| 1258 | } |
| 1259 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1260 | if (config->expect_peer_signature_algorithm != 0 && |
| 1261 | config->expect_peer_signature_algorithm != |
| 1262 | SSL_get_peer_signature_algorithm(ssl)) { |
| 1263 | fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n", |
| 1264 | SSL_get_peer_signature_algorithm(ssl), |
| 1265 | config->expect_peer_signature_algorithm); |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1266 | return false; |
| 1267 | } |
| 1268 | |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1269 | if (config->expect_curve_id != 0) { |
| 1270 | uint16_t curve_id = SSL_get_curve_id(ssl); |
| 1271 | if (static_cast<uint16_t>(config->expect_curve_id) != curve_id) { |
| 1272 | fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id, |
| 1273 | static_cast<uint16_t>(config->expect_curve_id)); |
| 1274 | return false; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | if (config->expect_dhe_group_size != 0) { |
| 1279 | unsigned dhe_group_size = SSL_get_dhe_group_size(ssl); |
| 1280 | if (static_cast<unsigned>(config->expect_dhe_group_size) != |
| 1281 | dhe_group_size) { |
| 1282 | fprintf(stderr, "dhe_group_size was %u, wanted %d\n", dhe_group_size, |
| 1283 | config->expect_dhe_group_size); |
David Benjamin | 4cc36ad | 2015-12-19 14:23:26 -0500 | [diff] [blame] | 1284 | return false; |
| 1285 | } |
| 1286 | } |
| 1287 | |
David Benjamin | bb9e36e | 2016-08-03 14:14:47 -0400 | [diff] [blame] | 1288 | if (!config->psk.empty()) { |
| 1289 | if (SSL_get_peer_cert_chain(ssl) != nullptr) { |
| 1290 | fprintf(stderr, "Received peer certificate on a PSK cipher.\n"); |
| 1291 | return false; |
| 1292 | } |
| 1293 | } else if (!config->is_server || config->require_any_client_certificate) { |
| 1294 | if (SSL_get_peer_cert_chain(ssl) == nullptr) { |
| 1295 | fprintf(stderr, "Received no peer certificate but expected one.\n"); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1296 | return false; |
| 1297 | } |
| 1298 | } |
David Benjamin | bb9e36e | 2016-08-03 14:14:47 -0400 | [diff] [blame] | 1299 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1300 | return true; |
| 1301 | } |
| 1302 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1303 | // DoExchange runs a test SSL exchange against the peer. On success, it returns |
| 1304 | // true and sets |*out_session| to the negotiated SSL session. If the test is a |
| 1305 | // resumption attempt, |is_resume| is true and |session| is the session from the |
| 1306 | // previous exchange. |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1307 | static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session, |
| 1308 | SSL_CTX *ssl_ctx, const TestConfig *config, |
| 1309 | bool is_resume, SSL_SESSION *session) { |
| 1310 | bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx)); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1311 | if (!ssl) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1312 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1313 | } |
| 1314 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1315 | if (!SetTestConfig(ssl.get(), config) || |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 1316 | !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1317 | return false; |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 1318 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1319 | |
Adam Langley | 5f0efe0 | 2015-02-20 13:03:16 -0800 | [diff] [blame] | 1320 | if (config->fallback_scsv && |
| 1321 | !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) { |
| 1322 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1323 | } |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame^] | 1324 | // Install the certificate synchronously if nothing else will handle it. |
| 1325 | if (!config->use_early_callback && |
| 1326 | !config->use_old_client_cert_callback && |
| 1327 | !config->async && |
| 1328 | !InstallCertificate(ssl.get())) { |
| 1329 | return false; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1330 | } |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame^] | 1331 | SSL_set_cert_cb(ssl.get(), CertCallback, nullptr); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1332 | if (config->require_any_client_certificate) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1333 | 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] | 1334 | NULL); |
| 1335 | } |
| 1336 | if (config->verify_peer) { |
| 1337 | SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1338 | } |
| 1339 | if (config->false_start) { |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 1340 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1341 | } |
| 1342 | if (config->cbc_record_splitting) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1343 | SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1344 | } |
| 1345 | if (config->partial_write) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1346 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1347 | } |
Steven Valdez | 4f94b1c | 2016-05-24 12:31:07 -0400 | [diff] [blame] | 1348 | if (config->no_tls13) { |
| 1349 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3); |
| 1350 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1351 | if (config->no_tls12) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1352 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1353 | } |
| 1354 | if (config->no_tls11) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1355 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1356 | } |
| 1357 | if (config->no_tls1) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1358 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1359 | } |
| 1360 | if (config->no_ssl3) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1361 | SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1362 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1363 | if (!config->expected_channel_id.empty() || |
| 1364 | config->enable_channel_id) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1365 | SSL_enable_tls_channel_id(ssl.get()); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1366 | } |
| 1367 | if (!config->send_channel_id.empty()) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1368 | SSL_enable_tls_channel_id(ssl.get()); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1369 | if (!config->async) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1370 | // The async case will be supplied by |ChannelIdCallback|. |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1371 | bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(config->send_channel_id); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1372 | if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1373 | return false; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1374 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1375 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1376 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1377 | if (!config->host_name.empty() && |
| 1378 | !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1379 | return false; |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 1380 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1381 | if (!config->advertise_alpn.empty() && |
| 1382 | SSL_set_alpn_protos(ssl.get(), |
| 1383 | (const uint8_t *)config->advertise_alpn.data(), |
| 1384 | config->advertise_alpn.size()) != 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1385 | return false; |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 1386 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1387 | if (!config->psk.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1388 | SSL_set_psk_client_callback(ssl.get(), PskClientCallback); |
| 1389 | SSL_set_psk_server_callback(ssl.get(), PskServerCallback); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1390 | } |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1391 | if (!config->psk_identity.empty() && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1392 | !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1393 | return false; |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1394 | } |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1395 | if (!config->srtp_profiles.empty() && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1396 | !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1397 | return false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1398 | } |
| 1399 | if (config->enable_ocsp_stapling && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1400 | !SSL_enable_ocsp_stapling(ssl.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1401 | return false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1402 | } |
| 1403 | if (config->enable_signed_cert_timestamps && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1404 | !SSL_enable_signed_cert_timestamps(ssl.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1405 | return false; |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 1406 | } |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1407 | if (config->min_version != 0 && |
David Benjamin | e470690 | 2016-09-20 15:12:23 -0400 | [diff] [blame] | 1408 | !SSL_set_min_proto_version(ssl.get(), (uint16_t)config->min_version)) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1409 | return false; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 1410 | } |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1411 | if (config->max_version != 0 && |
David Benjamin | e470690 | 2016-09-20 15:12:23 -0400 | [diff] [blame] | 1412 | !SSL_set_max_proto_version(ssl.get(), (uint16_t)config->max_version)) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1413 | return false; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 1414 | } |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 1415 | if (config->mtu != 0) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1416 | SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU); |
| 1417 | SSL_set_mtu(ssl.get(), config->mtu); |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 1418 | } |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 1419 | if (config->install_ddos_callback) { |
| 1420 | SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback); |
| 1421 | } |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 1422 | if (config->renegotiate_once) { |
| 1423 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once); |
| 1424 | } |
| 1425 | if (config->renegotiate_freely) { |
| 1426 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely); |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 1427 | } |
Adam Langley | 27a0d08 | 2015-11-03 13:34:10 -0800 | [diff] [blame] | 1428 | if (config->renegotiate_ignore) { |
| 1429 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore); |
| 1430 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1431 | if (!config->check_close_notify) { |
| 1432 | SSL_set_quiet_shutdown(ssl.get(), 1); |
| 1433 | } |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 1434 | if (config->disable_npn) { |
| 1435 | SSL_set_options(ssl.get(), SSL_OP_DISABLE_NPN); |
| 1436 | } |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 1437 | if (config->p384_only) { |
| 1438 | int nid = NID_secp384r1; |
| 1439 | if (!SSL_set1_curves(ssl.get(), &nid, 1)) { |
| 1440 | return false; |
| 1441 | } |
| 1442 | } |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 1443 | if (config->enable_all_curves) { |
| 1444 | static const int kAllCurves[] = { |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1445 | NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_X25519, |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 1446 | }; |
| 1447 | if (!SSL_set1_curves(ssl.get(), kAllCurves, |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 1448 | OPENSSL_ARRAY_SIZE(kAllCurves))) { |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | } |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 1452 | if (config->initial_timeout_duration_ms > 0) { |
| 1453 | DTLSv1_set_initial_timeout_duration(ssl.get(), |
| 1454 | config->initial_timeout_duration_ms); |
| 1455 | } |
David Benjamin | a252b34 | 2016-09-26 19:57:53 -0400 | [diff] [blame] | 1456 | if (config->max_cert_list > 0) { |
| 1457 | SSL_set_max_cert_list(ssl.get(), config->max_cert_list); |
| 1458 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1459 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1460 | int sock = Connect(config->port); |
| 1461 | if (sock == -1) { |
| 1462 | return false; |
| 1463 | } |
| 1464 | SocketCloser closer(sock); |
| 1465 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1466 | bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_NOCLOSE)); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1467 | if (!bio) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1468 | return false; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1469 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1470 | if (config->is_dtls) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1471 | bssl::UniquePtr<BIO> packeted = PacketedBioCreate(!config->async); |
David Benjamin | b7c5e84 | 2016-03-28 09:59:10 -0400 | [diff] [blame] | 1472 | if (!packeted) { |
| 1473 | return false; |
| 1474 | } |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 1475 | GetTestState(ssl.get())->packeted_bio = packeted.get(); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1476 | BIO_push(packeted.get(), bio.release()); |
| 1477 | bio = std::move(packeted); |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1478 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1479 | if (config->async) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1480 | bssl::UniquePtr<BIO> async_scoped = |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1481 | config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate(); |
David Benjamin | b7c5e84 | 2016-03-28 09:59:10 -0400 | [diff] [blame] | 1482 | if (!async_scoped) { |
| 1483 | return false; |
| 1484 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1485 | BIO_push(async_scoped.get(), bio.release()); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1486 | GetTestState(ssl.get())->async_bio = async_scoped.get(); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1487 | bio = std::move(async_scoped); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1488 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1489 | SSL_set_bio(ssl.get(), bio.get(), bio.get()); |
| 1490 | bio.release(); // SSL_set_bio takes ownership. |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1491 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1492 | if (session != NULL) { |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1493 | if (!config->is_server) { |
| 1494 | if (SSL_set_session(ssl.get(), session) != 1) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1495 | return false; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1496 | } |
| 1497 | } else if (config->async) { |
| 1498 | // The internal session cache is disabled, so install the session |
| 1499 | // manually. |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 1500 | SSL_SESSION_up_ref(session); |
| 1501 | GetTestState(ssl.get())->pending_session.reset(session); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
David Benjamin | a07c0fc | 2015-05-13 13:19:42 -0400 | [diff] [blame] | 1505 | if (SSL_get_current_cipher(ssl.get()) != nullptr) { |
| 1506 | fprintf(stderr, "non-null cipher before handshake\n"); |
| 1507 | return false; |
| 1508 | } |
| 1509 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1510 | int ret; |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 1511 | if (config->implicit_handshake) { |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1512 | if (config->is_server) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1513 | SSL_set_accept_state(ssl.get()); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1514 | } else { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1515 | SSL_set_connect_state(ssl.get()); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1516 | } |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 1517 | } else { |
| 1518 | do { |
| 1519 | if (config->is_server) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1520 | ret = SSL_accept(ssl.get()); |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 1521 | } else { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1522 | ret = SSL_connect(ssl.get()); |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 1523 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1524 | } while (config->async && RetryAsync(ssl.get(), ret)); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1525 | if (ret != 1 || |
| 1526 | !CheckHandshakeProperties(ssl.get(), is_resume)) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1527 | return false; |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 1528 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1529 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1530 | // Reset the state to assert later that the callback isn't called in |
| 1531 | // renegotations. |
| 1532 | GetTestState(ssl.get())->got_new_session = false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1533 | } |
| 1534 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1535 | if (config->export_keying_material > 0) { |
| 1536 | std::vector<uint8_t> result( |
| 1537 | static_cast<size_t>(config->export_keying_material)); |
| 1538 | if (!SSL_export_keying_material( |
| 1539 | ssl.get(), result.data(), result.size(), |
| 1540 | config->export_label.data(), config->export_label.size(), |
| 1541 | reinterpret_cast<const uint8_t*>(config->export_context.data()), |
| 1542 | config->export_context.size(), config->use_export_context)) { |
| 1543 | fprintf(stderr, "failed to export keying material\n"); |
| 1544 | return false; |
| 1545 | } |
| 1546 | if (WriteAll(ssl.get(), result.data(), result.size()) < 0) { |
| 1547 | return false; |
| 1548 | } |
| 1549 | } |
| 1550 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1551 | if (config->tls_unique) { |
| 1552 | uint8_t tls_unique[16]; |
| 1553 | size_t tls_unique_len; |
| 1554 | if (!SSL_get_tls_unique(ssl.get(), tls_unique, &tls_unique_len, |
| 1555 | sizeof(tls_unique))) { |
| 1556 | fprintf(stderr, "failed to get tls-unique\n"); |
| 1557 | return false; |
| 1558 | } |
| 1559 | |
| 1560 | if (tls_unique_len != 12) { |
| 1561 | fprintf(stderr, "expected 12 bytes of tls-unique but got %u", |
| 1562 | static_cast<unsigned>(tls_unique_len)); |
| 1563 | return false; |
| 1564 | } |
| 1565 | |
| 1566 | if (WriteAll(ssl.get(), tls_unique, tls_unique_len) < 0) { |
| 1567 | return false; |
| 1568 | } |
| 1569 | } |
| 1570 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1571 | if (config->send_alert) { |
| 1572 | if (DoSendFatalAlert(ssl.get(), SSL_AD_DECOMPRESSION_FAILURE) < 0) { |
| 1573 | return false; |
| 1574 | } |
| 1575 | return true; |
| 1576 | } |
| 1577 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1578 | if (config->write_different_record_sizes) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1579 | if (config->is_dtls) { |
| 1580 | fprintf(stderr, "write_different_record_sizes not supported for DTLS\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1581 | return false; |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1582 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1583 | // This mode writes a number of different record sizes in an attempt to |
| 1584 | // trip up the CBC record splitting code. |
Adam Langley | bc94929 | 2015-06-18 21:32:44 -0700 | [diff] [blame] | 1585 | static const size_t kBufLen = 32769; |
| 1586 | std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]); |
| 1587 | memset(buf.get(), 0x42, kBufLen); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1588 | static const size_t kRecordSizes[] = { |
| 1589 | 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769}; |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 1590 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1591 | const size_t len = kRecordSizes[i]; |
Adam Langley | bc94929 | 2015-06-18 21:32:44 -0700 | [diff] [blame] | 1592 | if (len > kBufLen) { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1593 | fprintf(stderr, "Bad kRecordSizes value.\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1594 | return false; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1595 | } |
Adam Langley | bc94929 | 2015-06-18 21:32:44 -0700 | [diff] [blame] | 1596 | if (WriteAll(ssl.get(), buf.get(), len) < 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1597 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1598 | } |
| 1599 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1600 | } else { |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 1601 | if (config->shim_writes_first) { |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1602 | if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"), |
| 1603 | 5) < 0) { |
| 1604 | return false; |
| 1605 | } |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 1606 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1607 | if (!config->shim_shuts_down) { |
| 1608 | for (;;) { |
Adam Langley | a0a8dc2 | 2015-09-09 10:22:00 -0700 | [diff] [blame] | 1609 | static const size_t kBufLen = 16384; |
| 1610 | std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]); |
| 1611 | |
David Benjamin | 2c99d28 | 2015-09-01 10:23:00 -0400 | [diff] [blame] | 1612 | // Read only 512 bytes at a time in TLS to ensure records may be |
| 1613 | // returned in multiple reads. |
Adam Langley | a0a8dc2 | 2015-09-09 10:22:00 -0700 | [diff] [blame] | 1614 | int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512); |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1615 | int err = SSL_get_error(ssl.get(), n); |
| 1616 | if (err == SSL_ERROR_ZERO_RETURN || |
| 1617 | (n == 0 && err == SSL_ERROR_SYSCALL)) { |
| 1618 | if (n != 0) { |
| 1619 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 1620 | return false; |
| 1621 | } |
| 1622 | // Stop on either clean or unclean shutdown. |
| 1623 | break; |
| 1624 | } else if (err != SSL_ERROR_NONE) { |
| 1625 | if (n > 0) { |
| 1626 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 1627 | return false; |
| 1628 | } |
| 1629 | return false; |
| 1630 | } |
| 1631 | // Successfully read data. |
| 1632 | if (n <= 0) { |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1633 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1634 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1635 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1636 | |
| 1637 | // After a successful read, with or without False Start, the handshake |
| 1638 | // must be complete. |
| 1639 | if (!GetTestState(ssl.get())->handshake_done) { |
| 1640 | fprintf(stderr, "handshake was not completed after SSL_read\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1641 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 1642 | } |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 1643 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1644 | for (int i = 0; i < n; i++) { |
| 1645 | buf[i] ^= 0xff; |
| 1646 | } |
Adam Langley | a0a8dc2 | 2015-09-09 10:22:00 -0700 | [diff] [blame] | 1647 | if (WriteAll(ssl.get(), buf.get(), n) < 0) { |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1648 | return false; |
| 1649 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 1650 | } |
| 1651 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1652 | } |
| 1653 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1654 | if (!config->is_server && !config->false_start && |
| 1655 | !config->implicit_handshake && |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1656 | // Session tickets are sent post-handshake in TLS 1.3. |
| 1657 | GetProtocolVersion(ssl.get()) < TLS1_3_VERSION && |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1658 | GetTestState(ssl.get())->got_new_session) { |
| 1659 | fprintf(stderr, "new session was established after the handshake\n"); |
| 1660 | return false; |
| 1661 | } |
| 1662 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1663 | if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) { |
| 1664 | bool expect_new_session = |
| 1665 | !config->expect_no_session && !config->shim_shuts_down; |
| 1666 | if (expect_new_session != GetTestState(ssl.get())->got_new_session) { |
| 1667 | fprintf(stderr, |
| 1668 | "new session was%s cached, but we expected the opposite\n", |
| 1669 | GetTestState(ssl.get())->got_new_session ? "" : " not"); |
| 1670 | return false; |
| 1671 | } |
| 1672 | } |
| 1673 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1674 | if (out_session) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1675 | *out_session = std::move(GetTestState(ssl.get())->new_session); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1676 | } |
| 1677 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1678 | ret = DoShutdown(ssl.get()); |
| 1679 | |
| 1680 | if (config->shim_shuts_down && config->check_close_notify) { |
| 1681 | // We initiate shutdown, so |SSL_shutdown| will return in two stages. First |
| 1682 | // it returns zero when our close_notify is sent, then one when the peer's |
| 1683 | // is received. |
| 1684 | if (ret != 0) { |
| 1685 | fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret); |
| 1686 | return false; |
| 1687 | } |
| 1688 | ret = DoShutdown(ssl.get()); |
| 1689 | } |
| 1690 | |
| 1691 | if (ret != 1) { |
| 1692 | fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret); |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
David Benjamin | 324dce4 | 2015-10-12 19:49:00 -0400 | [diff] [blame] | 1696 | if (SSL_total_renegotiations(ssl.get()) != |
| 1697 | config->expect_total_renegotiations) { |
| 1698 | fprintf(stderr, "Expected %d renegotiations, got %d\n", |
| 1699 | config->expect_total_renegotiations, |
| 1700 | SSL_total_renegotiations(ssl.get())); |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1704 | return true; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1705 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1706 | |
David Benjamin | ff3a149 | 2016-03-02 10:12:06 -0500 | [diff] [blame] | 1707 | class StderrDelimiter { |
| 1708 | public: |
| 1709 | ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); } |
| 1710 | }; |
| 1711 | |
Martin Kreichgauer | 19d5cf8 | 2016-08-09 17:48:22 -0700 | [diff] [blame] | 1712 | static int Main(int argc, char **argv) { |
David Benjamin | ff3a149 | 2016-03-02 10:12:06 -0500 | [diff] [blame] | 1713 | // To distinguish ASan's output from ours, add a trailing message to stderr. |
| 1714 | // Anything following this line will be considered an error. |
| 1715 | StderrDelimiter delimiter; |
| 1716 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1717 | #if defined(OPENSSL_WINDOWS) |
| 1718 | /* Initialize Winsock. */ |
| 1719 | WORD wsa_version = MAKEWORD(2, 2); |
| 1720 | WSADATA wsa_data; |
| 1721 | int wsa_err = WSAStartup(wsa_version, &wsa_data); |
| 1722 | if (wsa_err != 0) { |
| 1723 | fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); |
| 1724 | return 1; |
| 1725 | } |
| 1726 | if (wsa_data.wVersion != wsa_version) { |
| 1727 | fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); |
| 1728 | return 1; |
| 1729 | } |
| 1730 | #else |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1731 | signal(SIGPIPE, SIG_IGN); |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 1732 | #endif |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1733 | |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 1734 | CRYPTO_library_init(); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1735 | 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] | 1736 | 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] | 1737 | if (g_config_index < 0 || g_state_index < 0) { |
David Benjamin | ae3e487 | 2014-11-18 21:52:26 -0500 | [diff] [blame] | 1738 | return 1; |
| 1739 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1740 | |
| 1741 | TestConfig config; |
| 1742 | if (!ParseConfig(argc - 1, argv + 1, &config)) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1743 | return Usage(argv[0]); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1746 | bssl::UniquePtr<SSL_CTX> ssl_ctx = SetupCtx(&config); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1747 | if (!ssl_ctx) { |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 1748 | ERR_print_errors_fp(stderr); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1749 | return 1; |
| 1750 | } |
| 1751 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1752 | bssl::UniquePtr<SSL_SESSION> session; |
David Benjamin | 4666248 | 2016-08-17 00:51:00 -0400 | [diff] [blame] | 1753 | for (int i = 0; i < config.resume_count + 1; i++) { |
| 1754 | bool is_resume = i > 0; |
| 1755 | if (is_resume && !config.is_server && !session) { |
| 1756 | fprintf(stderr, "No session to offer.\n"); |
| 1757 | return 1; |
| 1758 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1759 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1760 | bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session); |
David Benjamin | 4666248 | 2016-08-17 00:51:00 -0400 | [diff] [blame] | 1761 | if (!DoExchange(&session, ssl_ctx.get(), &config, is_resume, |
| 1762 | offer_session.get())) { |
| 1763 | fprintf(stderr, "Connection %d failed.\n", i + 1); |
| 1764 | ERR_print_errors_fp(stderr); |
| 1765 | return 1; |
| 1766 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1769 | return 0; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1770 | } |
Martin Kreichgauer | 19d5cf8 | 2016-08-09 17:48:22 -0700 | [diff] [blame] | 1771 | |
| 1772 | } // namespace bssl |
| 1773 | |
| 1774 | int main(int argc, char **argv) { |
| 1775 | return bssl::Main(argc, argv); |
| 1776 | } |