Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 109 | #include <openssl/ssl.h> |
| 110 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 111 | #include <assert.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 112 | #include <limits.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 113 | #include <string.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 121 | #include "../crypto/internal.h" |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 122 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 123 | |
| 124 | |
Robert Sloan | 726e9d1 | 2018-09-11 11:45:04 -0700 | [diff] [blame] | 125 | BSSL_NAMESPACE_BEGIN |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 126 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 127 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *in, unsigned len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 128 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 129 | int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *in, |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 130 | int len) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 131 | assert(ssl_can_write(ssl)); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 132 | assert(!ssl->s3->aead_write_ctx->is_null_cipher()); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 133 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 134 | *out_needs_handshake = false; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 135 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 136 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 137 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 138 | return -1; |
| 139 | } |
| 140 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 141 | unsigned tot, n, nw; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 142 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 143 | assert(ssl->s3->wnum <= INT_MAX); |
| 144 | tot = ssl->s3->wnum; |
| 145 | ssl->s3->wnum = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 147 | // Ensure that if we end up with a smaller value of data to write out than |
| 148 | // the the original len from a write which didn't complete for non-blocking |
| 149 | // I/O and also somehow ended up avoiding the check for this in |
| 150 | // ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 151 | // end up with (len-tot) as a large number that will then promptly send |
| 152 | // beyond the end of the users buffer ... so we trap and report the error in |
| 153 | // a way the user will notice. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 154 | if (len < 0 || (size_t)len < tot) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 155 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 156 | return -1; |
| 157 | } |
| 158 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 159 | const int is_early_data_write = |
| 160 | !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write; |
| 161 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 162 | n = len - tot; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 163 | for (;;) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 164 | // max contains the maximum number of bytes that we can put into a record. |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 165 | unsigned max = ssl->max_send_fragment; |
Robert Sloan | cbf5ea6 | 2018-11-05 11:56:34 -0800 | [diff] [blame] | 166 | if (is_early_data_write && |
| 167 | max > ssl->session->ticket_max_early_data - |
| 168 | ssl->s3->hs->early_data_written) { |
| 169 | max = |
| 170 | ssl->session->ticket_max_early_data - ssl->s3->hs->early_data_written; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 171 | if (max == 0) { |
| 172 | ssl->s3->wnum = tot; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 173 | ssl->s3->hs->can_early_write = false; |
| 174 | *out_needs_handshake = true; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | } |
| 178 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 179 | if (n > max) { |
| 180 | nw = max; |
| 181 | } else { |
| 182 | nw = n; |
| 183 | } |
| 184 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 185 | int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &in[tot], nw); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 186 | if (ret <= 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 187 | ssl->s3->wnum = tot; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 188 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 191 | if (is_early_data_write) { |
| 192 | ssl->s3->hs->early_data_written += ret; |
| 193 | } |
| 194 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 195 | if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 196 | return tot + ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | } |
| 198 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 199 | n -= ret; |
| 200 | tot += ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 204 | static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *in, |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 205 | unsigned int len) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 206 | if (ssl->s3->wpend_tot > (int)len || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 207 | (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) && |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 208 | ssl->s3->wpend_buf != in) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 209 | ssl->s3->wpend_type != type) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 210 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY); |
| 211 | return -1; |
| 212 | } |
| 213 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 214 | int ret = ssl_write_buffer_flush(ssl); |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 215 | if (ret <= 0) { |
| 216 | return ret; |
| 217 | } |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 218 | ssl->s3->wpend_pending = false; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 219 | return ssl->s3->wpend_ret; |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 222 | // do_ssl3_write writes an SSL record of the given type. |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 223 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *in, unsigned len) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 224 | // If there is still data from the previous record, flush it. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 225 | if (ssl->s3->wpend_pending) { |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 226 | return ssl3_write_pending(ssl, type, in, len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 229 | SSLBuffer *buf = &ssl->s3->write_buffer; |
| 230 | if (len > SSL3_RT_MAX_PLAIN_LENGTH || buf->size() > 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 231 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | if (len == 0) { |
| 236 | return 0; |
| 237 | } |
| 238 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 239 | if (!tls_flush_pending_hs_data(ssl)) { |
| 240 | return -1; |
| 241 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 242 | size_t flight_len = 0; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 243 | if (ssl->s3->pending_flight != nullptr) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 244 | flight_len = |
| 245 | ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset; |
| 246 | } |
| 247 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 248 | size_t max_out = len + SSL_max_seal_overhead(ssl); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 249 | if (max_out < len || max_out + flight_len < max_out) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 250 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 251 | return -1; |
| 252 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 253 | max_out += flight_len; |
| 254 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 255 | if (!buf->EnsureCap(flight_len + ssl_seal_align_prefix_len(ssl), max_out)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 256 | return -1; |
| 257 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 258 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 259 | // Add any unflushed handshake data as a prefix. This may be a KeyUpdate |
| 260 | // acknowledgment or 0-RTT key change messages. |pending_flight| must be clear |
| 261 | // when data is added to |write_buffer| or it will be written in the wrong |
| 262 | // order. |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 263 | if (ssl->s3->pending_flight != nullptr) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 264 | OPENSSL_memcpy( |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 265 | buf->remaining().data(), |
| 266 | ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 267 | flight_len); |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 268 | ssl->s3->pending_flight.reset(); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 269 | ssl->s3->pending_flight_offset = 0; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 270 | buf->DidWrite(flight_len); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 273 | size_t ciphertext_len; |
| 274 | if (!tls_seal_record(ssl, buf->remaining().data(), &ciphertext_len, |
| 275 | buf->remaining().size(), type, in, len)) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 276 | return -1; |
| 277 | } |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 278 | buf->DidWrite(ciphertext_len); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 279 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 280 | // Now that we've made progress on the connection, uncork KeyUpdate |
| 281 | // acknowledgments. |
| 282 | ssl->s3->key_update_pending = false; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 284 | // Memorize arguments so that ssl3_write_pending can detect bad write retries |
| 285 | // later. |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 286 | ssl->s3->wpend_tot = len; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 287 | ssl->s3->wpend_buf = in; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 288 | ssl->s3->wpend_type = type; |
| 289 | ssl->s3->wpend_ret = len; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 290 | ssl->s3->wpend_pending = true; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 292 | // We now just need to write the buffer. |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 293 | return ssl3_write_pending(ssl, type, in, len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 296 | ssl_open_record_t ssl3_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 297 | size_t *out_consumed, uint8_t *out_alert, |
| 298 | Span<uint8_t> in) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 299 | assert(ssl_can_read(ssl)); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 300 | assert(!ssl->s3->aead_read_ctx->is_null_cipher()); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 301 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 302 | uint8_t type; |
| 303 | Span<uint8_t> body; |
| 304 | auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in); |
| 305 | if (ret != ssl_open_record_success) { |
| 306 | return ret; |
| 307 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 308 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 309 | const bool is_early_data_read = ssl->server && SSL_in_early_data(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 310 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 311 | if (type == SSL3_RT_HANDSHAKE) { |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 312 | // Post-handshake data prior to TLS 1.3 is always renegotiation, which we |
| 313 | // never accept as a server. Otherwise |ssl3_get_message| will send |
| 314 | // |SSL_R_EXCESSIVE_MESSAGE_SIZE|. |
| 315 | if (ssl->server && ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 316 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 317 | *out_alert = SSL_AD_NO_RENEGOTIATION; |
| 318 | return ssl_open_record_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 319 | } |
| 320 | |
Robert Sloan | a51059f | 2018-11-12 13:38:50 -0800 | [diff] [blame^] | 321 | if (!tls_append_handshake_data(ssl, body)) { |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 322 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 323 | return ssl_open_record_error; |
| 324 | } |
| 325 | return ssl_open_record_discard; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 328 | if (type != SSL3_RT_APPLICATION_DATA) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 329 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 330 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 331 | return ssl_open_record_error; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 334 | if (is_early_data_read) { |
| 335 | if (body.size() > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) { |
| 336 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA); |
| 337 | *out_alert = SSL3_AD_UNEXPECTED_MESSAGE; |
| 338 | return ssl_open_record_error; |
| 339 | } |
| 340 | |
| 341 | ssl->s3->hs->early_data_read += body.size(); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 344 | if (body.empty()) { |
| 345 | return ssl_open_record_discard; |
| 346 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 347 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 348 | *out = body; |
| 349 | return ssl_open_record_success; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 352 | ssl_open_record_t ssl3_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 353 | uint8_t *out_alert, |
| 354 | Span<uint8_t> in) { |
| 355 | uint8_t type; |
| 356 | Span<uint8_t> body; |
| 357 | auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in); |
| 358 | if (ret != ssl_open_record_success) { |
| 359 | return ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 360 | } |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 361 | |
| 362 | if (type != SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 363 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 364 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 365 | return ssl_open_record_error; |
| 366 | } |
| 367 | |
| 368 | if (body.size() != 1 || body[0] != SSL3_MT_CCS) { |
| 369 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 370 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 371 | return ssl_open_record_error; |
| 372 | } |
| 373 | |
| 374 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, body); |
| 375 | return ssl_open_record_success; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 378 | int ssl_send_alert(SSL *ssl, int level, int desc) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 379 | // It is illegal to send an alert when we've already sent a closing one. |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 380 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 381 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 382 | return -1; |
| 383 | } |
| 384 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 385 | if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 386 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 387 | } else { |
| 388 | assert(level == SSL3_AL_FATAL); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 389 | assert(desc != SSL_AD_CLOSE_NOTIFY); |
Robert Sloan | 3627296 | 2017-10-23 10:28:39 -0700 | [diff] [blame] | 390 | ssl->s3->write_shutdown = ssl_shutdown_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 393 | ssl->s3->alert_dispatch = 1; |
| 394 | ssl->s3->send_alert[0] = level; |
| 395 | ssl->s3->send_alert[1] = desc; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 396 | if (ssl->s3->write_buffer.empty()) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 397 | // Nothing is being written out, so the alert may be dispatched |
| 398 | // immediately. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 399 | return ssl->method->dispatch_alert(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 402 | // The alert will be dispatched later. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 403 | return -1; |
| 404 | } |
| 405 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 406 | int ssl3_dispatch_alert(SSL *ssl) { |
Robert Sloan | cbf5ea6 | 2018-11-05 11:56:34 -0800 | [diff] [blame] | 407 | if (ssl->ctx->quic_method) { |
| 408 | if (!ssl->ctx->quic_method->send_alert(ssl, ssl->s3->write_level, |
| 409 | ssl->s3->send_alert[1])) { |
| 410 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); |
| 411 | return 0; |
| 412 | } |
| 413 | } else { |
| 414 | int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2); |
| 415 | if (ret <= 0) { |
| 416 | return ret; |
| 417 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 418 | } |
Robert Sloan | cbf5ea6 | 2018-11-05 11:56:34 -0800 | [diff] [blame] | 419 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 420 | ssl->s3->alert_dispatch = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 421 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 422 | // If the alert is fatal, flush the BIO now. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 423 | if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 424 | BIO_flush(ssl->wbio.get()); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 427 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 428 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 429 | int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 430 | ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 431 | |
| 432 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 433 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 434 | |
Robert Sloan | 726e9d1 | 2018-09-11 11:45:04 -0700 | [diff] [blame] | 435 | BSSL_NAMESPACE_END |