Certificate error when trying to connect a csharp client to the purebasic server with TLS

Mac OSX specific forum
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 782
Joined: Fri Dec 04, 2015 9:26 pm

Certificate error when trying to connect a csharp client to the purebasic server with TLS

Post by skinkairewalker »

certificate error on MAC Sonoma 14.7.2 :

Code: Select all

/Users/skinkaire/RiderProjects/ConsoleApp2/bin/Debug/net8.0/ConsoleApp2
Connection TCP stabilished
Erro: Authentication failed, see inner exception.
Detailed Error Msg: The requested security protocol is not supported.

Process finished with exit code 0.
this same code works on Windows, which should work on MAC too, Does anyone have any idea what could be causing this?

[Client] Csharp Code Below >

Code: Select all

using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

class TlsClientWithReceive
{
    public static async Task Main(string[] args)
    {
        string server = "localhost";
        int port = 20252;

        try
        {
            using (TcpClient client = new TcpClient(server, port))
            {
                Console.WriteLine("Connection TCP stabilished");

                using (SslStream sslStream = new SslStream(
                    client.GetStream(),
                    false,
                    ValidateServerCertificate))
                {
                    await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
                    {
                        TargetHost = server,
                        EnabledSslProtocols = SslProtocols.Tls13
                    });

                    Console.WriteLine("Connection TCP stabilished with success");
                    Console.WriteLine($"Protocol used: {sslStream.SslProtocol}");

                    // Tarefa de recebimento
                    var receiveTask = Task.Run(async () =>
                    {
                        try
                        {
                            byte[] buffer = new byte[2048];
                            while (true)
                            {
                                int bytesRead = await sslStream.ReadAsync(buffer, 0, buffer.Length);
                                if (bytesRead > 0)
                                {
                                    string serverMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                                    Console.WriteLine($"\n[Server]: {serverMessage}");
                                }
                            }
                        }
                        catch (IOException)
                        {
                            Console.WriteLine("Server disconnected");
                        }
                    });

                    // Envio de mensagens
                    while (true)
                    {
                        Console.Write("You: ");
                        string message = Console.ReadLine();
                        if (message?.ToLower() == "exit")
                        {
                            Console.WriteLine("Exiting...");
                            break;
                        }

                        byte[] buffer = Encoding.UTF8.GetBytes(message + "\n");
                        await sslStream.WriteAsync(buffer, 0, buffer.Length);
                    }

                    client.Close();
                    await receiveTask;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Erro: {ex.Message}");
            if (ex.InnerException != null)
            {
                Console.WriteLine($"Detailed Error Msg: {ex.InnerException.Message}");
            }
        }
    }

    private static bool ValidateServerCertificate(
        object sender,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors sslPolicyErrors)
    {
        Console.WriteLine($"Cert Errors: {sslPolicyErrors}");
        return true; // Ignorar erros de validação para testes
    }
}

[Server] PB code below>

Code: Select all

Define.i Con, Timeout, Length, Format
Define Receive$, Key$, Cert$, ClientID
Define *Buffer
Global tmpKey.s = ""
Global tmpCert.s = ""

Key$ = "-----BEGIN PRIVATE KEY-----"+#LF$+
       "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAPEbSQq/uwESZduPtDd83qXXkSPf6lUNa17xhM2fOZQxGr0Fdmvw6IsC+QGX25EE1TG6TFQkHlM2rW8y6a3WEC/WzCNWaTCPYD/rguiAFG+4eQmwHjiJFVec0InjjSG9SX8xwS/gQeWdQniKROO4DmMJO8N7mdUhdHODSntXdr9zAgMBAAECgYAE+VMgbaQl+YMwbF6DZogRU8kivFPRPV2hr8nVlBtT+09Z5uryfx3NAFqytbdJ3penVviMI9KcVNxvFtXLSEc9KyjzgysorAfUpwFuECCLDbOXX0HlV6rgkqJdhyV6FybcDLvgcvulHQ64QdYRhW+jPx7vXk3h0/JRFqKQJsY7QQJBAPrDLJRPbAw+Mlq1fHBWk8Z1Qn1ivPAmz+2nPAgDya/xdAlb9GbFAMzCS3upIBpxW70uLI04OuTVhwYL194I5C0CQQD2JHtHp25SkIDpBgZGicEC7yAIE/wPC0P9X85UJqXx5dPx4HbEc8lqSKMbCzkbHyvjHonSHu00QxU1W6ZALFYf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1LqCDdtwDTnRDmVcca6HoUlvbjZLmWdLjltG1McNAATppTy/bF7vT3jXLobA1Vzs2g14POjYQhPnIbRPEnNzvAe+Se3y0YeFOwYarEyFBHKHODGIPaCnXGH8gB9fgcp2SYtLaPKvXdNL44VeYGbD4+fvUcu/zkXqTSg=="+#LF$+
        "-----END CERTIFICATE-----"

UseNetworkTLS(Key$, Cert$)
Con = CreateNetworkServer(#PB_Any, 20252, #PB_Network_TCP | #PB_Network_IPv4 | #PB_Network_TLSv1_3)
*Buffer = AllocateMemory(1000)

If Con
  Timeout = 10000
  
  Debug ("TCP Server with TLSv1_3")
  Debug ("Server Running on port : 20253")
  Repeat
    
      ClientID = EventClient()  
    
      Select NetworkServerEvent()
          
        Case #PB_NetworkEvent_Connect
          Debug ("client connected  ["+Str(ClientID)+"]")
        Case #PB_NetworkEvent_Disconnect  
          Debug ("client disconnected ["+Str(ClientID)+"]")
        Case #PB_NetworkEvent_Data
          
          Debug "Data !"
          PokeA(*Buffer, 0)
          Debug ReceiveNetworkData(ClientID, *Buffer, 1000)
          
          Debug ( "MsgReceived: "+PeekS(*Buffer, -1, #PB_UTF8) )
          SendNetworkString(ClientID, "Well received !!!")
          
        Case #PB_NetworkEvent_None
          Delay(200)
          Timeout - 1
      EndSelect
          
    Until Timeout = 0
    
    CloseNetworkServer(Con)
  Else
    Debug "Can't create the server"
EndIf