Esempio n. 1
0
        public static async Task <Keypair> LoadAsync(string path)
        {
            // TODO: async file reading
            byte[] contents = File.ReadAllBytes(path);
            await Task.FromResult(0);

            Type      armorProviderType = ArmorRecognizer.RecognizeArmor(contents);
            var       armorProvider     = (IArmorProvider)Activator.CreateInstance(armorProviderType);
            ArmorType armorType         = armorProvider.GetArmorType(contents);

            if (armorType != ArmorType.PublicKey && armorType != ArmorType.PrivateKey)
            {
                throw new InvalidOperationException(
                          "Armor není správného typu (neobsahuje ani veřejný, ani soukromý klíč)");
            }
            byte[] rawData = armorProvider.FromArmor(contents).rawData;
            var    keyPair = LZ4MessagePackSerializer.Deserialize <Keypair>(rawData);

            return(keyPair);
        }
Esempio n. 2
0
        private async void butOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            byte[] bytes     = Encoding.UTF8.GetBytes(textBox.Text);
            Type   armorType = ArmorRecognizer.RecognizeArmor(bytes);
            var    armor     = Activator.CreateInstance(armorType) as IArmorProvider;

            Debug.Assert(armor != null, nameof(armor) + " != null");
            (byte[] rawData, ArmorType armorDataType) = armor.FromArmor(bytes);
            if (armorDataType != ArmorType.PublicKey)
            {
                MessageBox.Show("Vložený text není veřejný klíč.");
                Close();
                return;
            }

            await Keychain.AddKeyPair(Keypair.FromMessagePack(rawData));

            DialogResult = DialogResult.OK;
            Close();
        }
Esempio n. 3
0
        private void butProvest_Click(object sender, EventArgs e)
        {
            // TODO allow file import
            // todo make async
            byte[]              content       = Encoding.UTF8.GetBytes(textBox_main.Text);
            Keypair             chosenKeypair = ucKeychain_mainTab.SelectedItems.FirstOrDefault();
            IArmorProvider      armorProvider = new HitaiArmorProvider();
            Message             message;
            PasswordInputDialog passwordDialog;
            Signature           signature;
            bool correctSignature;

            switch (comboBox_actions.SelectedIndex)
            {
            case 0:     // šifrovat
                if (chosenKeypair == null)
                {
                    MessageBox.Show("Musíte vybrat klíč adresáta.");
                    return;
                }

                message = AsymmetricEncryptionController.Encrypt(chosenKeypair, content);
                byte[] armor =
                    armorProvider.ToArmor(LZ4MessagePackSerializer.Serialize(message),
                                          ArmorType.Message);
                string result = Encoding.UTF8.GetString(armor);
                Clipboard.SetText(result);
                MessageBox.Show("Výsledek byl zkopírován do schránky.");
                break;

            case 1:     // dešifrovat
                Type armorClass = ArmorRecognizer.RecognizeArmor(content);
                armorProvider = (IArmorProvider)Activator.CreateInstance(armorClass);
                (byte[] bytes, ArmorType armorType) = armorProvider.FromArmor(content);
                // TODO nechat rozpoznat akci podle ArmorType
                if (armorType != ArmorType.Message)
                {
                    MessageBox.Show("Obsah není zprávou.");
                    return;
                }

                message = LZ4MessagePackSerializer.Deserialize <Message>(bytes);
                Keypair recipient =
                    Keychain.Keys.FirstOrDefault(x => x.ShortId == message.RecipientId);
                if (recipient == null)
                {
                    MessageBox.Show("Nebyl nalezen odpovídající soukromý klíč.");
                    return;
                }

                passwordDialog = new PasswordInputDialog("Dešifrování");
                if (passwordDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                byte[] data;
                try {
                    data = AsymmetricEncryptionController.Decrypt(message,
                                                                  passwordDialog.Password,
                                                                  recipient);
                }
                catch (Exception ex) {
                    while (ex is AggregateException)
                    {
                        ex = ex.InnerException;
                    }
                    if (!(ex is CryptographicException))
                    {
                        throw;
                    }
                    MessageBox.Show("Nesprávné heslo.");
                    return;
                }

                string clearText = Encoding.UTF8.GetString(data);
                textBox_main.Text = clearText;
                break;

            case 2:     // podepsat
                if (!chosenKeypair.IsPrivate)
                {
                    MessageBox.Show("Podepisující klíč musí být soukromý.");
                    return;
                }

                passwordDialog = new PasswordInputDialog("Podepisování");
                if (passwordDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                try {
                    signature = AsymmetricEncryptionController.Sign(
                        Encoding.UTF8.GetBytes(textBox_main.Text),
                        passwordDialog.Password, chosenKeypair);
                }
                catch (Exception ex) {
                    while (ex is AggregateException)
                    {
                        ex = ex.InnerException;
                    }
                    if (!(ex is CryptographicException))
                    {
                        throw;
                    }
                    MessageBox.Show("Nesprávné heslo.");
                    return;
                }
                Clipboard.SetText(Encoding.UTF8.GetString(armorProvider.ToArmor(
                                                              LZ4MessagePackSerializer.Serialize(signature),
                                                              ArmorType.Signature)));
                MessageBox.Show("Výsledek byl zkopírován do schránky");
                break;

            case 3:     // ověřit
                armorProvider =
                    (IArmorProvider)Activator.CreateInstance(
                        ArmorRecognizer.RecognizeArmor(content));
                (byte[] bytes2, ArmorType armorType2) = armorProvider.FromArmor(content);
                if (armorType2 != ArmorType.Signature)
                {
                    MessageBox.Show("Vstup neobsahuje podpis.");
                    return;
                }

                signature        = LZ4MessagePackSerializer.Deserialize <Signature>(bytes2);
                correctSignature = AsymmetricEncryptionController.Verify(signature, Keychain);
                string clearText2;
                try {
                    clearText2 = signature.GetCleartext();
                }
                catch {
                    clearText2 = null;
                }

                MessageBox.Show((correctSignature ? "Správný podpis" : "Nesprávný podpis") +
                                (string.IsNullOrEmpty(clearText2)
                                        ? "."
                                        : " pro zprávu:\n" + clearText2));
                break;

            case 4:     // šifrovat a podepsat
                var chooseKeyDialog =
                    new ChooseKeyDialog("Vyberte klíč, kterým chcete zprávu podepsat.")
                {
                    Keychain = Keychain
                };
                if (chooseKeyDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                message        = AsymmetricEncryptionController.Encrypt(chosenKeypair, content);
                passwordDialog = new PasswordInputDialog("Podepisování");
                if (passwordDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                try {
                    signature = AsymmetricEncryptionController.Sign(message,
                                                                    passwordDialog.Password, chooseKeyDialog.ChosenKeypair);
                }
                catch (Exception ex) {
                    while (ex is AggregateException)
                    {
                        ex = ex.InnerException;
                    }
                    if (!(ex is CryptographicException))
                    {
                        throw;
                    }
                    MessageBox.Show("Nesprávné heslo.");
                    return;
                }
                result = Encoding.UTF8.GetString(armorProvider.ToArmor(
                                                     LZ4MessagePackSerializer.Serialize(signature),
                                                     ArmorType.SignedMessage));
                Clipboard.SetText(result);
                MessageBox.Show("Výsledek byl zkopírován do schránky");
                break;

            case 5:     // dešifrovat a ověřit
                armorProvider =
                    (IArmorProvider)Activator.CreateInstance(
                        ArmorRecognizer.RecognizeArmor(content));
                (byte[] bytes3, ArmorType armorType3) = armorProvider.FromArmor(content);
                if (armorType3 != ArmorType.SignedMessage)
                {
                    MessageBox.Show("Vstup neobsahuje podepsanou zprávu.");
                    return;
                }

                signature        = LZ4MessagePackSerializer.Deserialize <Signature>(bytes3);
                correctSignature = AsymmetricEncryptionController.Verify(signature, Keychain);
                if (!correctSignature)
                {
                    MessageBox.Show(
                        "Podpis není správný. Zpráva nebude dešifrována z bezpečnostních důvodů.");
                    return;
                }

                if (!signature.ContainsMessage())
                {
                    MessageBox.Show(
                        "Podpis neobsahuje zašifrovanou zprávu. Chyba.");
                    return;
                }

                passwordDialog = new PasswordInputDialog("Dešifrování");
                if (passwordDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                try {
                    result = Encoding.UTF8.GetString(
                        AsymmetricEncryptionController.Decrypt(signature.GetMessage(),
                                                               passwordDialog.Password, Keychain));
                }
                catch (Exception ex) {
                    while (ex is AggregateException)
                    {
                        ex = ex.InnerException;
                    }
                    if (!(ex is CryptographicException))
                    {
                        throw;
                    }
                    MessageBox.Show("Nesprávné heslo.");
                    return;
                }
                MessageBox.Show(
                    "Podpis byl úspěšně ověřen a dešifrovaná zpráva bude zobrazena.");
                textBox_main.Text = result;
                break;

            default:
                throw new NotImplementedException();
            }
        }