Botan
1.11.15
|
00001 /* 00002 * TLS Blocking API 00003 * (C) 2013 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_TLS_BLOCKING_CHANNELS_H__ 00009 #define BOTAN_TLS_BLOCKING_CHANNELS_H__ 00010 00011 #include <botan/tls_client.h> 00012 #include <botan/tls_server.h> 00013 #include <deque> 00014 00015 namespace Botan { 00016 00017 //template<typename T> using secure_deque = std::vector<T, secure_allocator<T>>; 00018 00019 namespace TLS { 00020 00021 /** 00022 * Blocking TLS Client 00023 * Can be used directly, or subclass to get handshake and alert notifications 00024 */ 00025 class BOTAN_DLL Blocking_Client 00026 { 00027 public: 00028 /* 00029 * These functions are expected to block until completing entirely, or 00030 * fail by throwing an exception. 00031 */ 00032 typedef std::function<size_t (byte[], size_t)> read_fn; 00033 typedef std::function<void (const byte[], size_t)> write_fn; 00034 00035 typedef Client::next_protocol_fn next_protocol_fn; 00036 00037 Blocking_Client(read_fn reader, 00038 write_fn writer, 00039 Session_Manager& session_manager, 00040 Credentials_Manager& creds, 00041 const Policy& policy, 00042 RandomNumberGenerator& rng, 00043 const Server_Information& server_info = Server_Information(), 00044 const Protocol_Version offer_version = Protocol_Version::latest_tls_version(), 00045 next_protocol_fn npn = next_protocol_fn()); 00046 00047 /** 00048 * Completes full handshake then returns 00049 */ 00050 void do_handshake(); 00051 00052 /** 00053 * Number of bytes pending read in the plaintext buffer (bytes 00054 * readable without blocking) 00055 */ 00056 size_t pending() const { return m_plaintext.size(); } 00057 00058 /** 00059 * Blocking read, will return at least 1 byte or 0 on connection close 00060 */ 00061 size_t read(byte buf[], size_t buf_len); 00062 00063 void write(const byte buf[], size_t buf_len) { m_channel.send(buf, buf_len); } 00064 00065 const TLS::Channel& underlying_channel() const { return m_channel; } 00066 TLS::Channel& underlying_channel() { return m_channel; } 00067 00068 void close() { m_channel.close(); } 00069 00070 bool is_closed() const { return m_channel.is_closed(); } 00071 00072 std::vector<X509_Certificate> peer_cert_chain() const 00073 { return m_channel.peer_cert_chain(); } 00074 00075 virtual ~Blocking_Client() {} 00076 00077 protected: 00078 /** 00079 * Application can override to get the handshake complete notification 00080 */ 00081 virtual bool handshake_complete(const Session&) { return true; } 00082 00083 /** 00084 * Application can override to get notification of alerts 00085 */ 00086 virtual void alert_notification(const Alert&) {} 00087 00088 private: 00089 00090 bool handshake_cb(const Session&); 00091 00092 void data_cb(const byte data[], size_t data_len); 00093 00094 void alert_cb(const Alert alert, const byte data[], size_t data_len); 00095 00096 read_fn m_read; 00097 TLS::Client m_channel; 00098 secure_vector<byte> m_plaintext; 00099 }; 00100 00101 } 00102 00103 } 00104 00105 #endif