blob: efa2208d7dce0647b209c28d985058ea3270946c [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
27#include "internal.h"
28
29
30OPENSSL_COMPILE_ASSERT(0xffff <= INT_MAX, uint16_fits_in_int);
31
32OPENSSL_COMPILE_ASSERT((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
33 align_to_a_power_of_two);
34
35/* setup_buffer initializes |buf| with capacity |cap|, aligned such that data
36 * written after |header_len| is aligned to a |SSL3_ALIGN_PAYLOAD|-byte
37 * boundary. It returns one on success and zero on error. */
38static int setup_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) {
39 if (buf->buf != NULL || cap > 0xffff) {
40 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
41 return 0;
42 }
43
44 /* Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. */
45 buf->buf = OPENSSL_malloc(cap + SSL3_ALIGN_PAYLOAD - 1);
46 if (buf->buf == NULL) {
47 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
48 return 0;
49 }
50
51 /* Arrange the buffer such that the record body is aligned. */
52 buf->offset = (0 - header_len - (uintptr_t)buf->buf) &
53 (SSL3_ALIGN_PAYLOAD - 1);
54 buf->len = 0;
55 buf->cap = cap;
56 return 1;
57}
58
59static void consume_buffer(SSL3_BUFFER *buf, size_t len) {
60 if (len > buf->len) {
61 abort();
62 }
63 buf->offset += (uint16_t)len;
64 buf->len -= (uint16_t)len;
65 buf->cap -= (uint16_t)len;
66}
67
68static void clear_buffer(SSL3_BUFFER *buf) {
69 OPENSSL_free(buf->buf);
70 memset(buf, 0, sizeof(SSL3_BUFFER));
71}
72
David Benjamin03f00052015-11-18 20:41:11 -050073OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <=
74 0xffff,
David Benjaminb8d28cf2015-07-28 21:34:45 -040075 maximum_read_buffer_too_large);
76
77/* setup_read_buffer initializes the read buffer if not already initialized. It
78 * returns one on success and zero on failure. */
79static int setup_read_buffer(SSL *ssl) {
80 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
81
82 if (buf->buf != NULL) {
83 return 1;
84 }
85
86 size_t header_len = ssl_record_prefix_len(ssl);
David Benjamin2c99d282015-09-01 10:23:00 -040087 size_t cap = SSL3_RT_MAX_ENCRYPTED_LENGTH;
88 if (SSL_IS_DTLS(ssl)) {
89 cap += DTLS1_RT_HEADER_LENGTH;
90 } else {
91 cap += SSL3_RT_HEADER_LENGTH;
92 }
David Benjaminb8d28cf2015-07-28 21:34:45 -040093
94 return setup_buffer(buf, header_len, cap);
95}
96
97uint8_t *ssl_read_buffer(SSL *ssl) {
98 return ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset;
99}
100
101size_t ssl_read_buffer_len(const SSL *ssl) {
102 return ssl->s3->read_buffer.len;
103}
104
105static int dtls_read_buffer_next_packet(SSL *ssl) {
106 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
107
108 if (buf->len > 0) {
109 /* It is an error to call |dtls_read_buffer_extend| when the read buffer is
110 * not empty. */
111 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
112 return -1;
113 }
114
115 /* Read a single packet from |ssl->rbio|. |buf->cap| must fit in an int. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400116 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset, (int)buf->cap);
117 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500118 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400119 return ret;
120 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400121 /* |BIO_read| was bound by |buf->cap|, so this cannot overflow. */
122 buf->len = (uint16_t)ret;
123 return 1;
124}
125
126static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
127 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
128
129 if (len > buf->cap) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400130 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
131 return -1;
132 }
133
134 /* Read until the target length is reached. */
135 while (buf->len < len) {
136 /* The amount of data to read is bounded by |buf->cap|, which must fit in an
137 * int. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400138 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset + buf->len,
139 (int)(len - buf->len));
140 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500141 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400142 return ret;
143 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400144 /* |BIO_read| was bound by |buf->cap - buf->len|, so this cannot
145 * overflow. */
146 buf->len += (uint16_t)ret;
147 }
148
149 return 1;
150}
151
152int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
153 /* |ssl_read_buffer_extend_to| implicitly discards any consumed data. */
154 ssl_read_buffer_discard(ssl);
155
156 if (!setup_read_buffer(ssl)) {
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
David Benjaminb8d28cf2015-07-28 21:34:45 -0400165 int ret;
166 if (SSL_IS_DTLS(ssl)) {
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
181void ssl_read_buffer_consume(SSL *ssl, size_t len) {
182 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
183
184 consume_buffer(buf, len);
185 if (!SSL_IS_DTLS(ssl)) {
186 /* The TLS stack never reads beyond the current record, so there will never
187 * be unconsumed data. If read-ahead is ever reimplemented,
188 * |ssl_read_buffer_discard| will require a |memcpy| to shift the excess
189 * back to the front of the buffer, to ensure there is enough space for the
190 * next record. */
191 assert(buf->len == 0);
192 }
193}
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
228 size_t header_len = ssl_seal_prefix_len(ssl);
229
230 /* TODO(davidben): This matches the original behavior in keeping the malloc
231 * size consistent. Does this matter? |cap| could just be |max_len|. */
David Benjamin2c99d282015-09-01 10:23:00 -0400232 size_t cap = SSL3_RT_MAX_PLAIN_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
233 if (SSL_IS_DTLS(ssl)) {
234 cap += DTLS1_RT_HEADER_LENGTH;
235 } else {
236 cap += SSL3_RT_HEADER_LENGTH;
237 if (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) {
238 cap += SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
239 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400240 }
241
242 if (max_len > cap) {
243 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
244 return 0;
245 }
246
247 if (!setup_buffer(buf, header_len, cap)) {
248 return 0;
249 }
250 *out_ptr = buf->buf + buf->offset;
251 return 1;
252}
253
254void ssl_write_buffer_set_len(SSL *ssl, size_t len) {
255 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
256
257 if (len > buf->cap) {
258 abort();
259 }
260 buf->len = len;
261}
262
263static int tls_write_buffer_flush(SSL *ssl) {
264 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
265
266 while (buf->len > 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400267 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
268 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500269 ssl->rwstate = SSL_WRITING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400270 return ret;
271 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400272 consume_buffer(buf, (size_t)ret);
273 }
274 ssl_write_buffer_clear(ssl);
275 return 1;
276}
277
278static int dtls_write_buffer_flush(SSL *ssl) {
279 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
280 if (buf->len == 0) {
281 return 1;
282 }
283
284 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
David Benjamin13e81fc2015-11-02 17:16:13 -0500285 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500286 ssl->rwstate = SSL_WRITING;
David Benjamin13e81fc2015-11-02 17:16:13 -0500287 /* If the write failed, drop the write buffer anyway. Datagram transports
288 * can't write half a packet, so the caller is expected to retry from the
289 * top. */
290 ssl_write_buffer_clear(ssl);
291 return ret;
292 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400293 ssl_write_buffer_clear(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -0500294 return 1;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400295}
296
297int ssl_write_buffer_flush(SSL *ssl) {
298 if (ssl->wbio == NULL) {
299 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
300 return -1;
301 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400302
303 if (SSL_IS_DTLS(ssl)) {
304 return dtls_write_buffer_flush(ssl);
305 } else {
306 return tls_write_buffer_flush(ssl);
307 }
308}
309
310void ssl_write_buffer_clear(SSL *ssl) {
311 clear_buffer(&ssl->s3->write_buffer);
312}