blob: 7f11b6f1a3aaded5a0fb4e71e75307677751e01a [file] [log] [blame]
Kenny Rootb8494592015-09-25 02:29:14 +00001/* 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 Rootb8494592015-09-25 02:29:14 +000025
Robert Sloan69939df2017-01-09 10:53:07 -080026#include "../crypto/internal.h"
Kenny Rootb8494592015-09-25 02:29:14 +000027#include "internal.h"
28
29
Robert Sloanb6d070c2017-07-24 08:40:01 -070030namespace bssl {
31
Robert Sloana12bf462017-07-17 07:08:26 -070032/* BIO uses int instead of size_t. No lengths will exceed uint16_t, so this will
33 * not overflow. */
34static_assert(0xffff <= INT_MAX, "uint16_t does not fit in int");
Kenny Rootb8494592015-09-25 02:29:14 +000035
Robert Sloana12bf462017-07-17 07:08:26 -070036static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
37 "SSL3_ALIGN_PAYLOAD must be a power of 2");
Kenny Rootb8494592015-09-25 02:29:14 +000038
Robert Sloane56da3e2017-06-26 08:26:42 -070039/* 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 Rootb8494592015-09-25 02:29:14 +000041 * boundary. It returns one on success and zero on error. */
Robert Sloane56da3e2017-06-26 08:26:42 -070042static int ensure_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) {
43 if (cap > 0xffff) {
Kenny Rootb8494592015-09-25 02:29:14 +000044 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
45 return 0;
46 }
47
Robert Sloane56da3e2017-06-26 08:26:42 -070048 if (buf->cap >= cap) {
49 return 1;
50 }
51
Kenny Rootb8494592015-09-25 02:29:14 +000052 /* Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. */
Robert Sloana12bf462017-07-17 07:08:26 -070053 uint8_t *new_buf = (uint8_t *)OPENSSL_malloc(cap + SSL3_ALIGN_PAYLOAD - 1);
Robert Sloane56da3e2017-06-26 08:26:42 -070054 if (new_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +000055 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
56 return 0;
57 }
58
Robert Sloane56da3e2017-06-26 08:26:42 -070059 /* 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 Rootb8494592015-09-25 02:29:14 +000070 buf->cap = cap;
71 return 1;
72}
73
74static 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
83static void clear_buffer(SSL3_BUFFER *buf) {
84 OPENSSL_free(buf->buf);
Robert Sloan69939df2017-01-09 10:53:07 -080085 OPENSSL_memset(buf, 0, sizeof(SSL3_BUFFER));
Kenny Rootb8494592015-09-25 02:29:14 +000086}
87
Kenny Rootb8494592015-09-25 02:29:14 +000088uint8_t *ssl_read_buffer(SSL *ssl) {
89 return ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset;
90}
91
92size_t ssl_read_buffer_len(const SSL *ssl) {
93 return ssl->s3->read_buffer.len;
94}
95
96static 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 Rootb8494592015-09-25 02:29:14 +0000107 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset, (int)buf->cap);
108 if (ret <= 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400109 ssl->rwstate = SSL_READING;
Kenny Rootb8494592015-09-25 02:29:14 +0000110 return ret;
111 }
Kenny Rootb8494592015-09-25 02:29:14 +0000112 /* |BIO_read| was bound by |buf->cap|, so this cannot overflow. */
113 buf->len = (uint16_t)ret;
114 return 1;
115}
116
117static 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 Rootb8494592015-09-25 02:29:14 +0000121 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 Rootb8494592015-09-25 02:29:14 +0000129 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset + buf->len,
130 (int)(len - buf->len));
131 if (ret <= 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400132 ssl->rwstate = SSL_READING;
Kenny Rootb8494592015-09-25 02:29:14 +0000133 return ret;
134 }
Kenny Rootb8494592015-09-25 02:29:14 +0000135 /* |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
143int 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 Sloane56da3e2017-06-26 08:26:42 -0700147 if (SSL_is_dtls(ssl)) {
Robert Sloana12bf462017-07-17 07:08:26 -0700148 static_assert(
Robert Sloane56da3e2017-06-26 08:26:42 -0700149 DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
Robert Sloana12bf462017-07-17 07:08:26 -0700150 "DTLS read buffer is too large");
Robert Sloane56da3e2017-06-26 08:26:42 -0700151
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 Rootb8494592015-09-25 02:29:14 +0000157 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 Rootb8494592015-09-25 02:29:14 +0000165 int ret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400166 if (SSL_is_dtls(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000167 /* |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
181void ssl_read_buffer_consume(SSL *ssl, size_t len) {
182 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
183
184 consume_buffer(buf, len);
David Benjamin6e899c72016-06-09 18:02:18 -0400185
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 Benjaminc895d6b2016-08-11 13:26:41 -0400191 assert(SSL_is_dtls(ssl) || len == 0 || buf->len == 0);
Kenny Rootb8494592015-09-25 02:29:14 +0000192}
193
194void ssl_read_buffer_discard(SSL *ssl) {
195 if (ssl->s3->read_buffer.len == 0) {
196 ssl_read_buffer_clear(ssl);
197 }
198}
199
200void ssl_read_buffer_clear(SSL *ssl) {
201 clear_buffer(&ssl->s3->read_buffer);
202}
203
204
205int ssl_write_buffer_is_pending(const SSL *ssl) {
206 return ssl->s3->write_buffer.len > 0;
207}
208
Robert Sloana12bf462017-07-17 07:08:26 -0700209static_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 Rootb8494592015-09-25 02:29:14 +0000214
Robert Sloana12bf462017-07-17 07:08:26 -0700215static_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 Rootb8494592015-09-25 02:29:14 +0000219
220int 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 Sloane56da3e2017-06-26 08:26:42 -0700228 if (!ensure_buffer(buf, ssl_seal_align_prefix_len(ssl), max_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000229 return 0;
230 }
231 *out_ptr = buf->buf + buf->offset;
232 return 1;
233}
234
235void 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
244static int tls_write_buffer_flush(SSL *ssl) {
245 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
246
247 while (buf->len > 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000248 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
249 if (ret <= 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400250 ssl->rwstate = SSL_WRITING;
Kenny Rootb8494592015-09-25 02:29:14 +0000251 return ret;
252 }
Kenny Rootb8494592015-09-25 02:29:14 +0000253 consume_buffer(buf, (size_t)ret);
254 }
255 ssl_write_buffer_clear(ssl);
256 return 1;
257}
258
259static 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 Langleyfad63272015-11-12 12:15:39 -0800266 if (ret <= 0) {
David Benjamin4969cc92016-04-22 15:02:23 -0400267 ssl->rwstate = SSL_WRITING;
Adam Langleyfad63272015-11-12 12:15:39 -0800268 /* 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 Rootb8494592015-09-25 02:29:14 +0000274 ssl_write_buffer_clear(ssl);
Adam Langleyfad63272015-11-12 12:15:39 -0800275 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000276}
277
278int 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 Rootb8494592015-09-25 02:29:14 +0000283
David Benjaminc895d6b2016-08-11 13:26:41 -0400284 if (SSL_is_dtls(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000285 return dtls_write_buffer_flush(ssl);
286 } else {
287 return tls_write_buffer_flush(ssl);
288 }
289}
290
291void ssl_write_buffer_clear(SSL *ssl) {
292 clear_buffer(&ssl->s3->write_buffer);
293}
Robert Sloanb6d070c2017-07-24 08:40:01 -0700294
295} // namespace bssl