tango.net.device.SSLSocket

License:

BSD style: see license.txt

Author:

Jeff Davey
class SSLSocket : Socket #
SSLSocket is a sub-class of Socket. It's purpose is to provide SSL encryption at the socket level as well as easily fit into existing Tango network applications that may already be using Socket.
SSLSocket requires the OpenSSL library, and uses a dynamic binding to the library. You can find the library at http://www.openssl.org and a Win32 specific port at http://www.slproweb.com/products/Win32OpenSSL.html.

SSLSockets have two modes:

1. Client mode, useful for connecting to existing servers, but not accepting new connections. Accepting a new connection will cause the library to stall on a write on connection.

2. Server mode, useful for creating an SSL server, but not connecting to an existing server. Connection will cause the library to stall on a read on connection.

Example SSL client

1
2
3
4
5
6
7
8
9
10
auto s = new SSLSocket;
if (s.connect("www.yahoo.com", 443))
{
    char[1024] buff;

    s.write("GET / HTTP/1.0\r\n\r\n");
    auto bytesRead = s.read(buff);
    if (bytesRead != s.Eof)
        Stdout.formatln("received: {}", buff[0..bytesRead]);
}
this(bool config = true) [override] #
Create a default Client Mode SSLSocket.
void detach() [override] #
Release this SSLSocket. As per Socket.detach.
size_t write(void[] src) [override] #
Writes the passed buffer to the underlying socket stream. This will block until socket error.
As per Socket.write
size_t read(void[] dst) [override] #
Reads from the underlying socket stream. If needed, setTimeout will set the max length of time the read will take before returning.
As per Socket.read
SSLSocket shutdown() [override] #
Shuts down the underlying socket for reading and writing.
As per Socket.shutdown
void setCtx(SSLCtx ctx, bool clientMode = true) #
Used in conjuction with the above ctor with the create flag disabled. It is useful for accepting a new socket into a SSLSocket, and then re-using the Server's existing SSLCtx.

Params:

ctxSSLCtx class as provided by PKI
clientModeif true, the socket will be in Client Mode, Server otherwise.
class SSLServerSocket : ServerSocket #
SSLServerSocket is a sub-class of ServerSocket. It's purpose is to provide SSL encryption at the socket level as well as easily tie into existing Tango applications that may already be using ServerSocket.
SSLServerSocket requires the OpenSSL library, and uses a dynamic binding to the library. You can find the library at http://www.openssl.org and a Win32 specific port at http://www.slproweb.com/products/Win32OpenSSL.html.

Example SSL server

1
2
3
4
5
6
7
8
9
10
11
auto cert = new Certificate(cast(char[])File.get("public.pem"));
auto pkey = new PrivateKey(cast(char[])File.get("private.pem"));
auto ctx = new SSLCtx;
ctx.certificate(cert).privateKey(pkey);
auto server = new SSLServerSocket(443, ctx);
for(;;)
{
    auto sc = server.accept;
    sc.write("HTTP/1.1 200\r\n\r\n<b>Hello World</b>");
    sc.shutdown.close;
}
this(ushort port, SSLCtx ctx, int backlog = 32, bool reuse = false) #
this(Address addr, SSLCtx ctx, int backlog = 32, bool reuse = false) #
Constructs a new SSLServerSocket. This constructor is similar to ServerSocket, except it takes a SSLCtx as provided by PKI.

Params:

addrthe address to bind and listen on.
ctxthe provided SSLCtx
backlogthe number of connections to backlog before refusing connection
reuseif enabled, allow rebinding of existing ip/port
SSLSocket accept(SSLSocket recipient = null) #
Accepts a new conection and copies the provided server SSLCtx to a new SSLSocket.