Inheritance: Caliburn.Micro.PropertyChangedBase
Exemplo n.º 1
0
        public async void GenerateKeyFromInput()
        {
            CalculationError = string.Empty;
            IsCalculatingKey = true;

            var keyType = KeyType.Bytejail;
            if (BytejailFormat)
            {
                keyType = KeyType.Bytejail;
            }
            else if (CurveLockFormat)
            {
                keyType = KeyType.Curvelock;
            }
            else if (MiniLockFormat)
            {
                keyType = KeyType.Minilock;
            }

            if ((!string.IsNullOrEmpty(UserName)) && (!string.IsNullOrEmpty(UserPassword)))
            {
                if ((keyType == KeyType.Curvelock) || (keyType == KeyType.Minilock))
                {
                    if (!StringHelper.IsValidEmail(UserName))
                    {
                        CalculationError = "miniLock and CurveLock requires a valid email address as first input.";
                        IsCalculatingKey = false;
                        return;
                    }
                }
                try
                {
                    CalculatedKey = await CalculateNewKey(keyType, UserName, UserPassword);
                    TryClose(CalculatedKey != null);
                }
                catch (Exception)
                {
                    CalculationError = "Key calculation failed.";
                }
            }
            else
            {
                CalculationError = "Please enter something in both fields.";
            }
            IsCalculatingKey = false;
        }
Exemplo n.º 2
0
 public void AddKey(Key key)
 {
     if (key == null) return;
     var currentKeyStore = Keystore;
     if (currentKeyStore.Keys == null)
     {
         currentKeyStore.Keys = new List<Key>();
     }
     currentKeyStore.Keys.Add(key);
     Keystore = currentKeyStore;
 }
Exemplo n.º 3
0
 public void RemoveKey(Key key)
 {
     var currentKeyStore = Keystore;
     if (currentKeyStore.Keys != null)
     {
         for (var i = 0; i < currentKeyStore.Keys.Count; i++)
         {
             if (currentKeyStore.Keys[i].PublicKey.SequenceEqual(key.PublicKey))
             {
                 currentKeyStore.Keys.RemoveAt(0);
             }
         }
         Keystore = currentKeyStore;
     }
 }
Exemplo n.º 4
0
 /// <summary>
 ///     Calculate a new key.
 /// </summary>
 /// <returns>A calculated key.</returns>
 private static async Task<Key> CalculateNewKey(KeyType keyType, string userInputOne, string userInputTwo)
 {
     var newKey = await Task.Run(() =>
     {
         var keyPair = KeyGenerator.GenerateBytejailKeyPair(userInputOne, userInputTwo);
         var key = new Key
         {
             KeyType = keyType,
             PrivateKey = keyPair.PrivateKey,
             PublicKey = keyPair.PublicKey
         };
         return key;
     }).ConfigureAwait(true);
     return newKey;
 }
Exemplo n.º 5
0
 /// <summary>
 ///     Remove a key from the store and the listview.
 /// </summary>
 /// <param name="key">The key to remove.</param>
 private void RemoveKey(Key key)
 {
     _lageantCore.RemoveKey(key);
     _keys.Remove(key);
 }
Exemplo n.º 6
0
 /// <summary>
 ///     Add a key to the store and the listview.
 /// </summary>
 /// <param name="key">The key to add.</param>
 private void AddKey(Key key)
 {
     _lageantCore.AddKey(key);
     _keys.Add(key);
 }
Exemplo n.º 7
0
 /// <summary>
 ///     Generate a new random key.
 /// </summary>
 /// <returns>A random key.</returns>
 private static Key GenerateNewKey()
 {
     var keyPair = PublicKeyBox.GenerateKeyPair();
     var key = new Key
     {
         KeyType = KeyType.Lageant,
         PrivateKey = keyPair.PrivateKey,
         PublicKey = keyPair.PublicKey,
         KeyId = SodiumCore.GetRandomBytes(8),
         KeySalt = PasswordHash.GenerateSalt(),
         KeyNonce = PublicKeyBox.GenerateNonce()
     };
     return key;
 }
Exemplo n.º 8
0
        public async void LoadMinisignKeyIntoKeystore()
        {
            MainViewError = string.Empty;
            IsWorking = true;
            string filename;
            string password;
            var openFileDialog = new OpenFileDialog
            {
                DefaultExt = ".key",
                Filter = "Minisign private key (.key)|*.key",
                Multiselect = false
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                filename = openFileDialog.FileName;
                try
                {
                    var win = new PasswordInputViewModel {DisplayName = "Enter minisign password"};
                    dynamic settings = new ExpandoObject();
                    settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                    settings.Owner = GetView();
                    var inputOk = _windowManager.ShowDialog(win, null, settings);
                    if (inputOk == true)
                    {
                        password = win.UserPassword;
                        if (!string.IsNullOrEmpty(password))
                        {
                            var newKey = await Task.Run(() =>
                            {
                                var calculatedKey = new Key();
                                try
                                {
                                    calculatedKey.KeyType = KeyType.Minisign;
                                    var keyPair = Minisign.LoadPrivateKeyFromFile(filename, password);
                                    calculatedKey.PublicKey = keyPair.PublicKey;
                                    calculatedKey.PrivateKey = keyPair.SecretKey;
                                    calculatedKey.KeyId = keyPair.KeyId;
                                }
                                catch (OutOfMemoryException)
                                {
                                    MainViewError = "Could not load minisign key (out of memory).";
                                }
                                catch (Exception)
                                {
                                    MainViewError = "Could not load minisign key.";
                                }
                                return calculatedKey;
                            }).ConfigureAwait(true);

                            if (newKey != null)
                            {
                                if (newKey.PrivateKey != null)
                                {
                                    AddKey(newKey);
                                }
                            }
                        }
                        else
                        {
                            MainViewError = "Could not load minisign key (missing password)";
                        }
                    }
                }
                catch (Exception)
                {
                    MainViewError = "Could not load minisign key (failure)";
                }
            }
            else
            {
                MainViewError = "Could not load minisign key (missing password)";
            }
            IsWorking = false;
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Store/Export a key to file.
        /// </summary>
        public async void ExportKeyToFile()
        {
            MainViewError = string.Empty;
            IsWorking = true;
            string password;
            if (SelectedKey != null)
            {
                var exportKey = new Key
                {
                    PublicKey = SelectedKey.PublicKey,
                    PrivateKey = SelectedKey.PrivateKey,
                    KeyType = SelectedKey.KeyType,
                    KeyId = SelectedKey.KeyId ?? null,
                    KeySalt = SelectedKey.KeySalt ?? PasswordHash.GenerateSalt(),
                    KeyNonce = SelectedKey.KeyNonce ?? SecretBox.GenerateNonce()
                };

                var saveFileDialog = new SaveFileDialog
                {
                    OverwritePrompt = true,
                    AddExtension = true,
                    DefaultExt = ".lkey"
                };

                if (exportKey.KeyId != null)
                {
                    saveFileDialog.FileName = Utilities.BinaryToHex(exportKey.KeyId);
                }
                var result = saveFileDialog.ShowDialog();
                if (result == true)
                {
                    var filename = saveFileDialog.FileName;
                    try
                    {
                        var win = new PasswordInputViewModel {DisplayName = "Enter a new protection password"};
                        dynamic settings = new ExpandoObject();
                        settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                        settings.Owner = GetView();
                        var inputOk = _windowManager.ShowDialog(win, null, settings);
                        if (inputOk == true)
                        {
                            password = win.UserPassword;
                            if (!string.IsNullOrEmpty(password))
                            {
                                var encryptionKey = await Task.Run(() =>
                                {
                                    var tmpKey = new byte[32];
                                    try
                                    {
                                        tmpKey = PasswordHash.ScryptHashBinary(Encoding.UTF8.GetBytes(password),
                                            exportKey.KeySalt, PasswordHash.Strength.MediumSlow, 32);
                                    }
                                    catch (OutOfMemoryException)
                                    {
                                        MainViewError = "Could not export key (out of memory).";
                                    }
                                    catch (Exception)
                                    {
                                        MainViewError = "Could not export key.";
                                    }
                                    return tmpKey;
                                }).ConfigureAwait(true);

                                if (encryptionKey != null)
                                {
                                    exportKey.PrivateKey = SecretBox.Create(exportKey.PrivateKey, exportKey.KeyNonce,
                                        encryptionKey);
                                    using (var keyFileStream = File.OpenWrite(filename))
                                    {
                                        Serializer.SerializeWithLengthPrefix(keyFileStream, exportKey,
                                            PrefixStyle.Base128,
                                            0);
                                    }
                                }
                                else
                                {
                                    MainViewError = "Could not export key (missing encryption key?)";
                                }
                            }
                            else
                            {
                                MainViewError = "Could not export key (missing password)";
                            }
                        }
                    }
                    catch (Exception)
                    {
                        MainViewError = "Could not export key (failure)";
                    }
                }
                else
                {
                    MainViewError = "Could not export key (missing password)";
                }
            }
            IsWorking = false;
        }