David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
David Benjamin | 4cc36ad | 2015-12-19 14:23:26 -0500 | [diff] [blame] | 15 | #if !defined(__STDC_FORMAT_MACROS) |
| 16 | #define __STDC_FORMAT_MACROS |
| 17 | #endif |
| 18 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 19 | #include <openssl/base.h> |
| 20 | |
| 21 | #if !defined(OPENSSL_WINDOWS) |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 22 | #include <arpa/inet.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 23 | #include <netinet/in.h> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 24 | #include <netinet/tcp.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 25 | #include <signal.h> |
| 26 | #include <sys/socket.h> |
David Benjamin | 0abd6f2 | 2015-12-04 21:49:53 -0500 | [diff] [blame] | 27 | #include <sys/time.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 28 | #include <unistd.h> |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 29 | #else |
| 30 | #include <io.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 31 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 32 | #include <winsock2.h> |
| 33 | #include <ws2tcpip.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 34 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 35 | |
David Benjamin | 4fec04b | 2016-10-10 14:56:47 -0400 | [diff] [blame] | 36 | OPENSSL_MSVC_PRAGMA(comment(lib, "Ws2_32.lib")) |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 39 | #include <assert.h> |
David Benjamin | 4cc36ad | 2015-12-19 14:23:26 -0500 | [diff] [blame] | 40 | #include <inttypes.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 41 | #include <string.h> |
David Benjamin | c386440 | 2017-07-14 16:48:36 -0400 | [diff] [blame] | 42 | #include <time.h> |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 43 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 44 | #include <openssl/aead.h> |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 45 | #include <openssl/bio.h> |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 46 | #include <openssl/buf.h> |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 47 | #include <openssl/bytestring.h> |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 48 | #include <openssl/cipher.h> |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 49 | #include <openssl/crypto.h> |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 50 | #include <openssl/digest.h> |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 51 | #include <openssl/err.h> |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 52 | #include <openssl/evp.h> |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 53 | #include <openssl/hmac.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 54 | #include <openssl/nid.h> |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 55 | #include <openssl/rand.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 56 | #include <openssl/ssl.h> |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 57 | #include <openssl/x509.h> |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 58 | |
David Benjamin | 45fb1be | 2015-03-22 16:31:27 -0400 | [diff] [blame] | 59 | #include <memory> |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 60 | #include <string> |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 61 | #include <vector> |
David Benjamin | 45fb1be | 2015-03-22 16:31:27 -0400 | [diff] [blame] | 62 | |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 63 | #include "../../crypto/internal.h" |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 64 | #include "../internal.h" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 65 | #include "async_bio.h" |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 66 | #include "fuzzer.h" |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 67 | #include "packeted_bio.h" |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 68 | #include "test_config.h" |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 69 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 70 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 71 | static CRYPTO_BUFFER_POOL *g_pool = nullptr; |
| 72 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 73 | #if !defined(OPENSSL_WINDOWS) |
| 74 | static int closesocket(int sock) { |
| 75 | return close(sock); |
| 76 | } |
| 77 | |
| 78 | static void PrintSocketError(const char *func) { |
| 79 | perror(func); |
| 80 | } |
| 81 | #else |
| 82 | static void PrintSocketError(const char *func) { |
| 83 | fprintf(stderr, "%s: %d\n", func, WSAGetLastError()); |
| 84 | } |
| 85 | #endif |
| 86 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 87 | static int Usage(const char *program) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 88 | fprintf(stderr, "Usage: %s [flags...]\n", program); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 89 | return 1; |
| 90 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 91 | |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 92 | struct TestState { |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 93 | // async_bio is async BIO which pauses reads and writes. |
| 94 | BIO *async_bio = nullptr; |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 95 | // packeted_bio is the packeted BIO which simulates read timeouts. |
| 96 | BIO *packeted_bio = nullptr; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 97 | bssl::UniquePtr<EVP_PKEY> channel_id; |
David Benjamin | 0d4db50 | 2015-03-23 18:46:05 -0400 | [diff] [blame] | 98 | bool cert_ready = false; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 99 | bssl::UniquePtr<SSL_SESSION> session; |
| 100 | bssl::UniquePtr<SSL_SESSION> pending_session; |
David Benjamin | 0d4db50 | 2015-03-23 18:46:05 -0400 | [diff] [blame] | 101 | bool early_callback_called = false; |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 102 | bool handshake_done = false; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 103 | // private_key is the underlying private key used when testing custom keys. |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 104 | bssl::UniquePtr<EVP_PKEY> private_key; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 105 | std::vector<uint8_t> private_key_result; |
| 106 | // private_key_retries is the number of times an asynchronous private key |
| 107 | // operation has been retried. |
| 108 | unsigned private_key_retries = 0; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 109 | bool got_new_session = false; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 110 | bssl::UniquePtr<SSL_SESSION> new_session; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 111 | bool ticket_decrypt_done = false; |
| 112 | bool alpn_select_done = false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 113 | bool is_resume = false; |
Alessandro Ghedini | 958346a | 2016-12-20 19:42:15 +0000 | [diff] [blame] | 114 | bool early_callback_ready = false; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 115 | bool custom_verify_ready = false; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 116 | }; |
| 117 | |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 118 | static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 119 | int index, long argl, void *argp) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 120 | delete ((TestState *)ptr); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int g_config_index = 0; |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 124 | static int g_state_index = 0; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 125 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 126 | static bool SetTestConfig(SSL *ssl, const TestConfig *config) { |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 127 | return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 128 | } |
| 129 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 130 | static const TestConfig *GetTestConfig(const SSL *ssl) { |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 131 | return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 132 | } |
| 133 | |
David Benjamin | 2205093 | 2015-11-23 13:44:48 -0500 | [diff] [blame] | 134 | static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) { |
| 135 | // |SSL_set_ex_data| takes ownership of |state| only on success. |
| 136 | if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) { |
| 137 | state.release(); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 143 | static TestState *GetTestState(const SSL *ssl) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 144 | return (TestState *)SSL_get_ex_data(ssl, g_state_index); |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 145 | } |
| 146 | |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 147 | static bool LoadCertificate(bssl::UniquePtr<X509> *out_x509, |
| 148 | bssl::UniquePtr<STACK_OF(X509)> *out_chain, |
| 149 | const std::string &file) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 150 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 151 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 152 | return false; |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 153 | } |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 154 | |
| 155 | out_x509->reset(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 156 | if (!*out_x509) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | out_chain->reset(sk_X509_new_null()); |
| 161 | if (!*out_chain) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // Keep reading the certificate chain. |
| 166 | for (;;) { |
| 167 | bssl::UniquePtr<X509> cert( |
| 168 | PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 169 | if (!cert) { |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if (!sk_X509_push(out_chain->get(), cert.get())) { |
| 174 | return false; |
| 175 | } |
| 176 | cert.release(); // sk_X509_push takes ownership. |
| 177 | } |
| 178 | |
| 179 | uint32_t err = ERR_peek_last_error(); |
| 180 | if (ERR_GET_LIB(err) != ERR_LIB_PEM || |
| 181 | ERR_GET_REASON(err) != PEM_R_NO_START_LINE) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | ERR_clear_error(); |
| 186 | return true; |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 189 | static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) { |
| 190 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 191 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
| 192 | return nullptr; |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 193 | } |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 194 | return bssl::UniquePtr<EVP_PKEY>( |
| 195 | PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Adam Langley | 2ff7933 | 2017-02-28 13:45:39 -0800 | [diff] [blame] | 198 | static bool FromHexDigit(uint8_t *out, char c) { |
| 199 | if ('0' <= c && c <= '9') { |
| 200 | *out = c - '0'; |
| 201 | return true; |
| 202 | } |
| 203 | if ('a' <= c && c <= 'f') { |
| 204 | *out = c - 'a' + 10; |
| 205 | return true; |
| 206 | } |
| 207 | if ('A' <= c && c <= 'F') { |
| 208 | *out = c - 'A' + 10; |
| 209 | return true; |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | static bool HexDecode(std::string *out, const std::string &in) { |
| 215 | if ((in.size() & 1) != 0) { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | std::unique_ptr<uint8_t[]> buf(new uint8_t[in.size() / 2]); |
| 220 | for (size_t i = 0; i < in.size() / 2; i++) { |
| 221 | uint8_t high, low; |
| 222 | if (!FromHexDigit(&high, in[i*2]) || |
| 223 | !FromHexDigit(&low, in[i*2+1])) { |
| 224 | return false; |
| 225 | } |
| 226 | buf[i] = (high << 4) | low; |
| 227 | } |
| 228 | |
| 229 | out->assign(reinterpret_cast<const char *>(buf.get()), in.size() / 2); |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | static std::vector<std::string> SplitParts(const std::string &in, |
| 234 | const char delim) { |
| 235 | std::vector<std::string> ret; |
| 236 | size_t start = 0; |
| 237 | |
| 238 | for (size_t i = 0; i < in.size(); i++) { |
| 239 | if (in[i] == delim) { |
| 240 | ret.push_back(in.substr(start, i - start)); |
| 241 | start = i + 1; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | ret.push_back(in.substr(start, std::string::npos)); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static std::vector<std::string> DecodeHexStrings( |
| 250 | const std::string &hex_strings) { |
| 251 | std::vector<std::string> ret; |
| 252 | const std::vector<std::string> parts = SplitParts(hex_strings, ','); |
| 253 | |
| 254 | for (const auto &part : parts) { |
| 255 | std::string binary; |
| 256 | if (!HexDecode(&binary, part)) { |
| 257 | fprintf(stderr, "Bad hex string: %s\n", part.c_str()); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | ret.push_back(binary); |
| 262 | } |
| 263 | |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static bssl::UniquePtr<STACK_OF(X509_NAME)> DecodeHexX509Names( |
| 268 | const std::string &hex_names) { |
| 269 | const std::vector<std::string> der_names = DecodeHexStrings(hex_names); |
| 270 | bssl::UniquePtr<STACK_OF(X509_NAME)> ret(sk_X509_NAME_new_null()); |
David Benjamin | 0391f16 | 2017-06-15 15:27:59 -0400 | [diff] [blame] | 271 | if (!ret) { |
| 272 | return nullptr; |
| 273 | } |
Adam Langley | 2ff7933 | 2017-02-28 13:45:39 -0800 | [diff] [blame] | 274 | |
| 275 | for (const auto &der_name : der_names) { |
| 276 | const uint8_t *const data = |
| 277 | reinterpret_cast<const uint8_t *>(der_name.data()); |
| 278 | const uint8_t *derp = data; |
| 279 | bssl::UniquePtr<X509_NAME> name( |
| 280 | d2i_X509_NAME(nullptr, &derp, der_name.size())); |
| 281 | if (!name || derp != data + der_name.size()) { |
| 282 | fprintf(stderr, "Failed to parse X509_NAME.\n"); |
| 283 | return nullptr; |
| 284 | } |
| 285 | |
| 286 | if (!sk_X509_NAME_push(ret.get(), name.get())) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | name.release(); |
| 290 | } |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 295 | static ssl_private_key_result_t AsyncPrivateKeySign( |
| 296 | 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] | 297 | uint16_t signature_algorithm, const uint8_t *in, size_t in_len) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 298 | TestState *test_state = GetTestState(ssl); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 299 | if (!test_state->private_key_result.empty()) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 300 | fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n"); |
| 301 | abort(); |
| 302 | } |
| 303 | |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 304 | // Determine the hash. |
| 305 | const EVP_MD *md; |
| 306 | switch (signature_algorithm) { |
| 307 | case SSL_SIGN_RSA_PKCS1_SHA1: |
| 308 | case SSL_SIGN_ECDSA_SHA1: |
| 309 | md = EVP_sha1(); |
| 310 | break; |
| 311 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 312 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 313 | case SSL_SIGN_RSA_PSS_SHA256: |
| 314 | md = EVP_sha256(); |
| 315 | break; |
| 316 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 317 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 318 | case SSL_SIGN_RSA_PSS_SHA384: |
| 319 | md = EVP_sha384(); |
| 320 | break; |
| 321 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 322 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 323 | case SSL_SIGN_RSA_PSS_SHA512: |
| 324 | md = EVP_sha512(); |
| 325 | break; |
| 326 | case SSL_SIGN_RSA_PKCS1_MD5_SHA1: |
| 327 | md = EVP_md5_sha1(); |
| 328 | break; |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 329 | case SSL_SIGN_ED25519: |
| 330 | md = nullptr; |
| 331 | break; |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 332 | default: |
| 333 | fprintf(stderr, "Unknown signature algorithm %04x.\n", |
| 334 | signature_algorithm); |
| 335 | return ssl_private_key_failure; |
| 336 | } |
| 337 | |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 338 | bssl::ScopedEVP_MD_CTX ctx; |
| 339 | EVP_PKEY_CTX *pctx; |
| 340 | if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr, |
| 341 | test_state->private_key.get())) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 342 | return ssl_private_key_failure; |
| 343 | } |
| 344 | |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 345 | // Configure additional signature parameters. |
| 346 | switch (signature_algorithm) { |
| 347 | case SSL_SIGN_RSA_PSS_SHA256: |
| 348 | case SSL_SIGN_RSA_PSS_SHA384: |
| 349 | case SSL_SIGN_RSA_PSS_SHA512: |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 350 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 351 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 352 | -1 /* salt len = hash len */)) { |
| 353 | return ssl_private_key_failure; |
| 354 | } |
| 355 | } |
| 356 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 357 | // Write the signature into |test_state|. |
| 358 | size_t len = 0; |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 359 | if (!EVP_DigestSign(ctx.get(), nullptr, &len, in, in_len)) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 360 | return ssl_private_key_failure; |
| 361 | } |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 362 | test_state->private_key_result.resize(len); |
David Benjamin | 1967094 | 2017-05-31 19:07:31 -0400 | [diff] [blame] | 363 | if (!EVP_DigestSign(ctx.get(), test_state->private_key_result.data(), &len, |
| 364 | in, in_len)) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 365 | return ssl_private_key_failure; |
| 366 | } |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 367 | test_state->private_key_result.resize(len); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 368 | |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 369 | // The signature will be released asynchronously in |AsyncPrivateKeyComplete|. |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 370 | return ssl_private_key_retry; |
| 371 | } |
| 372 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 373 | static ssl_private_key_result_t AsyncPrivateKeyDecrypt( |
| 374 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 375 | const uint8_t *in, size_t in_len) { |
| 376 | TestState *test_state = GetTestState(ssl); |
| 377 | if (!test_state->private_key_result.empty()) { |
| 378 | fprintf(stderr, |
| 379 | "AsyncPrivateKeyDecrypt called with operation pending.\n"); |
| 380 | abort(); |
| 381 | } |
| 382 | |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 383 | RSA *rsa = EVP_PKEY_get0_RSA(test_state->private_key.get()); |
| 384 | if (rsa == NULL) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 385 | fprintf(stderr, |
| 386 | "AsyncPrivateKeyDecrypt called with incorrect key type.\n"); |
| 387 | abort(); |
| 388 | } |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 389 | test_state->private_key_result.resize(RSA_size(rsa)); |
David Benjamin | ef14b2d | 2015-11-11 14:01:27 -0800 | [diff] [blame] | 390 | if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(), |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 391 | RSA_size(rsa), in, in_len, RSA_NO_PADDING)) { |
| 392 | return ssl_private_key_failure; |
| 393 | } |
| 394 | |
| 395 | test_state->private_key_result.resize(*out_len); |
| 396 | |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 397 | // The decryption will be released asynchronously in |AsyncPrivateComplete|. |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 398 | return ssl_private_key_retry; |
| 399 | } |
| 400 | |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 401 | static ssl_private_key_result_t AsyncPrivateKeyComplete( |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 402 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) { |
| 403 | TestState *test_state = GetTestState(ssl); |
| 404 | if (test_state->private_key_result.empty()) { |
| 405 | fprintf(stderr, |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 406 | "AsyncPrivateKeyComplete called without operation pending.\n"); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 407 | abort(); |
| 408 | } |
| 409 | |
| 410 | if (test_state->private_key_retries < 2) { |
| 411 | // Only return the decryption on the second attempt, to test both incomplete |
| 412 | // |decrypt| and |decrypt_complete|. |
| 413 | return ssl_private_key_retry; |
| 414 | } |
| 415 | |
| 416 | if (max_out < test_state->private_key_result.size()) { |
| 417 | fprintf(stderr, "Output buffer too small.\n"); |
| 418 | return ssl_private_key_failure; |
| 419 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 420 | OPENSSL_memcpy(out, test_state->private_key_result.data(), |
| 421 | test_state->private_key_result.size()); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 422 | *out_len = test_state->private_key_result.size(); |
| 423 | |
| 424 | test_state->private_key_result.clear(); |
| 425 | test_state->private_key_retries = 0; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 426 | return ssl_private_key_success; |
| 427 | } |
| 428 | |
| 429 | static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = { |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 430 | nullptr /* type */, |
| 431 | nullptr /* max_signature_len */, |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 432 | AsyncPrivateKeySign, |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 433 | nullptr /* sign_digest */, |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 434 | AsyncPrivateKeyDecrypt, |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 435 | AsyncPrivateKeyComplete, |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 436 | }; |
| 437 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 438 | template<typename T> |
Adam Langley | 10f97f3 | 2016-07-12 08:09:33 -0700 | [diff] [blame] | 439 | struct Free { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 440 | void operator()(T *buf) { |
| 441 | free(buf); |
| 442 | } |
| 443 | }; |
| 444 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 445 | static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509, |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 446 | bssl::UniquePtr<STACK_OF(X509)> *out_chain, |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 447 | bssl::UniquePtr<EVP_PKEY> *out_pkey) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 448 | const TestConfig *config = GetTestConfig(ssl); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 449 | |
| 450 | if (!config->digest_prefs.empty()) { |
David Benjamin | 8ba6a14 | 2017-05-05 20:49:53 -0400 | [diff] [blame] | 451 | bssl::UniquePtr<char> digest_prefs( |
| 452 | OPENSSL_strdup(config->digest_prefs.c_str())); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 453 | std::vector<int> digest_list; |
| 454 | |
| 455 | for (;;) { |
Adam Langley | 67251f2 | 2015-09-23 15:01:07 -0700 | [diff] [blame] | 456 | char *token = |
| 457 | strtok(digest_list.empty() ? digest_prefs.get() : nullptr, ","); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 458 | if (token == nullptr) { |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | digest_list.push_back(EVP_MD_type(EVP_get_digestbyname(token))); |
| 463 | } |
| 464 | |
| 465 | if (!SSL_set_private_key_digest_prefs(ssl, digest_list.data(), |
| 466 | digest_list.size())) { |
| 467 | return false; |
| 468 | } |
| 469 | } |
| 470 | |
David Benjamin | ca3d545 | 2016-07-14 12:51:01 -0400 | [diff] [blame] | 471 | if (!config->signing_prefs.empty()) { |
| 472 | std::vector<uint16_t> u16s(config->signing_prefs.begin(), |
| 473 | config->signing_prefs.end()); |
| 474 | if (!SSL_set_signing_algorithm_prefs(ssl, u16s.data(), u16s.size())) { |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 479 | if (!config->key_file.empty()) { |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 480 | *out_pkey = LoadPrivateKey(config->key_file.c_str()); |
| 481 | if (!*out_pkey) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 482 | return false; |
| 483 | } |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 484 | } |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 485 | if (!config->cert_file.empty() && |
| 486 | !LoadCertificate(out_x509, out_chain, config->cert_file.c_str())) { |
| 487 | return false; |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 488 | } |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 489 | if (!config->ocsp_response.empty() && |
Alessandro Ghedini | 559f064 | 2016-12-07 12:55:32 +0000 | [diff] [blame] | 490 | !SSL_set_ocsp_response(ssl, (const uint8_t *)config->ocsp_response.data(), |
| 491 | config->ocsp_response.size())) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 492 | return false; |
| 493 | } |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 494 | return true; |
| 495 | } |
| 496 | |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 497 | static bool InstallCertificate(SSL *ssl) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 498 | bssl::UniquePtr<X509> x509; |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 499 | bssl::UniquePtr<STACK_OF(X509)> chain; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 500 | bssl::UniquePtr<EVP_PKEY> pkey; |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 501 | if (!GetCertificate(ssl, &x509, &chain, &pkey)) { |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 502 | return false; |
| 503 | } |
| 504 | |
| 505 | if (pkey) { |
| 506 | TestState *test_state = GetTestState(ssl); |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 507 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 508 | if (config->async) { |
| 509 | test_state->private_key = std::move(pkey); |
| 510 | SSL_set_private_key_method(ssl, &g_async_private_key_method); |
| 511 | } else if (!SSL_use_PrivateKey(ssl, pkey.get())) { |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (x509 && !SSL_use_certificate(ssl, x509.get())) { |
| 517 | return false; |
| 518 | } |
| 519 | |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 520 | if (sk_X509_num(chain.get()) > 0 && |
| 521 | !SSL_set1_chain(ssl, chain.get())) { |
| 522 | return false; |
| 523 | } |
| 524 | |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 525 | return true; |
| 526 | } |
| 527 | |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 528 | static enum ssl_select_cert_result_t SelectCertificateCallback( |
| 529 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 530 | const TestConfig *config = GetTestConfig(client_hello->ssl); |
| 531 | GetTestState(client_hello->ssl)->early_callback_called = true; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 532 | |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 533 | if (!config->expected_server_name.empty()) { |
| 534 | const uint8_t *extension_data; |
| 535 | size_t extension_len; |
| 536 | CBS extension, server_name_list, host_name; |
| 537 | uint8_t name_type; |
| 538 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 539 | if (!SSL_early_callback_ctx_extension_get( |
| 540 | client_hello, TLSEXT_TYPE_server_name, &extension_data, |
| 541 | &extension_len)) { |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 542 | fprintf(stderr, "Could not find server_name extension.\n"); |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 543 | return ssl_select_cert_error; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | CBS_init(&extension, extension_data, extension_len); |
| 547 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) || |
| 548 | CBS_len(&extension) != 0 || |
| 549 | !CBS_get_u8(&server_name_list, &name_type) || |
| 550 | name_type != TLSEXT_NAMETYPE_host_name || |
| 551 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 552 | CBS_len(&server_name_list) != 0) { |
| 553 | fprintf(stderr, "Could not decode server_name extension.\n"); |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 554 | return ssl_select_cert_error; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | if (!CBS_mem_equal(&host_name, |
| 558 | (const uint8_t*)config->expected_server_name.data(), |
| 559 | config->expected_server_name.size())) { |
| 560 | fprintf(stderr, "Server name mismatch.\n"); |
| 561 | } |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 562 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 563 | |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 564 | if (config->fail_early_callback) { |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 565 | return ssl_select_cert_error; |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 566 | } |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 567 | |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 568 | // Install the certificate in the early callback. |
| 569 | if (config->use_early_callback) { |
Alessandro Ghedini | 958346a | 2016-12-20 19:42:15 +0000 | [diff] [blame] | 570 | bool early_callback_ready = |
| 571 | GetTestState(client_hello->ssl)->early_callback_ready; |
| 572 | if (config->async && !early_callback_ready) { |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 573 | // Install the certificate asynchronously. |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 574 | return ssl_select_cert_retry; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 575 | } |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 576 | if (!InstallCertificate(client_hello->ssl)) { |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 577 | return ssl_select_cert_error; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 578 | } |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 579 | } |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 580 | return ssl_select_cert_success; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 581 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 582 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 583 | static bool CheckCertificateRequest(SSL *ssl) { |
| 584 | const TestConfig *config = GetTestConfig(ssl); |
| 585 | |
| 586 | if (!config->expected_certificate_types.empty()) { |
| 587 | const uint8_t *certificate_types; |
| 588 | size_t certificate_types_len = |
| 589 | SSL_get0_certificate_types(ssl, &certificate_types); |
| 590 | if (certificate_types_len != config->expected_certificate_types.size() || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 591 | OPENSSL_memcmp(certificate_types, |
| 592 | config->expected_certificate_types.data(), |
| 593 | certificate_types_len) != 0) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 594 | fprintf(stderr, "certificate types mismatch\n"); |
| 595 | return false; |
| 596 | } |
| 597 | } |
| 598 | |
Adam Langley | 2ff7933 | 2017-02-28 13:45:39 -0800 | [diff] [blame] | 599 | if (!config->expected_client_ca_list.empty()) { |
| 600 | bssl::UniquePtr<STACK_OF(X509_NAME)> expected = |
| 601 | DecodeHexX509Names(config->expected_client_ca_list); |
| 602 | const size_t num_expected = sk_X509_NAME_num(expected.get()); |
| 603 | |
| 604 | const STACK_OF(X509_NAME) *received = SSL_get_client_CA_list(ssl); |
| 605 | const size_t num_received = sk_X509_NAME_num(received); |
| 606 | |
| 607 | if (num_received != num_expected) { |
| 608 | fprintf(stderr, "expected %u names in CertificateRequest but got %u\n", |
| 609 | static_cast<unsigned>(num_expected), |
| 610 | static_cast<unsigned>(num_received)); |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | for (size_t i = 0; i < num_received; i++) { |
| 615 | if (X509_NAME_cmp(sk_X509_NAME_value(received, i), |
| 616 | sk_X509_NAME_value(expected.get(), i)) != 0) { |
| 617 | fprintf(stderr, "names in CertificateRequest differ at index #%d\n", |
| 618 | static_cast<unsigned>(i)); |
| 619 | return false; |
| 620 | } |
| 621 | } |
Adam Langley | d6c22ee | 2017-03-02 12:56:32 -0800 | [diff] [blame] | 622 | |
| 623 | STACK_OF(CRYPTO_BUFFER) *buffers = SSL_get0_server_requested_CAs(ssl); |
| 624 | if (sk_CRYPTO_BUFFER_num(buffers) != num_received) { |
| 625 | fprintf(stderr, |
| 626 | "Mismatch between SSL_get_server_requested_CAs and " |
| 627 | "SSL_get_client_CA_list.\n"); |
| 628 | return false; |
| 629 | } |
Adam Langley | 2ff7933 | 2017-02-28 13:45:39 -0800 | [diff] [blame] | 630 | } |
| 631 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 632 | return true; |
| 633 | } |
| 634 | |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 635 | static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 636 | if (!CheckCertificateRequest(ssl)) { |
| 637 | return -1; |
| 638 | } |
| 639 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 640 | if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) { |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 641 | return -1; |
| 642 | } |
| 643 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 644 | bssl::UniquePtr<X509> x509; |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 645 | bssl::UniquePtr<STACK_OF(X509)> chain; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 646 | bssl::UniquePtr<EVP_PKEY> pkey; |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 647 | if (!GetCertificate(ssl, &x509, &chain, &pkey)) { |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 648 | return -1; |
| 649 | } |
| 650 | |
| 651 | // Return zero for no certificate. |
| 652 | if (!x509) { |
| 653 | return 0; |
| 654 | } |
| 655 | |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 656 | // Chains and asynchronous private keys are not supported with client_cert_cb. |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 657 | *out_x509 = x509.release(); |
| 658 | *out_pkey = pkey.release(); |
| 659 | return 1; |
| 660 | } |
| 661 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 662 | static int CertCallback(SSL *ssl, void *arg) { |
| 663 | const TestConfig *config = GetTestConfig(ssl); |
| 664 | |
| 665 | // Check the CertificateRequest metadata is as expected. |
| 666 | if (!SSL_is_server(ssl) && !CheckCertificateRequest(ssl)) { |
| 667 | return -1; |
| 668 | } |
| 669 | |
| 670 | if (config->fail_cert_callback) { |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | // The certificate will be installed via other means. |
| 675 | if (!config->async || config->use_early_callback) { |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | if (!GetTestState(ssl)->cert_ready) { |
| 680 | return -1; |
| 681 | } |
| 682 | if (!InstallCertificate(ssl)) { |
| 683 | return 0; |
| 684 | } |
| 685 | return 1; |
| 686 | } |
| 687 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 688 | static bool CheckVerifyCallback(SSL *ssl) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 689 | const TestConfig *config = GetTestConfig(ssl); |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 690 | if (!config->expected_ocsp_response.empty()) { |
| 691 | const uint8_t *data; |
| 692 | size_t len; |
| 693 | SSL_get0_ocsp_response(ssl, &data, &len); |
| 694 | if (len == 0) { |
| 695 | fprintf(stderr, "OCSP response not available in verify callback\n"); |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 696 | return false; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 700 | return true; |
| 701 | } |
| 702 | |
| 703 | static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
| 704 | SSL* ssl = (SSL*)X509_STORE_CTX_get_ex_data(store_ctx, |
| 705 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 706 | const TestConfig *config = GetTestConfig(ssl); |
| 707 | if (!CheckVerifyCallback(ssl)) { |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | if (config->verify_fail) { |
| 712 | store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION; |
| 713 | return 0; |
| 714 | } |
| 715 | |
David Benjamin | 67666e7 | 2014-07-12 15:47:52 -0400 | [diff] [blame] | 716 | return 1; |
| 717 | } |
| 718 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 719 | static ssl_verify_result_t CustomVerifyCallback(SSL *ssl, uint8_t *out_alert) { |
| 720 | const TestConfig *config = GetTestConfig(ssl); |
| 721 | if (!CheckVerifyCallback(ssl)) { |
| 722 | return ssl_verify_invalid; |
| 723 | } |
| 724 | |
| 725 | if (config->async && !GetTestState(ssl)->custom_verify_ready) { |
| 726 | return ssl_verify_retry; |
| 727 | } |
| 728 | |
| 729 | if (config->verify_fail) { |
| 730 | return ssl_verify_invalid; |
| 731 | } |
| 732 | |
| 733 | return ssl_verify_ok; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 734 | } |
| 735 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 736 | static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out, |
| 737 | unsigned int *out_len, void *arg) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 738 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 739 | if (config->advertise_npn.empty()) { |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 740 | return SSL_TLSEXT_ERR_NOACK; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 741 | } |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 742 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 743 | *out = (const uint8_t*)config->advertise_npn.data(); |
| 744 | *out_len = config->advertise_npn.size(); |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 745 | return SSL_TLSEXT_ERR_OK; |
| 746 | } |
| 747 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 748 | static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen, |
David Benjamin | 8b36841 | 2015-03-14 01:54:17 -0400 | [diff] [blame] | 749 | const uint8_t* in, unsigned inlen, void* arg) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 750 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 751 | if (config->select_next_proto.empty()) { |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 752 | return SSL_TLSEXT_ERR_NOACK; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 753 | } |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 754 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 755 | *out = (uint8_t*)config->select_next_proto.data(); |
| 756 | *outlen = config->select_next_proto.size(); |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 757 | return SSL_TLSEXT_ERR_OK; |
| 758 | } |
| 759 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 760 | static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen, |
| 761 | const uint8_t* in, unsigned inlen, void* arg) { |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 762 | if (GetTestState(ssl)->alpn_select_done) { |
| 763 | fprintf(stderr, "AlpnSelectCallback called after completion.\n"); |
| 764 | exit(1); |
| 765 | } |
| 766 | |
| 767 | GetTestState(ssl)->alpn_select_done = true; |
| 768 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 769 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 594e7d2 | 2016-03-17 17:49:56 -0400 | [diff] [blame] | 770 | if (config->decline_alpn) { |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 771 | return SSL_TLSEXT_ERR_NOACK; |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 772 | } |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 773 | |
| 774 | if (!config->expected_advertised_alpn.empty() && |
| 775 | (config->expected_advertised_alpn.size() != inlen || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 776 | OPENSSL_memcmp(config->expected_advertised_alpn.data(), in, inlen) != |
| 777 | 0)) { |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 778 | fprintf(stderr, "bad ALPN select callback inputs\n"); |
| 779 | exit(1); |
| 780 | } |
| 781 | |
| 782 | *out = (const uint8_t*)config->select_alpn.data(); |
| 783 | *outlen = config->select_alpn.size(); |
| 784 | return SSL_TLSEXT_ERR_OK; |
| 785 | } |
| 786 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 787 | static unsigned PskClientCallback(SSL *ssl, const char *hint, |
| 788 | char *out_identity, |
| 789 | unsigned max_identity_len, |
| 790 | uint8_t *out_psk, unsigned max_psk_len) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 791 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 792 | |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 793 | if (config->psk_identity.empty()) { |
| 794 | if (hint != nullptr) { |
| 795 | fprintf(stderr, "Server PSK hint was non-null.\n"); |
| 796 | return 0; |
| 797 | } |
| 798 | } else if (hint == nullptr || |
| 799 | strcmp(hint, config->psk_identity.c_str()) != 0) { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 800 | fprintf(stderr, "Server PSK hint did not match.\n"); |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | // Account for the trailing '\0' for the identity. |
| 805 | if (config->psk_identity.size() >= max_identity_len || |
| 806 | config->psk.size() > max_psk_len) { |
| 807 | fprintf(stderr, "PSK buffers too small\n"); |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | BUF_strlcpy(out_identity, config->psk_identity.c_str(), |
| 812 | max_identity_len); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 813 | OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size()); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 814 | return config->psk.size(); |
| 815 | } |
| 816 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 817 | static unsigned PskServerCallback(SSL *ssl, const char *identity, |
| 818 | uint8_t *out_psk, unsigned max_psk_len) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 819 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 820 | |
| 821 | if (strcmp(identity, config->psk_identity.c_str()) != 0) { |
| 822 | fprintf(stderr, "Client PSK identity did not match.\n"); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | if (config->psk.size() > max_psk_len) { |
| 827 | fprintf(stderr, "PSK buffers too small\n"); |
| 828 | return 0; |
| 829 | } |
| 830 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 831 | OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size()); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 832 | return config->psk.size(); |
| 833 | } |
| 834 | |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 835 | static timeval g_clock; |
| 836 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 837 | static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 838 | *out_clock = g_clock; |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 839 | } |
| 840 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 841 | static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 842 | *out_pkey = GetTestState(ssl)->channel_id.release(); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 843 | } |
| 844 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 845 | static SSL_SESSION *GetSessionCallback(SSL *ssl, uint8_t *data, int len, |
| 846 | int *copy) { |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 847 | TestState *async_state = GetTestState(ssl); |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 848 | if (async_state->session) { |
| 849 | *copy = 0; |
| 850 | return async_state->session.release(); |
| 851 | } else if (async_state->pending_session) { |
| 852 | return SSL_magic_pending_session_ptr(); |
| 853 | } else { |
| 854 | return NULL; |
| 855 | } |
| 856 | } |
| 857 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 858 | static int DDoSCallback(const SSL_CLIENT_HELLO *client_hello) { |
| 859 | const TestConfig *config = GetTestConfig(client_hello->ssl); |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 860 | static int callback_num = 0; |
| 861 | |
| 862 | callback_num++; |
| 863 | if (config->fail_ddos_callback || |
| 864 | (config->fail_second_ddos_callback && callback_num == 2)) { |
| 865 | return 0; |
| 866 | } |
| 867 | return 1; |
| 868 | } |
| 869 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 870 | static void InfoCallback(const SSL *ssl, int type, int val) { |
| 871 | if (type == SSL_CB_HANDSHAKE_DONE) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 872 | if (GetTestConfig(ssl)->handshake_never_done) { |
David Benjamin | 7a4aaa4 | 2016-09-20 17:58:14 -0400 | [diff] [blame] | 873 | fprintf(stderr, "Handshake unexpectedly completed.\n"); |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 874 | // Abort before any expected error code is printed, to ensure the overall |
| 875 | // test fails. |
| 876 | abort(); |
| 877 | } |
| 878 | GetTestState(ssl)->handshake_done = true; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 879 | |
| 880 | // Callbacks may be called again on a new handshake. |
| 881 | GetTestState(ssl)->ticket_decrypt_done = false; |
| 882 | GetTestState(ssl)->alpn_select_done = false; |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 886 | static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) { |
| 887 | GetTestState(ssl)->got_new_session = true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 888 | GetTestState(ssl)->new_session.reset(session); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 889 | return 1; |
| 890 | } |
| 891 | |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 892 | static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 893 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 894 | int encrypt) { |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 895 | if (!encrypt) { |
| 896 | if (GetTestState(ssl)->ticket_decrypt_done) { |
| 897 | fprintf(stderr, "TicketKeyCallback called after completion.\n"); |
| 898 | return -1; |
| 899 | } |
| 900 | |
| 901 | GetTestState(ssl)->ticket_decrypt_done = true; |
| 902 | } |
| 903 | |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 904 | // This is just test code, so use the all-zeros key. |
| 905 | static const uint8_t kZeros[16] = {0}; |
| 906 | |
| 907 | if (encrypt) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 908 | OPENSSL_memcpy(key_name, kZeros, sizeof(kZeros)); |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 909 | RAND_bytes(iv, 16); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 910 | } else if (OPENSSL_memcmp(key_name, kZeros, 16) != 0) { |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) || |
| 915 | !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) { |
| 916 | return -1; |
| 917 | } |
| 918 | |
| 919 | if (!encrypt) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 920 | return GetTestConfig(ssl)->renew_ticket ? 2 : 1; |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 921 | } |
| 922 | return 1; |
| 923 | } |
| 924 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 925 | // kCustomExtensionValue is the extension value that the custom extension |
| 926 | // callbacks will add. |
Adam Langley | c5b23a1 | 2015-07-30 18:19:26 -0700 | [diff] [blame] | 927 | static const uint16_t kCustomExtensionValue = 1234; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 928 | static void *const kCustomExtensionAddArg = |
| 929 | reinterpret_cast<void *>(kCustomExtensionValue); |
| 930 | static void *const kCustomExtensionParseArg = |
| 931 | reinterpret_cast<void *>(kCustomExtensionValue + 1); |
| 932 | static const char kCustomExtensionContents[] = "custom extension"; |
| 933 | |
| 934 | static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value, |
| 935 | const uint8_t **out, size_t *out_len, |
| 936 | int *out_alert_value, void *add_arg) { |
| 937 | if (extension_value != kCustomExtensionValue || |
| 938 | add_arg != kCustomExtensionAddArg) { |
| 939 | abort(); |
| 940 | } |
| 941 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 942 | if (GetTestConfig(ssl)->custom_extension_skip) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 943 | return 0; |
| 944 | } |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 945 | if (GetTestConfig(ssl)->custom_extension_fail_add) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents); |
| 950 | *out_len = sizeof(kCustomExtensionContents) - 1; |
| 951 | |
| 952 | return 1; |
| 953 | } |
| 954 | |
| 955 | static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value, |
| 956 | const uint8_t *out, void *add_arg) { |
| 957 | if (extension_value != kCustomExtensionValue || |
| 958 | add_arg != kCustomExtensionAddArg || |
| 959 | out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) { |
| 960 | abort(); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value, |
| 965 | const uint8_t *contents, |
| 966 | size_t contents_len, |
| 967 | int *out_alert_value, void *parse_arg) { |
| 968 | if (extension_value != kCustomExtensionValue || |
| 969 | parse_arg != kCustomExtensionParseArg) { |
| 970 | abort(); |
| 971 | } |
| 972 | |
| 973 | if (contents_len != sizeof(kCustomExtensionContents) - 1 || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 974 | OPENSSL_memcmp(contents, kCustomExtensionContents, contents_len) != 0) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 975 | *out_alert_value = SSL_AD_DECODE_ERROR; |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | return 1; |
| 980 | } |
| 981 | |
David Benjamin | 8b17671 | 2016-10-27 21:51:24 -0400 | [diff] [blame] | 982 | static int ServerNameCallback(SSL *ssl, int *out_alert, void *arg) { |
| 983 | // SNI must be accessible from the SNI callback. |
| 984 | const TestConfig *config = GetTestConfig(ssl); |
| 985 | const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 986 | if (server_name == nullptr || |
| 987 | std::string(server_name) != config->expected_server_name) { |
| 988 | fprintf(stderr, "servername mismatch (got %s; want %s)\n", server_name, |
| 989 | config->expected_server_name.c_str()); |
| 990 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 991 | } |
| 992 | |
| 993 | return SSL_TLSEXT_ERR_OK; |
| 994 | } |
| 995 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 996 | // Connect returns a new socket connected to localhost on |port| or -1 on |
| 997 | // error. |
| 998 | static int Connect(uint16_t port) { |
David Benjamin | c386440 | 2017-07-14 16:48:36 -0400 | [diff] [blame] | 999 | time_t start_time = time(nullptr); |
Matthew Braithwaite | 2d04cf0 | 2017-05-19 18:13:25 -0700 | [diff] [blame] | 1000 | for (int af : { AF_INET6, AF_INET }) { |
| 1001 | int sock = socket(af, SOCK_STREAM, 0); |
| 1002 | if (sock == -1) { |
| 1003 | PrintSocketError("socket"); |
| 1004 | return -1; |
| 1005 | } |
| 1006 | int nodelay = 1; |
| 1007 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 1008 | reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) { |
| 1009 | PrintSocketError("setsockopt"); |
| 1010 | closesocket(sock); |
| 1011 | return -1; |
| 1012 | } |
| 1013 | |
| 1014 | sockaddr_storage ss; |
| 1015 | OPENSSL_memset(&ss, 0, sizeof(ss)); |
| 1016 | ss.ss_family = af; |
| 1017 | socklen_t len = 0; |
| 1018 | |
| 1019 | if (af == AF_INET6) { |
| 1020 | sockaddr_in6 *sin6 = (sockaddr_in6 *) &ss; |
| 1021 | len = sizeof(*sin6); |
| 1022 | sin6->sin6_port = htons(port); |
| 1023 | if (!inet_pton(AF_INET6, "::1", &sin6->sin6_addr)) { |
| 1024 | PrintSocketError("inet_pton"); |
| 1025 | closesocket(sock); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | } else if (af == AF_INET) { |
| 1029 | sockaddr_in *sin = (sockaddr_in *) &ss; |
| 1030 | len = sizeof(*sin); |
| 1031 | sin->sin_port = htons(port); |
| 1032 | if (!inet_pton(AF_INET, "127.0.0.1", &sin->sin_addr)) { |
| 1033 | PrintSocketError("inet_pton"); |
| 1034 | closesocket(sock); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | if (connect(sock, reinterpret_cast<const sockaddr*>(&ss), len) == 0) { |
| 1040 | return sock; |
| 1041 | } |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1042 | closesocket(sock); |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1043 | } |
David Benjamin | c386440 | 2017-07-14 16:48:36 -0400 | [diff] [blame] | 1044 | |
David Benjamin | 8a5dcbc | 2017-07-13 18:43:51 -0400 | [diff] [blame] | 1045 | PrintSocketError("connect"); |
David Benjamin | c386440 | 2017-07-14 16:48:36 -0400 | [diff] [blame] | 1046 | // TODO(davidben): Remove this logging when https://crbug.com/boringssl/199 is |
| 1047 | // resolved. |
| 1048 | fprintf(stderr, "start_time = %lld, end_time = %lld\n", |
| 1049 | static_cast<long long>(start_time), |
| 1050 | static_cast<long long>(time(nullptr))); |
Matthew Braithwaite | 2d04cf0 | 2017-05-19 18:13:25 -0700 | [diff] [blame] | 1051 | return -1; |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | class SocketCloser { |
| 1055 | public: |
| 1056 | explicit SocketCloser(int sock) : sock_(sock) {} |
| 1057 | ~SocketCloser() { |
| 1058 | // Half-close and drain the socket before releasing it. This seems to be |
| 1059 | // necessary for graceful shutdown on Windows. It will also avoid write |
| 1060 | // failures in the test runner. |
| 1061 | #if defined(OPENSSL_WINDOWS) |
| 1062 | shutdown(sock_, SD_SEND); |
| 1063 | #else |
| 1064 | shutdown(sock_, SHUT_WR); |
| 1065 | #endif |
| 1066 | while (true) { |
| 1067 | char buf[1024]; |
| 1068 | if (recv(sock_, buf, sizeof(buf), 0) <= 0) { |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | closesocket(sock_); |
| 1073 | } |
| 1074 | |
| 1075 | private: |
| 1076 | const int sock_; |
| 1077 | }; |
| 1078 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1079 | static void ssl_ctx_add_session(SSL_SESSION *session, void *void_param) { |
| 1080 | SSL_SESSION *new_session = SSL_SESSION_dup( |
| 1081 | session, SSL_SESSION_INCLUDE_NONAUTH | SSL_SESSION_INCLUDE_TICKET); |
| 1082 | if (new_session != nullptr) { |
| 1083 | SSL_CTX_add_session((SSL_CTX *)void_param, new_session); |
| 1084 | } |
| 1085 | SSL_SESSION_free(new_session); |
| 1086 | } |
| 1087 | |
| 1088 | static bssl::UniquePtr<SSL_CTX> SetupCtx(SSL_CTX *old_ctx, |
| 1089 | const TestConfig *config) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1090 | bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new( |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1091 | config->is_dtls ? DTLS_method() : TLS_method())); |
| 1092 | if (!ssl_ctx) { |
| 1093 | return nullptr; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 1096 | SSL_CTX_set0_buffer_pool(ssl_ctx.get(), g_pool); |
| 1097 | |
David Benjamin | 3cfeb95 | 2017-03-01 16:48:38 -0500 | [diff] [blame] | 1098 | // Enable SSL 3.0 and TLS 1.3 for tests. |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1099 | if (!config->is_dtls && |
David Benjamin | 3cfeb95 | 2017-03-01 16:48:38 -0500 | [diff] [blame] | 1100 | (!SSL_CTX_set_min_proto_version(ssl_ctx.get(), SSL3_VERSION) || |
| 1101 | !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION))) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1102 | return nullptr; |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 1105 | std::string cipher_list = "ALL"; |
| 1106 | if (!config->cipher.empty()) { |
| 1107 | cipher_list = config->cipher; |
| 1108 | SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 1109 | } |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 1110 | if (!SSL_CTX_set_strict_cipher_list(ssl_ctx.get(), cipher_list.c_str())) { |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 1111 | return nullptr; |
| 1112 | } |
| 1113 | |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1114 | if (config->async && config->is_server) { |
| 1115 | // Disable the internal session cache. To test asynchronous session lookup, |
| 1116 | // we use an external session cache. |
| 1117 | SSL_CTX_set_session_cache_mode( |
| 1118 | ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL); |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1119 | SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback); |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1120 | } else { |
| 1121 | SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 1122 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1123 | |
David Benjamin | d4c2bce | 2015-10-17 12:28:18 -0400 | [diff] [blame] | 1124 | SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 1125 | |
David Benjamin | acb6dcc | 2016-03-10 09:15:01 -0500 | [diff] [blame] | 1126 | if (config->use_old_client_cert_callback) { |
| 1127 | SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback); |
| 1128 | } |
| 1129 | |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 1130 | SSL_CTX_set_next_protos_advertised_cb( |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1131 | ssl_ctx.get(), NextProtosAdvertisedCallback, NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 1132 | if (!config->select_next_proto.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1133 | SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback, |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1134 | NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1137 | if (!config->select_alpn.empty() || config->decline_alpn) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1138 | SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL); |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 1139 | } |
David Benjamin | 1f5f62b | 2014-07-12 16:18:02 -0400 | [diff] [blame] | 1140 | |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1141 | SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1142 | |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 1143 | SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback); |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 1144 | |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 1145 | SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1146 | SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback); |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 1147 | |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 1148 | if (config->use_ticket_callback) { |
| 1149 | SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback); |
| 1150 | } |
| 1151 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 1152 | if (config->enable_client_custom_extension && |
| 1153 | !SSL_CTX_add_client_custom_ext( |
| 1154 | ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback, |
| 1155 | CustomExtensionFreeCallback, kCustomExtensionAddArg, |
| 1156 | CustomExtensionParseCallback, kCustomExtensionParseArg)) { |
| 1157 | return nullptr; |
| 1158 | } |
| 1159 | |
| 1160 | if (config->enable_server_custom_extension && |
| 1161 | !SSL_CTX_add_server_custom_ext( |
| 1162 | ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback, |
| 1163 | CustomExtensionFreeCallback, kCustomExtensionAddArg, |
| 1164 | CustomExtensionParseCallback, kCustomExtensionParseArg)) { |
| 1165 | return nullptr; |
| 1166 | } |
| 1167 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 1168 | if (!config->use_custom_verify_callback) { |
| 1169 | SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), CertVerifyCallback, NULL); |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1172 | if (!config->signed_cert_timestamps.empty() && |
| 1173 | !SSL_CTX_set_signed_cert_timestamp_list( |
| 1174 | ssl_ctx.get(), (const uint8_t *)config->signed_cert_timestamps.data(), |
| 1175 | config->signed_cert_timestamps.size())) { |
| 1176 | return nullptr; |
| 1177 | } |
| 1178 | |
Adam Langley | 2ff7933 | 2017-02-28 13:45:39 -0800 | [diff] [blame] | 1179 | if (!config->use_client_ca_list.empty()) { |
| 1180 | if (config->use_client_ca_list == "<NULL>") { |
| 1181 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr); |
| 1182 | } else { |
| 1183 | bssl::UniquePtr<STACK_OF(X509_NAME)> names = |
| 1184 | DecodeHexX509Names(config->use_client_ca_list); |
| 1185 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), names.release()); |
| 1186 | } |
David Benjamin | 2f8935d | 2016-07-13 19:47:39 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 1189 | if (config->enable_grease) { |
| 1190 | SSL_CTX_set_grease_enabled(ssl_ctx.get(), 1); |
| 1191 | } |
| 1192 | |
David Benjamin | 8b17671 | 2016-10-27 21:51:24 -0400 | [diff] [blame] | 1193 | if (!config->expected_server_name.empty()) { |
| 1194 | SSL_CTX_set_tlsext_servername_callback(ssl_ctx.get(), ServerNameCallback); |
| 1195 | } |
| 1196 | |
David Benjamin | 4199b0d | 2016-11-01 13:58:25 -0400 | [diff] [blame] | 1197 | if (!config->ticket_key.empty() && |
| 1198 | !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), config->ticket_key.data(), |
| 1199 | config->ticket_key.size())) { |
| 1200 | return nullptr; |
| 1201 | } |
| 1202 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1203 | if (config->enable_early_data) { |
| 1204 | SSL_CTX_set_early_data_enabled(ssl_ctx.get(), 1); |
| 1205 | } |
| 1206 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 1207 | SSL_CTX_set_tls13_variant( |
| 1208 | ssl_ctx.get(), static_cast<enum tls13_variant_t>(config->tls13_variant)); |
| 1209 | |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 1210 | if (config->allow_unknown_alpn_protos) { |
| 1211 | SSL_CTX_set_allow_unknown_alpn_protos(ssl_ctx.get(), 1); |
| 1212 | } |
| 1213 | |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 1214 | if (config->enable_ed25519) { |
| 1215 | SSL_CTX_set_ed25519_enabled(ssl_ctx.get(), 1); |
| 1216 | } |
| 1217 | |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 1218 | if (!config->verify_prefs.empty()) { |
| 1219 | std::vector<uint16_t> u16s(config->verify_prefs.begin(), |
| 1220 | config->verify_prefs.end()); |
| 1221 | if (!SSL_CTX_set_verify_algorithm_prefs(ssl_ctx.get(), u16s.data(), |
| 1222 | u16s.size())) { |
| 1223 | return nullptr; |
| 1224 | } |
| 1225 | } |
| 1226 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1227 | if (old_ctx) { |
| 1228 | uint8_t keys[48]; |
| 1229 | if (!SSL_CTX_get_tlsext_ticket_keys(old_ctx, &keys, sizeof(keys)) || |
| 1230 | !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), keys, sizeof(keys))) { |
| 1231 | return nullptr; |
| 1232 | } |
| 1233 | lh_SSL_SESSION_doall_arg(old_ctx->sessions, ssl_ctx_add_session, |
| 1234 | ssl_ctx.get()); |
| 1235 | } |
| 1236 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1237 | return ssl_ctx; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1238 | } |
| 1239 | |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1240 | // RetryAsync is called after a failed operation on |ssl| with return code |
| 1241 | // |ret|. If the operation should be retried, it simulates one asynchronous |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1242 | // event and returns true. Otherwise it returns false. |
| 1243 | static bool RetryAsync(SSL *ssl, int ret) { |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1244 | // No error; don't retry. |
| 1245 | if (ret >= 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1246 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1247 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1248 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1249 | TestState *test_state = GetTestState(ssl); |
Matt Braithwaite | 6278e24 | 2016-06-14 08:18:22 -0700 | [diff] [blame] | 1250 | assert(GetTestConfig(ssl)->async); |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1251 | |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 1252 | if (test_state->packeted_bio != nullptr && |
| 1253 | PacketedBioAdvanceClock(test_state->packeted_bio)) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1254 | // The DTLS retransmit logic silently ignores write failures. So the test |
| 1255 | // may progress, allow writes through synchronously. |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 1256 | AsyncBioEnforceWriteQuota(test_state->async_bio, false); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1257 | int timeout_ret = DTLSv1_handle_timeout(ssl); |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 1258 | AsyncBioEnforceWriteQuota(test_state->async_bio, true); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1259 | |
| 1260 | if (timeout_ret < 0) { |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1261 | fprintf(stderr, "Error retransmitting.\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1262 | return false; |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1263 | } |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1264 | return true; |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1265 | } |
| 1266 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1267 | // See if we needed to read or write more. If so, allow one byte through on |
| 1268 | // the appropriate end to maximally stress the state machine. |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1269 | switch (SSL_get_error(ssl, ret)) { |
| 1270 | case SSL_ERROR_WANT_READ: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1271 | AsyncBioAllowRead(test_state->async_bio, 1); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1272 | return true; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1273 | case SSL_ERROR_WANT_WRITE: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1274 | AsyncBioAllowWrite(test_state->async_bio, 1); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1275 | return true; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1276 | case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1277 | bssl::UniquePtr<EVP_PKEY> pkey = |
| 1278 | LoadPrivateKey(GetTestConfig(ssl)->send_channel_id); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1279 | if (!pkey) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1280 | return false; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1281 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1282 | test_state->channel_id = std::move(pkey); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1283 | return true; |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1284 | } |
David Benjamin | 41fdbcd | 2015-02-09 03:13:35 -0500 | [diff] [blame] | 1285 | case SSL_ERROR_WANT_X509_LOOKUP: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1286 | test_state->cert_ready = true; |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1287 | return true; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1288 | case SSL_ERROR_PENDING_SESSION: |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1289 | test_state->session = std::move(test_state->pending_session); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1290 | return true; |
David Benjamin | 6f5c0f4 | 2015-02-24 01:23:21 -0500 | [diff] [blame] | 1291 | case SSL_ERROR_PENDING_CERTIFICATE: |
Alessandro Ghedini | 958346a | 2016-12-20 19:42:15 +0000 | [diff] [blame] | 1292 | test_state->early_callback_ready = true; |
| 1293 | return true; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1294 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1295 | test_state->private_key_retries++; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1296 | return true; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 1297 | case SSL_ERROR_WANT_CERTIFICATE_VERIFY: |
| 1298 | test_state->custom_verify_ready = true; |
| 1299 | return true; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1300 | default: |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1301 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1302 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1303 | } |
| 1304 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1305 | // DoRead reads from |ssl|, resolving any asynchronous operations. It returns |
| 1306 | // the result value of the final |SSL_read| call. |
| 1307 | static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1308 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1309 | TestState *test_state = GetTestState(ssl); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1310 | int ret; |
| 1311 | do { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1312 | if (config->async) { |
| 1313 | // The DTLS retransmit logic silently ignores write failures. So the test |
| 1314 | // may progress, allow writes through synchronously. |SSL_read| may |
| 1315 | // trigger a retransmit, so disconnect the write quota. |
| 1316 | AsyncBioEnforceWriteQuota(test_state->async_bio, false); |
| 1317 | } |
David Benjamin | f3fbade | 2016-09-19 13:08:16 -0400 | [diff] [blame] | 1318 | ret = config->peek_then_read ? SSL_peek(ssl, out, max_out) |
| 1319 | : SSL_read(ssl, out, max_out); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 1320 | if (config->async) { |
| 1321 | AsyncBioEnforceWriteQuota(test_state->async_bio, true); |
| 1322 | } |
David Benjamin | 7bb1d29 | 2016-11-01 19:45:06 -0400 | [diff] [blame] | 1323 | |
| 1324 | // Run the exporter after each read. This is to test that the exporter fails |
| 1325 | // during a renegotiation. |
| 1326 | if (config->use_exporter_between_reads) { |
| 1327 | uint8_t buf; |
| 1328 | if (!SSL_export_keying_material(ssl, &buf, 1, NULL, 0, NULL, 0, 0)) { |
| 1329 | fprintf(stderr, "failed to export keying material\n"); |
| 1330 | return -1; |
| 1331 | } |
| 1332 | } |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1333 | } while (config->async && RetryAsync(ssl, ret)); |
David Benjamin | f3fbade | 2016-09-19 13:08:16 -0400 | [diff] [blame] | 1334 | |
| 1335 | if (config->peek_then_read && ret > 0) { |
| 1336 | std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]); |
| 1337 | |
| 1338 | // SSL_peek should synchronously return the same data. |
| 1339 | int ret2 = SSL_peek(ssl, buf.get(), ret); |
| 1340 | if (ret2 != ret || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1341 | OPENSSL_memcmp(buf.get(), out, ret) != 0) { |
David Benjamin | f3fbade | 2016-09-19 13:08:16 -0400 | [diff] [blame] | 1342 | fprintf(stderr, "First and second SSL_peek did not match.\n"); |
| 1343 | return -1; |
| 1344 | } |
| 1345 | |
| 1346 | // SSL_read should synchronously return the same data and consume it. |
| 1347 | ret2 = SSL_read(ssl, buf.get(), ret); |
| 1348 | if (ret2 != ret || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1349 | OPENSSL_memcmp(buf.get(), out, ret) != 0) { |
David Benjamin | f3fbade | 2016-09-19 13:08:16 -0400 | [diff] [blame] | 1350 | fprintf(stderr, "SSL_peek and SSL_read did not match.\n"); |
| 1351 | return -1; |
| 1352 | } |
| 1353 | } |
| 1354 | |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1355 | return ret; |
| 1356 | } |
| 1357 | |
| 1358 | // WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous |
| 1359 | // operations. It returns the result of the final |SSL_write| call. |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 1360 | static int WriteAll(SSL *ssl, const void *in_, size_t in_len) { |
| 1361 | const uint8_t *in = reinterpret_cast<const uint8_t *>(in_); |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1362 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1363 | int ret; |
| 1364 | do { |
| 1365 | ret = SSL_write(ssl, in, in_len); |
| 1366 | if (ret > 0) { |
| 1367 | in += ret; |
| 1368 | in_len -= ret; |
| 1369 | } |
| 1370 | } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0)); |
| 1371 | return ret; |
| 1372 | } |
| 1373 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1374 | // DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It |
| 1375 | // returns the result of the final |SSL_shutdown| call. |
| 1376 | static int DoShutdown(SSL *ssl) { |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1377 | const TestConfig *config = GetTestConfig(ssl); |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1378 | int ret; |
| 1379 | do { |
| 1380 | ret = SSL_shutdown(ssl); |
| 1381 | } while (config->async && RetryAsync(ssl, ret)); |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1385 | // DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous |
| 1386 | // operations. It returns the result of the final |SSL_send_fatal_alert| call. |
| 1387 | static int DoSendFatalAlert(SSL *ssl, uint8_t alert) { |
| 1388 | const TestConfig *config = GetTestConfig(ssl); |
| 1389 | int ret; |
| 1390 | do { |
| 1391 | ret = SSL_send_fatal_alert(ssl, alert); |
| 1392 | } while (config->async && RetryAsync(ssl, ret)); |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1396 | static uint16_t GetProtocolVersion(const SSL *ssl) { |
| 1397 | uint16_t version = SSL_version(ssl); |
| 1398 | if (!SSL_is_dtls(ssl)) { |
| 1399 | return version; |
| 1400 | } |
| 1401 | return 0x0201 + ~version; |
| 1402 | } |
| 1403 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1404 | // CheckHandshakeProperties checks, immediately after |ssl| completes its |
| 1405 | // initial handshake (or False Starts), whether all the properties are |
| 1406 | // consistent with the test configuration and invariants. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1407 | static bool CheckHandshakeProperties(SSL *ssl, bool is_resume, |
| 1408 | const TestConfig *config) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1409 | if (SSL_get_current_cipher(ssl) == nullptr) { |
| 1410 | fprintf(stderr, "null cipher after handshake\n"); |
| 1411 | return false; |
| 1412 | } |
| 1413 | |
David Benjamin | d9cbb53 | 2017-07-07 13:17:19 -0400 | [diff] [blame] | 1414 | if (config->expect_version != 0 && |
| 1415 | SSL_version(ssl) != config->expect_version) { |
| 1416 | fprintf(stderr, "want version %04x, got %04x\n", config->expect_version, |
| 1417 | SSL_version(ssl)); |
| 1418 | return false; |
| 1419 | } |
| 1420 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1421 | bool expect_resume = |
| 1422 | is_resume && (!config->expect_session_miss || SSL_in_early_data(ssl)); |
| 1423 | if (!!SSL_session_reused(ssl) != expect_resume) { |
| 1424 | fprintf(stderr, "session unexpectedly was%s reused\n", |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1425 | SSL_session_reused(ssl) ? "" : " not"); |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 1429 | bool expect_handshake_done = |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1430 | (is_resume || !config->false_start) && !SSL_in_early_data(ssl); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1431 | if (expect_handshake_done != GetTestState(ssl)->handshake_done) { |
| 1432 | fprintf(stderr, "handshake was%s completed\n", |
| 1433 | GetTestState(ssl)->handshake_done ? "" : " not"); |
| 1434 | return false; |
| 1435 | } |
| 1436 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1437 | if (expect_handshake_done && !config->is_server) { |
| 1438 | bool expect_new_session = |
| 1439 | !config->expect_no_session && |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1440 | (!SSL_session_reused(ssl) || config->expect_ticket_renewal) && |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1441 | // Session tickets are sent post-handshake in TLS 1.3. |
| 1442 | GetProtocolVersion(ssl) < TLS1_3_VERSION; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1443 | if (expect_new_session != GetTestState(ssl)->got_new_session) { |
| 1444 | fprintf(stderr, |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1445 | "new session was%s cached, but we expected the opposite\n", |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 1446 | GetTestState(ssl)->got_new_session ? "" : " not"); |
| 1447 | return false; |
| 1448 | } |
| 1449 | } |
| 1450 | |
David Benjamin | b5c58db | 2017-01-28 01:39:29 -0500 | [diff] [blame] | 1451 | if (!is_resume) { |
| 1452 | if (config->expect_session_id && !GetTestState(ssl)->got_new_session) { |
| 1453 | fprintf(stderr, "session was not cached on the server.\n"); |
| 1454 | return false; |
| 1455 | } |
| 1456 | if (config->expect_no_session_id && GetTestState(ssl)->got_new_session) { |
| 1457 | fprintf(stderr, "session was unexpectedly cached on the server.\n"); |
| 1458 | return false; |
| 1459 | } |
| 1460 | } |
| 1461 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1462 | if (config->is_server && !GetTestState(ssl)->early_callback_called) { |
| 1463 | fprintf(stderr, "early callback not called\n"); |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
| 1467 | if (!config->expected_server_name.empty()) { |
| 1468 | const char *server_name = |
| 1469 | SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
David Benjamin | 8b17671 | 2016-10-27 21:51:24 -0400 | [diff] [blame] | 1470 | if (server_name == nullptr || |
| 1471 | server_name != config->expected_server_name) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1472 | fprintf(stderr, "servername mismatch (got %s; want %s)\n", |
| 1473 | server_name, config->expected_server_name.c_str()); |
| 1474 | return false; |
| 1475 | } |
| 1476 | } |
| 1477 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1478 | if (!config->expected_next_proto.empty()) { |
| 1479 | const uint8_t *next_proto; |
| 1480 | unsigned next_proto_len; |
| 1481 | SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
| 1482 | if (next_proto_len != config->expected_next_proto.size() || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1483 | OPENSSL_memcmp(next_proto, config->expected_next_proto.data(), |
| 1484 | next_proto_len) != 0) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1485 | fprintf(stderr, "negotiated next proto mismatch\n"); |
| 1486 | return false; |
| 1487 | } |
| 1488 | } |
| 1489 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1490 | if (!config->is_server) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1491 | const uint8_t *alpn_proto; |
| 1492 | unsigned alpn_proto_len; |
| 1493 | SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len); |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1494 | if (alpn_proto_len != config->expected_alpn.size() || |
| 1495 | OPENSSL_memcmp(alpn_proto, config->expected_alpn.data(), |
| 1496 | alpn_proto_len) != 0) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1497 | fprintf(stderr, "negotiated alpn proto mismatch\n"); |
| 1498 | return false; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | if (!config->expected_channel_id.empty()) { |
| 1503 | uint8_t channel_id[64]; |
| 1504 | if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) { |
| 1505 | fprintf(stderr, "no channel id negotiated\n"); |
| 1506 | return false; |
| 1507 | } |
| 1508 | if (config->expected_channel_id.size() != 64 || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1509 | OPENSSL_memcmp(config->expected_channel_id.data(), channel_id, 64) != |
| 1510 | 0) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1511 | fprintf(stderr, "channel id mismatch\n"); |
| 1512 | return false; |
| 1513 | } |
| 1514 | } |
| 1515 | |
David Benjamin | d261004 | 2017-01-03 10:49:28 -0500 | [diff] [blame] | 1516 | if (config->expect_extended_master_secret && !SSL_get_extms_support(ssl)) { |
| 1517 | fprintf(stderr, "No EMS for connection when expected\n"); |
| 1518 | return false; |
| 1519 | } |
| 1520 | |
| 1521 | if (config->expect_secure_renegotiation && |
| 1522 | !SSL_get_secure_renegotiation_support(ssl)) { |
| 1523 | fprintf(stderr, "No secure renegotiation for connection when expected\n"); |
| 1524 | return false; |
| 1525 | } |
| 1526 | |
| 1527 | if (config->expect_no_secure_renegotiation && |
| 1528 | SSL_get_secure_renegotiation_support(ssl)) { |
| 1529 | fprintf(stderr, |
| 1530 | "Secure renegotiation unexpectedly negotiated for connection\n"); |
| 1531 | return false; |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | if (!config->expected_ocsp_response.empty()) { |
| 1535 | const uint8_t *data; |
| 1536 | size_t len; |
| 1537 | SSL_get0_ocsp_response(ssl, &data, &len); |
| 1538 | if (config->expected_ocsp_response.size() != len || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1539 | OPENSSL_memcmp(config->expected_ocsp_response.data(), data, len) != 0) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1540 | fprintf(stderr, "OCSP response mismatch\n"); |
| 1541 | return false; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | if (!config->expected_signed_cert_timestamps.empty()) { |
| 1546 | const uint8_t *data; |
| 1547 | size_t len; |
| 1548 | SSL_get0_signed_cert_timestamp_list(ssl, &data, &len); |
| 1549 | if (config->expected_signed_cert_timestamps.size() != len || |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1550 | OPENSSL_memcmp(config->expected_signed_cert_timestamps.data(), data, |
| 1551 | len) != 0) { |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1552 | fprintf(stderr, "SCT list mismatch\n"); |
| 1553 | return false; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1557 | if (config->expect_verify_result) { |
| 1558 | int expected_verify_result = config->verify_fail ? |
| 1559 | X509_V_ERR_APPLICATION_VERIFICATION : |
| 1560 | X509_V_OK; |
| 1561 | |
| 1562 | if (SSL_get_verify_result(ssl) != expected_verify_result) { |
| 1563 | fprintf(stderr, "Wrong certificate verification result\n"); |
| 1564 | return false; |
| 1565 | } |
| 1566 | } |
| 1567 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1568 | if (config->expect_peer_signature_algorithm != 0 && |
| 1569 | config->expect_peer_signature_algorithm != |
| 1570 | SSL_get_peer_signature_algorithm(ssl)) { |
| 1571 | fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n", |
| 1572 | SSL_get_peer_signature_algorithm(ssl), |
| 1573 | config->expect_peer_signature_algorithm); |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1574 | return false; |
| 1575 | } |
| 1576 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1577 | if (config->expect_curve_id != 0) { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1578 | uint16_t curve_id = SSL_get_curve_id(ssl); |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1579 | if (static_cast<uint16_t>(config->expect_curve_id) != curve_id) { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1580 | fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id, |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 1581 | static_cast<uint16_t>(config->expect_curve_id)); |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1582 | return false; |
| 1583 | } |
| 1584 | } |
| 1585 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 1586 | uint16_t cipher_id = |
| 1587 | static_cast<uint16_t>(SSL_CIPHER_get_id(SSL_get_current_cipher(ssl))); |
| 1588 | if (config->expect_cipher_aes != 0 && |
| 1589 | EVP_has_aes_hardware() && |
| 1590 | static_cast<uint16_t>(config->expect_cipher_aes) != cipher_id) { |
| 1591 | fprintf(stderr, "Cipher ID was %04x, wanted %04x (has AES hardware)\n", |
| 1592 | cipher_id, static_cast<uint16_t>(config->expect_cipher_aes)); |
| 1593 | return false; |
| 1594 | } |
| 1595 | |
| 1596 | if (config->expect_cipher_no_aes != 0 && |
| 1597 | !EVP_has_aes_hardware() && |
| 1598 | static_cast<uint16_t>(config->expect_cipher_no_aes) != cipher_id) { |
| 1599 | fprintf(stderr, "Cipher ID was %04x, wanted %04x (no AES hardware)\n", |
| 1600 | cipher_id, static_cast<uint16_t>(config->expect_cipher_no_aes)); |
| 1601 | return false; |
| 1602 | } |
| 1603 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1604 | if (is_resume && !SSL_in_early_data(ssl)) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1605 | if ((config->expect_accept_early_data && !SSL_early_data_accepted(ssl)) || |
| 1606 | (config->expect_reject_early_data && SSL_early_data_accepted(ssl))) { |
| 1607 | fprintf(stderr, |
| 1608 | "Early data was%s accepted, but we expected the opposite\n", |
| 1609 | SSL_early_data_accepted(ssl) ? "" : " not"); |
| 1610 | return false; |
| 1611 | } |
| 1612 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 1613 | |
David Benjamin | bb9e36e | 2016-08-03 14:14:47 -0400 | [diff] [blame] | 1614 | if (!config->psk.empty()) { |
| 1615 | if (SSL_get_peer_cert_chain(ssl) != nullptr) { |
| 1616 | fprintf(stderr, "Received peer certificate on a PSK cipher.\n"); |
| 1617 | return false; |
| 1618 | } |
| 1619 | } else if (!config->is_server || config->require_any_client_certificate) { |
| 1620 | if (SSL_get_peer_cert_chain(ssl) == nullptr) { |
| 1621 | fprintf(stderr, "Received no peer certificate but expected one.\n"); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1622 | return false; |
| 1623 | } |
| 1624 | } |
David Benjamin | bb9e36e | 2016-08-03 14:14:47 -0400 | [diff] [blame] | 1625 | |
David Benjamin | 2c51645 | 2016-11-15 10:16:54 +0900 | [diff] [blame] | 1626 | if (!config->expect_peer_cert_file.empty()) { |
| 1627 | bssl::UniquePtr<X509> expect_leaf; |
| 1628 | bssl::UniquePtr<STACK_OF(X509)> expect_chain; |
| 1629 | if (!LoadCertificate(&expect_leaf, &expect_chain, |
| 1630 | config->expect_peer_cert_file)) { |
| 1631 | return false; |
| 1632 | } |
| 1633 | |
| 1634 | // For historical reasons, clients report a chain with a leaf and servers |
| 1635 | // without. |
| 1636 | if (!config->is_server) { |
| 1637 | if (!sk_X509_insert(expect_chain.get(), expect_leaf.get(), 0)) { |
| 1638 | return false; |
| 1639 | } |
| 1640 | X509_up_ref(expect_leaf.get()); // sk_X509_push takes ownership. |
| 1641 | } |
| 1642 | |
| 1643 | bssl::UniquePtr<X509> leaf(SSL_get_peer_certificate(ssl)); |
| 1644 | STACK_OF(X509) *chain = SSL_get_peer_cert_chain(ssl); |
| 1645 | if (X509_cmp(leaf.get(), expect_leaf.get()) != 0) { |
| 1646 | fprintf(stderr, "Received a different leaf certificate than expected.\n"); |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
| 1650 | if (sk_X509_num(chain) != sk_X509_num(expect_chain.get())) { |
| 1651 | fprintf(stderr, "Received a chain of length %zu instead of %zu.\n", |
| 1652 | sk_X509_num(chain), sk_X509_num(expect_chain.get())); |
| 1653 | return false; |
| 1654 | } |
| 1655 | |
| 1656 | for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 1657 | if (X509_cmp(sk_X509_value(chain, i), |
| 1658 | sk_X509_value(expect_chain.get(), i)) != 0) { |
| 1659 | fprintf(stderr, "Chain certificate %zu did not match.\n", |
| 1660 | i + 1); |
| 1661 | return false; |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 1666 | bool expected_sha256_client_cert = config->expect_sha256_client_cert_initial; |
| 1667 | if (is_resume) { |
| 1668 | expected_sha256_client_cert = config->expect_sha256_client_cert_resume; |
| 1669 | } |
| 1670 | |
| 1671 | if (SSL_get_session(ssl)->peer_sha256_valid != expected_sha256_client_cert) { |
| 1672 | fprintf(stderr, |
| 1673 | "Unexpected SHA-256 client cert state: expected:%d is_resume:%d.\n", |
| 1674 | expected_sha256_client_cert, is_resume); |
| 1675 | return false; |
| 1676 | } |
| 1677 | |
| 1678 | if (expected_sha256_client_cert && |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 1679 | SSL_get_session(ssl)->certs != nullptr) { |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 1680 | fprintf(stderr, "Have both client cert and SHA-256 hash: is_resume:%d.\n", |
| 1681 | is_resume); |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1685 | if (is_resume && config->expect_ticket_age_skew != 0 && |
| 1686 | SSL_get_ticket_age_skew(ssl) != config->expect_ticket_age_skew) { |
| 1687 | fprintf(stderr, "Ticket age skew was %" PRId32 ", wanted %d\n", |
| 1688 | SSL_get_ticket_age_skew(ssl), config->expect_ticket_age_skew); |
| 1689 | return false; |
| 1690 | } |
| 1691 | |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 1692 | return true; |
| 1693 | } |
| 1694 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 1695 | static bool WriteSettings(int i, const TestConfig *config, |
| 1696 | const SSL_SESSION *session) { |
| 1697 | if (config->write_settings.empty()) { |
| 1698 | return true; |
| 1699 | } |
| 1700 | |
| 1701 | // Treat write_settings as a path prefix for each connection in the run. |
| 1702 | char buf[DECIMAL_SIZE(int)]; |
| 1703 | snprintf(buf, sizeof(buf), "%d", i); |
| 1704 | std::string path = config->write_settings + buf; |
| 1705 | |
| 1706 | bssl::ScopedCBB cbb; |
| 1707 | if (!CBB_init(cbb.get(), 64)) { |
| 1708 | return false; |
| 1709 | } |
| 1710 | |
| 1711 | if (session != nullptr) { |
| 1712 | uint8_t *data; |
| 1713 | size_t len; |
| 1714 | if (!SSL_SESSION_to_bytes(session, &data, &len)) { |
| 1715 | return false; |
| 1716 | } |
| 1717 | bssl::UniquePtr<uint8_t> free_data(data); |
| 1718 | CBB child; |
| 1719 | if (!CBB_add_u16(cbb.get(), kSessionTag) || |
| 1720 | !CBB_add_u24_length_prefixed(cbb.get(), &child) || |
| 1721 | !CBB_add_bytes(&child, data, len) || |
| 1722 | !CBB_flush(cbb.get())) { |
| 1723 | return false; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | if (config->is_server && |
| 1728 | (config->require_any_client_certificate || config->verify_peer) && |
| 1729 | !CBB_add_u16(cbb.get(), kRequestClientCert)) { |
| 1730 | return false; |
| 1731 | } |
| 1732 | |
David Benjamin | 1ffb4a4 | 2017-07-10 17:47:09 -0400 | [diff] [blame] | 1733 | if (config->tls13_variant != 0 && |
| 1734 | (!CBB_add_u16(cbb.get(), kTLS13Variant) || |
| 1735 | !CBB_add_u8(cbb.get(), static_cast<uint8_t>(config->tls13_variant)))) { |
| 1736 | return false; |
| 1737 | } |
| 1738 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 1739 | uint8_t *settings; |
| 1740 | size_t settings_len; |
| 1741 | if (!CBB_add_u16(cbb.get(), kDataTag) || |
| 1742 | !CBB_finish(cbb.get(), &settings, &settings_len)) { |
| 1743 | return false; |
| 1744 | } |
| 1745 | bssl::UniquePtr<uint8_t> free_settings(settings); |
| 1746 | |
| 1747 | using ScopedFILE = std::unique_ptr<FILE, decltype(&fclose)>; |
| 1748 | ScopedFILE file(fopen(path.c_str(), "w"), fclose); |
| 1749 | if (!file) { |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | return fwrite(settings, settings_len, 1, file.get()) == 1; |
| 1754 | } |
| 1755 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1756 | static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session, SSL *ssl, |
| 1757 | const TestConfig *config, bool is_resume, bool is_retry); |
| 1758 | |
| 1759 | // DoConnection tests an SSL connection against the peer. On success, it returns |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1760 | // true and sets |*out_session| to the negotiated SSL session. If the test is a |
| 1761 | // resumption attempt, |is_resume| is true and |session| is the session from the |
| 1762 | // previous exchange. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1763 | static bool DoConnection(bssl::UniquePtr<SSL_SESSION> *out_session, |
| 1764 | SSL_CTX *ssl_ctx, const TestConfig *config, |
| 1765 | const TestConfig *retry_config, bool is_resume, |
| 1766 | SSL_SESSION *session) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1767 | bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx)); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1768 | if (!ssl) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1769 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1770 | } |
| 1771 | |
David Benjamin | 7e7a82d | 2016-05-20 20:12:42 -0400 | [diff] [blame] | 1772 | if (!SetTestConfig(ssl.get(), config) || |
David Benjamin | 2d445c0 | 2015-02-09 13:03:50 -0500 | [diff] [blame] | 1773 | !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1774 | return false; |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 1775 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1776 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1777 | GetTestState(ssl.get())->is_resume = is_resume; |
| 1778 | |
Adam Langley | 5f0efe0 | 2015-02-20 13:03:16 -0800 | [diff] [blame] | 1779 | if (config->fallback_scsv && |
| 1780 | !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) { |
| 1781 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1782 | } |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 1783 | // Install the certificate synchronously if nothing else will handle it. |
| 1784 | if (!config->use_early_callback && |
| 1785 | !config->use_old_client_cert_callback && |
| 1786 | !config->async && |
| 1787 | !InstallCertificate(ssl.get())) { |
| 1788 | return false; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1789 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1790 | if (!config->use_old_client_cert_callback) { |
| 1791 | SSL_set_cert_cb(ssl.get(), CertCallback, nullptr); |
| 1792 | } |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 1793 | int mode = SSL_VERIFY_NONE; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1794 | if (config->require_any_client_certificate) { |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 1795 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1796 | } |
| 1797 | if (config->verify_peer) { |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 1798 | mode = SSL_VERIFY_PEER; |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1799 | } |
David Benjamin | c3648fa | 2017-07-01 10:50:56 -0400 | [diff] [blame] | 1800 | if (config->verify_peer_if_no_obc) { |
| 1801 | // Set SSL_VERIFY_FAIL_IF_NO_PEER_CERT so testing whether client |
| 1802 | // certificates were requested is easy. |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame^] | 1803 | mode = SSL_VERIFY_PEER | SSL_VERIFY_PEER_IF_NO_OBC | |
| 1804 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1805 | } |
| 1806 | if (config->use_custom_verify_callback) { |
| 1807 | SSL_set_custom_verify(ssl.get(), mode, CustomVerifyCallback); |
| 1808 | } else if (mode != SSL_VERIFY_NONE) { |
| 1809 | SSL_set_verify(ssl.get(), mode, NULL); |
David Benjamin | c3648fa | 2017-07-01 10:50:56 -0400 | [diff] [blame] | 1810 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1811 | if (config->false_start) { |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 1812 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1813 | } |
| 1814 | if (config->cbc_record_splitting) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1815 | SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1816 | } |
| 1817 | if (config->partial_write) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1818 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1819 | } |
Steven Valdez | 4f94b1c | 2016-05-24 12:31:07 -0400 | [diff] [blame] | 1820 | if (config->no_tls13) { |
| 1821 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3); |
| 1822 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1823 | if (config->no_tls12) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1824 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1825 | } |
| 1826 | if (config->no_tls11) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1827 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1828 | } |
| 1829 | if (config->no_tls1) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1830 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1831 | } |
| 1832 | if (config->no_ssl3) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1833 | SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3); |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1834 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1835 | if (!config->expected_channel_id.empty() || |
| 1836 | config->enable_channel_id) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 1837 | SSL_set_tls_channel_id_enabled(ssl.get(), 1); |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1838 | } |
| 1839 | if (!config->send_channel_id.empty()) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 1840 | SSL_set_tls_channel_id_enabled(ssl.get(), 1); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1841 | if (!config->async) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1842 | // The async case will be supplied by |ChannelIdCallback|. |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1843 | bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(config->send_channel_id); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1844 | if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1845 | return false; |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 1846 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1847 | } |
David Benjamin | a08e49d | 2014-08-24 01:46:07 -0400 | [diff] [blame] | 1848 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1849 | if (!config->host_name.empty() && |
| 1850 | !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1851 | return false; |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 1852 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1853 | if (!config->advertise_alpn.empty() && |
| 1854 | SSL_set_alpn_protos(ssl.get(), |
| 1855 | (const uint8_t *)config->advertise_alpn.data(), |
| 1856 | config->advertise_alpn.size()) != 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1857 | return false; |
David Benjamin | ae2888f | 2014-09-06 12:58:58 -0400 | [diff] [blame] | 1858 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1859 | if (!config->psk.empty()) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1860 | SSL_set_psk_client_callback(ssl.get(), PskClientCallback); |
| 1861 | SSL_set_psk_server_callback(ssl.get(), PskServerCallback); |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1862 | } |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1863 | if (!config->psk_identity.empty() && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1864 | !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1865 | return false; |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1866 | } |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1867 | if (!config->srtp_profiles.empty() && |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1868 | !SSL_set_srtp_profiles(ssl.get(), config->srtp_profiles.c_str())) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1869 | return false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1870 | } |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 1871 | if (config->enable_ocsp_stapling) { |
| 1872 | SSL_enable_ocsp_stapling(ssl.get()); |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1873 | } |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 1874 | if (config->enable_signed_cert_timestamps) { |
| 1875 | SSL_enable_signed_cert_timestamps(ssl.get()); |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 1876 | } |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1877 | if (config->min_version != 0 && |
David Benjamin | e470690 | 2016-09-20 15:12:23 -0400 | [diff] [blame] | 1878 | !SSL_set_min_proto_version(ssl.get(), (uint16_t)config->min_version)) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1879 | return false; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 1880 | } |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1881 | if (config->max_version != 0 && |
David Benjamin | e470690 | 2016-09-20 15:12:23 -0400 | [diff] [blame] | 1882 | !SSL_set_max_proto_version(ssl.get(), (uint16_t)config->max_version)) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1883 | return false; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 1884 | } |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 1885 | if (config->mtu != 0) { |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1886 | SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU); |
| 1887 | SSL_set_mtu(ssl.get(), config->mtu); |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 1888 | } |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 1889 | if (config->install_ddos_callback) { |
| 1890 | SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback); |
| 1891 | } |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 1892 | if (config->renegotiate_once) { |
| 1893 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once); |
| 1894 | } |
| 1895 | if (config->renegotiate_freely) { |
| 1896 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely); |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 1897 | } |
Adam Langley | 27a0d08 | 2015-11-03 13:34:10 -0800 | [diff] [blame] | 1898 | if (config->renegotiate_ignore) { |
| 1899 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore); |
| 1900 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 1901 | if (!config->check_close_notify) { |
| 1902 | SSL_set_quiet_shutdown(ssl.get(), 1); |
| 1903 | } |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 1904 | if (config->p384_only) { |
| 1905 | int nid = NID_secp384r1; |
| 1906 | if (!SSL_set1_curves(ssl.get(), &nid, 1)) { |
| 1907 | return false; |
| 1908 | } |
| 1909 | } |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 1910 | if (config->enable_all_curves) { |
| 1911 | static const int kAllCurves[] = { |
Adam Langley | 764ab98 | 2017-03-10 18:01:30 -0800 | [diff] [blame] | 1912 | NID_secp224r1, NID_X9_62_prime256v1, NID_secp384r1, |
| 1913 | NID_secp521r1, NID_X25519, |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 1914 | }; |
| 1915 | if (!SSL_set1_curves(ssl.get(), kAllCurves, |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 1916 | OPENSSL_ARRAY_SIZE(kAllCurves))) { |
David Benjamin | 8c2b3bf | 2015-12-18 20:55:44 -0500 | [diff] [blame] | 1917 | return false; |
| 1918 | } |
| 1919 | } |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 1920 | if (config->initial_timeout_duration_ms > 0) { |
| 1921 | DTLSv1_set_initial_timeout_duration(ssl.get(), |
| 1922 | config->initial_timeout_duration_ms); |
| 1923 | } |
David Benjamin | a252b34 | 2016-09-26 19:57:53 -0400 | [diff] [blame] | 1924 | if (config->max_cert_list > 0) { |
| 1925 | SSL_set_max_cert_list(ssl.get(), config->max_cert_list); |
| 1926 | } |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 1927 | if (!is_resume && config->retain_only_sha256_client_cert_initial) { |
| 1928 | SSL_set_retain_only_sha256_of_client_certs(ssl.get(), 1); |
| 1929 | } |
| 1930 | if (is_resume && config->retain_only_sha256_client_cert_resume) { |
| 1931 | SSL_set_retain_only_sha256_of_client_certs(ssl.get(), 1); |
| 1932 | } |
David Benjamin | e3fbb36 | 2017-01-06 16:19:28 -0500 | [diff] [blame] | 1933 | if (config->max_send_fragment > 0) { |
| 1934 | SSL_set_max_send_fragment(ssl.get(), config->max_send_fragment); |
| 1935 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 1936 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 1937 | int sock = Connect(config->port); |
| 1938 | if (sock == -1) { |
| 1939 | return false; |
| 1940 | } |
| 1941 | SocketCloser closer(sock); |
| 1942 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1943 | bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_NOCLOSE)); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1944 | if (!bio) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1945 | return false; |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1946 | } |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1947 | if (config->is_dtls) { |
David Benjamin | 11c8289 | 2017-02-23 20:40:31 -0500 | [diff] [blame] | 1948 | bssl::UniquePtr<BIO> packeted = PacketedBioCreate(&g_clock); |
David Benjamin | b7c5e84 | 2016-03-28 09:59:10 -0400 | [diff] [blame] | 1949 | if (!packeted) { |
| 1950 | return false; |
| 1951 | } |
David Benjamin | 585d7a4 | 2016-06-02 14:58:00 -0400 | [diff] [blame] | 1952 | GetTestState(ssl.get())->packeted_bio = packeted.get(); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1953 | BIO_push(packeted.get(), bio.release()); |
| 1954 | bio = std::move(packeted); |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 1955 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 1956 | if (config->async) { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 1957 | bssl::UniquePtr<BIO> async_scoped = |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 1958 | config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate(); |
David Benjamin | b7c5e84 | 2016-03-28 09:59:10 -0400 | [diff] [blame] | 1959 | if (!async_scoped) { |
| 1960 | return false; |
| 1961 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1962 | BIO_push(async_scoped.get(), bio.release()); |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 1963 | GetTestState(ssl.get())->async_bio = async_scoped.get(); |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1964 | bio = std::move(async_scoped); |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1965 | } |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 1966 | SSL_set_bio(ssl.get(), bio.get(), bio.get()); |
| 1967 | bio.release(); // SSL_set_bio takes ownership. |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 1968 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1969 | if (session != NULL) { |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1970 | if (!config->is_server) { |
| 1971 | if (SSL_set_session(ssl.get(), session) != 1) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 1972 | return false; |
David Benjamin | 1b8b691 | 2015-02-09 04:28:16 -0500 | [diff] [blame] | 1973 | } |
| 1974 | } else if (config->async) { |
| 1975 | // The internal session cache is disabled, so install the session |
| 1976 | // manually. |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 1977 | SSL_SESSION_up_ref(session); |
| 1978 | GetTestState(ssl.get())->pending_session.reset(session); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | |
David Benjamin | a07c0fc | 2015-05-13 13:19:42 -0400 | [diff] [blame] | 1982 | if (SSL_get_current_cipher(ssl.get()) != nullptr) { |
| 1983 | fprintf(stderr, "non-null cipher before handshake\n"); |
| 1984 | return false; |
| 1985 | } |
| 1986 | |
David Benjamin | 4784b99 | 2017-03-25 18:22:19 -0500 | [diff] [blame] | 1987 | if (config->is_server) { |
| 1988 | SSL_set_accept_state(ssl.get()); |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 1989 | } else { |
David Benjamin | 4784b99 | 2017-03-25 18:22:19 -0500 | [diff] [blame] | 1990 | SSL_set_connect_state(ssl.get()); |
| 1991 | } |
| 1992 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1993 | bool ret = DoExchange(out_session, ssl.get(), config, is_resume, false); |
| 1994 | if (!config->is_server && is_resume && config->expect_reject_early_data) { |
| 1995 | // We must have failed due to an early data rejection. |
| 1996 | if (ret) { |
| 1997 | fprintf(stderr, "0-RTT exchange unexpected succeeded.\n"); |
| 1998 | return false; |
| 1999 | } |
| 2000 | if (SSL_get_error(ssl.get(), -1) != SSL_ERROR_EARLY_DATA_REJECTED) { |
| 2001 | fprintf(stderr, |
| 2002 | "SSL_get_error did not signal SSL_ERROR_EARLY_DATA_REJECTED.\n"); |
| 2003 | return false; |
| 2004 | } |
| 2005 | |
| 2006 | // Before reseting, early state should still be available. |
| 2007 | if (!SSL_in_early_data(ssl.get()) || |
| 2008 | !CheckHandshakeProperties(ssl.get(), is_resume, config)) { |
| 2009 | fprintf(stderr, "SSL_in_early_data returned false before reset.\n"); |
| 2010 | return false; |
| 2011 | } |
| 2012 | |
| 2013 | // Reset the connection and try again at 1-RTT. |
| 2014 | SSL_reset_early_data_reject(ssl.get()); |
| 2015 | |
| 2016 | // After reseting, the socket should report it is no longer in an early data |
| 2017 | // state. |
| 2018 | if (SSL_in_early_data(ssl.get())) { |
| 2019 | fprintf(stderr, "SSL_in_early_data returned true after reset.\n"); |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
| 2023 | if (!SetTestConfig(ssl.get(), retry_config)) { |
| 2024 | return false; |
| 2025 | } |
| 2026 | |
| 2027 | ret = DoExchange(out_session, ssl.get(), retry_config, is_resume, true); |
| 2028 | } |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session, SSL *ssl, |
| 2033 | const TestConfig *config, bool is_resume, |
| 2034 | bool is_retry) { |
David Benjamin | 4784b99 | 2017-03-25 18:22:19 -0500 | [diff] [blame] | 2035 | int ret; |
| 2036 | if (!config->implicit_handshake) { |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 2037 | do { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2038 | ret = SSL_do_handshake(ssl); |
| 2039 | } while (config->async && RetryAsync(ssl, ret)); |
David Benjamin | 91eab5c | 2015-06-18 18:35:46 -0400 | [diff] [blame] | 2040 | if (ret != 1 || |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2041 | !CheckHandshakeProperties(ssl, is_resume, config)) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2042 | return false; |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 2043 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 2044 | |
David Benjamin | 8c26d75 | 2017-03-26 15:13:51 -0500 | [diff] [blame] | 2045 | if (config->handshake_twice) { |
| 2046 | do { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2047 | ret = SSL_do_handshake(ssl); |
| 2048 | } while (config->async && RetryAsync(ssl, ret)); |
David Benjamin | 8c26d75 | 2017-03-26 15:13:51 -0500 | [diff] [blame] | 2049 | if (ret != 1) { |
| 2050 | return false; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | // Skip the |config->async| logic as this should be a no-op. |
| 2055 | if (config->no_op_extra_handshake && |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2056 | SSL_do_handshake(ssl) != 1) { |
David Benjamin | 8c26d75 | 2017-03-26 15:13:51 -0500 | [diff] [blame] | 2057 | fprintf(stderr, "Extra SSL_do_handshake was not a no-op.\n"); |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 2061 | // Reset the state to assert later that the callback isn't called in |
| 2062 | // renegotations. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2063 | GetTestState(ssl)->got_new_session = false; |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 2064 | } |
| 2065 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 2066 | if (config->export_keying_material > 0) { |
| 2067 | std::vector<uint8_t> result( |
| 2068 | static_cast<size_t>(config->export_keying_material)); |
| 2069 | if (!SSL_export_keying_material( |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2070 | ssl, result.data(), result.size(), config->export_label.data(), |
| 2071 | config->export_label.size(), |
| 2072 | reinterpret_cast<const uint8_t *>(config->export_context.data()), |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 2073 | config->export_context.size(), config->use_export_context)) { |
| 2074 | fprintf(stderr, "failed to export keying material\n"); |
| 2075 | return false; |
| 2076 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2077 | if (WriteAll(ssl, result.data(), result.size()) < 0) { |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 2078 | return false; |
| 2079 | } |
| 2080 | } |
| 2081 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 2082 | if (config->tls_unique) { |
| 2083 | uint8_t tls_unique[16]; |
| 2084 | size_t tls_unique_len; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2085 | if (!SSL_get_tls_unique(ssl, tls_unique, &tls_unique_len, |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 2086 | sizeof(tls_unique))) { |
| 2087 | fprintf(stderr, "failed to get tls-unique\n"); |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | if (tls_unique_len != 12) { |
| 2092 | fprintf(stderr, "expected 12 bytes of tls-unique but got %u", |
| 2093 | static_cast<unsigned>(tls_unique_len)); |
| 2094 | return false; |
| 2095 | } |
| 2096 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2097 | if (WriteAll(ssl, tls_unique, tls_unique_len) < 0) { |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 2098 | return false; |
| 2099 | } |
| 2100 | } |
| 2101 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 2102 | if (config->send_alert) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2103 | if (DoSendFatalAlert(ssl, SSL_AD_DECOMPRESSION_FAILURE) < 0) { |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 2104 | return false; |
| 2105 | } |
| 2106 | return true; |
| 2107 | } |
| 2108 | |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 2109 | if (config->write_different_record_sizes) { |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 2110 | if (config->is_dtls) { |
| 2111 | fprintf(stderr, "write_different_record_sizes not supported for DTLS\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2112 | return false; |
David Benjamin | 6fd297b | 2014-08-11 18:43:38 -0400 | [diff] [blame] | 2113 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2114 | // This mode writes a number of different record sizes in an attempt to |
| 2115 | // trip up the CBC record splitting code. |
Adam Langley | bc94929 | 2015-06-18 21:32:44 -0700 | [diff] [blame] | 2116 | static const size_t kBufLen = 32769; |
| 2117 | std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2118 | OPENSSL_memset(buf.get(), 0x42, kBufLen); |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2119 | static const size_t kRecordSizes[] = { |
| 2120 | 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769}; |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 2121 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2122 | const size_t len = kRecordSizes[i]; |
Adam Langley | bc94929 | 2015-06-18 21:32:44 -0700 | [diff] [blame] | 2123 | if (len > kBufLen) { |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2124 | fprintf(stderr, "Bad kRecordSizes value.\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2125 | return false; |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2126 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2127 | if (WriteAll(ssl, buf.get(), len) < 0) { |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2128 | return false; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 2129 | } |
| 2130 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2131 | } else { |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 2132 | static const char kInitialWrite[] = "hello"; |
| 2133 | bool pending_initial_write = false; |
David Benjamin | a1eaba1 | 2017-01-01 23:19:22 -0500 | [diff] [blame] | 2134 | if (config->read_with_unfinished_write) { |
| 2135 | if (!config->async) { |
| 2136 | fprintf(stderr, "-read-with-unfinished-write requires -async.\n"); |
| 2137 | return false; |
| 2138 | } |
| 2139 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2140 | // Let only one byte of the record through. |
| 2141 | AsyncBioAllowWrite(GetTestState(ssl)->async_bio, 1); |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 2142 | int write_ret = |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2143 | SSL_write(ssl, kInitialWrite, strlen(kInitialWrite)); |
| 2144 | if (SSL_get_error(ssl, write_ret) != SSL_ERROR_WANT_WRITE) { |
David Benjamin | a1eaba1 | 2017-01-01 23:19:22 -0500 | [diff] [blame] | 2145 | fprintf(stderr, "Failed to leave unfinished write.\n"); |
| 2146 | return false; |
| 2147 | } |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 2148 | pending_initial_write = true; |
| 2149 | } else if (config->shim_writes_first) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2150 | if (WriteAll(ssl, kInitialWrite, strlen(kInitialWrite)) < 0) { |
David Benjamin | 6c2563e | 2015-04-03 03:47:47 -0400 | [diff] [blame] | 2151 | return false; |
| 2152 | } |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 2153 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2154 | if (!config->shim_shuts_down) { |
| 2155 | for (;;) { |
David Benjamin | 2c99d28 | 2015-09-01 10:23:00 -0400 | [diff] [blame] | 2156 | // Read only 512 bytes at a time in TLS to ensure records may be |
| 2157 | // returned in multiple reads. |
David Benjamin | e3fbb36 | 2017-01-06 16:19:28 -0500 | [diff] [blame] | 2158 | size_t read_size = config->is_dtls ? 16384 : 512; |
| 2159 | if (config->read_size > 0) { |
| 2160 | read_size = config->read_size; |
| 2161 | } |
| 2162 | std::unique_ptr<uint8_t[]> buf(new uint8_t[read_size]); |
| 2163 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2164 | int n = DoRead(ssl, buf.get(), read_size); |
| 2165 | int err = SSL_get_error(ssl, n); |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2166 | if (err == SSL_ERROR_ZERO_RETURN || |
| 2167 | (n == 0 && err == SSL_ERROR_SYSCALL)) { |
| 2168 | if (n != 0) { |
| 2169 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 2170 | return false; |
| 2171 | } |
| 2172 | // Stop on either clean or unclean shutdown. |
| 2173 | break; |
| 2174 | } else if (err != SSL_ERROR_NONE) { |
| 2175 | if (n > 0) { |
| 2176 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 2177 | return false; |
| 2178 | } |
| 2179 | return false; |
| 2180 | } |
| 2181 | // Successfully read data. |
| 2182 | if (n <= 0) { |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 2183 | fprintf(stderr, "Invalid SSL_get_error output\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2184 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 2185 | } |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2186 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2187 | if (!config->is_server && is_resume && !is_retry && |
| 2188 | config->expect_reject_early_data) { |
| 2189 | fprintf(stderr, |
| 2190 | "Unexpectedly received data instead of 0-RTT reject.\n"); |
| 2191 | return false; |
| 2192 | } |
| 2193 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2194 | // After a successful read, with or without False Start, the handshake |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 2195 | // must be complete unless we are doing early data. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2196 | if (!GetTestState(ssl)->handshake_done && |
| 2197 | !SSL_early_data_accepted(ssl)) { |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2198 | fprintf(stderr, "handshake was not completed after SSL_read\n"); |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2199 | return false; |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 2200 | } |
David Benjamin | 87e4acd | 2015-04-02 19:57:35 -0400 | [diff] [blame] | 2201 | |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 2202 | // Clear the initial write, if unfinished. |
| 2203 | if (pending_initial_write) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2204 | if (WriteAll(ssl, kInitialWrite, strlen(kInitialWrite)) < 0) { |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 2205 | return false; |
| 2206 | } |
| 2207 | pending_initial_write = false; |
| 2208 | } |
| 2209 | |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2210 | for (int i = 0; i < n; i++) { |
| 2211 | buf[i] ^= 0xff; |
| 2212 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2213 | if (WriteAll(ssl, buf.get(), n) < 0) { |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2214 | return false; |
| 2215 | } |
Kenny Root | 7fdeaf1 | 2014-08-05 15:23:37 -0700 | [diff] [blame] | 2216 | } |
| 2217 | } |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 2218 | } |
| 2219 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 2220 | if (!config->is_server && !config->false_start && |
| 2221 | !config->implicit_handshake && |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 2222 | // Session tickets are sent post-handshake in TLS 1.3. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2223 | GetProtocolVersion(ssl) < TLS1_3_VERSION && |
| 2224 | GetTestState(ssl)->got_new_session) { |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 2225 | fprintf(stderr, "new session was established after the handshake\n"); |
| 2226 | return false; |
| 2227 | } |
| 2228 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2229 | if (GetProtocolVersion(ssl) >= TLS1_3_VERSION && !config->is_server) { |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 2230 | bool expect_new_session = |
| 2231 | !config->expect_no_session && !config->shim_shuts_down; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2232 | if (expect_new_session != GetTestState(ssl)->got_new_session) { |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 2233 | fprintf(stderr, |
| 2234 | "new session was%s cached, but we expected the opposite\n", |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2235 | GetTestState(ssl)->got_new_session ? "" : " not"); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 2236 | return false; |
| 2237 | } |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 2238 | |
| 2239 | if (expect_new_session) { |
| 2240 | bool got_early_data_info = |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2241 | GetTestState(ssl)->new_session->ticket_max_early_data != 0; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 2242 | if (config->expect_early_data_info != got_early_data_info) { |
| 2243 | fprintf( |
| 2244 | stderr, |
| 2245 | "new session did%s include ticket_early_data_info, but we expected " |
| 2246 | "the opposite\n", |
| 2247 | got_early_data_info ? "" : " not"); |
| 2248 | return false; |
| 2249 | } |
| 2250 | } |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 2251 | } |
| 2252 | |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2253 | if (out_session) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2254 | *out_session = std::move(GetTestState(ssl)->new_session); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2257 | ret = DoShutdown(ssl); |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2258 | |
| 2259 | if (config->shim_shuts_down && config->check_close_notify) { |
| 2260 | // We initiate shutdown, so |SSL_shutdown| will return in two stages. First |
| 2261 | // it returns zero when our close_notify is sent, then one when the peer's |
| 2262 | // is received. |
| 2263 | if (ret != 0) { |
| 2264 | fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret); |
| 2265 | return false; |
| 2266 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2267 | ret = DoShutdown(ssl); |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | if (ret != 1) { |
| 2271 | fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret); |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2275 | if (SSL_total_renegotiations(ssl) != config->expect_total_renegotiations) { |
David Benjamin | 324dce4 | 2015-10-12 19:49:00 -0400 | [diff] [blame] | 2276 | fprintf(stderr, "Expected %d renegotiations, got %d\n", |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2277 | config->expect_total_renegotiations, SSL_total_renegotiations(ssl)); |
David Benjamin | 324dce4 | 2015-10-12 19:49:00 -0400 | [diff] [blame] | 2278 | return false; |
| 2279 | } |
| 2280 | |
David Benjamin | 40f101b | 2015-02-20 11:23:42 -0500 | [diff] [blame] | 2281 | return true; |
David Benjamin | 025b3d3 | 2014-07-01 19:53:04 -0400 | [diff] [blame] | 2282 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2283 | |
David Benjamin | ff3a149 | 2016-03-02 10:12:06 -0500 | [diff] [blame] | 2284 | class StderrDelimiter { |
| 2285 | public: |
| 2286 | ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); } |
| 2287 | }; |
| 2288 | |
David Benjamin | aac1e2d | 2016-12-06 22:35:41 -0500 | [diff] [blame] | 2289 | int main(int argc, char **argv) { |
David Benjamin | ff3a149 | 2016-03-02 10:12:06 -0500 | [diff] [blame] | 2290 | // To distinguish ASan's output from ours, add a trailing message to stderr. |
| 2291 | // Anything following this line will be considered an error. |
| 2292 | StderrDelimiter delimiter; |
| 2293 | |
David Benjamin | 87c8a64 | 2015-02-21 01:54:29 -0500 | [diff] [blame] | 2294 | #if defined(OPENSSL_WINDOWS) |
| 2295 | /* Initialize Winsock. */ |
| 2296 | WORD wsa_version = MAKEWORD(2, 2); |
| 2297 | WSADATA wsa_data; |
| 2298 | int wsa_err = WSAStartup(wsa_version, &wsa_data); |
| 2299 | if (wsa_err != 0) { |
| 2300 | fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); |
| 2301 | return 1; |
| 2302 | } |
| 2303 | if (wsa_data.wVersion != wsa_version) { |
| 2304 | fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); |
| 2305 | return 1; |
| 2306 | } |
| 2307 | #else |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2308 | signal(SIGPIPE, SIG_IGN); |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 2309 | #endif |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2310 | |
David Benjamin | 7a1eefd | 2015-10-17 23:39:22 -0400 | [diff] [blame] | 2311 | CRYPTO_library_init(); |
David Benjamin | d9e0701 | 2015-02-09 03:04:34 -0500 | [diff] [blame] | 2312 | 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] | 2313 | 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] | 2314 | if (g_config_index < 0 || g_state_index < 0) { |
David Benjamin | ae3e487 | 2014-11-18 21:52:26 -0500 | [diff] [blame] | 2315 | return 1; |
| 2316 | } |
David Benjamin | 5a593af | 2014-08-11 19:51:50 -0400 | [diff] [blame] | 2317 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2318 | TestConfig initial_config, resume_config, retry_config; |
| 2319 | if (!ParseConfig(argc - 1, argv + 1, &initial_config, &resume_config, |
| 2320 | &retry_config)) { |
David Benjamin | c273d2c | 2015-02-09 12:59:46 -0500 | [diff] [blame] | 2321 | return Usage(argv[0]); |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2322 | } |
| 2323 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 2324 | g_pool = CRYPTO_BUFFER_POOL_new(); |
| 2325 | |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 2326 | // Some code treats the zero time special, so initialize the clock to a |
| 2327 | // non-zero time. |
| 2328 | g_clock.tv_sec = 1234; |
| 2329 | g_clock.tv_usec = 1234; |
| 2330 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 2331 | bssl::UniquePtr<SSL_CTX> ssl_ctx; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2332 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 2333 | bssl::UniquePtr<SSL_SESSION> session; |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 2334 | for (int i = 0; i < initial_config.resume_count + 1; i++) { |
David Benjamin | 4666248 | 2016-08-17 00:51:00 -0400 | [diff] [blame] | 2335 | bool is_resume = i > 0; |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 2336 | TestConfig *config = is_resume ? &resume_config : &initial_config; |
| 2337 | ssl_ctx = SetupCtx(ssl_ctx.get(), config); |
| 2338 | if (!ssl_ctx) { |
| 2339 | ERR_print_errors_fp(stderr); |
| 2340 | return 1; |
| 2341 | } |
| 2342 | |
| 2343 | if (is_resume && !initial_config.is_server && !session) { |
David Benjamin | 4666248 | 2016-08-17 00:51:00 -0400 | [diff] [blame] | 2344 | fprintf(stderr, "No session to offer.\n"); |
| 2345 | return 1; |
| 2346 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2347 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 2348 | bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session); |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 2349 | if (!WriteSettings(i, config, offer_session.get())) { |
| 2350 | fprintf(stderr, "Error writing settings.\n"); |
| 2351 | return 1; |
| 2352 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2353 | if (!DoConnection(&session, ssl_ctx.get(), config, &retry_config, is_resume, |
| 2354 | offer_session.get())) { |
David Benjamin | 4666248 | 2016-08-17 00:51:00 -0400 | [diff] [blame] | 2355 | fprintf(stderr, "Connection %d failed.\n", i + 1); |
| 2356 | ERR_print_errors_fp(stderr); |
| 2357 | return 1; |
| 2358 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2359 | |
Steven Valdez | 873ebc9 | 2017-05-09 12:12:58 -0400 | [diff] [blame] | 2360 | if (config->resumption_delay != 0) { |
| 2361 | g_clock.tv_sec += config->resumption_delay; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2362 | } |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2363 | } |
| 2364 | |
David Benjamin | a7f333d | 2015-02-09 02:37:18 -0500 | [diff] [blame] | 2365 | return 0; |
David Benjamin | 1d5c83e | 2014-07-22 19:20:02 -0400 | [diff] [blame] | 2366 | } |