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