public static void TestFindMagicGuidFromSimpleFile() { using (Stream testStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
/// <summary> /// Loads an AxCrypt file from the specified reader. After this, the reader is positioned to /// read encrypted data. /// </summary> /// <param name="stream">The stream to read from. Will be disposed when this instance is disposed.</param> /// <returns>True if the key was valid, false if it was wrong.</returns> public bool Load(Stream stream, AesKey key) { _reader = AxCryptReader.Create(stream); DocumentHeaders documentHeaders = new DocumentHeaders(key); PassphraseIsValid = documentHeaders.Load(_reader); if (PassphraseIsValid) { DocumentHeaders = documentHeaders; } return(PassphraseIsValid); }
public static void TestFindMagicGuidFirstAndOnly() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
public static void TestFindMagicGuidButInputTooShort() { using (MemoryStream testStream = new MemoryStream()) { byte[] someBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; testStream.Write(someBytes, 0, someBytes.Length); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.False, "There should be no Guid found, since there are not enough bytes in the stream."); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.EndOfStream), "Nothing has been found yet in the stream."); } } }
public static void TestFindMagicGuidWithOtherFirstButNoMore() { using (MemoryStream testStream = new MemoryStream()) { byte[] someBytes = Encoding.UTF8.GetBytes("This is a test string that we'll convert into some random bytes...."); testStream.Write(someBytes, 0, someBytes.Length); AxCrypt1Guid.Write(testStream); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
public static void TestBadKey() { using (Stream testStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader reader = AxCryptReader.Create(testStream)) { Passphrase passphrase = new Passphrase("b"); DocumentHeaders documentHeaders = new DocumentHeaders(passphrase.DerivedPassphrase); bool isPassphraseValid = documentHeaders.Load(reader); Assert.That(isPassphraseValid, Is.False, "The passphrase is intentionally wrong for this test case."); Assert.That(documentHeaders.HmacSubkey, Is.Null, "Since the passphrase is wrong, HmacSubkey should return null."); Assert.That(documentHeaders.DataSubkey, Is.Null, "Since the passphrase is wrong, DataSubkey should return null."); Assert.That(documentHeaders.HeadersSubkey, Is.Null, "Since the passphrase is wrong, HeadersSubkey should return null."); } } }
public void TestUnwrapFromSimpleFile() { using (Stream testStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { KeyWrap1HeaderBlock keyWrapHeaderBlock = null; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { int headers = 0; while (axCryptReader.Read()) { switch (axCryptReader.CurrentItemType) { case AxCryptItemType.None: break; case AxCryptItemType.MagicGuid: break; case AxCryptItemType.HeaderBlock: if (axCryptReader.CurrentHeaderBlock.HeaderBlockType == HeaderBlockType.KeyWrap1) { keyWrapHeaderBlock = (KeyWrap1HeaderBlock)axCryptReader.CurrentHeaderBlock; ++headers; } break; case AxCryptItemType.Data: break; case AxCryptItemType.EndOfStream: break; default: break; } } Assert.That(headers, Is.EqualTo(1), "We're expecting exactly one KeyWrap1 block to be found!"); byte[] wrapped = keyWrapHeaderBlock.GetKeyData(); using (KeyWrap keyWrap = new KeyWrap(new Passphrase("a").DerivedPassphrase, keyWrapHeaderBlock.Salt, keyWrapHeaderBlock.Iterations, KeyWrapMode.AxCrypt)) { byte[] unwrapped = keyWrap.Unwrap(wrapped); Assert.That(unwrapped.Length, Is.Not.EqualTo(0), "An unwrapped key is invalid if it is returned as a zero-length array."); } } } }
public static void TestFindPreambleHeaderBlockFromSimpleFile() { using (Stream testStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { bool blockFound = false; int headers = 0; while (axCryptReader.Read()) { switch (axCryptReader.CurrentItemType) { case AxCryptItemType.None: break; case AxCryptItemType.MagicGuid: break; case AxCryptItemType.HeaderBlock: if (axCryptReader.CurrentHeaderBlock.HeaderBlockType == HeaderBlockType.Preamble) { Assert.That(blockFound, Is.False, "We should only find one single PreambleHeaderBlock"); blockFound = true; Assert.That(headers, Is.EqualTo(0), "Preamble must be first"); PreambleHeaderBlock preambleHeaderBlock = (PreambleHeaderBlock)axCryptReader.CurrentHeaderBlock; Assert.That(preambleHeaderBlock.Hmac.Length, Is.EqualTo(16), "The HMAC in the preamble must be exactly 16 bytes."); } ++headers; break; case AxCryptItemType.Data: break; case AxCryptItemType.EndOfStream: break; default: break; } } Assert.That(blockFound, Is.True, "We're expecting a VersionHeaderBlock to be found!"); } } }
public static void TestFactoryMethod() { Assert.Throws <ArgumentNullException>(() => { using (AxCryptReader axCryptReader = AxCryptReader.Create(null)) { } }, "A non-null input-stream must be specified."); using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); inputStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); } } }
public void TestFindPreambleHeaderBlockNotFirstShouldThrow() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); VersionHeaderBlock versionHeaderBlock = new VersionHeaderBlock(); versionHeaderBlock.Write(testStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(testStream); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.Throws <FileFormatException>(() => axCryptReader.Read()); } } }
public void TestFindPreambleHeaderBlockFirstButMoreThanOnceShouldThrow() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(testStream); preambleHeaderBlock.Write(testStream); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "We're expecting to have found a Preamble specifically"); Assert.Throws <FileFormatException>(() => axCryptReader.Read()); } } }
public static void TestFindVersionHeaderBlockFromSimpleFile() { using (Stream testStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { bool blockFound = false; int headers = 0; while (axCryptReader.Read()) { switch (axCryptReader.CurrentItemType) { case AxCryptItemType.None: break; case AxCryptItemType.MagicGuid: break; case AxCryptItemType.HeaderBlock: if (axCryptReader.CurrentHeaderBlock.HeaderBlockType == HeaderBlockType.Version) { Assert.That(blockFound, Is.False, "We should only find one single VersionHeaderBlock"); blockFound = true; } ++headers; break; case AxCryptItemType.Data: break; case AxCryptItemType.EndOfStream: break; default: break; } } Assert.That(blockFound, Is.True, "We're expecting a VersionHeaderBlock to be found!"); } } }
public void TestFindFindIdTag() { using (MemoryStream testStream = new MemoryStream()) { AxCrypt1Guid.Write(testStream); new PreambleHeaderBlock().Write(testStream); new IdTagHeaderBlock("A test").Write(testStream); testStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(testStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "This should be a header block"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "This should be an Preamble block"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "This should be a header block"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.IdTag), "This should be an IdTag block"); IdTagHeaderBlock idTagHeaderBlock = (IdTagHeaderBlock)axCryptReader.CurrentHeaderBlock; Assert.That(idTagHeaderBlock.IdTag, Is.EqualTo("A test"), "We're expecting to be able to read the same tag we wrote"); } } }
public static void TestUnrecognizedHeaderBlock() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock(); preambleHeaderBlock.Write(inputStream); UnrecognizedHeaderBlock unrecognizedHeaderBlock = new UnrecognizedHeaderBlock(HeaderBlockType.Unrecognized, new byte[0]); unrecognizedHeaderBlock.Write(inputStream); inputStream.Position = 0; using (AxCryptReader axCryptReader = AxCryptReader.Create(inputStream)) { Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the Guid"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.MagicGuid), "We're expecting to have found a MagicGuid"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Preamble), "We're expecting to have found a Preamble specifically"); Assert.That(axCryptReader.Read(), Is.True, "We should be able to read the next HeaderBlock"); Assert.That(axCryptReader.CurrentItemType, Is.EqualTo(AxCryptItemType.HeaderBlock), "We're expecting to have found a HeaderBlock"); Assert.That(axCryptReader.CurrentHeaderBlock.HeaderBlockType, Is.EqualTo(HeaderBlockType.Unrecognized), "We're expecting to have found an unrecognized block specifically"); } } }