2015-07-29 02:34:45 +01:00
|
|
|
/* Copyright (c) 2015, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/mem.h>
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../crypto/internal.h"
|
2015-07-29 02:34:45 +01:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
namespace bssl {
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// BIO uses int instead of size_t. No lengths will exceed uint16_t, so this will
|
|
|
|
// not overflow.
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert(0xffff <= INT_MAX, "uint16_t does not fit in int");
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
|
|
|
|
"SSL3_ALIGN_PAYLOAD must be a power of 2");
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
void SSLBuffer::Clear() {
|
|
|
|
free(buf_); // Allocated with malloc().
|
|
|
|
buf_ = nullptr;
|
|
|
|
offset_ = 0;
|
|
|
|
size_ = 0;
|
|
|
|
cap_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SSLBuffer::EnsureCap(size_t header_len, size_t new_cap) {
|
|
|
|
if (new_cap > 0xffff) {
|
2015-07-29 02:34:45 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
2017-10-13 21:50:39 +01:00
|
|
|
return false;
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
if (cap_ >= new_cap) {
|
|
|
|
return true;
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment.
|
2017-08-18 22:24:36 +01:00
|
|
|
//
|
|
|
|
// Since this buffer gets allocated quite frequently and doesn't contain any
|
|
|
|
// sensitive data, we allocate with malloc rather than |OPENSSL_malloc| and
|
|
|
|
// avoid zeroing on free.
|
2017-10-13 21:50:39 +01:00
|
|
|
uint8_t *new_buf = (uint8_t *)malloc(new_cap + SSL3_ALIGN_PAYLOAD - 1);
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
if (new_buf == NULL) {
|
2015-07-29 02:34:45 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
|
2017-10-13 21:50:39 +01:00
|
|
|
return false;
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Offset the buffer such that the record body is aligned.
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
size_t new_offset =
|
|
|
|
(0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1);
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
if (buf_ != NULL) {
|
|
|
|
OPENSSL_memcpy(new_buf + new_offset, buf_ + offset_, size_);
|
|
|
|
free(buf_); // Allocated with malloc().
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
buf_ = new_buf;
|
|
|
|
offset_ = new_offset;
|
|
|
|
cap_ = new_cap;
|
|
|
|
return true;
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
void SSLBuffer::DidWrite(size_t new_size) {
|
|
|
|
if (new_size > cap() - size()) {
|
2015-07-29 02:34:45 +01:00
|
|
|
abort();
|
|
|
|
}
|
2017-10-13 21:50:39 +01:00
|
|
|
size_ += new_size;
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
void SSLBuffer::Consume(size_t len) {
|
|
|
|
if (len > size_) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
offset_ += (uint16_t)len;
|
|
|
|
size_ -= (uint16_t)len;
|
|
|
|
cap_ -= (uint16_t)len;
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
void SSLBuffer::DiscardConsumed() {
|
|
|
|
if (size_ == 0) {
|
|
|
|
Clear();
|
|
|
|
}
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dtls_read_buffer_next_packet(SSL *ssl) {
|
2017-10-13 21:50:39 +01:00
|
|
|
SSLBuffer *buf = &ssl->s3->read_buffer;
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
if (!buf->empty()) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// It is an error to call |dtls_read_buffer_extend| when the read buffer is
|
|
|
|
// not empty.
|
2015-07-29 02:34:45 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
// Read a single packet from |ssl->rbio|. |buf->cap()| must fit in an int.
|
|
|
|
int ret = BIO_read(ssl->rbio, buf->data(), static_cast<int>(buf->cap()));
|
2015-07-29 02:34:45 +01:00
|
|
|
if (ret <= 0) {
|
2016-03-12 03:56:19 +00:00
|
|
|
ssl->rwstate = SSL_READING;
|
2015-07-29 02:34:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2017-10-13 21:50:39 +01:00
|
|
|
buf->DidWrite(static_cast<size_t>(ret));
|
2015-07-29 02:34:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
|
2017-10-13 21:50:39 +01:00
|
|
|
SSLBuffer *buf = &ssl->s3->read_buffer;
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
if (len > buf->cap()) {
|
2015-07-29 02:34:45 +01:00
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// Read until the target length is reached.
|
2017-10-13 21:50:39 +01:00
|
|
|
while (buf->size() < len) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// The amount of data to read is bounded by |buf->cap|, which must fit in an
|
|
|
|
// int.
|
2017-10-13 21:50:39 +01:00
|
|
|
int ret = BIO_read(ssl->rbio, buf->data() + buf->size(),
|
|
|
|
static_cast<int>(len - buf->size()));
|
2015-07-29 02:34:45 +01:00
|
|
|
if (ret <= 0) {
|
2016-03-12 03:56:19 +00:00
|
|
|
ssl->rwstate = SSL_READING;
|
2015-07-29 02:34:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2017-10-13 21:50:39 +01:00
|
|
|
buf->DidWrite(static_cast<size_t>(ret));
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// |ssl_read_buffer_extend_to| implicitly discards any consumed data.
|
2017-10-13 21:50:39 +01:00
|
|
|
ssl->s3->read_buffer.DiscardConsumed();
|
2015-07-29 02:34:45 +01:00
|
|
|
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
if (SSL_is_dtls(ssl)) {
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert(
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
|
2017-07-15 00:36:07 +01:00
|
|
|
"DTLS read buffer is too large");
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
|
2017-08-29 21:33:21 +01:00
|
|
|
// The |len| parameter is ignored in DTLS.
|
Size TLS read buffers based on the size requested.
Like the write half, rather than allocating the maximum size needed and
relying on the malloc implementation to pool this sanely, allocate the
size the TLS record-layer code believes it needs.
As currently arranged, this will cause us to alternate from a small
allocation (for the record header) and then an allocation sized to the
record itself. Windows is reportedly bad at pooling large allocations,
so, *if the server sends us smaller records*, this will avoid hitting
the problem cases.
If the server sends us size 16k records, the maximum allowed by ther
protocol, we simply must buffer up to that amount and will continue to
allocate similar sizes as before (although slightly smaller; this CL
also fixes small double-counting we did on the allocation sizes).
Separately, we'll gather some metrics in Chromium to see what common
record sizes are to determine if this optimization is sufficient. This
is intended as an easy optimization we can do now, in advance of ongoing
work to fix the extra layer of buffering between Chromium and BoringSSL
with an in-place decrypt API.
Bug: chromium:524258
Change-Id: I233df29df1212154c49fee4285ccc37be12f81dc
Reviewed-on: https://boringssl-review.googlesource.com/17329
Reviewed-by: Adam Langley <agl@google.com>
2017-06-22 23:07:15 +01:00
|
|
|
len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
if (!ssl->s3->read_buffer.EnsureCap(ssl_record_prefix_len(ssl), len)) {
|
2015-07-29 02:34:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->rbio == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
2016-08-02 21:22:34 +01:00
|
|
|
if (SSL_is_dtls(ssl)) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// |len| is ignored for a datagram transport.
|
2015-07-29 02:34:45 +01:00
|
|
|
ret = dtls_read_buffer_next_packet(ssl);
|
|
|
|
} else {
|
|
|
|
ret = tls_read_buffer_extend_to(ssl, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
2017-08-29 21:33:21 +01:00
|
|
|
// If the buffer was empty originally and remained empty after attempting to
|
|
|
|
// extend it, release the buffer until the next attempt.
|
2017-10-13 21:50:39 +01:00
|
|
|
ssl->s3->read_buffer.DiscardConsumed();
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:36:20 +01:00
|
|
|
int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
|
|
|
|
size_t consumed, uint8_t alert) {
|
|
|
|
*out_retry = false;
|
|
|
|
if (ret != ssl_open_record_partial) {
|
2017-10-13 21:50:39 +01:00
|
|
|
ssl->s3->read_buffer.Consume(consumed);
|
2017-10-06 22:36:20 +01:00
|
|
|
}
|
|
|
|
if (ret != ssl_open_record_success) {
|
|
|
|
// Nothing was returned to the caller, so discard anything marked consumed.
|
2017-10-13 21:50:39 +01:00
|
|
|
ssl->s3->read_buffer.DiscardConsumed();
|
2017-10-06 22:36:20 +01:00
|
|
|
}
|
|
|
|
switch (ret) {
|
|
|
|
case ssl_open_record_success:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case ssl_open_record_partial: {
|
|
|
|
int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
|
|
|
|
if (read_ret <= 0) {
|
|
|
|
return read_ret;
|
|
|
|
}
|
|
|
|
*out_retry = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ssl_open_record_discard:
|
|
|
|
*out_retry = true;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case ssl_open_record_close_notify:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ssl_open_record_error:
|
|
|
|
if (alert != 0) {
|
|
|
|
ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert(SSL3_RT_HEADER_LENGTH * 2 +
|
|
|
|
SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
|
|
|
|
SSL3_RT_MAX_PLAIN_LENGTH <=
|
|
|
|
0xffff,
|
|
|
|
"maximum TLS write buffer is too large");
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-07-15 00:36:07 +01:00
|
|
|
static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
|
|
|
|
SSL3_RT_MAX_PLAIN_LENGTH <=
|
|
|
|
0xffff,
|
|
|
|
"maximum DTLS write buffer is too large");
|
2015-07-29 02:34:45 +01:00
|
|
|
|
|
|
|
static int tls_write_buffer_flush(SSL *ssl) {
|
2017-10-13 21:50:39 +01:00
|
|
|
SSLBuffer *buf = &ssl->s3->write_buffer;
|
2015-07-29 02:34:45 +01:00
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
while (!buf->empty()) {
|
|
|
|
int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
|
2015-07-29 02:34:45 +01:00
|
|
|
if (ret <= 0) {
|
2016-03-12 03:56:19 +00:00
|
|
|
ssl->rwstate = SSL_WRITING;
|
2015-07-29 02:34:45 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2017-10-13 21:50:39 +01:00
|
|
|
buf->Consume(static_cast<size_t>(ret));
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
2017-10-13 21:50:39 +01:00
|
|
|
buf->Clear();
|
2015-07-29 02:34:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dtls_write_buffer_flush(SSL *ssl) {
|
2017-10-13 21:50:39 +01:00
|
|
|
SSLBuffer *buf = &ssl->s3->write_buffer;
|
|
|
|
if (buf->empty()) {
|
2015-07-29 02:34:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:50:39 +01:00
|
|
|
int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
|
2015-11-02 22:16:13 +00:00
|
|
|
if (ret <= 0) {
|
2016-03-12 03:56:19 +00:00
|
|
|
ssl->rwstate = SSL_WRITING;
|
2017-08-29 21:33:21 +01:00
|
|
|
// If the write failed, drop the write buffer anyway. Datagram transports
|
|
|
|
// can't write half a packet, so the caller is expected to retry from the
|
|
|
|
// top.
|
2017-10-13 21:50:39 +01:00
|
|
|
buf->Clear();
|
2015-11-02 22:16:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-10-13 21:50:39 +01:00
|
|
|
buf->Clear();
|
2015-11-02 22:16:13 +00:00
|
|
|
return 1;
|
2015-07-29 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ssl_write_buffer_flush(SSL *ssl) {
|
|
|
|
if (ssl->wbio == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:22:34 +01:00
|
|
|
if (SSL_is_dtls(ssl)) {
|
2015-07-29 02:34:45 +01:00
|
|
|
return dtls_write_buffer_flush(ssl);
|
|
|
|
} else {
|
|
|
|
return tls_write_buffer_flush(ssl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Move libssl's internals into the bssl namespace.
This is horrible, but everything else I tried was worse. The goal with
this CL is to take the extern "C" out of ssl/internal.h and move most
symbols to namespace bssl, so we can start using C++ helpers and
destructors without worry.
Complications:
- Public API functions must be extern "C" and match their declaration in
ssl.h, which is unnamespaced. C++ really does not want you to
interleave namespaced and unnamespaced things. One can actually write
a namespaced extern "C" function, but this means, from C++'s
perspective, the function is namespaced. Trying to namespace the
public header would worked but ended up too deep a rabbithole.
- Our STACK_OF macros do not work right in namespaces.
- The typedefs for our exposed but opaque types are visible in the
header files and copied into consuming projects as forward
declarations. We ultimately want to give SSL a destructor, but
clobbering an unnamespaced ssl_st::~ssl_st seems bad manners.
- MSVC complains about ambiguous names if one typedefs SSL to bssl::SSL.
This CL opts for:
- ssl/*.cc must begin with #define BORINGSSL_INTERNAL_CXX_TYPES. This
informs the public headers to create forward declarations which are
compatible with our namespaces.
- For now, C++-defined type FOO ends up at bssl::FOO with a typedef
outside. Later I imagine we'll rename many of them.
- Internal functions get namespace bssl, so we stop worrying about
stomping the tls1_prf symbol. Exported C functions are stuck as they
are. Rather than try anything weird, bite the bullet and reorder files
which have a mix of public and private functions. I expect that over
time, the public functions will become fairly small as we move logic
to more idiomatic C++.
Files without any public C functions can just be written normally.
- To avoid MSVC troubles, some bssl types are renamed to CPlusPlusStyle
in advance of them being made idiomatic C++.
Bug: 132
Change-Id: Ic931895e117c38b14ff8d6e5a273e868796c7581
Reviewed-on: https://boringssl-review.googlesource.com/18124
Reviewed-by: David Benjamin <davidben@google.com>
2017-07-18 21:34:25 +01:00
|
|
|
} // namespace bssl
|