public EditViewFileViewModel(FileEntrySecret current, byte[] derivedPassword, int zeroBasedIndexNumber, List <string> keyIds, Action positiveAction, Action negativeAction, Action <int /* zeroBasedIndexNumber */, bool /* isNowSecure */, bool /* Was Security Modified */, string /* Key identifier */> edit)
    {
        this.KeyIdentifiers = new ObservableCollection <string>();
        foreach (string keyIdentifier in keyIds)
        {
            this.KeyIdentifiers.Add(keyIdentifier);
        }

        if (keyIds.Count > 0)
        {
            this.SelectedKeyIdentifier = keyIds[0];
        }

        this.onPositiveClose = positiveAction;
        this.onNegativeClose = negativeAction;
        this.editFile        = edit;

        this.currentFileEntrySecret = current;
        this.derivedPassword        = derivedPassword;
        this.zeroBasedIndexNumber   = zeroBasedIndexNumber;
        this.wasOriginallySecret    = true;
        this.IsSecret = true;

        this.Filename = currentFileEntrySecret.GetFilename(this.derivedPassword);
    }
        public void DeepCopyTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                111, 222, 31, 4, 5, 6, 1, 82, 93, 102, 112, 120, 103, 104, 15, 16
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xfb, 0x13, 0xaa, 0xf5, 0x36, 0xbb, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "nic2e.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0, 23, 34, 33, 22, 222, 111 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            FileEntrySecret fesCopy        = new FileEntrySecret(fes);
            string          filenameInCopy = fesCopy.GetFilename(derivedKey);

            // Assert
            Assert.IsFalse(string.IsNullOrEmpty(filenameInCopy));
            Assert.AreEqual(filename, filenameInCopy);
            Assert.AreNotSame(fes.audalfData, fesCopy.audalfData, "AUDALF byte arrays should be in different memory locations");
            CollectionAssert.AreEqual(fes.keyIdentifier, fesCopy.keyIdentifier);
            Assert.AreNotSame(fes.keyIdentifier, fesCopy.keyIdentifier, "Key identifier byte arrays should be in different memory locations");
            Assert.AreEqual(fes.checksum, fesCopy.checksum);
        }
        public void GetFileEntryTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "ni12ce.pdf";

            byte[] fileContent = new byte[] { 1, 2, 32, 11, 2, byte.MaxValue, 0, 0, 2, 34, 45, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            FileEntry feCopy = fes.GetFileEntry(derivedKey);

            // Assert
            Assert.IsTrue(ComparisonHelper.AreFileEntriesEqual(fe, feCopy));
            Assert.AreEqual(fe.creationTime, feCopy.creationTime);
            Assert.AreEqual(fe.modificationTime, feCopy.modificationTime);
        }
        public void SetFileContentTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                111, 222, 31, 4, 5, 6, 7, 8, 98, 10, 11, 12, 131, 104, 15, 16
            };

            SymmetricKeyAlgorithm ska = SymmetricKeyAlgorithm.GenerateNew(SymmetricEncryptionAlgorithm.AES_CTR);

            string filename = "backup.zip";

            byte[] fileContent = Encoding.UTF8.GetBytes("peace, happiness, freedom and something...");

            FileEntry fileEntry = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fileEntry, "does not matter", ska, derivedKey);

            byte[] fileContent1 = Encoding.UTF8.GetBytes("this is a wrong backup for this situation");

            // Act
            bool shouldBeTrue = fes.SetFileContent(fileContent1, derivedKey);

            byte[] fileContent2  = fes.GetFileContent(derivedKey);
            bool   shouldBeFalse = fes.SetFileContent(fileContent1, new byte[] { 1, 2, 3 });

            // Assert
            Assert.IsTrue(shouldBeTrue);
            Assert.IsFalse(shouldBeFalse);
            CollectionAssert.AreEqual(fileContent1, fileContent2);
        }
        public void ChecksumSurvivesRoundtrip()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 255
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "nice.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes1 = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            string checksum1 = fes1.GetChecksumAsHex();

            string json = JsonConvert.SerializeObject(fes1, Formatting.Indented);

            FileEntrySecret fes2 = JsonConvert.DeserializeObject <FileEntrySecret>(json);

            // Assert
            Assert.AreEqual(64, checksum1.Length);
            Assert.AreEqual(checksum1, fes2.GetChecksumAsHex());
        }
        public void GetKeyIdentifierTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                111, 222, 31, 47, 25, 138, 78, 83, 111, 110, 221, 18, 213, 104, 15, 16
            };
            byte[] initialCounter = new byte[] { 0xa0, 0xb1, 0xcb, 0xcd, 0xaa, 0xc5, 0x13, 0xb5, 0x58, 0x59, 0x13, 0x2b, 0x33, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 256, settingsAES_CTR);

            string keyIdentifier = "primary";

            string filename = "nice.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            // Act
            FileEntrySecret fes = new FileEntrySecret(fe, keyIdentifier, skaAES_CTR, derivedKey);

            // Assert
            Assert.AreEqual(keyIdentifier, fes.GetKeyIdentifier());
        }
        public void CanBeDecryptedWithDerivedPassword()
        {
            byte[] derivedKey1 = new byte[16] {
                111, 222, 36, 47, 75, 168, 78, 13, 61, 118, 221, 18, 213, 104, 15, 16
            };
            byte[] derivedKey2 = new byte[16] {
                111, 222, 36, 47, 75, 168, 78, 13, 61, 118, 221, 18, 213, 104, 15, 15
            };
            byte[] initialCounter = new byte[] { 0xa7, 0xb1, 0xcb, 0xcd, 0xaa, 0xc5, 0xd3, 0xb5, 0x58, 0x51, 0x95, 0x2b, 0x33, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 256, settingsAES_CTR);

            string keyIdentifier = "primary";

            string filename = "nice.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 100, 222, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            // Act
            FileEntrySecret fes = new FileEntrySecret(fe, keyIdentifier, skaAES_CTR, derivedKey1);

            // Assert
            Assert.IsTrue(fes.CanBeDecryptedWithDerivedPassword(derivedKey1));
            Assert.IsFalse(fes.CanBeDecryptedWithDerivedPassword(null));
            Assert.IsFalse(fes.CanBeDecryptedWithDerivedPassword(new byte[] {}));
            Assert.IsFalse(fes.CanBeDecryptedWithDerivedPassword(derivedKey2));
        }
        public void ConstructorTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            Dictionary <string, object> testDictionary = new Dictionary <string, object>()
            {
                { FileEntry.filenameKey, "filename.txt" }
            };

            FileEntrySecret fes = new FileEntrySecret(testDictionary, "does not matter", skaAES_CTR, derivedKey);

            // Act

            // Assert
            Assert.IsNotNull(fes);
            Assert.IsNotNull(fes.audalfData);
        }
        public void GetModificationTimeTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 255
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "nice.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            DateTimeOffset fileEntryModificationTime1 = fes.GetModificationTime(derivedKey);

            Thread.Sleep(1100);
            fes.SetFilename("much_nicer.pdf", derivedKey);
            DateTimeOffset fileEntryModificationTime2 = fes.GetModificationTime(derivedKey);

            // Assert
            Assert.Greater(fileEntryModificationTime2, fileEntryModificationTime1);
        }
        public void GetFileContentLengthInBytes()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 255
            };
            byte[] initialCounter = new byte[] { 0x40, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0x38, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "nice232fwf.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 8, 9, 33, 44, 55, 66, 77 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            long fileContentLength = fes.GetFileContentLengthInBytes(derivedKey);

            // Assert
            Assert.AreEqual(fileContent.LongLength, fileContentLength);
        }
        public void GetFileContentTest()
        {
            // Arrange
            byte[] derivedKey = new byte[16] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 255
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            SettingsAES_CTR settingsAES_CTR = new SettingsAES_CTR(initialCounter);

            SymmetricKeyAlgorithm skaAES_CTR = new SymmetricKeyAlgorithm(SymmetricEncryptionAlgorithm.AES_CTR, 128, settingsAES_CTR);

            string filename = "nice.pdf";

            byte[] fileContent = new byte[] { 1, 2, 3, 1, 2, byte.MaxValue, 0, 0, 0, 0, 0, 0 };

            FileEntry fe = new FileEntry(filename, fileContent);

            FileEntrySecret fes = new FileEntrySecret(fe, "does not matter", skaAES_CTR, derivedKey);

            // Act
            byte[] rtFileContent = fes.GetFileContent(derivedKey);

            // Assert
            CollectionAssert.AreEqual(fileContent, rtFileContent);
        }
Ejemplo n.º 12
0
    private void EditFileInCollection(int zeroBasedIndexNumber, bool isNowSecure, bool wasSecurityModified, string keyIdentifier)
    {
        if (wasSecurityModified)
        {
            // If file jumps from secure <-> unsecure
            if (isNowSecure)
            {
                this.csc.AddFileEntrySecret(this.derivedPasswords[keyIdentifier], this.csc.files[zeroBasedIndexNumber], keyIdentifier);
                this.csc.files.RemoveAt(zeroBasedIndexNumber);
            }
            else
            {
                FileEntrySecret fes             = this.csc.fileSecrets[zeroBasedIndexNumber];
                byte[]          derivedPassword = derivedPasswords[fes.GetKeyIdentifier()];
                FileEntry       tempFileEntry   = new FileEntry(fes.GetFilename(derivedPassword), fes.GetFileContent(derivedPassword));
                this.csc.fileSecrets.RemoveAt(zeroBasedIndexNumber);
                this.csc.files.Add(tempFileEntry);
            }
        }
        else
        {
            if (isNowSecure)
            {
                // TODO: support key change
            }
        }

        // Editing a file modifies the structure
        this.isModified = true;
        this.UpdateMainTitle(this.filePath != null ? this.filePath : untitledTempName);

        this.GenerateFileSimplifiedsFromCommonSecrets();
    }
Ejemplo n.º 13
0
 public EditViewFileWindow(FileEntrySecret current, byte[] derivedPassword, int zeroBasedIndexNumber, List <string> keyIdentifiers, Action <int /* zeroBasedIndexNumber */, bool /* isNowSecure */, bool /* Was Security Modified */, string /* Key identifier */> editFile)
 {
     InitializeComponent();
     DataContext = new EditViewFileViewModel(current, derivedPassword, zeroBasedIndexNumber, keyIdentifiers, EditClose, CancelClose, editFile);
 }
 public static bool AreFileEntrySecretsEqual(FileEntrySecret fileEntrySecret1, FileEntrySecret fileEntrySecret2)
 {
     return(StructuralComparisons.StructuralEqualityComparer.Equals(fileEntrySecret1.audalfData, fileEntrySecret2.audalfData) && AreSymmetricKeyAlgorithmsEqual(fileEntrySecret1.algorithm, fileEntrySecret2.algorithm));
 }