blob: aefc04430cdcb02221b0165934e8b4368fedaf55 [file] [log] [blame]
David Benjaminb8d28cf2015-07-28 21:34:45 -04001/* 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 Benjamin17cf2cb2016-12-13 01:07:13 -050027#include "../crypto/internal.h"
David Benjaminb8d28cf2015-07-28 21:34:45 -040028#include "internal.h"
29
30
31OPENSSL_COMPILE_ASSERT(0xffff <= INT_MAX, uint16_fits_in_int);
32
33OPENSSL_COMPILE_ASSERT((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
34 align_to_a_power_of_two);
35
36/* setup_buffer initializes |buf| with capacity |cap|, aligned such that data
37 * written after |header_len| is aligned to a |SSL3_ALIGN_PAYLOAD|-byte
38 * boundary. It returns one on success and zero on error. */
39static int setup_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) {
40 if (buf->buf != NULL || cap > 0xffff) {
41 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
42 return 0;
43 }
44
45 /* Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. */
46 buf->buf = OPENSSL_malloc(cap + SSL3_ALIGN_PAYLOAD - 1);
47 if (buf->buf == NULL) {
48 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
49 return 0;
50 }
51
52 /* Arrange the buffer such that the record body is aligned. */
53 buf->offset = (0 - header_len - (uintptr_t)buf->buf) &
54 (SSL3_ALIGN_PAYLOAD - 1);
55 buf->len = 0;
56 buf->cap = cap;
57 return 1;
58}
59
60static void consume_buffer(SSL3_BUFFER *buf, size_t len) {
61 if (len > buf->len) {
62 abort();
63 }
64 buf->offset += (uint16_t)len;
65 buf->len -= (uint16_t)len;
66 buf->cap -= (uint16_t)len;
67}
68
69static void clear_buffer(SSL3_BUFFER *buf) {
70 OPENSSL_free(buf->buf);
David Benjamin17cf2cb2016-12-13 01:07:13 -050071 OPENSSL_memset(buf, 0, sizeof(SSL3_BUFFER));
David Benjaminb8d28cf2015-07-28 21:34:45 -040072}
73
David Benjamin03f00052015-11-18 20:41:11 -050074OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <=
75 0xffff,
David Benjaminb8d28cf2015-07-28 21:34:45 -040076 maximum_read_buffer_too_large);
77
78/* setup_read_buffer initializes the read buffer if not already initialized. It
79 * returns one on success and zero on failure. */
80static int setup_read_buffer(SSL *ssl) {
81 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
82
83 if (buf->buf != NULL) {
84 return 1;
85 }
86
87 size_t header_len = ssl_record_prefix_len(ssl);
David Benjamin2c99d282015-09-01 10:23:00 -040088 size_t cap = SSL3_RT_MAX_ENCRYPTED_LENGTH;
David Benjamince079fd2016-08-02 16:22:34 -040089 if (SSL_is_dtls(ssl)) {
David Benjamin2c99d282015-09-01 10:23:00 -040090 cap += DTLS1_RT_HEADER_LENGTH;
91 } else {
92 cap += SSL3_RT_HEADER_LENGTH;
93 }
David Benjaminb8d28cf2015-07-28 21:34:45 -040094
95 return setup_buffer(buf, header_len, cap);
96}
97
98uint8_t *ssl_read_buffer(SSL *ssl) {
99 return ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset;
100}
101
102size_t ssl_read_buffer_len(const SSL *ssl) {
103 return ssl->s3->read_buffer.len;
104}
105
106static int dtls_read_buffer_next_packet(SSL *ssl) {
107 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
108
109 if (buf->len > 0) {
110 /* It is an error to call |dtls_read_buffer_extend| when the read buffer is
111 * not empty. */
112 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
113 return -1;
114 }
115
116 /* Read a single packet from |ssl->rbio|. |buf->cap| must fit in an int. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400117 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset, (int)buf->cap);
118 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500119 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400120 return ret;
121 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400122 /* |BIO_read| was bound by |buf->cap|, so this cannot overflow. */
123 buf->len = (uint16_t)ret;
124 return 1;
125}
126
127static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
128 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
129
130 if (len > buf->cap) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400131 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
132 return -1;
133 }
134
135 /* Read until the target length is reached. */
136 while (buf->len < len) {
137 /* The amount of data to read is bounded by |buf->cap|, which must fit in an
138 * int. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400139 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset + buf->len,
140 (int)(len - buf->len));
141 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500142 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400143 return ret;
144 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400145 /* |BIO_read| was bound by |buf->cap - buf->len|, so this cannot
146 * overflow. */
147 buf->len += (uint16_t)ret;
148 }
149
150 return 1;
151}
152
153int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
154 /* |ssl_read_buffer_extend_to| implicitly discards any consumed data. */
155 ssl_read_buffer_discard(ssl);
156
157 if (!setup_read_buffer(ssl)) {
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 Benjaminb8d28cf2015-07-28 21:34:45 -0400166 int ret;
David Benjamince079fd2016-08-02 16:22:34 -0400167 if (SSL_is_dtls(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400168 /* |len| is ignored for a datagram transport. */
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) {
175 /* If the buffer was empty originally and remained empty after attempting to
176 * extend it, release the buffer until the next attempt. */
177 ssl_read_buffer_discard(ssl);
178 }
179 return ret;
180}
181
182void ssl_read_buffer_consume(SSL *ssl, size_t len) {
183 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
184
185 consume_buffer(buf, len);
David Benjamin728f3542016-06-02 15:42:01 -0400186
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 Benjamince079fd2016-08-02 16:22:34 -0400192 assert(SSL_is_dtls(ssl) || len == 0 || buf->len == 0);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400193}
194
195void ssl_read_buffer_discard(SSL *ssl) {
196 if (ssl->s3->read_buffer.len == 0) {
197 ssl_read_buffer_clear(ssl);
198 }
199}
200
201void ssl_read_buffer_clear(SSL *ssl) {
202 clear_buffer(&ssl->s3->read_buffer);
203}
204
205
206int ssl_write_buffer_is_pending(const SSL *ssl) {
207 return ssl->s3->write_buffer.len > 0;
208}
209
210OPENSSL_COMPILE_ASSERT(SSL3_RT_HEADER_LENGTH * 2 +
211 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
212 SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
213 maximum_tls_write_buffer_too_large);
214
215OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH +
216 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
217 SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
218 maximum_dtls_write_buffer_too_large);
219
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
David Benjamin0f5d7d32017-03-27 10:15:15 -0500228 if (!setup_buffer(buf, ssl_seal_align_prefix_len(ssl), max_len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400229 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) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400248 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
249 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500250 ssl->rwstate = SSL_WRITING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400251 return ret;
252 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400253 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);
David Benjamin13e81fc2015-11-02 17:16:13 -0500266 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500267 ssl->rwstate = SSL_WRITING;
David Benjamin13e81fc2015-11-02 17:16:13 -0500268 /* 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 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400274 ssl_write_buffer_clear(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -0500275 return 1;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400276}
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 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400283
David Benjamince079fd2016-08-02 16:22:34 -0400284 if (SSL_is_dtls(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400285 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}