how to use TlsSocket directly?

0 votes
asked Feb 10 by smertrios (260 points)
edited Feb 10 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.

1 Answer

0 votes
answered Feb 11 by Lukas Matyska (51,690 points)
selected Feb 11 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 help.rebex.net 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));
...