Example #1
0
    public bool ActualSaveAsCommonSecretsContainer()
    {
        // Check that there is a least one secret when saving (otherwise there is no way to verify passwords when opening)
        if (!this.CommonSecretsContainerHasAtLeastOneSecret())
        {
            MessageBox.Show("There must be at least one secret! Otherwise no password verification can be done for file.", "Error");
            return(false);
        }

        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "CommonSecrets JSON (*.commonsecrets.json)|*.commonsecrets.json|CommonSecrets XML (*.commonsecrets.xml)|*.commonsecrets.xml";
        saveFileDialog.Title  = "Save a CommonSecrets file";
        if (saveFileDialog.ShowDialog() == true && !string.IsNullOrEmpty(saveFileDialog.FileName))
        {
            try
            {
                // Assume JSON for now
                byte[] jsonBytes = SerializationDefinitions.serializers[DeserializationFormat.Json](this.csc);
                File.WriteAllBytes(saveFileDialog.FileName, jsonBytes);
                this.filePath   = saveFileDialog.FileName;
                this.saveFormat = DeserializationFormat.Json;
                this.isModified = false;
                this.UpdateMainTitle(this.filePath);
                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show($"Error happened while saving: {e}", "Error");
            }
        }

        return(false);
    }
        /// <summary>
        /// Final open step when file contains secrets
        /// </summary>
        /// <param name="filename">Filename</param>
        /// <param name="fileFormat">File format</param>
        /// <param name="tempContainer">Temp container</param>
        /// <param name="newDerivedPasswords">New derived passwords</param>
        private void FinalOpenStepWithSecrets(string filename, DeserializationFormat fileFormat, CommonSecretsContainer tempContainer, Dictionary <string, byte[]> newDerivedPasswords)
        {
            // Check that every entry can be decoded with given passwords
            bool success = true;

            foreach (LoginInformationSecret loginInformationSecret in tempContainer.loginInformationSecrets)
            {
                if (!loginInformationSecret.CanBeDecryptedWithDerivedPassword(newDerivedPasswords[loginInformationSecret.GetKeyIdentifier()]))
                {
                    //MessageBox.Show($"Cannot decrypt login information secret which uses key identifier: {loginInformationSecret.GetKeyIdentifier()}", "Decryption error");
                    success = false;
                    break;
                }
            }

            if (!success)
            {
                return;
            }

            foreach (NoteSecret noteSecret in tempContainer.noteSecrets)
            {
                if (!noteSecret.CanBeDecryptedWithDerivedPassword(newDerivedPasswords[noteSecret.GetKeyIdentifier()]))
                {
                    //MessageBox.Show($"Cannot decrypt note secret which uses key identifier: {noteSecret.GetKeyIdentifier()}", "Decryption error");
                    success = false;
                    break;
                }
            }

            if (!success)
            {
                return;
            }

            foreach (FileEntrySecret fileEntrySecret in tempContainer.fileSecrets)
            {
                if (!fileEntrySecret.CanBeDecryptedWithDerivedPassword(newDerivedPasswords[fileEntrySecret.GetKeyIdentifier()]))
                {
                    //MessageBox.Show($"Cannot decrypt file secret which uses key identifier: {fileEntrySecret.GetKeyIdentifier()}", "Decryption error");
                    success = false;
                    break;
                }
            }

            if (!success)
            {
                return;
            }

            // SUCCESS POINT
            this.derivedPasswords.Clear();
            foreach (var kvp in newDerivedPasswords)
            {
                this.derivedPasswords.Add(kvp.Key, kvp.Value);
            }

            this.csc        = tempContainer;
            this.isModified = false;
            this.filePath   = filename;

            // Select the save format based on format of file opened (and assuming we know how to save it)
            if (DeserializationDefinitions.deserializers[fileFormat].savingSupported)
            {
                this.saveFormat = fileFormat;
            }
            else
            {
                this.saveFormat = DeserializationFormat.None;
            }

            this.UpdateMainTitle(filename);

            // Enable save features
            OnPropertyChanged(nameof(this.IsSaveEnabled));

            // Change UI
            OnPropertyChanged(nameof(this.TabsVisibility));
            OnPropertyChanged(nameof(this.WizardVisibility));

            this.GenerateLoginSimplifiedsFromCommonSecrets();
            this.GenerateNoteSimplifiedsFromCommonSecrets();
            this.GenerateFileSimplifiedsFromCommonSecrets();
        }