| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "test_config.h" |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | #include <limits.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 |
| 25 | #include <algorithm> |
| 26 | #include <functional> |
| 27 | #include <limits> |
| 28 | #include <memory> |
| 29 | #include <type_traits> |
| 30 |
| 31 | #include <openssl/aead.h> |
| 32 | #include <openssl/base64.h> |
| 33 | #include <openssl/hmac.h> |
| 34 | #include <openssl/hpke.h> |
| 35 | #include <openssl/rand.h> |
| 36 | #include <openssl/span.h> |
| 37 | #include <openssl/ssl.h> |
| 38 |
| 39 | #include "../../crypto/internal.h" |
| 40 | #include "handshake_util.h" |
| 41 | #include "mock_quic_transport.h" |
| 42 | #include "test_state.h" |
| 43 |
| 44 | #if defined(OPENSSL_WINDOWS) |
| 45 | // Windows defines struct timeval in winsock2.h. |
| 46 | #include <winsock2.h> |
| 47 | #else |
| 48 | #include <sys/time.h> |
| 49 | #endif |
| 50 |
| 51 | namespace { |
| 52 |
| 53 | template <typename Config> |
| 54 | struct Flag { |
| 55 | const char *name; |
| 56 | bool has_param; |
| 57 | // skip_handshaker, if true, causes this flag to be skipped when |
| 58 | // forwarding flags to the handshaker. This should be used with flags |
| 59 | // that only impact connecting to the runner. |
| 60 | bool skip_handshaker; |
| 61 | // If |has_param| is false, |param| will be nullptr. |
| 62 | std::function<bool(Config *config, const char *param)> set_param; |
| 63 | }; |
| 64 |
| 65 | template <typename Config> |
| 66 | Flag<Config> BoolFlag(const char *name, bool Config::*field, |
| 67 | bool skip_handshaker = false) { |
| 68 | return Flag<Config>{name, false, skip_handshaker, |
| 69 | [=](Config *config, const char *) -> bool { |
| 70 | config->*field = true; |
| 71 | return true; |
| 72 | }}; |
| 73 | } |
| 74 |
| 75 | template <typename Config> |
| 76 | Flag<Config> OptionalBoolTrueFlag(const char *name, |
| 77 | std::optional<bool> Config::*field, |
| 78 | bool skip_handshaker = false) { |
| 79 | return Flag<Config>{name, false, skip_handshaker, |
| 80 | [=](Config *config, const char *) -> bool { |
| 81 | config->*field = true; |
| 82 | return true; |
| 83 | }}; |
| 84 | } |
| 85 |
| 86 | template <typename Config> |
| 87 | Flag<Config> OptionalBoolFalseFlag(const char *name, |
| 88 | std::optional<bool> Config::*field, |
| 89 | bool skip_handshaker = false) { |
| 90 | return Flag<Config>{name, false, skip_handshaker, |
| 91 | [=](Config *config, const char *) -> bool { |
| 92 | config->*field = false; |
| 93 | return true; |
| 94 | }}; |
| 95 | } |
| 96 |
| 97 | template <typename T> |
| 98 | bool StringToInt(T *out, const char *str) { |
| 99 | static_assert(std::is_integral<T>::value, "not an integral type"); |
| 100 |
| 101 | // |strtoull| allows leading '-' with wraparound. Additionally, both |
| 102 | // functions accept empty strings and leading whitespace. |
| 103 | if (!OPENSSL_isdigit(static_cast<unsigned char>(*str)) && |
| 104 | (!std::is_signed<T>::value || *str != '-')) { |
| 105 | return false; |
| 106 | } |
| 107 |
| 108 | errno = 0; |
| 109 | char *end; |
| 110 | if (std::is_signed<T>::value) { |
| 111 | static_assert(sizeof(T) <= sizeof(long long), |
| 112 | "type too large for long long"); |
| 113 | long long value = strtoll(str, &end, 10); |
| 114 | if (value < static_cast<long long>(std::numeric_limits<T>::min()) || |
| 115 | value > static_cast<long long>(std::numeric_limits<T>::max())) { |
| 116 | return false; |
| 117 | } |
| 118 | *out = static_cast<T>(value); |
| 119 | } else { |
| 120 | static_assert(sizeof(T) <= sizeof(unsigned long long), |
| 121 | "type too large for unsigned long long"); |
| 122 | unsigned long long value = strtoull(str, &end, 10); |
| 123 | if (value > |
| 124 | static_cast<unsigned long long>(std::numeric_limits<T>::max())) { |
| 125 | return false; |
| 126 | } |
| 127 | *out = static_cast<T>(value); |
| 128 | } |
| 129 |
| 130 | // Check for overflow and that the whole input was consumed. |
| 131 | return errno != ERANGE && *end == '\0'; |
| 132 | } |
| 133 |
| 134 | template <typename Config, typename T> |
| 135 | Flag<Config> IntFlag(const char *name, T Config::*field, |
| 136 | bool skip_handshaker = false) { |
| 137 | return Flag<Config>{name, true, skip_handshaker, |
| 138 | [=](Config *config, const char *param) -> bool { |
| 139 | return StringToInt(&(config->*field), param); |
| 140 | }}; |
| 141 | } |
| 142 |
| 143 | template <typename Config, typename T> |
| 144 | Flag<Config> OptionalIntFlag(const char *name, std::optional<T> Config::*field, |
| 145 | bool skip_handshaker = false) { |
| 146 | return Flag<Config>{name, true, skip_handshaker, |
| 147 | [=](Config *config, const char *param) -> bool { |
| 148 | T value; |
| 149 | if (!StringToInt(&value, param)) { |
| 150 | return false; |
| 151 | } |
| 152 | config->*field = value; |
| 153 | return true; |
| 154 | }}; |
| 155 | } |
| 156 |
| 157 | template <typename Config, typename T> |
| 158 | Flag<Config> IntVectorFlag(const char *name, std::vector<T> Config::*field, |
| 159 | bool skip_handshaker = false) { |
| 160 | return Flag<Config>{name, true, skip_handshaker, |
| 161 | [=](Config *config, const char *param) -> bool { |
| 162 | T value; |
| 163 | if (!StringToInt(&value, param)) { |
| 164 | return false; |
| 165 | } |
| 166 | (config->*field).push_back(value); |
| 167 | return true; |
| 168 | }}; |
| 169 | } |
| 170 |
| 171 | template <typename Config> |
| 172 | Flag<Config> StringFlag(const char *name, std::string Config::*field, |
| 173 | bool skip_handshaker = false) { |
| 174 | return Flag<Config>{name, true, skip_handshaker, |
| 175 | [=](Config *config, const char *param) -> bool { |
| 176 | config->*field = param; |
| 177 | return true; |
| 178 | }}; |
| 179 | } |
| 180 |
| 181 | template <typename Config> |
| 182 | Flag<Config> OptionalStringFlag(const char *name, |
| 183 | std::optional<std::string> Config::*field, |
| 184 | bool skip_handshaker = false) { |
| 185 | return Flag<Config>{name, true, skip_handshaker, |
| 186 | [=](Config *config, const char *param) -> bool { |
| 187 | (config->*field).emplace(param); |
| 188 | return true; |
| 189 | }}; |
| 190 | } |
| 191 |
| 192 | bool DecodeBase64(std::vector<uint8_t> *out, const std::string &in) { |
| 193 | size_t len; |
| 194 | if (!EVP_DecodedLength(&len, in.size())) { |
| 195 | fprintf(stderr, "Invalid base64: %s.\n", in.c_str()); |
| 196 | return false; |
| 197 | } |
| 198 | out->resize(len); |
| 199 | if (!EVP_DecodeBase64(out->data(), &len, out->size(), |
| 200 | reinterpret_cast<const uint8_t *>(in.data()), |
| 201 | in.size())) { |
| 202 | fprintf(stderr, "Invalid base64: %s.\n", in.c_str()); |
| 203 | return false; |
| 204 | } |
| 205 | out->resize(len); |
| 206 | return true; |
| 207 | } |
| 208 |
| 209 | template <typename Config> |
| 210 | Flag<Config> Base64Flag(const char *name, std::vector<uint8_t> Config::*field, |
| 211 | bool skip_handshaker = false) { |
| 212 | return Flag<Config>{name, true, skip_handshaker, |
| 213 | [=](Config *config, const char *param) -> bool { |
| 214 | return DecodeBase64(&(config->*field), param); |
| 215 | }}; |
| 216 | } |
| 217 |
| 218 | template <typename Config> |
| 219 | Flag<Config> OptionalBase64Flag( |
| 220 | const char *name, std::optional<std::vector<uint8_t>> Config::*field, |
| 221 | bool skip_handshaker = false) { |
| 222 | return Flag<Config>{name, true, skip_handshaker, |
| 223 | [=](Config *config, const char *param) -> bool { |
| 224 | (config->*field).emplace(); |
| 225 | return DecodeBase64(&*(config->*field), param); |
| 226 | }}; |
| 227 | } |
| 228 |
| 229 | template <typename Config> |
| 230 | Flag<Config> Base64VectorFlag(const char *name, |
| 231 | std::vector<std::vector<uint8_t>> Config::*field, |
| 232 | bool skip_handshaker = false) { |
| 233 | return Flag<Config>{name, true, skip_handshaker, |
| 234 | [=](Config *config, const char *param) -> bool { |
| 235 | std::vector<uint8_t> value; |
| 236 | if (!DecodeBase64(&value, param)) { |
| 237 | return false; |
| 238 | } |
| 239 | (config->*field).push_back(std::move(value)); |
| 240 | return true; |
| 241 | }}; |
| 242 | } |
| 243 |
| 244 | template <typename Config> |
| 245 | Flag<Config> StringPairVectorFlag( |
| 246 | const char *name, |
| 247 | std::vector<std::pair<std::string, std::string>> Config::*field, |
| 248 | bool skip_handshaker = false) { |
| 249 | return Flag<Config>{ |
| 250 | name, true, skip_handshaker, |
| 251 | [=](Config *config, const char *param) -> bool { |
| 252 | const char *comma = strchr(param, ','); |
| 253 | if (!comma) { |
| 254 | return false; |
| 255 | } |
| 256 | (config->*field) |
| 257 | .push_back(std::make_pair(std::string(param, comma - param), |
| 258 | std::string(comma + 1))); |
| 259 | return true; |
| 260 | }}; |
| 261 | } |
| 262 |
| 263 | Flag<TestConfig> NewCredentialFlag(const char *name, |
| 264 | CredentialConfigType type) { |
| 265 | return Flag<TestConfig>{name, /*has_param=*/false, /*skip_handshaker=*/false, |
| 266 | [=](TestConfig *config, const char *param) -> bool { |
| 267 | config->credentials.emplace_back(); |
| 268 | config->credentials.back().type = type; |
| 269 | return true; |
| 270 | }}; |
| 271 | } |
| 272 |
| 273 | Flag<TestConfig> CredentialFlagWithDefault(Flag<TestConfig> default_flag, |
| 274 | Flag<CredentialConfig> flag) { |
| 275 | BSSL_CHECK(strcmp(default_flag.name, flag.name) == 0); |
| 276 | BSSL_CHECK(default_flag.has_param == flag.has_param); |
| 277 | return Flag<TestConfig>{flag.name, flag.has_param, /*skip_handshaker=*/false, |
| 278 | [=](TestConfig *config, const char *param) -> bool { |
| 279 | if (config->credentials.empty()) { |
| 280 | return default_flag.set_param(config, param); |
| 281 | } |
| 282 | return flag.set_param(&config->credentials.back(), |
| 283 | param); |
| 284 | }}; |
| 285 | } |
| 286 |
| 287 | Flag<TestConfig> CredentialFlag(Flag<CredentialConfig> flag) { |
| 288 | return Flag<TestConfig>{flag.name, flag.has_param, /*skip_handshaker=*/false, |
| 289 | [=](TestConfig *config, const char *param) -> bool { |
| 290 | if (config->credentials.empty()) { |
| 291 | fprintf(stderr, "No credentials configured.\n"); |
| 292 | return false; |
| 293 | } |
| 294 | return flag.set_param(&config->credentials.back(), |
| 295 | param); |
| 296 | }}; |
| 297 | } |
| 298 |
| 299 | struct FlagNameComparator { |
| 300 | template <typename Config> |
| 301 | bool operator()(const Flag<Config> &flag1, const Flag<Config> &flag2) const { |
| 302 | return strcmp(flag1.name, flag2.name) < 0; |
| 303 | } |
| 304 |
| 305 | template <typename Config> |
| 306 | bool operator()(const Flag<Config> &flag, const char *name) const { |
| 307 | return strcmp(flag.name, name) < 0; |
| 308 | } |
| 309 | }; |
| 310 |
| 311 | const Flag<TestConfig> *FindFlag(const char *name) { |
| 312 | static const std::vector<Flag<TestConfig>> flags = [] { |
| 313 | std::vector<Flag<TestConfig>> ret = { |
| 314 | IntFlag("-port", &TestConfig::port, /*skip_handshaker=*/true), |
| 315 | BoolFlag("-ipv6", &TestConfig::ipv6, /*skip_handshaker=*/true), |
| 316 | IntFlag("-shim-id", &TestConfig::shim_id, /*skip_handshaker=*/true), |
| 317 | BoolFlag("-server", &TestConfig::is_server), |
| 318 | BoolFlag("-dtls", &TestConfig::is_dtls), |
| 319 | BoolFlag("-quic", &TestConfig::is_quic), |
| 320 | IntFlag("-resume-count", &TestConfig::resume_count), |
| 321 | StringFlag("-write-settings", &TestConfig::write_settings), |
| 322 | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) |
| 323 | BoolFlag("-fuzzer-mode", &TestConfig::fuzzer_mode), |
| 324 | #endif |
| 325 | BoolFlag("-fallback-scsv", &TestConfig::fallback_scsv), |
| 326 | IntVectorFlag("-verify-prefs", &TestConfig::verify_prefs), |
| 327 | IntVectorFlag("-expect-peer-verify-pref", |
| 328 | &TestConfig::expect_peer_verify_prefs), |
| 329 | IntVectorFlag("-curves", &TestConfig::curves), |
| 330 | StringFlag("-trust-cert", &TestConfig::trust_cert), |
| 331 | StringFlag("-expect-server-name", &TestConfig::expect_server_name), |
| 332 | BoolFlag("-enable-ech-grease", &TestConfig::enable_ech_grease), |
| 333 | Base64VectorFlag("-ech-server-config", &TestConfig::ech_server_configs), |
| 334 | Base64VectorFlag("-ech-server-key", &TestConfig::ech_server_keys), |
| 335 | IntVectorFlag("-ech-is-retry-config", &TestConfig::ech_is_retry_config), |
| 336 | BoolFlag("-expect-ech-accept", &TestConfig::expect_ech_accept), |
| 337 | StringFlag("-expect-ech-name-override", |
| 338 | &TestConfig::expect_ech_name_override), |
| 339 | BoolFlag("-expect-no-ech-name-override", |
| 340 | &TestConfig::expect_no_ech_name_override), |
| 341 | Base64Flag("-expect-ech-retry-configs", |
| 342 | &TestConfig::expect_ech_retry_configs), |
| 343 | BoolFlag("-expect-no-ech-retry-configs", |
| 344 | &TestConfig::expect_no_ech_retry_configs), |
| 345 | Base64Flag("-ech-config-list", &TestConfig::ech_config_list), |
| 346 | Base64Flag("-expect-certificate-types", |
| 347 | &TestConfig::expect_certificate_types), |
| 348 | BoolFlag("-require-any-client-certificate", |
| 349 | &TestConfig::require_any_client_certificate), |
| 350 | StringFlag("-advertise-npn", &TestConfig::advertise_npn), |
| 351 | BoolFlag("-advertise-empty-npn", &TestConfig::advertise_empty_npn), |
| 352 | StringFlag("-expect-next-proto", &TestConfig::expect_next_proto), |
| 353 | BoolFlag("-expect-no-next-proto", &TestConfig::expect_no_next_proto), |
| 354 | BoolFlag("-false-start", &TestConfig::false_start), |
| 355 | StringFlag("-select-next-proto", &TestConfig::select_next_proto), |
| 356 | BoolFlag("-select-empty-next-proto", |
| 357 | &TestConfig::select_empty_next_proto), |
| 358 | BoolFlag("-async", &TestConfig::async), |
| 359 | BoolFlag("-write-different-record-sizes", |
| 360 | &TestConfig::write_different_record_sizes), |
| 361 | BoolFlag("-cbc-record-splitting", &TestConfig::cbc_record_splitting), |
| 362 | BoolFlag("-partial-write", &TestConfig::partial_write), |
| 363 | BoolFlag("-no-tls13", &TestConfig::no_tls13), |
| 364 | BoolFlag("-no-tls12", &TestConfig::no_tls12), |
| 365 | BoolFlag("-no-tls11", &TestConfig::no_tls11), |
| 366 | BoolFlag("-no-tls1", &TestConfig::no_tls1), |
| 367 | BoolFlag("-no-ticket", &TestConfig::no_ticket), |
| 368 | Base64Flag("-expect-channel-id", &TestConfig::expect_channel_id), |
| 369 | BoolFlag("-enable-channel-id", &TestConfig::enable_channel_id), |
| 370 | StringFlag("-send-channel-id", &TestConfig::send_channel_id), |
| 371 | BoolFlag("-shim-writes-first", &TestConfig::shim_writes_first), |
| 372 | StringFlag("-host-name", &TestConfig::host_name), |
| 373 | StringFlag("-advertise-alpn", &TestConfig::advertise_alpn), |
| 374 | StringFlag("-expect-alpn", &TestConfig::expect_alpn), |
| 375 | StringFlag("-expect-advertised-alpn", |
| 376 | &TestConfig::expect_advertised_alpn), |
| 377 | StringFlag("-select-alpn", &TestConfig::select_alpn), |
| 378 | BoolFlag("-decline-alpn", &TestConfig::decline_alpn), |
| 379 | BoolFlag("-reject-alpn", &TestConfig::reject_alpn), |
| 380 | BoolFlag("-select-empty-alpn", &TestConfig::select_empty_alpn), |
| 381 | BoolFlag("-defer-alps", &TestConfig::defer_alps), |
| 382 | StringPairVectorFlag("-application-settings", |
| 383 | &TestConfig::application_settings), |
| 384 | OptionalStringFlag("-expect-peer-application-settings", |
| 385 | &TestConfig::expect_peer_application_settings), |
| 386 | BoolFlag("-alps-use-new-codepoint", |
| 387 | &TestConfig::alps_use_new_codepoint), |
| 388 | Base64Flag("-quic-transport-params", |
| 389 | &TestConfig::quic_transport_params), |
| 390 | Base64Flag("-expect-quic-transport-params", |
| 391 | &TestConfig::expect_quic_transport_params), |
| 392 | IntFlag("-quic-use-legacy-codepoint", |
| 393 | &TestConfig::quic_use_legacy_codepoint), |
| 394 | BoolFlag("-expect-session-miss", &TestConfig::expect_session_miss), |
| 395 | BoolFlag("-expect-extended-master-secret", |
| 396 | &TestConfig::expect_extended_master_secret), |
| 397 | StringFlag("-psk", &TestConfig::psk), |
| 398 | StringFlag("-psk-identity", &TestConfig::psk_identity), |
| 399 | StringFlag("-srtp-profiles", &TestConfig::srtp_profiles), |
| 400 | BoolFlag("-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling), |
| 401 | BoolFlag("-enable-signed-cert-timestamps", |
| 402 | &TestConfig::enable_signed_cert_timestamps), |
| 403 | Base64Flag("-expect-signed-cert-timestamps", |
| 404 | &TestConfig::expect_signed_cert_timestamps), |
| 405 | IntFlag("-min-version", &TestConfig::min_version), |
| 406 | IntFlag("-max-version", &TestConfig::max_version), |
| 407 | IntFlag("-expect-version", &TestConfig::expect_version), |
| 408 | IntFlag("-mtu", &TestConfig::mtu), |
| 409 | BoolFlag("-implicit-handshake", &TestConfig::implicit_handshake), |
| 410 | BoolFlag("-use-early-callback", &TestConfig::use_early_callback), |
| 411 | BoolFlag("-fail-early-callback", &TestConfig::fail_early_callback), |
| 412 | BoolFlag("-fail-early-callback-ech-rewind", |
| 413 | &TestConfig::fail_early_callback_ech_rewind), |
| 414 | BoolFlag("-install-ddos-callback", &TestConfig::install_ddos_callback), |
| 415 | BoolFlag("-fail-ddos-callback", &TestConfig::fail_ddos_callback), |
| 416 | BoolFlag("-fail-cert-callback", &TestConfig::fail_cert_callback), |
| 417 | StringFlag("-cipher", &TestConfig::cipher), |
| 418 | BoolFlag("-handshake-never-done", &TestConfig::handshake_never_done), |
| 419 | IntFlag("-export-keying-material", &TestConfig::export_keying_material), |
| 420 | StringFlag("-export-label", &TestConfig::export_label), |
| 421 | StringFlag("-export-context", &TestConfig::export_context), |
| 422 | BoolFlag("-use-export-context", &TestConfig::use_export_context), |
| 423 | BoolFlag("-tls-unique", &TestConfig::tls_unique), |
| 424 | BoolFlag("-expect-ticket-renewal", &TestConfig::expect_ticket_renewal), |
| 425 | BoolFlag("-expect-no-session", &TestConfig::expect_no_session), |
| 426 | BoolFlag("-expect-ticket-supports-early-data", |
| 427 | &TestConfig::expect_ticket_supports_early_data), |
| 428 | BoolFlag("-expect-accept-early-data", |
| 429 | &TestConfig::expect_accept_early_data), |
| 430 | BoolFlag("-expect-reject-early-data", |
| 431 | &TestConfig::expect_reject_early_data), |
| 432 | BoolFlag("-expect-no-offer-early-data", |
| 433 | &TestConfig::expect_no_offer_early_data), |
| 434 | BoolFlag("-expect-no-server-name", &TestConfig::expect_no_server_name), |
| 435 | BoolFlag("-use-ticket-callback", &TestConfig::use_ticket_callback), |
| 436 | BoolFlag("-use-ticket-aead-callback", |
| 437 | &TestConfig::use_ticket_aead_callback), |
| 438 | BoolFlag("-renew-ticket", &TestConfig::renew_ticket), |
| 439 | BoolFlag("-skip-ticket", &TestConfig::skip_ticket), |
| 440 | BoolFlag("-enable-early-data", &TestConfig::enable_early_data), |
| 441 | Base64Flag("-expect-ocsp-response", &TestConfig::expect_ocsp_response), |
| 442 | BoolFlag("-check-close-notify", &TestConfig::check_close_notify), |
| 443 | BoolFlag("-shim-shuts-down", &TestConfig::shim_shuts_down), |
| 444 | BoolFlag("-verify-fail", &TestConfig::verify_fail), |
| 445 | BoolFlag("-verify-peer", &TestConfig::verify_peer), |
| 446 | BoolFlag("-expect-verify-result", &TestConfig::expect_verify_result), |
| 447 | IntFlag("-expect-total-renegotiations", |
| 448 | &TestConfig::expect_total_renegotiations), |
| 449 | BoolFlag("-renegotiate-once", &TestConfig::renegotiate_once), |
| 450 | BoolFlag("-renegotiate-freely", &TestConfig::renegotiate_freely), |
| 451 | BoolFlag("-renegotiate-ignore", &TestConfig::renegotiate_ignore), |
| 452 | BoolFlag("-renegotiate-explicit", &TestConfig::renegotiate_explicit), |
| 453 | BoolFlag("-forbid-renegotiation-after-handshake", |
| 454 | &TestConfig::forbid_renegotiation_after_handshake), |
| 455 | IntFlag("-expect-peer-signature-algorithm", |
| 456 | &TestConfig::expect_peer_signature_algorithm), |
| 457 | IntFlag("-expect-curve-id", &TestConfig::expect_curve_id), |
| 458 | BoolFlag("-use-old-client-cert-callback", |
| 459 | &TestConfig::use_old_client_cert_callback), |
| 460 | IntFlag("-initial-timeout-duration-ms", |
| 461 | &TestConfig::initial_timeout_duration_ms), |
| 462 | StringFlag("-use-client-ca-list", &TestConfig::use_client_ca_list), |
| 463 | StringFlag("-expect-client-ca-list", |
| 464 | &TestConfig::expect_client_ca_list), |
| 465 | BoolFlag("-send-alert", &TestConfig::send_alert), |
| 466 | BoolFlag("-peek-then-read", &TestConfig::peek_then_read), |
| 467 | BoolFlag("-enable-grease", &TestConfig::enable_grease), |
| 468 | BoolFlag("-permute-extensions", &TestConfig::permute_extensions), |
| 469 | IntFlag("-max-cert-list", &TestConfig::max_cert_list), |
| 470 | Base64Flag("-ticket-key", &TestConfig::ticket_key), |
| 471 | BoolFlag("-use-exporter-between-reads", |
| 472 | &TestConfig::use_exporter_between_reads), |
| 473 | IntFlag("-expect-cipher-aes", &TestConfig::expect_cipher_aes), |
| 474 | IntFlag("-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes), |
| 475 | IntFlag("-expect-cipher", &TestConfig::expect_cipher), |
| 476 | StringFlag("-expect-peer-cert-file", |
| 477 | &TestConfig::expect_peer_cert_file), |
| 478 | IntFlag("-resumption-delay", &TestConfig::resumption_delay), |
| 479 | BoolFlag("-retain-only-sha256-client-cert", |
| 480 | &TestConfig::retain_only_sha256_client_cert), |
| 481 | BoolFlag("-expect-sha256-client-cert", |
| 482 | &TestConfig::expect_sha256_client_cert), |
| 483 | BoolFlag("-read-with-unfinished-write", |
| 484 | &TestConfig::read_with_unfinished_write), |
| 485 | BoolFlag("-expect-secure-renegotiation", |
| 486 | &TestConfig::expect_secure_renegotiation), |
| 487 | BoolFlag("-expect-no-secure-renegotiation", |
| 488 | &TestConfig::expect_no_secure_renegotiation), |
| 489 | IntFlag("-max-send-fragment", &TestConfig::max_send_fragment), |
| 490 | IntFlag("-read-size", &TestConfig::read_size), |
| 491 | BoolFlag("-expect-session-id", &TestConfig::expect_session_id), |
| 492 | BoolFlag("-expect-no-session-id", &TestConfig::expect_no_session_id), |
| 493 | IntFlag("-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew), |
| 494 | BoolFlag("-no-op-extra-handshake", &TestConfig::no_op_extra_handshake), |
| 495 | BoolFlag("-handshake-twice", &TestConfig::handshake_twice), |
| 496 | BoolFlag("-allow-unknown-alpn-protos", |
| 497 | &TestConfig::allow_unknown_alpn_protos), |
| 498 | BoolFlag("-use-custom-verify-callback", |
| 499 | &TestConfig::use_custom_verify_callback), |
| 500 | StringFlag("-expect-msg-callback", &TestConfig::expect_msg_callback), |
| 501 | BoolFlag("-allow-false-start-without-alpn", |
| 502 | &TestConfig::allow_false_start_without_alpn), |
| 503 | BoolFlag("-handoff", &TestConfig::handoff), |
| 504 | BoolFlag("-handshake-hints", &TestConfig::handshake_hints), |
| 505 | BoolFlag("-allow-hint-mismatch", &TestConfig::allow_hint_mismatch), |
| 506 | BoolFlag("-use-ocsp-callback", &TestConfig::use_ocsp_callback), |
| 507 | BoolFlag("-set-ocsp-in-callback", &TestConfig::set_ocsp_in_callback), |
| 508 | BoolFlag("-decline-ocsp-callback", &TestConfig::decline_ocsp_callback), |
| 509 | BoolFlag("-fail-ocsp-callback", &TestConfig::fail_ocsp_callback), |
| 510 | BoolFlag("-install-cert-compression-algs", |
| 511 | &TestConfig::install_cert_compression_algs), |
| 512 | IntFlag("-install-one-cert-compression-alg", |
| 513 | &TestConfig::install_one_cert_compression_alg), |
| 514 | BoolFlag("-reverify-on-resume", &TestConfig::reverify_on_resume), |
| 515 | BoolFlag("-ignore-rsa-key-usage", &TestConfig::ignore_rsa_key_usage), |
| 516 | BoolFlag("-expect-key-usage-invalid", |
| 517 | &TestConfig::expect_key_usage_invalid), |
| 518 | BoolFlag("-is-handshaker-supported", |
| 519 | &TestConfig::is_handshaker_supported), |
| 520 | BoolFlag("-handshaker-resume", &TestConfig::handshaker_resume), |
| 521 | StringFlag("-handshaker-path", &TestConfig::handshaker_path), |
| 522 | BoolFlag("-jdk11-workaround", &TestConfig::jdk11_workaround), |
| 523 | BoolFlag("-server-preference", &TestConfig::server_preference), |
| 524 | BoolFlag("-export-traffic-secrets", |
| 525 | &TestConfig::export_traffic_secrets), |
| 526 | BoolFlag("-key-update", &TestConfig::key_update), |
| 527 | BoolFlag("-key-update-before-read", |
| 528 | &TestConfig::key_update_before_read), |
| 529 | StringFlag("-expect-early-data-reason", |
| 530 | &TestConfig::expect_early_data_reason), |
| 531 | BoolFlag("-expect-hrr", &TestConfig::expect_hrr), |
| 532 | BoolFlag("-expect-no-hrr", &TestConfig::expect_no_hrr), |
| 533 | BoolFlag("-wait-for-debugger", &TestConfig::wait_for_debugger), |
| 534 | StringFlag("-quic-early-data-context", |
| 535 | &TestConfig::quic_early_data_context), |
| 536 | IntFlag("-early-write-after-message", |
| 537 | &TestConfig::early_write_after_message), |
| 538 | BoolFlag("-fips-202205", &TestConfig::fips_202205), |
| 539 | BoolFlag("-wpa-202304", &TestConfig::wpa_202304), |
| 540 | BoolFlag("-cnsa-202407", &TestConfig::cnsa_202407), |
| 541 | OptionalBoolTrueFlag("-expect-peer-match-trust-anchor", |
| 542 | &TestConfig::expect_peer_match_trust_anchor), |
| 543 | OptionalBoolFalseFlag("-expect-no-peer-match-trust-anchor", |
| 544 | &TestConfig::expect_peer_match_trust_anchor), |
| 545 | OptionalBase64Flag("-expect-peer-available-trust-anchors", |
| 546 | &TestConfig::expect_peer_available_trust_anchors), |
| 547 | OptionalBase64Flag("-requested-trust-anchors", |
| 548 | &TestConfig::requested_trust_anchors), |
| 549 | OptionalIntFlag("-expect-selected-credential", |
| 550 | &TestConfig::expect_selected_credential), |
| 551 | // Credential flags are stateful. First, use one of the |
| 552 | // -new-*-credential flags to introduce a new credential. Then the flags |
| 553 | // below switch from acting on the legacy credential to the newly-added |
| 554 | // one. Repeat this process to continue adding them. |
| 555 | NewCredentialFlag("-new-x509-credential", CredentialConfigType::kX509), |
| 556 | NewCredentialFlag("-new-delegated-credential", |
| 557 | CredentialConfigType::kDelegated), |
| 558 | NewCredentialFlag("-new-spake2plusv1-credential", |
| 559 | CredentialConfigType::kSPAKE2PlusV1), |
| 560 | CredentialFlagWithDefault( |
| 561 | StringFlag("-cert-file", &TestConfig::cert_file), |
| 562 | StringFlag("-cert-file", &CredentialConfig::cert_file)), |
| 563 | CredentialFlagWithDefault( |
| 564 | StringFlag("-key-file", &TestConfig::key_file), |
| 565 | StringFlag("-key-file", &CredentialConfig::key_file)), |
| 566 | CredentialFlagWithDefault( |
| 567 | IntVectorFlag("-signing-prefs", &TestConfig::signing_prefs), |
| 568 | IntVectorFlag("-signing-prefs", &CredentialConfig::signing_prefs)), |
| 569 | CredentialFlag(Base64Flag("-delegated-credential", |
| 570 | &CredentialConfig::delegated_credential)), |
| 571 | CredentialFlagWithDefault( |
| 572 | Base64Flag("-ocsp-response", &TestConfig::ocsp_response), |
| 573 | Base64Flag("-ocsp-response", &CredentialConfig::ocsp_response)), |
| 574 | CredentialFlagWithDefault( |
| 575 | Base64Flag("-signed-cert-timestamps", |
| 576 | &TestConfig::signed_cert_timestamps), |
| 577 | Base64Flag("-signed-cert-timestamps", |
| 578 | &CredentialConfig::signed_cert_timestamps)), |
| 579 | CredentialFlag(BoolFlag("-must-match-issuer", |
| 580 | &CredentialConfig::must_match_issuer)), |
| 581 | CredentialFlag( |
| 582 | Base64Flag("-pake-context", &CredentialConfig::pake_context)), |
| 583 | CredentialFlag( |
| 584 | Base64Flag("-pake-client-id", &CredentialConfig::pake_client_id)), |
| 585 | CredentialFlag( |
| 586 | Base64Flag("-pake-server-id", &CredentialConfig::pake_server_id)), |
| 587 | CredentialFlag( |
| 588 | Base64Flag("-pake-password", &CredentialConfig::pake_password)), |
| 589 | CredentialFlag( |
| 590 | BoolFlag("-wrong-pake-role", &CredentialConfig::wrong_pake_role)), |
| 591 | CredentialFlag( |
| 592 | Base64Flag("-trust-anchor-id", &CredentialConfig::trust_anchor_id)), |
| 593 | IntFlag("-private-key-delay-ms", &TestConfig::private_key_delay_ms), |
| 594 | BoolFlag("-resumption-across-names-enabled", |
| 595 | &TestConfig::resumption_across_names_enabled), |
| 596 | OptionalBoolTrueFlag("-expect-resumable-across-names", |
| 597 | &TestConfig::expect_resumable_across_names), |
| 598 | OptionalBoolFalseFlag("-expect-not-resumable-across-names", |
| 599 | &TestConfig::expect_resumable_across_names), |
| 600 | }; |
| 601 | std::sort(ret.begin(), ret.end(), FlagNameComparator{}); |
| 602 | return ret; |
| 603 | }(); |
| 604 | auto iter = |
| 605 | std::lower_bound(flags.begin(), flags.end(), name, FlagNameComparator{}); |
| 606 | if (iter == flags.end() || strcmp(iter->name, name) != 0) { |
| 607 | return nullptr; |
| 608 | } |
| 609 | return &*iter; |
| 610 | } |
| 611 |
| 612 | // RemovePrefix checks if |*str| begins with |prefix| + "-". If so, it advances |
| 613 | // |*str| past |prefix| (but not past the "-") and returns true. Otherwise, it |
| 614 | // returns false and leaves |*str| unmodified. |
| 615 | bool RemovePrefix(const char **str, const char *prefix) { |
| 616 | size_t prefix_len = strlen(prefix); |
| 617 | if (strncmp(*str, prefix, strlen(prefix)) == 0 && (*str)[prefix_len] == '-') { |
| 618 | *str += strlen(prefix); |
| 619 | return true; |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 |
| 624 | } // namespace |
| 625 |
| 626 | bool ParseConfig(int argc, char **argv, bool is_shim, TestConfig *out_initial, |
| 627 | TestConfig *out_resume, TestConfig *out_retry) { |
| 628 | for (int i = 0; i < argc; i++) { |
| 629 | bool skip = false; |
| 630 | const char *arg = argv[i]; |
| 631 | const char *name = arg; |
| 632 |
| 633 | // -on-shim and -on-handshaker prefixes enable flags only on the shim or |
| 634 | // handshaker. |
| 635 | if (RemovePrefix(&name, "-on-shim")) { |
| 636 | if (!is_shim) { |
| 637 | skip = true; |
| 638 | } |
| 639 | } else if (RemovePrefix(&name, "-on-handshaker")) { |
| 640 | if (is_shim) { |
| 641 | skip = true; |
| 642 | } |
| 643 | } |
| 644 |
| 645 | // The following prefixes allow different configurations for each of the |
| 646 | // initial, resumption, and 0-RTT retry handshakes. |
| 647 | TestConfig *out = nullptr; |
| 648 | if (RemovePrefix(&name, "-on-initial")) { |
| 649 | out = out_initial; |
| 650 | } else if (RemovePrefix(&name, "-on-resume")) { |
| 651 | out = out_resume; |
| 652 | } else if (RemovePrefix(&name, "-on-retry")) { |
| 653 | out = out_retry; |
| 654 | } |
| 655 |
| 656 | const Flag<TestConfig> *flag = FindFlag(name); |
| 657 | if (flag == nullptr) { |
| 658 | fprintf(stderr, "Unrecognized flag: %s\n", name); |
| 659 | return false; |
| 660 | } |
| 661 |
| 662 | const char *param = nullptr; |
| 663 | if (flag->has_param) { |
| 664 | if (i >= argc) { |
| 665 | fprintf(stderr, "Missing parameter for %s\n", name); |
| 666 | return false; |
| 667 | } |
| 668 | i++; |
| 669 | param = argv[i]; |
| 670 | } |
| 671 |
| 672 | if (!flag->skip_handshaker) { |
| 673 | out_initial->handshaker_args.push_back(arg); |
| 674 | if (flag->has_param) { |
| 675 | out_initial->handshaker_args.push_back(param); |
| 676 | } |
| 677 | } |
| 678 |
| 679 | if (!skip) { |
| 680 | if (out != nullptr) { |
| 681 | if (!flag->set_param(out, param)) { |
| 682 | fprintf(stderr, "Invalid parameter for %s: %s\n", name, param); |
| 683 | return false; |
| 684 | } |
| 685 | } else { |
| 686 | // Unprefixed flags apply to all three. |
| 687 | if (!flag->set_param(out_initial, param) || |
| 688 | !flag->set_param(out_resume, param) || |
| 689 | !flag->set_param(out_retry, param)) { |
| 690 | fprintf(stderr, "Invalid parameter for %s: %s\n", name, param); |
| 691 | return false; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 |
| 697 | out_resume->handshaker_args = out_initial->handshaker_args; |
| 698 | out_retry->handshaker_args = out_initial->handshaker_args; |
| 699 | return true; |
| 700 | } |
| 701 |
| 702 | static CRYPTO_BUFFER_POOL *BufferPool() { |
| 703 | static CRYPTO_BUFFER_POOL *pool = [&] { |
| 704 | OPENSSL_disable_malloc_failures_for_testing(); |
| 705 | CRYPTO_BUFFER_POOL *ret = CRYPTO_BUFFER_POOL_new(); |
| 706 | BSSL_CHECK(ret != nullptr); |
| 707 | OPENSSL_enable_malloc_failures_for_testing(); |
| 708 | return ret; |
| 709 | }(); |
| 710 | return pool; |
| 711 | } |
| 712 |
| 713 | static int TestConfigExDataIndex() { |
| 714 | static int index = [&] { |
| 715 | OPENSSL_disable_malloc_failures_for_testing(); |
| 716 | int ret = SSL_get_ex_new_index(0, nullptr, nullptr, nullptr, nullptr); |
| 717 | BSSL_CHECK(ret >= 0); |
| 718 | OPENSSL_enable_malloc_failures_for_testing(); |
| 719 | return ret; |
| 720 | }(); |
| 721 | return index; |
| 722 | } |
| 723 |
| 724 | bool SetTestConfig(SSL *ssl, const TestConfig *config) { |
| 725 | return SSL_set_ex_data(ssl, TestConfigExDataIndex(), (void *)config) == 1; |
| 726 | } |
| 727 |
| 728 | const TestConfig *GetTestConfig(const SSL *ssl) { |
| 729 | return static_cast<const TestConfig *>( |
| 730 | SSL_get_ex_data(ssl, TestConfigExDataIndex())); |
| 731 | } |
| 732 |
| 733 | struct CredentialInfo { |
| 734 | int number = -1; |
| 735 | bssl::UniquePtr<EVP_PKEY> private_key; |
| 736 | }; |
| 737 |
| 738 | static void CredentialInfoExDataFree(void *parent, void *ptr, |
| 739 | CRYPTO_EX_DATA *ad, int index, long argl, |
| 740 | void *argp) { |
| 741 | delete static_cast<CredentialInfo *>(ptr); |
| 742 | } |
| 743 |
| 744 | static int CredentialInfoExDataIndex() { |
| 745 | static int index = [&] { |
| 746 | OPENSSL_disable_malloc_failures_for_testing(); |
| 747 | int ret = SSL_CREDENTIAL_get_ex_new_index(0, nullptr, nullptr, nullptr, |
| 748 | CredentialInfoExDataFree); |
| 749 | BSSL_CHECK(ret >= 0); |
| 750 | OPENSSL_enable_malloc_failures_for_testing(); |
| 751 | return ret; |
| 752 | }(); |
| 753 | return index; |
| 754 | } |
| 755 |
| 756 | static const CredentialInfo *GetCredentialInfo(const SSL_CREDENTIAL *cred) { |
| 757 | return static_cast<const CredentialInfo *>( |
| 758 | SSL_CREDENTIAL_get_ex_data(cred, CredentialInfoExDataIndex())); |
| 759 | } |
| 760 |
| 761 | static bool SetCredentialInfo(SSL_CREDENTIAL *cred, |
| 762 | std::unique_ptr<CredentialInfo> info) { |
| 763 | if (!SSL_CREDENTIAL_set_ex_data(cred, CredentialInfoExDataIndex(), |
| 764 | info.get())) { |
| 765 | return false; |
| 766 | } |
| 767 | info.release(); // |cred| takes ownership on success. |
| 768 | return true; |
| 769 | } |
| 770 |
| 771 | static int LegacyOCSPCallback(SSL *ssl, void *arg) { |
| 772 | const TestConfig *config = GetTestConfig(ssl); |
| 773 | if (!SSL_is_server(ssl)) { |
| 774 | return !config->fail_ocsp_callback; |
| 775 | } |
| 776 |
| 777 | if (!config->ocsp_response.empty() && config->set_ocsp_in_callback && |
| 778 | !SSL_set_ocsp_response(ssl, (const uint8_t *)config->ocsp_response.data(), |
| 779 | config->ocsp_response.size())) { |
| 780 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 781 | } |
| 782 | if (config->fail_ocsp_callback) { |
| 783 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 784 | } |
| 785 | if (config->decline_ocsp_callback) { |
| 786 | return SSL_TLSEXT_ERR_NOACK; |
| 787 | } |
| 788 | return SSL_TLSEXT_ERR_OK; |
| 789 | } |
| 790 |
| 791 | static int ServerNameCallback(SSL *ssl, int *out_alert, void *arg) { |
| 792 | // SNI must be accessible from the SNI callback. |
| 793 | const TestConfig *config = GetTestConfig(ssl); |
| 794 | const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 795 | if (server_name == nullptr || |
| 796 | std::string(server_name) != config->expect_server_name) { |
| 797 | fprintf(stderr, "servername mismatch (got %s; want %s).\n", server_name, |
| 798 | config->expect_server_name.c_str()); |
| 799 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 800 | } |
| 801 |
| 802 | return SSL_TLSEXT_ERR_OK; |
| 803 | } |
| 804 |
| 805 | static int NextProtoSelectCallback(SSL *ssl, uint8_t **out, uint8_t *outlen, |
| 806 | const uint8_t *in, unsigned inlen, |
| 807 | void *arg) { |
| 808 | const TestConfig *config = GetTestConfig(ssl); |
| 809 | *out = (uint8_t *)config->select_next_proto.data(); |
| 810 | *outlen = config->select_next_proto.size(); |
| 811 | return SSL_TLSEXT_ERR_OK; |
| 812 | } |
| 813 |
| 814 | static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out, |
| 815 | unsigned int *out_len, void *arg) { |
| 816 | const TestConfig *config = GetTestConfig(ssl); |
| 817 | if (config->advertise_npn.empty() && !config->advertise_empty_npn) { |
| 818 | return SSL_TLSEXT_ERR_NOACK; |
| 819 | } |
| 820 |
| 821 | if (config->advertise_npn.size() > UINT_MAX) { |
| 822 | fprintf(stderr, "NPN value too large.\n"); |
| 823 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 824 | } |
| 825 |
| 826 | *out = reinterpret_cast<const uint8_t *>(config->advertise_npn.data()); |
| 827 | *out_len = static_cast<unsigned>(config->advertise_npn.size()); |
| 828 | return SSL_TLSEXT_ERR_OK; |
| 829 | } |
| 830 |
| 831 | static void MessageCallback(int is_write, int version, int content_type, |
| 832 | const void *buf, size_t len, SSL *ssl, void *arg) { |
| 833 | const uint8_t *buf_u8 = reinterpret_cast<const uint8_t *>(buf); |
| 834 | const TestConfig *config = GetTestConfig(ssl); |
| 835 | TestState *state = GetTestState(ssl); |
| 836 | if (!state->msg_callback_ok) { |
| 837 | return; |
| 838 | } |
| 839 |
| 840 | if (content_type == SSL3_RT_HEADER) { |
| 841 | if (config->is_dtls) { |
| 842 | // Starting DTLS 1.3, record headers are variable-length, but they will |
| 843 | // not be longer than DTLS 1.2's 13-byte header. |
| 844 | if (len > 13) { |
| 845 | fprintf(stderr, "DTLS record header is too long: %zu.\n", len); |
| 846 | } |
| 847 | return; |
| 848 | } |
| 849 | if (len != SSL3_RT_HEADER_LENGTH) { |
| 850 | fprintf(stderr, "Incorrect length for record header: %zu.\n", len); |
| 851 | state->msg_callback_ok = false; |
| 852 | } |
| 853 | return; |
| 854 | } |
| 855 |
| 856 | state->msg_callback_text += is_write ? "write " : "read "; |
| 857 | switch (content_type) { |
| 858 | case 0: |
| 859 | if (version != SSL2_VERSION) { |
| 860 | fprintf(stderr, "Incorrect version for V2ClientHello: %x.\n", |
| 861 | static_cast<unsigned>(version)); |
| 862 | state->msg_callback_ok = false; |
| 863 | return; |
| 864 | } |
| 865 | state->msg_callback_text += "v2clienthello\n"; |
| 866 | return; |
| 867 |
| 868 | case SSL3_RT_CLIENT_HELLO_INNER: |
| 869 | case SSL3_RT_HANDSHAKE: { |
| 870 | CBS cbs; |
| 871 | CBS_init(&cbs, buf_u8, len); |
| 872 | uint8_t type; |
| 873 | uint32_t msg_len; |
| 874 | if (!CBS_get_u8(&cbs, &type) || |
| 875 | // TODO(davidben): Reporting on entire messages would be more |
| 876 | // consistent than fragments. |
| 877 | (config->is_dtls && |
| 878 | !CBS_skip(&cbs, 3 /* total */ + 2 /* seq */ + 3 /* frag_off */)) || |
| 879 | !CBS_get_u24(&cbs, &msg_len) || !CBS_skip(&cbs, msg_len) || |
| 880 | CBS_len(&cbs) != 0) { |
| 881 | fprintf(stderr, "Could not parse handshake message.\n"); |
| 882 | state->msg_callback_ok = false; |
| 883 | return; |
| 884 | } |
| 885 | char text[16]; |
| 886 | if (content_type == SSL3_RT_CLIENT_HELLO_INNER) { |
| 887 | if (type != SSL3_MT_CLIENT_HELLO) { |
| 888 | fprintf(stderr, "Invalid header for ClientHelloInner.\n"); |
| 889 | state->msg_callback_ok = false; |
| 890 | return; |
| 891 | } |
| 892 | state->msg_callback_text += "clienthelloinner\n"; |
| 893 | } else { |
| 894 | snprintf(text, sizeof(text), "hs %d\n", type); |
| 895 | state->msg_callback_text += text; |
| 896 | if (!is_write) { |
| 897 | state->last_message_received = type; |
| 898 | } |
| 899 | } |
| 900 | return; |
| 901 | } |
| 902 |
| 903 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
| 904 | if (len != 1 || buf_u8[0] != 1) { |
| 905 | fprintf(stderr, "Invalid ChangeCipherSpec.\n"); |
| 906 | state->msg_callback_ok = false; |
| 907 | return; |
| 908 | } |
| 909 | state->msg_callback_text += "ccs\n"; |
| 910 | return; |
| 911 |
| 912 | case SSL3_RT_ALERT: |
| 913 | if (len != 2) { |
| 914 | fprintf(stderr, "Invalid alert.\n"); |
| 915 | state->msg_callback_ok = false; |
| 916 | return; |
| 917 | } |
| 918 | char text[16]; |
| 919 | snprintf(text, sizeof(text), "alert %d %d\n", buf_u8[0], buf_u8[1]); |
| 920 | state->msg_callback_text += text; |
| 921 | return; |
| 922 |
| 923 | case SSL3_RT_ACK: |
| 924 | state->msg_callback_text += "ack\n"; |
| 925 | return; |
| 926 |
| 927 | default: |
| 928 | fprintf(stderr, "Invalid content_type: %d.\n", content_type); |
| 929 | state->msg_callback_ok = false; |
| 930 | } |
| 931 | } |
| 932 |
| 933 | static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 934 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 935 | int encrypt) { |
| 936 | if (!encrypt) { |
| 937 | if (GetTestState(ssl)->ticket_decrypt_done) { |
| 938 | fprintf(stderr, "TicketKeyCallback called after completion.\n"); |
| 939 | return -1; |
| 940 | } |
| 941 |
| 942 | GetTestState(ssl)->ticket_decrypt_done = true; |
| 943 | } |
| 944 |
| 945 | // This is just test code, so use the all-zeros key. |
| 946 | static const uint8_t kZeros[16] = {0}; |
| 947 |
| 948 | if (encrypt) { |
| 949 | if (GetTestConfig(ssl)->skip_ticket) { |
| 950 | return 0; |
| 951 | } |
| 952 | OPENSSL_memcpy(key_name, kZeros, sizeof(kZeros)); |
| 953 | RAND_bytes(iv, 16); |
| 954 | } else if (OPENSSL_memcmp(key_name, kZeros, 16) != 0) { |
| 955 | return 0; |
| 956 | } |
| 957 |
| 958 | if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) || |
| 959 | !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) { |
| 960 | return -1; |
| 961 | } |
| 962 |
| 963 | if (!encrypt) { |
| 964 | return GetTestConfig(ssl)->renew_ticket ? 2 : 1; |
| 965 | } |
| 966 | return 1; |
| 967 | } |
| 968 |
| 969 | static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) { |
| 970 | // This callback is called as the handshake completes. |SSL_get_session| |
| 971 | // must continue to work and, historically, |SSL_in_init| returned false at |
| 972 | // this point. |
| 973 | if (SSL_in_init(ssl) || SSL_get_session(ssl) == nullptr) { |
| 974 | fprintf(stderr, "Invalid state for NewSessionCallback.\n"); |
| 975 | abort(); |
| 976 | } |
| 977 |
| 978 | GetTestState(ssl)->got_new_session = true; |
| 979 | GetTestState(ssl)->new_session.reset(session); |
| 980 | return 1; |
| 981 | } |
| 982 |
| 983 | static void InfoCallback(const SSL *ssl, int type, int val) { |
| 984 | if (type == SSL_CB_HANDSHAKE_DONE) { |
| 985 | if (GetTestConfig(ssl)->handshake_never_done) { |
| 986 | fprintf(stderr, "Handshake unexpectedly completed.\n"); |
| 987 | // Abort before any expected error code is printed, to ensure the overall |
| 988 | // test fails. |
| 989 | abort(); |
| 990 | } |
| 991 |
| 992 | // This callback is called when the handshake completes. |SSL_get_session| |
| 993 | // must continue to work and |SSL_in_init| must return false. |
| 994 | if (SSL_in_init(ssl) || SSL_get_session(ssl) == nullptr) { |
| 995 | fprintf(stderr, "Invalid state for SSL_CB_HANDSHAKE_DONE.\n"); |
| 996 | abort(); |
| 997 | } |
| 998 |
| 999 | TestState *test_state = GetTestState(ssl); |
| 1000 | test_state->handshake_done = true; |
| 1001 |
| 1002 | // Save the selected credential for the tests to assert on. |
| 1003 | const SSL_CREDENTIAL *cred = SSL_get0_selected_credential(ssl); |
| 1004 | const CredentialInfo *cred_info = |
| 1005 | cred != nullptr ? GetCredentialInfo(cred) : nullptr; |
| 1006 | test_state->selected_credential = |
| 1007 | cred_info != nullptr ? cred_info->number : -1; |
| 1008 | } |
| 1009 | } |
| 1010 |
| 1011 | static SSL_SESSION *GetSessionCallback(SSL *ssl, const uint8_t *data, int len, |
| 1012 | int *copy) { |
| 1013 | TestState *async_state = GetTestState(ssl); |
| 1014 | if (async_state->session) { |
| 1015 | *copy = 0; |
| 1016 | return async_state->session.release(); |
| 1017 | } else if (async_state->pending_session) { |
| 1018 | return SSL_magic_pending_session_ptr(); |
| 1019 | } else { |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | } |
| 1023 |
| 1024 | static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { |
| 1025 | *out_clock = *GetClock(); |
| 1026 | } |
| 1027 |
| 1028 | static int AlpnSelectCallback(SSL *ssl, const uint8_t **out, uint8_t *outlen, |
| 1029 | const uint8_t *in, unsigned inlen, void *arg) { |
| 1030 | if (GetTestState(ssl)->alpn_select_done) { |
| 1031 | fprintf(stderr, "AlpnSelectCallback called after completion.\n"); |
| 1032 | exit(1); |
| 1033 | } |
| 1034 |
| 1035 | GetTestState(ssl)->alpn_select_done = true; |
| 1036 |
| 1037 | const TestConfig *config = GetTestConfig(ssl); |
| 1038 | if (config->decline_alpn) { |
| 1039 | return SSL_TLSEXT_ERR_NOACK; |
| 1040 | } |
| 1041 | if (config->reject_alpn) { |
| 1042 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 1043 | } |
| 1044 |
| 1045 | if (!config->expect_advertised_alpn.empty() && |
| 1046 | bssl::StringAsBytes(config->expect_advertised_alpn) != |
| 1047 | bssl::Span(in, inlen)) { |
| 1048 | fprintf(stderr, "bad ALPN select callback inputs.\n"); |
| 1049 | exit(1); |
| 1050 | } |
| 1051 |
| 1052 | if (config->defer_alps) { |
| 1053 | for (const auto &pair : config->application_settings) { |
| 1054 | if (!SSL_add_application_settings( |
| 1055 | ssl, reinterpret_cast<const uint8_t *>(pair.first.data()), |
| 1056 | pair.first.size(), |
| 1057 | reinterpret_cast<const uint8_t *>(pair.second.data()), |
| 1058 | pair.second.size())) { |
| 1059 | fprintf(stderr, "error configuring ALPS.\n"); |
| 1060 | exit(1); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 |
| 1065 | assert(config->select_alpn.empty() || !config->select_empty_alpn); |
| 1066 | *out = (const uint8_t *)config->select_alpn.data(); |
| 1067 | *outlen = config->select_alpn.size(); |
| 1068 | return SSL_TLSEXT_ERR_OK; |
| 1069 | } |
| 1070 |
| 1071 | static bool CheckVerifyCallback(SSL *ssl) { |
| 1072 | const TestConfig *config = GetTestConfig(ssl); |
| 1073 | if (!config->expect_ocsp_response.empty()) { |
| 1074 | const uint8_t *data; |
| 1075 | size_t len; |
| 1076 | SSL_get0_ocsp_response(ssl, &data, &len); |
| 1077 | if (len == 0) { |
| 1078 | fprintf(stderr, "OCSP response not available in verify callback.\n"); |
| 1079 | return false; |
| 1080 | } |
| 1081 | } |
| 1082 |
| 1083 | const char *name_override; |
| 1084 | size_t name_override_len; |
| 1085 | SSL_get0_ech_name_override(ssl, &name_override, &name_override_len); |
| 1086 | if (config->expect_no_ech_name_override && name_override_len != 0) { |
| 1087 | fprintf(stderr, "Unexpected ECH name override.\n"); |
| 1088 | return false; |
| 1089 | } |
| 1090 | if (!config->expect_ech_name_override.empty() && |
| 1091 | config->expect_ech_name_override != |
| 1092 | std::string(name_override, name_override_len)) { |
| 1093 | fprintf(stderr, "ECH name did not match expected value.\n"); |
| 1094 | return false; |
| 1095 | } |
| 1096 |
| 1097 | if (config->expect_peer_match_trust_anchor.has_value() && |
| 1098 | !!SSL_peer_matched_trust_anchor(ssl) != |
| 1099 | config->expect_peer_match_trust_anchor.value()) { |
| 1100 | fprintf(stderr, "Peer unexpected %s a requested trust anchor", |
| 1101 | SSL_peer_matched_trust_anchor(ssl) ? "matched" : "failed to match"); |
| 1102 | return false; |
| 1103 | } |
| 1104 |
| 1105 | if (config->expect_peer_available_trust_anchors.has_value()) { |
| 1106 | const uint8_t *peer_ids; |
| 1107 | size_t peer_ids_len; |
| 1108 | SSL_get0_peer_available_trust_anchors(ssl, &peer_ids, &peer_ids_len); |
| 1109 | if (bssl::Span(peer_ids, peer_ids_len) != |
| 1110 | *config->expect_peer_available_trust_anchors) { |
| 1111 | fprintf(stderr, |
| 1112 | "Peer's available trust anchors did not match expectations."); |
| 1113 | return false; |
| 1114 | } |
| 1115 | } |
| 1116 |
| 1117 | if (GetTestState(ssl)->cert_verified) { |
| 1118 | fprintf(stderr, "Certificate verified twice.\n"); |
| 1119 | return false; |
| 1120 | } |
| 1121 |
| 1122 | return true; |
| 1123 | } |
| 1124 |
| 1125 | static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
| 1126 | SSL *ssl = (SSL *)X509_STORE_CTX_get_ex_data( |
| 1127 | store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1128 | const TestConfig *config = GetTestConfig(ssl); |
| 1129 | if (!CheckVerifyCallback(ssl)) { |
| 1130 | return 0; |
| 1131 | } |
| 1132 |
| 1133 | GetTestState(ssl)->cert_verified = true; |
| 1134 | if (config->verify_fail) { |
| 1135 | X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_APPLICATION_VERIFICATION); |
| 1136 | return 0; |
| 1137 | } |
| 1138 |
| 1139 | return 1; |
| 1140 | } |
| 1141 |
| 1142 | bool LoadCertificate(bssl::UniquePtr<X509> *out_x509, |
| 1143 | bssl::UniquePtr<STACK_OF(X509)> *out_chain, |
| 1144 | const std::string &file) { |
| 1145 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); |
| 1146 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
| 1147 | return false; |
| 1148 | } |
| 1149 |
| 1150 | out_x509->reset(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 1151 | if (!*out_x509) { |
| 1152 | return false; |
| 1153 | } |
| 1154 |
| 1155 | out_chain->reset(sk_X509_new_null()); |
| 1156 | if (!*out_chain) { |
| 1157 | return false; |
| 1158 | } |
| 1159 |
| 1160 | // Keep reading the certificate chain. |
| 1161 | for (;;) { |
| 1162 | bssl::UniquePtr<X509> cert( |
| 1163 | PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 1164 | if (!cert) { |
| 1165 | break; |
| 1166 | } |
| 1167 |
| 1168 | if (!bssl::PushToStack(out_chain->get(), std::move(cert))) { |
| 1169 | return false; |
| 1170 | } |
| 1171 | } |
| 1172 |
| 1173 | uint32_t err = ERR_peek_last_error(); |
| 1174 | if (ERR_GET_LIB(err) != ERR_LIB_PEM || |
| 1175 | ERR_GET_REASON(err) != PEM_R_NO_START_LINE) { |
| 1176 | return false; |
| 1177 | } |
| 1178 |
| 1179 | ERR_clear_error(); |
| 1180 | return true; |
| 1181 | } |
| 1182 |
| 1183 | bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) { |
| 1184 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); |
| 1185 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
| 1186 | return nullptr; |
| 1187 | } |
| 1188 | return bssl::UniquePtr<EVP_PKEY>( |
| 1189 | PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); |
| 1190 | } |
| 1191 |
| 1192 | static bssl::UniquePtr<CRYPTO_BUFFER> X509ToBuffer(X509 *x509) { |
| 1193 | uint8_t *der = nullptr; |
| 1194 | int der_len = i2d_X509(x509, &der); |
| 1195 | if (der_len < 0) { |
| 1196 | return nullptr; |
| 1197 | } |
| 1198 | bssl::UniquePtr<uint8_t> free_der(der); |
| 1199 | return bssl::UniquePtr<CRYPTO_BUFFER>( |
| 1200 | CRYPTO_BUFFER_new(der, der_len, nullptr)); |
| 1201 | } |
| 1202 |
| 1203 |
| 1204 | static ssl_private_key_result_t AsyncPrivateKeyComplete(SSL *ssl, uint8_t *out, |
| 1205 | size_t *out_len, |
| 1206 | size_t max_out); |
| 1207 |
| 1208 | static EVP_PKEY *GetPrivateKey(SSL *ssl) { |
| 1209 | const CredentialInfo *cred_info = |
| 1210 | GetCredentialInfo(SSL_get0_selected_credential(ssl)); |
| 1211 | if (cred_info != nullptr) { |
| 1212 | return cred_info->private_key.get(); |
| 1213 | } |
| 1214 |
| 1215 | return GetTestState(ssl)->private_key.get(); |
| 1216 | } |
| 1217 |
| 1218 | static ssl_private_key_result_t AsyncPrivateKeySign( |
| 1219 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 1220 | uint16_t signature_algorithm, const uint8_t *in, size_t in_len) { |
| 1221 | TestState *test_state = GetTestState(ssl); |
| 1222 | test_state->used_private_key = true; |
| 1223 | if (!test_state->private_key_result.empty()) { |
| 1224 | fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n"); |
| 1225 | abort(); |
| 1226 | } |
| 1227 |
| 1228 | EVP_PKEY *private_key = GetPrivateKey(ssl); |
| 1229 | if (EVP_PKEY_id(private_key) != |
| 1230 | SSL_get_signature_algorithm_key_type(signature_algorithm)) { |
| 1231 | fprintf(stderr, "Key type does not match signature algorithm.\n"); |
| 1232 | abort(); |
| 1233 | } |
| 1234 |
| 1235 | // Determine the hash. |
| 1236 | const EVP_MD *md = SSL_get_signature_algorithm_digest(signature_algorithm); |
| 1237 | bssl::ScopedEVP_MD_CTX ctx; |
| 1238 | EVP_PKEY_CTX *pctx; |
| 1239 | if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr, private_key)) { |
| 1240 | return ssl_private_key_failure; |
| 1241 | } |
| 1242 |
| 1243 | // Configure additional signature parameters. |
| 1244 | if (SSL_is_signature_algorithm_rsa_pss(signature_algorithm)) { |
| 1245 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 1246 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) { |
| 1247 | return ssl_private_key_failure; |
| 1248 | } |
| 1249 | } |
| 1250 |
| 1251 | // Write the signature into |test_state|. |
| 1252 | size_t len = 0; |
| 1253 | if (!EVP_DigestSign(ctx.get(), nullptr, &len, in, in_len)) { |
| 1254 | return ssl_private_key_failure; |
| 1255 | } |
| 1256 | test_state->private_key_result.resize(len); |
| 1257 | if (!EVP_DigestSign(ctx.get(), test_state->private_key_result.data(), &len, |
| 1258 | in, in_len)) { |
| 1259 | return ssl_private_key_failure; |
| 1260 | } |
| 1261 | test_state->private_key_result.resize(len); |
| 1262 |
| 1263 | return AsyncPrivateKeyComplete(ssl, out, out_len, max_out); |
| 1264 | } |
| 1265 |
| 1266 | static ssl_private_key_result_t AsyncPrivateKeyDecrypt(SSL *ssl, uint8_t *out, |
| 1267 | size_t *out_len, |
| 1268 | size_t max_out, |
| 1269 | const uint8_t *in, |
| 1270 | size_t in_len) { |
| 1271 | TestState *test_state = GetTestState(ssl); |
| 1272 | test_state->used_private_key = true; |
| 1273 | if (!test_state->private_key_result.empty()) { |
| 1274 | fprintf(stderr, "AsyncPrivateKeyDecrypt called with operation pending.\n"); |
| 1275 | abort(); |
| 1276 | } |
| 1277 |
| 1278 | EVP_PKEY *private_key = GetPrivateKey(ssl); |
| 1279 | RSA *rsa = EVP_PKEY_get0_RSA(private_key); |
| 1280 | if (rsa == NULL) { |
| 1281 | fprintf(stderr, "AsyncPrivateKeyDecrypt called with incorrect key type.\n"); |
| 1282 | abort(); |
| 1283 | } |
| 1284 | test_state->private_key_result.resize(RSA_size(rsa)); |
| 1285 | if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(), |
| 1286 | RSA_size(rsa), in, in_len, RSA_NO_PADDING)) { |
| 1287 | return ssl_private_key_failure; |
| 1288 | } |
| 1289 |
| 1290 | test_state->private_key_result.resize(*out_len); |
| 1291 |
| 1292 | return AsyncPrivateKeyComplete(ssl, out, out_len, max_out); |
| 1293 | } |
| 1294 |
| 1295 | static ssl_private_key_result_t AsyncPrivateKeyComplete(SSL *ssl, uint8_t *out, |
| 1296 | size_t *out_len, |
| 1297 | size_t max_out) { |
| 1298 | TestState *test_state = GetTestState(ssl); |
| 1299 | if (test_state->private_key_result.empty()) { |
| 1300 | fprintf(stderr, |
| 1301 | "AsyncPrivateKeyComplete called without operation pending.\n"); |
| 1302 | abort(); |
| 1303 | } |
| 1304 |
| 1305 | if (GetTestConfig(ssl)->async && test_state->private_key_retries < 2) { |
| 1306 | // Only return the decryption on the second attempt, to test both incomplete |
| 1307 | // |sign|/|decrypt| and |complete|. |
| 1308 | return ssl_private_key_retry; |
| 1309 | } |
| 1310 |
| 1311 | if (max_out < test_state->private_key_result.size()) { |
| 1312 | fprintf(stderr, "Output buffer too small.\n"); |
| 1313 | return ssl_private_key_failure; |
| 1314 | } |
| 1315 | OPENSSL_memcpy(out, test_state->private_key_result.data(), |
| 1316 | test_state->private_key_result.size()); |
| 1317 | *out_len = test_state->private_key_result.size(); |
| 1318 |
| 1319 | test_state->private_key_result.clear(); |
| 1320 | test_state->private_key_retries = 0; |
| 1321 | return ssl_private_key_success; |
| 1322 | } |
| 1323 |
| 1324 | static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = { |
| 1325 | AsyncPrivateKeySign, |
| 1326 | AsyncPrivateKeyDecrypt, |
| 1327 | AsyncPrivateKeyComplete, |
| 1328 | }; |
| 1329 |
| 1330 | static size_t AsyncTicketMaxOverhead(SSL *ssl) { |
| 1331 | const EVP_AEAD *aead = EVP_aead_aes_128_gcm_siv(); |
| 1332 | return EVP_AEAD_max_overhead(aead) + EVP_AEAD_nonce_length(aead); |
| 1333 | } |
| 1334 |
| 1335 | static int AsyncTicketSeal(SSL *ssl, uint8_t *out, size_t *out_len, |
| 1336 | size_t max_out_len, const uint8_t *in, |
| 1337 | size_t in_len) { |
| 1338 | if (GetTestConfig(ssl)->skip_ticket) { |
| 1339 | *out_len = 0; |
| 1340 | return 1; |
| 1341 | } |
| 1342 |
| 1343 | auto out_span = bssl::Span(out, max_out_len); |
| 1344 | // Encrypt the ticket with the all zero key and a random nonce. |
| 1345 | static const uint8_t kKey[16] = {0}; |
| 1346 | const EVP_AEAD *aead = EVP_aead_aes_128_gcm_siv(); |
| 1347 | size_t nonce_len = EVP_AEAD_nonce_length(aead); |
| 1348 | if (out_span.size() < nonce_len) { |
| 1349 | return 0; |
| 1350 | } |
| 1351 | auto nonce = out_span.first(nonce_len); |
| 1352 | out_span = out_span.subspan(nonce_len); |
| 1353 | RAND_bytes(nonce.data(), nonce.size()); |
| 1354 | bssl::ScopedEVP_AEAD_CTX ctx; |
| 1355 | size_t len; |
| 1356 | if (!EVP_AEAD_CTX_init(ctx.get(), EVP_aead_aes_128_gcm_siv(), kKey, |
| 1357 | sizeof(kKey), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr) || |
| 1358 | !EVP_AEAD_CTX_seal(ctx.get(), out_span.data(), &len, out_span.size(), |
| 1359 | nonce.data(), nonce.size(), in, in_len, |
| 1360 | /*ad=*/nullptr, /*ad_len=*/0)) { |
| 1361 | return 0; |
| 1362 | } |
| 1363 | *out_len = nonce.size() + len; |
| 1364 | return 1; |
| 1365 | } |
| 1366 |
| 1367 | static ssl_ticket_aead_result_t AsyncTicketOpen(SSL *ssl, uint8_t *out, |
| 1368 | size_t *out_len, |
| 1369 | size_t max_out_len, |
| 1370 | const uint8_t *in, |
| 1371 | size_t in_len) { |
| 1372 | auto in_span = bssl::Span(in, in_len); |
| 1373 | const TestConfig *test_config = GetTestConfig(ssl); |
| 1374 | TestState *test_state = GetTestState(ssl); |
| 1375 | if (test_state->ticket_decrypt_done) { |
| 1376 | fprintf(stderr, "AsyncTicketOpen called after completion.\n"); |
| 1377 | return ssl_ticket_aead_error; |
| 1378 | } |
| 1379 | if (test_config->renew_ticket) { |
| 1380 | fprintf(stderr, "-renew-ticket not supported with async tickets.\n"); |
| 1381 | return ssl_ticket_aead_error; |
| 1382 | } |
| 1383 | if (test_config->async && !test_state->async_ticket_decrypt_ready) { |
| 1384 | return ssl_ticket_aead_retry; |
| 1385 | } |
| 1386 |
| 1387 | const EVP_AEAD *aead = EVP_aead_aes_128_gcm_siv(); |
| 1388 | size_t nonce_len = EVP_AEAD_nonce_length(aead); |
| 1389 | if (in_span.size() < nonce_len) { |
| 1390 | return ssl_ticket_aead_error; |
| 1391 | } |
| 1392 | auto nonce = in_span.first(nonce_len); |
| 1393 | in_span = in_span.subspan(nonce_len); |
| 1394 |
| 1395 | static const uint8_t kKey[16] = {0}; |
| 1396 | bssl::ScopedEVP_AEAD_CTX ctx; |
| 1397 | if (!EVP_AEAD_CTX_init(ctx.get(), EVP_aead_aes_128_gcm_siv(), kKey, |
| 1398 | sizeof(kKey), EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) { |
| 1399 | return ssl_ticket_aead_error; |
| 1400 | } |
| 1401 | if (!EVP_AEAD_CTX_open(ctx.get(), out, out_len, max_out_len, nonce.data(), |
| 1402 | nonce.size(), in_span.data(), in_span.size(), |
| 1403 | /*ad=*/nullptr, /*ad_len=*/0)) { |
| 1404 | ERR_clear_error(); |
| 1405 | return ssl_ticket_aead_ignore_ticket; |
| 1406 | } |
| 1407 | test_state->ticket_decrypt_done = true; |
| 1408 | return ssl_ticket_aead_success; |
| 1409 | } |
| 1410 |
| 1411 | static const SSL_TICKET_AEAD_METHOD g_async_ticket_aead_method = { |
| 1412 | AsyncTicketMaxOverhead, |
| 1413 | AsyncTicketSeal, |
| 1414 | AsyncTicketOpen, |
| 1415 | }; |
| 1416 |
| 1417 | static bssl::UniquePtr<SSL_CREDENTIAL> CredentialFromConfig( |
| 1418 | const TestConfig &config, const CredentialConfig &cred_config, int number) { |
| 1419 | bssl::UniquePtr<SSL_CREDENTIAL> cred; |
| 1420 | switch (cred_config.type) { |
| 1421 | case CredentialConfigType::kX509: |
| 1422 | cred.reset(SSL_CREDENTIAL_new_x509()); |
| 1423 | break; |
| 1424 | case CredentialConfigType::kDelegated: |
| 1425 | cred.reset(SSL_CREDENTIAL_new_delegated()); |
| 1426 | break; |
| 1427 | case CredentialConfigType::kSPAKE2PlusV1: { |
| 1428 | uint8_t pw_verifier_w0[32]; |
| 1429 | uint8_t pw_verifier_w1[32]; |
| 1430 | uint8_t registration_record[65]; |
| 1431 | if (!SSL_spake2plusv1_register(pw_verifier_w0, pw_verifier_w1, |
| 1432 | registration_record, |
| 1433 | cred_config.pake_password.data(), |
| 1434 | cred_config.pake_password.size(), |
| 1435 | cred_config.pake_client_id.data(), |
| 1436 | cred_config.pake_client_id.size(), |
| 1437 | cred_config.pake_server_id.data(), |
| 1438 | cred_config.pake_server_id.size())) { |
| 1439 | return nullptr; |
| 1440 | } |
| 1441 | bool is_server = |
| 1442 | cred_config.wrong_pake_role ? !config.is_server : config.is_server; |
| 1443 | if (is_server) { |
| 1444 | cred.reset(SSL_CREDENTIAL_new_spake2plusv1_server( |
| 1445 | cred_config.pake_context.data(), cred_config.pake_context.size(), |
| 1446 | cred_config.pake_client_id.data(), |
| 1447 | cred_config.pake_client_id.size(), |
| 1448 | cred_config.pake_server_id.data(), |
| 1449 | cred_config.pake_server_id.size(), |
| 1450 | /*attempts=*/1, pw_verifier_w0, sizeof(pw_verifier_w0), |
| 1451 | registration_record, sizeof(registration_record))); |
| 1452 | } else { |
| 1453 | cred.reset(SSL_CREDENTIAL_new_spake2plusv1_client( |
| 1454 | cred_config.pake_context.data(), cred_config.pake_context.size(), |
| 1455 | cred_config.pake_client_id.data(), |
| 1456 | cred_config.pake_client_id.size(), |
| 1457 | cred_config.pake_server_id.data(), |
| 1458 | cred_config.pake_server_id.size(), |
| 1459 | /*attempts=*/1, pw_verifier_w0, sizeof(pw_verifier_w0), |
| 1460 | pw_verifier_w1, sizeof(pw_verifier_w1))); |
| 1461 | } |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | if (cred == nullptr) { |
| 1466 | return nullptr; |
| 1467 | } |
| 1468 |
| 1469 | auto info = std::make_unique<CredentialInfo>(); |
| 1470 | info->number = number; |
| 1471 |
| 1472 | if (!cred_config.cert_file.empty()) { |
| 1473 | bssl::UniquePtr<X509> x509; |
| 1474 | bssl::UniquePtr<STACK_OF(X509)> chain; |
| 1475 | if (!LoadCertificate(&x509, &chain, cred_config.cert_file.c_str())) { |
| 1476 | return nullptr; |
| 1477 | } |
| 1478 | std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> buffers; |
| 1479 | buffers.push_back(X509ToBuffer(x509.get())); |
| 1480 | if (buffers.back() == nullptr) { |
| 1481 | return nullptr; |
| 1482 | } |
| 1483 | for (X509 *cert : chain.get()) { |
| 1484 | buffers.push_back(X509ToBuffer(cert)); |
| 1485 | if (buffers.back() == nullptr) { |
| 1486 | return nullptr; |
| 1487 | } |
| 1488 | } |
| 1489 | std::vector<CRYPTO_BUFFER *> buffers_raw; |
| 1490 | for (const auto &buffer : buffers) { |
| 1491 | buffers_raw.push_back(buffer.get()); |
| 1492 | } |
| 1493 | if (!SSL_CREDENTIAL_set1_cert_chain(cred.get(), buffers_raw.data(), |
| 1494 | buffers_raw.size())) { |
| 1495 | return nullptr; |
| 1496 | } |
| 1497 | } |
| 1498 |
| 1499 | if (!cred_config.key_file.empty()) { |
| 1500 | bssl::UniquePtr<EVP_PKEY> pkey = |
| 1501 | LoadPrivateKey(cred_config.key_file.c_str()); |
| 1502 | if (pkey == nullptr) { |
| 1503 | return nullptr; |
| 1504 | } |
| 1505 | if (config.async || config.handshake_hints) { |
| 1506 | info->private_key = std::move(pkey); |
| 1507 | if (!SSL_CREDENTIAL_set_private_key_method(cred.get(), |
| 1508 | &g_async_private_key_method)) { |
| 1509 | return nullptr; |
| 1510 | } |
| 1511 | } else { |
| 1512 | if (!SSL_CREDENTIAL_set1_private_key(cred.get(), pkey.get())) { |
| 1513 | return nullptr; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 |
| 1518 | if (!cred_config.signing_prefs.empty() && |
| 1519 | !SSL_CREDENTIAL_set1_signing_algorithm_prefs( |
| 1520 | cred.get(), cred_config.signing_prefs.data(), |
| 1521 | cred_config.signing_prefs.size())) { |
| 1522 | return nullptr; |
| 1523 | } |
| 1524 |
| 1525 | if (!cred_config.delegated_credential.empty()) { |
| 1526 | bssl::UniquePtr<CRYPTO_BUFFER> buf( |
| 1527 | CRYPTO_BUFFER_new(cred_config.delegated_credential.data(), |
| 1528 | cred_config.delegated_credential.size(), nullptr)); |
| 1529 | if (buf == nullptr || |
| 1530 | !SSL_CREDENTIAL_set1_delegated_credential(cred.get(), buf.get())) { |
| 1531 | return nullptr; |
| 1532 | } |
| 1533 | } |
| 1534 |
| 1535 | if (!cred_config.ocsp_response.empty()) { |
| 1536 | bssl::UniquePtr<CRYPTO_BUFFER> buf( |
| 1537 | CRYPTO_BUFFER_new(cred_config.ocsp_response.data(), |
| 1538 | cred_config.ocsp_response.size(), nullptr)); |
| 1539 | if (buf == nullptr || |
| 1540 | !SSL_CREDENTIAL_set1_ocsp_response(cred.get(), buf.get())) { |
| 1541 | return nullptr; |
| 1542 | } |
| 1543 | } |
| 1544 |
| 1545 | if (!cred_config.signed_cert_timestamps.empty()) { |
| 1546 | bssl::UniquePtr<CRYPTO_BUFFER> buf( |
| 1547 | CRYPTO_BUFFER_new(cred_config.signed_cert_timestamps.data(), |
| 1548 | cred_config.signed_cert_timestamps.size(), nullptr)); |
| 1549 | if (buf == nullptr || !SSL_CREDENTIAL_set1_signed_cert_timestamp_list( |
| 1550 | cred.get(), buf.get())) { |
| 1551 | return nullptr; |
| 1552 | } |
| 1553 | } |
| 1554 |
| 1555 | if (cred_config.must_match_issuer) { |
| 1556 | SSL_CREDENTIAL_set_must_match_issuer(cred.get(), 1); |
| 1557 | } |
| 1558 |
| 1559 | if (!cred_config.trust_anchor_id.empty()) { |
| 1560 | if (!SSL_CREDENTIAL_set1_trust_anchor_id( |
| 1561 | cred.get(), cred_config.trust_anchor_id.data(), |
| 1562 | cred_config.trust_anchor_id.size())) { |
| 1563 | return nullptr; |
| 1564 | } |
| 1565 | } |
| 1566 |
| 1567 | if (!SetCredentialInfo(cred.get(), std::move(info))) { |
| 1568 | return nullptr; |
| 1569 | } |
| 1570 |
| 1571 | return cred; |
| 1572 | } |
| 1573 |
| 1574 | static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509, |
| 1575 | bssl::UniquePtr<STACK_OF(X509)> *out_chain, |
| 1576 | bssl::UniquePtr<EVP_PKEY> *out_pkey) { |
| 1577 | const TestConfig *config = GetTestConfig(ssl); |
| 1578 |
| 1579 | if (!config->signing_prefs.empty()) { |
| 1580 | if (!SSL_set_signing_algorithm_prefs(ssl, config->signing_prefs.data(), |
| 1581 | config->signing_prefs.size())) { |
| 1582 | return false; |
| 1583 | } |
| 1584 | } |
| 1585 |
| 1586 | if (!config->key_file.empty()) { |
| 1587 | *out_pkey = LoadPrivateKey(config->key_file.c_str()); |
| 1588 | if (!*out_pkey) { |
| 1589 | return false; |
| 1590 | } |
| 1591 | } |
| 1592 | if (!config->cert_file.empty() && |
| 1593 | !LoadCertificate(out_x509, out_chain, config->cert_file.c_str())) { |
| 1594 | return false; |
| 1595 | } |
| 1596 | if (!config->ocsp_response.empty() && !config->set_ocsp_in_callback && |
| 1597 | !SSL_set_ocsp_response(ssl, (const uint8_t *)config->ocsp_response.data(), |
| 1598 | config->ocsp_response.size())) { |
| 1599 | return false; |
| 1600 | } |
| 1601 |
| 1602 | for (size_t i = 0; i < config->credentials.size(); i++) { |
| 1603 | bssl::UniquePtr<SSL_CREDENTIAL> cred = CredentialFromConfig( |
| 1604 | *config, config->credentials[i], static_cast<int>(i)); |
| 1605 | if (cred == nullptr || !SSL_add1_credential(ssl, cred.get())) { |
| 1606 | return false; |
| 1607 | } |
| 1608 | } |
| 1609 |
| 1610 | return true; |
| 1611 | } |
| 1612 |
| 1613 | static bool HexDecode(std::string *out, const std::string &in) { |
| 1614 | if ((in.size() & 1) != 0) { |
| 1615 | return false; |
| 1616 | } |
| 1617 |
| 1618 | auto buf = std::make_unique<uint8_t[]>(in.size() / 2); |
| 1619 | for (size_t i = 0; i < in.size() / 2; i++) { |
| 1620 | uint8_t high, low; |
| 1621 | if (!OPENSSL_fromxdigit(&high, in[i * 2]) || |
| 1622 | !OPENSSL_fromxdigit(&low, in[i * 2 + 1])) { |
| 1623 | return false; |
| 1624 | } |
| 1625 | buf[i] = (high << 4) | low; |
| 1626 | } |
| 1627 |
| 1628 | out->assign(reinterpret_cast<const char *>(buf.get()), in.size() / 2); |
| 1629 | return true; |
| 1630 | } |
| 1631 |
| 1632 | static std::vector<std::string> SplitParts(const std::string &in, |
| 1633 | const char delim) { |
| 1634 | std::vector<std::string> ret; |
| 1635 | size_t start = 0; |
| 1636 |
| 1637 | for (size_t i = 0; i < in.size(); i++) { |
| 1638 | if (in[i] == delim) { |
| 1639 | ret.push_back(in.substr(start, i - start)); |
| 1640 | start = i + 1; |
| 1641 | } |
| 1642 | } |
| 1643 |
| 1644 | ret.push_back(in.substr(start, std::string::npos)); |
| 1645 | return ret; |
| 1646 | } |
| 1647 |
| 1648 | static std::vector<std::string> DecodeHexStrings( |
| 1649 | const std::string &hex_strings) { |
| 1650 | std::vector<std::string> ret; |
| 1651 | const std::vector<std::string> parts = SplitParts(hex_strings, ','); |
| 1652 |
| 1653 | for (const auto &part : parts) { |
| 1654 | std::string binary; |
| 1655 | if (!HexDecode(&binary, part)) { |
| 1656 | fprintf(stderr, "Bad hex string: %s.\n", part.c_str()); |
| 1657 | return ret; |
| 1658 | } |
| 1659 |
| 1660 | ret.push_back(binary); |
| 1661 | } |
| 1662 |
| 1663 | return ret; |
| 1664 | } |
| 1665 |
| 1666 | static bssl::UniquePtr<STACK_OF(X509_NAME)> DecodeHexX509Names( |
| 1667 | const std::string &hex_names) { |
| 1668 | const std::vector<std::string> der_names = DecodeHexStrings(hex_names); |
| 1669 | bssl::UniquePtr<STACK_OF(X509_NAME)> ret(sk_X509_NAME_new_null()); |
| 1670 | if (!ret) { |
| 1671 | return nullptr; |
| 1672 | } |
| 1673 |
| 1674 | for (const auto &der_name : der_names) { |
| 1675 | const uint8_t *const data = |
| 1676 | reinterpret_cast<const uint8_t *>(der_name.data()); |
| 1677 | const uint8_t *derp = data; |
| 1678 | bssl::UniquePtr<X509_NAME> name( |
| 1679 | d2i_X509_NAME(nullptr, &derp, der_name.size())); |
| 1680 | if (!name || derp != data + der_name.size()) { |
| 1681 | fprintf(stderr, "Failed to parse X509_NAME.\n"); |
| 1682 | return nullptr; |
| 1683 | } |
| 1684 |
| 1685 | if (!bssl::PushToStack(ret.get(), std::move(name))) { |
| 1686 | return nullptr; |
| 1687 | } |
| 1688 | } |
| 1689 |
| 1690 | return ret; |
| 1691 | } |
| 1692 |
| 1693 | static bool CheckPeerVerifyPrefs(SSL *ssl) { |
| 1694 | const TestConfig *config = GetTestConfig(ssl); |
| 1695 | if (!config->expect_peer_verify_prefs.empty()) { |
| 1696 | const uint16_t *peer_sigalgs; |
| 1697 | size_t num_peer_sigalgs = |
| 1698 | SSL_get0_peer_verify_algorithms(ssl, &peer_sigalgs); |
| 1699 | if (config->expect_peer_verify_prefs.size() != num_peer_sigalgs) { |
| 1700 | fprintf(stderr, |
| 1701 | "peer verify preferences length mismatch (got %zu, wanted %zu)\n", |
| 1702 | num_peer_sigalgs, config->expect_peer_verify_prefs.size()); |
| 1703 | return false; |
| 1704 | } |
| 1705 | for (size_t i = 0; i < num_peer_sigalgs; i++) { |
| 1706 | if (peer_sigalgs[i] != config->expect_peer_verify_prefs[i]) { |
| 1707 | fprintf(stderr, |
| 1708 | "peer verify preference %zu mismatch (got %04x, wanted %04x\n", |
| 1709 | i, peer_sigalgs[i], config->expect_peer_verify_prefs[i]); |
| 1710 | return false; |
| 1711 | } |
| 1712 | } |
| 1713 | } |
| 1714 | return true; |
| 1715 | } |
| 1716 |
| 1717 | static bool CheckCertificateRequest(SSL *ssl) { |
| 1718 | const TestConfig *config = GetTestConfig(ssl); |
| 1719 |
| 1720 | if (!CheckPeerVerifyPrefs(ssl)) { |
| 1721 | return false; |
| 1722 | } |
| 1723 |
| 1724 | if (!config->expect_certificate_types.empty()) { |
| 1725 | const uint8_t *certificate_types; |
| 1726 | size_t certificate_types_len = |
| 1727 | SSL_get0_certificate_types(ssl, &certificate_types); |
| 1728 | if (bssl::Span(config->expect_certificate_types) != |
| 1729 | bssl::Span(certificate_types, certificate_types_len)) { |
| 1730 | fprintf(stderr, "certificate types mismatch.\n"); |
| 1731 | return false; |
| 1732 | } |
| 1733 | } |
| 1734 |
| 1735 | if (!config->expect_client_ca_list.empty()) { |
| 1736 | bssl::UniquePtr<STACK_OF(X509_NAME)> expected = |
| 1737 | DecodeHexX509Names(config->expect_client_ca_list); |
| 1738 | const size_t num_expected = sk_X509_NAME_num(expected.get()); |
| 1739 |
| 1740 | const STACK_OF(X509_NAME) *received = SSL_get_client_CA_list(ssl); |
| 1741 | const size_t num_received = sk_X509_NAME_num(received); |
| 1742 |
| 1743 | if (num_received != num_expected) { |
| 1744 | fprintf(stderr, "expected %zu names in CertificateRequest but got %zu.\n", |
| 1745 | num_expected, num_received); |
| 1746 | return false; |
| 1747 | } |
| 1748 |
| 1749 | for (size_t i = 0; i < num_received; i++) { |
| 1750 | if (X509_NAME_cmp(sk_X509_NAME_value(received, i), |
| 1751 | sk_X509_NAME_value(expected.get(), i)) != 0) { |
| 1752 | fprintf(stderr, "names in CertificateRequest differ at index #%zu.\n", |
| 1753 | i); |
| 1754 | return false; |
| 1755 | } |
| 1756 | } |
| 1757 |
| 1758 | const STACK_OF(CRYPTO_BUFFER) *buffers = SSL_get0_server_requested_CAs(ssl); |
| 1759 | if (sk_CRYPTO_BUFFER_num(buffers) != num_received) { |
| 1760 | fprintf(stderr, |
| 1761 | "Mismatch between SSL_get_server_requested_CAs and " |
| 1762 | "SSL_get_client_CA_list.\n"); |
| 1763 | return false; |
| 1764 | } |
| 1765 | } |
| 1766 |
| 1767 | return true; |
| 1768 | } |
| 1769 |
| 1770 | static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) { |
| 1771 | if (!CheckCertificateRequest(ssl)) { |
| 1772 | return -1; |
| 1773 | } |
| 1774 |
| 1775 | if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) { |
| 1776 | return -1; |
| 1777 | } |
| 1778 |
| 1779 | bssl::UniquePtr<X509> x509; |
| 1780 | bssl::UniquePtr<STACK_OF(X509)> chain; |
| 1781 | bssl::UniquePtr<EVP_PKEY> pkey; |
| 1782 | if (!GetCertificate(ssl, &x509, &chain, &pkey)) { |
| 1783 | return -1; |
| 1784 | } |
| 1785 |
| 1786 | // Return zero for no certificate. |
| 1787 | if (!x509) { |
| 1788 | return 0; |
| 1789 | } |
| 1790 |
| 1791 | // Chains and asynchronous private keys are not supported with client_cert_cb. |
| 1792 | *out_x509 = x509.release(); |
| 1793 | *out_pkey = pkey.release(); |
| 1794 | return 1; |
| 1795 | } |
| 1796 |
| 1797 | static bool InstallCertificate(SSL *ssl) { |
| 1798 | bssl::UniquePtr<X509> x509; |
| 1799 | bssl::UniquePtr<STACK_OF(X509)> chain; |
| 1800 | bssl::UniquePtr<EVP_PKEY> pkey; |
| 1801 | if (!GetCertificate(ssl, &x509, &chain, &pkey)) { |
| 1802 | return false; |
| 1803 | } |
| 1804 |
| 1805 | if (pkey) { |
| 1806 | TestState *test_state = GetTestState(ssl); |
| 1807 | const TestConfig *config = GetTestConfig(ssl); |
| 1808 | if (config->async || config->handshake_hints) { |
| 1809 | // Install a custom private key if testing asynchronous callbacks, or if |
| 1810 | // testing handshake hints. In the handshake hints case, we wish to check |
| 1811 | // that hints only mismatch when allowed. |
| 1812 | test_state->private_key = std::move(pkey); |
| 1813 | SSL_set_private_key_method(ssl, &g_async_private_key_method); |
| 1814 | } else if (!SSL_use_PrivateKey(ssl, pkey.get())) { |
| 1815 | return false; |
| 1816 | } |
| 1817 | } |
| 1818 |
| 1819 | if (x509 && !SSL_use_certificate(ssl, x509.get())) { |
| 1820 | return false; |
| 1821 | } |
| 1822 |
| 1823 | if (sk_X509_num(chain.get()) > 0 && !SSL_set1_chain(ssl, chain.get())) { |
| 1824 | return false; |
| 1825 | } |
| 1826 |
| 1827 | return true; |
| 1828 | } |
| 1829 |
| 1830 | static enum ssl_select_cert_result_t SelectCertificateCallback( |
| 1831 | const SSL_CLIENT_HELLO *client_hello) { |
| 1832 | SSL *ssl = client_hello->ssl; |
| 1833 | const TestConfig *config = GetTestConfig(ssl); |
| 1834 | TestState *test_state = GetTestState(ssl); |
| 1835 | test_state->early_callback_called = true; |
| 1836 |
| 1837 | // Invoke the rewind before we sanity check SNI because we will |
| 1838 | // end up calling the select_cert_cb twice with two different SNIs. |
| 1839 | if (SSL_ech_accepted(ssl) && config->fail_early_callback_ech_rewind) { |
| 1840 | return ssl_select_cert_disable_ech; |
| 1841 | } |
| 1842 |
| 1843 | const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 1844 |
| 1845 | if (config->expect_no_server_name && server_name != nullptr) { |
| 1846 | fprintf(stderr, "Expected no server name but got %s.\n", server_name); |
| 1847 | return ssl_select_cert_error; |
| 1848 | } |
| 1849 |
| 1850 | if (!config->expect_server_name.empty()) { |
| 1851 | if (server_name == nullptr || |
| 1852 | std::string(server_name) != config->expect_server_name) { |
| 1853 | fprintf(stderr, |
| 1854 | "Server name mismatch in early callback (got %s; want %s).\n", |
| 1855 | server_name, config->expect_server_name.c_str()); |
| 1856 | return ssl_select_cert_error; |
| 1857 | } |
| 1858 | } |
| 1859 |
| 1860 | if (config->fail_early_callback) { |
| 1861 | return ssl_select_cert_error; |
| 1862 | } |
| 1863 |
| 1864 | // Simulate some asynchronous work in the early callback. |
| 1865 | if ((config->use_early_callback || test_state->get_handshake_hints_cb) && |
| 1866 | config->async && !test_state->early_callback_ready) { |
| 1867 | return ssl_select_cert_retry; |
| 1868 | } |
| 1869 |
| 1870 | if (test_state->get_handshake_hints_cb && |
| 1871 | !test_state->get_handshake_hints_cb(client_hello)) { |
| 1872 | return ssl_select_cert_error; |
| 1873 | } |
| 1874 |
| 1875 | if (config->use_early_callback && !InstallCertificate(ssl)) { |
| 1876 | return ssl_select_cert_error; |
| 1877 | } |
| 1878 |
| 1879 | return ssl_select_cert_success; |
| 1880 | } |
| 1881 |
| 1882 | static int SetQuicReadSecret(SSL *ssl, enum ssl_encryption_level_t level, |
| 1883 | const SSL_CIPHER *cipher, const uint8_t *secret, |
| 1884 | size_t secret_len) { |
| 1885 | MockQuicTransport *quic_transport = GetTestState(ssl)->quic_transport.get(); |
| 1886 | if (quic_transport == nullptr) { |
| 1887 | fprintf(stderr, "No QUIC transport.\n"); |
| 1888 | return 0; |
| 1889 | } |
| 1890 | return quic_transport->SetReadSecret(level, cipher, secret, secret_len); |
| 1891 | } |
| 1892 |
| 1893 | static int SetQuicWriteSecret(SSL *ssl, enum ssl_encryption_level_t level, |
| 1894 | const SSL_CIPHER *cipher, const uint8_t *secret, |
| 1895 | size_t secret_len) { |
| 1896 | MockQuicTransport *quic_transport = GetTestState(ssl)->quic_transport.get(); |
| 1897 | if (quic_transport == nullptr) { |
| 1898 | fprintf(stderr, "No QUIC transport.\n"); |
| 1899 | return 0; |
| 1900 | } |
| 1901 | return quic_transport->SetWriteSecret(level, cipher, secret, secret_len); |
| 1902 | } |
| 1903 |
| 1904 | static int AddQuicHandshakeData(SSL *ssl, enum ssl_encryption_level_t level, |
| 1905 | const uint8_t *data, size_t len) { |
| 1906 | MockQuicTransport *quic_transport = GetTestState(ssl)->quic_transport.get(); |
| 1907 | if (quic_transport == nullptr) { |
| 1908 | fprintf(stderr, "No QUIC transport.\n"); |
| 1909 | return 0; |
| 1910 | } |
| 1911 | return quic_transport->WriteHandshakeData(level, data, len); |
| 1912 | } |
| 1913 |
| 1914 | static int FlushQuicFlight(SSL *ssl) { |
| 1915 | MockQuicTransport *quic_transport = GetTestState(ssl)->quic_transport.get(); |
| 1916 | if (quic_transport == nullptr) { |
| 1917 | fprintf(stderr, "No QUIC transport.\n"); |
| 1918 | return 0; |
| 1919 | } |
| 1920 | return quic_transport->Flush(); |
| 1921 | } |
| 1922 |
| 1923 | static int SendQuicAlert(SSL *ssl, enum ssl_encryption_level_t level, |
| 1924 | uint8_t alert) { |
| 1925 | MockQuicTransport *quic_transport = GetTestState(ssl)->quic_transport.get(); |
| 1926 | if (quic_transport == nullptr) { |
| 1927 | fprintf(stderr, "No QUIC transport.\n"); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | return quic_transport->SendAlert(level, alert); |
| 1931 | } |
| 1932 |
| 1933 | static const SSL_QUIC_METHOD g_quic_method = { |
| 1934 | SetQuicReadSecret, SetQuicWriteSecret, AddQuicHandshakeData, |
| 1935 | FlushQuicFlight, SendQuicAlert, |
| 1936 | }; |
| 1937 |
| 1938 | static bool MaybeInstallCertCompressionAlg( |
| 1939 | const TestConfig *config, SSL_CTX *ssl_ctx, uint16_t alg, |
| 1940 | ssl_cert_compression_func_t compress, |
| 1941 | ssl_cert_decompression_func_t decompress) { |
| 1942 | if (!config->install_cert_compression_algs && |
| 1943 | config->install_one_cert_compression_alg != alg) { |
| 1944 | return true; |
| 1945 | } |
| 1946 | return SSL_CTX_add_cert_compression_alg(ssl_ctx, alg, compress, decompress); |
| 1947 | } |
| 1948 |
| 1949 | bssl::UniquePtr<SSL_CTX> TestConfig::SetupCtx(SSL_CTX *old_ctx) const { |
| 1950 | bssl::UniquePtr<SSL_CTX> ssl_ctx( |
| 1951 | SSL_CTX_new(is_dtls ? DTLS_method() : TLS_method())); |
| 1952 | if (!ssl_ctx) { |
| 1953 | return nullptr; |
| 1954 | } |
| 1955 |
| 1956 | SSL_CTX_set0_buffer_pool(ssl_ctx.get(), BufferPool()); |
| 1957 |
| 1958 | std::string cipher_list = "ALL:TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"; |
| 1959 | if (!cipher.empty()) { |
| 1960 | cipher_list = cipher; |
| 1961 | SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 1962 | } |
| 1963 | if (!SSL_CTX_set_strict_cipher_list(ssl_ctx.get(), cipher_list.c_str())) { |
| 1964 | return nullptr; |
| 1965 | } |
| 1966 |
| 1967 | if (async && is_server) { |
| 1968 | // Disable the internal session cache. To test asynchronous session lookup, |
| 1969 | // we use an external session cache. |
| 1970 | SSL_CTX_set_session_cache_mode( |
| 1971 | ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL); |
| 1972 | SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback); |
| 1973 | } else { |
| 1974 | SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 1975 | } |
| 1976 |
| 1977 | SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback); |
| 1978 |
| 1979 | if (use_old_client_cert_callback) { |
| 1980 | SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback); |
| 1981 | } |
| 1982 |
| 1983 | SSL_CTX_set_next_protos_advertised_cb(ssl_ctx.get(), |
| 1984 | NextProtosAdvertisedCallback, NULL); |
| 1985 | if (!select_next_proto.empty() || select_empty_next_proto) { |
| 1986 | SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback, |
| 1987 | NULL); |
| 1988 | } |
| 1989 |
| 1990 | if (!select_alpn.empty() || decline_alpn || reject_alpn || |
| 1991 | select_empty_alpn) { |
| 1992 | SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL); |
| 1993 | } |
| 1994 |
| 1995 | SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback); |
| 1996 |
| 1997 | SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback); |
| 1998 | SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback); |
| 1999 |
| 2000 | if (use_ticket_aead_callback) { |
| 2001 | SSL_CTX_set_ticket_aead_method(ssl_ctx.get(), &g_async_ticket_aead_method); |
| 2002 | } else if (use_ticket_callback || handshake_hints) { |
| 2003 | // If using handshake hints, always enable some ticket callback, so we can |
| 2004 | // check that hints only mismatch when allowed. The ticket callback also |
| 2005 | // uses a constant key, which simplifies the test. |
| 2006 | SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback); |
| 2007 | } |
| 2008 |
| 2009 | if (!use_custom_verify_callback) { |
| 2010 | SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), CertVerifyCallback, NULL); |
| 2011 | } |
| 2012 |
| 2013 | if (!signed_cert_timestamps.empty() && |
| 2014 | !SSL_CTX_set_signed_cert_timestamp_list( |
| 2015 | ssl_ctx.get(), (const uint8_t *)signed_cert_timestamps.data(), |
| 2016 | signed_cert_timestamps.size())) { |
| 2017 | return nullptr; |
| 2018 | } |
| 2019 |
| 2020 | if (!use_client_ca_list.empty()) { |
| 2021 | if (use_client_ca_list == "<NULL>") { |
| 2022 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr); |
| 2023 | } else if (use_client_ca_list == "<EMPTY>") { |
| 2024 | bssl::UniquePtr<STACK_OF(X509_NAME)> names; |
| 2025 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), names.release()); |
| 2026 | } else { |
| 2027 | bssl::UniquePtr<STACK_OF(X509_NAME)> names = |
| 2028 | DecodeHexX509Names(use_client_ca_list); |
| 2029 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), names.release()); |
| 2030 | } |
| 2031 | } |
| 2032 |
| 2033 | if (enable_grease) { |
| 2034 | SSL_CTX_set_grease_enabled(ssl_ctx.get(), 1); |
| 2035 | } |
| 2036 |
| 2037 | if (permute_extensions) { |
| 2038 | SSL_CTX_set_permute_extensions(ssl_ctx.get(), 1); |
| 2039 | } |
| 2040 |
| 2041 | if (!expect_server_name.empty()) { |
| 2042 | SSL_CTX_set_tlsext_servername_callback(ssl_ctx.get(), ServerNameCallback); |
| 2043 | } |
| 2044 |
| 2045 | if (enable_early_data) { |
| 2046 | SSL_CTX_set_early_data_enabled(ssl_ctx.get(), 1); |
| 2047 | } |
| 2048 |
| 2049 | if (allow_unknown_alpn_protos) { |
| 2050 | SSL_CTX_set_allow_unknown_alpn_protos(ssl_ctx.get(), 1); |
| 2051 | } |
| 2052 |
| 2053 | if (!verify_prefs.empty()) { |
| 2054 | if (!SSL_CTX_set_verify_algorithm_prefs(ssl_ctx.get(), verify_prefs.data(), |
| 2055 | verify_prefs.size())) { |
| 2056 | return nullptr; |
| 2057 | } |
| 2058 | } |
| 2059 |
| 2060 | SSL_CTX_set_msg_callback(ssl_ctx.get(), MessageCallback); |
| 2061 |
| 2062 | if (allow_false_start_without_alpn) { |
| 2063 | SSL_CTX_set_false_start_allowed_without_alpn(ssl_ctx.get(), 1); |
| 2064 | } |
| 2065 |
| 2066 | if (use_ocsp_callback) { |
| 2067 | SSL_CTX_set_tlsext_status_cb(ssl_ctx.get(), LegacyOCSPCallback); |
| 2068 | } |
| 2069 |
| 2070 | if (resumption_across_names_enabled) { |
| 2071 | SSL_CTX_set_resumption_across_names_enabled(ssl_ctx.get(), 1); |
| 2072 | } |
| 2073 |
| 2074 | if (old_ctx) { |
| 2075 | uint8_t keys[48]; |
| 2076 | if (!SSL_CTX_get_tlsext_ticket_keys(old_ctx, &keys, sizeof(keys)) || |
| 2077 | !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), keys, sizeof(keys))) { |
| 2078 | return nullptr; |
| 2079 | } |
| 2080 | CopySessions(ssl_ctx.get(), old_ctx); |
| 2081 | } else if (!ticket_key.empty() && |
| 2082 | !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), ticket_key.data(), |
| 2083 | ticket_key.size())) { |
| 2084 | return nullptr; |
| 2085 | } |
| 2086 |
| 2087 | // These mock compression algorithms match the corresponding ones in |
| 2088 | // |addCertCompressionTests|. |
| 2089 | if (!MaybeInstallCertCompressionAlg( |
| 2090 | this, ssl_ctx.get(), 0xff02, |
| 2091 | [](SSL *ssl, CBB *out, const uint8_t *in, size_t in_len) -> int { |
| 2092 | if (!CBB_add_u8(out, 1) || !CBB_add_u8(out, 2) || |
| 2093 | !CBB_add_u8(out, 3) || !CBB_add_u8(out, 4) || |
| 2094 | !CBB_add_bytes(out, in, in_len)) { |
| 2095 | return 0; |
| 2096 | } |
| 2097 | return 1; |
| 2098 | }, |
| 2099 | [](SSL *ssl, CRYPTO_BUFFER **out, size_t uncompressed_len, |
| 2100 | const uint8_t *in, size_t in_len) -> int { |
| 2101 | if (in_len < 4 || in[0] != 1 || in[1] != 2 || in[2] != 3 || |
| 2102 | in[3] != 4 || uncompressed_len != in_len - 4) { |
| 2103 | return 0; |
| 2104 | } |
| 2105 | const bssl::Span<const uint8_t> uncompressed(in + 4, in_len - 4); |
| 2106 | *out = CRYPTO_BUFFER_new(uncompressed.data(), uncompressed.size(), |
| 2107 | nullptr); |
| 2108 | return *out != nullptr; |
| 2109 | }) || |
| 2110 | !MaybeInstallCertCompressionAlg( |
| 2111 | this, ssl_ctx.get(), 0xff01, |
| 2112 | [](SSL *ssl, CBB *out, const uint8_t *in, size_t in_len) -> int { |
| 2113 | if (in_len < 2 || in[0] != 0 || in[1] != 0) { |
| 2114 | return 0; |
| 2115 | } |
| 2116 | return CBB_add_bytes(out, in + 2, in_len - 2); |
| 2117 | }, |
| 2118 | [](SSL *ssl, CRYPTO_BUFFER **out, size_t uncompressed_len, |
| 2119 | const uint8_t *in, size_t in_len) -> int { |
| 2120 | if (uncompressed_len != 2 + in_len) { |
| 2121 | return 0; |
| 2122 | } |
| 2123 | auto buf = std::make_unique<uint8_t[]>(2 + in_len); |
| 2124 | buf[0] = 0; |
| 2125 | buf[1] = 0; |
| 2126 | OPENSSL_memcpy(&buf[2], in, in_len); |
| 2127 | *out = CRYPTO_BUFFER_new(buf.get(), 2 + in_len, nullptr); |
| 2128 | return *out != nullptr; |
| 2129 | }) || |
| 2130 | !MaybeInstallCertCompressionAlg( |
| 2131 | this, ssl_ctx.get(), 0xff03, |
| 2132 | [](SSL *ssl, CBB *out, const uint8_t *in, size_t in_len) -> int { |
| 2133 | uint8_t byte; |
| 2134 | return RAND_bytes(&byte, 1) && // |
| 2135 | CBB_add_u8(out, byte) && // |
| 2136 | CBB_add_bytes(out, in, in_len); |
| 2137 | }, |
| 2138 | [](SSL *ssl, CRYPTO_BUFFER **out, size_t uncompressed_len, |
| 2139 | const uint8_t *in, size_t in_len) -> int { |
| 2140 | if (uncompressed_len + 1 != in_len) { |
| 2141 | return 0; |
| 2142 | } |
| 2143 | *out = CRYPTO_BUFFER_new(in + 1, in_len - 1, nullptr); |
| 2144 | return *out != nullptr; |
| 2145 | })) { |
| 2146 | fprintf(stderr, "SSL_CTX_add_cert_compression_alg failed.\n"); |
| 2147 | abort(); |
| 2148 | } |
| 2149 |
| 2150 | if (server_preference) { |
| 2151 | SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 2152 | } |
| 2153 |
| 2154 | if (is_quic) { |
| 2155 | SSL_CTX_set_quic_method(ssl_ctx.get(), &g_quic_method); |
| 2156 | } |
| 2157 |
| 2158 | return ssl_ctx; |
| 2159 | } |
| 2160 |
| 2161 | static int DDoSCallback(const SSL_CLIENT_HELLO *client_hello) { |
| 2162 | const TestConfig *config = GetTestConfig(client_hello->ssl); |
| 2163 | return config->fail_ddos_callback ? 0 : 1; |
| 2164 | } |
| 2165 |
| 2166 | static unsigned PskClientCallback(SSL *ssl, const char *hint, |
| 2167 | char *out_identity, unsigned max_identity_len, |
| 2168 | uint8_t *out_psk, unsigned max_psk_len) { |
| 2169 | const TestConfig *config = GetTestConfig(ssl); |
| 2170 |
| 2171 | if (config->psk_identity.empty()) { |
| 2172 | if (hint != nullptr) { |
| 2173 | fprintf(stderr, "Server PSK hint was non-null.\n"); |
| 2174 | return 0; |
| 2175 | } |
| 2176 | } else if (hint == nullptr || |
| 2177 | strcmp(hint, config->psk_identity.c_str()) != 0) { |
| 2178 | fprintf(stderr, "Server PSK hint did not match.\n"); |
| 2179 | return 0; |
| 2180 | } |
| 2181 |
| 2182 | // Account for the trailing '\0' for the identity. |
| 2183 | if (config->psk_identity.size() >= max_identity_len || |
| 2184 | config->psk.size() > max_psk_len) { |
| 2185 | fprintf(stderr, "PSK buffers too small.\n"); |
| 2186 | return 0; |
| 2187 | } |
| 2188 |
| 2189 | OPENSSL_strlcpy(out_identity, config->psk_identity.c_str(), max_identity_len); |
| 2190 | OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 2191 | return static_cast<unsigned>(config->psk.size()); |
| 2192 | } |
| 2193 |
| 2194 | static unsigned PskServerCallback(SSL *ssl, const char *identity, |
| 2195 | uint8_t *out_psk, unsigned max_psk_len) { |
| 2196 | const TestConfig *config = GetTestConfig(ssl); |
| 2197 |
| 2198 | if (strcmp(identity, config->psk_identity.c_str()) != 0) { |
| 2199 | fprintf(stderr, "Client PSK identity did not match.\n"); |
| 2200 | return 0; |
| 2201 | } |
| 2202 |
| 2203 | if (config->psk.size() > max_psk_len) { |
| 2204 | fprintf(stderr, "PSK buffers too small.\n"); |
| 2205 | return 0; |
| 2206 | } |
| 2207 |
| 2208 | OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 2209 | return static_cast<unsigned>(config->psk.size()); |
| 2210 | } |
| 2211 |
| 2212 | static ssl_verify_result_t CustomVerifyCallback(SSL *ssl, uint8_t *out_alert) { |
| 2213 | const TestConfig *config = GetTestConfig(ssl); |
| 2214 | if (!CheckVerifyCallback(ssl)) { |
| 2215 | return ssl_verify_invalid; |
| 2216 | } |
| 2217 |
| 2218 | if (config->async && !GetTestState(ssl)->custom_verify_ready) { |
| 2219 | return ssl_verify_retry; |
| 2220 | } |
| 2221 |
| 2222 | GetTestState(ssl)->cert_verified = true; |
| 2223 | if (config->verify_fail) { |
| 2224 | return ssl_verify_invalid; |
| 2225 | } |
| 2226 |
| 2227 | return ssl_verify_ok; |
| 2228 | } |
| 2229 |
| 2230 | static int CertCallback(SSL *ssl, void *arg) { |
| 2231 | const TestConfig *config = GetTestConfig(ssl); |
| 2232 |
| 2233 | // Check the peer certificate metadata is as expected. |
| 2234 | if ((!SSL_is_server(ssl) && !CheckCertificateRequest(ssl)) || |
| 2235 | !CheckPeerVerifyPrefs(ssl)) { |
| 2236 | return -1; |
| 2237 | } |
| 2238 |
| 2239 | if (config->fail_cert_callback) { |
| 2240 | return 0; |
| 2241 | } |
| 2242 |
| 2243 | // The certificate will be installed via other means. |
| 2244 | if (!config->async || config->use_early_callback) { |
| 2245 | return 1; |
| 2246 | } |
| 2247 |
| 2248 | if (!GetTestState(ssl)->cert_ready) { |
| 2249 | return -1; |
| 2250 | } |
| 2251 | if (!InstallCertificate(ssl)) { |
| 2252 | return 0; |
| 2253 | } |
| 2254 | return 1; |
| 2255 | } |
| 2256 |
| 2257 | bssl::UniquePtr<SSL> TestConfig::NewSSL( |
| 2258 | SSL_CTX *ssl_ctx, SSL_SESSION *session, |
| 2259 | std::unique_ptr<TestState> test_state) const { |
| 2260 | bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx)); |
| 2261 | if (!ssl) { |
| 2262 | return nullptr; |
| 2263 | } |
| 2264 |
| 2265 | if (!SetTestConfig(ssl.get(), this)) { |
| 2266 | return nullptr; |
| 2267 | } |
| 2268 | if (test_state != nullptr) { |
| 2269 | if (!SetTestState(ssl.get(), std::move(test_state))) { |
| 2270 | return nullptr; |
| 2271 | } |
| 2272 | } |
| 2273 |
| 2274 | if (fallback_scsv && !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) { |
| 2275 | return nullptr; |
| 2276 | } |
| 2277 | // Install the certificate synchronously if nothing else will handle it. |
| 2278 | if (!use_early_callback && !use_old_client_cert_callback && !async && |
| 2279 | !InstallCertificate(ssl.get())) { |
| 2280 | return nullptr; |
| 2281 | } |
| 2282 | if (!use_old_client_cert_callback) { |
| 2283 | SSL_set_cert_cb(ssl.get(), CertCallback, nullptr); |
| 2284 | } |
| 2285 | int mode = SSL_VERIFY_NONE; |
| 2286 | if (require_any_client_certificate) { |
| 2287 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 2288 | } |
| 2289 | if (verify_peer) { |
| 2290 | mode = SSL_VERIFY_PEER; |
| 2291 | } |
| 2292 | if (use_custom_verify_callback) { |
| 2293 | SSL_set_custom_verify(ssl.get(), mode, CustomVerifyCallback); |
| 2294 | } else if (mode != SSL_VERIFY_NONE) { |
| 2295 | SSL_set_verify(ssl.get(), mode, NULL); |
| 2296 | } |
| 2297 | if (false_start) { |
| 2298 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START); |
| 2299 | } |
| 2300 | if (cbc_record_splitting) { |
| 2301 | SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING); |
| 2302 | } |
| 2303 | if (partial_write) { |
| 2304 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE); |
| 2305 | } |
| 2306 | if (reverify_on_resume) { |
| 2307 | SSL_CTX_set_reverify_on_resume(ssl_ctx, 1); |
| 2308 | } |
| 2309 | if (ignore_rsa_key_usage) { |
| 2310 | SSL_set_enforce_rsa_key_usage(ssl.get(), 0); |
| 2311 | } |
| 2312 | if (no_tls13) { |
| 2313 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3); |
| 2314 | } |
| 2315 | if (no_tls12) { |
| 2316 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2); |
| 2317 | } |
| 2318 | if (no_tls11) { |
| 2319 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1); |
| 2320 | } |
| 2321 | if (no_tls1) { |
| 2322 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1); |
| 2323 | } |
| 2324 | if (no_ticket) { |
| 2325 | SSL_set_options(ssl.get(), SSL_OP_NO_TICKET); |
| 2326 | } |
| 2327 | if (!expect_channel_id.empty() || enable_channel_id) { |
| 2328 | SSL_set_tls_channel_id_enabled(ssl.get(), 1); |
| 2329 | } |
| 2330 | if (enable_ech_grease) { |
| 2331 | SSL_set_enable_ech_grease(ssl.get(), 1); |
| 2332 | } |
| 2333 | if (static_cast<int>(fips_202205) + static_cast<int>(wpa_202304) + |
| 2334 | static_cast<int>(cnsa_202407) > |
| 2335 | 1) { |
| 2336 | fprintf(stderr, "Multiple policy options given\n"); |
| 2337 | return nullptr; |
| 2338 | } |
| 2339 | if (fips_202205 && !SSL_set_compliance_policy( |
| 2340 | ssl.get(), ssl_compliance_policy_fips_202205)) { |
| 2341 | fprintf(stderr, "SSL_set_compliance_policy failed\n"); |
| 2342 | return nullptr; |
| 2343 | } |
| 2344 | if (wpa_202304 && !SSL_set_compliance_policy( |
| 2345 | ssl.get(), ssl_compliance_policy_wpa3_192_202304)) { |
| 2346 | fprintf(stderr, "SSL_set_compliance_policy failed\n"); |
| 2347 | return nullptr; |
| 2348 | } |
| 2349 | if (cnsa_202407 && !SSL_set_compliance_policy( |
| 2350 | ssl.get(), ssl_compliance_policy_cnsa_202407)) { |
| 2351 | fprintf(stderr, "SSL_set_compliance_policy failed\n"); |
| 2352 | return nullptr; |
| 2353 | } |
| 2354 | if (!ech_config_list.empty() && |
| 2355 | !SSL_set1_ech_config_list(ssl.get(), ech_config_list.data(), |
| 2356 | ech_config_list.size())) { |
| 2357 | return nullptr; |
| 2358 | } |
| 2359 | if (ech_server_configs.size() != ech_server_keys.size() || |
| 2360 | ech_server_configs.size() != ech_is_retry_config.size()) { |
| 2361 | fprintf(stderr, |
| 2362 | "-ech-server-config, -ech-server-key, and -ech-is-retry-config " |
| 2363 | "flags must match.\n"); |
| 2364 | return nullptr; |
| 2365 | } |
| 2366 | if (!ech_server_configs.empty()) { |
| 2367 | bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 2368 | if (!keys) { |
| 2369 | return nullptr; |
| 2370 | } |
| 2371 | for (size_t i = 0; i < ech_server_configs.size(); i++) { |
| 2372 | bssl::Span<const uint8_t> ech_config = ech_server_configs[i]; |
| 2373 | bssl::Span<const uint8_t> ech_private_key = ech_server_keys[i]; |
| 2374 | const int is_retry_config = ech_is_retry_config[i]; |
| 2375 | bssl::ScopedEVP_HPKE_KEY key; |
| 2376 | if (!EVP_HPKE_KEY_init(key.get(), EVP_hpke_x25519_hkdf_sha256(), |
| 2377 | ech_private_key.data(), ech_private_key.size()) || |
| 2378 | !SSL_ECH_KEYS_add(keys.get(), is_retry_config, ech_config.data(), |
| 2379 | ech_config.size(), key.get())) { |
| 2380 | return nullptr; |
| 2381 | } |
| 2382 | } |
| 2383 | if (!SSL_CTX_set1_ech_keys(ssl_ctx, keys.get())) { |
| 2384 | return nullptr; |
| 2385 | } |
| 2386 | } |
| 2387 | if (!send_channel_id.empty()) { |
| 2388 | bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(send_channel_id); |
| 2389 | if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) { |
| 2390 | return nullptr; |
| 2391 | } |
| 2392 | } |
| 2393 | if (!host_name.empty() && |
| 2394 | !SSL_set_tlsext_host_name(ssl.get(), host_name.c_str())) { |
| 2395 | return nullptr; |
| 2396 | } |
| 2397 | if (!advertise_alpn.empty() && |
| 2398 | SSL_set_alpn_protos( |
| 2399 | ssl.get(), reinterpret_cast<const uint8_t *>(advertise_alpn.data()), |
| 2400 | advertise_alpn.size()) != 0) { |
| 2401 | return nullptr; |
| 2402 | } |
| 2403 | if (!defer_alps) { |
| 2404 | for (const auto &pair : application_settings) { |
| 2405 | if (!SSL_add_application_settings( |
| 2406 | ssl.get(), reinterpret_cast<const uint8_t *>(pair.first.data()), |
| 2407 | pair.first.size(), |
| 2408 | reinterpret_cast<const uint8_t *>(pair.second.data()), |
| 2409 | pair.second.size())) { |
| 2410 | return nullptr; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | if (!psk.empty()) { |
| 2415 | SSL_set_psk_client_callback(ssl.get(), PskClientCallback); |
| 2416 | SSL_set_psk_server_callback(ssl.get(), PskServerCallback); |
| 2417 | } |
| 2418 | if (!psk_identity.empty() && |
| 2419 | !SSL_use_psk_identity_hint(ssl.get(), psk_identity.c_str())) { |
| 2420 | return nullptr; |
| 2421 | } |
| 2422 | if (!srtp_profiles.empty() && |
| 2423 | !SSL_set_srtp_profiles(ssl.get(), srtp_profiles.c_str())) { |
| 2424 | return nullptr; |
| 2425 | } |
| 2426 | if (requested_trust_anchors.has_value() && |
| 2427 | !SSL_set1_requested_trust_anchors(ssl.get(), |
| 2428 | requested_trust_anchors->data(), |
| 2429 | requested_trust_anchors->size())) { |
| 2430 | return nullptr; |
| 2431 | } |
| 2432 | if (enable_ocsp_stapling) { |
| 2433 | SSL_enable_ocsp_stapling(ssl.get()); |
| 2434 | } |
| 2435 | if (enable_signed_cert_timestamps) { |
| 2436 | SSL_enable_signed_cert_timestamps(ssl.get()); |
| 2437 | } |
| 2438 | // (D)TLS 1.0 and 1.1 are disabled by default, but the runner expects them to |
| 2439 | // be enabled. |
| 2440 | // TODO(davidben): Update the tests to explicitly enable the versions they |
| 2441 | // need. |
| 2442 | if (!SSL_set_min_proto_version( |
| 2443 | ssl.get(), SSL_is_dtls(ssl.get()) ? DTLS1_VERSION : TLS1_VERSION)) { |
| 2444 | return nullptr; |
| 2445 | } |
| 2446 | if (min_version != 0 && !SSL_set_min_proto_version(ssl.get(), min_version)) { |
| 2447 | return nullptr; |
| 2448 | } |
| 2449 | // TODO(crbug.com/42290594): Remove this once DTLS 1.3 is enabled by default. |
| 2450 | if (is_dtls && max_version == 0 && |
| 2451 | !SSL_set_max_proto_version(ssl.get(), DTLS1_3_VERSION)) { |
| 2452 | return nullptr; |
| 2453 | } |
| 2454 | if (max_version != 0 && !SSL_set_max_proto_version(ssl.get(), max_version)) { |
| 2455 | return nullptr; |
| 2456 | } |
| 2457 | if (mtu != 0) { |
| 2458 | SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU); |
| 2459 | SSL_set_mtu(ssl.get(), mtu); |
| 2460 | } |
| 2461 | if (install_ddos_callback) { |
| 2462 | SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback); |
| 2463 | } |
| 2464 | SSL_set_shed_handshake_config(ssl.get(), true); |
| 2465 | if (renegotiate_once) { |
| 2466 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once); |
| 2467 | } |
| 2468 | if (renegotiate_freely || forbid_renegotiation_after_handshake) { |
| 2469 | // |forbid_renegotiation_after_handshake| will disable renegotiation later. |
| 2470 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely); |
| 2471 | } |
| 2472 | if (renegotiate_ignore) { |
| 2473 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore); |
| 2474 | } |
| 2475 | if (renegotiate_explicit) { |
| 2476 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_explicit); |
| 2477 | } |
| 2478 | if (!check_close_notify) { |
| 2479 | SSL_set_quiet_shutdown(ssl.get(), 1); |
| 2480 | } |
| 2481 | if (!curves.empty() && |
| 2482 | !SSL_set1_group_ids(ssl.get(), curves.data(), curves.size())) { |
| 2483 | return nullptr; |
| 2484 | } |
| 2485 | if (initial_timeout_duration_ms > 0) { |
| 2486 | DTLSv1_set_initial_timeout_duration(ssl.get(), initial_timeout_duration_ms); |
| 2487 | } |
| 2488 | if (max_cert_list > 0) { |
| 2489 | SSL_set_max_cert_list(ssl.get(), max_cert_list); |
| 2490 | } |
| 2491 | if (retain_only_sha256_client_cert) { |
| 2492 | SSL_set_retain_only_sha256_of_client_certs(ssl.get(), 1); |
| 2493 | } |
| 2494 | if (max_send_fragment > 0) { |
| 2495 | SSL_set_max_send_fragment(ssl.get(), max_send_fragment); |
| 2496 | } |
| 2497 | if (alps_use_new_codepoint) { |
| 2498 | SSL_set_alps_use_new_codepoint(ssl.get(), 1); |
| 2499 | } |
| 2500 | if (quic_use_legacy_codepoint != -1) { |
| 2501 | SSL_set_quic_use_legacy_codepoint(ssl.get(), quic_use_legacy_codepoint); |
| 2502 | } |
| 2503 | if (!quic_transport_params.empty()) { |
| 2504 | if (!SSL_set_quic_transport_params(ssl.get(), quic_transport_params.data(), |
| 2505 | quic_transport_params.size())) { |
| 2506 | return nullptr; |
| 2507 | } |
| 2508 | } |
| 2509 | if (jdk11_workaround) { |
| 2510 | SSL_set_jdk11_workaround(ssl.get(), 1); |
| 2511 | } |
| 2512 |
| 2513 | if (session != NULL) { |
| 2514 | if (!is_server) { |
| 2515 | if (SSL_set_session(ssl.get(), session) != 1) { |
| 2516 | return nullptr; |
| 2517 | } |
| 2518 | } else if (async) { |
| 2519 | // The internal session cache is disabled, so install the session |
| 2520 | // manually. |
| 2521 | SSL_SESSION_up_ref(session); |
| 2522 | GetTestState(ssl.get())->pending_session.reset(session); |
| 2523 | } |
| 2524 | } |
| 2525 |
| 2526 | if (!quic_early_data_context.empty() && |
| 2527 | !SSL_set_quic_early_data_context( |
| 2528 | ssl.get(), |
| 2529 | reinterpret_cast<const uint8_t *>(quic_early_data_context.data()), |
| 2530 | quic_early_data_context.size())) { |
| 2531 | return nullptr; |
| 2532 | } |
| 2533 |
| 2534 | return ssl; |
| 2535 | } |
| 2536 |