| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 |
| 17 | #include <openssl/ssl.h> |
| 18 |
| 19 | #include <algorithm> |
| 20 |
| 21 | #include <assert.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 |
| 26 | #include <openssl/bytestring.h> |
| 27 | #include <openssl/crypto.h> |
| 28 | #include <openssl/err.h> |
| 29 | #include <openssl/lhash.h> |
| 30 | #include <openssl/mem.h> |
| 31 | #include <openssl/rand.h> |
| 32 |
| 33 | #include "../crypto/internal.h" |
| 34 | #include "internal.h" |
| 35 |
| 36 | #if defined(OPENSSL_WINDOWS) |
| 37 | #include <sys/timeb.h> |
| 38 | #else |
| 39 | #include <sys/socket.h> |
| 40 | #include <sys/time.h> |
| 41 | #endif |
| 42 |
| 43 |
| 44 | BSSL_NAMESPACE_BEGIN |
| 45 |
| 46 | static_assert(SSL3_RT_MAX_ENCRYPTED_OVERHEAD >= |
| 47 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD, |
| 48 | "max overheads are inconsistent"); |
| 49 |
| 50 | // |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it |
| 51 | // to avoid downstream churn. |
| 52 | OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL) |
| 53 |
| 54 | // The following errors are no longer emitted, but are used in nginx without |
| 55 | // #ifdefs. |
| 56 | OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG) |
| 57 | OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED) |
| 58 |
| 59 | // Some error codes are special. Ensure the make_errors.go script never |
| 60 | // regresses this. |
| 61 | static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION == |
| 62 | SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET, |
| 63 | "alert reason code mismatch"); |
| 64 |
| 65 | // kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. |
| 66 | static const size_t kMaxHandshakeSize = (1u << 24) - 1; |
| 67 |
| 68 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = |
| 69 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 70 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = |
| 71 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 72 |
| 73 | bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) { |
| 74 | uint8_t *ptr; |
| 75 | size_t len; |
| 76 | if (!CBB_finish(cbb, &ptr, &len)) { |
| 77 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 78 | return false; |
| 79 | } |
| 80 | out->Reset(ptr, len); |
| 81 | return true; |
| 82 | } |
| 83 |
| 84 | void ssl_reset_error_state(SSL *ssl) { |
| 85 | // Functions which use |SSL_get_error| must reset I/O and error state on |
| 86 | // entry. |
| 87 | ssl->s3->rwstate = SSL_ERROR_NONE; |
| 88 | ERR_clear_error(); |
| 89 | ERR_clear_system_error(); |
| 90 | } |
| 91 |
| 92 | void ssl_set_read_error(SSL *ssl) { |
| 93 | ssl->s3->read_shutdown = ssl_shutdown_error; |
| 94 | ssl->s3->read_error.reset(ERR_save_state()); |
| 95 | } |
| 96 |
| 97 | static bool check_read_error(const SSL *ssl) { |
| 98 | if (ssl->s3->read_shutdown == ssl_shutdown_error) { |
| 99 | ERR_restore_state(ssl->s3->read_error.get()); |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 |
| 105 | bool ssl_can_write(const SSL *ssl) { |
| 106 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write; |
| 107 | } |
| 108 |
| 109 | bool ssl_can_read(const SSL *ssl) { |
| 110 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read; |
| 111 | } |
| 112 |
| 113 | ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed, |
| 114 | uint8_t *out_alert, Span<uint8_t> in) { |
| 115 | *out_consumed = 0; |
| 116 | if (!check_read_error(ssl)) { |
| 117 | *out_alert = 0; |
| 118 | return ssl_open_record_error; |
| 119 | } |
| 120 | auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in); |
| 121 | if (ret == ssl_open_record_error) { |
| 122 | ssl_set_read_error(ssl); |
| 123 | } |
| 124 | return ret; |
| 125 | } |
| 126 |
| 127 | ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 128 | uint8_t *out_alert, |
| 129 | Span<uint8_t> in) { |
| 130 | *out_consumed = 0; |
| 131 | if (!check_read_error(ssl)) { |
| 132 | *out_alert = 0; |
| 133 | return ssl_open_record_error; |
| 134 | } |
| 135 | auto ret = |
| 136 | ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in); |
| 137 | if (ret == ssl_open_record_error) { |
| 138 | ssl_set_read_error(ssl); |
| 139 | } |
| 140 | return ret; |
| 141 | } |
| 142 |
| 143 | ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 144 | size_t *out_consumed, uint8_t *out_alert, |
| 145 | Span<uint8_t> in) { |
| 146 | *out_consumed = 0; |
| 147 | if (!check_read_error(ssl)) { |
| 148 | *out_alert = 0; |
| 149 | return ssl_open_record_error; |
| 150 | } |
| 151 | auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in); |
| 152 | if (ret == ssl_open_record_error) { |
| 153 | ssl_set_read_error(ssl); |
| 154 | } |
| 155 | return ret; |
| 156 | } |
| 157 |
| 158 | static uint8_t hex_char_consttime(uint8_t b) { |
| 159 | declassify_assert(b < 16); |
| 160 | return constant_time_select_8(constant_time_lt_8(b, 10), b + '0', |
| 161 | b - 10 + 'a'); |
| 162 | } |
| 163 |
| 164 | static bool cbb_add_hex_consttime(CBB *cbb, Span<const uint8_t> in) { |
| 165 | uint8_t *out; |
| 166 | if (!CBB_add_space(cbb, &out, in.size() * 2)) { |
| 167 | return false; |
| 168 | } |
| 169 |
| 170 | for (uint8_t b : in) { |
| 171 | *(out++) = hex_char_consttime(b >> 4); |
| 172 | *(out++) = hex_char_consttime(b & 0xf); |
| 173 | } |
| 174 |
| 175 | return true; |
| 176 | } |
| 177 |
| 178 | bool ssl_log_secret(const SSL *ssl, const char *label, |
| 179 | Span<const uint8_t> secret) { |
| 180 | if (ssl->ctx->keylog_callback == NULL) { |
| 181 | return true; |
| 182 | } |
| 183 |
| 184 | ScopedCBB cbb; |
| 185 | Array<uint8_t> line; |
| 186 | auto label_bytes = bssl::StringAsBytes(label); |
| 187 | if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 + |
| 188 | secret.size() * 2 + 1) || |
| 189 | !CBB_add_bytes(cbb.get(), label_bytes.data(), label_bytes.size()) || |
| 190 | !CBB_add_u8(cbb.get(), ' ') || |
| 191 | !cbb_add_hex_consttime(cbb.get(), ssl->s3->client_random) || |
| 192 | !CBB_add_u8(cbb.get(), ' ') || |
| 193 | // Convert to hex in constant time to avoid leaking |secret|. If the |
| 194 | // callback discards the data, we should not introduce side channels. |
| 195 | !cbb_add_hex_consttime(cbb.get(), secret) || |
| 196 | !CBB_add_u8(cbb.get(), 0 /* NUL */) || |
| 197 | !CBBFinishArray(cbb.get(), &line)) { |
| 198 | return false; |
| 199 | } |
| 200 |
| 201 | ssl->ctx->keylog_callback(ssl, reinterpret_cast<const char *>(line.data())); |
| 202 | return true; |
| 203 | } |
| 204 |
| 205 | void ssl_do_info_callback(const SSL *ssl, int type, int value) { |
| 206 | void (*cb)(const SSL *ssl, int type, int value) = NULL; |
| 207 | if (ssl->info_callback != NULL) { |
| 208 | cb = ssl->info_callback; |
| 209 | } else if (ssl->ctx->info_callback != NULL) { |
| 210 | cb = ssl->ctx->info_callback; |
| 211 | } |
| 212 |
| 213 | if (cb != NULL) { |
| 214 | cb(ssl, type, value); |
| 215 | } |
| 216 | } |
| 217 |
| 218 | void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type, |
| 219 | Span<const uint8_t> in) { |
| 220 | if (ssl->msg_callback == NULL) { |
| 221 | return; |
| 222 | } |
| 223 |
| 224 | // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for |
| 225 | // a V2ClientHello. |
| 226 | int version; |
| 227 | switch (content_type) { |
| 228 | case 0: |
| 229 | // V2ClientHello |
| 230 | version = SSL2_VERSION; |
| 231 | break; |
| 232 | case SSL3_RT_HEADER: |
| 233 | version = 0; |
| 234 | break; |
| 235 | default: |
| 236 | version = SSL_version(ssl); |
| 237 | } |
| 238 |
| 239 | ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), |
| 240 | const_cast<SSL *>(ssl), ssl->msg_callback_arg); |
| 241 | } |
| 242 |
| 243 | OPENSSL_timeval ssl_ctx_get_current_time(const SSL_CTX *ctx) { |
| 244 | if (ctx->current_time_cb != NULL) { |
| 245 | // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See |
| 246 | // https://crbug.com/boringssl/155. |
| 247 | struct timeval clock; |
| 248 | ctx->current_time_cb(nullptr /* ssl */, &clock); |
| 249 | if (clock.tv_sec < 0) { |
| 250 | assert(0); |
| 251 | return {0, 0}; |
| 252 | } else { |
| 253 | return {static_cast<uint64_t>(clock.tv_sec), |
| 254 | static_cast<uint32_t>(clock.tv_usec)}; |
| 255 | } |
| 256 | } |
| 257 |
| 258 | #if defined(OPENSSL_WINDOWS) |
| 259 | struct _timeb time; |
| 260 | _ftime(&time); |
| 261 | if (time.time < 0) { |
| 262 | assert(0); |
| 263 | return {0, 0}; |
| 264 | } else { |
| 265 | return {static_cast<uint64_t>(time.time), |
| 266 | static_cast<uint32_t>(time.millitm * 1000)}; |
| 267 | } |
| 268 | #else |
| 269 | struct timeval clock; |
| 270 | gettimeofday(&clock, NULL); |
| 271 | if (clock.tv_sec < 0) { |
| 272 | assert(0); |
| 273 | return {0, 0}; |
| 274 | } else { |
| 275 | return {static_cast<uint64_t>(clock.tv_sec), |
| 276 | static_cast<uint32_t>(clock.tv_usec)}; |
| 277 | } |
| 278 | #endif |
| 279 | } |
| 280 |
| 281 | void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) { ctx->handoff = on; } |
| 282 |
| 283 | static bool ssl_can_renegotiate(const SSL *ssl) { |
| 284 | if (ssl->server || SSL_is_dtls(ssl)) { |
| 285 | return false; |
| 286 | } |
| 287 |
| 288 | if (ssl->s3->version != 0 // |
| 289 | && ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 290 | return false; |
| 291 | } |
| 292 |
| 293 | // The config has already been shed. |
| 294 | if (!ssl->config) { |
| 295 | return false; |
| 296 | } |
| 297 |
| 298 | switch (ssl->renegotiate_mode) { |
| 299 | case ssl_renegotiate_ignore: |
| 300 | case ssl_renegotiate_never: |
| 301 | return false; |
| 302 |
| 303 | case ssl_renegotiate_freely: |
| 304 | case ssl_renegotiate_explicit: |
| 305 | return true; |
| 306 | case ssl_renegotiate_once: |
| 307 | return ssl->s3->total_renegotiations == 0; |
| 308 | } |
| 309 |
| 310 | assert(0); |
| 311 | return false; |
| 312 | } |
| 313 |
| 314 | static void ssl_maybe_shed_handshake_config(SSL *ssl) { |
| 315 | if (ssl->s3->hs != nullptr || // |
| 316 | ssl->config == nullptr || // |
| 317 | !ssl->config->shed_handshake_config || // |
| 318 | ssl_can_renegotiate(ssl)) { |
| 319 | return; |
| 320 | } |
| 321 |
| 322 | ssl->config.reset(); |
| 323 | } |
| 324 |
| 325 | void SSL_set_handoff_mode(SSL *ssl, bool on) { |
| 326 | if (!ssl->config) { |
| 327 | return; |
| 328 | } |
| 329 | ssl->config->handoff = on; |
| 330 | } |
| 331 |
| 332 | bool SSL_get_traffic_secrets(const SSL *ssl, |
| 333 | Span<const uint8_t> *out_read_traffic_secret, |
| 334 | Span<const uint8_t> *out_write_traffic_secret) { |
| 335 | // This API is not well-defined for DTLS 1.3 (see https://crbug.com/42290608) |
| 336 | // or QUIC, where multiple epochs may be alive at once. |
| 337 | if (SSL_is_dtls(ssl) || SSL_is_quic(ssl)) { |
| 338 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 339 | return false; |
| 340 | } |
| 341 |
| 342 | if (!ssl->s3->initial_handshake_complete) { |
| 343 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 344 | return false; |
| 345 | } |
| 346 |
| 347 | if (SSL_version(ssl) < TLS1_3_VERSION) { |
| 348 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 349 | return false; |
| 350 | } |
| 351 |
| 352 | *out_read_traffic_secret = ssl->s3->read_traffic_secret; |
| 353 | *out_write_traffic_secret = ssl->s3->write_traffic_secret; |
| 354 | return true; |
| 355 | } |
| 356 |
| 357 | void SSL_CTX_set_aes_hw_override_for_testing(SSL_CTX *ctx, |
| 358 | bool override_value) { |
| 359 | ctx->aes_hw_override = true; |
| 360 | ctx->aes_hw_override_value = override_value; |
| 361 | } |
| 362 |
| 363 | void SSL_set_aes_hw_override_for_testing(SSL *ssl, bool override_value) { |
| 364 | ssl->config->aes_hw_override = true; |
| 365 | ssl->config->aes_hw_override_value = override_value; |
| 366 | } |
| 367 |
| 368 | BSSL_NAMESPACE_END |
| 369 |
| 370 | using namespace bssl; |
| 371 |
| 372 | int SSL_library_init(void) { return 1; } |
| 373 |
| 374 | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
| 375 | return 1; |
| 376 | } |
| 377 |
| 378 | static uint32_t ssl_session_hash(const SSL_SESSION *sess) { |
| 379 | return ssl_hash_session_id(sess->session_id); |
| 380 | } |
| 381 |
| 382 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) { |
| 383 | return Span(a->session_id) == b->session_id ? 0 : 1; |
| 384 | } |
| 385 |
| 386 | ssl_ctx_st::ssl_ctx_st(const SSL_METHOD *ssl_method) |
| 387 | : RefCounted(CheckSubClass()), |
| 388 | method(ssl_method->method), |
| 389 | x509_method(ssl_method->x509_method), |
| 390 | retain_only_sha256_of_client_certs(false), |
| 391 | quiet_shutdown(false), |
| 392 | ocsp_stapling_enabled(false), |
| 393 | signed_cert_timestamps_enabled(false), |
| 394 | channel_id_enabled(false), |
| 395 | grease_enabled(false), |
| 396 | permute_extensions(false), |
| 397 | allow_unknown_alpn_protos(false), |
| 398 | false_start_allowed_without_alpn(false), |
| 399 | handoff(false), |
| 400 | enable_early_data(false), |
| 401 | aes_hw_override(false), |
| 402 | aes_hw_override_value(false), |
| 403 | resumption_across_names_enabled(false) { |
| 404 | CRYPTO_MUTEX_init(&lock); |
| 405 | CRYPTO_new_ex_data(&ex_data); |
| 406 | } |
| 407 |
| 408 | ssl_ctx_st::~ssl_ctx_st() { |
| 409 | // Free the internal session cache. Note that this calls the caller-supplied |
| 410 | // remove callback, so we must do it before clearing ex_data. (See ticket |
| 411 | // [openssl.org #212].) |
| 412 | SSL_CTX_flush_sessions(this, 0); |
| 413 |
| 414 | CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, this, &ex_data); |
| 415 |
| 416 | CRYPTO_MUTEX_cleanup(&lock); |
| 417 | lh_SSL_SESSION_free(sessions); |
| 418 | x509_method->ssl_ctx_free(this); |
| 419 | } |
| 420 |
| 421 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) { |
| 422 | if (method == NULL) { |
| 423 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED); |
| 424 | return nullptr; |
| 425 | } |
| 426 |
| 427 | UniquePtr<SSL_CTX> ret = MakeUnique<SSL_CTX>(method); |
| 428 | if (!ret) { |
| 429 | return nullptr; |
| 430 | } |
| 431 |
| 432 | ret->cert = MakeUnique<CERT>(method->x509_method); |
| 433 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
| 434 | ret->client_CA.reset(sk_CRYPTO_BUFFER_new_null()); |
| 435 | ret->CA_names.reset(sk_CRYPTO_BUFFER_new_null()); |
| 436 | if (ret->cert == nullptr || // |
| 437 | !ret->cert->is_valid() || // |
| 438 | ret->sessions == nullptr || // |
| 439 | ret->client_CA == nullptr || // |
| 440 | ret->CA_names == nullptr || // |
| 441 | !ret->x509_method->ssl_ctx_new(ret.get())) { |
| 442 | return nullptr; |
| 443 | } |
| 444 |
| 445 | if (!SSL_CTX_set_strict_cipher_list(ret.get(), SSL_DEFAULT_CIPHER_LIST) || |
| 446 | // Lock the SSL_CTX to the specified version, for compatibility with |
| 447 | // legacy uses of SSL_METHOD. |
| 448 | !SSL_CTX_set_max_proto_version(ret.get(), method->version) || |
| 449 | !SSL_CTX_set_min_proto_version(ret.get(), method->version)) { |
| 450 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 451 | return nullptr; |
| 452 | } |
| 453 |
| 454 | return ret.release(); |
| 455 | } |
| 456 |
| 457 | int SSL_CTX_up_ref(SSL_CTX *ctx) { |
| 458 | ctx->UpRefInternal(); |
| 459 | return 1; |
| 460 | } |
| 461 |
| 462 | void SSL_CTX_free(SSL_CTX *ctx) { |
| 463 | if (ctx != nullptr) { |
| 464 | ctx->DecRefInternal(); |
| 465 | } |
| 466 | } |
| 467 |
| 468 | ssl_st::ssl_st(SSL_CTX *ctx_arg) |
| 469 | : method(ctx_arg->method), |
| 470 | max_send_fragment(ctx_arg->max_send_fragment), |
| 471 | msg_callback(ctx_arg->msg_callback), |
| 472 | msg_callback_arg(ctx_arg->msg_callback_arg), |
| 473 | ctx(UpRef(ctx_arg)), |
| 474 | session_ctx(UpRef(ctx_arg)), |
| 475 | options(ctx->options), |
| 476 | mode(ctx->mode), |
| 477 | max_cert_list(ctx->max_cert_list), |
| 478 | server(false), |
| 479 | quiet_shutdown(ctx->quiet_shutdown), |
| 480 | enable_early_data(ctx->enable_early_data), |
| 481 | resumption_across_names_enabled(ctx->resumption_across_names_enabled) { |
| 482 | CRYPTO_new_ex_data(&ex_data); |
| 483 | } |
| 484 |
| 485 | ssl_st::~ssl_st() { |
| 486 | CRYPTO_free_ex_data(&g_ex_data_class_ssl, this, &ex_data); |
| 487 | // |config| refers to |this|, so we must release it earlier. |
| 488 | config.reset(); |
| 489 | if (method != NULL) { |
| 490 | method->ssl_free(this); |
| 491 | } |
| 492 | } |
| 493 |
| 494 | SSL *SSL_new(SSL_CTX *ctx) { |
| 495 | if (ctx == nullptr) { |
| 496 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX); |
| 497 | return nullptr; |
| 498 | } |
| 499 |
| 500 | UniquePtr<SSL> ssl = MakeUnique<SSL>(ctx); |
| 501 | if (ssl == nullptr) { |
| 502 | return nullptr; |
| 503 | } |
| 504 |
| 505 | ssl->config = MakeUnique<SSL_CONFIG>(ssl.get()); |
| 506 | if (ssl->config == nullptr) { |
| 507 | return nullptr; |
| 508 | } |
| 509 | ssl->config->conf_min_version = ctx->conf_min_version; |
| 510 | ssl->config->conf_max_version = ctx->conf_max_version; |
| 511 |
| 512 | ssl->config->cert = ssl_cert_dup(ctx->cert.get()); |
| 513 | if (ssl->config->cert == nullptr) { |
| 514 | return nullptr; |
| 515 | } |
| 516 |
| 517 | ssl->config->verify_mode = ctx->verify_mode; |
| 518 | ssl->config->verify_callback = ctx->default_verify_callback; |
| 519 | ssl->config->custom_verify_callback = ctx->custom_verify_callback; |
| 520 | ssl->config->retain_only_sha256_of_client_certs = |
| 521 | ctx->retain_only_sha256_of_client_certs; |
| 522 | ssl->config->permute_extensions = ctx->permute_extensions; |
| 523 | ssl->config->aes_hw_override = ctx->aes_hw_override; |
| 524 | ssl->config->aes_hw_override_value = ctx->aes_hw_override_value; |
| 525 | ssl->config->compliance_policy = ctx->compliance_policy; |
| 526 |
| 527 | if (!ssl->config->supported_group_list.CopyFrom(ctx->supported_group_list) || |
| 528 | !ssl->config->alpn_client_proto_list.CopyFrom( |
| 529 | ctx->alpn_client_proto_list) || |
| 530 | !ssl->config->verify_sigalgs.CopyFrom(ctx->verify_sigalgs)) { |
| 531 | return nullptr; |
| 532 | } |
| 533 |
| 534 | if (ctx->requested_trust_anchors) { |
| 535 | ssl->config->requested_trust_anchors.emplace(); |
| 536 | if (!ssl->config->requested_trust_anchors->CopyFrom( |
| 537 | *ctx->requested_trust_anchors)) { |
| 538 | return nullptr; |
| 539 | } |
| 540 | } |
| 541 |
| 542 | if (ctx->psk_identity_hint) { |
| 543 | ssl->config->psk_identity_hint.reset( |
| 544 | OPENSSL_strdup(ctx->psk_identity_hint.get())); |
| 545 | if (ssl->config->psk_identity_hint == nullptr) { |
| 546 | return nullptr; |
| 547 | } |
| 548 | } |
| 549 | ssl->config->psk_client_callback = ctx->psk_client_callback; |
| 550 | ssl->config->psk_server_callback = ctx->psk_server_callback; |
| 551 |
| 552 | ssl->config->channel_id_enabled = ctx->channel_id_enabled; |
| 553 | ssl->config->channel_id_private = UpRef(ctx->channel_id_private); |
| 554 |
| 555 | ssl->config->signed_cert_timestamps_enabled = |
| 556 | ctx->signed_cert_timestamps_enabled; |
| 557 | ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled; |
| 558 | ssl->config->handoff = ctx->handoff; |
| 559 | ssl->quic_method = ctx->quic_method; |
| 560 |
| 561 | if (!ssl->method->ssl_new(ssl.get()) || |
| 562 | !ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) { |
| 563 | return nullptr; |
| 564 | } |
| 565 |
| 566 | return ssl.release(); |
| 567 | } |
| 568 |
| 569 | SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg) |
| 570 | : ssl(ssl_arg), |
| 571 | ech_grease_enabled(false), |
| 572 | signed_cert_timestamps_enabled(false), |
| 573 | ocsp_stapling_enabled(false), |
| 574 | channel_id_enabled(false), |
| 575 | enforce_rsa_key_usage(true), |
| 576 | retain_only_sha256_of_client_certs(false), |
| 577 | handoff(false), |
| 578 | shed_handshake_config(false), |
| 579 | jdk11_workaround(false), |
| 580 | quic_use_legacy_codepoint(false), |
| 581 | permute_extensions(false), |
| 582 | alps_use_new_codepoint(false) { |
| 583 | assert(ssl); |
| 584 | } |
| 585 |
| 586 | SSL_CONFIG::~SSL_CONFIG() { |
| 587 | if (ssl->ctx != nullptr) { |
| 588 | ssl->ctx->x509_method->ssl_config_free(this); |
| 589 | } |
| 590 | } |
| 591 |
| 592 | void SSL_free(SSL *ssl) { Delete(ssl); } |
| 593 |
| 594 | void SSL_set_connect_state(SSL *ssl) { |
| 595 | ssl->server = false; |
| 596 | ssl->do_handshake = ssl_client_handshake; |
| 597 | } |
| 598 |
| 599 | void SSL_set_accept_state(SSL *ssl) { |
| 600 | ssl->server = true; |
| 601 | ssl->do_handshake = ssl_server_handshake; |
| 602 | } |
| 603 |
| 604 | void SSL_set0_rbio(SSL *ssl, BIO *rbio) { ssl->rbio.reset(rbio); } |
| 605 |
| 606 | void SSL_set0_wbio(SSL *ssl, BIO *wbio) { ssl->wbio.reset(wbio); } |
| 607 |
| 608 | void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) { |
| 609 | // For historical reasons, this function has many different cases in ownership |
| 610 | // handling. |
| 611 |
| 612 | // If nothing has changed, do nothing |
| 613 | if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) { |
| 614 | return; |
| 615 | } |
| 616 |
| 617 | // If the two arguments are equal, one fewer reference is granted than |
| 618 | // taken. |
| 619 | if (rbio != NULL && rbio == wbio) { |
| 620 | BIO_up_ref(rbio); |
| 621 | } |
| 622 |
| 623 | // If only the wbio is changed, adopt only one reference. |
| 624 | if (rbio == SSL_get_rbio(ssl)) { |
| 625 | SSL_set0_wbio(ssl, wbio); |
| 626 | return; |
| 627 | } |
| 628 |
| 629 | // There is an asymmetry here for historical reasons. If only the rbio is |
| 630 | // changed AND the rbio and wbio were originally different, then we only adopt |
| 631 | // one reference. |
| 632 | if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) { |
| 633 | SSL_set0_rbio(ssl, rbio); |
| 634 | return; |
| 635 | } |
| 636 |
| 637 | // Otherwise, adopt both references. |
| 638 | SSL_set0_rbio(ssl, rbio); |
| 639 | SSL_set0_wbio(ssl, wbio); |
| 640 | } |
| 641 |
| 642 | BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio.get(); } |
| 643 |
| 644 | BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio.get(); } |
| 645 |
| 646 | size_t SSL_quic_max_handshake_flight_len(const SSL *ssl, |
| 647 | enum ssl_encryption_level_t level) { |
| 648 | // Limits flights to 16K by default when there are no large |
| 649 | // (certificate-carrying) messages. |
| 650 | static const size_t kDefaultLimit = 16384; |
| 651 |
| 652 | switch (level) { |
| 653 | case ssl_encryption_initial: |
| 654 | return kDefaultLimit; |
| 655 | case ssl_encryption_early_data: |
| 656 | // QUIC does not send EndOfEarlyData. |
| 657 | return 0; |
| 658 | case ssl_encryption_handshake: |
| 659 | if (ssl->server) { |
| 660 | // Servers may receive Certificate message if configured to request |
| 661 | // client certificates. |
| 662 | if (!!(ssl->config->verify_mode & SSL_VERIFY_PEER) && |
| 663 | ssl->max_cert_list > kDefaultLimit) { |
| 664 | return ssl->max_cert_list; |
| 665 | } |
| 666 | } else { |
| 667 | // Clients may receive both Certificate message and a CertificateRequest |
| 668 | // message. |
| 669 | if (2 * ssl->max_cert_list > kDefaultLimit) { |
| 670 | return 2 * ssl->max_cert_list; |
| 671 | } |
| 672 | } |
| 673 | return kDefaultLimit; |
| 674 | case ssl_encryption_application: |
| 675 | // Note there is not actually a bound on the number of NewSessionTickets |
| 676 | // one may send in a row. This level may need more involved flow |
| 677 | // control. See https://github.com/quicwg/base-drafts/issues/1834. |
| 678 | return kDefaultLimit; |
| 679 | } |
| 680 |
| 681 | return 0; |
| 682 | } |
| 683 |
| 684 | enum ssl_encryption_level_t SSL_quic_read_level(const SSL *ssl) { |
| 685 | assert(SSL_is_quic(ssl)); |
| 686 | return ssl->s3->quic_read_level; |
| 687 | } |
| 688 |
| 689 | enum ssl_encryption_level_t SSL_quic_write_level(const SSL *ssl) { |
| 690 | assert(SSL_is_quic(ssl)); |
| 691 | return ssl->s3->quic_write_level; |
| 692 | } |
| 693 |
| 694 | int SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level, |
| 695 | const uint8_t *data, size_t len) { |
| 696 | if (!SSL_is_quic(ssl)) { |
| 697 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 698 | return 0; |
| 699 | } |
| 700 |
| 701 | if (level != ssl->s3->quic_read_level) { |
| 702 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED); |
| 703 | return 0; |
| 704 | } |
| 705 |
| 706 | size_t new_len = (ssl->s3->hs_buf ? ssl->s3->hs_buf->length : 0) + len; |
| 707 | if (new_len < len || |
| 708 | new_len > SSL_quic_max_handshake_flight_len(ssl, level)) { |
| 709 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 710 | return 0; |
| 711 | } |
| 712 |
| 713 | return tls_append_handshake_data(ssl, Span(data, len)); |
| 714 | } |
| 715 |
| 716 | int SSL_do_handshake(SSL *ssl) { |
| 717 | ssl_reset_error_state(ssl); |
| 718 |
| 719 | if (ssl->do_handshake == NULL) { |
| 720 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 721 | return -1; |
| 722 | } |
| 723 |
| 724 | if (!SSL_in_init(ssl)) { |
| 725 | return 1; |
| 726 | } |
| 727 |
| 728 | // Run the handshake. |
| 729 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 730 |
| 731 | bool early_return = false; |
| 732 | int ret = ssl_run_handshake(hs, &early_return); |
| 733 | ssl_do_info_callback( |
| 734 | ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret); |
| 735 | if (ret <= 0) { |
| 736 | return ret; |
| 737 | } |
| 738 |
| 739 | // Destroy the handshake object if the handshake has completely finished. |
| 740 | if (!early_return) { |
| 741 | ssl->s3->hs.reset(); |
| 742 | ssl_maybe_shed_handshake_config(ssl); |
| 743 | } |
| 744 |
| 745 | return 1; |
| 746 | } |
| 747 |
| 748 | int SSL_connect(SSL *ssl) { |
| 749 | if (ssl->do_handshake == NULL) { |
| 750 | // Not properly initialized yet |
| 751 | SSL_set_connect_state(ssl); |
| 752 | } |
| 753 |
| 754 | return SSL_do_handshake(ssl); |
| 755 | } |
| 756 |
| 757 | int SSL_accept(SSL *ssl) { |
| 758 | if (ssl->do_handshake == NULL) { |
| 759 | // Not properly initialized yet |
| 760 | SSL_set_accept_state(ssl); |
| 761 | } |
| 762 |
| 763 | return SSL_do_handshake(ssl); |
| 764 | } |
| 765 |
| 766 | static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) { |
| 767 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 768 | return tls13_post_handshake(ssl, msg); |
| 769 | } |
| 770 |
| 771 | // Check for renegotiation on the server before parsing to use the correct |
| 772 | // error. Renegotiation is triggered by a different message for servers. |
| 773 | if (ssl->server) { |
| 774 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 775 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 776 | return 0; |
| 777 | } |
| 778 |
| 779 | if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) { |
| 780 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 781 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST); |
| 782 | return 0; |
| 783 | } |
| 784 |
| 785 | if (ssl->renegotiate_mode == ssl_renegotiate_ignore) { |
| 786 | return 1; // Ignore the HelloRequest. |
| 787 | } |
| 788 |
| 789 | ssl->s3->renegotiate_pending = true; |
| 790 | if (ssl->renegotiate_mode == ssl_renegotiate_explicit) { |
| 791 | return 1; // Handle it later. |
| 792 | } |
| 793 |
| 794 | if (!SSL_renegotiate(ssl)) { |
| 795 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 796 | return 0; |
| 797 | } |
| 798 |
| 799 | return 1; |
| 800 | } |
| 801 |
| 802 | int SSL_process_quic_post_handshake(SSL *ssl) { |
| 803 | ssl_reset_error_state(ssl); |
| 804 |
| 805 | if (!SSL_is_quic(ssl) || SSL_in_init(ssl)) { |
| 806 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 807 | return 0; |
| 808 | } |
| 809 |
| 810 | // Replay post-handshake message errors. |
| 811 | if (!check_read_error(ssl)) { |
| 812 | return 0; |
| 813 | } |
| 814 |
| 815 | // Process any buffered post-handshake messages. |
| 816 | SSLMessage msg; |
| 817 | while (ssl->method->get_message(ssl, &msg)) { |
| 818 | // Handle the post-handshake message and try again. |
| 819 | if (!ssl_do_post_handshake(ssl, msg)) { |
| 820 | ssl_set_read_error(ssl); |
| 821 | return 0; |
| 822 | } |
| 823 | ssl->method->next_message(ssl); |
| 824 | } |
| 825 |
| 826 | return 1; |
| 827 | } |
| 828 |
| 829 | static int ssl_read_impl(SSL *ssl) { |
| 830 | ssl_reset_error_state(ssl); |
| 831 |
| 832 | if (ssl->do_handshake == NULL) { |
| 833 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 834 | return -1; |
| 835 | } |
| 836 |
| 837 | // Replay post-handshake message errors. |
| 838 | if (!check_read_error(ssl)) { |
| 839 | return -1; |
| 840 | } |
| 841 |
| 842 | while (ssl->s3->pending_app_data.empty()) { |
| 843 | if (ssl->s3->renegotiate_pending) { |
| 844 | ssl->s3->rwstate = SSL_ERROR_WANT_RENEGOTIATE; |
| 845 | return -1; |
| 846 | } |
| 847 |
| 848 | // If a read triggered a DTLS ACK or retransmit, resolve that before reading |
| 849 | // more. |
| 850 | if (SSL_is_dtls(ssl)) { |
| 851 | int ret = ssl->method->flush(ssl); |
| 852 | if (ret <= 0) { |
| 853 | return ret; |
| 854 | } |
| 855 | } |
| 856 |
| 857 | // Complete the current handshake, if any. False Start will cause |
| 858 | // |SSL_do_handshake| to return mid-handshake, so this may require multiple |
| 859 | // iterations. |
| 860 | while (!ssl_can_read(ssl)) { |
| 861 | int ret = SSL_do_handshake(ssl); |
| 862 | if (ret < 0) { |
| 863 | return ret; |
| 864 | } |
| 865 | if (ret == 0) { |
| 866 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 867 | return -1; |
| 868 | } |
| 869 | } |
| 870 |
| 871 | // Process any buffered post-handshake messages. |
| 872 | SSLMessage msg; |
| 873 | if (ssl->method->get_message(ssl, &msg)) { |
| 874 | // If we received an interrupt in early read (EndOfEarlyData), loop again |
| 875 | // for the handshake to process it. |
| 876 | if (SSL_in_init(ssl)) { |
| 877 | ssl->s3->hs->can_early_read = false; |
| 878 | continue; |
| 879 | } |
| 880 |
| 881 | // Handle the post-handshake message and try again. |
| 882 | if (!ssl_do_post_handshake(ssl, msg)) { |
| 883 | ssl_set_read_error(ssl); |
| 884 | return -1; |
| 885 | } |
| 886 | ssl->method->next_message(ssl); |
| 887 | continue; // Loop again. We may have begun a new handshake. |
| 888 | } |
| 889 |
| 890 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 891 | size_t consumed = 0; |
| 892 | auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed, |
| 893 | &alert, ssl->s3->read_buffer.span()); |
| 894 | bool retry; |
| 895 | int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert); |
| 896 | if (bio_ret <= 0) { |
| 897 | return bio_ret; |
| 898 | } |
| 899 | if (!retry) { |
| 900 | assert(!ssl->s3->pending_app_data.empty()); |
| 901 | ssl->s3->key_update_count = 0; |
| 902 | } |
| 903 | } |
| 904 |
| 905 | return 1; |
| 906 | } |
| 907 |
| 908 | int SSL_read(SSL *ssl, void *buf, int num) { |
| 909 | int ret = SSL_peek(ssl, buf, num); |
| 910 | if (ret <= 0) { |
| 911 | return ret; |
| 912 | } |
| 913 | // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS |
| 914 | // is not a stream. See https://crbug.com/boringssl/65. |
| 915 | ssl->s3->pending_app_data = |
| 916 | ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret)); |
| 917 | if (ssl->s3->pending_app_data.empty()) { |
| 918 | ssl->s3->read_buffer.DiscardConsumed(); |
| 919 | } |
| 920 | return ret; |
| 921 | } |
| 922 |
| 923 | int SSL_peek(SSL *ssl, void *buf, int num) { |
| 924 | if (SSL_is_quic(ssl)) { |
| 925 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 926 | return -1; |
| 927 | } |
| 928 |
| 929 | int ret = ssl_read_impl(ssl); |
| 930 | if (ret <= 0) { |
| 931 | return ret; |
| 932 | } |
| 933 | if (num <= 0) { |
| 934 | return num; |
| 935 | } |
| 936 | size_t todo = |
| 937 | std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num)); |
| 938 | OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo); |
| 939 | return static_cast<int>(todo); |
| 940 | } |
| 941 |
| 942 | int SSL_write(SSL *ssl, const void *buf, int num) { |
| 943 | ssl_reset_error_state(ssl); |
| 944 |
| 945 | if (SSL_is_quic(ssl)) { |
| 946 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 947 | return -1; |
| 948 | } |
| 949 |
| 950 | if (ssl->do_handshake == NULL) { |
| 951 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 952 | return -1; |
| 953 | } |
| 954 |
| 955 | int ret = 0; |
| 956 | size_t bytes_written = 0; |
| 957 | bool needs_handshake = false; |
| 958 | do { |
| 959 | // If necessary, complete the handshake implicitly. |
| 960 | if (!ssl_can_write(ssl)) { |
| 961 | ret = SSL_do_handshake(ssl); |
| 962 | if (ret < 0) { |
| 963 | return ret; |
| 964 | } |
| 965 | if (ret == 0) { |
| 966 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 967 | return -1; |
| 968 | } |
| 969 | } |
| 970 |
| 971 | if (num < 0) { |
| 972 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
| 973 | return -1; |
| 974 | } |
| 975 | ret = ssl->method->write_app_data( |
| 976 | ssl, &needs_handshake, &bytes_written, |
| 977 | Span(static_cast<const uint8_t *>(buf), static_cast<size_t>(num))); |
| 978 | } while (needs_handshake); |
| 979 | return ret <= 0 ? ret : static_cast<int>(bytes_written); |
| 980 | } |
| 981 |
| 982 | int SSL_key_update(SSL *ssl, int request_type) { |
| 983 | ssl_reset_error_state(ssl); |
| 984 |
| 985 | if (ssl->do_handshake == NULL) { |
| 986 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 987 | return 0; |
| 988 | } |
| 989 |
| 990 | if (SSL_is_quic(ssl)) { |
| 991 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 992 | return 0; |
| 993 | } |
| 994 |
| 995 | if (!ssl->s3->initial_handshake_complete) { |
| 996 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_NOT_COMPLETE); |
| 997 | return 0; |
| 998 | } |
| 999 |
| 1000 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 1001 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 1002 | return 0; |
| 1003 | } |
| 1004 |
| 1005 | return tls13_add_key_update(ssl, request_type); |
| 1006 | } |
| 1007 |
| 1008 | int SSL_shutdown(SSL *ssl) { |
| 1009 | ssl_reset_error_state(ssl); |
| 1010 |
| 1011 | if (ssl->do_handshake == NULL) { |
| 1012 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1013 | return -1; |
| 1014 | } |
| 1015 |
| 1016 | // If we are in the middle of a handshake, silently succeed. Consumers often |
| 1017 | // call this function before |SSL_free|, whether the handshake succeeded or |
| 1018 | // not. We assume the caller has already handled failed handshakes. |
| 1019 | if (SSL_in_init(ssl)) { |
| 1020 | return 1; |
| 1021 | } |
| 1022 |
| 1023 | if (ssl->quiet_shutdown) { |
| 1024 | // Do nothing if configured not to send a close_notify. |
| 1025 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
| 1026 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
| 1027 | return 1; |
| 1028 | } |
| 1029 |
| 1030 | // This function completes in two stages. It sends a close_notify and then it |
| 1031 | // waits for a close_notify to come in. Perform exactly one action and return |
| 1032 | // whether or not it succeeds. |
| 1033 |
| 1034 | if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) { |
| 1035 | // Send a close_notify. |
| 1036 | if (ssl_send_alert_impl(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) { |
| 1037 | return -1; |
| 1038 | } |
| 1039 | } else if (ssl->s3->alert_dispatch) { |
| 1040 | // Finish sending the close_notify. |
| 1041 | if (ssl->method->dispatch_alert(ssl) <= 0) { |
| 1042 | return -1; |
| 1043 | } |
| 1044 | } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) { |
| 1045 | if (SSL_is_dtls(ssl)) { |
| 1046 | // Bidirectional shutdown doesn't make sense for an unordered |
| 1047 | // transport. DTLS alerts also aren't delivered reliably, so we may even |
| 1048 | // time out because the peer never received our close_notify. Report to |
| 1049 | // the caller that the channel has fully shut down. |
| 1050 | if (ssl->s3->read_shutdown == ssl_shutdown_error) { |
| 1051 | ERR_restore_state(ssl->s3->read_error.get()); |
| 1052 | return -1; |
| 1053 | } |
| 1054 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
| 1055 | } else { |
| 1056 | // Process records until an error, close_notify, or application data. |
| 1057 | if (ssl_read_impl(ssl) > 0) { |
| 1058 | // We received some unexpected application data. |
| 1059 | OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_ON_SHUTDOWN); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) { |
| 1063 | return -1; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 |
| 1068 | // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. |
| 1069 | return ssl->s3->read_shutdown == ssl_shutdown_close_notify; |
| 1070 | } |
| 1071 |
| 1072 | int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) { |
| 1073 | if (ssl->s3->alert_dispatch) { |
| 1074 | if (ssl->s3->send_alert[0] != SSL3_AL_FATAL || |
| 1075 | ssl->s3->send_alert[1] != alert) { |
| 1076 | // We are already attempting to write a different alert. |
| 1077 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 1078 | return -1; |
| 1079 | } |
| 1080 | return ssl->method->dispatch_alert(ssl); |
| 1081 | } |
| 1082 |
| 1083 | return ssl_send_alert_impl(ssl, SSL3_AL_FATAL, alert); |
| 1084 | } |
| 1085 |
| 1086 | int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params, |
| 1087 | size_t params_len) { |
| 1088 | return ssl->config && |
| 1089 | ssl->config->quic_transport_params.CopyFrom(Span(params, params_len)); |
| 1090 | } |
| 1091 |
| 1092 | void SSL_get_peer_quic_transport_params(const SSL *ssl, |
| 1093 | const uint8_t **out_params, |
| 1094 | size_t *out_params_len) { |
| 1095 | *out_params = ssl->s3->peer_quic_transport_params.data(); |
| 1096 | *out_params_len = ssl->s3->peer_quic_transport_params.size(); |
| 1097 | } |
| 1098 |
| 1099 | int SSL_set_quic_early_data_context(SSL *ssl, const uint8_t *context, |
| 1100 | size_t context_len) { |
| 1101 | return ssl->config && ssl->config->quic_early_data_context.CopyFrom( |
| 1102 | Span(context, context_len)); |
| 1103 | } |
| 1104 |
| 1105 | void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) { |
| 1106 | ctx->enable_early_data = !!enabled; |
| 1107 | } |
| 1108 |
| 1109 | void SSL_set_early_data_enabled(SSL *ssl, int enabled) { |
| 1110 | ssl->enable_early_data = !!enabled; |
| 1111 | } |
| 1112 |
| 1113 | int SSL_in_early_data(const SSL *ssl) { |
| 1114 | if (ssl->s3->hs == NULL) { |
| 1115 | return 0; |
| 1116 | } |
| 1117 | return ssl->s3->hs->in_early_data; |
| 1118 | } |
| 1119 |
| 1120 | int SSL_early_data_accepted(const SSL *ssl) { |
| 1121 | return ssl->s3->early_data_accepted; |
| 1122 | } |
| 1123 |
| 1124 | void SSL_reset_early_data_reject(SSL *ssl) { |
| 1125 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 1126 | if (hs == NULL || // |
| 1127 | hs->wait != ssl_hs_early_data_rejected) { |
| 1128 | abort(); |
| 1129 | } |
| 1130 |
| 1131 | hs->wait = ssl_hs_ok; |
| 1132 | hs->in_early_data = false; |
| 1133 | hs->early_session.reset(); |
| 1134 |
| 1135 | // Discard any unfinished writes from the perspective of |SSL_write|'s |
| 1136 | // retry. The handshake will transparently flush out the pending record |
| 1137 | // (discarded by the server) to keep the framing correct. |
| 1138 | ssl->s3->pending_write = {}; |
| 1139 | } |
| 1140 |
| 1141 | enum ssl_early_data_reason_t SSL_get_early_data_reason(const SSL *ssl) { |
| 1142 | return ssl->s3->early_data_reason; |
| 1143 | } |
| 1144 |
| 1145 | const char *SSL_early_data_reason_string(enum ssl_early_data_reason_t reason) { |
| 1146 | switch (reason) { |
| 1147 | case ssl_early_data_unknown: |
| 1148 | return "unknown"; |
| 1149 | case ssl_early_data_disabled: |
| 1150 | return "disabled"; |
| 1151 | case ssl_early_data_accepted: |
| 1152 | return "accepted"; |
| 1153 | case ssl_early_data_protocol_version: |
| 1154 | return "protocol_version"; |
| 1155 | case ssl_early_data_peer_declined: |
| 1156 | return "peer_declined"; |
| 1157 | case ssl_early_data_no_session_offered: |
| 1158 | return "no_session_offered"; |
| 1159 | case ssl_early_data_session_not_resumed: |
| 1160 | return "session_not_resumed"; |
| 1161 | case ssl_early_data_unsupported_for_session: |
| 1162 | return "unsupported_for_session"; |
| 1163 | case ssl_early_data_hello_retry_request: |
| 1164 | return "hello_retry_request"; |
| 1165 | case ssl_early_data_alpn_mismatch: |
| 1166 | return "alpn_mismatch"; |
| 1167 | case ssl_early_data_channel_id: |
| 1168 | return "channel_id"; |
| 1169 | case ssl_early_data_ticket_age_skew: |
| 1170 | return "ticket_age_skew"; |
| 1171 | case ssl_early_data_quic_parameter_mismatch: |
| 1172 | return "quic_parameter_mismatch"; |
| 1173 | case ssl_early_data_alps_mismatch: |
| 1174 | return "alps_mismatch"; |
| 1175 | } |
| 1176 |
| 1177 | return nullptr; |
| 1178 | } |
| 1179 |
| 1180 | static int bio_retry_reason_to_error(int reason) { |
| 1181 | switch (reason) { |
| 1182 | case BIO_RR_CONNECT: |
| 1183 | return SSL_ERROR_WANT_CONNECT; |
| 1184 | case BIO_RR_ACCEPT: |
| 1185 | return SSL_ERROR_WANT_ACCEPT; |
| 1186 | default: |
| 1187 | return SSL_ERROR_SYSCALL; |
| 1188 | } |
| 1189 | } |
| 1190 |
| 1191 | int SSL_get_error(const SSL *ssl, int ret_code) { |
| 1192 | if (ret_code > 0) { |
| 1193 | return SSL_ERROR_NONE; |
| 1194 | } |
| 1195 |
| 1196 | // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, |
| 1197 | // where we do encode the error |
| 1198 | uint32_t err = ERR_peek_error(); |
| 1199 | if (err != 0) { |
| 1200 | if (ERR_GET_LIB(err) == ERR_LIB_SYS) { |
| 1201 | return SSL_ERROR_SYSCALL; |
| 1202 | } |
| 1203 | return SSL_ERROR_SSL; |
| 1204 | } |
| 1205 |
| 1206 | if (ret_code == 0) { |
| 1207 | if (ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN) { |
| 1208 | return SSL_ERROR_ZERO_RETURN; |
| 1209 | } |
| 1210 | // An EOF was observed which violates the protocol, and the underlying |
| 1211 | // transport does not participate in the error queue. Bubble up to the |
| 1212 | // caller. |
| 1213 | return SSL_ERROR_SYSCALL; |
| 1214 | } |
| 1215 |
| 1216 | switch (ssl->s3->rwstate) { |
| 1217 | case SSL_ERROR_PENDING_SESSION: |
| 1218 | case SSL_ERROR_PENDING_CERTIFICATE: |
| 1219 | case SSL_ERROR_HANDOFF: |
| 1220 | case SSL_ERROR_HANDBACK: |
| 1221 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 1222 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
| 1223 | case SSL_ERROR_PENDING_TICKET: |
| 1224 | case SSL_ERROR_EARLY_DATA_REJECTED: |
| 1225 | case SSL_ERROR_WANT_CERTIFICATE_VERIFY: |
| 1226 | case SSL_ERROR_WANT_RENEGOTIATE: |
| 1227 | case SSL_ERROR_HANDSHAKE_HINTS_READY: |
| 1228 | return ssl->s3->rwstate; |
| 1229 |
| 1230 | case SSL_ERROR_WANT_READ: { |
| 1231 | if (SSL_is_quic(ssl)) { |
| 1232 | return SSL_ERROR_WANT_READ; |
| 1233 | } |
| 1234 | BIO *bio = SSL_get_rbio(ssl); |
| 1235 | if (BIO_should_read(bio)) { |
| 1236 | return SSL_ERROR_WANT_READ; |
| 1237 | } |
| 1238 |
| 1239 | if (BIO_should_write(bio)) { |
| 1240 | // TODO(davidben): OpenSSL historically checked for writes on the read |
| 1241 | // BIO. Can this be removed? |
| 1242 | return SSL_ERROR_WANT_WRITE; |
| 1243 | } |
| 1244 |
| 1245 | if (BIO_should_io_special(bio)) { |
| 1246 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
| 1247 | } |
| 1248 |
| 1249 | break; |
| 1250 | } |
| 1251 |
| 1252 | case SSL_ERROR_WANT_WRITE: { |
| 1253 | BIO *bio = SSL_get_wbio(ssl); |
| 1254 | if (BIO_should_write(bio)) { |
| 1255 | return SSL_ERROR_WANT_WRITE; |
| 1256 | } |
| 1257 |
| 1258 | if (BIO_should_read(bio)) { |
| 1259 | // TODO(davidben): OpenSSL historically checked for reads on the write |
| 1260 | // BIO. Can this be removed? |
| 1261 | return SSL_ERROR_WANT_READ; |
| 1262 | } |
| 1263 |
| 1264 | if (BIO_should_io_special(bio)) { |
| 1265 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
| 1266 | } |
| 1267 |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 |
| 1272 | return SSL_ERROR_SYSCALL; |
| 1273 | } |
| 1274 |
| 1275 | const char *SSL_error_description(int err) { |
| 1276 | switch (err) { |
| 1277 | case SSL_ERROR_NONE: |
| 1278 | return "NONE"; |
| 1279 | case SSL_ERROR_SSL: |
| 1280 | return "SSL"; |
| 1281 | case SSL_ERROR_WANT_READ: |
| 1282 | return "WANT_READ"; |
| 1283 | case SSL_ERROR_WANT_WRITE: |
| 1284 | return "WANT_WRITE"; |
| 1285 | case SSL_ERROR_WANT_X509_LOOKUP: |
| 1286 | return "WANT_X509_LOOKUP"; |
| 1287 | case SSL_ERROR_SYSCALL: |
| 1288 | return "SYSCALL"; |
| 1289 | case SSL_ERROR_ZERO_RETURN: |
| 1290 | return "ZERO_RETURN"; |
| 1291 | case SSL_ERROR_WANT_CONNECT: |
| 1292 | return "WANT_CONNECT"; |
| 1293 | case SSL_ERROR_WANT_ACCEPT: |
| 1294 | return "WANT_ACCEPT"; |
| 1295 | case SSL_ERROR_PENDING_SESSION: |
| 1296 | return "PENDING_SESSION"; |
| 1297 | case SSL_ERROR_PENDING_CERTIFICATE: |
| 1298 | return "PENDING_CERTIFICATE"; |
| 1299 | case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
| 1300 | return "WANT_PRIVATE_KEY_OPERATION"; |
| 1301 | case SSL_ERROR_PENDING_TICKET: |
| 1302 | return "PENDING_TICKET"; |
| 1303 | case SSL_ERROR_EARLY_DATA_REJECTED: |
| 1304 | return "EARLY_DATA_REJECTED"; |
| 1305 | case SSL_ERROR_WANT_CERTIFICATE_VERIFY: |
| 1306 | return "WANT_CERTIFICATE_VERIFY"; |
| 1307 | case SSL_ERROR_HANDOFF: |
| 1308 | return "HANDOFF"; |
| 1309 | case SSL_ERROR_HANDBACK: |
| 1310 | return "HANDBACK"; |
| 1311 | case SSL_ERROR_WANT_RENEGOTIATE: |
| 1312 | return "WANT_RENEGOTIATE"; |
| 1313 | case SSL_ERROR_HANDSHAKE_HINTS_READY: |
| 1314 | return "HANDSHAKE_HINTS_READY"; |
| 1315 | default: |
| 1316 | return nullptr; |
| 1317 | } |
| 1318 | } |
| 1319 |
| 1320 | uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) { |
| 1321 | ctx->options |= options; |
| 1322 | return ctx->options; |
| 1323 | } |
| 1324 |
| 1325 | uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) { |
| 1326 | ctx->options &= ~options; |
| 1327 | return ctx->options; |
| 1328 | } |
| 1329 |
| 1330 | uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; } |
| 1331 |
| 1332 | uint32_t SSL_set_options(SSL *ssl, uint32_t options) { |
| 1333 | ssl->options |= options; |
| 1334 | return ssl->options; |
| 1335 | } |
| 1336 |
| 1337 | uint32_t SSL_clear_options(SSL *ssl, uint32_t options) { |
| 1338 | ssl->options &= ~options; |
| 1339 | return ssl->options; |
| 1340 | } |
| 1341 |
| 1342 | uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; } |
| 1343 |
| 1344 | uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1345 | ctx->mode |= mode; |
| 1346 | return ctx->mode; |
| 1347 | } |
| 1348 |
| 1349 | uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1350 | ctx->mode &= ~mode; |
| 1351 | return ctx->mode; |
| 1352 | } |
| 1353 |
| 1354 | uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; } |
| 1355 |
| 1356 | uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) { |
| 1357 | ssl->mode |= mode; |
| 1358 | return ssl->mode; |
| 1359 | } |
| 1360 |
| 1361 | uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) { |
| 1362 | ssl->mode &= ~mode; |
| 1363 | return ssl->mode; |
| 1364 | } |
| 1365 |
| 1366 | uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; } |
| 1367 |
| 1368 | void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) { |
| 1369 | ctx->pool = pool; |
| 1370 | } |
| 1371 |
| 1372 | int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len, |
| 1373 | size_t max_out) { |
| 1374 | *out_len = 0; |
| 1375 | OPENSSL_memset(out, 0, max_out); |
| 1376 |
| 1377 | // tls-unique is not defined for TLS 1.3. |
| 1378 | if (!ssl->s3->initial_handshake_complete || |
| 1379 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1380 | return 0; |
| 1381 | } |
| 1382 |
| 1383 | // The tls-unique value is the first Finished message in the handshake, which |
| 1384 | // is the client's in a full handshake and the server's for a resumption. See |
| 1385 | // https://tools.ietf.org/html/rfc5929#section-3.1. |
| 1386 | Span<const uint8_t> finished = ssl->s3->previous_client_finished; |
| 1387 | if (ssl->session != NULL) { |
| 1388 | // tls-unique is broken for resumed sessions unless EMS is used. |
| 1389 | if (!ssl->session->extended_master_secret) { |
| 1390 | return 0; |
| 1391 | } |
| 1392 | finished = ssl->s3->previous_server_finished; |
| 1393 | } |
| 1394 |
| 1395 | *out_len = finished.size(); |
| 1396 | if (finished.size() > max_out) { |
| 1397 | *out_len = max_out; |
| 1398 | } |
| 1399 |
| 1400 | OPENSSL_memcpy(out, finished.data(), *out_len); |
| 1401 | return 1; |
| 1402 | } |
| 1403 |
| 1404 | static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx, |
| 1405 | size_t sid_ctx_len) { |
| 1406 | if (!cert->sid_ctx.TryCopyFrom(Span(sid_ctx, sid_ctx_len))) { |
| 1407 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 1408 | return 0; |
| 1409 | } |
| 1410 |
| 1411 | return 1; |
| 1412 | } |
| 1413 |
| 1414 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx, |
| 1415 | size_t sid_ctx_len) { |
| 1416 | return set_session_id_context(ctx->cert.get(), sid_ctx, sid_ctx_len); |
| 1417 | } |
| 1418 |
| 1419 | int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx, |
| 1420 | size_t sid_ctx_len) { |
| 1421 | if (!ssl->config) { |
| 1422 | return 0; |
| 1423 | } |
| 1424 | return set_session_id_context(ssl->config->cert.get(), sid_ctx, sid_ctx_len); |
| 1425 | } |
| 1426 |
| 1427 | const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) { |
| 1428 | if (!ssl->config) { |
| 1429 | assert(ssl->config); |
| 1430 | *out_len = 0; |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | *out_len = ssl->config->cert->sid_ctx.size(); |
| 1434 | return ssl->config->cert->sid_ctx.data(); |
| 1435 | } |
| 1436 |
| 1437 | int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); } |
| 1438 |
| 1439 | int SSL_get_rfd(const SSL *ssl) { |
| 1440 | int ret = -1; |
| 1441 | BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1442 | if (b != NULL) { |
| 1443 | BIO_get_fd(b, &ret); |
| 1444 | } |
| 1445 | return ret; |
| 1446 | } |
| 1447 |
| 1448 | int SSL_get_wfd(const SSL *ssl) { |
| 1449 | int ret = -1; |
| 1450 | BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1451 | if (b != NULL) { |
| 1452 | BIO_get_fd(b, &ret); |
| 1453 | } |
| 1454 | return ret; |
| 1455 | } |
| 1456 |
| 1457 | #if !defined(OPENSSL_NO_SOCK) |
| 1458 | int SSL_set_fd(SSL *ssl, int fd) { |
| 1459 | BIO *bio = BIO_new(BIO_s_socket()); |
| 1460 | if (bio == NULL) { |
| 1461 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 1462 | return 0; |
| 1463 | } |
| 1464 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
| 1465 | SSL_set_bio(ssl, bio, bio); |
| 1466 | return 1; |
| 1467 | } |
| 1468 |
| 1469 | int SSL_set_wfd(SSL *ssl, int fd) { |
| 1470 | BIO *rbio = SSL_get_rbio(ssl); |
| 1471 | if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET || |
| 1472 | BIO_get_fd(rbio, NULL) != fd) { |
| 1473 | BIO *bio = BIO_new(BIO_s_socket()); |
| 1474 | if (bio == NULL) { |
| 1475 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
| 1479 | SSL_set0_wbio(ssl, bio); |
| 1480 | } else { |
| 1481 | // Copy the rbio over to the wbio. |
| 1482 | BIO_up_ref(rbio); |
| 1483 | SSL_set0_wbio(ssl, rbio); |
| 1484 | } |
| 1485 |
| 1486 | return 1; |
| 1487 | } |
| 1488 |
| 1489 | int SSL_set_rfd(SSL *ssl, int fd) { |
| 1490 | BIO *wbio = SSL_get_wbio(ssl); |
| 1491 | if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET || |
| 1492 | BIO_get_fd(wbio, NULL) != fd) { |
| 1493 | BIO *bio = BIO_new(BIO_s_socket()); |
| 1494 | if (bio == NULL) { |
| 1495 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
| 1496 | return 0; |
| 1497 | } |
| 1498 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
| 1499 | SSL_set0_rbio(ssl, bio); |
| 1500 | } else { |
| 1501 | // Copy the wbio over to the rbio. |
| 1502 | BIO_up_ref(wbio); |
| 1503 | SSL_set0_rbio(ssl, wbio); |
| 1504 | } |
| 1505 | return 1; |
| 1506 | } |
| 1507 | #endif // !OPENSSL_NO_SOCK |
| 1508 |
| 1509 | static size_t copy_finished(void *out, size_t out_len, Span<const uint8_t> in) { |
| 1510 | if (out_len > in.size()) { |
| 1511 | out_len = in.size(); |
| 1512 | } |
| 1513 | OPENSSL_memcpy(out, in.data(), out_len); |
| 1514 | return in.size(); |
| 1515 | } |
| 1516 |
| 1517 | size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) { |
| 1518 | if (!ssl->s3->initial_handshake_complete || |
| 1519 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1520 | return 0; |
| 1521 | } |
| 1522 |
| 1523 | if (ssl->server) { |
| 1524 | return copy_finished(buf, count, ssl->s3->previous_server_finished); |
| 1525 | } |
| 1526 |
| 1527 | return copy_finished(buf, count, ssl->s3->previous_client_finished); |
| 1528 | } |
| 1529 |
| 1530 | size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) { |
| 1531 | if (!ssl->s3->initial_handshake_complete || |
| 1532 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1533 | return 0; |
| 1534 | } |
| 1535 |
| 1536 | if (ssl->server) { |
| 1537 | return copy_finished(buf, count, ssl->s3->previous_client_finished); |
| 1538 | } |
| 1539 |
| 1540 | return copy_finished(buf, count, ssl->s3->previous_server_finished); |
| 1541 | } |
| 1542 |
| 1543 | int SSL_get_verify_mode(const SSL *ssl) { |
| 1544 | if (!ssl->config) { |
| 1545 | assert(ssl->config); |
| 1546 | return -1; |
| 1547 | } |
| 1548 | return ssl->config->verify_mode; |
| 1549 | } |
| 1550 |
| 1551 | int SSL_get_extms_support(const SSL *ssl) { |
| 1552 | // TLS 1.3 does not require extended master secret and always reports as |
| 1553 | // supporting it. |
| 1554 | if (ssl->s3->version == 0) { |
| 1555 | return 0; |
| 1556 | } |
| 1557 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1558 | return 1; |
| 1559 | } |
| 1560 |
| 1561 | // If the initial handshake completed, query the established session. |
| 1562 | if (ssl->s3->established_session != NULL) { |
| 1563 | return ssl->s3->established_session->extended_master_secret; |
| 1564 | } |
| 1565 |
| 1566 | // Otherwise, query the in-progress handshake. |
| 1567 | if (ssl->s3->hs != NULL) { |
| 1568 | return ssl->s3->hs->extended_master_secret; |
| 1569 | } |
| 1570 | assert(0); |
| 1571 | return 0; |
| 1572 | } |
| 1573 |
| 1574 | int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; } |
| 1575 |
| 1576 | int SSL_get_read_ahead(const SSL *ssl) { return 0; } |
| 1577 |
| 1578 | int SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { return 1; } |
| 1579 |
| 1580 | int SSL_set_read_ahead(SSL *ssl, int yes) { return 1; } |
| 1581 |
| 1582 | int SSL_pending(const SSL *ssl) { |
| 1583 | return static_cast<int>(ssl->s3->pending_app_data.size()); |
| 1584 | } |
| 1585 |
| 1586 | int SSL_has_pending(const SSL *ssl) { |
| 1587 | return SSL_pending(ssl) != 0 || !ssl->s3->read_buffer.empty(); |
| 1588 | } |
| 1589 |
| 1590 | static bool has_cert_and_key(const SSL_CREDENTIAL *cred) { |
| 1591 | // TODO(davidben): If |cred->key_method| is set, that should be fine too. |
| 1592 | if (cred->privkey == nullptr) { |
| 1593 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 1594 | return false; |
| 1595 | } |
| 1596 |
| 1597 | if (cred->chain == nullptr || |
| 1598 | sk_CRYPTO_BUFFER_value(cred->chain.get(), 0) == nullptr) { |
| 1599 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 1600 | return false; |
| 1601 | } |
| 1602 |
| 1603 | return true; |
| 1604 | } |
| 1605 |
| 1606 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) { |
| 1607 | // There is no need to actually check consistency because inconsistent values |
| 1608 | // can never be configured. |
| 1609 | return has_cert_and_key(ctx->cert->legacy_credential.get()); |
| 1610 | } |
| 1611 |
| 1612 | int SSL_check_private_key(const SSL *ssl) { |
| 1613 | if (!ssl->config) { |
| 1614 | return 0; |
| 1615 | } |
| 1616 |
| 1617 | // There is no need to actually check consistency because inconsistent values |
| 1618 | // can never be configured. |
| 1619 | return has_cert_and_key(ssl->config->cert->legacy_credential.get()); |
| 1620 | } |
| 1621 |
| 1622 | long SSL_get_default_timeout(const SSL *ssl) { |
| 1623 | return SSL_DEFAULT_SESSION_TIMEOUT; |
| 1624 | } |
| 1625 |
| 1626 | int SSL_renegotiate(SSL *ssl) { |
| 1627 | // Caller-initiated renegotiation is not supported. |
| 1628 | if (!ssl->s3->renegotiate_pending) { |
| 1629 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 1630 | return 0; |
| 1631 | } |
| 1632 |
| 1633 | if (!ssl_can_renegotiate(ssl)) { |
| 1634 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 1635 | return 0; |
| 1636 | } |
| 1637 |
| 1638 | // We should not have told the caller to release the private key. |
| 1639 | assert(!SSL_can_release_private_key(ssl)); |
| 1640 |
| 1641 | // Renegotiation is only supported at quiescent points in the application |
| 1642 | // protocol, namely in HTTPS, just before reading the HTTP response. |
| 1643 | // Require the record-layer be idle and avoid complexities of sending a |
| 1644 | // handshake record while an application_data record is being written. |
| 1645 | if (!ssl->s3->write_buffer.empty() || |
| 1646 | ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 1647 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 1648 | return 0; |
| 1649 | } |
| 1650 |
| 1651 | // Begin a new handshake. |
| 1652 | if (ssl->s3->hs != nullptr) { |
| 1653 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1654 | return 0; |
| 1655 | } |
| 1656 | ssl->s3->hs = ssl_handshake_new(ssl); |
| 1657 | if (ssl->s3->hs == nullptr) { |
| 1658 | return 0; |
| 1659 | } |
| 1660 |
| 1661 | ssl->s3->renegotiate_pending = false; |
| 1662 | ssl->s3->total_renegotiations++; |
| 1663 | return 1; |
| 1664 | } |
| 1665 |
| 1666 | int SSL_renegotiate_pending(SSL *ssl) { |
| 1667 | return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete; |
| 1668 | } |
| 1669 |
| 1670 | int SSL_total_renegotiations(const SSL *ssl) { |
| 1671 | return ssl->s3->total_renegotiations; |
| 1672 | } |
| 1673 |
| 1674 | size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) { |
| 1675 | return ctx->max_cert_list; |
| 1676 | } |
| 1677 |
| 1678 | void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) { |
| 1679 | if (max_cert_list > kMaxHandshakeSize) { |
| 1680 | max_cert_list = kMaxHandshakeSize; |
| 1681 | } |
| 1682 | ctx->max_cert_list = (uint32_t)max_cert_list; |
| 1683 | } |
| 1684 |
| 1685 | size_t SSL_get_max_cert_list(const SSL *ssl) { return ssl->max_cert_list; } |
| 1686 |
| 1687 | void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) { |
| 1688 | if (max_cert_list > kMaxHandshakeSize) { |
| 1689 | max_cert_list = kMaxHandshakeSize; |
| 1690 | } |
| 1691 | ssl->max_cert_list = (uint32_t)max_cert_list; |
| 1692 | } |
| 1693 |
| 1694 | int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) { |
| 1695 | if (max_send_fragment < 512) { |
| 1696 | max_send_fragment = 512; |
| 1697 | } |
| 1698 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1699 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1700 | } |
| 1701 | ctx->max_send_fragment = (uint16_t)max_send_fragment; |
| 1702 |
| 1703 | return 1; |
| 1704 | } |
| 1705 |
| 1706 | int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) { |
| 1707 | if (max_send_fragment < 512) { |
| 1708 | max_send_fragment = 512; |
| 1709 | } |
| 1710 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1711 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1712 | } |
| 1713 | ssl->max_send_fragment = (uint16_t)max_send_fragment; |
| 1714 |
| 1715 | return 1; |
| 1716 | } |
| 1717 |
| 1718 | int SSL_set_mtu(SSL *ssl, unsigned mtu) { |
| 1719 | if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) { |
| 1720 | return 0; |
| 1721 | } |
| 1722 | ssl->d1->mtu = mtu; |
| 1723 | return 1; |
| 1724 | } |
| 1725 |
| 1726 | int SSL_get_secure_renegotiation_support(const SSL *ssl) { |
| 1727 | if (ssl->s3->version == 0) { |
| 1728 | return 0; |
| 1729 | } |
| 1730 | return ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1731 | ssl->s3->send_connection_binding; |
| 1732 | } |
| 1733 |
| 1734 | size_t SSL_CTX_sess_number(const SSL_CTX *ctx) { |
| 1735 | MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
| 1736 | return lh_SSL_SESSION_num_items(ctx->sessions); |
| 1737 | } |
| 1738 |
| 1739 | unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) { |
| 1740 | unsigned long ret = ctx->session_cache_size; |
| 1741 | ctx->session_cache_size = size; |
| 1742 | return ret; |
| 1743 | } |
| 1744 |
| 1745 | unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) { |
| 1746 | return ctx->session_cache_size; |
| 1747 | } |
| 1748 |
| 1749 | int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) { |
| 1750 | int ret = ctx->session_cache_mode; |
| 1751 | ctx->session_cache_mode = mode; |
| 1752 | return ret; |
| 1753 | } |
| 1754 |
| 1755 | int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) { |
| 1756 | return ctx->session_cache_mode; |
| 1757 | } |
| 1758 |
| 1759 |
| 1760 | int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) { |
| 1761 | if (out == NULL) { |
| 1762 | return 48; |
| 1763 | } |
| 1764 | if (len != 48) { |
| 1765 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1766 | return 0; |
| 1767 | } |
| 1768 |
| 1769 | // The default ticket keys are initialized lazily. Trigger a key |
| 1770 | // rotation to initialize them. |
| 1771 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 1772 | return 0; |
| 1773 | } |
| 1774 |
| 1775 | uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out); |
| 1776 | MutexReadLock lock(&ctx->lock); |
| 1777 | OPENSSL_memcpy(out_bytes, ctx->ticket_key_current->name, 16); |
| 1778 | OPENSSL_memcpy(out_bytes + 16, ctx->ticket_key_current->hmac_key, 16); |
| 1779 | OPENSSL_memcpy(out_bytes + 32, ctx->ticket_key_current->aes_key, 16); |
| 1780 | return 1; |
| 1781 | } |
| 1782 |
| 1783 | int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) { |
| 1784 | if (in == NULL) { |
| 1785 | return 48; |
| 1786 | } |
| 1787 | if (len != 48) { |
| 1788 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1789 | return 0; |
| 1790 | } |
| 1791 | auto key = MakeUnique<TicketKey>(); |
| 1792 | if (!key) { |
| 1793 | return 0; |
| 1794 | } |
| 1795 | const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in); |
| 1796 | OPENSSL_memcpy(key->name, in_bytes, 16); |
| 1797 | OPENSSL_memcpy(key->hmac_key, in_bytes + 16, 16); |
| 1798 | OPENSSL_memcpy(key->aes_key, in_bytes + 32, 16); |
| 1799 | // Disable automatic key rotation for manually-configured keys. This is now |
| 1800 | // the caller's responsibility. |
| 1801 | key->next_rotation_tv_sec = 0; |
| 1802 | ctx->ticket_key_current = std::move(key); |
| 1803 | ctx->ticket_key_prev.reset(); |
| 1804 | return 1; |
| 1805 | } |
| 1806 |
| 1807 | int SSL_CTX_set_tlsext_ticket_key_cb( |
| 1808 | SSL_CTX *ctx, |
| 1809 | int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 1810 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, int encrypt)) { |
| 1811 | ctx->ticket_key_cb = callback; |
| 1812 | return 1; |
| 1813 | } |
| 1814 |
| 1815 | static bool check_group_ids(Span<const uint16_t> group_ids) { |
| 1816 | for (uint16_t group_id : group_ids) { |
| 1817 | if (ssl_group_id_to_nid(group_id) == NID_undef) { |
| 1818 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1819 | return false; |
| 1820 | } |
| 1821 | } |
| 1822 | return true; |
| 1823 | } |
| 1824 |
| 1825 | int SSL_CTX_set1_group_ids(SSL_CTX *ctx, const uint16_t *group_ids, |
| 1826 | size_t num_group_ids) { |
| 1827 | auto span = Span(group_ids, num_group_ids); |
| 1828 | return check_group_ids(span) && ctx->supported_group_list.CopyFrom(span); |
| 1829 | } |
| 1830 |
| 1831 | int SSL_set1_group_ids(SSL *ssl, const uint16_t *group_ids, |
| 1832 | size_t num_group_ids) { |
| 1833 | if (!ssl->config) { |
| 1834 | return 0; |
| 1835 | } |
| 1836 | auto span = Span(group_ids, num_group_ids); |
| 1837 | return check_group_ids(span) && |
| 1838 | ssl->config->supported_group_list.CopyFrom(span); |
| 1839 | } |
| 1840 |
| 1841 | static bool ssl_nids_to_group_ids(Array<uint16_t> *out_group_ids, |
| 1842 | Span<const int> nids) { |
| 1843 | Array<uint16_t> group_ids; |
| 1844 | if (!group_ids.InitForOverwrite(nids.size())) { |
| 1845 | return false; |
| 1846 | } |
| 1847 |
| 1848 | for (size_t i = 0; i < nids.size(); i++) { |
| 1849 | if (!ssl_nid_to_group_id(&group_ids[i], nids[i])) { |
| 1850 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1851 | return false; |
| 1852 | } |
| 1853 | } |
| 1854 |
| 1855 | *out_group_ids = std::move(group_ids); |
| 1856 | return true; |
| 1857 | } |
| 1858 |
| 1859 | int SSL_CTX_set1_groups(SSL_CTX *ctx, const int *groups, size_t num_groups) { |
| 1860 | return ssl_nids_to_group_ids(&ctx->supported_group_list, |
| 1861 | Span(groups, num_groups)); |
| 1862 | } |
| 1863 |
| 1864 | int SSL_set1_groups(SSL *ssl, const int *groups, size_t num_groups) { |
| 1865 | if (!ssl->config) { |
| 1866 | return 0; |
| 1867 | } |
| 1868 | return ssl_nids_to_group_ids(&ssl->config->supported_group_list, |
| 1869 | Span(groups, num_groups)); |
| 1870 | } |
| 1871 |
| 1872 | static bool ssl_str_to_group_ids(Array<uint16_t> *out_group_ids, |
| 1873 | const char *str) { |
| 1874 | // Count the number of groups in the list. |
| 1875 | size_t count = 0; |
| 1876 | const char *ptr = str, *col; |
| 1877 | do { |
| 1878 | col = strchr(ptr, ':'); |
| 1879 | count++; |
| 1880 | if (col) { |
| 1881 | ptr = col + 1; |
| 1882 | } |
| 1883 | } while (col); |
| 1884 |
| 1885 | Array<uint16_t> group_ids; |
| 1886 | if (!group_ids.InitForOverwrite(count)) { |
| 1887 | return false; |
| 1888 | } |
| 1889 |
| 1890 | size_t i = 0; |
| 1891 | ptr = str; |
| 1892 | do { |
| 1893 | col = strchr(ptr, ':'); |
| 1894 | if (!ssl_name_to_group_id(&group_ids[i++], ptr, |
| 1895 | col ? (size_t)(col - ptr) : strlen(ptr))) { |
| 1896 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1897 | return false; |
| 1898 | } |
| 1899 | if (col) { |
| 1900 | ptr = col + 1; |
| 1901 | } |
| 1902 | } while (col); |
| 1903 |
| 1904 | assert(i == count); |
| 1905 | *out_group_ids = std::move(group_ids); |
| 1906 | return true; |
| 1907 | } |
| 1908 |
| 1909 | int SSL_CTX_set1_groups_list(SSL_CTX *ctx, const char *groups) { |
| 1910 | return ssl_str_to_group_ids(&ctx->supported_group_list, groups); |
| 1911 | } |
| 1912 |
| 1913 | int SSL_set1_groups_list(SSL *ssl, const char *groups) { |
| 1914 | if (!ssl->config) { |
| 1915 | return 0; |
| 1916 | } |
| 1917 | return ssl_str_to_group_ids(&ssl->config->supported_group_list, groups); |
| 1918 | } |
| 1919 |
| 1920 | uint16_t SSL_get_group_id(const SSL *ssl) { |
| 1921 | SSL_SESSION *session = SSL_get_session(ssl); |
| 1922 | if (session == NULL) { |
| 1923 | return 0; |
| 1924 | } |
| 1925 |
| 1926 | return session->group_id; |
| 1927 | } |
| 1928 |
| 1929 | int SSL_get_negotiated_group(const SSL *ssl) { |
| 1930 | uint16_t group_id = SSL_get_group_id(ssl); |
| 1931 | if (group_id == 0) { |
| 1932 | return NID_undef; |
| 1933 | } |
| 1934 | return ssl_group_id_to_nid(group_id); |
| 1935 | } |
| 1936 |
| 1937 | int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) { return 1; } |
| 1938 |
| 1939 | int SSL_set_tmp_dh(SSL *ssl, const DH *dh) { return 1; } |
| 1940 |
| 1941 | STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) { |
| 1942 | return ctx->cipher_list->ciphers.get(); |
| 1943 | } |
| 1944 |
| 1945 | int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) { |
| 1946 | if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) { |
| 1947 | return 0; |
| 1948 | } |
| 1949 | return ctx->cipher_list->in_group_flags[i]; |
| 1950 | } |
| 1951 |
| 1952 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) { |
| 1953 | if (ssl == NULL) { |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | if (ssl->config == NULL) { |
| 1957 | assert(ssl->config); |
| 1958 | return NULL; |
| 1959 | } |
| 1960 |
| 1961 | return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get() |
| 1962 | : ssl->ctx->cipher_list->ciphers.get(); |
| 1963 | } |
| 1964 |
| 1965 | const char *SSL_get_cipher_list(const SSL *ssl, int n) { |
| 1966 | if (ssl == NULL) { |
| 1967 | return NULL; |
| 1968 | } |
| 1969 |
| 1970 | STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl); |
| 1971 | if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) { |
| 1972 | return NULL; |
| 1973 | } |
| 1974 |
| 1975 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n); |
| 1976 | if (c == NULL) { |
| 1977 | return NULL; |
| 1978 | } |
| 1979 |
| 1980 | return c->name; |
| 1981 | } |
| 1982 |
| 1983 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) { |
| 1984 | const bool has_aes_hw = ctx->aes_hw_override ? ctx->aes_hw_override_value |
| 1985 | : EVP_has_aes_hardware(); |
| 1986 | return ssl_create_cipher_list(&ctx->cipher_list, has_aes_hw, str, |
| 1987 | false /* not strict */); |
| 1988 | } |
| 1989 |
| 1990 | int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) { |
| 1991 | const bool has_aes_hw = ctx->aes_hw_override ? ctx->aes_hw_override_value |
| 1992 | : EVP_has_aes_hardware(); |
| 1993 | return ssl_create_cipher_list(&ctx->cipher_list, has_aes_hw, str, |
| 1994 | true /* strict */); |
| 1995 | } |
| 1996 |
| 1997 | int SSL_set_cipher_list(SSL *ssl, const char *str) { |
| 1998 | if (!ssl->config) { |
| 1999 | return 0; |
| 2000 | } |
| 2001 | const bool has_aes_hw = ssl->config->aes_hw_override |
| 2002 | ? ssl->config->aes_hw_override_value |
| 2003 | : EVP_has_aes_hardware(); |
| 2004 | return ssl_create_cipher_list(&ssl->config->cipher_list, has_aes_hw, str, |
| 2005 | false /* not strict */); |
| 2006 | } |
| 2007 |
| 2008 | int SSL_set_strict_cipher_list(SSL *ssl, const char *str) { |
| 2009 | if (!ssl->config) { |
| 2010 | return 0; |
| 2011 | } |
| 2012 | const bool has_aes_hw = ssl->config->aes_hw_override |
| 2013 | ? ssl->config->aes_hw_override_value |
| 2014 | : EVP_has_aes_hardware(); |
| 2015 | return ssl_create_cipher_list(&ssl->config->cipher_list, has_aes_hw, str, |
| 2016 | true /* strict */); |
| 2017 | } |
| 2018 |
| 2019 | const char *SSL_get_servername(const SSL *ssl, const int type) { |
| 2020 | if (type != TLSEXT_NAMETYPE_host_name) { |
| 2021 | return NULL; |
| 2022 | } |
| 2023 |
| 2024 | // Historically, |SSL_get_servername| was also the configuration getter |
| 2025 | // corresponding to |SSL_set_tlsext_host_name|. |
| 2026 | if (ssl->hostname != nullptr) { |
| 2027 | return ssl->hostname.get(); |
| 2028 | } |
| 2029 |
| 2030 | return ssl->s3->hostname.get(); |
| 2031 | } |
| 2032 |
| 2033 | int SSL_get_servername_type(const SSL *ssl) { |
| 2034 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) { |
| 2035 | return -1; |
| 2036 | } |
| 2037 | return TLSEXT_NAMETYPE_host_name; |
| 2038 | } |
| 2039 |
| 2040 | void SSL_CTX_set_custom_verify( |
| 2041 | SSL_CTX *ctx, int mode, |
| 2042 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
| 2043 | ctx->verify_mode = mode; |
| 2044 | ctx->custom_verify_callback = callback; |
| 2045 | } |
| 2046 |
| 2047 | void SSL_set_custom_verify( |
| 2048 | SSL *ssl, int mode, |
| 2049 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
| 2050 | if (!ssl->config) { |
| 2051 | return; |
| 2052 | } |
| 2053 | ssl->config->verify_mode = mode; |
| 2054 | ssl->config->custom_verify_callback = callback; |
| 2055 | } |
| 2056 |
| 2057 | void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) { |
| 2058 | ctx->signed_cert_timestamps_enabled = true; |
| 2059 | } |
| 2060 |
| 2061 | void SSL_enable_signed_cert_timestamps(SSL *ssl) { |
| 2062 | if (!ssl->config) { |
| 2063 | return; |
| 2064 | } |
| 2065 | ssl->config->signed_cert_timestamps_enabled = true; |
| 2066 | } |
| 2067 |
| 2068 | void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) { |
| 2069 | ctx->ocsp_stapling_enabled = true; |
| 2070 | } |
| 2071 |
| 2072 | void SSL_enable_ocsp_stapling(SSL *ssl) { |
| 2073 | if (!ssl->config) { |
| 2074 | return; |
| 2075 | } |
| 2076 | ssl->config->ocsp_stapling_enabled = true; |
| 2077 | } |
| 2078 |
| 2079 | void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, |
| 2080 | size_t *out_len) { |
| 2081 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2082 | if (ssl->server || !session || !session->signed_cert_timestamp_list) { |
| 2083 | *out_len = 0; |
| 2084 | *out = NULL; |
| 2085 | return; |
| 2086 | } |
| 2087 |
| 2088 | *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get()); |
| 2089 | *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get()); |
| 2090 | } |
| 2091 |
| 2092 | void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, |
| 2093 | size_t *out_len) { |
| 2094 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2095 | if (ssl->server || !session || !session->ocsp_response) { |
| 2096 | *out_len = 0; |
| 2097 | *out = NULL; |
| 2098 | return; |
| 2099 | } |
| 2100 |
| 2101 | *out = CRYPTO_BUFFER_data(session->ocsp_response.get()); |
| 2102 | *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get()); |
| 2103 | } |
| 2104 |
| 2105 | int SSL_set_tlsext_host_name(SSL *ssl, const char *name) { |
| 2106 | ssl->hostname.reset(); |
| 2107 | if (name == nullptr) { |
| 2108 | return 1; |
| 2109 | } |
| 2110 |
| 2111 | size_t len = strlen(name); |
| 2112 | if (len == 0 || len > TLSEXT_MAXLEN_host_name) { |
| 2113 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME); |
| 2114 | return 0; |
| 2115 | } |
| 2116 | ssl->hostname.reset(OPENSSL_strdup(name)); |
| 2117 | if (ssl->hostname == nullptr) { |
| 2118 | return 0; |
| 2119 | } |
| 2120 | return 1; |
| 2121 | } |
| 2122 |
| 2123 | int SSL_CTX_set_tlsext_servername_callback( |
| 2124 | SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) { |
| 2125 | ctx->servername_callback = callback; |
| 2126 | return 1; |
| 2127 | } |
| 2128 |
| 2129 | int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) { |
| 2130 | ctx->servername_arg = arg; |
| 2131 | return 1; |
| 2132 | } |
| 2133 |
| 2134 | int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer, |
| 2135 | unsigned peer_len, const uint8_t *supported, |
| 2136 | unsigned supported_len) { |
| 2137 | *out = nullptr; |
| 2138 | *out_len = 0; |
| 2139 |
| 2140 | // Both |peer| and |supported| must be valid protocol lists, but |peer| may be |
| 2141 | // empty in NPN. |
| 2142 | auto peer_span = Span(peer, peer_len); |
| 2143 | auto supported_span = Span(supported, supported_len); |
| 2144 | if ((!peer_span.empty() && !ssl_is_valid_alpn_list(peer_span)) || |
| 2145 | !ssl_is_valid_alpn_list(supported_span)) { |
| 2146 | return OPENSSL_NPN_NO_OVERLAP; |
| 2147 | } |
| 2148 |
| 2149 | // For each protocol in peer preference order, see if we support it. |
| 2150 | CBS cbs = peer_span, proto; |
| 2151 | while (CBS_len(&cbs) != 0) { |
| 2152 | if (!CBS_get_u8_length_prefixed(&cbs, &proto) || CBS_len(&proto) == 0) { |
| 2153 | return OPENSSL_NPN_NO_OVERLAP; |
| 2154 | } |
| 2155 |
| 2156 | if (ssl_alpn_list_contains_protocol(Span(supported, supported_len), |
| 2157 | proto)) { |
| 2158 | // This function is not const-correct for compatibility with existing |
| 2159 | // callers. |
| 2160 | *out = const_cast<uint8_t *>(CBS_data(&proto)); |
| 2161 | // A u8 length prefix will fit in |uint8_t|. |
| 2162 | *out_len = static_cast<uint8_t>(CBS_len(&proto)); |
| 2163 | return OPENSSL_NPN_NEGOTIATED; |
| 2164 | } |
| 2165 | } |
| 2166 |
| 2167 | // There's no overlap between our protocols and the peer's list. In ALPN, the |
| 2168 | // caller is expected to fail the connection with no_application_protocol. In |
| 2169 | // NPN, the caller is expected to opportunistically select the first protocol. |
| 2170 | // See draft-agl-tls-nextprotoneg-04, section 6. |
| 2171 | cbs = supported_span; |
| 2172 | if (!CBS_get_u8_length_prefixed(&cbs, &proto) || CBS_len(&proto) == 0) { |
| 2173 | return OPENSSL_NPN_NO_OVERLAP; |
| 2174 | } |
| 2175 |
| 2176 | // See above. |
| 2177 | *out = const_cast<uint8_t *>(CBS_data(&proto)); |
| 2178 | *out_len = static_cast<uint8_t>(CBS_len(&proto)); |
| 2179 | return OPENSSL_NPN_NO_OVERLAP; |
| 2180 | } |
| 2181 |
| 2182 | void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data, |
| 2183 | unsigned *out_len) { |
| 2184 | // NPN protocols have one-byte lengths, so they must fit in |unsigned|. |
| 2185 | assert(ssl->s3->next_proto_negotiated.size() <= UINT_MAX); |
| 2186 | *out_data = ssl->s3->next_proto_negotiated.data(); |
| 2187 | *out_len = static_cast<unsigned>(ssl->s3->next_proto_negotiated.size()); |
| 2188 | } |
| 2189 |
| 2190 | void SSL_CTX_set_next_protos_advertised_cb( |
| 2191 | SSL_CTX *ctx, |
| 2192 | int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg), |
| 2193 | void *arg) { |
| 2194 | ctx->next_protos_advertised_cb = cb; |
| 2195 | ctx->next_protos_advertised_cb_arg = arg; |
| 2196 | } |
| 2197 |
| 2198 | void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, |
| 2199 | int (*cb)(SSL *ssl, uint8_t **out, |
| 2200 | uint8_t *out_len, |
| 2201 | const uint8_t *in, |
| 2202 | unsigned in_len, void *arg), |
| 2203 | void *arg) { |
| 2204 | ctx->next_proto_select_cb = cb; |
| 2205 | ctx->next_proto_select_cb_arg = arg; |
| 2206 | } |
| 2207 |
| 2208 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos, |
| 2209 | size_t protos_len) { |
| 2210 | // Note this function's return value is backwards. |
| 2211 | auto span = Span(protos, protos_len); |
| 2212 | if (!span.empty() && !ssl_is_valid_alpn_list(span)) { |
| 2213 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL_LIST); |
| 2214 | return 1; |
| 2215 | } |
| 2216 | return ctx->alpn_client_proto_list.CopyFrom(span) ? 0 : 1; |
| 2217 | } |
| 2218 |
| 2219 | int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, size_t protos_len) { |
| 2220 | // Note this function's return value is backwards. |
| 2221 | if (!ssl->config) { |
| 2222 | return 1; |
| 2223 | } |
| 2224 | auto span = Span(protos, protos_len); |
| 2225 | if (!span.empty() && !ssl_is_valid_alpn_list(span)) { |
| 2226 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL_LIST); |
| 2227 | return 1; |
| 2228 | } |
| 2229 | return ssl->config->alpn_client_proto_list.CopyFrom(span) ? 0 : 1; |
| 2230 | } |
| 2231 |
| 2232 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, |
| 2233 | int (*cb)(SSL *ssl, const uint8_t **out, |
| 2234 | uint8_t *out_len, const uint8_t *in, |
| 2235 | unsigned in_len, void *arg), |
| 2236 | void *arg) { |
| 2237 | ctx->alpn_select_cb = cb; |
| 2238 | ctx->alpn_select_cb_arg = arg; |
| 2239 | } |
| 2240 |
| 2241 | void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data, |
| 2242 | unsigned *out_len) { |
| 2243 | Span<const uint8_t> protocol; |
| 2244 | if (SSL_in_early_data(ssl) && !ssl->server) { |
| 2245 | protocol = ssl->s3->hs->early_session->early_alpn; |
| 2246 | } else { |
| 2247 | protocol = ssl->s3->alpn_selected; |
| 2248 | } |
| 2249 | // ALPN protocols have one-byte lengths, so they must fit in |unsigned|. |
| 2250 | assert(protocol.size() < UINT_MAX); |
| 2251 | *out_data = protocol.data(); |
| 2252 | *out_len = static_cast<unsigned>(protocol.size()); |
| 2253 | } |
| 2254 |
| 2255 | void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) { |
| 2256 | ctx->allow_unknown_alpn_protos = !!enabled; |
| 2257 | } |
| 2258 |
| 2259 | int SSL_add_application_settings(SSL *ssl, const uint8_t *proto, |
| 2260 | size_t proto_len, const uint8_t *settings, |
| 2261 | size_t settings_len) { |
| 2262 | if (!ssl->config) { |
| 2263 | return 0; |
| 2264 | } |
| 2265 | ALPSConfig config; |
| 2266 | if (!config.protocol.CopyFrom(Span(proto, proto_len)) || |
| 2267 | !config.settings.CopyFrom(Span(settings, settings_len)) || |
| 2268 | !ssl->config->alps_configs.Push(std::move(config))) { |
| 2269 | return 0; |
| 2270 | } |
| 2271 | return 1; |
| 2272 | } |
| 2273 |
| 2274 | void SSL_get0_peer_application_settings(const SSL *ssl, |
| 2275 | const uint8_t **out_data, |
| 2276 | size_t *out_len) { |
| 2277 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 2278 | Span<const uint8_t> settings = |
| 2279 | session ? session->peer_application_settings : Span<const uint8_t>(); |
| 2280 | *out_data = settings.data(); |
| 2281 | *out_len = settings.size(); |
| 2282 | } |
| 2283 |
| 2284 | int SSL_has_application_settings(const SSL *ssl) { |
| 2285 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 2286 | return session && session->has_application_settings; |
| 2287 | } |
| 2288 |
| 2289 | void SSL_set_alps_use_new_codepoint(SSL *ssl, int use_new) { |
| 2290 | if (!ssl->config) { |
| 2291 | return; |
| 2292 | } |
| 2293 | ssl->config->alps_use_new_codepoint = !!use_new; |
| 2294 | } |
| 2295 |
| 2296 | int SSL_CTX_add_cert_compression_alg(SSL_CTX *ctx, uint16_t alg_id, |
| 2297 | ssl_cert_compression_func_t compress, |
| 2298 | ssl_cert_decompression_func_t decompress) { |
| 2299 | assert(compress != nullptr || decompress != nullptr); |
| 2300 |
| 2301 | for (const auto &alg : ctx->cert_compression_algs) { |
| 2302 | if (alg.alg_id == alg_id) { |
| 2303 | return 0; |
| 2304 | } |
| 2305 | } |
| 2306 |
| 2307 | CertCompressionAlg alg; |
| 2308 | alg.alg_id = alg_id; |
| 2309 | alg.compress = compress; |
| 2310 | alg.decompress = decompress; |
| 2311 | return ctx->cert_compression_algs.Push(alg); |
| 2312 | } |
| 2313 |
| 2314 | void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) { |
| 2315 | ctx->channel_id_enabled = !!enabled; |
| 2316 | } |
| 2317 |
| 2318 | int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) { |
| 2319 | SSL_CTX_set_tls_channel_id_enabled(ctx, 1); |
| 2320 | return 1; |
| 2321 | } |
| 2322 |
| 2323 | void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) { |
| 2324 | if (!ssl->config) { |
| 2325 | return; |
| 2326 | } |
| 2327 | ssl->config->channel_id_enabled = !!enabled; |
| 2328 | } |
| 2329 |
| 2330 | int SSL_enable_tls_channel_id(SSL *ssl) { |
| 2331 | SSL_set_tls_channel_id_enabled(ssl, 1); |
| 2332 | return 1; |
| 2333 | } |
| 2334 |
| 2335 | static int is_p256_key(EVP_PKEY *private_key) { |
| 2336 | const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key); |
| 2337 | return ec_key != NULL && EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) == |
| 2338 | NID_X9_62_prime256v1; |
| 2339 | } |
| 2340 |
| 2341 | int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) { |
| 2342 | if (!is_p256_key(private_key)) { |
| 2343 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2344 | return 0; |
| 2345 | } |
| 2346 |
| 2347 | ctx->channel_id_private = UpRef(private_key); |
| 2348 | return 1; |
| 2349 | } |
| 2350 |
| 2351 | int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) { |
| 2352 | if (!ssl->config) { |
| 2353 | return 0; |
| 2354 | } |
| 2355 | if (!is_p256_key(private_key)) { |
| 2356 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2357 | return 0; |
| 2358 | } |
| 2359 |
| 2360 | ssl->config->channel_id_private = UpRef(private_key); |
| 2361 | return 1; |
| 2362 | } |
| 2363 |
| 2364 | size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) { |
| 2365 | if (!ssl->s3->channel_id_valid) { |
| 2366 | return 0; |
| 2367 | } |
| 2368 | OPENSSL_memcpy(out, ssl->s3->channel_id, (max_out < 64) ? max_out : 64); |
| 2369 | return 64; |
| 2370 | } |
| 2371 |
| 2372 | size_t SSL_get0_certificate_types(const SSL *ssl, const uint8_t **out_types) { |
| 2373 | Span<const uint8_t> types; |
| 2374 | if (!ssl->server && ssl->s3->hs != nullptr) { |
| 2375 | types = ssl->s3->hs->certificate_types; |
| 2376 | } |
| 2377 | *out_types = types.data(); |
| 2378 | return types.size(); |
| 2379 | } |
| 2380 |
| 2381 | size_t SSL_get0_peer_verify_algorithms(const SSL *ssl, |
| 2382 | const uint16_t **out_sigalgs) { |
| 2383 | Span<const uint16_t> sigalgs; |
| 2384 | if (ssl->s3->hs != nullptr) { |
| 2385 | sigalgs = ssl->s3->hs->peer_sigalgs; |
| 2386 | } |
| 2387 | *out_sigalgs = sigalgs.data(); |
| 2388 | return sigalgs.size(); |
| 2389 | } |
| 2390 |
| 2391 | size_t SSL_get0_peer_delegation_algorithms(const SSL *ssl, |
| 2392 | const uint16_t **out_sigalgs) { |
| 2393 | Span<const uint16_t> sigalgs; |
| 2394 | if (ssl->s3->hs != nullptr) { |
| 2395 | sigalgs = ssl->s3->hs->peer_delegated_credential_sigalgs; |
| 2396 | } |
| 2397 | *out_sigalgs = sigalgs.data(); |
| 2398 | return sigalgs.size(); |
| 2399 | } |
| 2400 |
| 2401 | EVP_PKEY *SSL_get_privatekey(const SSL *ssl) { |
| 2402 | if (!ssl->config) { |
| 2403 | assert(ssl->config); |
| 2404 | return nullptr; |
| 2405 | } |
| 2406 | return ssl->config->cert->legacy_credential->privkey.get(); |
| 2407 | } |
| 2408 |
| 2409 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) { |
| 2410 | return ctx->cert->legacy_credential->privkey.get(); |
| 2411 | } |
| 2412 |
| 2413 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) { |
| 2414 | const SSL_SESSION *session = SSL_get_session(ssl); |
| 2415 | return session == nullptr ? nullptr : session->cipher; |
| 2416 | } |
| 2417 |
| 2418 | int SSL_session_reused(const SSL *ssl) { |
| 2419 | return ssl->s3->session_reused || SSL_in_early_data(ssl); |
| 2420 | } |
| 2421 |
| 2422 | const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; } |
| 2423 |
| 2424 | const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; } |
| 2425 |
| 2426 | int SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; } |
| 2427 |
| 2428 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) { |
| 2429 | ctx->quiet_shutdown = (mode != 0); |
| 2430 | } |
| 2431 |
| 2432 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) { |
| 2433 | return ctx->quiet_shutdown; |
| 2434 | } |
| 2435 |
| 2436 | void SSL_set_quiet_shutdown(SSL *ssl, int mode) { |
| 2437 | ssl->quiet_shutdown = (mode != 0); |
| 2438 | } |
| 2439 |
| 2440 | int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; } |
| 2441 |
| 2442 | void SSL_set_shutdown(SSL *ssl, int mode) { |
| 2443 | // It is an error to clear any bits that have already been set. (We can't try |
| 2444 | // to get a second close_notify or send two.) |
| 2445 | assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl)); |
| 2446 |
| 2447 | if (mode & SSL_RECEIVED_SHUTDOWN && |
| 2448 | ssl->s3->read_shutdown == ssl_shutdown_none) { |
| 2449 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
| 2450 | } |
| 2451 |
| 2452 | if (mode & SSL_SENT_SHUTDOWN && |
| 2453 | ssl->s3->write_shutdown == ssl_shutdown_none) { |
| 2454 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
| 2455 | } |
| 2456 | } |
| 2457 |
| 2458 | int SSL_get_shutdown(const SSL *ssl) { |
| 2459 | int ret = 0; |
| 2460 | if (ssl->s3->read_shutdown != ssl_shutdown_none) { |
| 2461 | // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify |
| 2462 | // and fatal alert. |
| 2463 | ret |= SSL_RECEIVED_SHUTDOWN; |
| 2464 | } |
| 2465 | if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) { |
| 2466 | // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. |
| 2467 | ret |= SSL_SENT_SHUTDOWN; |
| 2468 | } |
| 2469 | return ret; |
| 2470 | } |
| 2471 |
| 2472 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx.get(); } |
| 2473 |
| 2474 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) { |
| 2475 | if (!ssl->config) { |
| 2476 | return NULL; |
| 2477 | } |
| 2478 | if (ssl->ctx.get() == ctx) { |
| 2479 | return ssl->ctx.get(); |
| 2480 | } |
| 2481 |
| 2482 | // One cannot change the X.509 callbacks during a connection. |
| 2483 | if (ssl->ctx->x509_method != ctx->x509_method) { |
| 2484 | assert(0); |
| 2485 | return NULL; |
| 2486 | } |
| 2487 |
| 2488 | UniquePtr<CERT> new_cert = ssl_cert_dup(ctx->cert.get()); |
| 2489 | if (!new_cert) { |
| 2490 | return nullptr; |
| 2491 | } |
| 2492 |
| 2493 | ssl->config->cert = std::move(new_cert); |
| 2494 | ssl->ctx = UpRef(ctx); |
| 2495 | ssl->enable_early_data = ssl->ctx->enable_early_data; |
| 2496 |
| 2497 | return ssl->ctx.get(); |
| 2498 | } |
| 2499 |
| 2500 | void SSL_set_info_callback(SSL *ssl, |
| 2501 | void (*cb)(const SSL *ssl, int type, int value)) { |
| 2502 | ssl->info_callback = cb; |
| 2503 | } |
| 2504 |
| 2505 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, |
| 2506 | int value) { |
| 2507 | return ssl->info_callback; |
| 2508 | } |
| 2509 |
| 2510 | int SSL_state(const SSL *ssl) { |
| 2511 | return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK; |
| 2512 | } |
| 2513 |
| 2514 | void SSL_set_state(SSL *ssl, int state) {} |
| 2515 |
| 2516 | char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) { |
| 2517 | if (len <= 0) { |
| 2518 | return NULL; |
| 2519 | } |
| 2520 | buf[0] = '\0'; |
| 2521 | return buf; |
| 2522 | } |
| 2523 |
| 2524 | int SSL_get_shared_sigalgs(SSL *ssl, int idx, int *psign, int *phash, |
| 2525 | int *psignandhash, uint8_t *rsig, uint8_t *rhash) { |
| 2526 | return 0; |
| 2527 | } |
| 2528 |
| 2529 | int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method) { |
| 2530 | if (ctx->method->is_dtls) { |
| 2531 | return 0; |
| 2532 | } |
| 2533 | ctx->quic_method = quic_method; |
| 2534 | return 1; |
| 2535 | } |
| 2536 |
| 2537 | int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method) { |
| 2538 | if (ssl->method->is_dtls) { |
| 2539 | return 0; |
| 2540 | } |
| 2541 | ssl->quic_method = quic_method; |
| 2542 | return 1; |
| 2543 | } |
| 2544 |
| 2545 | int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 2546 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
| 2547 | return CRYPTO_get_ex_new_index_ex(&g_ex_data_class_ssl, argl, argp, |
| 2548 | free_func); |
| 2549 | } |
| 2550 |
| 2551 | int SSL_set_ex_data(SSL *ssl, int idx, void *data) { |
| 2552 | return CRYPTO_set_ex_data(&ssl->ex_data, idx, data); |
| 2553 | } |
| 2554 |
| 2555 | void *SSL_get_ex_data(const SSL *ssl, int idx) { |
| 2556 | return CRYPTO_get_ex_data(&ssl->ex_data, idx); |
| 2557 | } |
| 2558 |
| 2559 | int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 2560 | CRYPTO_EX_dup *dup_unused, |
| 2561 | CRYPTO_EX_free *free_func) { |
| 2562 | return CRYPTO_get_ex_new_index_ex(&g_ex_data_class_ssl_ctx, argl, argp, |
| 2563 | free_func); |
| 2564 | } |
| 2565 |
| 2566 | int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) { |
| 2567 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
| 2568 | } |
| 2569 |
| 2570 | void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) { |
| 2571 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
| 2572 | } |
| 2573 |
| 2574 | int SSL_want(const SSL *ssl) { |
| 2575 | // Historically, OpenSSL did not track |SSL_ERROR_ZERO_RETURN| as an |rwstate| |
| 2576 | // value. We do, but map it back to |SSL_ERROR_NONE| to preserve the original |
| 2577 | // behavior. |
| 2578 | return ssl->s3->rwstate == SSL_ERROR_ZERO_RETURN ? SSL_ERROR_NONE |
| 2579 | : ssl->s3->rwstate; |
| 2580 | } |
| 2581 |
| 2582 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, |
| 2583 | RSA *(*cb)(SSL *ssl, int is_export, |
| 2584 | int keylength)) {} |
| 2585 |
| 2586 | void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, |
| 2587 | int keylength)) {} |
| 2588 |
| 2589 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
| 2590 | DH *(*cb)(SSL *ssl, int is_export, |
| 2591 | int keylength)) {} |
| 2592 |
| 2593 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export, |
| 2594 | int keylength)) {} |
| 2595 |
| 2596 | static int use_psk_identity_hint(UniquePtr<char> *out, |
| 2597 | const char *identity_hint) { |
| 2598 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
| 2599 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
| 2600 | return 0; |
| 2601 | } |
| 2602 |
| 2603 | // Clear currently configured hint, if any. |
| 2604 | out->reset(); |
| 2605 |
| 2606 | // Treat the empty hint as not supplying one. Plain PSK makes it possible to |
| 2607 | // send either no hint (omit ServerKeyExchange) or an empty hint, while |
| 2608 | // ECDHE_PSK can only spell empty hint. Having different capabilities is odd, |
| 2609 | // so we interpret empty and missing as identical. |
| 2610 | if (identity_hint != NULL && identity_hint[0] != '\0') { |
| 2611 | out->reset(OPENSSL_strdup(identity_hint)); |
| 2612 | if (*out == nullptr) { |
| 2613 | return 0; |
| 2614 | } |
| 2615 | } |
| 2616 |
| 2617 | return 1; |
| 2618 | } |
| 2619 |
| 2620 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) { |
| 2621 | return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint); |
| 2622 | } |
| 2623 |
| 2624 | int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) { |
| 2625 | if (!ssl->config) { |
| 2626 | return 0; |
| 2627 | } |
| 2628 | return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint); |
| 2629 | } |
| 2630 |
| 2631 | const char *SSL_get_psk_identity_hint(const SSL *ssl) { |
| 2632 | if (ssl == NULL) { |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | if (ssl->config == NULL) { |
| 2636 | assert(ssl->config); |
| 2637 | return NULL; |
| 2638 | } |
| 2639 | return ssl->config->psk_identity_hint.get(); |
| 2640 | } |
| 2641 |
| 2642 | const char *SSL_get_psk_identity(const SSL *ssl) { |
| 2643 | if (ssl == NULL) { |
| 2644 | return NULL; |
| 2645 | } |
| 2646 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2647 | if (session == NULL) { |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | return session->psk_identity.get(); |
| 2651 | } |
| 2652 |
| 2653 | void SSL_set_psk_client_callback( |
| 2654 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2655 | unsigned max_identity_len, uint8_t *psk, |
| 2656 | unsigned max_psk_len)) { |
| 2657 | if (!ssl->config) { |
| 2658 | return; |
| 2659 | } |
| 2660 | ssl->config->psk_client_callback = cb; |
| 2661 | } |
| 2662 |
| 2663 | void SSL_CTX_set_psk_client_callback( |
| 2664 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2665 | unsigned max_identity_len, uint8_t *psk, |
| 2666 | unsigned max_psk_len)) { |
| 2667 | ctx->psk_client_callback = cb; |
| 2668 | } |
| 2669 |
| 2670 | void SSL_set_psk_server_callback(SSL *ssl, |
| 2671 | unsigned (*cb)(SSL *ssl, const char *identity, |
| 2672 | uint8_t *psk, |
| 2673 | unsigned max_psk_len)) { |
| 2674 | if (!ssl->config) { |
| 2675 | return; |
| 2676 | } |
| 2677 | ssl->config->psk_server_callback = cb; |
| 2678 | } |
| 2679 |
| 2680 | void SSL_CTX_set_psk_server_callback( |
| 2681 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk, |
| 2682 | unsigned max_psk_len)) { |
| 2683 | ctx->psk_server_callback = cb; |
| 2684 | } |
| 2685 |
| 2686 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
| 2687 | void (*cb)(int write_p, int version, |
| 2688 | int content_type, const void *buf, |
| 2689 | size_t len, SSL *ssl, void *arg)) { |
| 2690 | ctx->msg_callback = cb; |
| 2691 | } |
| 2692 |
| 2693 | void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) { |
| 2694 | ctx->msg_callback_arg = arg; |
| 2695 | } |
| 2696 |
| 2697 | void SSL_set_msg_callback(SSL *ssl, |
| 2698 | void (*cb)(int write_p, int version, int content_type, |
| 2699 | const void *buf, size_t len, SSL *ssl, |
| 2700 | void *arg)) { |
| 2701 | ssl->msg_callback = cb; |
| 2702 | } |
| 2703 |
| 2704 | void SSL_set_msg_callback_arg(SSL *ssl, void *arg) { |
| 2705 | ssl->msg_callback_arg = arg; |
| 2706 | } |
| 2707 |
| 2708 | void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, |
| 2709 | void (*cb)(const SSL *ssl, const char *line)) { |
| 2710 | ctx->keylog_callback = cb; |
| 2711 | } |
| 2712 |
| 2713 | void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl, |
| 2714 | const char *line) { |
| 2715 | return ctx->keylog_callback; |
| 2716 | } |
| 2717 |
| 2718 | void SSL_CTX_set_current_time_cb(SSL_CTX *ctx, |
| 2719 | void (*cb)(const SSL *ssl, |
| 2720 | struct timeval *out_clock)) { |
| 2721 | ctx->current_time_cb = cb; |
| 2722 | } |
| 2723 |
| 2724 | int SSL_can_release_private_key(const SSL *ssl) { |
| 2725 | if (ssl_can_renegotiate(ssl)) { |
| 2726 | // If the connection can renegotiate (client only), the private key may be |
| 2727 | // used in a future handshake. |
| 2728 | return 0; |
| 2729 | } |
| 2730 |
| 2731 | // Otherwise, this is determined by the current handshake. |
| 2732 | return !ssl->s3->hs || ssl->s3->hs->can_release_private_key; |
| 2733 | } |
| 2734 |
| 2735 | int SSL_is_init_finished(const SSL *ssl) { return !SSL_in_init(ssl); } |
| 2736 |
| 2737 | int SSL_in_init(const SSL *ssl) { |
| 2738 | // This returns false once all the handshake state has been finalized, to |
| 2739 | // allow callbacks and getters based on SSL_in_init to return the correct |
| 2740 | // values. |
| 2741 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 2742 | return hs != nullptr && !hs->handshake_finalized; |
| 2743 | } |
| 2744 |
| 2745 | int SSL_in_false_start(const SSL *ssl) { |
| 2746 | if (ssl->s3->hs == NULL) { |
| 2747 | return 0; |
| 2748 | } |
| 2749 | return ssl->s3->hs->in_false_start; |
| 2750 | } |
| 2751 |
| 2752 | int SSL_cutthrough_complete(const SSL *ssl) { return SSL_in_false_start(ssl); } |
| 2753 |
| 2754 | int SSL_is_server(const SSL *ssl) { return ssl->server; } |
| 2755 |
| 2756 | int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; } |
| 2757 |
| 2758 | int SSL_is_quic(const SSL *ssl) { return ssl->quic_method != nullptr; } |
| 2759 |
| 2760 | void SSL_CTX_set_select_certificate_cb( |
| 2761 | SSL_CTX *ctx, |
| 2762 | enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) { |
| 2763 | ctx->select_certificate_cb = cb; |
| 2764 | } |
| 2765 |
| 2766 | void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx, |
| 2767 | int (*cb)(const SSL_CLIENT_HELLO *)) { |
| 2768 | ctx->dos_protection_cb = cb; |
| 2769 | } |
| 2770 |
| 2771 | void SSL_CTX_set_reverify_on_resume(SSL_CTX *ctx, int enabled) { |
| 2772 | ctx->reverify_on_resume = !!enabled; |
| 2773 | } |
| 2774 |
| 2775 | void SSL_set_enforce_rsa_key_usage(SSL *ssl, int enabled) { |
| 2776 | if (!ssl->config) { |
| 2777 | return; |
| 2778 | } |
| 2779 | ssl->config->enforce_rsa_key_usage = !!enabled; |
| 2780 | } |
| 2781 |
| 2782 | int SSL_was_key_usage_invalid(const SSL *ssl) { |
| 2783 | return ssl->s3->was_key_usage_invalid; |
| 2784 | } |
| 2785 |
| 2786 | void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) { |
| 2787 | ssl->renegotiate_mode = mode; |
| 2788 |
| 2789 | // Check if |ssl_can_renegotiate| has changed and the configuration may now be |
| 2790 | // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then |
| 2791 | // disable after the handshake once the ALPN protocol is known to be HTTP/2. |
| 2792 | ssl_maybe_shed_handshake_config(ssl); |
| 2793 | } |
| 2794 |
| 2795 | int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv, |
| 2796 | const uint8_t **out_write_iv, size_t *out_iv_len) { |
| 2797 | // No cipher suites maintain stateful internal IVs in DTLS. It would not be |
| 2798 | // compatible with reordering. |
| 2799 | if (SSL_is_dtls(ssl)) { |
| 2800 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2801 | return 0; |
| 2802 | } |
| 2803 |
| 2804 | size_t write_iv_len; |
| 2805 | if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) || |
| 2806 | !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) || |
| 2807 | *out_iv_len != write_iv_len) { |
| 2808 | return 0; |
| 2809 | } |
| 2810 |
| 2811 | return 1; |
| 2812 | } |
| 2813 |
| 2814 | uint64_t SSL_get_read_sequence(const SSL *ssl) { |
| 2815 | if (SSL_is_dtls(ssl)) { |
| 2816 | // TODO(crbug.com/42290608): This API needs to reworked. |
| 2817 | // |
| 2818 | // In DTLS 1.2, right at an epoch transition, |read_epoch| may not have |
| 2819 | // received any records. We will then return that sequence 0 is the highest |
| 2820 | // received, but it's really -1, which is not representable. This is mostly |
| 2821 | // moot because, after the handshake, we will never be in the state. |
| 2822 | // |
| 2823 | // In DTLS 1.3, epochs do not transition until the first record comes in. |
| 2824 | // This avoids the DTLS 1.2 problem but introduces a different problem: |
| 2825 | // during a KeyUpdate (which may occur in the steady state), both epochs are |
| 2826 | // live. We'll likely need a new API for DTLS offload. |
| 2827 | const DTLSReadEpoch *read_epoch = &ssl->d1->read_epoch; |
| 2828 | return DTLSRecordNumber(read_epoch->epoch, read_epoch->bitmap.max_seq_num()) |
| 2829 | .combined(); |
| 2830 | } |
| 2831 | return ssl->s3->read_sequence; |
| 2832 | } |
| 2833 |
| 2834 | uint64_t SSL_get_write_sequence(const SSL *ssl) { |
| 2835 | if (SSL_is_dtls(ssl)) { |
| 2836 | return ssl->d1->write_epoch.next_record.combined(); |
| 2837 | } |
| 2838 |
| 2839 | return ssl->s3->write_sequence; |
| 2840 | } |
| 2841 |
| 2842 | uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) { |
| 2843 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2844 | if (session == NULL) { |
| 2845 | return 0; |
| 2846 | } |
| 2847 |
| 2848 | return session->peer_signature_algorithm; |
| 2849 | } |
| 2850 |
| 2851 | size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2852 | if (max_out == 0) { |
| 2853 | return sizeof(ssl->s3->client_random); |
| 2854 | } |
| 2855 | if (max_out > sizeof(ssl->s3->client_random)) { |
| 2856 | max_out = sizeof(ssl->s3->client_random); |
| 2857 | } |
| 2858 | OPENSSL_memcpy(out, ssl->s3->client_random, max_out); |
| 2859 | return max_out; |
| 2860 | } |
| 2861 |
| 2862 | size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2863 | if (max_out == 0) { |
| 2864 | return sizeof(ssl->s3->server_random); |
| 2865 | } |
| 2866 | if (max_out > sizeof(ssl->s3->server_random)) { |
| 2867 | max_out = sizeof(ssl->s3->server_random); |
| 2868 | } |
| 2869 | OPENSSL_memcpy(out, ssl->s3->server_random, max_out); |
| 2870 | return max_out; |
| 2871 | } |
| 2872 |
| 2873 | const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) { |
| 2874 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 2875 | if (hs == NULL) { |
| 2876 | return NULL; |
| 2877 | } |
| 2878 | return hs->new_cipher; |
| 2879 | } |
| 2880 |
| 2881 | void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) { |
| 2882 | if (!ssl->config) { |
| 2883 | return; |
| 2884 | } |
| 2885 | ssl->config->retain_only_sha256_of_client_certs = !!enabled; |
| 2886 | } |
| 2887 |
| 2888 | void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) { |
| 2889 | ctx->retain_only_sha256_of_client_certs = !!enabled; |
| 2890 | } |
| 2891 |
| 2892 | void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) { |
| 2893 | ctx->grease_enabled = !!enabled; |
| 2894 | } |
| 2895 |
| 2896 | void SSL_CTX_set_permute_extensions(SSL_CTX *ctx, int enabled) { |
| 2897 | ctx->permute_extensions = !!enabled; |
| 2898 | } |
| 2899 |
| 2900 | void SSL_set_permute_extensions(SSL *ssl, int enabled) { |
| 2901 | if (!ssl->config) { |
| 2902 | return; |
| 2903 | } |
| 2904 | ssl->config->permute_extensions = !!enabled; |
| 2905 | } |
| 2906 |
| 2907 | int32_t SSL_get_ticket_age_skew(const SSL *ssl) { |
| 2908 | return ssl->s3->ticket_age_skew; |
| 2909 | } |
| 2910 |
| 2911 | void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) { |
| 2912 | ctx->false_start_allowed_without_alpn = !!allowed; |
| 2913 | } |
| 2914 |
| 2915 | int SSL_used_hello_retry_request(const SSL *ssl) { |
| 2916 | return ssl->s3->used_hello_retry_request; |
| 2917 | } |
| 2918 |
| 2919 | void SSL_set_shed_handshake_config(SSL *ssl, int enable) { |
| 2920 | if (!ssl->config) { |
| 2921 | return; |
| 2922 | } |
| 2923 | ssl->config->shed_handshake_config = !!enable; |
| 2924 | } |
| 2925 |
| 2926 | void SSL_set_jdk11_workaround(SSL *ssl, int enable) { |
| 2927 | if (!ssl->config) { |
| 2928 | return; |
| 2929 | } |
| 2930 | ssl->config->jdk11_workaround = !!enable; |
| 2931 | } |
| 2932 |
| 2933 | void SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy) { |
| 2934 | if (!ssl->config) { |
| 2935 | return; |
| 2936 | } |
| 2937 | ssl->config->quic_use_legacy_codepoint = !!use_legacy; |
| 2938 | } |
| 2939 |
| 2940 | int SSL_clear(SSL *ssl) { |
| 2941 | if (!ssl->config) { |
| 2942 | return 0; // SSL_clear may not be used after shedding config. |
| 2943 | } |
| 2944 |
| 2945 | // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously |
| 2946 | // established session to be offered the next time around. wpa_supplicant |
| 2947 | // depends on this behavior, so emulate it. |
| 2948 | UniquePtr<SSL_SESSION> session; |
| 2949 | if (!ssl->server && ssl->s3->established_session != NULL) { |
| 2950 | session = UpRef(ssl->s3->established_session); |
| 2951 | } |
| 2952 |
| 2953 | // The ssl->d1->mtu is simultaneously configuration (preserved across |
| 2954 | // clear) and connection-specific state (gets reset). |
| 2955 | // |
| 2956 | // TODO(davidben): Avoid this. |
| 2957 | unsigned mtu = 0; |
| 2958 | if (ssl->d1 != NULL) { |
| 2959 | mtu = ssl->d1->mtu; |
| 2960 | } |
| 2961 |
| 2962 | ssl->method->ssl_free(ssl); |
| 2963 | if (!ssl->method->ssl_new(ssl)) { |
| 2964 | return 0; |
| 2965 | } |
| 2966 |
| 2967 | if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 2968 | ssl->d1->mtu = mtu; |
| 2969 | } |
| 2970 |
| 2971 | if (session != nullptr) { |
| 2972 | SSL_set_session(ssl, session.get()); |
| 2973 | } |
| 2974 |
| 2975 | return 1; |
| 2976 | } |
| 2977 |
| 2978 | int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; } |
| 2979 | int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; } |
| 2980 | int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2981 | int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; } |
| 2982 | int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2983 | int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; } |
| 2984 | int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; } |
| 2985 | int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; } |
| 2986 | int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; } |
| 2987 | int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; } |
| 2988 | int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; } |
| 2989 |
| 2990 | int SSL_num_renegotiations(const SSL *ssl) { |
| 2991 | return SSL_total_renegotiations(ssl); |
| 2992 | } |
| 2993 |
| 2994 | int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; } |
| 2995 | int SSL_need_tmp_RSA(const SSL *ssl) { return 0; } |
| 2996 | int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; } |
| 2997 | int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; } |
| 2998 | void ERR_load_SSL_strings(void) {} |
| 2999 | void SSL_load_error_strings(void) {} |
| 3000 | int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); } |
| 3001 |
| 3002 | int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) { |
| 3003 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 3004 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 3005 | return 0; |
| 3006 | } |
| 3007 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 3008 | return SSL_CTX_set1_groups(ctx, &nid, 1); |
| 3009 | } |
| 3010 |
| 3011 | int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) { |
| 3012 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 3013 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 3014 | return 0; |
| 3015 | } |
| 3016 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 3017 | return SSL_set1_groups(ssl, &nid, 1); |
| 3018 | } |
| 3019 |
| 3020 | void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx, |
| 3021 | const SSL_TICKET_AEAD_METHOD *aead_method) { |
| 3022 | ctx->ticket_aead_method = aead_method; |
| 3023 | } |
| 3024 |
| 3025 | SSL_SESSION *SSL_process_tls13_new_session_ticket(SSL *ssl, const uint8_t *buf, |
| 3026 | size_t buf_len) { |
| 3027 | if (SSL_in_init(ssl) || // |
| 3028 | ssl_protocol_version(ssl) != TLS1_3_VERSION || // |
| 3029 | ssl->server) { |
| 3030 | // Only TLS 1.3 clients are supported. |
| 3031 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 3032 | return nullptr; |
| 3033 | } |
| 3034 |
| 3035 | CBS cbs, body; |
| 3036 | CBS_init(&cbs, buf, buf_len); |
| 3037 | uint8_t type; |
| 3038 | if (!CBS_get_u8(&cbs, &type) || // |
| 3039 | !CBS_get_u24_length_prefixed(&cbs, &body) || // |
| 3040 | CBS_len(&cbs) != 0) { |
| 3041 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3042 | return nullptr; |
| 3043 | } |
| 3044 |
| 3045 | UniquePtr<SSL_SESSION> session = tls13_create_session_with_ticket(ssl, &body); |
| 3046 | if (!session) { |
| 3047 | // |tls13_create_session_with_ticket| puts the correct error. |
| 3048 | return nullptr; |
| 3049 | } |
| 3050 | return session.release(); |
| 3051 | } |
| 3052 |
| 3053 | int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets) { |
| 3054 | num_tickets = std::min(num_tickets, kMaxTickets); |
| 3055 | static_assert(kMaxTickets <= 0xff, "Too many tickets."); |
| 3056 | ctx->num_tickets = static_cast<uint8_t>(num_tickets); |
| 3057 | return 1; |
| 3058 | } |
| 3059 |
| 3060 | size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx) { return ctx->num_tickets; } |
| 3061 |
| 3062 | int SSL_set_tlsext_status_type(SSL *ssl, int type) { |
| 3063 | if (!ssl->config) { |
| 3064 | return 0; |
| 3065 | } |
| 3066 | ssl->config->ocsp_stapling_enabled = type == TLSEXT_STATUSTYPE_ocsp; |
| 3067 | return 1; |
| 3068 | } |
| 3069 |
| 3070 | int SSL_get_tlsext_status_type(const SSL *ssl) { |
| 3071 | if (ssl->server) { |
| 3072 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 3073 | return hs != nullptr && hs->ocsp_stapling_requested |
| 3074 | ? TLSEXT_STATUSTYPE_ocsp |
| 3075 | : TLSEXT_STATUSTYPE_nothing; |
| 3076 | } |
| 3077 |
| 3078 | return ssl->config != nullptr && ssl->config->ocsp_stapling_enabled |
| 3079 | ? TLSEXT_STATUSTYPE_ocsp |
| 3080 | : TLSEXT_STATUSTYPE_nothing; |
| 3081 | } |
| 3082 |
| 3083 | int SSL_set_tlsext_status_ocsp_resp(SSL *ssl, uint8_t *resp, size_t resp_len) { |
| 3084 | if (SSL_set_ocsp_response(ssl, resp, resp_len)) { |
| 3085 | OPENSSL_free(resp); |
| 3086 | return 1; |
| 3087 | } |
| 3088 | return 0; |
| 3089 | } |
| 3090 |
| 3091 | size_t SSL_get_tlsext_status_ocsp_resp(const SSL *ssl, const uint8_t **out) { |
| 3092 | size_t ret; |
| 3093 | SSL_get0_ocsp_response(ssl, out, &ret); |
| 3094 | return ret; |
| 3095 | } |
| 3096 |
| 3097 | int SSL_CTX_set_tlsext_status_cb(SSL_CTX *ctx, |
| 3098 | int (*callback)(SSL *ssl, void *arg)) { |
| 3099 | ctx->legacy_ocsp_callback = callback; |
| 3100 | return 1; |
| 3101 | } |
| 3102 |
| 3103 | int SSL_CTX_set_tlsext_status_arg(SSL_CTX *ctx, void *arg) { |
| 3104 | ctx->legacy_ocsp_callback_arg = arg; |
| 3105 | return 1; |
| 3106 | } |
| 3107 |
| 3108 | uint16_t SSL_get_curve_id(const SSL *ssl) { return SSL_get_group_id(ssl); } |
| 3109 |
| 3110 | const char *SSL_get_curve_name(uint16_t curve_id) { |
| 3111 | return SSL_get_group_name(curve_id); |
| 3112 | } |
| 3113 |
| 3114 | size_t SSL_get_all_curve_names(const char **out, size_t max_out) { |
| 3115 | return SSL_get_all_group_names(out, max_out); |
| 3116 | } |
| 3117 |
| 3118 | int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t num_curves) { |
| 3119 | return SSL_CTX_set1_groups(ctx, curves, num_curves); |
| 3120 | } |
| 3121 |
| 3122 | int SSL_set1_curves(SSL *ssl, const int *curves, size_t num_curves) { |
| 3123 | return SSL_set1_groups(ssl, curves, num_curves); |
| 3124 | } |
| 3125 |
| 3126 | int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) { |
| 3127 | return SSL_CTX_set1_groups_list(ctx, curves); |
| 3128 | } |
| 3129 |
| 3130 | int SSL_set1_curves_list(SSL *ssl, const char *curves) { |
| 3131 | return SSL_set1_groups_list(ssl, curves); |
| 3132 | } |
| 3133 |
| 3134 | namespace fips202205 { |
| 3135 |
| 3136 | // (References are to SP 800-52r2): |
| 3137 |
| 3138 | // Section 3.4.2.2 |
| 3139 | // "at least one of the NIST-approved curves, P-256 (secp256r1) and P384 |
| 3140 | // (secp384r1), shall be supported as described in RFC 8422." |
| 3141 | // |
| 3142 | // Section 3.3.1 |
| 3143 | // "The server shall be configured to only use cipher suites that are |
| 3144 | // composed entirely of NIST approved algorithms" |
| 3145 | static const uint16_t kGroups[] = {SSL_GROUP_SECP256R1, SSL_GROUP_SECP384R1}; |
| 3146 |
| 3147 | static const uint16_t kSigAlgs[] = { |
| 3148 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 3149 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 3150 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 3151 | // Table 4.1: |
| 3152 | // "The curve should be P-256 or P-384" |
| 3153 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 3154 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 3155 | SSL_SIGN_RSA_PSS_RSAE_SHA256, |
| 3156 | SSL_SIGN_RSA_PSS_RSAE_SHA384, |
| 3157 | SSL_SIGN_RSA_PSS_RSAE_SHA512, |
| 3158 | }; |
| 3159 |
| 3160 | static const char kTLS12Ciphers[] = |
| 3161 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:" |
| 3162 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:" |
| 3163 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:" |
| 3164 | "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; |
| 3165 |
| 3166 | static int Configure(SSL_CTX *ctx) { |
| 3167 | ctx->compliance_policy = ssl_compliance_policy_fips_202205; |
| 3168 |
| 3169 | return |
| 3170 | // Section 3.1: |
| 3171 | // "Servers that support government-only applications shall be |
| 3172 | // configured to use TLS 1.2 and should be configured to use TLS 1.3 |
| 3173 | // as well. These servers should not be configured to use TLS 1.1 and |
| 3174 | // shall not use TLS 1.0, SSL 3.0, or SSL 2.0. |
| 3175 | SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) && |
| 3176 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) && |
| 3177 | // Sections 3.3.1.1.1 and 3.3.1.1.2 are ambiguous about whether |
| 3178 | // HMAC-SHA-1 cipher suites are permitted with TLS 1.2. However, later the |
| 3179 | // Encrypt-then-MAC extension is required for all CBC cipher suites and so |
| 3180 | // it's easier to drop them. |
| 3181 | SSL_CTX_set_strict_cipher_list(ctx, kTLS12Ciphers) && |
| 3182 | SSL_CTX_set1_group_ids(ctx, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
| 3183 | SSL_CTX_set_signing_algorithm_prefs(ctx, kSigAlgs, |
| 3184 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3185 | SSL_CTX_set_verify_algorithm_prefs(ctx, kSigAlgs, |
| 3186 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3187 | } |
| 3188 |
| 3189 | static int Configure(SSL *ssl) { |
| 3190 | ssl->config->compliance_policy = ssl_compliance_policy_fips_202205; |
| 3191 |
| 3192 | // See |Configure(SSL_CTX)|, above, for reasoning. |
| 3193 | return SSL_set_min_proto_version(ssl, TLS1_2_VERSION) && |
| 3194 | SSL_set_max_proto_version(ssl, TLS1_3_VERSION) && |
| 3195 | SSL_set_strict_cipher_list(ssl, kTLS12Ciphers) && |
| 3196 | SSL_set1_group_ids(ssl, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
| 3197 | SSL_set_signing_algorithm_prefs(ssl, kSigAlgs, |
| 3198 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3199 | SSL_set_verify_algorithm_prefs(ssl, kSigAlgs, |
| 3200 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3201 | } |
| 3202 |
| 3203 | } // namespace fips202205 |
| 3204 |
| 3205 | namespace wpa202304 { |
| 3206 |
| 3207 | // See WPA version 3.1, section 3.5. |
| 3208 |
| 3209 | static const uint16_t kGroups[] = {SSL_GROUP_SECP384R1}; |
| 3210 |
| 3211 | static const uint16_t kSigAlgs[] = { |
| 3212 | SSL_SIGN_RSA_PKCS1_SHA384, // |
| 3213 | SSL_SIGN_RSA_PKCS1_SHA512, // |
| 3214 | SSL_SIGN_ECDSA_SECP384R1_SHA384, // |
| 3215 | SSL_SIGN_RSA_PSS_RSAE_SHA384, // |
| 3216 | SSL_SIGN_RSA_PSS_RSAE_SHA512, // |
| 3217 | }; |
| 3218 |
| 3219 | static const char kTLS12Ciphers[] = |
| 3220 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:" |
| 3221 | "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; |
| 3222 |
| 3223 | static int Configure(SSL_CTX *ctx) { |
| 3224 | ctx->compliance_policy = ssl_compliance_policy_wpa3_192_202304; |
| 3225 |
| 3226 | return SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) && |
| 3227 | SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) && |
| 3228 | SSL_CTX_set_strict_cipher_list(ctx, kTLS12Ciphers) && |
| 3229 | SSL_CTX_set1_group_ids(ctx, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
| 3230 | SSL_CTX_set_signing_algorithm_prefs(ctx, kSigAlgs, |
| 3231 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3232 | SSL_CTX_set_verify_algorithm_prefs(ctx, kSigAlgs, |
| 3233 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3234 | } |
| 3235 |
| 3236 | static int Configure(SSL *ssl) { |
| 3237 | ssl->config->compliance_policy = ssl_compliance_policy_wpa3_192_202304; |
| 3238 |
| 3239 | return SSL_set_min_proto_version(ssl, TLS1_2_VERSION) && |
| 3240 | SSL_set_max_proto_version(ssl, TLS1_3_VERSION) && |
| 3241 | SSL_set_strict_cipher_list(ssl, kTLS12Ciphers) && |
| 3242 | SSL_set1_group_ids(ssl, kGroups, OPENSSL_ARRAY_SIZE(kGroups)) && |
| 3243 | SSL_set_signing_algorithm_prefs(ssl, kSigAlgs, |
| 3244 | OPENSSL_ARRAY_SIZE(kSigAlgs)) && |
| 3245 | SSL_set_verify_algorithm_prefs(ssl, kSigAlgs, |
| 3246 | OPENSSL_ARRAY_SIZE(kSigAlgs)); |
| 3247 | } |
| 3248 |
| 3249 | } // namespace wpa202304 |
| 3250 |
| 3251 | namespace cnsa202407 { |
| 3252 |
| 3253 | static int Configure(SSL_CTX *ctx) { |
| 3254 | ctx->compliance_policy = ssl_compliance_policy_cnsa_202407; |
| 3255 | return 1; |
| 3256 | } |
| 3257 |
| 3258 | static int Configure(SSL *ssl) { |
| 3259 | ssl->config->compliance_policy = ssl_compliance_policy_cnsa_202407; |
| 3260 | return 1; |
| 3261 | } |
| 3262 |
| 3263 | } // namespace cnsa202407 |
| 3264 |
| 3265 | int SSL_CTX_set_compliance_policy(SSL_CTX *ctx, |
| 3266 | enum ssl_compliance_policy_t policy) { |
| 3267 | switch (policy) { |
| 3268 | case ssl_compliance_policy_fips_202205: |
| 3269 | return fips202205::Configure(ctx); |
| 3270 | case ssl_compliance_policy_wpa3_192_202304: |
| 3271 | return wpa202304::Configure(ctx); |
| 3272 | case ssl_compliance_policy_cnsa_202407: |
| 3273 | return cnsa202407::Configure(ctx); |
| 3274 | default: |
| 3275 | return 0; |
| 3276 | } |
| 3277 | } |
| 3278 |
| 3279 | enum ssl_compliance_policy_t SSL_CTX_get_compliance_policy(const SSL_CTX *ctx) { |
| 3280 | return ctx->compliance_policy; |
| 3281 | } |
| 3282 |
| 3283 | int SSL_set_compliance_policy(SSL *ssl, enum ssl_compliance_policy_t policy) { |
| 3284 | switch (policy) { |
| 3285 | case ssl_compliance_policy_fips_202205: |
| 3286 | return fips202205::Configure(ssl); |
| 3287 | case ssl_compliance_policy_wpa3_192_202304: |
| 3288 | return wpa202304::Configure(ssl); |
| 3289 | case ssl_compliance_policy_cnsa_202407: |
| 3290 | return cnsa202407::Configure(ssl); |
| 3291 | default: |
| 3292 | return 0; |
| 3293 | } |
| 3294 | } |
| 3295 |
| 3296 | enum ssl_compliance_policy_t SSL_get_compliance_policy(const SSL *ssl) { |
| 3297 | return ssl->config->compliance_policy; |
| 3298 | } |
| 3299 |
| 3300 | int SSL_peer_matched_trust_anchor(const SSL *ssl) { |
| 3301 | return ssl->s3->hs != nullptr && ssl->s3->hs->peer_matched_trust_anchor; |
| 3302 | } |
| 3303 |
| 3304 | void SSL_get0_peer_available_trust_anchors(const SSL *ssl, const uint8_t **out, |
| 3305 | size_t *out_len) { |
| 3306 | Span<const uint8_t> ret; |
| 3307 | if (SSL_HANDSHAKE *hs = ssl->s3->hs.get(); hs != nullptr) { |
| 3308 | ret = hs->peer_available_trust_anchors; |
| 3309 | } |
| 3310 | *out = ret.data(); |
| 3311 | *out_len = ret.size(); |
| 3312 | } |
| 3313 |
| 3314 | int SSL_CTX_set1_requested_trust_anchors(SSL_CTX *ctx, const uint8_t *ids, |
| 3315 | size_t ids_len) { |
| 3316 | auto span = Span(ids, ids_len); |
| 3317 | if (!ssl_is_valid_trust_anchor_list(span)) { |
| 3318 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TRUST_ANCHOR_LIST); |
| 3319 | return 0; |
| 3320 | } |
| 3321 | Array<uint8_t> copy; |
| 3322 | if (!copy.CopyFrom(span)) { |
| 3323 | return 0; |
| 3324 | } |
| 3325 | ctx->requested_trust_anchors = std::move(copy); |
| 3326 | return 1; |
| 3327 | } |
| 3328 |
| 3329 | int SSL_set1_requested_trust_anchors(SSL *ssl, const uint8_t *ids, |
| 3330 | size_t ids_len) { |
| 3331 | if (!ssl->config) { |
| 3332 | return 0; |
| 3333 | } |
| 3334 | auto span = Span(ids, ids_len); |
| 3335 | if (!ssl_is_valid_trust_anchor_list(span)) { |
| 3336 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TRUST_ANCHOR_LIST); |
| 3337 | return 0; |
| 3338 | } |
| 3339 | Array<uint8_t> copy; |
| 3340 | if (!copy.CopyFrom(span)) { |
| 3341 | return 0; |
| 3342 | } |
| 3343 | ssl->config->requested_trust_anchors = std::move(copy); |
| 3344 | return 1; |
| 3345 | } |
| 3346 |