Skip to content

Commit

Permalink
create ssl/tls doc
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Sep 9, 2023
1 parent 51222e7 commit 88dfc7c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- TCP
- [TcpClient](tcp-client.md "Netly tcp client")
- [TcpServer](tcp-server.md "Netly tcp server")
- [SSL/TLS](/ "Netly ssl and tls")
- [SSL/TLS](ssl-tls.md "Netly ssl and tls")
- [MessageFraming](/ "Netly tcp message framing")
- UDP
- [UdpClient](udp-client.md "Netly udp client")
Expand Down
77 changes: 77 additions & 0 deletions docs/ssl-tls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# <return>explication</return> SSL/TLS

## Client
For use/enable ``SSL/TLS`` on ``Netly`` with ``TcpClient`` instance use code bellow.

<return>Warning</return> When you enable ``SSL/TLS`` and your server not use ``SSL/TLS`` you connection will be closed.

- Default config
```cs
using Netly;

TcpClient client = new TcpClient(framing: true);

// Enable SSL/TLS connection.
client.UseEncryption(true);
```

- Custom validatiion
```cs
using Netly;

TcpClient client = new TcpClient(framing: true);

// Enable SSL/TLS connection.
client.UseEncryption(true, Validator);

bool Validator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{

#if false
// Default behaviour
return true;
#endif

// Custom validation
// Source: https://learn.microsoft.com/dotnet/api/system.net.security.sslstream

if (sslPolicyErrors == SslPolicyErrors.None) {
// Valid server
return true;
}

Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

// refuse connection
return false;

}
```

## Server
For use/enable ``SSL/TLS`` on ``Netly`` with ``TcpServer`` instance use code bellow.

<return>Warning</return> When you enable ``SSL/TLS`` and your client not use ``SSL/TLS`` you client connection will be closed.


```cs
// Warning: See about generate pfx on SSL/TLS page now we will see about startup this!
// Warning: Convert pfx file for bytes only using UTF8 for prevent erros
byte[] pfx = <class>.<method-get-fpx-buffer>();
string pfxPassword = <class>.<method-get-pfx-password>();

// Enable SSL/TLS
TcpServer.UseEncryption(pfx, pfxPassword, SslProtocols.Tls12); // TLS v1.2
// If password or PFX buffer is invalid you will receive error message on <TcpServer.OnError(Actiion<Exception> callback)>
```

## Create PFX (PKCS #12)
* Requirement ``OpenSSL``
- linux: Use package manager
- Ubuntu: ``sudo apt install openssl`` or ``sudo apt install libssl-dev``
- Windows:
- Download windows: https://wiki.openssl.org/index.php/Binaries
- Add OpenSSL folder on ``path`` (``Environment Variables``)
- Generate ``PFX (PKCS #12)``: https://www.ibm.com/docs/en/api-connect/10.0.x?topic=overview-generating-self-signed-certificate-using-openssl
5 changes: 5 additions & 0 deletions docs/tcp-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ using Netly;

<br>

- ##### <return>void</return> UseEncryption(<params>byte[] pfxCertificate</params>, <params>string pfxPassword</params>, <params>SslProtocols encryptionProtocol</params>)
<sub>Used for enable TLS/SSL from server side.</sub>

<br>

- ##### <return>void</return> ToData(<params>byte[] buffer</params>) <br> <return>void</return> ToData(<params>string buffer</params>)
<sub>Broadcast raw buffer to all connected clients from <params>Clients</params> array, ``buffer`` is ``string`` or ``byte[]`` (bytes).</sub>

Expand Down

0 comments on commit 88dfc7c

Please sign in to comment.