blob: 79f0caeb1c82adcaf07729dc95ac25951aca6d97 [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>
David Benjaminb8d28cf2015-07-28 21:34:45 -040025
David Benjamin17cf2cb2016-12-13 01:07:13 -050026#include "../crypto/internal.h"
David Benjaminb8d28cf2015-07-28 21:34:45 -040027#include "internal.h"
28
29
David Benjamin86e95b82017-07-18 16:34:25 -040030namespace bssl {
31
David Benjaminc11ea9422017-08-29 16:33:21 -040032// BIO uses int instead of size_t. No lengths will exceed uint16_t, so this will
33// not overflow.
David Benjamina3d76d02017-07-14 19:36:07 -040034static_assert(0xffff <= INT_MAX, "uint16_t does not fit in int");
David Benjaminb8d28cf2015-07-28 21:34:45 -040035
David Benjamina3d76d02017-07-14 19:36:07 -040036static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
37 "SSL3_ALIGN_PAYLOAD must be a power of 2");
David Benjaminb8d28cf2015-07-28 21:34:45 -040038
David Benjaminc11ea9422017-08-29 16:33:21 -040039// 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
41// boundary. It returns one on success and zero on error.
David Benjamin31209502017-06-22 18:07:15 -040042static int ensure_buffer(SSL3_BUFFER *buf, size_t header_len, size_t cap) {
43 if (cap > 0xffff) {
David Benjaminb8d28cf2015-07-28 21:34:45 -040044 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
45 return 0;
46 }
47
David Benjamin31209502017-06-22 18:07:15 -040048 if (buf->cap >= cap) {
49 return 1;
50 }
51
David Benjaminc11ea9422017-08-29 16:33:21 -040052 // Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment.
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070053 //
54 // Since this buffer gets allocated quite frequently and doesn't contain any
55 // sensitive data, we allocate with malloc rather than |OPENSSL_malloc| and
56 // avoid zeroing on free.
57 uint8_t *new_buf = (uint8_t *)malloc(cap + SSL3_ALIGN_PAYLOAD - 1);
David Benjamin31209502017-06-22 18:07:15 -040058 if (new_buf == NULL) {
David Benjaminb8d28cf2015-07-28 21:34:45 -040059 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
60 return 0;
61 }
62
David Benjaminc11ea9422017-08-29 16:33:21 -040063 // Offset the buffer such that the record body is aligned.
David Benjamin31209502017-06-22 18:07:15 -040064 size_t new_offset =
65 (0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1);
66
67 if (buf->buf != NULL) {
68 OPENSSL_memcpy(new_buf + new_offset, buf->buf + buf->offset, buf->len);
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070069 free(buf->buf); // Allocated with malloc().
David Benjamin31209502017-06-22 18:07:15 -040070 }
71
72 buf->buf = new_buf;
73 buf->offset = new_offset;
David Benjaminb8d28cf2015-07-28 21:34:45 -040074 buf->cap = cap;
75 return 1;
76}
77
78static void consume_buffer(SSL3_BUFFER *buf, size_t len) {
79 if (len > buf->len) {
80 abort();
81 }
82 buf->offset += (uint16_t)len;
83 buf->len -= (uint16_t)len;
84 buf->cap -= (uint16_t)len;
85}
86
87static void clear_buffer(SSL3_BUFFER *buf) {
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070088 free(buf->buf); // Allocated with malloc().
David Benjamin17cf2cb2016-12-13 01:07:13 -050089 OPENSSL_memset(buf, 0, sizeof(SSL3_BUFFER));
David Benjaminb8d28cf2015-07-28 21:34:45 -040090}
91
David Benjaminc64d1232017-10-04 18:14:28 -040092Span<uint8_t> ssl_read_buffer(SSL *ssl) {
93 return MakeSpan(ssl->s3->read_buffer.buf + ssl->s3->read_buffer.offset,
94 ssl->s3->read_buffer.len);
David Benjaminb8d28cf2015-07-28 21:34:45 -040095}
96
97static int dtls_read_buffer_next_packet(SSL *ssl) {
98 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
99
100 if (buf->len > 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400101 // It is an error to call |dtls_read_buffer_extend| when the read buffer is
102 // not empty.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400103 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
104 return -1;
105 }
106
David Benjaminc11ea9422017-08-29 16:33:21 -0400107 // Read a single packet from |ssl->rbio|. |buf->cap| must fit in an int.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400108 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset, (int)buf->cap);
109 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500110 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400111 return ret;
112 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400113 // |BIO_read| was bound by |buf->cap|, so this cannot overflow.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400114 buf->len = (uint16_t)ret;
115 return 1;
116}
117
118static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
119 SSL3_BUFFER *buf = &ssl->s3->read_buffer;
120
121 if (len > buf->cap) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400122 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
123 return -1;
124 }
125
David Benjaminc11ea9422017-08-29 16:33:21 -0400126 // Read until the target length is reached.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400127 while (buf->len < len) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400128 // The amount of data to read is bounded by |buf->cap|, which must fit in an
129 // int.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400130 int ret = BIO_read(ssl->rbio, buf->buf + buf->offset + buf->len,
131 (int)(len - buf->len));
132 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500133 ssl->rwstate = SSL_READING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400134 return ret;
135 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400136 // |BIO_read| was bound by |buf->cap - buf->len|, so this cannot
137 // overflow.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400138 buf->len += (uint16_t)ret;
139 }
140
141 return 1;
142}
143
144int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400145 // |ssl_read_buffer_extend_to| implicitly discards any consumed data.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400146 ssl_read_buffer_discard(ssl);
147
David Benjamin31209502017-06-22 18:07:15 -0400148 if (SSL_is_dtls(ssl)) {
David Benjamina3d76d02017-07-14 19:36:07 -0400149 static_assert(
David Benjamin31209502017-06-22 18:07:15 -0400150 DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
David Benjamina3d76d02017-07-14 19:36:07 -0400151 "DTLS read buffer is too large");
David Benjamin31209502017-06-22 18:07:15 -0400152
David Benjaminc11ea9422017-08-29 16:33:21 -0400153 // The |len| parameter is ignored in DTLS.
David Benjamin31209502017-06-22 18:07:15 -0400154 len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
155 }
156
157 if (!ensure_buffer(&ssl->s3->read_buffer, ssl_record_prefix_len(ssl), len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400158 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 Benjaminc11ea9422017-08-29 16:33:21 -0400168 // |len| is ignored for a datagram transport.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400169 ret = dtls_read_buffer_next_packet(ssl);
170 } else {
171 ret = tls_read_buffer_extend_to(ssl, len);
172 }
173
174 if (ret <= 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400175 // If the buffer was empty originally and remained empty after attempting to
176 // extend it, release the buffer until the next attempt.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400177 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400187 // 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
David Benjamina3d76d02017-07-14 19:36:07 -0400210static_assert(SSL3_RT_HEADER_LENGTH * 2 +
211 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
212 SSL3_RT_MAX_PLAIN_LENGTH <=
213 0xffff,
214 "maximum TLS write buffer is too large");
David Benjaminb8d28cf2015-07-28 21:34:45 -0400215
David Benjamina3d76d02017-07-14 19:36:07 -0400216static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
217 SSL3_RT_MAX_PLAIN_LENGTH <=
218 0xffff,
219 "maximum DTLS write buffer is too large");
David Benjaminb8d28cf2015-07-28 21:34:45 -0400220
221int ssl_write_buffer_init(SSL *ssl, uint8_t **out_ptr, size_t max_len) {
222 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
223
224 if (buf->buf != NULL) {
225 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
226 return 0;
227 }
228
David Benjamin31209502017-06-22 18:07:15 -0400229 if (!ensure_buffer(buf, ssl_seal_align_prefix_len(ssl), max_len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400230 return 0;
231 }
232 *out_ptr = buf->buf + buf->offset;
233 return 1;
234}
235
236void ssl_write_buffer_set_len(SSL *ssl, size_t len) {
237 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
238
239 if (len > buf->cap) {
240 abort();
241 }
242 buf->len = len;
243}
244
245static int tls_write_buffer_flush(SSL *ssl) {
246 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
247
248 while (buf->len > 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400249 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
250 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500251 ssl->rwstate = SSL_WRITING;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400252 return ret;
253 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400254 consume_buffer(buf, (size_t)ret);
255 }
256 ssl_write_buffer_clear(ssl);
257 return 1;
258}
259
260static int dtls_write_buffer_flush(SSL *ssl) {
261 SSL3_BUFFER *buf = &ssl->s3->write_buffer;
262 if (buf->len == 0) {
263 return 1;
264 }
265
266 int ret = BIO_write(ssl->wbio, buf->buf + buf->offset, buf->len);
David Benjamin13e81fc2015-11-02 17:16:13 -0500267 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500268 ssl->rwstate = SSL_WRITING;
David Benjaminc11ea9422017-08-29 16:33:21 -0400269 // If the write failed, drop the write buffer anyway. Datagram transports
270 // can't write half a packet, so the caller is expected to retry from the
271 // top.
David Benjamin13e81fc2015-11-02 17:16:13 -0500272 ssl_write_buffer_clear(ssl);
273 return ret;
274 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400275 ssl_write_buffer_clear(ssl);
David Benjamin13e81fc2015-11-02 17:16:13 -0500276 return 1;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400277}
278
279int ssl_write_buffer_flush(SSL *ssl) {
280 if (ssl->wbio == NULL) {
281 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
282 return -1;
283 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400284
David Benjamince079fd2016-08-02 16:22:34 -0400285 if (SSL_is_dtls(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400286 return dtls_write_buffer_flush(ssl);
287 } else {
288 return tls_write_buffer_flush(ssl);
289 }
290}
291
292void ssl_write_buffer_clear(SSL *ssl) {
293 clear_buffer(&ssl->s3->write_buffer);
294}
David Benjamin86e95b82017-07-18 16:34:25 -0400295
296} // namespace bssl