Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 109 | #include <openssl/ssl.h> |
| 110 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <assert.h> |
Adam Langley | 87750b4 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 112 | #include <limits.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 113 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [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 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 121 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 123 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 124 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 126 | /* ssl3_get_record reads a new input record. On success, it places it in |
| 127 | * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if |
| 128 | * more data is needed. */ |
| 129 | static int ssl3_get_record(SSL *ssl) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | again: |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 131 | switch (ssl->s3->recv_shutdown) { |
| 132 | case ssl_shutdown_none: |
| 133 | break; |
| 134 | case ssl_shutdown_fatal_alert: |
| 135 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 136 | return -1; |
| 137 | case ssl_shutdown_close_notify: |
David Benjamin | 8f73135 | 2016-03-10 01:15:15 -0500 | [diff] [blame] | 138 | return 0; |
David Benjamin | 8f73135 | 2016-03-10 01:15:15 -0500 | [diff] [blame] | 139 | } |
| 140 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 141 | CBS body; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 142 | uint8_t type, alert; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 143 | size_t consumed; |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 144 | enum ssl_open_record_t open_ret = |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 145 | tls_open_record(ssl, &type, &body, &consumed, &alert, |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 146 | ssl_read_buffer(ssl), ssl_read_buffer_len(ssl)); |
| 147 | if (open_ret != ssl_open_record_partial) { |
| 148 | ssl_read_buffer_consume(ssl, consumed); |
| 149 | } |
| 150 | switch (open_ret) { |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 151 | case ssl_open_record_partial: { |
| 152 | int read_ret = ssl_read_buffer_extend_to(ssl, consumed); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 153 | if (read_ret <= 0) { |
| 154 | return read_ret; |
| 155 | } |
| 156 | goto again; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 157 | } |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 158 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 159 | case ssl_open_record_success: |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 160 | if (CBS_len(&body) > 0xffff) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 161 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 162 | return -1; |
| 163 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 165 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 166 | rr->type = type; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 167 | rr->length = (uint16_t)CBS_len(&body); |
| 168 | rr->data = (uint8_t *)CBS_data(&body); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 169 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 171 | case ssl_open_record_discard: |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 172 | goto again; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 174 | case ssl_open_record_close_notify: |
| 175 | return 0; |
| 176 | |
| 177 | case ssl_open_record_fatal_alert: |
| 178 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 180 | case ssl_open_record_error: |
| 181 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 182 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 183 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 185 | assert(0); |
| 186 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 187 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | |
David Benjamin | c933a47 | 2015-05-30 16:14:58 -0400 | [diff] [blame] | 190 | int ssl3_write_app_data(SSL *ssl, const void *buf, int len) { |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 191 | assert(!SSL_in_init(ssl) || SSL_in_false_start(ssl)); |
| 192 | |
David Benjamin | c933a47 | 2015-05-30 16:14:58 -0400 | [diff] [blame] | 193 | return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len); |
| 194 | } |
| 195 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 196 | /* Call this to write data in records of type |type|. It will return <= 0 if |
| 197 | * not all data has been sent or non-blocking IO. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 198 | int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, int len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | const uint8_t *buf = buf_; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 200 | unsigned tot, n, nw; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 202 | assert(ssl->s3->wnum <= INT_MAX); |
| 203 | tot = ssl->s3->wnum; |
| 204 | ssl->s3->wnum = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 206 | /* Ensure that if we end up with a smaller value of data to write out than |
| 207 | * the the original len from a write which didn't complete for non-blocking |
| 208 | * I/O and also somehow ended up avoiding the check for this in |
| 209 | * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 210 | * end up with (len-tot) as a large number that will then promptly send |
| 211 | * beyond the end of the users buffer ... so we trap and report the error in |
| 212 | * a way the user will notice. */ |
| 213 | if (len < 0 || (size_t)len < tot) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 214 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 215 | return -1; |
| 216 | } |
Adam Langley | 9611cfc | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 218 | n = (len - tot); |
| 219 | for (;;) { |
| 220 | /* max contains the maximum number of bytes that we can put into a |
| 221 | * record. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 222 | unsigned max = ssl->max_send_fragment; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 223 | if (n > max) { |
| 224 | nw = max; |
| 225 | } else { |
| 226 | nw = n; |
| 227 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 228 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 229 | int ret = do_ssl3_write(ssl, type, &buf[tot], nw); |
| 230 | if (ret <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 231 | ssl->s3->wnum = tot; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 232 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 233 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 235 | if (ret == (int)n || (type == SSL3_RT_APPLICATION_DATA && |
| 236 | (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { |
| 237 | return tot + ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 238 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 240 | n -= ret; |
| 241 | tot += ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 245 | static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf, |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 246 | unsigned int len) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 247 | if (ssl->s3->wpend_tot > (int)len || |
David Benjamin | c6722cd | 2016-10-24 20:13:20 -0400 | [diff] [blame] | 248 | (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) && |
| 249 | ssl->s3->wpend_buf != buf) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 250 | ssl->s3->wpend_type != type) { |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 251 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY); |
| 252 | return -1; |
| 253 | } |
| 254 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 255 | int ret = ssl_write_buffer_flush(ssl); |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 256 | if (ret <= 0) { |
| 257 | return ret; |
| 258 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 259 | return ssl->s3->wpend_ret; |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 260 | } |
| 261 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 262 | /* do_ssl3_write writes an SSL record of the given type. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 263 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 264 | /* If there is still data from the previous record, flush it. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 265 | if (ssl_write_buffer_is_pending(ssl)) { |
| 266 | return ssl3_write_pending(ssl, type, buf, len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 267 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | /* If we have an alert to send, lets send it */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 270 | if (ssl->s3->alert_dispatch) { |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 271 | int ret = ssl->method->dispatch_alert(ssl); |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 272 | if (ret <= 0) { |
| 273 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | } |
| 275 | /* if it went, fall through and send more stuff */ |
| 276 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 278 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 279 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 280 | return -1; |
| 281 | } |
Adam Langley | c6c8ae8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 283 | if (len == 0) { |
| 284 | return 0; |
| 285 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | da863608 | 2016-11-04 15:44:28 -0400 | [diff] [blame] | 287 | size_t max_out = len + SSL_max_seal_overhead(ssl); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 288 | if (max_out < len) { |
| 289 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 290 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 291 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 292 | uint8_t *out; |
| 293 | size_t ciphertext_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 294 | if (!ssl_write_buffer_init(ssl, &out, max_out) || |
| 295 | !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 296 | return -1; |
| 297 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 298 | ssl_write_buffer_set_len(ssl, ciphertext_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 300 | /* memorize arguments so that ssl3_write_pending can detect bad write retries |
| 301 | * later */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 302 | ssl->s3->wpend_tot = len; |
| 303 | ssl->s3->wpend_buf = buf; |
| 304 | ssl->s3->wpend_type = type; |
| 305 | ssl->s3->wpend_ret = len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 306 | |
| 307 | /* we now just need to write the buffer */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 308 | return ssl3_write_pending(ssl, type, buf, len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 309 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 311 | static int consume_record(SSL *ssl, uint8_t *out, int len, int peek) { |
| 312 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 313 | |
| 314 | if (len <= 0) { |
| 315 | return len; |
| 316 | } |
| 317 | |
| 318 | if (len > (int)rr->length) { |
| 319 | len = (int)rr->length; |
| 320 | } |
| 321 | |
| 322 | memcpy(out, rr->data, len); |
| 323 | if (!peek) { |
| 324 | rr->length -= len; |
| 325 | rr->data += len; |
| 326 | if (rr->length == 0) { |
| 327 | /* The record has been consumed, so we may now clear the buffer. */ |
| 328 | ssl_read_buffer_discard(ssl); |
| 329 | } |
| 330 | } |
| 331 | return len; |
| 332 | } |
| 333 | |
| 334 | int ssl3_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len, |
| 335 | int peek) { |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 336 | assert(!SSL_in_init(ssl)); |
David Benjamin | 4bea850 | 2016-05-12 09:34:55 -0400 | [diff] [blame] | 337 | assert(ssl->s3->initial_handshake_complete); |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 338 | *out_got_handshake = 0; |
David Benjamin | 4bea850 | 2016-05-12 09:34:55 -0400 | [diff] [blame] | 339 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 340 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 341 | |
| 342 | for (;;) { |
| 343 | /* A previous iteration may have read a partial handshake message. Do not |
| 344 | * allow more app data in that case. */ |
| 345 | int has_hs_data = ssl->init_buf != NULL && ssl->init_buf->length > 0; |
| 346 | |
| 347 | /* Get new packet if necessary. */ |
| 348 | if (rr->length == 0 && !has_hs_data) { |
| 349 | int ret = ssl3_get_record(ssl); |
| 350 | if (ret <= 0) { |
| 351 | return ret; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (has_hs_data || rr->type == SSL3_RT_HANDSHAKE) { |
| 356 | /* Post-handshake data prior to TLS 1.3 is always renegotiation, which we |
| 357 | * never accept as a server. Otherwise |ssl3_get_message| will send |
| 358 | * |SSL_R_EXCESSIVE_MESSAGE_SIZE|. */ |
| 359 | if (ssl->server && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 360 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 361 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | /* Parse post-handshake handshake messages. */ |
| 366 | int ret = ssl3_get_message(ssl, -1, ssl_dont_hash_message); |
| 367 | if (ret <= 0) { |
| 368 | return ret; |
| 369 | } |
| 370 | *out_got_handshake = 1; |
| 371 | return -1; |
| 372 | } |
| 373 | |
| 374 | if (rr->type != SSL3_RT_APPLICATION_DATA) { |
| 375 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 376 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | if (rr->length != 0) { |
| 381 | return consume_record(ssl, buf, len, peek); |
| 382 | } |
| 383 | |
| 384 | /* Discard empty records and loop again. */ |
| 385 | } |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 386 | } |
| 387 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 388 | int ssl3_read_change_cipher_spec(SSL *ssl) { |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame] | 389 | SSL3_RECORD *rr = &ssl->s3->rrec; |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 390 | |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame] | 391 | if (rr->length == 0) { |
| 392 | int ret = ssl3_get_record(ssl); |
| 393 | if (ret <= 0) { |
| 394 | return ret; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 399 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 400 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) { |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 405 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 406 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 407 | return -1; |
| 408 | } |
| 409 | |
David Benjamin | c027999 | 2016-09-19 20:15:07 -0400 | [diff] [blame] | 410 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, |
| 411 | rr->length); |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame] | 412 | |
| 413 | rr->length = 0; |
| 414 | ssl_read_buffer_discard(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 415 | return 1; |
| 416 | } |
| 417 | |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 418 | void ssl3_read_close_notify(SSL *ssl) { |
David Benjamin | 4aa4081 | 2016-06-01 19:40:23 -0400 | [diff] [blame] | 419 | /* Read records until an error or close_notify. */ |
| 420 | while (ssl3_get_record(ssl) > 0) { |
| 421 | ; |
| 422 | } |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 423 | } |
| 424 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 425 | int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len) { |
| 426 | SSL3_RECORD *rr = &ssl->s3->rrec; |
David Benjamin | 397c8e6 | 2016-07-08 14:14:36 -0700 | [diff] [blame] | 427 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 428 | for (;;) { |
| 429 | /* Get new packet if necessary. */ |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 430 | if (rr->length == 0) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 431 | int ret = ssl3_get_record(ssl); |
| 432 | if (ret <= 0) { |
| 433 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 437 | if (rr->type != SSL3_RT_HANDSHAKE) { |
| 438 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 439 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 440 | return -1; |
| 441 | } |
| 442 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 443 | if (rr->length != 0) { |
| 444 | return consume_record(ssl, buf, len, 0 /* consume data */); |
| 445 | } |
| 446 | |
| 447 | /* Discard empty records and loop again. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 448 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 449 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 451 | int ssl3_send_alert(SSL *ssl, int level, int desc) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 452 | /* It is illegal to send an alert when we've already sent a closing one. */ |
| 453 | if (ssl->s3->send_shutdown != ssl_shutdown_none) { |
| 454 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | if (level == SSL3_AL_FATAL) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 459 | ssl->s3->send_shutdown = ssl_shutdown_fatal_alert; |
| 460 | } else if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) { |
| 461 | ssl->s3->send_shutdown = ssl_shutdown_close_notify; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 462 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 464 | ssl->s3->alert_dispatch = 1; |
| 465 | ssl->s3->send_alert[0] = level; |
| 466 | ssl->s3->send_alert[1] = desc; |
| 467 | if (!ssl_write_buffer_is_pending(ssl)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 468 | /* Nothing is being written out, so the alert may be dispatched |
| 469 | * immediately. */ |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 470 | return ssl->method->dispatch_alert(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 473 | /* The alert will be dispatched later. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 474 | return -1; |
| 475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 477 | int ssl3_dispatch_alert(SSL *ssl) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 478 | ssl->s3->alert_dispatch = 0; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 479 | int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2); |
| 480 | if (ret <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 481 | ssl->s3->alert_dispatch = 1; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 482 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 483 | } |
| 484 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 485 | /* If the alert is fatal, flush the BIO now. */ |
| 486 | if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 487 | BIO_flush(ssl->wbio); |
| 488 | } |
| 489 | |
David Benjamin | c027999 | 2016-09-19 20:15:07 -0400 | [diff] [blame] | 490 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert, |
| 491 | 2); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 492 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 493 | int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 494 | ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 495 | |
| 496 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 497 | } |