SslStream.AuthenticateAsClient takes readtimeout value to authenticate client

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I am developing one sdk which communicates with my device. I am using SslStream class for secure communiction.
I am using System.Net.Sockets.Socket class for connection.
I have set Socket.ReceiveTimeout = 60000 (1 min) and SendTimeout = 60000 ( 1 min)
Problems
1) I dontt know whether my approch to use SslStream class for secure communication is right or not. If I am doing any mistake then please guide me.
2) when I am calling sslStream.AuthenticteAsClient function it take 1 min (i think it takes the value of receivetimeout of socket) and then gives me response. I dont wnat to do that.
What should I do to get immediate response either success or failure.
My codeclass Program
{
public static int Main(string[] args)
{
const string serverCertificateName = "192.168.1.20";
const string machineName = "192.168.1.20";

var client = new SslTcpClient();
client.Connect(machineName);

}

}

public class SslTcpClient
{
private Socket client = null;

// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None || sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch)
return true;

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

// Do not allow this client to communicate with unauthenticated servers.
return false;
}

/// <summary>
/// Connects client with server using specified IpAddress and Port number
/// </summary>
/// <returns>bool : returns true of false</returns>
public bool Connect(string ipaddress)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

client.ReceiveTimeout = 60000;
client.SendTimeout = 60000;

////Connect using a timeout (1 seconds)
var result = client.BeginConnect(ipaddress, 9734, null, null);
var success = result.AsyncWaitHandle.WaitOne(1000, true);

if (!success)
{
// NOTE, MUST CLOSE THE SOCKET
client.Close();
throw new TimeoutException("Ethernet open connection timeout");
}

result.AsyncWaitHandle.Close();
client.EndConnect(result);

CheckSslAuthentication();

return client.Connected;
}

/// <summary>
/// Authenticates server with X509Certificate
/// </summary>
private void CheckSslAuthentication()
{
SslStream sslStream = null;
try
{
var clientSocketStream = new NetworkStream(client, true);

// Create an SSL stream that will close the clients stream.
sslStream = new SslStream(clientSocketStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate));

var collection = new X509CertificateCollection
{
new X509Certificate(@"Certificatesmytempcert.pfx")
};

sslStream.AuthenticateAsClient("192.168.1.20", collection, SslProtocols.Ssl3, false);
}
catch (AuthenticationException e)
{
Console.WriteLine(e.Message);
}

// Encode a test message into a byte array.
// Signal the end of the message using the "<EOF>".
var messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>");

var serverMessage = string.Empty;
// Send hello message to the server.
if (sslStream != null)
{
sslStream.Write(messsage);
sslStream.Flush();

// Read message from the server.
serverMessage = ReadMessage(sslStream);
}

Console.WriteLine("Server says: {0}", serverMessage);
Console.ReadKey();
}

private static string ReadMessage(Stream sslStream)
{
// Read the message sent by the server.
// The end of the message is signaled using the
// "<EOF>" marker.
var buffer = new byte[2048];
var messageData = new StringBuilder();
var bytes = -1;
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length);

// Use Decoder class to convert from bytes to UTF8
// in case a character spans two buffers.
var decoder = Encoding.UTF8.GetDecoder();
var chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.Append(chars);
// Check for EOF.
if (messageData.ToString().IndexOf("<EOF>", System.StringComparison.Ordinal) != -1)
{
break;
}
} while (bytes != 0);

return messageData.ToString();
}
}
If anyone has any solution of my problems, Please help me
thanks
Regards, Hiren Bharadwa

View the full article
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
E
Replies
0
Views
124
elfenliedtopfan55
E
Back
Top