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