FEZ Spider AuthenticateAsServer Error

Okay, last step for me to achieve my goal. The gadget successfully sends ssl messages as a client. So I know the SSL Seed has been updated. I created a digital certificate pem file using this wiki: GHI Electronics – Where Hardware Meets Software and placed it in the project resources. It loads fine. Last step to AuthenticateAsServer and I get the error below:

Exception System.Net.Sockets.SocketException - CLR_E_FAIL (7)

#### Message: 
#### Microsoft.SPOT.Net.Security.SslNative::SecureAccept [IP: 0000] ####
#### Microsoft.SPOT.Net.Security.SslStream::Authenticate [IP: 0045] ####
#### Microsoft.SPOT.Net.Security.SslStream::AuthenticateAsServer [IP: 000d] ####
#### Microsoft.SPOT.Net.Security.SslStream::AuthenticateAsServer [IP: 0008] ####
#### GadgeteerApp1.Program::TCPServer [IP: 0061] ####
#### SocketException ErrorCode = 5
#### SocketException ErrorCode = 5

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.Security.dll
#### SocketException ErrorCode = 5
#### SocketException ErrorCode = 5
#### SocketException ErrorCode = 5
#### SocketException ErrorCode = 5
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in System.Net.Security.dll
#### SocketException ErrorCode = 5
#### SocketException ErrorCode = 5
Exception was thrown: System.Net.Sockets.SocketException


Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, relayPort);
            server.Bind(localEndPoint);
            server.Listen(1);
            byte[] receive;
            while (true)
            {
                try
                {
                    Debug.Print("wait for Connect");
                    Socket clientSocket = server.Accept();
                    receive = new byte[100];
                    SslStream sslStream = new SslStream(server);
                    X509Certificate cert = new X509Certificate(Resources.GetBytes(Resources.BinaryResources.PemFileName), "PemPassword");
                    sslStream.AuthenticateAsServer(cert, SslVerification.NoVerification, SslProtocols.Default);

andre - the example in the other post, what is the Session variable? I’m not sure how to define it.

I figured out that I was using the server socket to create the SslStream object when I should have been using the client socket. Once I made that change the client authenticated the server just fine. Now I’m faced with the fact that I can’t figure out how to decode the bytes from the SslStream to a string. Your example shows that the SslStream might come in multiple reads. Do you have a good example of how to receive the SslStream bytes and convert to a string?

If it helps, in the full version of .net this is the code I was using to parse an SslStream. It doesn’t work in the mf .net because the decode doesn’t have some of the function available:


static string ReadMessage(SslStream sslStream)
        {
            // Read the  message sent by the server. 
            // The end of the message is signaled using the 
            // "<EOF>" marker.
            byte[] buffer = new byte[2048];
            StringBuilder messageData = new StringBuilder();
            int 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.
                Decoder decoder = Encoding.UTF8.GetDecoder();
                char[] 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>") != -1)
                {
                    break;
                }
            } while (bytes != 0);

            return messageData.ToString();
        }

Thanks andre.m! Worked perfect. I was able to combine your code with this toolbox that I found http://netmftoolbox.codeplex.com/wikipage?title=Toolbox.NETMF.Tools to convert the bytes to a string. Pretty cool toolbox. I click on the +1 in your post…I’m not sure what it does, I’m assuming it is good though. I wish I had a checkbox for you. Thanks again!