David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include <openssl/bio.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/type_check.h> |
| 26 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 27 | #include "../crypto/internal.h" |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 28 | #include "internal.h" |
| 29 | |
| 30 | |
| 31 | OPENSSL_COMPILE_ASSERT(0xffff <= INT_MAX, uint16_fits_in_int); |
| 32 | |
| 33 | OPENSSL_COMPILE_ASSERT((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0, |
| 34 | align_to_a_power_of_two); |
| 35 | |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 36 | /* ensure_buffer ensures |buf| has capacity at least |cap|, aligned such that |
| 37 | * data written after |header_len| is aligned to a |SSL3_ALIGN_PAYLOAD|-byte |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 38 | * boundary. It returns one on success and zero on error. */ |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 39 | static int ensure_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) { |
| 40 | if (cap > 0xffff) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 41 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 42 | return 0; |
| 43 | } |
| 44 | |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 45 | if (buf->cap >= cap) { |
| 46 | return 1; |
| 47 | } |
| 48 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 49 | /* Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. */ |
David Benjamin | e64d2c7 | 2017-07-12 16:31:08 -0400 | [diff] [blame^] | 50 | uint8_t *new_buf = (uint8_t *)OPENSSL_malloc(cap + SSL3_ALIGN_PAYLOAD - 1); |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 51 | if (new_buf == NULL) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 52 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 53 | return 0; |
| 54 | } |
| 55 | |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 56 | /* Offset the buffer such that the record body is aligned. */ |
| 57 | size_t new_offset = |
| 58 | (0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1); |
| 59 | |
| 60 | if (buf->buf != NULL) { |
| 61 | OPENSSL_memcpy(new_buf + new_offset, buf->buf + buf->offset, buf->len); |
| 62 | OPENSSL_free(buf->buf); |
| 63 | } |
| 64 | |
| 65 | buf->buf = new_buf; |
| 66 | buf->offset = new_offset; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 67 | buf->cap = cap; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | static void consume_buffer(SSL3_BUFFER *buf, size_t len) { |
| 72 | if (len > buf->len) { |
| 73 | abort(); |
| 74 | } |
| 75 | buf->offset += (uint16_t)len; |
| 76 | buf->len -= (uint16_t)len; |
| 77 | buf->cap -= (uint16_t)len; |
| 78 | } |
| 79 | |
| 80 | static void clear_buffer(SSL3_BUFFER *buf) { |
| 81 | OPENSSL_free(buf->buf); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 82 | OPENSSL_memset(buf, 0, sizeof(SSL3_BUFFER)); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 83 | } |
| 84 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 85 | uint8_t *ssl_read_buffer(SSL *ssl) { |
| 86 | return ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset; |
| 87 | } |
| 88 | |
| 89 | size_t ssl_read_buffer_len(const SSL *ssl) { |
| 90 | return ssl->s3->read_buffer.len; |
| 91 | } |
| 92 | |
| 93 | static int dtls_read_buffer_next_packet(SSL *ssl) { |
| 94 | SSL3_BUFFER *buf = &ssl->s3->read_buffer; |
| 95 | |
| 96 | if (buf->len > 0) { |
| 97 | /* It is an error to call |dtls_read_buffer_extend| when the read buffer is |
| 98 | * not empty. */ |
| 99 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | /* Read a single packet from |ssl->rbio|. |buf->cap| must fit in an int. */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 104 | int ret = BIO_read(ssl->rbio, buf->buf + buf->offset, (int)buf->cap); |
| 105 | if (ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 106 | ssl->rwstate = SSL_READING; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 107 | return ret; |
| 108 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 109 | /* |BIO_read| was bound by |buf->cap|, so this cannot overflow. */ |
| 110 | buf->len = (uint16_t)ret; |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | static int tls_read_buffer_extend_to(SSL *ssl, size_t len) { |
| 115 | SSL3_BUFFER *buf = &ssl->s3->read_buffer; |
| 116 | |
| 117 | if (len > buf->cap) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 118 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | /* Read until the target length is reached. */ |
| 123 | while (buf->len < len) { |
| 124 | /* The amount of data to read is bounded by |buf->cap|, which must fit in an |
| 125 | * int. */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 126 | int ret = BIO_read(ssl->rbio, buf->buf + buf->offset + buf->len, |
| 127 | (int)(len - buf->len)); |
| 128 | if (ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 129 | ssl->rwstate = SSL_READING; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 130 | return ret; |
| 131 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 132 | /* |BIO_read| was bound by |buf->cap - buf->len|, so this cannot |
| 133 | * overflow. */ |
| 134 | buf->len += (uint16_t)ret; |
| 135 | } |
| 136 | |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | int ssl_read_buffer_extend_to(SSL *ssl, size_t len) { |
| 141 | /* |ssl_read_buffer_extend_to| implicitly discards any consumed data. */ |
| 142 | ssl_read_buffer_discard(ssl); |
| 143 | |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 144 | if (SSL_is_dtls(ssl)) { |
| 145 | OPENSSL_COMPILE_ASSERT( |
| 146 | DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff, |
| 147 | dtls_read_buffer_too_large); |
| 148 | |
| 149 | /* The |len| parameter is ignored in DTLS. */ |
| 150 | len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH; |
| 151 | } |
| 152 | |
| 153 | if (!ensure_buffer(&ssl->s3->read_buffer, ssl_record_prefix_len(ssl), len)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | if (ssl->rbio == NULL) { |
| 158 | OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 159 | return -1; |
| 160 | } |
| 161 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 162 | int ret; |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 163 | if (SSL_is_dtls(ssl)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 164 | /* |len| is ignored for a datagram transport. */ |
| 165 | ret = dtls_read_buffer_next_packet(ssl); |
| 166 | } else { |
| 167 | ret = tls_read_buffer_extend_to(ssl, len); |
| 168 | } |
| 169 | |
| 170 | if (ret <= 0) { |
| 171 | /* If the buffer was empty originally and remained empty after attempting to |
| 172 | * extend it, release the buffer until the next attempt. */ |
| 173 | ssl_read_buffer_discard(ssl); |
| 174 | } |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | void ssl_read_buffer_consume(SSL *ssl, size_t len) { |
| 179 | SSL3_BUFFER *buf = &ssl->s3->read_buffer; |
| 180 | |
| 181 | consume_buffer(buf, len); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 182 | |
| 183 | /* The TLS stack never reads beyond the current record, so there will never be |
| 184 | * unconsumed data. If read-ahead is ever reimplemented, |
| 185 | * |ssl_read_buffer_discard| will require a |memcpy| to shift the excess back |
| 186 | * to the front of the buffer, to ensure there is enough space for the next |
| 187 | * record. */ |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 188 | assert(SSL_is_dtls(ssl) || len == 0 || buf->len == 0); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void ssl_read_buffer_discard(SSL *ssl) { |
| 192 | if (ssl->s3->read_buffer.len == 0) { |
| 193 | ssl_read_buffer_clear(ssl); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void ssl_read_buffer_clear(SSL *ssl) { |
| 198 | clear_buffer(&ssl->s3->read_buffer); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | int ssl_write_buffer_is_pending(const SSL *ssl) { |
| 203 | return ssl->s3->write_buffer.len > 0; |
| 204 | } |
| 205 | |
| 206 | OPENSSL_COMPILE_ASSERT(SSL3_RT_HEADER_LENGTH * 2 + |
| 207 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 + |
| 208 | SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff, |
| 209 | maximum_tls_write_buffer_too_large); |
| 210 | |
| 211 | OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH + |
| 212 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + |
| 213 | SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff, |
| 214 | maximum_dtls_write_buffer_too_large); |
| 215 | |
| 216 | int ssl_write_buffer_init(SSL *ssl, uint8_t **out_ptr, size_t max_len) { |
| 217 | SSL3_BUFFER *buf = &ssl->s3->write_buffer; |
| 218 | |
| 219 | if (buf->buf != NULL) { |
| 220 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 221 | return 0; |
| 222 | } |
| 223 | |
David Benjamin | 3120950 | 2017-06-22 18:07:15 -0400 | [diff] [blame] | 224 | if (!ensure_buffer(buf, ssl_seal_align_prefix_len(ssl), max_len)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | *out_ptr = buf->buf + buf->offset; |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | void ssl_write_buffer_set_len(SSL *ssl, size_t len) { |
| 232 | SSL3_BUFFER *buf = &ssl->s3->write_buffer; |
| 233 | |
| 234 | if (len > buf->cap) { |
| 235 | abort(); |
| 236 | } |
| 237 | buf->len = len; |
| 238 | } |
| 239 | |
| 240 | static int tls_write_buffer_flush(SSL *ssl) { |
| 241 | SSL3_BUFFER *buf = &ssl->s3->write_buffer; |
| 242 | |
| 243 | while (buf->len > 0) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 244 | int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len); |
| 245 | if (ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 246 | ssl->rwstate = SSL_WRITING; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 247 | return ret; |
| 248 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 249 | consume_buffer(buf, (size_t)ret); |
| 250 | } |
| 251 | ssl_write_buffer_clear(ssl); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | static int dtls_write_buffer_flush(SSL *ssl) { |
| 256 | SSL3_BUFFER *buf = &ssl->s3->write_buffer; |
| 257 | if (buf->len == 0) { |
| 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 262 | if (ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 263 | ssl->rwstate = SSL_WRITING; |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 264 | /* If the write failed, drop the write buffer anyway. Datagram transports |
| 265 | * can't write half a packet, so the caller is expected to retry from the |
| 266 | * top. */ |
| 267 | ssl_write_buffer_clear(ssl); |
| 268 | return ret; |
| 269 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 270 | ssl_write_buffer_clear(ssl); |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 271 | return 1; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int ssl_write_buffer_flush(SSL *ssl) { |
| 275 | if (ssl->wbio == NULL) { |
| 276 | OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 277 | return -1; |
| 278 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 279 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 280 | if (SSL_is_dtls(ssl)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 281 | return dtls_write_buffer_flush(ssl); |
| 282 | } else { |
| 283 | return tls_write_buffer_flush(ssl); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void ssl_write_buffer_clear(SSL *ssl) { |
| 288 | clear_buffer(&ssl->s3->write_buffer); |
| 289 | } |