Example #1
0
        private int VerifyDetached(String filename, String sender)
        {
            Message message = new Message(File.ReadAllBytes(filename));
            String[] siglines = Util.GetLines(File.ReadAllText(filename + ".pses"));
            message.Signature = Convert.FromBase64String(String.Join("", siglines, 1, siglines.Length - 2));

            if (Verify(message, sender, false))
                return 0;
            else
                return 1;
        }
Example #2
0
 public void Decrypt(String privateKey)
 {
     Message temp = new Message(Crypto.RAWDecrypt(ciphertext, privateKey));
     cleartext = temp.cleartext;
     signature = temp.signature;
     comments = temp.comments;
 }
Example #3
0
        private int Verify(String filename, String sender)
        {
            Message message;

            try
            {
                message = new Message(File.ReadAllText(filename, Encoding.UTF8));
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return 2;
            }

            if (Verify(message, sender, true))
                return 0;
            else
                return 1;
        }
Example #4
0
        private bool Verify(Message message, String sender, bool includeComments)
        {
            String publicKey = FetchPublicKey(sender);

            if (publicKey == null)
                return false;

            bool result = message.Verify(publicKey, includeComments);

            if (result)
                Console.Error.WriteLine("Message verification succeeded.");
            else
                Console.Error.WriteLine("Message verification failed.");

            return result;
        }
Example #5
0
        private void Sign(String filename, String passphrase)
        {
            try
            {
                core.InitializeKeys(passphrase);
            }
            catch
            {
                Console.Error.WriteLine("Invalid passphrase");
                return;
            }

            String outFile = filename + ".pses";

            Message message = new Message(File.ReadAllText(filename, Encoding.UTF8));
            message.Sign(core.PrivateKey);

            if (Util.Write(outFile, message.ToString()))
            {
                Console.Error.WriteLine("Output written to {0}", outFile);
            }
        }
Example #6
0
        private void SignDetached(String filename, String passphrase, String outfile)
        {
            try
            {
                core.InitializeKeys(passphrase);
            }
            catch
            {
                Console.Error.WriteLine("Invalid passphrase");
                return;
            }

            Message message = new Message(File.ReadAllBytes(filename));
            message.Sign(core.PrivateKey, false);

            if (Util.Write(outfile, message.getSignature(), true))
            {
                Console.Error.WriteLine("Output written to {0}", outfile);
            }
        }
Example #7
0
        private String FetchPublicKey(String userID)
        {
            String publicKey;

            while (userID == null || userID == "")
            {
                Console.Write("Sender: ");
                userID = Console.ReadLine();
                userID.Trim();
            }

            try
            {
                Connect();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0}", e.Message);
                return null;
            }

            try
            {
                publicKey = server.KeyObt(userID);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0}", e.Message);
                return null;
            }

            Message message = new Message(publicKey);

            if (message.Verify(serverKey))
            {
                return Encoding.UTF8.GetString(message.Cleartext);
            }
            else
            {
                Console.Error.WriteLine("WARNING: Message from server is tampered with.");
                return null;
            }
        }
Example #8
0
        private void Initialize(String passphrase)
        {
            if (File.Exists(core.KeyFile))
            {
                Console.Write("Are you ABSOLUTELY sure that you want to delete your existing keys FOREVER? (y/N): ");
                String response = Console.ReadLine();
                response.Trim();

                if (response == "y")
                    File.Delete(core.KeyFile);
                else
                    return;
            }

            try
            {
                core.InitializeKeys(passphrase);
            }
            catch
            {
                Console.Error.WriteLine("Invalid passphrase");
                return;
            }

            Console.Write("Username: "******"Email: ");
            String email = Console.ReadLine();

            StreamWriter sw = new StreamWriter(Path.Combine(core.ApplicationDataFolder, "identity"));
            sw.WriteLine(username);
            sw.WriteLine(email);
            sw.Close();

            Connect();
            String questionsFromServer;

            try
            {
                questionsFromServer = server.InitKeySet_AskQuestions(username, email);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return;
            }

            Message questions = new Message(questionsFromServer);

            if (!questions.Verify(serverKey))
            {
                Console.Error.WriteLine("WARNING: Message from server is tampered with.");
                Console.Error.WriteLine(questionsFromServer);
                return;
            }

            Console.WriteLine("Questions:");
            Console.WriteLine(questions.getCleartext());
            Console.Write("Answers: ");
            String answers = Console.ReadLine();

            byte[] message = Encoding.UTF8.GetBytes(answers);
            Rijndael aes = Rijndael.Create();
            String encrypted = Crypto.Encrypt(message, serverKey, aes);

            ArrayList key = new ArrayList();
            key.AddRange(aes.Key);
            key.AddRange(aes.IV);

            File.WriteAllBytes(Path.Combine(core.ApplicationDataFolder, "answers.key"),
                               (byte[]) key.ToArray(Type.GetType("System.Byte")));

            try
            {
                server.InitKeySet_EnvelopeAnswers(username, email, encrypted);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return;
            }

            Console.Error.WriteLine("Answers sent. Please check your email to finalize PractiSES initialization.");
        }
Example #9
0
        /*
        private void WriteIdentity(String username, String email)
        {
            StreamWriter sw = new StreamWriter(Path.Combine(core.ApplicationDataFolder, "identity"));
            sw.WriteLine(username);
            sw.WriteLine(email);
            sw.Close();
        }
        */
        private void Encrypt(String filename, String recipient, String outfile)
        {
            String publicKey = FetchPublicKey(recipient);
            if (publicKey == null)
                return;

            Message message = new Message(File.ReadAllBytes(filename));
            message.Encrypt(publicKey);

            if (Util.Write(outfile, message.ToString()))
            {
                Console.Error.WriteLine("Output written to {0}", outfile);
            }
        }
Example #10
0
        private void Decrypt(String filename, String passphrase, String outfile)
        {
            try
            {
                core.InitializeKeys(passphrase);
            }
            catch
            {
                Console.Error.WriteLine("Invalid passphrase");
                return;
            }

            Message message = new Message(File.ReadAllText(filename));
            message.Decrypt(core.PrivateKey);

            if (Util.Write(outfile, message.Cleartext))
            {
                Console.Error.WriteLine("Output written to {0}", outfile);
            }
        }
Example #11
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;
        }
Example #12
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();
        }
Example #13
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");
        }