I am trying to make a SSL connection between a client in Xamarin app (in iOS device), and a remote server listening to SSL connections. The thing is that my server is not authenticating correctly the client certificate. I have added to trusted certificates the server.pfx and client.pfx in my iPhone, should I try to add to trusted certificates both client and server in my ubuntu server too?
Client - side code :
try
{
// Establish a TCP connection to the server
var client = new TcpClient("...", 443);
// Create an SslStream over the TCP connection
var sslStream = new SslStream(client.GetStream(), false);
// Authenticate the client with the server using a client SSL/TLS certificate, if necessary
X509Certificate2 clientCertificate = new X509Certificate2("client.pfx", "root");
Console.WriteLine($"[X509] Certificate subject: {clientCertificate.Subject}\n");
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Add(clientCertificate);
Console.WriteLine($"[X509] Number of certs: {collection.Count}\n");
await sslStream.AuthenticateAsClientAsync("...", collection, SslProtocols.Tls12, false);
Server - side code:
while (true)
{
var client = listener.AcceptTcpClient();
var clientEndpoint = client.Client.RemoteEndPoint as IPEndPoint;
if (clientEndpoint != null)
{
var clientIp = clientEndpoint.Address.ToString();
Console.WriteLine($"[TCP]Client connected from IP address: {clientIp}");
}
else
{
Console.WriteLine($"No client connected");
}
Console.WriteLine($"[TCP] Listening on port: {port}, ipaddr: {ipAddress}. Client availability:" + $"{client.Available}. ");
// Wrap the stream with an SSL stream
SslStream sslStream = new SslStream(client.GetStream(), false);
try
{
// Authenticate the SSL connection with the private key password
sslStream.AuthenticateAsServer(serverCertificate, true, SslProtocols.Tls12, false);
// ...
}
catch() { }
}
Error that appears in server-side logs:
[TCP]Client connected from IP address: ... [TCP] Listening on port: 443, ipaddr: .... Client availability:0. Error: System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream. at System.Net.Security.SslStream.ReceiveBlobAsync[TIOAdapter](CancellationToken cancellationToken) at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken) at System.Net.Security.SslStream.AuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions) at Program.Main(String[] args) in /root/keyexchange_tfgraul/Program.cs:line 55
Client error log:
System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Mono.Security.Interface.TlsException: CertificateUnknown at Mono.AppleTls.AppleTlsContext.EvaluateTrust () [0x000bf] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/Mono.AppleTls/AppleTlsContext.cs:306 at Mono.AppleTls.AppleTlsContext.ProcessHandshake () [0x00075] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/Mono.AppleTls/AppleTlsContext.cs:213 at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) [0x000da] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:715 at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:289 at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) [0x000fc] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:223 --- End of inner exception stack trace ---
Any idea?
Any help is welcome, R.