1 /* Copyright (c) 2015, Google Inc.
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.
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. */
15 #include <openssl/ssl.h>
22 #include <openssl/bio.h>
23 #include <openssl/err.h>
24 #include <openssl/mem.h>
26 #include "../crypto/internal.h"
32 // BIO uses int instead of size_t. No lengths will exceed uint16_t, so this will
34 static_assert(0xffff <= INT_MAX, "uint16_t does not fit in int");
36 static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
37 "SSL3_ALIGN_PAYLOAD must be a power of 2");
39 void SSLBuffer::Clear() {
40 free(buf_); // Allocated with malloc().
47 bool SSLBuffer::EnsureCap(size_t header_len, size_t new_cap) {
48 if (new_cap > 0xffff) {
49 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
53 if (cap_ >= new_cap) {
57 // Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment.
59 // Since this buffer gets allocated quite frequently and doesn't contain any
60 // sensitive data, we allocate with malloc rather than |OPENSSL_malloc| and
61 // avoid zeroing on free.
62 uint8_t *new_buf = (uint8_t *)malloc(new_cap + SSL3_ALIGN_PAYLOAD - 1);
63 if (new_buf == NULL) {
64 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
68 // Offset the buffer such that the record body is aligned.
70 (0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1);
73 OPENSSL_memcpy(new_buf + new_offset, buf_ + offset_, size_);
74 free(buf_); // Allocated with malloc().
83 void SSLBuffer::DidWrite(size_t new_size) {
84 if (new_size > cap() - size()) {
90 void SSLBuffer::Consume(size_t len) {
94 offset_ += (uint16_t)len;
95 size_ -= (uint16_t)len;
96 cap_ -= (uint16_t)len;
99 void SSLBuffer::DiscardConsumed() {
105 static int dtls_read_buffer_next_packet(SSL *ssl) {
106 SSLBuffer *buf = &ssl->s3->read_buffer;
109 // It is an error to call |dtls_read_buffer_extend| when the read buffer is
111 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
115 // Read a single packet from |ssl->rbio|. |buf->cap()| must fit in an int.
116 int ret = BIO_read(ssl->rbio, buf->data(), static_cast<int>(buf->cap()));
118 ssl->s3->rwstate = SSL_READING;
121 buf->DidWrite(static_cast<size_t>(ret));
125 static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
126 SSLBuffer *buf = &ssl->s3->read_buffer;
128 if (len > buf->cap()) {
129 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
133 // Read until the target length is reached.
134 while (buf->size() < len) {
135 // The amount of data to read is bounded by |buf->cap|, which must fit in an
137 int ret = BIO_read(ssl->rbio, buf->data() + buf->size(),
138 static_cast<int>(len - buf->size()));
140 ssl->s3->rwstate = SSL_READING;
143 buf->DidWrite(static_cast<size_t>(ret));
149 int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
150 // |ssl_read_buffer_extend_to| implicitly discards any consumed data.
151 ssl->s3->read_buffer.DiscardConsumed();
153 if (SSL_is_dtls(ssl)) {
155 DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
156 "DTLS read buffer is too large");
158 // The |len| parameter is ignored in DTLS.
159 len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
162 if (!ssl->s3->read_buffer.EnsureCap(ssl_record_prefix_len(ssl), len)) {
166 if (ssl->rbio == NULL) {
167 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
172 if (SSL_is_dtls(ssl)) {
173 // |len| is ignored for a datagram transport.
174 ret = dtls_read_buffer_next_packet(ssl);
176 ret = tls_read_buffer_extend_to(ssl, len);
180 // If the buffer was empty originally and remained empty after attempting to
181 // extend it, release the buffer until the next attempt.
182 ssl->s3->read_buffer.DiscardConsumed();
187 int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
188 size_t consumed, uint8_t alert) {
190 if (ret != ssl_open_record_partial) {
191 ssl->s3->read_buffer.Consume(consumed);
193 if (ret != ssl_open_record_success) {
194 // Nothing was returned to the caller, so discard anything marked consumed.
195 ssl->s3->read_buffer.DiscardConsumed();
198 case ssl_open_record_success:
201 case ssl_open_record_partial: {
202 int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
210 case ssl_open_record_discard:
214 case ssl_open_record_close_notify:
217 case ssl_open_record_error:
219 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
228 static_assert(SSL3_RT_HEADER_LENGTH * 2 +
229 SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
230 SSL3_RT_MAX_PLAIN_LENGTH <=
232 "maximum TLS write buffer is too large");
234 static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
235 SSL3_RT_MAX_PLAIN_LENGTH <=
237 "maximum DTLS write buffer is too large");
239 static int tls_write_buffer_flush(SSL *ssl) {
240 SSLBuffer *buf = &ssl->s3->write_buffer;
242 while (!buf->empty()) {
243 int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
245 ssl->s3->rwstate = SSL_WRITING;
248 buf->Consume(static_cast<size_t>(ret));
254 static int dtls_write_buffer_flush(SSL *ssl) {
255 SSLBuffer *buf = &ssl->s3->write_buffer;
260 int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
262 ssl->s3->rwstate = SSL_WRITING;
263 // If the write failed, drop the write buffer anyway. Datagram transports
264 // can't write half a packet, so the caller is expected to retry from the
273 int ssl_write_buffer_flush(SSL *ssl) {
274 if (ssl->wbio == NULL) {
275 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
279 if (SSL_is_dtls(ssl)) {
280 return dtls_write_buffer_flush(ssl);
282 return tls_write_buffer_flush(ssl);