0

I am new to C#. I am trying to establish a TCP communication using TLS over LAN but have no idea how to do it. I have done the same in java using following code :

            SSLSocketFactory sslSocketFactory = Util.BuildSslSocketFactoryForLAN(getApplicationContext());
            Socket tcpSocket = sslSocketFactory.createSocket("IP address", "port");
           
            is = tcpSocket.getInputStream();
            OutputStream os = tcpSocket.getOutputStream();

            devicePairingReq = Util.hexStringToByteArray("9F" + data);

            os.write(devicePairingReq);
            os.flush();

            while (true) {
                tcpSocket.setSoTimeout(1000 * 90);

                resp = new byte[15];
                test = is.read(resp);
                tcpSocket.close();

     public static SSLSocketFactory BuildSslSocketFactoryForLAN(Context context) {
    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream is = context.getResources().getAssets().open("name.pem");
        InputStream caInput = new BufferedInputStream(is);
        Certificate ca;
        try {
            ca = cf.generateCertificate(caInput);
        } finally {
            caInput.close();
        }
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);

        sslContext.init(null, tmf.getTrustManagers(), null);
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        return sslSocketFactory;
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    return null;
}

I am totally new to C# so have very little idea about it. I am trying to sent a message to tcp server from mobile client over LAN.

iSukhi
  • 25
  • 1
  • 10
  • I would Google for C# TLS socket examples and pick the one that best suits your needs. Here are two (of many!) examples: [Simple example of TCP for C# TLS SSL](https://www.programmersought.com/article/4670276171/), and [Configure certificate authentication in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/certauth) – paulsm4 Mar 28 '21 at 06:44
  • @paulsm4 sir, Thank you but I googled a lot before putting up a question here. But the solutions are not as per my projects requirement. I just need to build the client which accepts the certificate and after that I should be able to establish the connection. Thats why I put my sample java code which is successfully working. Thank you for your examples but it is not solving my purpose. – iSukhi Mar 28 '21 at 12:34
  • Does this answer your question? [SSL Client Authentication with certificate using c#](https://stackoverflow.com/questions/64586721/ssl-client-authentication-with-certificate-using-c-sharp) – paulsm4 Mar 28 '21 at 18:24
  • @paulsm4 sir, Not The link you share tells about the SSL client authentication for HTTP call where I am trying to do the TCP call over local WiFi. – iSukhi Mar 29 '21 at 15:34

0 Answers0