Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include "test_config.h" |
| 16 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 17 | #include <assert.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <memory> |
| 23 | |
| 24 | #include <openssl/base64.h> |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 25 | #include <openssl/rand.h> |
| 26 | #include <openssl/ssl.h> |
| 27 | |
| 28 | #include "../../crypto/internal.h" |
| 29 | #include "../internal.h" |
| 30 | #include "test_state.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
| 34 | template <typename T> |
| 35 | struct Flag { |
| 36 | const char *flag; |
| 37 | T TestConfig::*member; |
| 38 | }; |
| 39 | |
| 40 | // FindField looks for the flag in |flags| that matches |flag|. If one is found, |
| 41 | // it returns a pointer to the corresponding field in |config|. Otherwise, it |
| 42 | // returns NULL. |
| 43 | template<typename T, size_t N> |
| 44 | T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) { |
| 45 | for (size_t i = 0; i < N; i++) { |
| 46 | if (strcmp(flag, flags[i].flag) == 0) { |
| 47 | return &(config->*(flags[i].member)); |
| 48 | } |
| 49 | } |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | const Flag<bool> kBoolFlags[] = { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 54 | {"-server", &TestConfig::is_server}, |
| 55 | {"-dtls", &TestConfig::is_dtls}, |
| 56 | {"-fallback-scsv", &TestConfig::fallback_scsv}, |
| 57 | {"-require-any-client-certificate", |
| 58 | &TestConfig::require_any_client_certificate}, |
| 59 | {"-false-start", &TestConfig::false_start}, |
| 60 | {"-async", &TestConfig::async}, |
| 61 | {"-write-different-record-sizes", |
| 62 | &TestConfig::write_different_record_sizes}, |
| 63 | {"-cbc-record-splitting", &TestConfig::cbc_record_splitting}, |
| 64 | {"-partial-write", &TestConfig::partial_write}, |
| 65 | {"-no-tls13", &TestConfig::no_tls13}, |
| 66 | {"-no-tls12", &TestConfig::no_tls12}, |
| 67 | {"-no-tls11", &TestConfig::no_tls11}, |
| 68 | {"-no-tls1", &TestConfig::no_tls1}, |
| 69 | {"-no-ticket", &TestConfig::no_ticket}, |
| 70 | {"-enable-channel-id", &TestConfig::enable_channel_id}, |
| 71 | {"-shim-writes-first", &TestConfig::shim_writes_first}, |
| 72 | {"-expect-session-miss", &TestConfig::expect_session_miss}, |
| 73 | {"-decline-alpn", &TestConfig::decline_alpn}, |
| 74 | {"-select-empty-alpn", &TestConfig::select_empty_alpn}, |
| 75 | {"-expect-extended-master-secret", |
| 76 | &TestConfig::expect_extended_master_secret}, |
| 77 | {"-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling}, |
| 78 | {"-enable-signed-cert-timestamps", |
| 79 | &TestConfig::enable_signed_cert_timestamps}, |
| 80 | {"-implicit-handshake", &TestConfig::implicit_handshake}, |
| 81 | {"-use-early-callback", &TestConfig::use_early_callback}, |
| 82 | {"-fail-early-callback", &TestConfig::fail_early_callback}, |
| 83 | {"-install-ddos-callback", &TestConfig::install_ddos_callback}, |
| 84 | {"-fail-ddos-callback", &TestConfig::fail_ddos_callback}, |
| 85 | {"-fail-cert-callback", &TestConfig::fail_cert_callback}, |
| 86 | {"-handshake-never-done", &TestConfig::handshake_never_done}, |
| 87 | {"-use-export-context", &TestConfig::use_export_context}, |
| 88 | {"-tls-unique", &TestConfig::tls_unique}, |
| 89 | {"-expect-ticket-renewal", &TestConfig::expect_ticket_renewal}, |
| 90 | {"-expect-no-session", &TestConfig::expect_no_session}, |
| 91 | {"-expect-ticket-supports-early-data", |
| 92 | &TestConfig::expect_ticket_supports_early_data}, |
| 93 | {"-use-ticket-callback", &TestConfig::use_ticket_callback}, |
| 94 | {"-renew-ticket", &TestConfig::renew_ticket}, |
| 95 | {"-enable-early-data", &TestConfig::enable_early_data}, |
| 96 | {"-check-close-notify", &TestConfig::check_close_notify}, |
| 97 | {"-shim-shuts-down", &TestConfig::shim_shuts_down}, |
| 98 | {"-verify-fail", &TestConfig::verify_fail}, |
| 99 | {"-verify-peer", &TestConfig::verify_peer}, |
| 100 | {"-verify-peer-if-no-obc", &TestConfig::verify_peer_if_no_obc}, |
| 101 | {"-expect-verify-result", &TestConfig::expect_verify_result}, |
| 102 | {"-renegotiate-once", &TestConfig::renegotiate_once}, |
| 103 | {"-renegotiate-freely", &TestConfig::renegotiate_freely}, |
| 104 | {"-renegotiate-ignore", &TestConfig::renegotiate_ignore}, |
| 105 | {"-forbid-renegotiation-after-handshake", |
| 106 | &TestConfig::forbid_renegotiation_after_handshake}, |
| 107 | {"-enable-all-curves", &TestConfig::enable_all_curves}, |
| 108 | {"-use-old-client-cert-callback", |
| 109 | &TestConfig::use_old_client_cert_callback}, |
| 110 | {"-send-alert", &TestConfig::send_alert}, |
| 111 | {"-peek-then-read", &TestConfig::peek_then_read}, |
| 112 | {"-enable-grease", &TestConfig::enable_grease}, |
| 113 | {"-use-exporter-between-reads", &TestConfig::use_exporter_between_reads}, |
| 114 | {"-retain-only-sha256-client-cert", |
| 115 | &TestConfig::retain_only_sha256_client_cert}, |
| 116 | {"-expect-sha256-client-cert", &TestConfig::expect_sha256_client_cert}, |
| 117 | {"-read-with-unfinished-write", &TestConfig::read_with_unfinished_write}, |
| 118 | {"-expect-secure-renegotiation", &TestConfig::expect_secure_renegotiation}, |
| 119 | {"-expect-no-secure-renegotiation", |
| 120 | &TestConfig::expect_no_secure_renegotiation}, |
| 121 | {"-expect-session-id", &TestConfig::expect_session_id}, |
| 122 | {"-expect-no-session-id", &TestConfig::expect_no_session_id}, |
| 123 | {"-expect-accept-early-data", &TestConfig::expect_accept_early_data}, |
| 124 | {"-expect-reject-early-data", &TestConfig::expect_reject_early_data}, |
| 125 | {"-expect-no-offer-early-data", &TestConfig::expect_no_offer_early_data}, |
| 126 | {"-no-op-extra-handshake", &TestConfig::no_op_extra_handshake}, |
| 127 | {"-handshake-twice", &TestConfig::handshake_twice}, |
| 128 | {"-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos}, |
| 129 | {"-enable-ed25519", &TestConfig::enable_ed25519}, |
| 130 | {"-use-custom-verify-callback", &TestConfig::use_custom_verify_callback}, |
| 131 | {"-allow-false-start-without-alpn", |
| 132 | &TestConfig::allow_false_start_without_alpn}, |
| 133 | {"-ignore-tls13-downgrade", &TestConfig::ignore_tls13_downgrade}, |
| 134 | {"-expect-tls13-downgrade", &TestConfig::expect_tls13_downgrade}, |
| 135 | {"-handoff", &TestConfig::handoff}, |
| 136 | {"-no-rsa-pss-rsae-certs", &TestConfig::no_rsa_pss_rsae_certs}, |
| 137 | {"-use-ocsp-callback", &TestConfig::use_ocsp_callback}, |
| 138 | {"-set-ocsp-in-callback", &TestConfig::set_ocsp_in_callback}, |
| 139 | {"-decline-ocsp-callback", &TestConfig::decline_ocsp_callback}, |
| 140 | {"-fail-ocsp-callback", &TestConfig::fail_ocsp_callback}, |
| 141 | {"-install-cert-compression-algs", |
| 142 | &TestConfig::install_cert_compression_algs}, |
| 143 | {"-is-handshaker-supported", &TestConfig::is_handshaker_supported}, |
| 144 | {"-handshaker-resume", &TestConfig::handshaker_resume}, |
| 145 | {"-reverify-on-resume", &TestConfig::reverify_on_resume}, |
| 146 | {"-enforce-rsa-key-usage", &TestConfig::enforce_rsa_key_usage}, |
| 147 | {"-jdk11-workaround", &TestConfig::jdk11_workaround}, |
| 148 | {"-server-preference", &TestConfig::server_preference}, |
| 149 | {"-export-traffic-secrets", &TestConfig::export_traffic_secrets}, |
| 150 | {"-key-update", &TestConfig::key_update}, |
| 151 | {"-expect-delegated-credential-used", |
| 152 | &TestConfig::expect_delegated_credential_used}, |
| 153 | {"-enable-pq-experiment-signal", &TestConfig::enable_pq_experiment_signal}, |
| 154 | {"-expect-pq-experiment-signal", &TestConfig::expect_pq_experiment_signal}, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | const Flag<std::string> kStringFlags[] = { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 158 | {"-write-settings", &TestConfig::write_settings}, |
| 159 | {"-key-file", &TestConfig::key_file}, |
| 160 | {"-cert-file", &TestConfig::cert_file}, |
| 161 | {"-expect-server-name", &TestConfig::expect_server_name}, |
| 162 | {"-advertise-npn", &TestConfig::advertise_npn}, |
| 163 | {"-expect-next-proto", &TestConfig::expect_next_proto}, |
| 164 | {"-select-next-proto", &TestConfig::select_next_proto}, |
| 165 | {"-send-channel-id", &TestConfig::send_channel_id}, |
| 166 | {"-host-name", &TestConfig::host_name}, |
| 167 | {"-advertise-alpn", &TestConfig::advertise_alpn}, |
| 168 | {"-expect-alpn", &TestConfig::expect_alpn}, |
| 169 | {"-expect-late-alpn", &TestConfig::expect_late_alpn}, |
| 170 | {"-expect-advertised-alpn", &TestConfig::expect_advertised_alpn}, |
| 171 | {"-select-alpn", &TestConfig::select_alpn}, |
| 172 | {"-psk", &TestConfig::psk}, |
| 173 | {"-psk-identity", &TestConfig::psk_identity}, |
| 174 | {"-srtp-profiles", &TestConfig::srtp_profiles}, |
| 175 | {"-cipher", &TestConfig::cipher}, |
| 176 | {"-export-label", &TestConfig::export_label}, |
| 177 | {"-export-context", &TestConfig::export_context}, |
| 178 | {"-expect-peer-cert-file", &TestConfig::expect_peer_cert_file}, |
| 179 | {"-use-client-ca-list", &TestConfig::use_client_ca_list}, |
| 180 | {"-expect-client-ca-list", &TestConfig::expect_client_ca_list}, |
| 181 | {"-expect-msg-callback", &TestConfig::expect_msg_callback}, |
| 182 | {"-handshaker-path", &TestConfig::handshaker_path}, |
| 183 | {"-delegated-credential", &TestConfig::delegated_credential}, |
| 184 | {"-expect-early-data-reason", &TestConfig::expect_early_data_reason}, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | const Flag<std::string> kBase64Flags[] = { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 188 | {"-expect-certificate-types", &TestConfig::expect_certificate_types}, |
| 189 | {"-expect-channel-id", &TestConfig::expect_channel_id}, |
| 190 | {"-token-binding-params", &TestConfig::send_token_binding_params}, |
| 191 | {"-expect-ocsp-response", &TestConfig::expect_ocsp_response}, |
| 192 | {"-expect-signed-cert-timestamps", |
| 193 | &TestConfig::expect_signed_cert_timestamps}, |
| 194 | {"-ocsp-response", &TestConfig::ocsp_response}, |
| 195 | {"-signed-cert-timestamps", &TestConfig::signed_cert_timestamps}, |
| 196 | {"-ticket-key", &TestConfig::ticket_key}, |
| 197 | {"-quic-transport-params", &TestConfig::quic_transport_params}, |
| 198 | {"-expect-quic-transport-params", |
| 199 | &TestConfig::expect_quic_transport_params}, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | const Flag<int> kIntFlags[] = { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 203 | {"-port", &TestConfig::port}, |
| 204 | {"-resume-count", &TestConfig::resume_count}, |
| 205 | {"-expect-token-binding-param", &TestConfig::expect_token_binding_param}, |
| 206 | {"-min-version", &TestConfig::min_version}, |
| 207 | {"-max-version", &TestConfig::max_version}, |
| 208 | {"-expect-version", &TestConfig::expect_version}, |
| 209 | {"-mtu", &TestConfig::mtu}, |
| 210 | {"-export-keying-material", &TestConfig::export_keying_material}, |
| 211 | {"-expect-total-renegotiations", &TestConfig::expect_total_renegotiations}, |
| 212 | {"-expect-peer-signature-algorithm", |
| 213 | &TestConfig::expect_peer_signature_algorithm}, |
| 214 | {"-expect-curve-id", &TestConfig::expect_curve_id}, |
| 215 | {"-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms}, |
| 216 | {"-max-cert-list", &TestConfig::max_cert_list}, |
| 217 | {"-expect-cipher-aes", &TestConfig::expect_cipher_aes}, |
| 218 | {"-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes}, |
| 219 | {"-resumption-delay", &TestConfig::resumption_delay}, |
| 220 | {"-max-send-fragment", &TestConfig::max_send_fragment}, |
| 221 | {"-read-size", &TestConfig::read_size}, |
| 222 | {"-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew}, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 223 | }; |
| 224 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 225 | const Flag<std::vector<int>> kIntVectorFlags[] = { |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 226 | {"-signing-prefs", &TestConfig::signing_prefs}, |
| 227 | {"-verify-prefs", &TestConfig::verify_prefs}, |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 228 | {"-expect-peer-verify-pref", &TestConfig::expect_peer_verify_prefs}, |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 229 | {"-curves", &TestConfig::curves}, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 230 | }; |
| 231 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 232 | bool ParseFlag(char *flag, int argc, char **argv, int *i, |
| 233 | bool skip, TestConfig *out_config) { |
| 234 | bool *bool_field = FindField(out_config, kBoolFlags, flag); |
| 235 | if (bool_field != NULL) { |
| 236 | if (!skip) { |
| 237 | *bool_field = true; |
| 238 | } |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | std::string *string_field = FindField(out_config, kStringFlags, flag); |
| 243 | if (string_field != NULL) { |
| 244 | *i = *i + 1; |
| 245 | if (*i >= argc) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 246 | fprintf(stderr, "Missing parameter.\n"); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | if (!skip) { |
| 250 | string_field->assign(argv[*i]); |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | std::string *base64_field = FindField(out_config, kBase64Flags, flag); |
| 256 | if (base64_field != NULL) { |
| 257 | *i = *i + 1; |
| 258 | if (*i >= argc) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 259 | fprintf(stderr, "Missing parameter.\n"); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | size_t len; |
| 263 | if (!EVP_DecodedLength(&len, strlen(argv[*i]))) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 264 | fprintf(stderr, "Invalid base64: %s.\n", argv[*i]); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]); |
| 268 | if (!EVP_DecodeBase64(decoded.get(), &len, len, |
| 269 | reinterpret_cast<const uint8_t *>(argv[*i]), |
| 270 | strlen(argv[*i]))) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 271 | fprintf(stderr, "Invalid base64: %s.\n", argv[*i]); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | if (!skip) { |
| 275 | base64_field->assign(reinterpret_cast<const char *>(decoded.get()), |
| 276 | len); |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | int *int_field = FindField(out_config, kIntFlags, flag); |
| 282 | if (int_field) { |
| 283 | *i = *i + 1; |
| 284 | if (*i >= argc) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 285 | fprintf(stderr, "Missing parameter.\n"); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | if (!skip) { |
| 289 | *int_field = atoi(argv[*i]); |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | std::vector<int> *int_vector_field = |
| 295 | FindField(out_config, kIntVectorFlags, flag); |
| 296 | if (int_vector_field) { |
| 297 | *i = *i + 1; |
| 298 | if (*i >= argc) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 299 | fprintf(stderr, "Missing parameter.\n"); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
| 303 | // Each instance of the flag adds to the list. |
| 304 | if (!skip) { |
| 305 | int_vector_field->push_back(atoi(argv[*i])); |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 310 | fprintf(stderr, "Unknown argument: %s.\n", flag); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 314 | const char kInit[] = "-on-initial"; |
| 315 | const char kResume[] = "-on-resume"; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 316 | const char kRetry[] = "-on-retry"; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 317 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 318 | } // namespace |
| 319 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 320 | bool ParseConfig(int argc, char **argv, |
| 321 | TestConfig *out_initial, |
| 322 | TestConfig *out_resume, |
| 323 | TestConfig *out_retry) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 324 | out_initial->argc = out_resume->argc = out_retry->argc = argc; |
| 325 | out_initial->argv = out_resume->argv = out_retry->argv = argv; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 326 | for (int i = 0; i < argc; i++) { |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 327 | bool skip = false; |
| 328 | char *flag = argv[i]; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 329 | if (strncmp(flag, kInit, strlen(kInit)) == 0) { |
| 330 | if (!ParseFlag(flag + strlen(kInit), argc, argv, &i, skip, out_initial)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 331 | return false; |
| 332 | } |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 333 | } else if (strncmp(flag, kResume, strlen(kResume)) == 0) { |
| 334 | if (!ParseFlag(flag + strlen(kResume), argc, argv, &i, skip, |
| 335 | out_resume)) { |
| 336 | return false; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 337 | } |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 338 | } else if (strncmp(flag, kRetry, strlen(kRetry)) == 0) { |
| 339 | if (!ParseFlag(flag + strlen(kRetry), argc, argv, &i, skip, out_retry)) { |
| 340 | return false; |
| 341 | } |
| 342 | } else { |
| 343 | int i_init = i; |
| 344 | int i_resume = i; |
| 345 | if (!ParseFlag(flag, argc, argv, &i_init, skip, out_initial) || |
| 346 | !ParseFlag(flag, argc, argv, &i_resume, skip, out_resume) || |
| 347 | !ParseFlag(flag, argc, argv, &i, skip, out_retry)) { |
| 348 | return false; |
| 349 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 350 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | return true; |
| 354 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 355 | |
| 356 | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
| 357 | static int g_config_index = 0; |
| 358 | static CRYPTO_BUFFER_POOL *g_pool = nullptr; |
| 359 | |
| 360 | static void init_once() { |
| 361 | g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 362 | if (g_config_index < 0) { |
| 363 | abort(); |
| 364 | } |
| 365 | g_pool = CRYPTO_BUFFER_POOL_new(); |
| 366 | if (!g_pool) { |
| 367 | abort(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | bool SetTestConfig(SSL *ssl, const TestConfig *config) { |
| 372 | CRYPTO_once(&once, init_once); |
| 373 | return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1; |
| 374 | } |
| 375 | |
| 376 | const TestConfig *GetTestConfig(const SSL *ssl) { |
| 377 | CRYPTO_once(&once, init_once); |
| 378 | return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index); |
| 379 | } |
| 380 | |
| 381 | static int LegacyOCSPCallback(SSL *ssl, void *arg) { |
| 382 | const TestConfig *config = GetTestConfig(ssl); |
| 383 | if (!SSL_is_server(ssl)) { |
| 384 | return !config->fail_ocsp_callback; |
| 385 | } |
| 386 | |
| 387 | if (!config->ocsp_response.empty() && config->set_ocsp_in_callback && |
| 388 | !SSL_set_ocsp_response(ssl, (const uint8_t *)config->ocsp_response.data(), |
| 389 | config->ocsp_response.size())) { |
| 390 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 391 | } |
| 392 | if (config->fail_ocsp_callback) { |
| 393 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 394 | } |
| 395 | if (config->decline_ocsp_callback) { |
| 396 | return SSL_TLSEXT_ERR_NOACK; |
| 397 | } |
| 398 | return SSL_TLSEXT_ERR_OK; |
| 399 | } |
| 400 | |
| 401 | static int ServerNameCallback(SSL *ssl, int *out_alert, void *arg) { |
| 402 | // SNI must be accessible from the SNI callback. |
| 403 | const TestConfig *config = GetTestConfig(ssl); |
| 404 | const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 405 | if (server_name == nullptr || |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 406 | std::string(server_name) != config->expect_server_name) { |
| 407 | fprintf(stderr, "servername mismatch (got %s; want %s).\n", server_name, |
| 408 | config->expect_server_name.c_str()); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 409 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 410 | } |
| 411 | |
| 412 | return SSL_TLSEXT_ERR_OK; |
| 413 | } |
| 414 | |
| 415 | static int NextProtoSelectCallback(SSL *ssl, uint8_t **out, uint8_t *outlen, |
| 416 | const uint8_t *in, unsigned inlen, |
| 417 | void *arg) { |
| 418 | const TestConfig *config = GetTestConfig(ssl); |
| 419 | if (config->select_next_proto.empty()) { |
| 420 | return SSL_TLSEXT_ERR_NOACK; |
| 421 | } |
| 422 | |
| 423 | *out = (uint8_t *)config->select_next_proto.data(); |
| 424 | *outlen = config->select_next_proto.size(); |
| 425 | return SSL_TLSEXT_ERR_OK; |
| 426 | } |
| 427 | |
| 428 | static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out, |
| 429 | unsigned int *out_len, void *arg) { |
| 430 | const TestConfig *config = GetTestConfig(ssl); |
| 431 | if (config->advertise_npn.empty()) { |
| 432 | return SSL_TLSEXT_ERR_NOACK; |
| 433 | } |
| 434 | |
| 435 | *out = (const uint8_t *)config->advertise_npn.data(); |
| 436 | *out_len = config->advertise_npn.size(); |
| 437 | return SSL_TLSEXT_ERR_OK; |
| 438 | } |
| 439 | |
| 440 | static void MessageCallback(int is_write, int version, int content_type, |
| 441 | const void *buf, size_t len, SSL *ssl, void *arg) { |
| 442 | const uint8_t *buf_u8 = reinterpret_cast<const uint8_t *>(buf); |
| 443 | const TestConfig *config = GetTestConfig(ssl); |
| 444 | TestState *state = GetTestState(ssl); |
| 445 | if (!state->msg_callback_ok) { |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | if (content_type == SSL3_RT_HEADER) { |
| 450 | if (len != |
| 451 | (config->is_dtls ? DTLS1_RT_HEADER_LENGTH : SSL3_RT_HEADER_LENGTH)) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 452 | fprintf(stderr, "Incorrect length for record header: %zu.\n", len); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 453 | state->msg_callback_ok = false; |
| 454 | } |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | state->msg_callback_text += is_write ? "write " : "read "; |
| 459 | switch (content_type) { |
| 460 | case 0: |
| 461 | if (version != SSL2_VERSION) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 462 | fprintf(stderr, "Incorrect version for V2ClientHello: %x.\n", version); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 463 | state->msg_callback_ok = false; |
| 464 | return; |
| 465 | } |
| 466 | state->msg_callback_text += "v2clienthello\n"; |
| 467 | return; |
| 468 | |
| 469 | case SSL3_RT_HANDSHAKE: { |
| 470 | CBS cbs; |
| 471 | CBS_init(&cbs, buf_u8, len); |
| 472 | uint8_t type; |
| 473 | uint32_t msg_len; |
| 474 | if (!CBS_get_u8(&cbs, &type) || |
| 475 | // TODO(davidben): Reporting on entire messages would be more |
| 476 | // consistent than fragments. |
| 477 | (config->is_dtls && |
| 478 | !CBS_skip(&cbs, 3 /* total */ + 2 /* seq */ + 3 /* frag_off */)) || |
| 479 | !CBS_get_u24(&cbs, &msg_len) || !CBS_skip(&cbs, msg_len) || |
| 480 | CBS_len(&cbs) != 0) { |
| 481 | fprintf(stderr, "Could not parse handshake message.\n"); |
| 482 | state->msg_callback_ok = false; |
| 483 | return; |
| 484 | } |
| 485 | char text[16]; |
| 486 | snprintf(text, sizeof(text), "hs %d\n", type); |
| 487 | state->msg_callback_text += text; |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
| 492 | if (len != 1 || buf_u8[0] != 1) { |
| 493 | fprintf(stderr, "Invalid ChangeCipherSpec.\n"); |
| 494 | state->msg_callback_ok = false; |
| 495 | return; |
| 496 | } |
| 497 | state->msg_callback_text += "ccs\n"; |
| 498 | return; |
| 499 | |
| 500 | case SSL3_RT_ALERT: |
| 501 | if (len != 2) { |
| 502 | fprintf(stderr, "Invalid alert.\n"); |
| 503 | state->msg_callback_ok = false; |
| 504 | return; |
| 505 | } |
| 506 | char text[16]; |
| 507 | snprintf(text, sizeof(text), "alert %d %d\n", buf_u8[0], buf_u8[1]); |
| 508 | state->msg_callback_text += text; |
| 509 | return; |
| 510 | |
| 511 | default: |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 512 | fprintf(stderr, "Invalid content_type: %d.\n", content_type); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 513 | state->msg_callback_ok = false; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 518 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 519 | int encrypt) { |
| 520 | if (!encrypt) { |
| 521 | if (GetTestState(ssl)->ticket_decrypt_done) { |
| 522 | fprintf(stderr, "TicketKeyCallback called after completion.\n"); |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | GetTestState(ssl)->ticket_decrypt_done = true; |
| 527 | } |
| 528 | |
| 529 | // This is just test code, so use the all-zeros key. |
| 530 | static const uint8_t kZeros[16] = {0}; |
| 531 | |
| 532 | if (encrypt) { |
| 533 | OPENSSL_memcpy(key_name, kZeros, sizeof(kZeros)); |
| 534 | RAND_bytes(iv, 16); |
| 535 | } else if (OPENSSL_memcmp(key_name, kZeros, 16) != 0) { |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) || |
| 540 | !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) { |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | if (!encrypt) { |
| 545 | return GetTestConfig(ssl)->renew_ticket ? 2 : 1; |
| 546 | } |
| 547 | return 1; |
| 548 | } |
| 549 | |
| 550 | static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) { |
| 551 | // This callback is called as the handshake completes. |SSL_get_session| |
| 552 | // must continue to work and, historically, |SSL_in_init| returned false at |
| 553 | // this point. |
| 554 | if (SSL_in_init(ssl) || SSL_get_session(ssl) == nullptr) { |
| 555 | fprintf(stderr, "Invalid state for NewSessionCallback.\n"); |
| 556 | abort(); |
| 557 | } |
| 558 | |
| 559 | GetTestState(ssl)->got_new_session = true; |
| 560 | GetTestState(ssl)->new_session.reset(session); |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | static void InfoCallback(const SSL *ssl, int type, int val) { |
| 565 | if (type == SSL_CB_HANDSHAKE_DONE) { |
| 566 | if (GetTestConfig(ssl)->handshake_never_done) { |
| 567 | fprintf(stderr, "Handshake unexpectedly completed.\n"); |
| 568 | // Abort before any expected error code is printed, to ensure the overall |
| 569 | // test fails. |
| 570 | abort(); |
| 571 | } |
| 572 | // This callback is called when the handshake completes. |SSL_get_session| |
| 573 | // must continue to work and |SSL_in_init| must return false. |
| 574 | if (SSL_in_init(ssl) || SSL_get_session(ssl) == nullptr) { |
| 575 | fprintf(stderr, "Invalid state for SSL_CB_HANDSHAKE_DONE.\n"); |
| 576 | abort(); |
| 577 | } |
| 578 | GetTestState(ssl)->handshake_done = true; |
| 579 | |
| 580 | // Callbacks may be called again on a new handshake. |
| 581 | GetTestState(ssl)->ticket_decrypt_done = false; |
| 582 | GetTestState(ssl)->alpn_select_done = false; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static void ChannelIdCallback(SSL *ssl, EVP_PKEY **out_pkey) { |
| 587 | *out_pkey = GetTestState(ssl)->channel_id.release(); |
| 588 | } |
| 589 | |
| 590 | static SSL_SESSION *GetSessionCallback(SSL *ssl, const uint8_t *data, int len, |
| 591 | int *copy) { |
| 592 | TestState *async_state = GetTestState(ssl); |
| 593 | if (async_state->session) { |
| 594 | *copy = 0; |
| 595 | return async_state->session.release(); |
| 596 | } else if (async_state->pending_session) { |
| 597 | return SSL_magic_pending_session_ptr(); |
| 598 | } else { |
| 599 | return NULL; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) { |
| 604 | *out_clock = *GetClock(); |
| 605 | } |
| 606 | |
| 607 | static int AlpnSelectCallback(SSL *ssl, const uint8_t **out, uint8_t *outlen, |
| 608 | const uint8_t *in, unsigned inlen, void *arg) { |
| 609 | if (GetTestState(ssl)->alpn_select_done) { |
| 610 | fprintf(stderr, "AlpnSelectCallback called after completion.\n"); |
| 611 | exit(1); |
| 612 | } |
| 613 | |
| 614 | GetTestState(ssl)->alpn_select_done = true; |
| 615 | |
| 616 | const TestConfig *config = GetTestConfig(ssl); |
| 617 | if (config->decline_alpn) { |
| 618 | return SSL_TLSEXT_ERR_NOACK; |
| 619 | } |
| 620 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 621 | if (!config->expect_advertised_alpn.empty() && |
| 622 | (config->expect_advertised_alpn.size() != inlen || |
| 623 | OPENSSL_memcmp(config->expect_advertised_alpn.data(), in, inlen) != |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 624 | 0)) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 625 | fprintf(stderr, "bad ALPN select callback inputs.\n"); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 626 | exit(1); |
| 627 | } |
| 628 | |
| 629 | assert(config->select_alpn.empty() || !config->select_empty_alpn); |
| 630 | *out = (const uint8_t *)config->select_alpn.data(); |
| 631 | *outlen = config->select_alpn.size(); |
| 632 | return SSL_TLSEXT_ERR_OK; |
| 633 | } |
| 634 | |
| 635 | static bool CheckVerifyCallback(SSL *ssl) { |
| 636 | const TestConfig *config = GetTestConfig(ssl); |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 637 | if (!config->expect_ocsp_response.empty()) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 638 | const uint8_t *data; |
| 639 | size_t len; |
| 640 | SSL_get0_ocsp_response(ssl, &data, &len); |
| 641 | if (len == 0) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 642 | fprintf(stderr, "OCSP response not available in verify callback.\n"); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 643 | return false; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (GetTestState(ssl)->cert_verified) { |
| 648 | fprintf(stderr, "Certificate verified twice.\n"); |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
| 656 | SSL *ssl = (SSL *)X509_STORE_CTX_get_ex_data( |
| 657 | store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 658 | const TestConfig *config = GetTestConfig(ssl); |
| 659 | if (!CheckVerifyCallback(ssl)) { |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | GetTestState(ssl)->cert_verified = true; |
| 664 | if (config->verify_fail) { |
| 665 | store_ctx->error = X509_V_ERR_APPLICATION_VERIFICATION; |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | bool LoadCertificate(bssl::UniquePtr<X509> *out_x509, |
| 673 | bssl::UniquePtr<STACK_OF(X509)> *out_chain, |
| 674 | const std::string &file) { |
| 675 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); |
| 676 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | out_x509->reset(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 681 | if (!*out_x509) { |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | out_chain->reset(sk_X509_new_null()); |
| 686 | if (!*out_chain) { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | // Keep reading the certificate chain. |
| 691 | for (;;) { |
| 692 | bssl::UniquePtr<X509> cert( |
| 693 | PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); |
| 694 | if (!cert) { |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | if (!bssl::PushToStack(out_chain->get(), std::move(cert))) { |
| 699 | return false; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | uint32_t err = ERR_peek_last_error(); |
| 704 | if (ERR_GET_LIB(err) != ERR_LIB_PEM || |
| 705 | ERR_GET_REASON(err) != PEM_R_NO_START_LINE) { |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | ERR_clear_error(); |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) { |
| 714 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file())); |
| 715 | if (!bio || !BIO_read_filename(bio.get(), file.c_str())) { |
| 716 | return nullptr; |
| 717 | } |
| 718 | return bssl::UniquePtr<EVP_PKEY>( |
| 719 | PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL)); |
| 720 | } |
| 721 | |
| 722 | static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509, |
| 723 | bssl::UniquePtr<STACK_OF(X509)> *out_chain, |
| 724 | bssl::UniquePtr<EVP_PKEY> *out_pkey) { |
| 725 | const TestConfig *config = GetTestConfig(ssl); |
| 726 | |
| 727 | if (!config->signing_prefs.empty()) { |
| 728 | std::vector<uint16_t> u16s(config->signing_prefs.begin(), |
| 729 | config->signing_prefs.end()); |
| 730 | if (!SSL_set_signing_algorithm_prefs(ssl, u16s.data(), u16s.size())) { |
| 731 | return false; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (!config->key_file.empty()) { |
| 736 | *out_pkey = LoadPrivateKey(config->key_file.c_str()); |
| 737 | if (!*out_pkey) { |
| 738 | return false; |
| 739 | } |
| 740 | } |
| 741 | if (!config->cert_file.empty() && |
| 742 | !LoadCertificate(out_x509, out_chain, config->cert_file.c_str())) { |
| 743 | return false; |
| 744 | } |
| 745 | if (!config->ocsp_response.empty() && !config->set_ocsp_in_callback && |
| 746 | !SSL_set_ocsp_response(ssl, (const uint8_t *)config->ocsp_response.data(), |
| 747 | config->ocsp_response.size())) { |
| 748 | return false; |
| 749 | } |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | static bool FromHexDigit(uint8_t *out, char c) { |
| 754 | if ('0' <= c && c <= '9') { |
| 755 | *out = c - '0'; |
| 756 | return true; |
| 757 | } |
| 758 | if ('a' <= c && c <= 'f') { |
| 759 | *out = c - 'a' + 10; |
| 760 | return true; |
| 761 | } |
| 762 | if ('A' <= c && c <= 'F') { |
| 763 | *out = c - 'A' + 10; |
| 764 | return true; |
| 765 | } |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | static bool HexDecode(std::string *out, const std::string &in) { |
| 770 | if ((in.size() & 1) != 0) { |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | std::unique_ptr<uint8_t[]> buf(new uint8_t[in.size() / 2]); |
| 775 | for (size_t i = 0; i < in.size() / 2; i++) { |
| 776 | uint8_t high, low; |
| 777 | if (!FromHexDigit(&high, in[i * 2]) || !FromHexDigit(&low, in[i * 2 + 1])) { |
| 778 | return false; |
| 779 | } |
| 780 | buf[i] = (high << 4) | low; |
| 781 | } |
| 782 | |
| 783 | out->assign(reinterpret_cast<const char *>(buf.get()), in.size() / 2); |
| 784 | return true; |
| 785 | } |
| 786 | |
| 787 | static std::vector<std::string> SplitParts(const std::string &in, |
| 788 | const char delim) { |
| 789 | std::vector<std::string> ret; |
| 790 | size_t start = 0; |
| 791 | |
| 792 | for (size_t i = 0; i < in.size(); i++) { |
| 793 | if (in[i] == delim) { |
| 794 | ret.push_back(in.substr(start, i - start)); |
| 795 | start = i + 1; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | ret.push_back(in.substr(start, std::string::npos)); |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | static std::vector<std::string> DecodeHexStrings( |
| 804 | const std::string &hex_strings) { |
| 805 | std::vector<std::string> ret; |
| 806 | const std::vector<std::string> parts = SplitParts(hex_strings, ','); |
| 807 | |
| 808 | for (const auto &part : parts) { |
| 809 | std::string binary; |
| 810 | if (!HexDecode(&binary, part)) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 811 | fprintf(stderr, "Bad hex string: %s.\n", part.c_str()); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | ret.push_back(binary); |
| 816 | } |
| 817 | |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | static bssl::UniquePtr<STACK_OF(X509_NAME)> DecodeHexX509Names( |
| 822 | const std::string &hex_names) { |
| 823 | const std::vector<std::string> der_names = DecodeHexStrings(hex_names); |
| 824 | bssl::UniquePtr<STACK_OF(X509_NAME)> ret(sk_X509_NAME_new_null()); |
| 825 | if (!ret) { |
| 826 | return nullptr; |
| 827 | } |
| 828 | |
| 829 | for (const auto &der_name : der_names) { |
| 830 | const uint8_t *const data = |
| 831 | reinterpret_cast<const uint8_t *>(der_name.data()); |
| 832 | const uint8_t *derp = data; |
| 833 | bssl::UniquePtr<X509_NAME> name( |
| 834 | d2i_X509_NAME(nullptr, &derp, der_name.size())); |
| 835 | if (!name || derp != data + der_name.size()) { |
| 836 | fprintf(stderr, "Failed to parse X509_NAME.\n"); |
| 837 | return nullptr; |
| 838 | } |
| 839 | |
| 840 | if (!bssl::PushToStack(ret.get(), std::move(name))) { |
| 841 | return nullptr; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | static bool CheckPeerVerifyPrefs(SSL *ssl) { |
| 849 | const TestConfig *config = GetTestConfig(ssl); |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 850 | if (!config->expect_peer_verify_prefs.empty()) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 851 | const uint16_t *peer_sigalgs; |
| 852 | size_t num_peer_sigalgs = |
| 853 | SSL_get0_peer_verify_algorithms(ssl, &peer_sigalgs); |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 854 | if (config->expect_peer_verify_prefs.size() != num_peer_sigalgs) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 855 | fprintf(stderr, |
| 856 | "peer verify preferences length mismatch (got %zu, wanted %zu)\n", |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 857 | num_peer_sigalgs, config->expect_peer_verify_prefs.size()); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 858 | return false; |
| 859 | } |
| 860 | for (size_t i = 0; i < num_peer_sigalgs; i++) { |
| 861 | if (static_cast<int>(peer_sigalgs[i]) != |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 862 | config->expect_peer_verify_prefs[i]) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 863 | fprintf(stderr, |
| 864 | "peer verify preference %zu mismatch (got %04x, wanted %04x\n", |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 865 | i, peer_sigalgs[i], config->expect_peer_verify_prefs[i]); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 866 | return false; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | static bool CheckCertificateRequest(SSL *ssl) { |
| 874 | const TestConfig *config = GetTestConfig(ssl); |
| 875 | |
| 876 | if (!CheckPeerVerifyPrefs(ssl)) { |
| 877 | return false; |
| 878 | } |
| 879 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 880 | if (!config->expect_certificate_types.empty()) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 881 | const uint8_t *certificate_types; |
| 882 | size_t certificate_types_len = |
| 883 | SSL_get0_certificate_types(ssl, &certificate_types); |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 884 | if (certificate_types_len != config->expect_certificate_types.size() || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 885 | OPENSSL_memcmp(certificate_types, |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 886 | config->expect_certificate_types.data(), |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 887 | certificate_types_len) != 0) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 888 | fprintf(stderr, "certificate types mismatch.\n"); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 889 | return false; |
| 890 | } |
| 891 | } |
| 892 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 893 | if (!config->expect_client_ca_list.empty()) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 894 | bssl::UniquePtr<STACK_OF(X509_NAME)> expected = |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 895 | DecodeHexX509Names(config->expect_client_ca_list); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 896 | const size_t num_expected = sk_X509_NAME_num(expected.get()); |
| 897 | |
| 898 | const STACK_OF(X509_NAME) *received = SSL_get_client_CA_list(ssl); |
| 899 | const size_t num_received = sk_X509_NAME_num(received); |
| 900 | |
| 901 | if (num_received != num_expected) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 902 | fprintf(stderr, "expected %u names in CertificateRequest but got %u.\n", |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 903 | static_cast<unsigned>(num_expected), |
| 904 | static_cast<unsigned>(num_received)); |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | for (size_t i = 0; i < num_received; i++) { |
| 909 | if (X509_NAME_cmp(sk_X509_NAME_value(received, i), |
| 910 | sk_X509_NAME_value(expected.get(), i)) != 0) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 911 | fprintf(stderr, "names in CertificateRequest differ at index #%d.\n", |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 912 | static_cast<unsigned>(i)); |
| 913 | return false; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | const STACK_OF(CRYPTO_BUFFER) *buffers = SSL_get0_server_requested_CAs(ssl); |
| 918 | if (sk_CRYPTO_BUFFER_num(buffers) != num_received) { |
| 919 | fprintf(stderr, |
| 920 | "Mismatch between SSL_get_server_requested_CAs and " |
| 921 | "SSL_get_client_CA_list.\n"); |
| 922 | return false; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) { |
| 930 | if (!CheckCertificateRequest(ssl)) { |
| 931 | return -1; |
| 932 | } |
| 933 | |
| 934 | if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) { |
| 935 | return -1; |
| 936 | } |
| 937 | |
| 938 | bssl::UniquePtr<X509> x509; |
| 939 | bssl::UniquePtr<STACK_OF(X509)> chain; |
| 940 | bssl::UniquePtr<EVP_PKEY> pkey; |
| 941 | if (!GetCertificate(ssl, &x509, &chain, &pkey)) { |
| 942 | return -1; |
| 943 | } |
| 944 | |
| 945 | // Return zero for no certificate. |
| 946 | if (!x509) { |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | // Chains and asynchronous private keys are not supported with client_cert_cb. |
| 951 | *out_x509 = x509.release(); |
| 952 | *out_pkey = pkey.release(); |
| 953 | return 1; |
| 954 | } |
| 955 | |
| 956 | static ssl_private_key_result_t AsyncPrivateKeySign( |
| 957 | SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 958 | uint16_t signature_algorithm, const uint8_t *in, size_t in_len) { |
| 959 | TestState *test_state = GetTestState(ssl); |
| 960 | if (!test_state->private_key_result.empty()) { |
| 961 | fprintf(stderr, "AsyncPrivateKeySign called with operation pending.\n"); |
| 962 | abort(); |
| 963 | } |
| 964 | |
| 965 | if (EVP_PKEY_id(test_state->private_key.get()) != |
| 966 | SSL_get_signature_algorithm_key_type(signature_algorithm)) { |
| 967 | fprintf(stderr, "Key type does not match signature algorithm.\n"); |
| 968 | abort(); |
| 969 | } |
| 970 | |
| 971 | // Determine the hash. |
| 972 | const EVP_MD *md = SSL_get_signature_algorithm_digest(signature_algorithm); |
| 973 | bssl::ScopedEVP_MD_CTX ctx; |
| 974 | EVP_PKEY_CTX *pctx; |
| 975 | if (!EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr, |
| 976 | test_state->private_key.get())) { |
| 977 | return ssl_private_key_failure; |
| 978 | } |
| 979 | |
| 980 | // Configure additional signature parameters. |
| 981 | if (SSL_is_signature_algorithm_rsa_pss(signature_algorithm)) { |
| 982 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || |
| 983 | !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) { |
| 984 | return ssl_private_key_failure; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // Write the signature into |test_state|. |
| 989 | size_t len = 0; |
| 990 | if (!EVP_DigestSign(ctx.get(), nullptr, &len, in, in_len)) { |
| 991 | return ssl_private_key_failure; |
| 992 | } |
| 993 | test_state->private_key_result.resize(len); |
| 994 | if (!EVP_DigestSign(ctx.get(), test_state->private_key_result.data(), &len, |
| 995 | in, in_len)) { |
| 996 | return ssl_private_key_failure; |
| 997 | } |
| 998 | test_state->private_key_result.resize(len); |
| 999 | |
| 1000 | // The signature will be released asynchronously in |AsyncPrivateKeyComplete|. |
| 1001 | return ssl_private_key_retry; |
| 1002 | } |
| 1003 | |
| 1004 | static ssl_private_key_result_t AsyncPrivateKeyDecrypt(SSL *ssl, uint8_t *out, |
| 1005 | size_t *out_len, |
| 1006 | size_t max_out, |
| 1007 | const uint8_t *in, |
| 1008 | size_t in_len) { |
| 1009 | TestState *test_state = GetTestState(ssl); |
| 1010 | if (!test_state->private_key_result.empty()) { |
| 1011 | fprintf(stderr, "AsyncPrivateKeyDecrypt called with operation pending.\n"); |
| 1012 | abort(); |
| 1013 | } |
| 1014 | |
| 1015 | RSA *rsa = EVP_PKEY_get0_RSA(test_state->private_key.get()); |
| 1016 | if (rsa == NULL) { |
| 1017 | fprintf(stderr, "AsyncPrivateKeyDecrypt called with incorrect key type.\n"); |
| 1018 | abort(); |
| 1019 | } |
| 1020 | test_state->private_key_result.resize(RSA_size(rsa)); |
| 1021 | if (!RSA_decrypt(rsa, out_len, test_state->private_key_result.data(), |
| 1022 | RSA_size(rsa), in, in_len, RSA_NO_PADDING)) { |
| 1023 | return ssl_private_key_failure; |
| 1024 | } |
| 1025 | |
| 1026 | test_state->private_key_result.resize(*out_len); |
| 1027 | |
| 1028 | // The decryption will be released asynchronously in |AsyncPrivateComplete|. |
| 1029 | return ssl_private_key_retry; |
| 1030 | } |
| 1031 | |
| 1032 | static ssl_private_key_result_t AsyncPrivateKeyComplete(SSL *ssl, uint8_t *out, |
| 1033 | size_t *out_len, |
| 1034 | size_t max_out) { |
| 1035 | TestState *test_state = GetTestState(ssl); |
| 1036 | if (test_state->private_key_result.empty()) { |
| 1037 | fprintf(stderr, |
| 1038 | "AsyncPrivateKeyComplete called without operation pending.\n"); |
| 1039 | abort(); |
| 1040 | } |
| 1041 | |
| 1042 | if (test_state->private_key_retries < 2) { |
| 1043 | // Only return the decryption on the second attempt, to test both incomplete |
| 1044 | // |decrypt| and |decrypt_complete|. |
| 1045 | return ssl_private_key_retry; |
| 1046 | } |
| 1047 | |
| 1048 | if (max_out < test_state->private_key_result.size()) { |
| 1049 | fprintf(stderr, "Output buffer too small.\n"); |
| 1050 | return ssl_private_key_failure; |
| 1051 | } |
| 1052 | OPENSSL_memcpy(out, test_state->private_key_result.data(), |
| 1053 | test_state->private_key_result.size()); |
| 1054 | *out_len = test_state->private_key_result.size(); |
| 1055 | |
| 1056 | test_state->private_key_result.clear(); |
| 1057 | test_state->private_key_retries = 0; |
| 1058 | return ssl_private_key_success; |
| 1059 | } |
| 1060 | |
| 1061 | static const SSL_PRIVATE_KEY_METHOD g_async_private_key_method = { |
| 1062 | AsyncPrivateKeySign, |
| 1063 | AsyncPrivateKeyDecrypt, |
| 1064 | AsyncPrivateKeyComplete, |
| 1065 | }; |
| 1066 | |
| 1067 | static bool InstallCertificate(SSL *ssl) { |
| 1068 | bssl::UniquePtr<X509> x509; |
| 1069 | bssl::UniquePtr<STACK_OF(X509)> chain; |
| 1070 | bssl::UniquePtr<EVP_PKEY> pkey; |
| 1071 | if (!GetCertificate(ssl, &x509, &chain, &pkey)) { |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | if (pkey) { |
| 1076 | TestState *test_state = GetTestState(ssl); |
| 1077 | const TestConfig *config = GetTestConfig(ssl); |
| 1078 | if (config->async) { |
| 1079 | test_state->private_key = std::move(pkey); |
| 1080 | SSL_set_private_key_method(ssl, &g_async_private_key_method); |
| 1081 | } else if (!SSL_use_PrivateKey(ssl, pkey.get())) { |
| 1082 | return false; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | if (x509 && !SSL_use_certificate(ssl, x509.get())) { |
| 1087 | return false; |
| 1088 | } |
| 1089 | |
| 1090 | if (sk_X509_num(chain.get()) > 0 && !SSL_set1_chain(ssl, chain.get())) { |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | static enum ssl_select_cert_result_t SelectCertificateCallback( |
| 1098 | const SSL_CLIENT_HELLO *client_hello) { |
| 1099 | const TestConfig *config = GetTestConfig(client_hello->ssl); |
| 1100 | GetTestState(client_hello->ssl)->early_callback_called = true; |
| 1101 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1102 | if (!config->expect_server_name.empty()) { |
| 1103 | const char *server_name = |
| 1104 | SSL_get_servername(client_hello->ssl, TLSEXT_NAMETYPE_host_name); |
| 1105 | if (server_name == nullptr || |
| 1106 | std::string(server_name) != config->expect_server_name) { |
| 1107 | fprintf(stderr, |
| 1108 | "Server name mismatch in early callback (got %s; want %s).\n", |
| 1109 | server_name, config->expect_server_name.c_str()); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1110 | return ssl_select_cert_error; |
| 1111 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (config->fail_early_callback) { |
| 1115 | return ssl_select_cert_error; |
| 1116 | } |
| 1117 | |
| 1118 | // Install the certificate in the early callback. |
| 1119 | if (config->use_early_callback) { |
| 1120 | bool early_callback_ready = |
| 1121 | GetTestState(client_hello->ssl)->early_callback_ready; |
| 1122 | if (config->async && !early_callback_ready) { |
| 1123 | // Install the certificate asynchronously. |
| 1124 | return ssl_select_cert_retry; |
| 1125 | } |
| 1126 | if (!InstallCertificate(client_hello->ssl)) { |
| 1127 | return ssl_select_cert_error; |
| 1128 | } |
| 1129 | } |
| 1130 | return ssl_select_cert_success; |
| 1131 | } |
| 1132 | |
| 1133 | bssl::UniquePtr<SSL_CTX> TestConfig::SetupCtx(SSL_CTX *old_ctx) const { |
| 1134 | bssl::UniquePtr<SSL_CTX> ssl_ctx( |
| 1135 | SSL_CTX_new(is_dtls ? DTLS_method() : TLS_method())); |
| 1136 | if (!ssl_ctx) { |
| 1137 | return nullptr; |
| 1138 | } |
| 1139 | |
| 1140 | CRYPTO_once(&once, init_once); |
| 1141 | SSL_CTX_set0_buffer_pool(ssl_ctx.get(), g_pool); |
| 1142 | |
| 1143 | // Enable TLS 1.3 for tests. |
| 1144 | if (!is_dtls && |
| 1145 | !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION)) { |
| 1146 | return nullptr; |
| 1147 | } |
| 1148 | |
| 1149 | std::string cipher_list = "ALL"; |
| 1150 | if (!cipher.empty()) { |
| 1151 | cipher_list = cipher; |
| 1152 | SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 1153 | } |
| 1154 | if (!SSL_CTX_set_strict_cipher_list(ssl_ctx.get(), cipher_list.c_str())) { |
| 1155 | return nullptr; |
| 1156 | } |
| 1157 | |
| 1158 | if (async && is_server) { |
| 1159 | // Disable the internal session cache. To test asynchronous session lookup, |
| 1160 | // we use an external session cache. |
| 1161 | SSL_CTX_set_session_cache_mode( |
| 1162 | ssl_ctx.get(), SSL_SESS_CACHE_BOTH | SSL_SESS_CACHE_NO_INTERNAL); |
| 1163 | SSL_CTX_sess_set_get_cb(ssl_ctx.get(), GetSessionCallback); |
| 1164 | } else { |
| 1165 | SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 1166 | } |
| 1167 | |
| 1168 | SSL_CTX_set_select_certificate_cb(ssl_ctx.get(), SelectCertificateCallback); |
| 1169 | |
| 1170 | if (use_old_client_cert_callback) { |
| 1171 | SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback); |
| 1172 | } |
| 1173 | |
| 1174 | SSL_CTX_set_next_protos_advertised_cb(ssl_ctx.get(), |
| 1175 | NextProtosAdvertisedCallback, NULL); |
| 1176 | if (!select_next_proto.empty()) { |
| 1177 | SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback, |
| 1178 | NULL); |
| 1179 | } |
| 1180 | |
| 1181 | if (!select_alpn.empty() || decline_alpn || select_empty_alpn) { |
| 1182 | SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL); |
| 1183 | } |
| 1184 | |
| 1185 | SSL_CTX_set_channel_id_cb(ssl_ctx.get(), ChannelIdCallback); |
| 1186 | |
| 1187 | SSL_CTX_set_current_time_cb(ssl_ctx.get(), CurrentTimeCallback); |
| 1188 | |
| 1189 | SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback); |
| 1190 | SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback); |
| 1191 | |
| 1192 | if (use_ticket_callback) { |
| 1193 | SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback); |
| 1194 | } |
| 1195 | |
| 1196 | if (!use_custom_verify_callback) { |
| 1197 | SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), CertVerifyCallback, NULL); |
| 1198 | } |
| 1199 | |
| 1200 | if (!signed_cert_timestamps.empty() && |
| 1201 | !SSL_CTX_set_signed_cert_timestamp_list( |
| 1202 | ssl_ctx.get(), (const uint8_t *)signed_cert_timestamps.data(), |
| 1203 | signed_cert_timestamps.size())) { |
| 1204 | return nullptr; |
| 1205 | } |
| 1206 | |
| 1207 | if (!use_client_ca_list.empty()) { |
| 1208 | if (use_client_ca_list == "<NULL>") { |
| 1209 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr); |
| 1210 | } else if (use_client_ca_list == "<EMPTY>") { |
| 1211 | bssl::UniquePtr<STACK_OF(X509_NAME)> names; |
| 1212 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), names.release()); |
| 1213 | } else { |
| 1214 | bssl::UniquePtr<STACK_OF(X509_NAME)> names = |
| 1215 | DecodeHexX509Names(use_client_ca_list); |
| 1216 | SSL_CTX_set_client_CA_list(ssl_ctx.get(), names.release()); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (enable_grease) { |
| 1221 | SSL_CTX_set_grease_enabled(ssl_ctx.get(), 1); |
| 1222 | } |
| 1223 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1224 | if (!expect_server_name.empty()) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1225 | SSL_CTX_set_tlsext_servername_callback(ssl_ctx.get(), ServerNameCallback); |
| 1226 | } |
| 1227 | |
| 1228 | if (enable_early_data) { |
| 1229 | SSL_CTX_set_early_data_enabled(ssl_ctx.get(), 1); |
| 1230 | } |
| 1231 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1232 | if (allow_unknown_alpn_protos) { |
| 1233 | SSL_CTX_set_allow_unknown_alpn_protos(ssl_ctx.get(), 1); |
| 1234 | } |
| 1235 | |
| 1236 | if (enable_ed25519) { |
| 1237 | SSL_CTX_set_ed25519_enabled(ssl_ctx.get(), 1); |
| 1238 | } |
| 1239 | if (no_rsa_pss_rsae_certs) { |
| 1240 | SSL_CTX_set_rsa_pss_rsae_certs_enabled(ssl_ctx.get(), 0); |
| 1241 | } |
| 1242 | |
| 1243 | if (!verify_prefs.empty()) { |
| 1244 | std::vector<uint16_t> u16s(verify_prefs.begin(), verify_prefs.end()); |
| 1245 | if (!SSL_CTX_set_verify_algorithm_prefs(ssl_ctx.get(), u16s.data(), |
| 1246 | u16s.size())) { |
| 1247 | return nullptr; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | SSL_CTX_set_msg_callback(ssl_ctx.get(), MessageCallback); |
| 1252 | |
| 1253 | if (allow_false_start_without_alpn) { |
| 1254 | SSL_CTX_set_false_start_allowed_without_alpn(ssl_ctx.get(), 1); |
| 1255 | } |
| 1256 | |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 1257 | if (ignore_tls13_downgrade) { |
| 1258 | SSL_CTX_set_ignore_tls13_downgrade(ssl_ctx.get(), 1); |
| 1259 | } |
| 1260 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1261 | if (use_ocsp_callback) { |
| 1262 | SSL_CTX_set_tlsext_status_cb(ssl_ctx.get(), LegacyOCSPCallback); |
| 1263 | } |
| 1264 | |
| 1265 | if (old_ctx) { |
| 1266 | uint8_t keys[48]; |
| 1267 | if (!SSL_CTX_get_tlsext_ticket_keys(old_ctx, &keys, sizeof(keys)) || |
| 1268 | !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), keys, sizeof(keys))) { |
| 1269 | return nullptr; |
| 1270 | } |
| 1271 | CopySessions(ssl_ctx.get(), old_ctx); |
| 1272 | } else if (!ticket_key.empty() && |
| 1273 | !SSL_CTX_set_tlsext_ticket_keys(ssl_ctx.get(), ticket_key.data(), |
| 1274 | ticket_key.size())) { |
| 1275 | return nullptr; |
| 1276 | } |
| 1277 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1278 | if (install_cert_compression_algs && |
| 1279 | (!SSL_CTX_add_cert_compression_alg( |
| 1280 | ssl_ctx.get(), 0xff02, |
| 1281 | [](SSL *ssl, CBB *out, const uint8_t *in, size_t in_len) -> int { |
| 1282 | if (!CBB_add_u8(out, 1) || !CBB_add_u8(out, 2) || |
| 1283 | !CBB_add_u8(out, 3) || !CBB_add_u8(out, 4) || |
| 1284 | !CBB_add_bytes(out, in, in_len)) { |
| 1285 | return 0; |
| 1286 | } |
| 1287 | return 1; |
| 1288 | }, |
| 1289 | [](SSL *ssl, CRYPTO_BUFFER **out, size_t uncompressed_len, |
| 1290 | const uint8_t *in, size_t in_len) -> int { |
| 1291 | if (in_len < 4 || in[0] != 1 || in[1] != 2 || in[2] != 3 || |
| 1292 | in[3] != 4 || uncompressed_len != in_len - 4) { |
| 1293 | return 0; |
| 1294 | } |
| 1295 | const bssl::Span<const uint8_t> uncompressed(in + 4, in_len - 4); |
| 1296 | *out = CRYPTO_BUFFER_new(uncompressed.data(), uncompressed.size(), |
| 1297 | nullptr); |
| 1298 | return 1; |
| 1299 | }) || |
| 1300 | !SSL_CTX_add_cert_compression_alg( |
| 1301 | ssl_ctx.get(), 0xff01, |
| 1302 | [](SSL *ssl, CBB *out, const uint8_t *in, size_t in_len) -> int { |
| 1303 | if (in_len < 2 || in[0] != 0 || in[1] != 0) { |
| 1304 | return 0; |
| 1305 | } |
| 1306 | return CBB_add_bytes(out, in + 2, in_len - 2); |
| 1307 | }, |
| 1308 | [](SSL *ssl, CRYPTO_BUFFER **out, size_t uncompressed_len, |
| 1309 | const uint8_t *in, size_t in_len) -> int { |
| 1310 | if (uncompressed_len != 2 + in_len) { |
| 1311 | return 0; |
| 1312 | } |
| 1313 | std::unique_ptr<uint8_t[]> buf(new uint8_t[2 + in_len]); |
| 1314 | buf[0] = 0; |
| 1315 | buf[1] = 0; |
| 1316 | OPENSSL_memcpy(&buf[2], in, in_len); |
| 1317 | *out = CRYPTO_BUFFER_new(buf.get(), 2 + in_len, nullptr); |
| 1318 | return 1; |
| 1319 | }))) { |
| 1320 | fprintf(stderr, "SSL_CTX_add_cert_compression_alg failed.\n"); |
| 1321 | abort(); |
| 1322 | } |
| 1323 | |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1324 | if (server_preference) { |
| 1325 | SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE); |
| 1326 | } |
| 1327 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1328 | if (enable_pq_experiment_signal) { |
| 1329 | SSL_CTX_enable_pq_experiment_signal(ssl_ctx.get()); |
| 1330 | } |
| 1331 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1332 | return ssl_ctx; |
| 1333 | } |
| 1334 | |
| 1335 | static int DDoSCallback(const SSL_CLIENT_HELLO *client_hello) { |
| 1336 | const TestConfig *config = GetTestConfig(client_hello->ssl); |
| 1337 | return config->fail_ddos_callback ? 0 : 1; |
| 1338 | } |
| 1339 | |
| 1340 | static unsigned PskClientCallback(SSL *ssl, const char *hint, |
| 1341 | char *out_identity, unsigned max_identity_len, |
| 1342 | uint8_t *out_psk, unsigned max_psk_len) { |
| 1343 | const TestConfig *config = GetTestConfig(ssl); |
| 1344 | |
| 1345 | if (config->psk_identity.empty()) { |
| 1346 | if (hint != nullptr) { |
| 1347 | fprintf(stderr, "Server PSK hint was non-null.\n"); |
| 1348 | return 0; |
| 1349 | } |
| 1350 | } else if (hint == nullptr || |
| 1351 | strcmp(hint, config->psk_identity.c_str()) != 0) { |
| 1352 | fprintf(stderr, "Server PSK hint did not match.\n"); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | // Account for the trailing '\0' for the identity. |
| 1357 | if (config->psk_identity.size() >= max_identity_len || |
| 1358 | config->psk.size() > max_psk_len) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1359 | fprintf(stderr, "PSK buffers too small.\n"); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | BUF_strlcpy(out_identity, config->psk_identity.c_str(), max_identity_len); |
| 1364 | OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 1365 | return config->psk.size(); |
| 1366 | } |
| 1367 | |
| 1368 | static unsigned PskServerCallback(SSL *ssl, const char *identity, |
| 1369 | uint8_t *out_psk, unsigned max_psk_len) { |
| 1370 | const TestConfig *config = GetTestConfig(ssl); |
| 1371 | |
| 1372 | if (strcmp(identity, config->psk_identity.c_str()) != 0) { |
| 1373 | fprintf(stderr, "Client PSK identity did not match.\n"); |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | if (config->psk.size() > max_psk_len) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1378 | fprintf(stderr, "PSK buffers too small.\n"); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | OPENSSL_memcpy(out_psk, config->psk.data(), config->psk.size()); |
| 1383 | return config->psk.size(); |
| 1384 | } |
| 1385 | |
| 1386 | static ssl_verify_result_t CustomVerifyCallback(SSL *ssl, uint8_t *out_alert) { |
| 1387 | const TestConfig *config = GetTestConfig(ssl); |
| 1388 | if (!CheckVerifyCallback(ssl)) { |
| 1389 | return ssl_verify_invalid; |
| 1390 | } |
| 1391 | |
| 1392 | if (config->async && !GetTestState(ssl)->custom_verify_ready) { |
| 1393 | return ssl_verify_retry; |
| 1394 | } |
| 1395 | |
| 1396 | GetTestState(ssl)->cert_verified = true; |
| 1397 | if (config->verify_fail) { |
| 1398 | return ssl_verify_invalid; |
| 1399 | } |
| 1400 | |
| 1401 | return ssl_verify_ok; |
| 1402 | } |
| 1403 | |
| 1404 | static int CertCallback(SSL *ssl, void *arg) { |
| 1405 | const TestConfig *config = GetTestConfig(ssl); |
| 1406 | |
| 1407 | // Check the peer certificate metadata is as expected. |
| 1408 | if ((!SSL_is_server(ssl) && !CheckCertificateRequest(ssl)) || |
| 1409 | !CheckPeerVerifyPrefs(ssl)) { |
| 1410 | return -1; |
| 1411 | } |
| 1412 | |
| 1413 | if (config->fail_cert_callback) { |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | // The certificate will be installed via other means. |
| 1418 | if (!config->async || config->use_early_callback) { |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
| 1422 | if (!GetTestState(ssl)->cert_ready) { |
| 1423 | return -1; |
| 1424 | } |
| 1425 | if (!InstallCertificate(ssl)) { |
| 1426 | return 0; |
| 1427 | } |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
| 1431 | bssl::UniquePtr<SSL> TestConfig::NewSSL( |
| 1432 | SSL_CTX *ssl_ctx, SSL_SESSION *session, bool is_resume, |
| 1433 | std::unique_ptr<TestState> test_state) const { |
| 1434 | bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx)); |
| 1435 | if (!ssl) { |
| 1436 | return nullptr; |
| 1437 | } |
| 1438 | |
| 1439 | if (!SetTestConfig(ssl.get(), this)) { |
| 1440 | return nullptr; |
| 1441 | } |
| 1442 | if (test_state != nullptr) { |
| 1443 | if (!SetTestState(ssl.get(), std::move(test_state))) { |
| 1444 | return nullptr; |
| 1445 | } |
| 1446 | GetTestState(ssl.get())->is_resume = is_resume; |
| 1447 | } |
| 1448 | |
| 1449 | if (fallback_scsv && !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) { |
| 1450 | return nullptr; |
| 1451 | } |
| 1452 | // Install the certificate synchronously if nothing else will handle it. |
| 1453 | if (!use_early_callback && !use_old_client_cert_callback && !async && |
| 1454 | !InstallCertificate(ssl.get())) { |
| 1455 | return nullptr; |
| 1456 | } |
| 1457 | if (!use_old_client_cert_callback) { |
| 1458 | SSL_set_cert_cb(ssl.get(), CertCallback, nullptr); |
| 1459 | } |
| 1460 | int mode = SSL_VERIFY_NONE; |
| 1461 | if (require_any_client_certificate) { |
| 1462 | mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1463 | } |
| 1464 | if (verify_peer) { |
| 1465 | mode = SSL_VERIFY_PEER; |
| 1466 | } |
| 1467 | if (verify_peer_if_no_obc) { |
| 1468 | // Set SSL_VERIFY_FAIL_IF_NO_PEER_CERT so testing whether client |
| 1469 | // certificates were requested is easy. |
| 1470 | mode = SSL_VERIFY_PEER | SSL_VERIFY_PEER_IF_NO_OBC | |
| 1471 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1472 | } |
| 1473 | if (use_custom_verify_callback) { |
| 1474 | SSL_set_custom_verify(ssl.get(), mode, CustomVerifyCallback); |
| 1475 | } else if (mode != SSL_VERIFY_NONE) { |
| 1476 | SSL_set_verify(ssl.get(), mode, NULL); |
| 1477 | } |
| 1478 | if (false_start) { |
| 1479 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_FALSE_START); |
| 1480 | } |
| 1481 | if (cbc_record_splitting) { |
| 1482 | SSL_set_mode(ssl.get(), SSL_MODE_CBC_RECORD_SPLITTING); |
| 1483 | } |
| 1484 | if (partial_write) { |
| 1485 | SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE); |
| 1486 | } |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 1487 | if (reverify_on_resume) { |
| 1488 | SSL_CTX_set_reverify_on_resume(ssl_ctx, 1); |
| 1489 | } |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1490 | if (enforce_rsa_key_usage) { |
| 1491 | SSL_set_enforce_rsa_key_usage(ssl.get(), 1); |
| 1492 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1493 | if (no_tls13) { |
| 1494 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3); |
| 1495 | } |
| 1496 | if (no_tls12) { |
| 1497 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2); |
| 1498 | } |
| 1499 | if (no_tls11) { |
| 1500 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1); |
| 1501 | } |
| 1502 | if (no_tls1) { |
| 1503 | SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1); |
| 1504 | } |
| 1505 | if (no_ticket) { |
| 1506 | SSL_set_options(ssl.get(), SSL_OP_NO_TICKET); |
| 1507 | } |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1508 | if (!expect_channel_id.empty() || enable_channel_id) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1509 | SSL_set_tls_channel_id_enabled(ssl.get(), 1); |
| 1510 | } |
| 1511 | if (!send_channel_id.empty()) { |
| 1512 | SSL_set_tls_channel_id_enabled(ssl.get(), 1); |
| 1513 | if (!async) { |
| 1514 | // The async case will be supplied by |ChannelIdCallback|. |
| 1515 | bssl::UniquePtr<EVP_PKEY> pkey = LoadPrivateKey(send_channel_id); |
| 1516 | if (!pkey || !SSL_set1_tls_channel_id(ssl.get(), pkey.get())) { |
| 1517 | return nullptr; |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | if (!send_token_binding_params.empty()) { |
| 1522 | SSL_set_token_binding_params( |
| 1523 | ssl.get(), |
| 1524 | reinterpret_cast<const uint8_t *>(send_token_binding_params.data()), |
| 1525 | send_token_binding_params.length()); |
| 1526 | } |
| 1527 | if (!host_name.empty() && |
| 1528 | !SSL_set_tlsext_host_name(ssl.get(), host_name.c_str())) { |
| 1529 | return nullptr; |
| 1530 | } |
| 1531 | if (!advertise_alpn.empty() && |
| 1532 | SSL_set_alpn_protos(ssl.get(), (const uint8_t *)advertise_alpn.data(), |
| 1533 | advertise_alpn.size()) != 0) { |
| 1534 | return nullptr; |
| 1535 | } |
| 1536 | if (!psk.empty()) { |
| 1537 | SSL_set_psk_client_callback(ssl.get(), PskClientCallback); |
| 1538 | SSL_set_psk_server_callback(ssl.get(), PskServerCallback); |
| 1539 | } |
| 1540 | if (!psk_identity.empty() && |
| 1541 | !SSL_use_psk_identity_hint(ssl.get(), psk_identity.c_str())) { |
| 1542 | return nullptr; |
| 1543 | } |
| 1544 | if (!srtp_profiles.empty() && |
| 1545 | !SSL_set_srtp_profiles(ssl.get(), srtp_profiles.c_str())) { |
| 1546 | return nullptr; |
| 1547 | } |
| 1548 | if (enable_ocsp_stapling) { |
| 1549 | SSL_enable_ocsp_stapling(ssl.get()); |
| 1550 | } |
| 1551 | if (enable_signed_cert_timestamps) { |
| 1552 | SSL_enable_signed_cert_timestamps(ssl.get()); |
| 1553 | } |
| 1554 | if (min_version != 0 && |
| 1555 | !SSL_set_min_proto_version(ssl.get(), (uint16_t)min_version)) { |
| 1556 | return nullptr; |
| 1557 | } |
| 1558 | if (max_version != 0 && |
| 1559 | !SSL_set_max_proto_version(ssl.get(), (uint16_t)max_version)) { |
| 1560 | return nullptr; |
| 1561 | } |
| 1562 | if (mtu != 0) { |
| 1563 | SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU); |
| 1564 | SSL_set_mtu(ssl.get(), mtu); |
| 1565 | } |
| 1566 | if (install_ddos_callback) { |
| 1567 | SSL_CTX_set_dos_protection_cb(ssl_ctx, DDoSCallback); |
| 1568 | } |
| 1569 | SSL_set_shed_handshake_config(ssl.get(), true); |
| 1570 | if (renegotiate_once) { |
| 1571 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_once); |
| 1572 | } |
| 1573 | if (renegotiate_freely || forbid_renegotiation_after_handshake) { |
| 1574 | // |forbid_renegotiation_after_handshake| will disable renegotiation later. |
| 1575 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_freely); |
| 1576 | } |
| 1577 | if (renegotiate_ignore) { |
| 1578 | SSL_set_renegotiate_mode(ssl.get(), ssl_renegotiate_ignore); |
| 1579 | } |
| 1580 | if (!check_close_notify) { |
| 1581 | SSL_set_quiet_shutdown(ssl.get(), 1); |
| 1582 | } |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1583 | if (!curves.empty()) { |
| 1584 | std::vector<int> nids; |
| 1585 | for (auto curve : curves) { |
| 1586 | switch (curve) { |
| 1587 | case SSL_CURVE_SECP224R1: |
| 1588 | nids.push_back(NID_secp224r1); |
| 1589 | break; |
| 1590 | |
| 1591 | case SSL_CURVE_SECP256R1: |
| 1592 | nids.push_back(NID_X9_62_prime256v1); |
| 1593 | break; |
| 1594 | |
| 1595 | case SSL_CURVE_SECP384R1: |
| 1596 | nids.push_back(NID_secp384r1); |
| 1597 | break; |
| 1598 | |
| 1599 | case SSL_CURVE_SECP521R1: |
| 1600 | nids.push_back(NID_secp521r1); |
| 1601 | break; |
| 1602 | |
| 1603 | case SSL_CURVE_X25519: |
| 1604 | nids.push_back(NID_X25519); |
| 1605 | break; |
| 1606 | |
| 1607 | case SSL_CURVE_CECPQ2: |
| 1608 | nids.push_back(NID_CECPQ2); |
| 1609 | break; |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1610 | case SSL_CURVE_CECPQ2b: |
| 1611 | nids.push_back(NID_CECPQ2b); |
| 1612 | break; |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1613 | } |
| 1614 | if (!SSL_set1_curves(ssl.get(), &nids[0], nids.size())) { |
| 1615 | return nullptr; |
| 1616 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | if (enable_all_curves) { |
| 1620 | static const int kAllCurves[] = { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1621 | NID_secp224r1, NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, |
| 1622 | NID_X25519, NID_CECPQ2, NID_CECPQ2b, |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1623 | }; |
| 1624 | if (!SSL_set1_curves(ssl.get(), kAllCurves, |
| 1625 | OPENSSL_ARRAY_SIZE(kAllCurves))) { |
| 1626 | return nullptr; |
| 1627 | } |
| 1628 | } |
| 1629 | if (initial_timeout_duration_ms > 0) { |
| 1630 | DTLSv1_set_initial_timeout_duration(ssl.get(), initial_timeout_duration_ms); |
| 1631 | } |
| 1632 | if (max_cert_list > 0) { |
| 1633 | SSL_set_max_cert_list(ssl.get(), max_cert_list); |
| 1634 | } |
| 1635 | if (retain_only_sha256_client_cert) { |
| 1636 | SSL_set_retain_only_sha256_of_client_certs(ssl.get(), 1); |
| 1637 | } |
| 1638 | if (max_send_fragment > 0) { |
| 1639 | SSL_set_max_send_fragment(ssl.get(), max_send_fragment); |
| 1640 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1641 | if (!quic_transport_params.empty()) { |
| 1642 | if (!SSL_set_quic_transport_params( |
| 1643 | ssl.get(), |
| 1644 | reinterpret_cast<const uint8_t *>(quic_transport_params.data()), |
| 1645 | quic_transport_params.size())) { |
| 1646 | return nullptr; |
| 1647 | } |
| 1648 | } |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 1649 | if (jdk11_workaround) { |
| 1650 | SSL_set_jdk11_workaround(ssl.get(), 1); |
| 1651 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1652 | |
| 1653 | if (session != NULL) { |
| 1654 | if (!is_server) { |
| 1655 | if (SSL_set_session(ssl.get(), session) != 1) { |
| 1656 | return nullptr; |
| 1657 | } |
| 1658 | } else if (async) { |
| 1659 | // The internal session cache is disabled, so install the session |
| 1660 | // manually. |
| 1661 | SSL_SESSION_up_ref(session); |
| 1662 | GetTestState(ssl.get())->pending_session.reset(session); |
| 1663 | } |
| 1664 | } |
| 1665 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1666 | if (!delegated_credential.empty()) { |
| 1667 | std::string::size_type comma = delegated_credential.find(','); |
| 1668 | if (comma == std::string::npos) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1669 | fprintf(stderr, |
| 1670 | "failed to find comma in delegated credential argument.\n"); |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1671 | return nullptr; |
| 1672 | } |
| 1673 | |
| 1674 | const std::string dc_hex = delegated_credential.substr(0, comma); |
| 1675 | const std::string pkcs8_hex = delegated_credential.substr(comma + 1); |
| 1676 | std::string dc, pkcs8; |
| 1677 | if (!HexDecode(&dc, dc_hex) || !HexDecode(&pkcs8, pkcs8_hex)) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1678 | fprintf(stderr, "failed to hex decode delegated credential argument.\n"); |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1679 | return nullptr; |
| 1680 | } |
| 1681 | |
| 1682 | CBS dc_cbs(bssl::Span<const uint8_t>( |
| 1683 | reinterpret_cast<const uint8_t *>(dc.data()), dc.size())); |
| 1684 | CBS pkcs8_cbs(bssl::Span<const uint8_t>( |
| 1685 | reinterpret_cast<const uint8_t *>(pkcs8.data()), pkcs8.size())); |
| 1686 | |
| 1687 | bssl::UniquePtr<EVP_PKEY> priv(EVP_parse_private_key(&pkcs8_cbs)); |
| 1688 | if (!priv) { |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1689 | fprintf(stderr, "failed to parse delegated credential private key.\n"); |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1690 | return nullptr; |
| 1691 | } |
| 1692 | |
| 1693 | bssl::UniquePtr<CRYPTO_BUFFER> dc_buf( |
| 1694 | CRYPTO_BUFFER_new_from_CBS(&dc_cbs, nullptr)); |
| 1695 | if (!SSL_set1_delegated_credential(ssl.get(), dc_buf.get(), |
| 1696 | priv.get(), nullptr)) { |
| 1697 | fprintf(stderr, "SSL_set1_delegated_credential failed.\n"); |
| 1698 | return nullptr; |
| 1699 | } |
| 1700 | } |
| 1701 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1702 | return ssl; |
| 1703 | } |