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