Example #1
0
        public bool Load(Passphrase key, Guid cryptoId, Stream inputStream)
        {
            Headers       headers = new Headers();
            AxCryptReader reader  = headers.CreateReader(new LookAheadStream(inputStream));

            return(Load(key, cryptoId, reader, headers));
        }
Example #2
0
 private void DisposeInternal()
 {
     if (_reader != null)
     {
         _reader.Dispose();
         _reader = null;
     }
 }
Example #3
0
        public AxCryptReader CreateReader(LookAheadStream inputStream)
        {
            IList <HeaderBlock> headers = LoadUnversionedHeaders(inputStream);
            AxCryptReader       reader  = CreateVersionedReader(inputStream, headers);

            reader.Reinterpret(headers, HeaderBlocks);

            return(reader);
        }
Example #4
0
 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");
         }
     }
 }
Example #5
0
        /// <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);
        }
Example #6
0
 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");
         }
     }
 }
Example #7
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 (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.");
         }
     }
 }
Example #8
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 (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 void TestReaderNotPositionedAtData()
        {
            MemoryStream  encryptedFile = new MemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt);
            Headers       headers       = new Headers();
            AxCryptReader reader        = headers.CreateReader(new LookAheadStream(encryptedFile));

            using (V1AxCryptDocument document = new V1AxCryptDocument(reader))
            {
                Passphrase key     = new Passphrase("Å ä Ö");
                bool       keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers);
                Assert.That(keyIsOk, Is.True);

                reader.SetStartOfData();
                Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null));
            }
        }
Example #10
0
        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 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.");
                }
            }
        }
Example #12
0
        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!");
                }
            }
        }
Example #13
0
 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 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");
                }
            }
        }
Example #15
0
 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());
         }
     }
 }
Example #16
0
        /// <summary>
        /// Loads an AxCrypt file from the specified reader. After this, the reader is positioned to
        /// read encrypted data.
        /// </summary>
        /// <param name="passphrase">The passphrase.</param>
        /// <param name="cryptoId">The crypto identifier.</param>
        /// <param name="reader">The reader.</param>
        /// <param name="headers">The headers.</param>
        /// <returns>
        /// True if the key was valid, false if it was wrong.
        /// </returns>
        private bool Load(Passphrase passphrase, Guid cryptoId, AxCryptReader reader, Headers headers)
        {
            ResetState();
            if (cryptoId == new V1Aes128CryptoFactory().CryptoId)
            {
                return(PassphraseIsValid);
            }

            _reader       = reader;
            CryptoFactory = Resolve.CryptoFactory.Create(cryptoId);
            V2KeyWrapHeaderBlock keyWrap = headers.FindHeaderBlock <V2KeyWrapHeaderBlock>();
            IDerivedKey          key     = CryptoFactory.RestoreDerivedKey(passphrase, keyWrap.DerivationSalt, keyWrap.DerivationIterations);

            keyWrap.SetDerivedKey(CryptoFactory, key);
            DocumentHeaders   = new V2DocumentHeaders(keyWrap);
            PassphraseIsValid = DocumentHeaders.Load(headers);
            Properties        = EncryptedProperties.Create(this);

            return(PassphraseIsValid);
        }
Example #17
0
        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!");
                }
            }
        }
        /// <summary>
        /// Loads an AxCrypt file from the specified reader. After this, the reader is positioned to
        /// read encrypted data.
        /// </summary>
        /// <param name="inputStream">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>
        private bool Load(Passphrase passphrase, AxCryptReader reader, Headers headers)
        {
            _reader = reader;
            ResetState(passphrase);
            PassphraseIsValid = DocumentHeaders.Load(headers);
            if (!PassphraseIsValid)
            {
                return(false);
            }

            _hmacStream = new V1HmacStream(DocumentHeaders.HmacSubkey.Key);
            foreach (HeaderBlock header in DocumentHeaders.Headers.HeaderBlocks)
            {
                if (header.HeaderBlockType != HeaderBlockType.Preamble)
                {
                    header.Write(_hmacStream);
                }
            }

            Properties = EncryptedProperties.Create(this);
            return(true);
        }
Example #19
0
 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");
         }
     }
 }
Example #20
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (_dataCrypto != null)
                {
                    _dataCrypto.Dispose();
                    _dataCrypto = null;
                }
                if (_reader != null)
                {
                    _reader.Dispose();
                    _reader = null;
                }
            }

            _disposed = true;
        }
        public void TestDecryptToWithReaderWronglyPositioned()
        {
            using (MemoryStream inputStream = new MemoryStream())
            {
                byte[] text = Resolve.RandomGenerator.Generate(1000);
                inputStream.Write(text, 0, text.Length);
                inputStream.Position = 0;
                byte[] buffer = new byte[2500];
                using (IAxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 113))
                {
                    document.EncryptTo(inputStream, new MemoryStream(buffer), AxCryptOptions.EncryptWithCompression);

                    Headers       headers = new Headers();
                    AxCryptReader reader  = headers.CreateReader(new LookAheadStream(new MemoryStream(buffer)));
                    using (V2AxCryptDocument decryptedDocument = new V2AxCryptDocument(reader))
                    {
                        Assert.That(decryptedDocument.Load(new Passphrase("passphrase"), new V2Aes256CryptoFactory().CryptoId, headers), Is.True);
                        reader.SetStartOfData();
                        Assert.Throws <InvalidOperationException>(() => decryptedDocument.DecryptTo(Stream.Null));
                    }
                }
            }
        }
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (_reader != null)
                {
                    _reader.Dispose();
                    _reader = null;
                }
                if (_hmacStream != null)
                {
                    _hmacStream.Dispose();
                    _hmacStream = null;
                }
            }

            _disposed = true;
        }
        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 void TestHmacThrowsWhenTooLittleData()
        {
            using (MemoryStream plaintext = new MemoryStream(Resources.uncompressable_zip))
            {
                MemoryStream encryptedFile = new MemoryStream();
                using (V1AxCryptDocument encryptingDocument = new V1AxCryptDocument(new Passphrase("a"), 10))
                {
                    encryptingDocument.EncryptTo(plaintext, encryptedFile, AxCryptOptions.EncryptWithoutCompression);
                }

                encryptedFile.Position = 0;
                Headers       headers = new Headers();
                AxCryptReader reader  = headers.CreateReader(new LookAheadStream(encryptedFile));
                using (V1AxCryptDocument document = new V1AxCryptDocument(reader))
                {
                    Passphrase key     = new Passphrase("a");
                    bool       keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers);
                    Assert.That(keyIsOk, Is.True);

                    reader.InputStream.Read(new byte[16], 0, 16);
                    Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null));
                }
            }
        }
Example #25
0
 public V2AxCryptDocument(AxCryptReader reader)
     : this()
 {
     _reader = reader;
 }