aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2013-02-28 18:49:50 +0000
committerlloyd <[email protected]>2013-02-28 18:49:50 +0000
commit5d06d27894869db54c9dcad2e71326fca294521e (patch)
tree751f6a7e3ddf4248549d690e04e95bda42732c29
parenta89d21a2194e9175cd5d65742d44a82172834fe0 (diff)
Initial blocking client interface for simple uses and 1.10 compat
-rw-r--r--src/tls/tls_blocking.cpp85
-rw-r--r--src/tls/tls_blocking.h89
2 files changed, 174 insertions, 0 deletions
diff --git a/src/tls/tls_blocking.cpp b/src/tls/tls_blocking.cpp
new file mode 100644
index 000000000..e0f31b1ca
--- /dev/null
+++ b/src/tls/tls_blocking.cpp
@@ -0,0 +1,85 @@
+/*
+* TLS Blocking API
+* (C) 2013 Jack Lloyd
+*
+* Released under the terms of the Botan license
+*/
+
+#include <botan/tls_blocking.h>
+
+namespace Botan {
+
+namespace TLS {
+
+using namespace std::placeholders;
+
+Blocking_Client::Blocking_Client(std::function<size_t (byte[], size_t)> read_fn,
+ std::function<void (const byte[], size_t)> write_fn,
+ Session_Manager& session_manager,
+ Credentials_Manager& creds,
+ const Policy& policy,
+ RandomNumberGenerator& rng,
+ const Server_Information& server_info,
+ const Protocol_Version offer_version,
+ std::function<std::string (std::vector<std::string>)> next_protocol) :
+ m_read_fn(read_fn),
+ m_channel(write_fn,
+ std::bind(&Blocking_Client::process_data, this, _1, _2, _3),
+ std::bind(&Blocking_Client::handshake_complete, this, _1, _2, _3),
+ session_manager,
+ creds,
+ policy,
+ rng,
+ server_info,
+ offer_version,
+ next_protocol)
+ {
+ }
+
+#if 0
+Blocking_Client::Blocking_Client(std::function<size_t (byte[], size_t)> read_fn,
+ std::function<void (const byte[], size_t)> write_fn,
+ const TLS_Policy& policy,
+ RandomNumberGenerator& rng) :
+ m_read_fn(read_fn)
+#endif
+
+bool Blocking_Client::handshake_complete_cb(const Session& session)
+ {
+ return this->handshake_complete(session);
+ }
+
+void Blocking_Client::process_data(const byte data[], size_t data_len,
+ const Alert& alert)
+ {
+ m_plaintext.insert(m_plaintext.end(), data, data + data_len);
+
+ if(alert.is_valid())
+ alert_notification(alert);
+ }
+
+size_t Blocking_Client::read(byte buf[], size_t buf_len)
+ {
+ secure_vector<byte> readbuf(4096);
+
+ while(m_plaintext.empty())
+ {
+ const size_t readbuf_size = 4096;
+ byte readbuf[readbuf_size] = { 0 };
+
+ const size_t from_socket = m_read_fn(&readbuf[0], readbuf_size);
+ m_channel.received_data(&readbuf[0], from_socket);
+ }
+
+ const size_t returned = std::min(buf_len, m_plaintext.size());
+
+ for(size_t i = 0; i != returned; ++i)
+ buf[i] = m_plaintext[i];
+ m_plaintext.erase(m_plaintext.begin(), m_plaintext.begin() + returned);
+
+ return returned;
+ }
+
+}
+
+}
diff --git a/src/tls/tls_blocking.h b/src/tls/tls_blocking.h
new file mode 100644
index 000000000..d024cf894
--- /dev/null
+++ b/src/tls/tls_blocking.h
@@ -0,0 +1,89 @@
+/*
+* TLS Blocking API
+* (C) 2013 Jack Lloyd
+*
+* Released under the terms of the Botan license
+*/
+
+#ifndef BOTAN_TLS_BLOCKING_CHANNELS_H__
+#define BOTAN_TLS_BLOCKING_CHANNELS_H__
+
+#include <botan/tls_client.h>
+#include <botan/tls_server.h>
+#include <deque>
+
+namespace Botan {
+
+template<typename T> using secure_deque = std::vector<T, secure_allocator<T>>;
+
+namespace TLS {
+
+/**
+* Blocking TLS Client
+*/
+class BOTAN_DLL Blocking_Client
+ {
+ public:
+
+ Blocking_Client(std::function<size_t (byte[], size_t)> read_fn,
+ std::function<void (const byte[], size_t)> write_fn,
+ Session_Manager& session_manager,
+ Credentials_Manager& creds,
+ const Policy& policy,
+ RandomNumberGenerator& rng,
+ const Server_Information& server_info = Server_Information(),
+ const Protocol_Version offer_version = Protocol_Version::latest_tls_version(),
+ std::function<std::string (std::vector<std::string>)> next_protocol =
+ std::function<std::string (std::vector<std::string>)>());
+
+ // Constructor like the 1.10 Client API
+ Blocking_Client(std::function<size_t (byte[], size_t)> read_fn,
+ std::function<void (const byte[], size_t)> write_fn,
+ const Policy& policy,
+ RandomNumberGenerator& rng);
+
+ size_t currently_readable() const { return m_plaintext.size(); }
+
+ size_t read(byte buf[], size_t buf_len); // blocking read
+
+ void write(const byte buf[], size_t buf_len) { m_channel.send(buf, buf_len); }
+
+ const TLS::Channel& underlying_channel() const { return m_channel; }
+ TLS::Channel& underlying_channel() { return m_channel; }
+
+ void close() { m_channel.close(); }
+
+ bool is_closed() const { return m_channel.is_closed(); }
+
+ std::vector<X509_Certificate> peer_cert_chain() const
+ { return m_channel.peer_cert_chain(); }
+
+ virtual ~Blocking_Client() {}
+
+ protected:
+ /**
+ * Can override to get the handshake complete notification
+ */
+ virtual bool handshake_complete(const Session&) { return true; }
+
+ /**
+ * Can override to get notification of alerts
+ */
+ virtual void alert_notification(const Alert&) {}
+ private:
+
+ bool handshake_complete_cb(const Session&);
+
+ void process_data(const byte data[], size_t data_len,
+ const Alert& alert);
+
+ std::function<size_t (byte[], size_t)> m_read_fn;
+ TLS::Client m_channel;
+ secure_deque<byte> m_plaintext;
+ };
+
+}
+
+}
+
+#endif