Example #1
0
        /// <summary>
        /// Metoda sluzaca do logowanie klienta
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public bool LogClient(string login, byte[] password, int n)
        {
            foreach (Client client in database)
            {
                if (login == client.login)
                {
                    string pass = null;
                    foreach (byte b in password)
                    {
                        pass += (b.ToString() + " ");
                    }
                    --n;

                    byte[] hash = CryptoModule.HashNTimes(client.passwordHash, n);
                    string h    = null;
                    foreach (byte b in hash)
                    {
                        h += (b.ToString() + " ");
                    }

                    if (h.Equals(pass))
                    {
                        Console.WriteLine("LOG: Logged {0}", login);
                        client.isOnline = true;
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #2
0
 /// <summary>
 /// Konstruktor
 /// </summary>
 /// <param name="login"></param>
 /// <param name="password"></param>
 /// <param name="pesel"></param>
 public Client(string login, byte[] password, byte[] pesel)
 {
     this.login   = login;
     passwordHash = password;
     peselHash    = pesel;
     n            = 1;
     generator    = new Random();
     isOnline     = false;
     isRunning    = false;
     isConnected  = false;
     certificate  = CryptoModule.GenerateCeriticate(login);
 }
Example #3
0
        /// <summary>
        /// Metoda umozliwiajaca pobranie certyfikatu
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public X509Certificate2 GetCertificate(string login)
        {
            X509Certificate2 certificate = CryptoModule.GenerateCeriticate(login);

            foreach (Client c in database)
            {
                if (c.login == login)
                {
                    c.certificate = certificate;
                    return(c.certificate);
                }
            }
            X509Certificate2 wrong = null;

            return(wrong);
        }
Example #4
0
        /// <summary>
        /// Metoda do przetwarzania zgloszen
        /// </summary>
        /// <param name="client"></param>
        private void ProcessClient(TcpClient client)
        {
            SslStream sslStream = new SslStream(client.GetStream(), false);

            try
            {
                sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);
                int register = ReadOpeningMessage(sslStream, (IPEndPoint)client.Client.RemoteEndPoint);

                if (register == 2)
                {
                    byte[] result = Encoding.UTF8.GetBytes("1");

                    sslStream.Write(result);
                    sslStream.Flush();
                    sslStream.Close();
                    client.Close();
                    return;
                }
                else if (register == 1)
                {
                    byte[] buffer = new byte[128];
                    int    bytes  = sslStream.Read(buffer, 0, buffer.Length);
                    char[] chars  = new char[Encoding.UTF8.GetCharCount(buffer, 0, bytes)];
                    Encoding.UTF8.GetChars(buffer, 0, bytes, chars, 0);

                    StringBuilder messageData = new StringBuilder();
                    messageData.Append(chars);
                    string msg   = messageData.ToString();
                    string login = msg.Substring(3);

                    X509Certificate2 cer = database.GetCertificate(login);
                    byte[]           certificate;
                    if (cer != null)
                    {
                        certificate = CryptoModule.PreparePrivateCertToSend(cer);
                    }
                    else
                    {
                        throw new Exception();
                    }

                    sslStream.Write(certificate);
                    sslStream.Flush();

                    database.RunClient();
                }
                else
                {
                    throw new Exception("Something went wrong");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                byte[] result = Encoding.UTF8.GetBytes("0");
                sslStream.Write(result);
                sslStream.Flush();
                sslStream.Close();
                client.Close();
            }
        }
Example #5
0
        /// <summary>
        /// Metoda do obslugi wiadomosci miedzy klientami
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MsgService(object sender, MsgEvent e)
        {
            StringBuilder sb      = new StringBuilder();
            Decoder       decoder = Encoding.UTF8.GetDecoder();

            char[] chars = new char[decoder.GetCharCount(e.msg, 0, e.msg.Length)];
            decoder.GetChars(e.msg, 0, e.msg.Length, chars, 0);
            sb.Append(chars);

            string message = sb.ToString();

            if (String.Compare("ONL", 0, message, 0, 3) == 0)
            {
                string result = null;
                foreach (Client c in database)
                {
                    if (c.isRunning && !c.isConnected)
                    {
                        result += c.login + " ";
                    }
                }
                byte[] msg;
                if (result == null)
                {
                    msg = Encoding.UTF8.GetBytes("ONLRNikt nie jest online");
                }
                else
                {
                    msg = Encoding.UTF8.GetBytes("ONLR" + result);
                }
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        c.Send(msg);
                    }
                }
                return;
            }
            else if (String.Compare("ISO", 0, message, 0, 3) == 0)
            {
                string login = message.Substring(3);
                bool   isOn  = false;
                string result;
                foreach (Client c in database)
                {
                    if (c.login == login && c.isRunning && !c.isConnected)
                    {
                        isOn = true;
                        break;
                    }
                }
                if (isOn)
                {
                    result = "ISORTrue";
                    foreach (Client c in database)
                    {
                        if (c.login == e.login)
                        {
                            c.connectedTo = login;
                            break;
                        }
                    }
                }
                else
                {
                    result = "ISORFalse";
                }

                byte[] msg = Encoding.UTF8.GetBytes(result);
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        c.Send(msg);
                    }
                }
                return;
            }
            else if (String.Compare("GCR", 0, message, 0, 3) == 0)
            {
                X509Certificate2 cerAlice = null;
                X509Certificate2 cerBob   = null;
                string           alice    = e.login;
                string           bob      = null;
                foreach (Client c in database)
                {
                    if (c.login == alice)
                    {
                        bob = c.connectedTo;
                        break;
                    }
                }
                foreach (Client c in database)
                {
                    if (c.login == bob)
                    {
                        c.connectedTo = alice;
                        break;
                    }
                }
                Console.WriteLine("ALICE is {0}", alice);
                Console.WriteLine("BOB is {0}", bob);
                //Pobranie odpowiednich certyfikatow
                foreach (Client c in database)
                {
                    if (c.login == alice)
                    {
                        cerAlice = c.certificate;
                    }
                    else if (c.login == bob)
                    {
                        cerBob = c.certificate;
                    }
                }
                //Przygotowanie wiadomości dla Boba
                //sb.Clear();
                string prefix = "GCRB";
                int    bytes  = Encoding.UTF8.GetByteCount(prefix);
                //sb.Append(prefix).Append(bytes);
                //prefix = sb.ToString();
                //bytes = Encoding.UTF8.GetByteCount(prefix);
                byte[] pre     = Encoding.UTF8.GetBytes(prefix);
                byte[] rawData = CryptoModule.PreparePublicCertToSend(cerAlice);
                byte[] bobMsg  = new byte[rawData.Length + bytes];
                pre.CopyTo(bobMsg, 0);
                rawData.CopyTo(bobMsg, bytes);

                //Przygotowanie wiadomości dla Alice
                //sb.Clear();
                prefix = "GCRA";
                bytes  = Encoding.UTF8.GetByteCount(prefix);
                //sb.Append(prefix).Append(bytes);
                //prefix = sb.ToString();
                //bytes = Encoding.UTF8.GetByteCount(prefix);
                byte[] preA     = Encoding.UTF8.GetBytes(prefix);
                byte[] rawDataA = CryptoModule.PreparePublicCertToSend(cerBob);
                byte[] aliceMsg = new byte[rawDataA.Length + bytes];
                preA.CopyTo(aliceMsg, 0);
                rawDataA.CopyTo(aliceMsg, bytes);

                //Wyslanie wiadomosci
                foreach (Client c in database)
                {
                    if (c.login == alice)
                    {
                        c.Send(aliceMsg);
                        c.isConnected = true;
                        break;
                    }
                }
                Console.WriteLine("BOB certificate send to ALICE");
                foreach (Client c in database)
                {
                    if (c.login == bob)
                    {
                        c.Send(bobMsg);
                        c.isConnected = true;
                        break;
                    }
                }
                Console.WriteLine("ALICE certificate send to BOB");
                return;
            }
            else if (String.Compare("DIS", 0, message, 0, 3) == 0)
            {
                Console.WriteLine("Logging {0} out", e.login);
                string con    = null;
                bool   conect = false;
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        if (c.isConnected)
                        {
                            con    = c.connectedTo;
                            conect = true;
                        }
                        c.Stop();
                        Console.WriteLine("Client {0} disconnected", c.login);
                        break;
                    }
                }
                if (conect)
                {
                    foreach (Client c in database)
                    {
                        if (c.login == con)
                        {
                            c.Send(Encoding.UTF8.GetBytes("DIC"));
                            c.isConnected = false;
                            c.connectedTo = null;
                            break;
                        }
                    }
                }
            }
            else if (String.Compare("DIC", 0, message, 0, 3) == 0)
            {
                string log = null;
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        log           = c.connectedTo;
                        c.isConnected = false;
                        c.connectedTo = null;
                        break;
                    }
                }
                foreach (Client c in database)
                {
                    if (c.login == log)
                    {
                        c.Send(e.msg);
                        c.isConnected = false;
                        c.connectedTo = null;
                        break;
                    }
                }
            }
            else if (String.Compare("MSG", 0, message, 0, 3) == 0)
            {
                Console.WriteLine("Message from {0}", e.login);
                string log = null;
                foreach (Client c in database)
                {
                    if (c.login == e.login)
                    {
                        log = c.connectedTo;
                        break;
                    }
                }
                foreach (Client c in database)
                {
                    if (c.login == log)
                    {
                        c.Send(e.msg);
                        break;
                    }
                }
                Console.WriteLine("Message sent to {0}", log);
            }
        }