Beispiel #1
0
        private void ListenLoop()
        {
            if (scheduler.Verbose)
            {
                Console.WriteLine("Starting to listen on {0}", myEndpoint);
            }

            listener = new TcpListener(myEndpoint);
            listener.ExclusiveAddressUse = true;
            listener.Start();
            while (true)
            {
                if (scheduler.Verbose)
                {
                    Console.WriteLine("Waiting for the next incoming connection");
                }

                TcpClient client = listener.AcceptTcpClient();

                CertificateValidator myValidator = new CertificateValidator(scheduler);
                SslStream            sslStream   = new SslStream(client.GetStream(), leaveInnerStreamOpen: false, myValidator.ValidateSSLCertificate);
                sslStream.AuthenticateAsServer(scheduler.MyCert, clientCertificateRequired: true, checkCertificateRevocation: false);

                var remoteCert = sslStream.RemoteCertificate as X509Certificate2;

                if (scheduler.Verbose)
                {
                    Console.WriteLine("Received an incoming connection from remote certified as {0}",
                                      IoScheduler.CertificateToString(remoteCert));
                }

                ReceiverThread.Create(scheduler, sslStream);
                ServerSenderThread.Create(scheduler, sslStream);
            }
        }
Beispiel #2
0
        private void StartServer(PrivateIdentity myIdentity, string localHostNameOrAddress, int localPort)
        {
            onlyClient = false;

            try {
                myCert = new X509Certificate2(myIdentity.Pkcs12, "" /* empty password */, X509KeyStorageFlags.Exportable);
            }
            catch (Exception e) {
                Console.Error.WriteLine("Could not import private key. Exception:{0}", e);
                throw new Exception("Can't start server because private key not decryptable");
            }

            // The `local` parameters override the parameters in
            // `myIdentity`, unless they're empty or zero.

            if (localHostNameOrAddress == null || localHostNameOrAddress.Length == 0)
            {
                localHostNameOrAddress = myIdentity.HostNameOrAddress;
            }
            if (localPort == 0)
            {
                localPort = myIdentity.Port;
            }

            var address = LookupHostNameOrAddress(localHostNameOrAddress);

            if (address == null)
            {
                Console.Error.WriteLine("ERROR:  Could not find any addresses when resolving {0}, which I'm supposed to bind to.");
                throw new Exception("Can't resolve binding address");
            }
            var myEndpoint = new IPEndPoint(address, localPort);

            if (verbose)
            {
                Console.WriteLine("Starting I/O scheduler as server listening to {0} certified as {1}",
                                  myEndpoint, IoScheduler.CertificateToString(myCert));
            }

            sendDispatchThread = new SendDispatchThread(this);
            Thread st = new Thread(sendDispatchThread.Run);

            st.Start();

            // Start a thread to listen on my binding endpoint.

            listenerThread = new ListenerThread(this, myEndpoint);
            Thread lt = new Thread(listenerThread.Run);

            lt.Start();
        }
Beispiel #3
0
        private void ReceiveLoop()
        {
            bool success;

            if (scheduler.Verbose)
            {
                Console.WriteLine("Starting receive loop with remote identified as {0}", IoScheduler.CertificateToString(remoteCert));
            }

            while (true)
            {
                // Read the next message's size.

                UInt64 messageSize;
                success = IoEncoder.ReadUInt64(stream, out messageSize);
                if (!success)
                {
                    if (scheduler.Verbose)
                    {
                        Console.Error.WriteLine("Failed to receive message size from {0}", IoScheduler.CertificateToString(remoteCert));
                    }
                    return;
                }
                if (scheduler.Verbose)
                {
                    Console.WriteLine("Received message size {0} from {1}", messageSize, IoScheduler.CertificateToString(remoteCert));
                }

                byte[] messageBuf = new byte[messageSize];
                success = IoEncoder.ReadBytes(stream, messageBuf, 0, messageSize);
                if (!success)
                {
                    if (scheduler.Verbose)
                    {
                        Console.Error.WriteLine("Failed to receive message of size {0} from {1}",
                                                messageSize, IoScheduler.CertificateToString(remoteCert));
                    }
                    return;
                }
                if (scheduler.Verbose)
                {
                    Console.WriteLine("Received message of size {0} from {1}", messageSize, IoScheduler.CertificateToString(remoteCert));
                }

                ReceivedPacket packet = new ReceivedPacket(remoteCert, messageBuf);
                scheduler.NoteReceivedPacket(packet);
            }
        }
Beispiel #4
0
        public static ServerSenderThread Create(IoScheduler scheduler, SslStream stream)
        {
            var remoteCert           = stream.RemoteCertificate as X509Certificate2;
            var destinationPublicKey = IoScheduler.GetCertificatePublicKey(remoteCert);

            if (scheduler.Verbose)
            {
                Console.WriteLine("Creating sender thread to send to remote certified as {0}",
                                  IoScheduler.CertificateToString(remoteCert));
            }

            ServerSenderThread senderThread = new ServerSenderThread(scheduler, destinationPublicKey, stream, remoteCert);

            senderThread.Start();
            return(senderThread);
        }
Beispiel #5
0
        private void StartClient()
        {
            onlyClient = true;

            myCert = IronfleetCrypto.CreateTransientClientIdentity();

            if (verbose)
            {
                Console.WriteLine("Starting I/O scheduler as client with certificate {0}",
                                  IoScheduler.CertificateToString(myCert));
            }

            sendDispatchThread = new SendDispatchThread(this);
            Thread st = new Thread(sendDispatchThread.Run);

            st.Start();
        }
Beispiel #6
0
        protected override bool Connect()
        {
            var destinationPublicIdentity = scheduler.LookupPublicKey(destinationPublicKey);

            if (destinationPublicIdentity == null)
            {
                if (scheduler.Verbose)
                {
                    Console.Error.WriteLine("Could not connect to destination public key {0} because we don't know its address.",
                                            IoScheduler.PublicKeyToString(destinationPublicKey));
                }
                return(false);
            }

            if (scheduler.Verbose)
            {
                Console.WriteLine("Starting connection to {0}", IoScheduler.PublicIdentityToString(destinationPublicIdentity));
            }

            TcpClient client;

            try
            {
                client = new TcpClient(destinationPublicIdentity.HostNameOrAddress, destinationPublicIdentity.Port);
            }
            catch (Exception e)
            {
                scheduler.ReportException(e, "connecting to " + IoScheduler.PublicIdentityToString(destinationPublicIdentity));
                return(false);
            }

            var myCertificateCollection = new X509CertificateCollection();

            myCertificateCollection.Add(scheduler.MyCert);
            var myValidator = new CertificateValidator(scheduler, destinationPublicIdentity);

            try {
                stream = new SslStream(client.GetStream(), leaveInnerStreamOpen: false, myValidator.ValidateSSLCertificate);
                stream.AuthenticateAsClient(destinationPublicIdentity.FriendlyName, myCertificateCollection,
                                            checkCertificateRevocation: false);
            }
            catch (Exception e) {
                scheduler.ReportException(e, "authenticating connection to " + IoScheduler.PublicIdentityToString(destinationPublicIdentity));
                return(false);
            }

            remoteCert = stream.RemoteCertificate as X509Certificate2;

            if (!ByteArrayComparer.Default().Equals(IoScheduler.GetCertificatePublicKey(remoteCert), destinationPublicKey))
            {
                Console.Error.WriteLine("Connected to {0} expecting public key {1} but found public key {2}, so disconnecting.",
                                        IoScheduler.PublicIdentityToString(destinationPublicIdentity),
                                        IoScheduler.PublicKeyToString(destinationPublicKey),
                                        IoScheduler.PublicKeyToString(IoScheduler.GetCertificatePublicKey(remoteCert)));
                return(false);
            }

            if (scheduler.Verbose)
            {
                Console.WriteLine("Successfully connected to {0} and got certificate identifying it as {1}",
                                  IoScheduler.PublicIdentityToString(destinationPublicIdentity),
                                  IoScheduler.CertificateToString(remoteCert));
            }

            // Now that the connection is successful, create a thread to
            // receive packets on it.

            ReceiverThread receiverThread = ReceiverThread.Create(scheduler, stream);

            return(true);
        }
Beispiel #7
0
 protected override string EndpointDescription()
 {
     return(IoScheduler.CertificateToString(remoteCert));
 }