/// <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 TestInvalidItemType() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); new PreambleHeaderBlock().Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(inputStream)) { DocumentHeaders documentHeaders = new DocumentHeaders(new AesKey()); Assert.Throws <InternalErrorException>(() => { documentHeaders.Load(axCryptReader); }); } } }
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 static void TestHmac() { using (Stream inputStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = new AxCryptStreamReader(inputStream)) { Assert.Throws <InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "The reader is not positioned properly to get the HMAC."); Passphrase passphrase = new Passphrase("a"); DocumentHeaders documentHeaders = new DocumentHeaders(passphrase.DerivedPassphrase); bool keyIsOk = documentHeaders.Load(axCryptReader); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); using (Stream encrypedDataStream = axCryptReader.CreateEncryptedDataStream(documentHeaders.HmacSubkey.Key, documentHeaders.CipherTextLength, new ProgressContext())) { Assert.Throws <InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "We have not read the encrypted data yet."); Assert.That(axCryptReader.Read(), Is.False, "The reader should be at end of stream now, and Read() should return false."); encrypedDataStream.CopyTo(Stream.Null, 4096); Assert.That(documentHeaders.Hmac, Is.EqualTo(axCryptReader.Hmac), "The HMAC should be correct."); axCryptReader.Dispose(); Assert.Throws <ObjectDisposedException>(() => { DataHmac disposedHmac = axCryptReader.Hmac; Object.Equals(disposedHmac, null); }, "The reader is disposed."); } } } }
/// <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); }
/// <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 TestInvalidItemType() { using (MemoryStream inputStream = new MemoryStream()) { AxCrypt1Guid.Write(inputStream); new PreambleHeaderBlock().Write(inputStream); inputStream.Position = 0; using (AxCryptReaderForTest axCryptReader = new AxCryptReaderForTest(inputStream)) { DocumentHeaders documentHeaders = new DocumentHeaders(new AesKey()); Assert.Throws<InternalErrorException>(() => { documentHeaders.Load(axCryptReader); }); } } }
public static void TestHmac() { using (Stream inputStream = FakeRuntimeFileInfo.ExpandableMemoryStream(Resources.helloworld_key_a_txt)) { using (AxCryptReader axCryptReader = new AxCryptStreamReader(inputStream)) { Assert.Throws<InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "The reader is not positioned properly to get the HMAC."); Passphrase passphrase = new Passphrase("a"); DocumentHeaders documentHeaders = new DocumentHeaders(passphrase.DerivedPassphrase); bool keyIsOk = documentHeaders.Load(axCryptReader); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); using (Stream encrypedDataStream = axCryptReader.CreateEncryptedDataStream(documentHeaders.HmacSubkey.Key, documentHeaders.CipherTextLength, new ProgressContext())) { Assert.Throws<InvalidOperationException>(() => { if (axCryptReader.Hmac == null) { } }, "We have not read the encrypted data yet."); Assert.That(axCryptReader.Read(), Is.False, "The reader should be at end of stream now, and Read() should return false."); encrypedDataStream.CopyTo(Stream.Null, 4096); Assert.That(documentHeaders.Hmac, Is.EqualTo(axCryptReader.Hmac), "The HMAC should be correct."); axCryptReader.Dispose(); Assert.Throws<ObjectDisposedException>(() => { DataHmac disposedHmac = axCryptReader.Hmac; Object.Equals(disposedHmac, null); }, "The reader is disposed."); } } } }