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