From 5ce3b282c2d7af7785f5b785e5736272ea4e7071 Mon Sep 17 00:00:00 2001 From: Tim Oesterreich Date: Tue, 5 Mar 2019 16:56:44 +0100 Subject: factor out template-independent code --- src/lib/tls/asio/asio_stream.h | 84 +++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/lib/tls/asio/asio_stream.h b/src/lib/tls/asio/asio_stream.h index 5003ffd04..38d8c195a 100644 --- a/src/lib/tls/asio/asio_stream.h +++ b/src/lib/tls/asio/asio_stream.h @@ -308,18 +308,11 @@ class Stream : public StreamBase if(this->m_core.hasReceivedData()) { return this->m_core.copyReceivedData(buffers); } - boost::asio::const_buffer read_buffer = - { - this->m_core.input_buffer.data(), - m_nextLayer.read_some(this->m_core.input_buffer, ec) - }; - if(ec) - { return 0; } - try { - native_handle()->received_data(static_cast(read_buffer.data()), - read_buffer.size()); + tls_decrypt_some(ec); + if(ec) + { return 0; } } catch(const std::exception& ex) { @@ -343,21 +336,12 @@ class Stream : public StreamBase std::size_t write_some(const ConstBufferSequence& buffers, boost::system::error_code& ec) { - std::size_t sent = 0; - + std::size_t sent; try { - for(auto it = boost::asio::buffer_sequence_begin(buffers); - sent < MAX_PLAINTEXT_SIZE && it != boost::asio::buffer_sequence_end(buffers); - it++) - { - const std::size_t to_send = - std::min(MAX_PLAINTEXT_SIZE - sent, boost::asio::buffer_size(*it)); - native_handle()->send(static_cast(it->data()), to_send); - sent += to_send; - } + sent = tls_encrypt_some(buffers); } - catch(const std::exception& ex) + catch(const std::exception&) { ec = Botan::TLS::convertException(); return 0; @@ -389,23 +373,10 @@ class Stream : public StreamBase boost::asio::async_completion init(handler); - std::size_t sent = 0; - + std::size_t sent; try { - // NOTE: This is not asynchronous: it encrypts the data synchronously. - // Only writing on the socket is asynchronous. - for(auto it = boost::asio::buffer_sequence_begin(buffers); - it != boost::asio::buffer_sequence_end(buffers); - it++) - { - if(sent >= MAX_PLAINTEXT_SIZE) return; - boost::asio::const_buffer buffer = *it; - const auto amount = - std::min(MAX_PLAINTEXT_SIZE - sent, buffer.size()); - native_handle()->send(static_cast(buffer.data()), amount); - sent += amount; - } + sent = tls_encrypt_some(buffers); } catch(const std::exception&) { @@ -451,6 +422,45 @@ class Stream : public StreamBase return writtenBytes; } + void tls_decrypt_some(boost::system::error_code& ec) + { + boost::asio::const_buffer read_buffer = + { + this->m_core.input_buffer.data(), + m_nextLayer.read_some(this->m_core.input_buffer, ec) + }; + + if(ec) + { return; } + + native_handle()->received_data(static_cast(read_buffer.data()), + read_buffer.size()); + } + + template + std::size_t tls_encrypt_some(const ConstBufferSequence& buffers) + { + std::size_t sent = 0; + // NOTE: This is not asynchronous: it encrypts the data synchronously. + // Only writing on the socket is asynchronous. + for(auto it = boost::asio::buffer_sequence_begin(buffers); + it != boost::asio::buffer_sequence_end(buffers); + it++) + { + if(sent >= MAX_PLAINTEXT_SIZE) + { + return 0; + } + boost::asio::const_buffer buffer = *it; + const auto amount = + std::min(MAX_PLAINTEXT_SIZE - sent, buffer.size()); + native_handle()->send(static_cast(buffer.data()), amount); + sent += amount; + } + + return sent; + } + StreamLayer m_nextLayer; }; -- cgit v1.2.3