blob: df814faa2fa0ed4536966bc37b5df7f32f539758 [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);
David Benjamin728f3542016-06-02 15:42:01 -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. */
191 assert(SSL_IS_DTLS(ssl) || len == 0 || buf->len == 0);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400192}
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
209OPENSSL_COMPILE_ASSERT(SSL3_RT_HEADER_LENGTH * 2 +
210 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
211 SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
212 maximum_tls_write_buffer_too_large);
213
214OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH +
215 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
216 SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
217 maximum_dtls_write_buffer_too_large);
218
219int ssl_write_buffer_init(SSL *ssl, uint8_t **out_ptr, size_t max_len) {
220 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
221
222 if (buf->buf != NULL) {
223 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
224 return 0;
225 }
226
227 size_t header_len = ssl_seal_prefix_len(ssl);
228
229 /* TODO(davidben): This matches the original behavior in keeping the malloc
230 * size consistent. Does this matter? |cap| could just be |max_len|. */
David Benjamin2c99d282015-09-01 10:23:00 -0400231 size_t cap = SSL3_RT_MAX_PLAIN_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
232 if (SSL_IS_DTLS(ssl)) {
233 cap += DTLS1_RT_HEADER_LENGTH;
234 } else {
235 cap += SSL3_RT_HEADER_LENGTH;
236 if (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) {
237 cap += SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
238 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400239 }
240
241 if (max_len > cap) {
242 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
243 return 0;
244 }
245
246 if (!setup_buffer(buf, header_len, cap)) {
247 return 0;
248 }
249 *out_ptr = buf->buf + buf->offset;
250 return 1;
251}
252
253void ssl_write_buffer_set_len(SSL *ssl, size_t len) {
254 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
255
256 if (len > buf->cap) {
257 abort();
258 }
259 buf->len = len;
260}
261
262static int tls_write_buffer_flush(SSL *ssl) {
263 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
264
265 while (buf->len > 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400266 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
267 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500268 ssl->rwstate = SSL_WRITING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400269 return ret;
270 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400271 consume_buffer(buf, (size_t)ret);
272 }
273 ssl_write_buffer_clear(ssl);
274 return 1;
275}
276
277static int dtls_write_buffer_flush(SSL *ssl) {
278 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
279 if (buf->len == 0) {
280 return 1;
281 }
282
283 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
David Benjamin13e81fc2015-11-02 17:16:13 -0500284 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500285 ssl->rwstate = SSL_WRITING;
David Benjamin13e81fc2015-11-02 17:16:13 -0500286 /* If the write failed, drop the write buffer anyway. Datagram transports
287 * can't write half a packet, so the caller is expected to retry from the
288 * top. */
289 ssl_write_buffer_clear(ssl);
290 return ret;
291 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400292 ssl_write_buffer_clear(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -0500293 return 1;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400294}
295
296int ssl_write_buffer_flush(SSL *ssl) {
297 if (ssl->wbio == NULL) {
298 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
299 return -1;
300 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400301
302 if (SSL_IS_DTLS(ssl)) {
303 return dtls_write_buffer_flush(ssl);
304 } else {
305 return tls_write_buffer_flush(ssl);
306 }
307}
308
309void ssl_write_buffer_clear(SSL *ssl) {
310 clear_buffer(&ssl->s3->write_buffer);
311}