예제 #1
0
        private void EnsureFileFormatVersion()
        {
            VersionHeaderBlock versionHeaderBlock = FindHeaderBlock <VersionHeaderBlock>();

            if (versionHeaderBlock.FileVersionMajor > 3)
            {
                throw new FileFormatException("Too new file format.", ErrorStatus.TooNewFileFormatVersion);
            }
        }
        public static void TestVersionValues()
        {
            VersionHeaderBlock versionHeaderBlock = new VersionHeaderBlock();

            Assert.That(versionHeaderBlock.FileVersionMajor, Is.EqualTo(3), "This is the current default version number");
            Assert.That(versionHeaderBlock.FileVersionMinor, Is.EqualTo(2), "This is the current default version number");
            Assert.That(versionHeaderBlock.VersionMajor, Is.EqualTo(2), "This is the current default version number");
            Assert.That(versionHeaderBlock.VersionMinor, Is.EqualTo(0), "This is the current default version number");
            Assert.That(versionHeaderBlock.VersionMinuscule, Is.EqualTo(0), "This is the current default version number");
        }
예제 #3
0
        private AesKey GetMasterKey()
        {
            KeyWrap1HeaderBlock keyHeaderBlock     = FindHeaderBlock <KeyWrap1HeaderBlock>();
            VersionHeaderBlock  versionHeaderBlock = FindHeaderBlock <VersionHeaderBlock>();

            byte[] unwrappedKeyData = keyHeaderBlock.UnwrapMasterKey(_keyEncryptingKey, versionHeaderBlock.FileVersionMajor);
            if (unwrappedKeyData.Length == 0)
            {
                return(null);
            }
            return(new AesKey(unwrappedKeyData));
        }
예제 #4
0
        public override object Clone()
        {
            VersionHeaderBlock block = new VersionHeaderBlock((byte[])GetDataBlockBytesReference().Clone());

            return(block);
        }
예제 #5
0
 public override object Clone()
 {
     VersionHeaderBlock block = new VersionHeaderBlock((byte[])GetDataBlockBytesReference().Clone());
     return block;
 }
예제 #6
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;
        }
예제 #7
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;
        }
 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());
         }
     }
 }
예제 #9
0
        public void SetCurrentVersion()
        {
            VersionHeaderBlock versionHeaderBlock = FindHeaderBlock <VersionHeaderBlock>();

            versionHeaderBlock.SetCurrentVersion();
        }