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