1 /** 2 * TLS Blocking API 3 * 4 * Copyright: 5 * (C) 2013,2015 Jack Lloyd 6 * (C) 2014-2015 Etienne Cimon 7 * 8 * License: 9 * Botan is released under the Simplified BSD License (see LICENSE.md) 10 */ 11 module botan.tls.blocking; 12 13 import botan.constants; 14 static if (BOTAN_HAS_TLS): 15 16 import std.exception : enforce; 17 import botan.tls.client; 18 import botan.tls.server; 19 import botan.rng.rng; 20 import botan.tls.channel; 21 import botan.tls.session_manager; 22 import botan.tls.version_; 23 import botan.utils.mem_ops; 24 import memutils.circularbuffer; 25 import memutils.utils; 26 import std.algorithm; 27 28 alias DataReader = ubyte[] delegate(ubyte[]); 29 30 /** 31 * Blocking TLS Channel 32 */ 33 struct TLSBlockingChannel 34 { 35 public: 36 @disable this(this); 37 @disable this(); 38 39 /// Client constructor 40 this(DataReader read_fn, 41 DataWriter write_fn, 42 OnAlert alert_cb, 43 OnHandshakeComplete hs_cb, 44 TLSSessionManager session_manager, 45 TLSCredentialsManager creds, 46 TLSPolicy policy, 47 RandomNumberGenerator rng, 48 in TLSServerInformation server_info = TLSServerInformation(), 49 in TLSProtocolVersion offer_version = TLSProtocolVersion.latestTlsVersion(), 50 Vector!string next_protocols = Vector!string()) 51 { 52 m_is_client = true; 53 m_read_fn = read_fn; 54 m_alert_cb = alert_cb; 55 m_handshake_complete = hs_cb; 56 m_readbuf = Vector!ubyte(TLS_DEFAULT_BUFFERSIZE); 57 scope(failure) m_readbuf.destroy(); 58 m_impl.client = new TLSClient(write_fn, &dataCb, &alertCb, &handshakeCb, session_manager, creds, 59 policy, rng, server_info, offer_version, next_protocols.move); 60 } 61 62 /// Server constructor 63 this(DataReader read_fn, 64 DataWriter write_fn, 65 OnAlert alert_cb, 66 OnHandshakeComplete hs_cb, 67 TLSSessionManager session_manager, 68 TLSCredentialsManager creds, 69 TLSPolicy policy, 70 RandomNumberGenerator rng, 71 NextProtocolHandler next_proto = null, 72 SNIHandler sni_handler = null, 73 bool is_datagram = false, 74 size_t io_buf_sz = 16*1024) 75 { 76 m_is_client = false; 77 m_read_fn = read_fn; 78 m_alert_cb = alert_cb; 79 m_handshake_complete = hs_cb; 80 m_readbuf = Vector!ubyte(TLS_DEFAULT_BUFFERSIZE); 81 scope(failure) m_readbuf.destroy(); 82 m_impl.server = new TLSServer(write_fn, &dataCb, &alertCb, &handshakeCb, session_manager, creds, 83 policy, rng, next_proto, sni_handler, is_datagram, io_buf_sz); 84 } 85 86 /** 87 * Blocks until the full handhsake is complete 88 */ 89 void doHandshake() 90 { 91 while (!m_closed && channel !is null && !channel.isActive()) 92 { 93 ubyte[] readref = m_readbuf.ptr[0 .. m_readbuf.length]; 94 const ubyte[] from_socket = m_read_fn(readref); 95 enforce(channel!is null, "Connection closed during handshake"); 96 channel.receivedData(cast(const(ubyte)*)from_socket.ptr, from_socket.length); 97 } 98 } 99 100 /** 101 * Number of bytes pending read in the plaintext buffer (bytes 102 * readable without blocking) 103 */ 104 size_t pending() const { return m_plaintext.length; } 105 106 /// Returns an array of pending data 107 const(ubyte)[] peek() { 108 return m_plaintext.length > 0 ? m_plaintext.peek : null; 109 } 110 111 /// Reads until the destination ubyte array is full, utilizing internal buffers if necessary 112 void read(ubyte[] dest) 113 { 114 enforce(dest.length > 0, "Empty destination array"); 115 ubyte[] destlog = dest; 116 //logDebug("remaining length: ", dest.length); 117 ubyte[] remaining = dest; 118 while (remaining.length > 0) { 119 dest = readBuf(remaining); 120 enforce(dest.length > 0, "readBuf returned 0 length (connection closed)"); 121 remaining = remaining[dest.length .. $]; 122 //logDebug("remaining length: ", remaining.length); 123 } 124 //logDebug("finished with: ", cast(string) destlog); 125 } 126 127 /** 128 * Blocking ( if !pending() ) read, will return at least 1 ubyte or 0 on connection close 129 * supports replacement of internal read buffer when called until buf.length != returned buffer length 130 */ 131 ubyte[] readBuf(ubyte[] buf) 132 { 133 m_reading = true; 134 scope(exit) m_reading = false; 135 136 if (m_plaintext.length != 0) { 137 size_t len = min(m_plaintext.length, buf.length); 138 m_plaintext.read(buf[0 .. len]); 139 return buf[0 .. len]; 140 } 141 142 // if there's nothing in the buffers, read some packets and process them 143 while (m_plaintext.empty) 144 { 145 ubyte[] slice; 146 if (m_readbuf.length > 0) { 147 slice = m_readbuf.ptr[0 .. m_readbuf.length]; 148 } 149 const ubyte[] from_socket = m_read_fn(slice); 150 if (from_socket.length == 0) 151 return null; 152 153 enforce(channel !is null, "Connection closed while reading from TLS Channel"); 154 channel.receivedData(cast(const(ubyte)*)from_socket.ptr, from_socket.length); 155 156 if (from_socket.length == slice.length && m_readbuf.length < 256*1024) { 157 size_t next_len = m_readbuf.length * 2; 158 m_readbuf.destroy(); 159 m_readbuf = Vector!ubyte(next_len); 160 // increase for next time 161 } 162 163 } 164 165 if (buf.length == 0) return null; 166 167 const size_t returned = std.algorithm.min(buf.length, m_plaintext.length); 168 if (returned == 0) { 169 //logDebug("Destroyed return object"); 170 return null; 171 } 172 m_plaintext.read(buf[0 .. returned]); 173 174 175 //logDebug("Returning data"); 176 return buf[0 .. returned]; 177 } 178 179 void write(in ubyte[] buf) { 180 m_writing = true; 181 scope(exit) m_writing = false; 182 183 enforce(channel !is null, "Connection closed when attempting to write to channel"); 184 channel.send(cast(const(ubyte)*)buf.ptr, buf.length); 185 } 186 187 inout(TLSChannel) underlyingChannel() inout { return channel; } 188 189 void close() { enforce(channel); m_closed = true; channel.close(); } 190 191 bool isClosed() const { return m_closed || m_impl.client is null; } 192 193 @property bool isBusy() const { return m_reading || m_writing; } 194 195 const(Vector!X509Certificate) peerCertChain() const { enforce(channel); return channel.peerCertChain(); } 196 197 ~this() 198 { 199 if (isBusy) return; 200 if (m_is_client) 201 m_impl.client.destroy(); 202 else m_impl.server.destroy(); 203 } 204 205 /** 206 * get handshake complete notifications 207 */ 208 @property void onHandshakeComplete(OnHandshakeComplete handshake_complete) 209 { m_handshake_complete = handshake_complete; } 210 211 /** 212 * get notification of alerts 213 */ 214 @property void onAlertNotification(OnAlert alert_cb) 215 { 216 m_alert_cb = alert_cb; 217 } 218 219 private: 220 221 bool handshakeCb(in TLSSession session) 222 { 223 //logDebug("Handshake Complete"); 224 if (m_handshake_complete) 225 return m_handshake_complete(session); 226 return true; 227 } 228 229 void dataCb(in ubyte[] data) 230 { 231 if (m_plaintext.freeSpace < data.length) { 232 //logDebug("Growing m_plaintext from: ", m_plaintext.capacity, " to ", 8192 + m_plaintext.length + m_plaintext.freeSpace); 233 m_plaintext.capacity = std.algorithm.max(8192, data.length + data.length % 8192) + m_plaintext.capacity; 234 } 235 m_plaintext.put(data); 236 } 237 238 void alertCb(in TLSAlert alert, in ubyte[] ub) 239 { 240 //logDebug("Alert: ", alert.typeString(), " :", ub); 241 if (alert.isFatal) 242 m_closed = true; 243 if (m_alert_cb) 244 m_alert_cb(alert, ub); 245 } 246 247 union TLSImpl { 248 TLSClient client; 249 TLSServer server; 250 } 251 252 @property inout(TLSChannel) channel() inout { 253 return (m_is_client ? cast(inout(TLSChannel)) m_impl.client : cast(inout(TLSChannel)) m_impl.server); 254 } 255 256 bool m_reading; 257 bool m_writing; 258 bool m_is_client; 259 bool m_closed; 260 DataReader m_read_fn; 261 TLSImpl m_impl; 262 OnAlert m_alert_cb; 263 OnHandshakeComplete m_handshake_complete; 264 265 // Buffer 266 CircularBuffer!(ubyte, 0, SecureMem) m_plaintext; 267 268 Vector!ubyte m_readbuf; 269 } 270