blob: e6fd4e8b57fe10f000411039f24cf6fa053b9ac2 [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
David Benjamin31209502017-06-22 18:07:15 -040036/* ensure_buffer ensures |buf| has capacity at least |cap|, aligned such that
37 * data written after |header_len| is aligned to a |SSL3_ALIGN_PAYLOAD|-byte
David Benjaminb8d28cf2015-07-28 21:34:45 -040038 * boundary. It returns one on success and zero on error. */
David Benjamin31209502017-06-22 18:07:15 -040039static int ensure_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) {
40 if (cap > 0xffff) {
David Benjaminb8d28cf2015-07-28 21:34:45 -040041 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
42 return 0;
43 }
44
David Benjamin31209502017-06-22 18:07:15 -040045 if (buf->cap >= cap) {
46 return 1;
47 }
48
David Benjaminb8d28cf2015-07-28 21:34:45 -040049 /* Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. */
David Benjamine64d2c72017-07-12 16:31:08 -040050 uint8_t *new_buf = (uint8_t *)OPENSSL_malloc(cap + SSL3_ALIGN_PAYLOAD - 1);
David Benjamin31209502017-06-22 18:07:15 -040051 if (new_buf == NULL) {
David Benjaminb8d28cf2015-07-28 21:34:45 -040052 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
53 return 0;
54 }
55
David Benjamin31209502017-06-22 18:07:15 -040056 /* Offset the buffer such that the record body is aligned. */
57 size_t new_offset =
58 (0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1);
59
60 if (buf->buf != NULL) {
61 OPENSSL_memcpy(new_buf + new_offset, buf->buf + buf->offset, buf->len);
62 OPENSSL_free(buf->buf);
63 }
64
65 buf->buf = new_buf;
66 buf->offset = new_offset;
David Benjaminb8d28cf2015-07-28 21:34:45 -040067 buf->cap = cap;
68 return 1;
69}
70
71static void consume_buffer(SSL3_BUFFER *buf, size_t len) {
72 if (len > buf->len) {
73 abort();
74 }
75 buf->offset += (uint16_t)len;
76 buf->len -= (uint16_t)len;
77 buf->cap -= (uint16_t)len;
78}
79
80static void clear_buffer(SSL3_BUFFER *buf) {
81 OPENSSL_free(buf->buf);
David Benjamin17cf2cb2016-12-13 01:07:13 -050082 OPENSSL_memset(buf, 0, sizeof(SSL3_BUFFER));
David Benjaminb8d28cf2015-07-28 21:34:45 -040083}
84
David Benjaminb8d28cf2015-07-28 21:34:45 -040085uint8_t *ssl_read_buffer(SSL *ssl) {
86 return ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset;
87}
88
89size_t ssl_read_buffer_len(const SSL *ssl) {
90 return ssl->s3->read_buffer.len;
91}
92
93static int dtls_read_buffer_next_packet(SSL *ssl) {
94 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
95
96 if (buf->len > 0) {
97 /* It is an error to call |dtls_read_buffer_extend| when the read buffer is
98 * not empty. */
99 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
100 return -1;
101 }
102
103 /* Read a single packet from |ssl->rbio|. |buf->cap| must fit in an int. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400104 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset, (int)buf->cap);
105 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500106 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400107 return ret;
108 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400109 /* |BIO_read| was bound by |buf->cap|, so this cannot overflow. */
110 buf->len = (uint16_t)ret;
111 return 1;
112}
113
114static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
115 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
116
117 if (len > buf->cap) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400118 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
119 return -1;
120 }
121
122 /* Read until the target length is reached. */
123 while (buf->len < len) {
124 /* The amount of data to read is bounded by |buf->cap|, which must fit in an
125 * int. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400126 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset + buf->len,
127 (int)(len - buf->len));
128 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500129 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400130 return ret;
131 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400132 /* |BIO_read| was bound by |buf->cap - buf->len|, so this cannot
133 * overflow. */
134 buf->len += (uint16_t)ret;
135 }
136
137 return 1;
138}
139
140int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
141 /* |ssl_read_buffer_extend_to| implicitly discards any consumed data. */
142 ssl_read_buffer_discard(ssl);
143
David Benjamin31209502017-06-22 18:07:15 -0400144 if (SSL_is_dtls(ssl)) {
145 OPENSSL_COMPILE_ASSERT(
146 DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
147 dtls_read_buffer_too_large);
148
149 /* The |len| parameter is ignored in DTLS. */
150 len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
151 }
152
153 if (!ensure_buffer(&ssl->s3->read_buffer, ssl_record_prefix_len(ssl), len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400154 return -1;
155 }
156
157 if (ssl->rbio == NULL) {
158 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
159 return -1;
160 }
161
David Benjaminb8d28cf2015-07-28 21:34:45 -0400162 int ret;
David Benjamince079fd2016-08-02 16:22:34 -0400163 if (SSL_is_dtls(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400164 /* |len| is ignored for a datagram transport. */
165 ret = dtls_read_buffer_next_packet(ssl);
166 } else {
167 ret = tls_read_buffer_extend_to(ssl, len);
168 }
169
170 if (ret <= 0) {
171 /* If the buffer was empty originally and remained empty after attempting to
172 * extend it, release the buffer until the next attempt. */
173 ssl_read_buffer_discard(ssl);
174 }
175 return ret;
176}
177
178void ssl_read_buffer_consume(SSL *ssl, size_t len) {
179 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
180
181 consume_buffer(buf, len);
David Benjamin728f3542016-06-02 15:42:01 -0400182
183 /* The TLS stack never reads beyond the current record, so there will never be
184 * unconsumed data. If read-ahead is ever reimplemented,
185 * |ssl_read_buffer_discard| will require a |memcpy| to shift the excess back
186 * to the front of the buffer, to ensure there is enough space for the next
187 * record. */
David Benjamince079fd2016-08-02 16:22:34 -0400188 assert(SSL_is_dtls(ssl) || len == 0 || buf->len == 0);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400189}
190
191void ssl_read_buffer_discard(SSL *ssl) {
192 if (ssl->s3->read_buffer.len == 0) {
193 ssl_read_buffer_clear(ssl);
194 }
195}
196
197void ssl_read_buffer_clear(SSL *ssl) {
198 clear_buffer(&ssl->s3->read_buffer);
199}
200
201
202int ssl_write_buffer_is_pending(const SSL *ssl) {
203 return ssl->s3->write_buffer.len > 0;
204}
205
206OPENSSL_COMPILE_ASSERT(SSL3_RT_HEADER_LENGTH * 2 +
207 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
208 SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
209 maximum_tls_write_buffer_too_large);
210
211OPENSSL_COMPILE_ASSERT(DTLS1_RT_HEADER_LENGTH +
212 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
213 SSL3_RT_MAX_PLAIN_LENGTH <= 0xffff,
214 maximum_dtls_write_buffer_too_large);
215
216int ssl_write_buffer_init(SSL *ssl, uint8_t **out_ptr, size_t max_len) {
217 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
218
219 if (buf->buf != NULL) {
220 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
221 return 0;
222 }
223
David Benjamin31209502017-06-22 18:07:15 -0400224 if (!ensure_buffer(buf, ssl_seal_align_prefix_len(ssl), max_len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400225 return 0;
226 }
227 *out_ptr = buf->buf + buf->offset;
228 return 1;
229}
230
231void ssl_write_buffer_set_len(SSL *ssl, size_t len) {
232 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
233
234 if (len > buf->cap) {
235 abort();
236 }
237 buf->len = len;
238}
239
240static int tls_write_buffer_flush(SSL *ssl) {
241 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
242
243 while (buf->len > 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400244 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
245 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500246 ssl->rwstate = SSL_WRITING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400247 return ret;
248 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400249 consume_buffer(buf, (size_t)ret);
250 }
251 ssl_write_buffer_clear(ssl);
252 return 1;
253}
254
255static int dtls_write_buffer_flush(SSL *ssl) {
256 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
257 if (buf->len == 0) {
258 return 1;
259 }
260
261 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
David Benjamin13e81fc2015-11-02 17:16:13 -0500262 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500263 ssl->rwstate = SSL_WRITING;
David Benjamin13e81fc2015-11-02 17:16:13 -0500264 /* If the write failed, drop the write buffer anyway. Datagram transports
265 * can't write half a packet, so the caller is expected to retry from the
266 * top. */
267 ssl_write_buffer_clear(ssl);
268 return ret;
269 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400270 ssl_write_buffer_clear(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -0500271 return 1;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400272}
273
274int ssl_write_buffer_flush(SSL *ssl) {
275 if (ssl->wbio == NULL) {
276 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
277 return -1;
278 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400279
David Benjamince079fd2016-08-02 16:22:34 -0400280 if (SSL_is_dtls(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400281 return dtls_write_buffer_flush(ssl);
282 } else {
283 return tls_write_buffer_flush(ssl);
284 }
285}
286
287void ssl_write_buffer_clear(SSL *ssl) {
288 clear_buffer(&ssl->s3->write_buffer);
289}