| 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 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-2002 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 | #include <openssl/ssl.h> |
| 110 | |
| 111 | #include <assert.h> |
| 112 | #include <limits.h> |
| 113 | #include <string.h> |
| 114 | |
| 115 | #include <openssl/buf.h> |
| 116 | #include <openssl/err.h> |
| 117 | #include <openssl/evp.h> |
| 118 | #include <openssl/mem.h> |
| 119 | #include <openssl/rand.h> |
| 120 | |
| 121 | #include "../crypto/err/internal.h" |
| 122 | #include "../crypto/internal.h" |
| 123 | #include "internal.h" |
| 124 | |
| 125 | |
| 126 | BSSL_NAMESPACE_BEGIN |
| 127 | |
| 128 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *in, unsigned len); |
| 129 | |
| 130 | int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *in, |
| 131 | int len) { |
| 132 | assert(ssl_can_write(ssl)); |
| 133 | assert(!ssl->s3->aead_write_ctx->is_null_cipher()); |
| 134 | |
| 135 | *out_needs_handshake = false; |
| 136 | |
| 137 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 138 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | unsigned tot, n, nw; |
| 143 | |
| 144 | assert(ssl->s3->wnum <= INT_MAX); |
| 145 | tot = ssl->s3->wnum; |
| 146 | ssl->s3->wnum = 0; |
| 147 | |
| 148 | // Ensure that if we end up with a smaller value of data to write out than |
| 149 | // the the original len from a write which didn't complete for non-blocking |
| 150 | // I/O and also somehow ended up avoiding the check for this in |
| 151 | // ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 152 | // end up with (len-tot) as a large number that will then promptly send |
| 153 | // beyond the end of the users buffer ... so we trap and report the error in |
| 154 | // a way the user will notice. |
| 155 | if (len < 0 || (size_t)len < tot) { |
| 156 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | const int is_early_data_write = |
| 161 | !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write; |
| 162 | |
| 163 | n = len - tot; |
| 164 | for (;;) { |
| 165 | // max contains the maximum number of bytes that we can put into a record. |
| 166 | unsigned max = ssl->max_send_fragment; |
| 167 | if (is_early_data_write && |
| 168 | max > ssl->session->ticket_max_early_data - |
| 169 | ssl->s3->hs->early_data_written) { |
| 170 | max = |
| 171 | ssl->session->ticket_max_early_data - ssl->s3->hs->early_data_written; |
| 172 | if (max == 0) { |
| 173 | ssl->s3->wnum = tot; |
| 174 | ssl->s3->hs->can_early_write = false; |
| 175 | *out_needs_handshake = true; |
| 176 | return -1; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (n > max) { |
| 181 | nw = max; |
| 182 | } else { |
| 183 | nw = n; |
| 184 | } |
| 185 | |
| 186 | int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &in[tot], nw); |
| 187 | if (ret <= 0) { |
| 188 | ssl->s3->wnum = tot; |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | if (is_early_data_write) { |
| 193 | ssl->s3->hs->early_data_written += ret; |
| 194 | } |
| 195 | |
| 196 | if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) { |
| 197 | return tot + ret; |
| 198 | } |
| 199 | |
| 200 | n -= ret; |
| 201 | tot += ret; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *in, |
| 206 | unsigned int len) { |
| 207 | if (ssl->s3->wpend_tot > (int)len || |
| 208 | (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) && |
| 209 | ssl->s3->wpend_buf != in) || |
| 210 | ssl->s3->wpend_type != type) { |
| 211 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | int ret = ssl_write_buffer_flush(ssl); |
| 216 | if (ret <= 0) { |
| 217 | return ret; |
| 218 | } |
| 219 | ssl->s3->wpend_pending = false; |
| 220 | return ssl->s3->wpend_ret; |
| 221 | } |
| 222 | |
| 223 | // do_ssl3_write writes an SSL record of the given type. |
| 224 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *in, unsigned len) { |
| 225 | // If there is still data from the previous record, flush it. |
| 226 | if (ssl->s3->wpend_pending) { |
| 227 | return ssl3_write_pending(ssl, type, in, len); |
| 228 | } |
| 229 | |
| 230 | SSLBuffer *buf = &ssl->s3->write_buffer; |
| 231 | if (len > SSL3_RT_MAX_PLAIN_LENGTH || buf->size() > 0) { |
| 232 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | if (!tls_flush_pending_hs_data(ssl)) { |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | size_t flight_len = 0; |
| 241 | if (ssl->s3->pending_flight != nullptr) { |
| 242 | flight_len = |
| 243 | ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset; |
| 244 | } |
| 245 | |
| 246 | size_t max_out = flight_len; |
| 247 | if (len > 0) { |
| 248 | const size_t max_ciphertext_len = len + SSL_max_seal_overhead(ssl); |
| 249 | if (max_ciphertext_len < len || max_out + max_ciphertext_len < max_out) { |
| 250 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 251 | return -1; |
| 252 | } |
| 253 | max_out += max_ciphertext_len; |
| 254 | } |
| 255 | |
| 256 | if (max_out == 0) { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | if (!buf->EnsureCap(flight_len + ssl_seal_align_prefix_len(ssl), max_out)) { |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | // Add any unflushed handshake data as a prefix. This may be a KeyUpdate |
| 265 | // acknowledgment or 0-RTT key change messages. |pending_flight| must be clear |
| 266 | // when data is added to |write_buffer| or it will be written in the wrong |
| 267 | // order. |
| 268 | if (ssl->s3->pending_flight != nullptr) { |
| 269 | OPENSSL_memcpy( |
| 270 | buf->remaining().data(), |
| 271 | ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, |
| 272 | flight_len); |
| 273 | ssl->s3->pending_flight.reset(); |
| 274 | ssl->s3->pending_flight_offset = 0; |
| 275 | buf->DidWrite(flight_len); |
| 276 | } |
| 277 | |
| 278 | if (len > 0) { |
| 279 | size_t ciphertext_len; |
| 280 | if (!tls_seal_record(ssl, buf->remaining().data(), &ciphertext_len, |
| 281 | buf->remaining().size(), type, in, len)) { |
| 282 | return -1; |
| 283 | } |
| 284 | buf->DidWrite(ciphertext_len); |
| 285 | } |
| 286 | |
| 287 | // Now that we've made progress on the connection, uncork KeyUpdate |
| 288 | // acknowledgments. |
| 289 | ssl->s3->key_update_pending = false; |
| 290 | |
| 291 | // Memorize arguments so that ssl3_write_pending can detect bad write retries |
| 292 | // later. |
| 293 | ssl->s3->wpend_tot = len; |
| 294 | ssl->s3->wpend_buf = in; |
| 295 | ssl->s3->wpend_type = type; |
| 296 | ssl->s3->wpend_ret = len; |
| 297 | ssl->s3->wpend_pending = true; |
| 298 | |
| 299 | // We now just need to write the buffer. |
| 300 | return ssl3_write_pending(ssl, type, in, len); |
| 301 | } |
| 302 | |
| 303 | ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 304 | size_t *out_consumed, uint8_t *out_alert, |
| 305 | Span<uint8_t> in) { |
| 306 | assert(ssl_can_read(ssl)); |
| 307 | assert(!ssl->s3->aead_read_ctx->is_null_cipher()); |
| 308 | |
| 309 | uint8_t type; |
| 310 | Span<uint8_t> body; |
| 311 | auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in); |
| 312 | if (ret != ssl_open_record_success) { |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | const bool is_early_data_read = ssl->server && SSL_in_early_data(ssl); |
| 317 | |
| 318 | if (type == SSL3_RT_HANDSHAKE) { |
| 319 | // Post-handshake data prior to TLS 1.3 is always renegotiation, which we |
| 320 | // never accept as a server. Otherwise |ssl3_get_message| will send |
| 321 | // |SSL_R_EXCESSIVE_MESSAGE_SIZE|. |
| 322 | if (ssl->server && ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 323 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 324 | *out_alert = SSL_AD_NO_RENEGOTIATION; |
| 325 | return ssl_open_record_error; |
| 326 | } |
| 327 | |
| 328 | if (!tls_append_handshake_data(ssl, body)) { |
| 329 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 330 | return ssl_open_record_error; |
| 331 | } |
| 332 | return ssl_open_record_discard; |
| 333 | } |
| 334 | |
| 335 | if (type != SSL3_RT_APPLICATION_DATA) { |
| 336 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 337 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 338 | return ssl_open_record_error; |
| 339 | } |
| 340 | |
| 341 | if (is_early_data_read) { |
| 342 | if (body.size() > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) { |
| 343 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA); |
| 344 | *out_alert = SSL3_AD_UNEXPECTED_MESSAGE; |
| 345 | return ssl_open_record_error; |
| 346 | } |
| 347 | |
| 348 | ssl->s3->hs->early_data_read += body.size(); |
| 349 | } |
| 350 | |
| 351 | if (body.empty()) { |
| 352 | return ssl_open_record_discard; |
| 353 | } |
| 354 | |
| 355 | *out = body; |
| 356 | return ssl_open_record_success; |
| 357 | } |
| 358 | |
| 359 | ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 360 | uint8_t *out_alert, |
| 361 | Span<uint8_t> in) { |
| 362 | uint8_t type; |
| 363 | Span<uint8_t> body; |
| 364 | auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in); |
| 365 | if (ret != ssl_open_record_success) { |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | if (type != SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 370 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 371 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 372 | return ssl_open_record_error; |
| 373 | } |
| 374 | |
| 375 | if (body.size() != 1 || body[0] != SSL3_MT_CCS) { |
| 376 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 377 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 378 | return ssl_open_record_error; |
| 379 | } |
| 380 | |
| 381 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, body); |
| 382 | return ssl_open_record_success; |
| 383 | } |
| 384 | |
| 385 | void ssl_send_alert(SSL *ssl, int level, int desc) { |
| 386 | // This function is called in response to a fatal error from the peer. Ignore |
| 387 | // any failures writing the alert and report only the original error. In |
| 388 | // particular, if the transport uses |SSL_write|, our existing error will be |
| 389 | // clobbered so we must save and restore the error queue. See |
| 390 | // https://crbug.com/959305. |
| 391 | // |
| 392 | // TODO(davidben): Return the alert out of the handshake, rather than calling |
| 393 | // this function internally everywhere. |
| 394 | // |
| 395 | // TODO(davidben): This does not allow retrying if the alert hit EAGAIN. See |
| 396 | // https://crbug.com/boringssl/130. |
| 397 | UniquePtr<ERR_SAVE_STATE> err_state(ERR_save_state()); |
| 398 | ssl_send_alert_impl(ssl, level, desc); |
| 399 | ERR_restore_state(err_state.get()); |
| 400 | } |
| 401 | |
| 402 | int ssl_send_alert_impl(SSL *ssl, int level, int desc) { |
| 403 | // It is illegal to send an alert when we've already sent a closing one. |
| 404 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 405 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) { |
| 410 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
| 411 | } else { |
| 412 | assert(level == SSL3_AL_FATAL); |
| 413 | assert(desc != SSL_AD_CLOSE_NOTIFY); |
| 414 | ssl->s3->write_shutdown = ssl_shutdown_error; |
| 415 | } |
| 416 | |
| 417 | ssl->s3->alert_dispatch = 1; |
| 418 | ssl->s3->send_alert[0] = level; |
| 419 | ssl->s3->send_alert[1] = desc; |
| 420 | if (ssl->s3->write_buffer.empty()) { |
| 421 | // Nothing is being written out, so the alert may be dispatched |
| 422 | // immediately. |
| 423 | return ssl->method->dispatch_alert(ssl); |
| 424 | } |
| 425 | |
| 426 | // The alert will be dispatched later. |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | int ssl3_dispatch_alert(SSL *ssl) { |
| 431 | if (ssl->quic_method) { |
| 432 | if (!ssl->quic_method->send_alert(ssl, ssl->s3->write_level, |
| 433 | ssl->s3->send_alert[1])) { |
| 434 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); |
| 435 | return 0; |
| 436 | } |
| 437 | } else { |
| 438 | int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2); |
| 439 | if (ret <= 0) { |
| 440 | return ret; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | ssl->s3->alert_dispatch = 0; |
| 445 | |
| 446 | // If the alert is fatal, flush the BIO now. |
| 447 | if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 448 | BIO_flush(ssl->wbio.get()); |
| 449 | } |
| 450 | |
| 451 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert); |
| 452 | |
| 453 | int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 454 | ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
| 455 | |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | BSSL_NAMESPACE_END |
| 460 | |