aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHannes Rantzsch <[email protected]>2020-02-05 10:49:37 +0700
committerHannes Rantzsch <[email protected]>2020-03-16 22:20:14 +0700
commit6961c6dc06f60e60d45b3b37c54eeb61ce8f0e65 (patch)
treeae112ff9924c5c68053607781aba24d883caefe3
parent23b625e761d3dd34af14ec71dc8b44794e71d604 (diff)
fail gracefully when using Stream w/o handshake
-rw-r--r--src/lib/tls/asio/asio_stream.h7
-rw-r--r--src/tests/unit_asio_stream.cpp20
2 files changed, 26 insertions, 1 deletions
diff --git a/src/lib/tls/asio/asio_stream.h b/src/lib/tls/asio/asio_stream.h
index 4d2de0967..37f031571 100644
--- a/src/lib/tls/asio/asio_stream.h
+++ b/src/lib/tls/asio/asio_stream.h
@@ -113,7 +113,12 @@ class Stream
lowest_layer_type& lowest_layer() { return m_nextLayer.lowest_layer(); }
const lowest_layer_type& lowest_layer() const { return m_nextLayer.lowest_layer(); }
- native_handle_type native_handle() { return m_native_handle.get(); }
+ native_handle_type native_handle()
+ {
+ if(m_native_handle == nullptr)
+ { throw Invalid_State("Invalid handshake state"); }
+ return m_native_handle.get();
+ }
//! @}
//! \name configuration and callback setters
diff --git a/src/tests/unit_asio_stream.cpp b/src/tests/unit_asio_stream.cpp
index 5621a9ddd..aa65b145f 100644
--- a/src/tests/unit_asio_stream.cpp
+++ b/src/tests/unit_asio_stream.cpp
@@ -558,6 +558,24 @@ class Asio_Stream_Tests final : public Test
results.push_back(result);
}
+ void test_sync_no_handshake(std::vector<Test::Result>& results)
+ {
+ net::io_context ioc;
+ TestStream remote{ioc};
+
+ auto ctx = get_context();
+ Botan::TLS::Stream<TestStream> ssl(ctx, ioc); // Note that we're not using MockChannel here
+ ssl.next_layer().connect(remote);
+ error_code ec;
+
+ net::write(ssl, net::const_buffer(TEST_DATA, TEST_DATA_SIZE), ec);
+
+ Test::Result result("sync write_some without handshake fails gracefully");
+ result.confirm("reports an error", ec.failed());
+
+ results.push_back(result);
+ }
+
void test_sync_write_some_buffer_sequence(std::vector<Test::Result>& results)
{
net::io_context ioc;
@@ -755,6 +773,8 @@ class Asio_Stream_Tests final : public Test
{
std::vector<Test::Result> results;
+ test_sync_no_handshake(results);
+
test_sync_handshake(results);
test_sync_handshake_error(results);
test_sync_handshake_throw(results);