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