示例#1
0
        public static void Main(string[] args)
        {
            SecurityParameters securityParameters = new SecurityParameters();

            securityParameters.CipherSuiteIDs.Add(0x000a);
            securityParameters.ServerCertificateValidationCallback = new ServerCertificateValidationCallback(CertificateValidationCallback);
            securityParameters.ClientCertificateSelectionCallback  = new ClientCertificateSelectionCallback(CertificateSelectionCallback);

            if (args.Length >= 2)
            {
                X509CertificateCollection certs = new X509CertificateCollection();
                for (int i = 0; i < args.Length - 1; i++)
                {
                    certs.Add(new X509Certificate(args[i]));
                }

                // Get plugin manager for importing the private key
                string path      = System.Reflection.Assembly.GetAssembly(typeof(AaltoTLS.HandshakeLayer.HandshakeSession)).Location;
                string directory = Path.GetDirectoryName(path);
                CipherSuitePluginManager pluginManager = new CipherSuitePluginManager(directory);

                // Import the private key into asymmetric algorithm
                byte[] privateKeyData            = File.ReadAllBytes(args[args.Length - 1]);
                CertificatePrivateKey privateKey = pluginManager.GetPrivateKey(privateKeyData);

                securityParameters.AddCertificate(certs, privateKey);
            }

            string host = "www.mikestoolbox.net";
            int    port = 443;

            string request = "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n";

            try {
                TcpClient     tcpClient = new TcpClient(host, port);
                NetworkStream ns        = tcpClient.GetStream();
                SecureSession session   = new SecureSession(ns, securityParameters);
                session.PerformClientHandshake(host);
                session.Send(Encoding.UTF8.GetBytes(request));
                byte[] received = session.Receive();
                Console.WriteLine("Received data: " + Encoding.UTF8.GetString(received));
                session.Close();
            } catch (SocketException) {
                Console.WriteLine("Unable to connect to server");
                return;
            }
        }