Пример #1
0
        public static void TestConstructor()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(null)) { }
            }, "A non-null input-stream must be specified.");

            using (MemoryStream inputStream = new MemoryStream())
            {
                AxCrypt1Guid.Write(inputStream);
                inputStream.Position = 0;
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
                }
            }

            using (MemoryStream inputStream = new MemoryStream())
            {
                AxCrypt1Guid.Write(inputStream);
                inputStream.Position = 0;

                // The stream reader supports both externally supplied LookAheadStream or will wrap it if it is not.
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
                }
            }
        }
Пример #2
0
        public static void TestPrematureEndOfFile(CryptoImplementation cryptoImplementation)
        {
            SetupAssembly.AssemblySetupCrypto(cryptoImplementation);

            V1DocumentHeaders headers = new V1DocumentHeaders(new Passphrase("passphrase"), 10);

            using (MemoryStream stream = new MemoryStream())
            {
                headers.WriteWithoutHmac(stream);
                stream.Position = 0;

                using (V1AxCryptReader reader = new V1AxCryptReader(new LookAheadStream(stream)))
                {
                    AxCryptItemType lastItemType = AxCryptItemType.Undefined;
                    while (reader.Read())
                    {
                        lastItemType = reader.CurrentItemType;
                    }
                    Assert.That(lastItemType, Is.EqualTo(AxCryptItemType.Data));
                    Assert.That(reader.CurrentItemType, Is.EqualTo(AxCryptItemType.Data));

                    reader.SetStartOfData();
                    Assert.That(reader.Read(), Is.False);
                    Assert.That(reader.CurrentItemType, Is.EqualTo(AxCryptItemType.EndOfStream));

                    Assert.That(reader.Read(), Is.False);
                    Assert.That(reader.CurrentItemType, Is.EqualTo(AxCryptItemType.EndOfStream));
                }
            }
        }
Пример #3
0
 public static void TestFindMagicGuidFromSimpleFile()
 {
     using (Stream testStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
     {
         using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
         }
     }
 }
Пример #4
0
 public static void TestFindMagicGuidFirstAndOnly()
 {
     using (MemoryStream testStream = new MemoryStream())
     {
         AxCrypt1Guid.Write(testStream);
         testStream.Position = 0;
         using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
         }
     }
 }
Пример #5
0
 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 (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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.");
         }
     }
 }
Пример #6
0
 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 (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
         }
     }
 }
Пример #7
0
        public static void TestObjectDisposed()
        {
            using (Stream inputStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
            {
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(inputStream)))
                {
                    axCryptReader.Dispose();

                    Assert.Throws <ObjectDisposedException>(() =>
                    {
                        bool isOk = axCryptReader.Read();
                        Object.Equals(isOk, null);
                    }, "The reader is disposed.");
                }
            }
        }
        public void TestBadKey()
        {
            using (Stream testStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
            {
                using (V1AxCryptReader reader = new V1AxCryptReader(new LookAheadStream(testStream)))
                {
                    Passphrase        passphrase      = new Passphrase("b");
                    V1DocumentHeaders documentHeaders = new V1DocumentHeaders(passphrase, 73);
                    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 static void TestFindPreambleHeaderBlockFromSimpleFile()
        {
            using (Stream testStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
            {
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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(V1Hmac.RequiredLength), "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 void TestFindPreambleHeaderBlockNotFirstShouldThrow()
 {
     using (MemoryStream testStream = new MemoryStream())
     {
         AxCrypt1Guid.Write(testStream);
         VersionHeaderBlock versionHeaderBlock = new VersionHeaderBlock(new byte[] { 3, 2, 2, 0, 0 });
         versionHeaderBlock.Write(testStream);
         PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock();
         preambleHeaderBlock.Write(testStream);
         testStream.Position = 0;
         using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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());
         }
     }
 }
Пример #11
0
        public void TestUnwrapFromSimpleFile()
        {
            using (Stream testStream = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
            {
                V1KeyWrap1HeaderBlock keyWrapHeaderBlock = null;
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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 = (V1KeyWrap1HeaderBlock)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();
                    KeyWrap keyWrap   = new KeyWrap(keyWrapHeaderBlock.Salt, keyWrapHeaderBlock.KeyWrapIterations, KeyWrapMode.AxCrypt);
                    byte[]  unwrapped = keyWrap.Unwrap(new V1AesCrypto(new V1Aes128CryptoFactory(), new V1DerivedKey(new Passphrase("a")).DerivedKey, SymmetricIV.Zero128), wrapped);
                    Assert.That(unwrapped.Length, Is.Not.EqualTo(0), "An unwrapped key is invalid if it is returned as a zero-length array.");
                }
            }
        }
 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 (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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 = FakeDataStore.ExpandableMemoryStream(Resources.helloworld_key_a_txt))
            {
                using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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!");
                }
            }
        }
Пример #14
0
        private static AxCryptReader CreateVersionedReader(LookAheadStream inputStream, IList <HeaderBlock> headers)
        {
            AxCryptReader      reader;
            VersionHeaderBlock versionHeaderBlock = FindHeaderBlock <VersionHeaderBlock>(headers);

            switch (versionHeaderBlock.FileVersionMajor)
            {
            case 1:
            case 2:
            case 3:
                reader = new V1AxCryptReader(inputStream);
                break;

            case 4:
                reader = new V2AxCryptReader(inputStream);
                break;

            default:
                throw new FileFormatException("Too new file format. You need a more recent version.");
            }
            return(reader);
        }
Пример #15
0
 public void TestFindFindIdTag()
 {
     using (MemoryStream testStream = new MemoryStream())
     {
         AxCrypt1Guid.Write(testStream);
         new PreambleHeaderBlock().Write(testStream);
         new V1IdTagHeaderBlock("A test").Write(testStream);
         testStream.Position = 0;
         using (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
             V1IdTagHeaderBlock idTagHeaderBlock = (V1IdTagHeaderBlock)axCryptReader.CurrentHeaderBlock;
             Assert.That(idTagHeaderBlock.IdTag, Is.EqualTo("A test"), "We're expecting to be able to read the same tag we wrote");
         }
     }
 }
Пример #16
0
        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 (V1AxCryptReader axCryptReader = new V1AxCryptReader(new LookAheadStream(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");
                }
            }
        }