DidiSoft OpenSSL Library for .NET ships with a TLS client class compatible with TLS version 1.3.
Sample usage of the DidiSoft.OpenSsl.Tls.TlsClient class can be seen below:
Connecting
using DidiSoft.OpenSsl.Tls; public void TestTls() { using (TlsClient client = new TlsClient(TlsClient.TlsProtocol.Tls1_3)) { string serverHello = client.Connect("localhost", 8888); client.Write("my data"); string serverResponse = client.Read(); Console.WriteLine(serveresponse); } } |
Local Certificate
When establishing an SSL/TLS handshake we can provide a local certificate. In the example below we are using a self signed certificate.
using DidiSoft.OpenSsl; using DidiSoft.OpenSsl.Rsa; using DidiSoft.OpenSsl.Tls; using DidiSoft.OpenSsl.X509; ... public void TestTls() { OpenSslRsa rsa = new OpenSslRsa(); KeyPair kp = rsa.GenerateRsaKeyPair(DidiSoft.OpenSsl.KeyLength.Length1024); // Create self signed X509 Certificate X509Name props = new X509Name() { CN = "test" }; Certificate cert = Certificate.CreateSelfSignedCertificate(kp.Public, kp.Private, props); using (TlsClient client = new TlsClient(TlsClient.TlsProtocol.Tls1_2)) { // load the certificate before // the connection is established client.Certificate = cert; string hello = client.Connect("xtelecoms.net", 4443); Console.WriteLine(hello); client.Write(""); string response = client.Read(); Console.WriteLine(response); } } |
Asynchronous calls
For asynchronous programming the child class DidiSoft.OpenSsl.Tls.TlsClientAsync can be used with its async methods:
using DidiSoft.OpenSsl.Tls; public async void TestTlsAync() { using (TlsClientAsync client = new TlsClientAsync(TlsClient.TlsProtocol.Tls1_3)) { string serverHello = await client.ConnectAsync("localhost", 8888); await client.WriteAsync("my data"); string serverResponse = await client.ReadAsync(); Console.WriteLine(serverResponse); } } |