how to use TlsSocket directly?

0 votes
asked Feb 10, 2019 by smertrios (260 points)
edited Feb 10, 2019 by smertrios

Using cfnet 3.5, I want to mod the m2mqtt lib to connect to AWS.IoT using a Rebex TlsSocket.

Do you have any tutorial/sample/doc on using TlsSocket for TLS 1.2 in mutual authentication?

Thank you.

Applies to: Rebex TLS

1 Answer

0 votes
answered Feb 11, 2019 by Lukas Matyska (59,450 points)
selected Feb 11, 2019 by smertrios
Best answer

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 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("", 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)

// send data to the server over the secure channel

// 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));