public static void TestBadArguments()
        {
            PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock();

            Assert.Throws<ArgumentNullException>(() =>
            {
                preambleHeaderBlock.Hmac = null;
            });
        }
Esempio n. 2
0
        private void WriteInternal(Stream cipherStream, Stream hmacStream)
        {
            cipherStream.Position = 0;
            AxCrypt1Guid.Write(cipherStream);
            PreambleHeaderBlock preambleHaderBlock = FindHeaderBlock <PreambleHeaderBlock>();

            preambleHaderBlock.Write(cipherStream);
            foreach (HeaderBlock headerBlock in _headerBlocks)
            {
                if (headerBlock is DataHeaderBlock)
                {
                    continue;
                }
                if (headerBlock is PreambleHeaderBlock)
                {
                    continue;
                }
                headerBlock.Write(hmacStream);
            }
            DataHeaderBlock dataHeaderBlock = FindHeaderBlock <DataHeaderBlock>();

            dataHeaderBlock.Write(hmacStream);
        }
 public override object Clone()
 {
     PreambleHeaderBlock block = new PreambleHeaderBlock((byte[])GetDataBlockBytesReference().Clone());
     return block;
 }
Esempio n. 4
0
        private void ParseHeaderBlock(HeaderBlockType headerBlockType, byte[] dataBlock)
        {
            bool isFirst = CurrentItemType == AxCryptItemType.MagicGuid;

            CurrentItemType = AxCryptItemType.HeaderBlock;

            if (headerBlockType == HeaderBlockType.Preamble)
            {
                if (!isFirst)
                {
                    throw new FileFormatException("Preamble can only be first.", ErrorStatus.FileFormatError);
                }
                CurrentHeaderBlock    = new PreambleHeaderBlock(dataBlock);
                _sendDataToHmacStream = true;
                return;
            }
            else
            {
                if (isFirst)
                {
                    throw new FileFormatException("Preamble must be first.", ErrorStatus.FileFormatError);
                }
            }

            switch (headerBlockType)
            {
            case HeaderBlockType.Version:
                CurrentHeaderBlock = new VersionHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.KeyWrap1:
                CurrentHeaderBlock = new KeyWrap1HeaderBlock(dataBlock);
                break;

            case HeaderBlockType.KeyWrap2:
                CurrentHeaderBlock = new KeyWrap2HeaderBlock(dataBlock);
                break;

            case HeaderBlockType.IdTag:
                CurrentHeaderBlock = new IdTagHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.Data:
                CurrentHeaderBlock = new DataHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.FileNameInfo:
                CurrentHeaderBlock = new FileNameInfoHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.EncryptionInfo:
                CurrentHeaderBlock = new EncryptionInfoHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.CompressionInfo:
                CurrentHeaderBlock = new CompressionInfoHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.FileInfo:
                CurrentHeaderBlock = new FileInfoHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.Compression:
                CurrentHeaderBlock = new CompressionHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.UnicodeFileNameInfo:
                CurrentHeaderBlock = new UnicodeFileNameInfoHeaderBlock(dataBlock);
                break;

            case HeaderBlockType.Encrypted:
            case HeaderBlockType.None:
            case HeaderBlockType.Any:
                throw new FileFormatException("Illegal header block type.", ErrorStatus.FileFormatError);

            default:
                CurrentHeaderBlock = new UnrecognizedHeaderBlock(headerBlockType, dataBlock);
                break;
            }

            if (_sendDataToHmacStream)
            {
                CurrentHeaderBlock.Write(_hmacBufferStream);
            }

            return;
        }
Esempio n. 5
0
        private void ParseHeaderBlock(HeaderBlockType headerBlockType, byte[] dataBlock)
        {
            bool isFirst = CurrentItemType == AxCryptItemType.MagicGuid;
            CurrentItemType = AxCryptItemType.HeaderBlock;

            if (headerBlockType == HeaderBlockType.Preamble)
            {
                if (!isFirst)
                {
                    throw new FileFormatException("Preamble can only be first.", ErrorStatus.FileFormatError);
                }
                CurrentHeaderBlock = new PreambleHeaderBlock(dataBlock);
                _sendDataToHmacStream = true;
                return;
            }
            else
            {
                if (isFirst)
                {
                    throw new FileFormatException("Preamble must be first.", ErrorStatus.FileFormatError);
                }
            }

            switch (headerBlockType)
            {
                case HeaderBlockType.Version:
                    CurrentHeaderBlock = new VersionHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.KeyWrap1:
                    CurrentHeaderBlock = new KeyWrap1HeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.KeyWrap2:
                    CurrentHeaderBlock = new KeyWrap2HeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.IdTag:
                    CurrentHeaderBlock = new IdTagHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.Data:
                    CurrentHeaderBlock = new DataHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.FileNameInfo:
                    CurrentHeaderBlock = new FileNameInfoHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.EncryptionInfo:
                    CurrentHeaderBlock = new EncryptionInfoHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.CompressionInfo:
                    CurrentHeaderBlock = new CompressionInfoHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.FileInfo:
                    CurrentHeaderBlock = new FileInfoHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.Compression:
                    CurrentHeaderBlock = new CompressionHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.UnicodeFileNameInfo:
                    CurrentHeaderBlock = new UnicodeFileNameInfoHeaderBlock(dataBlock);
                    break;

                case HeaderBlockType.Encrypted:
                case HeaderBlockType.None:
                case HeaderBlockType.Any:
                    throw new FileFormatException("Illegal header block type.", ErrorStatus.FileFormatError);
                default:
                    CurrentHeaderBlock = new UnrecognizedHeaderBlock(headerBlockType, dataBlock);
                    break;
            }

            if (_sendDataToHmacStream)
            {
                CurrentHeaderBlock.Write(_hmacBufferStream);
            }

            return;
        }
Esempio n. 6
0
        public override object Clone()
        {
            PreambleHeaderBlock block = new PreambleHeaderBlock((byte[])GetDataBlockBytesReference().Clone());

            return(block);
        }
 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 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");
                }
            }
        }
        public static void TestInvalidHeaderBlockType()
        {
            using (MemoryStream inputStream = new MemoryStream())
            {
                AxCrypt1Guid.Write(inputStream);
                PreambleHeaderBlock preambleHeaderBlock = new PreambleHeaderBlock();
                preambleHeaderBlock.Write(inputStream);

                BadHeaderBlock badHeaderBlock = new BadHeaderBlock();
                badHeaderBlock.FakeHeaderBlockLength = 5;
                badHeaderBlock.SetHeaderBlockType(HeaderBlockType.Encrypted);
                badHeaderBlock.Write(inputStream);
                inputStream.Position = 0;

                using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(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.Throws<FileFormatException>(() =>
                    {
                        axCryptReader.Read();
                    });
                }
            }
        }