Question

I am writing a C# client that sends a Protobuf-net encrypted message to a server. Part of the client code reads as follows:

public static void RunClient() {
    TcpClient client = new TcpClient("168.72.70.62", 8443);
    Console.WriteLine("Client connected.");
    var stream = new MemoryStream();
    Serializer.Serialize<String>(stream, "Our client sends.");
    byte[] messsage = stream.ToArray();
    SslStream sslStream = new SslStream(client.GetStream(), false, 
            new RemoteCertificateValidationCallback(ValidateServerCertificate), 
            null);
    try {  // Server name must match name on server certificate
        sslStream.AuthenticateAsClient("168.72.70.62");
        sslStream.Write(messsage);
        sslStream.Flush();
        Console.WriteLine("Message sent.");
    } catch (AuthenticationException e) {
    ....
    }
    client.Close();
    Console.WriteLine("Client closed.");
}

This code is based on what I saw here: http://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx

The server is designed to register that a message event occurs and print out the message. It works for other types of clients. Both the server and the other clients are in Java and are based on Netty's SecureChatServer: https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/securechat/SecureChatServerHandler.java. For this client, the server registers that a message is sent but does not print out the message.

How can I get the server to print out the message?

Was it helpful?

Solution

EDIT: the actual solution

Netty expects a final \n character to be appended to the string. Using Write() does not append that final character, thus the string never flushes on the server. Pay attention: Environment.NewLine is platform dependent and may append \r\n if the message is sent from Windows clients. Netty, instead, honors a communication protocol which requires a Linux compliant newline. To comply to Netty's protocol append a literal \n manually to the string before serializing it!


Are you sure you correctly validate the server?

new RemoteCertificateValidationCallback(ValidateServerCertificate)

Try to remove this line. You will have the client to connect even on non-certified servers, but the rest should remain intact.

If the server now receives the message correctly, then it is the client that cannot validate the server correctly.

This may depend on many defects. Often it is the CA or the certificate. You can attach a custom validation handler if the server fails to be certificated. Use that and see what error you get.

Pointer: validation callback

BTW: are you sure you have to certify the server? You can have encryption without certifying the client or server. Those are all different security features of SSL and not always do you need all of them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top