2

The following piece of code (copy/paste ready, just replace the IP/port by something valid) throw an ArgumentNullException (nothing helpful in the stacktrace) on the line sslStream.AuthenticateAsClient:

namespace Test
{
    using System;
    using System.Net.Security;
    using System.Net.Sockets;
    using System.Security.Cryptography.X509Certificates;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var client = new TcpClient("VALID_IP_HERE", VALID_PORT_HERE);

            using (var sslStream = new SslStream(client.GetStream(), false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
            {
                // No parameter is null here
                sslStream.AuthenticateAsClient(
                    "some_string_here",
                    new X509CertificateCollection(),
                    System.Security.Authentication.SslProtocols.Tls12,
                    true);    
            }

            client.Close();
        }

        public static bool ValidateServerCertificate(object sender, X509Certificate certificate,
            X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    }
}

good ol' exception here

Why does it throw an exception? No parameter is null. The strange thing is that it only throws the exception when targeting framework .Net 4.6 or 4.6.1. It runs perfectly fine with .Net 4.5, 4.5.1 or 4.5.2.

Is this a framework bug?


EDIT:

In case you want to test, you could run the following code in another Console project:

namespace TestServer
{
    using System;
    using System.Net;
    using System.Net.Sockets;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var listener = new TcpListener(IPAddress.Any, 8883);
            listener.Start();
            var clientSocket = listener.AcceptTcpClient();

            // Nothing more here for the sake of simplicity

            Console.ReadKey();
        }
    }
}

Then use 127.0.0.1 / 8883 as IP / port on the client code side.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 1
    Works fine for me on any Framework version. Perhaps try a different dev machine? – DavidG Oct 21 '16 at 16:52
  • 1
    @DavidG After more investigation on another dev machine, it appears the exception is still thrown but you can actually resume the execution ("Break When Thrown" option). I didn't see that because for some reason my VS wouldn't resume the execution after the exception was thrown, works better after a VS reboot. That said, isn't this a weird (should I say buggy ?) behavior for the framework? – ken2k Oct 24 '16 at 13:19
  • I've tried again and it still doesn't exhibit the same issue. If I use your local console server, the client app just hangs on the `AuthenticateAsClient` method, presumable because it is waiting for a response that never arrives. Do you have any interesting/funky firewalls or anti-virus installed? – DavidG Oct 24 '16 at 15:43
  • @DavidG Nothing particular on my machine (that hosts both the client and the server for the test), just the regular Windows firewall that is disabled, no antivirus. For what it's worth, the full stack trace of the exception I get is _à System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)_. – ken2k Oct 24 '16 at 15:57
  • It's odd, maybe take a look at [the source](https://referencesource.microsoft.com/#system/net/System/Net/SecureProtocols/SslStream.cs,140) and see if you can figure it out! – DavidG Oct 24 '16 at 16:47

0 Answers0