예제 #1
0
파일: Server.cs 프로젝트: cbguder/practises
        /*private void GetCertificate(String domainName)
        {
            //String cert = rootServer.GetCertificate(domainName);
            //String[] certFields = cert.Split(',');
            //Console.WriteLine(cert);
            byte[] rawCertData = rootServer.GetCertificate(domainName);
            if (rawCertData != null)
            {
                Certificate.OpenCertificate();
                Certificate.AddCertificate(rawCertData);
                //Console.WriteLine(Convert.ToBase64String(rawCertData));
                Console.WriteLine("Certificate has been downloaded successfully.");
            }
        }*/
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);

            ServerObject serverobj = new ServerObject();
            //serverobj.KeyObt("*****@*****.**", DateTime.Now);

            Console.Write("Enter passphrase: ");
            passphrase = Console.ReadLine();
            passphrase.Trim();
            Core core = new Core(passphrase);
            core.ReadSettingsFile();

            Server server = new Server();
            server.Connect(core.GetXmlNodeInnerText("root_server"));

            DatabaseConnection connection = new DatabaseConnection();
            String publicKey = core.PublicKey;
            String dbPublicKey = connection.getPublicKey("server");
            connection.close();
            StreamWriter writer = new StreamWriter(core.ActionLogFile, true);
            if (publicKey != dbPublicKey)
            {
                writer.Write(DateTime.Now.ToString() + Core.space);
                writer.WriteLine("Server's old public key:");
                writer.WriteLine();
                writer.WriteLine(dbPublicKey);
                writer.WriteLine();

                //connection = new DatabaseConnection();
                //connection.setPublicKey("server", "server", publicKey);
                connection.updatePublicKey("server", "server", publicKey);
                Console.Write(DateTime.Now.ToString() + Core.space);
                Console.WriteLine("New key pair is set.");
            }
            connection.close();
            writer.Write(DateTime.Now.ToString() + Core.space);
            writer.WriteLine("Server's public key:");
            writer.WriteLine();
            writer.WriteLine(publicKey);
            writer.WriteLine();

            writer.Write(DateTime.Now.ToString() + Core.space);
            writer.WriteLine("Server started");
            writer.Close();

            Console.Write(DateTime.Now.ToString() + Core.space);
            Console.WriteLine("PractiSES Server started.");

            HttpServerChannel channel = new HttpServerChannel(80);
            ChannelServices.RegisterChannel(channel,false);

            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(PractiSES.ServerObject),
                "PractiSES",
                WellKnownObjectMode.SingleCall);
            System.Console.ReadLine();
        }
예제 #2
0
        public bool InitKeySet_SendPublicKey(String userID, String email, String publicKey, String macValue)
        {
            ActionLog_Write(email + ": InitKeySet_SendPublicKey");

            Console.WriteLine(beginProtocol);
            Console.WriteLine(email + ": InitKeySet_SendPublicKey");

            if (SendQuery(userID, email, publicKey, macValue))
            {
                DatabaseConnection connection = new DatabaseConnection();
                //connection.setPublicKey(userID, email, publicKey);
                connection.updatePublicKey(userID, email, publicKey);
                connection.close();

                ActionLog_Write(email + ": Public key is set to:\n\n" + publicKey + "\n");
                Console.WriteLine(email + ": Public key is set.");

                return true;
            }
            return false;
        }
예제 #3
0
        //get public key of a user ( complete )
        public String KeyObt(String email, DateTime date)
        {
            ActionLog_Write(email + ": KeyObt");

            Console.WriteLine(beginProtocol);
            Console.WriteLine(email + ": KeyObt");

            int index = email.IndexOf('@');
            String domainName = email.Substring(index, email.Length - index);
            String publicKey = null;
            Core core = new Core(Server.passphrase);
            if (core.GetXmlNodeInnerText("domain") == domainName)
            {
                DatabaseConnection connection = new DatabaseConnection();
                publicKey = connection.getPublicKey(email, date);
                connection.close();
            }
            else
            {
                byte[] rawCertData = Certificate.SearchCertificate(domainName);
                if (rawCertData == null)
                {
                    if (ConnectRootServer(core.GetXmlNodeInnerText("root_server")))
                    {
                        if (GetCertificate(domainName))
                        {
                            rawCertData = Certificate.SearchCertificate(domainName);
                        }
                    }
                }
                byte[] foreignServerCertPK = Certificate.GetPublicKey(rawCertData);
                String foreignServerXmlPK = Crypto.CertToXMLKey(foreignServerCertPK);
                Console.WriteLine("Public key of " + domainName + ":\n" + foreignServerXmlPK);
                String foreignServerHost = Certificate.GetHostName(rawCertData);

                ActionLog_Write("Connecting to foreign PractiSES server (" + foreignServerHost + ")...");
                Console.WriteLine("Connecting to foreign PractiSES server ({0})...", foreignServerHost);

                IServer foreignServer = (IServer)Activator.GetObject(typeof(IServer), "http://" + foreignServerHost + "/PractiSES");
                String signedPublicKey = foreignServer.KeyObt(email, date);
                if (signedPublicKey != null)
                {
                    Message foreignmessage = new Message(signedPublicKey);
                    //****************
                    if (foreignmessage.Verify(foreignServerXmlPK))
                    {
                        publicKey = foreignmessage.getCleartext();
                    }
                    //****************
                }
            }
            if (publicKey == null)
            {
                ActionLog_Write("Error - " + email + ": Email does not exist!");
                Console.WriteLine("Error - " + email + ": Email does not exist!");
                throw new Exception("Invalid user");
            }
            Message message = new Message(publicKey);
            message.AddComment("Email",email);
            message.Sign(core.PrivateKey);
            String result = message.ToString();
            return result;
        }
예제 #4
0
        private bool SendQuery(String userID, String email, String message, String macValue)
        {
            DatabaseConnection connection = new DatabaseConnection();
            String dbUserid = connection.getUserID(email);
            //   connection.close();
            if (userID == null)
            {
                ErrorLog_Write("Error - " + email + ": Email does not exist!");
                Console.WriteLine("Error - " + email + ": Email does not exist!");
                throw new Exception("Invalid user");
            }
            if (userID != dbUserid)
            {
                ErrorLog_Write("Error - " + email + ": User id does not exist!");
                Console.WriteLine("Error - " + email + ": User id does not exist!");
                throw new Exception("Invalid user");
            }
            //  connection = new DatabaseConnection();
            String dbMACPass = connection.getMACPass(email);
            //   connection.close();

            if (dbMACPass == null)
            {
                ErrorLog_Write("Error: MacPass does not exist!");
                Console.WriteLine("Error: MacPass does not exist!");
                throw new Exception("Invalid Mac Pass");
            }
            HMAC hmac = HMACSHA1.Create();
            hmac.Key = Convert.FromBase64String(dbMACPass);
            byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));
            if (Util.Compare(hash, Convert.FromBase64String(macValue)))
            {
                connection.removeMACPass(email);
                connection.close();

                return true;
            }
            connection.close();

            ErrorLog_Write("Error - " + email + ": MAC value is tampered, public key is not set.");
            Console.WriteLine("Error - " + email + ": MAC value is tampered, public key is not set.");
            throw new Exception("MAC value is tampered, public key is not set");
        }
예제 #5
0
        private bool EnvelopeAnswers(String userID, String email, String answersEnveloped, String bodyMsg)
        {
            DatabaseConnection connection = new DatabaseConnection();
            String dbUserid = connection.getUserID(email);
            //connection.close();
            if (userID == null)
            {
                ErrorLog_Write(email + ": Email does not exist!");
                Console.WriteLine(email + ": Email does not exist!");
                throw new Exception("Invalid user");
            }
            if (userID != dbUserid)
            {
                ErrorLog_Write(email + ": User id does not exist!");
                Console.WriteLine(email + ": User id does not exist!");
                throw new Exception("Invalid user");
            }
            Core core = new Core(Server.passphrase);
            String privateKey = core.PrivateKey;

            Rijndael aes = Rijndael.Create();
            AESInfo aesInfo = Crypto.Destruct(answersEnveloped, privateKey);
            String answers = Encoding.UTF8.GetString(Crypto.AESDecrypt(aesInfo.message, aes.CreateDecryptor(aesInfo.key, aesInfo.IV)));

            //  connection = new DatabaseConnection();
            String dbAnswers = connection.getAnswers(email);
            connection.close();
            if (answers == dbAnswers)
            {
                SendMail(email, aesInfo, bodyMsg);
                return true;
            }
            else
            {
                //protocol stops and socket is closed.
                ErrorMail(email);
                ErrorLog_Write("Error - " + email + ": Answers are not correct!");
                Console.WriteLine("Error - " + email + ": Answers are not correct!");
                throw new Exception("Answers are not correct");
            }
        }
예제 #6
0
        private String EncryptMACPass(String email, AESInfo aesInfo)
        {
            HMAC hmac = HMACSHA1.Create();

            Rijndael aes = Rijndael.Create();

            DatabaseConnection connection = new DatabaseConnection();
            connection.setMACPass(email, Convert.ToBase64String(hmac.Key));
            connection.close();

            String result = Util.Wrap(Convert.ToBase64String(Crypto.AESEncrypt(hmac.Key, aes.CreateEncryptor(aesInfo.key, aesInfo.IV))), 64);
            return result;
        }
예제 #7
0
        private String AskQuestions(String userID, String email)
        {
            Core core = new Core(Server.passphrase);
            DatabaseConnection connection = new DatabaseConnection();
            String dbUserid = connection.getUserID(email);
            connection.close();

            if (userID == null)
            {
                ErrorLog_Write(email + ": Email does not exist!");
                Console.WriteLine(email + ": Email does not exist!");
                throw new Exception("Invalid user");
            }
            if (userID != dbUserid)
            {
                ErrorLog_Write(email + ": User id does not exist!");
                Console.WriteLine(email + ": User id does not exist!");
                throw new Exception("Invalid user");
            }
            String questions = core.ReadSettingsFile();
            Message result = new Message(questions);
            result.Sign(core.PrivateKey);
            return result.ToString();
        }
예제 #8
0
        public bool USKeyUpdate_SendPublicKey(String userID, String email, String newPublicKey, String macValue)
        {
            ActionLog_Write(email + ": USKeyUpdate_SendPublicKey");

            Console.WriteLine(beginProtocol);
            Console.WriteLine(email + ": USKeyUpdate_SendPublicKey");

            if (SendQuery(userID, email, newPublicKey, macValue))
            {
                DatabaseConnection connection = new DatabaseConnection();
                connection.updatePublicKey(userID, email, newPublicKey);
                connection.close();

                ActionLog_Write(email + ": Public key is updated to:\n\n" + newPublicKey + "\n");
                Console.WriteLine(email + ": Public key is updated.");

                return true;
            }
            return false;
        }
예제 #9
0
        public bool USKeyRem_SendRemoveRequest(String userID, String email, String macValue)
        {
            ActionLog_Write(email + ": USKeyRem_SendPublicKey");

            Console.WriteLine(beginProtocol);
            Console.WriteLine(email + ": USKeyRem_SendPublicKey");

            if (SendQuery(userID, email, "I want to remove my current public key", macValue))
            {
                DatabaseConnection connection = new DatabaseConnection();
                connection.removePublicKey(userID, email);
                connection.close();

                ActionLog_Write(email + ": Public key is removed.");
                Console.WriteLine(email + ": Public key is removed.");

                return true;
            }
            return false;
        }
예제 #10
0
        public bool KeyUpdate(String userID, String email, Message signedMessage)
        {
            ActionLog_Write(email + ": KeyUpdate");

            Console.WriteLine(beginProtocol);
            Console.WriteLine(email + ": KeyUpdate");

            DatabaseConnection connection = new DatabaseConnection();
            String publicKey = connection.getPublicKey(email);
            if (signedMessage.Verify(publicKey))
            {
                if (DateTime.Compare(signedMessage.Time, DateTime.Now.AddHours(-1)) >= 0)
                {
                    bool result = connection.updatePublicKey(userID, email, signedMessage.getCleartext());
                    connection.close();
                    return result;
                }
            }
            connection.close();
            throw new Exception("Incorrect message");
        }