Unlike our high-level APIs, the TlsSocket
class doesn’t come with sample code, but it resembled System.Net.Sockets.Socket
class and its usage is very straightforward.
Its API is documented at www.rebex.net/doc/api/ and it can be used like this (enables TLS 1.2 only, configures mutual authentication):
// create TlsSocket instance and connect to a server
var socket = new TlsSocket();
socket.Connect("test.rebex.net", 990);
// configure TLS/SSL to negotiate TLS 1.2 only
socket.Parameters.Version = TlsVersion.TLS12;
// assign handler for client certificate authentication - use Certificate Store
socket.Parameters.CertificateRequestHandler = CertificateRequestHandler.StoreSearch;
// or use a certificate
//var cert = Certificate.LoadPfx("mycert.pfx", "password");
//socket.Parameters.CertificateRequestHandler = CertificateRequestHandler.CreateRequestHandler(cert);
// or implement your ICertificateRequestHandler
//socket.Parameters.CertificateRequestHandler = new MyCertificateRequestHandler();
// assign handler for server certificate authentication - use default verifier which uses Certificate Store
socket.Parameters.CertificateVerifier = CertificateVerifier.Default;
// or implement your ICertificateVerifier
//socket.Parameters.CertificateVerifier = new MyCertificateVerifier();
// negotiate a TLS/SSL session (start encryption)
socket.Negotiate();
// send data to the server over the secure channel
socket.Send(Encoding.ASCII.GetBytes("HELP"));
// receive data from the server over the secure channel
byte[] buffer = new byte[16 * 1024];
int n = socket.Receive(buffer, 0, buffer.Length);
Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, n));