/// <summary> /// Instantiate an instance of IAxCryptDocument appropriate for the file provided, i.e. V1 or V2. /// </summary> /// <param name="decryptionParameters">The possible decryption parameters to try.</param> /// <param name="inputStream">The input stream.</param> /// <returns></returns> public virtual IAxCryptDocument CreateDocument(IEnumerable <DecryptionParameter> decryptionParameters, Stream inputStream) { if (decryptionParameters == null) { throw new ArgumentNullException(nameof(decryptionParameters)); } Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(inputStream)); IAxCryptDocument document = AxCryptReaderBase.Document(reader); foreach (DecryptionParameter decryptionParameter in decryptionParameters) { if (decryptionParameter.Passphrase != null) { document.Load(decryptionParameter.Passphrase, decryptionParameter.CryptoId, headers); if (document.PassphraseIsValid) { document.DecryptionParameter = decryptionParameter; return(document); } } if (decryptionParameter.PrivateKey != null) { document.Load(decryptionParameter.PrivateKey, decryptionParameter.CryptoId, headers); if (document.PassphraseIsValid) { document.DecryptionParameter = decryptionParameter; return(document); } } } return(document); }
public void TestDecryptWithCancel() { IDataStore sourceFileInfo = New <IDataStore>(_helloWorldAxxPath); Passphrase passphrase = new Passphrase("a"); IProgressContext progress = new CancelProgressContext(new ProgressContext(new TimeSpan(0, 0, 0, 0, 100))); progress.Progressing += (object sender, ProgressEventArgs e) => { progress.Cancel = true; }; Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(new ProgressStream(sourceFileInfo.OpenRead(), progress))); using (IAxCryptDocument document = AxCryptReaderBase.Document(reader)) { bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, headers); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); IDataStore destinationInfo = New <IDataStore>(_rootPath.PathCombine("Destination", "Decrypted.txt")); FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; environment.CurrentTiming.CurrentTiming = new TimeSpan(0, 0, 0, 0, 100); using (FileLock destinationFileLock = New <FileLocker>().Acquire(destinationInfo)) { Assert.Throws <OperationCanceledException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, destinationFileLock, AxCryptOptions.None, progress); })); } } }
public bool Load(AxCryptReaderBase reader) { Headers headers = new Headers(); headers.Load(reader); return(Load(headers)); }
public void Trailers(AxCryptReaderBase reader) { if (reader == null) { throw new ArgumentNullException("reader"); } TrailerBlocks.Add(reader.CurrentHeaderBlock); ReadHeadersToLast(TrailerBlocks, reader, HeaderBlockType.V2Hmac); }
private static IList <HeaderBlock> LoadFromReader(AxCryptReaderBase vxReader) { List <HeaderBlock> headers = new List <HeaderBlock>(); vxReader.Read(); if (vxReader.CurrentItemType != AxCryptItemType.MagicGuid) { throw new FileFormatException("No magic Guid was found.", ErrorStatus.MagicGuidMissing); } ReadHeadersToLast(headers, vxReader, HeaderBlockType.Data); return(headers); }
public async Task TestEncryptFileWhenDestinationExists() { IDataStore sourceInfo = New <IDataStore>(_davidCopperfieldTxtPath); IDataStore expectedDestinationInfo = New <IDataStore>(AxCryptFile.MakeAxCryptFileName(sourceInfo)); using (Stream stream = expectedDestinationInfo.OpenWrite()) { } FileOperationsController controller = new FileOperationsController(); string destinationPath = String.Empty; LogOnIdentity logOnIdentity = null; controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) => { e.LogOnIdentity = new LogOnIdentity("allan"); }; controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { e.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "alternative-name.axx"); }; Guid cryptoId = Guid.Empty; controller.Completed += (object sender, FileOperationEventArgs e) => { destinationPath = e.SaveFileFullName; logOnIdentity = e.LogOnIdentity; cryptoId = e.CryptoId; }; FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath)); Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success."); Assert.That(Path.GetFileName(destinationPath), Is.EqualTo("alternative-name.axx"), "The alternative name should be used, since the default existed."); IDataStore destinationInfo = New <IDataStore>(destinationPath); Assert.That(destinationInfo.IsAvailable, "After encryption the destination file should be created."); EncryptionParameters encryptionParameters = new EncryptionParameters(cryptoId, logOnIdentity); await encryptionParameters.AddAsync(logOnIdentity.PublicKeys); Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(destinationInfo.OpenRead())); using (IAxCryptDocument document = AxCryptReaderBase.Document(reader)) { document.Load(logOnIdentity.Passphrase, cryptoId, headers); Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given."); } }
public void Trailers(AxCryptReaderBase axCryptReader) { _headers.Trailers(axCryptReader); using (Stream hmacStream = V2HmacStream.Create(HmacCalculator)) { foreach (HeaderBlock header in _headers.TrailerBlocks) { if (header.HeaderBlockType == HeaderBlockType.V2Hmac) { continue; } header.Write(hmacStream); } } }
private static void ReadHeadersToLast(IList <HeaderBlock> headerBlocks, AxCryptReaderBase axCryptReader, HeaderBlockType last) { while (axCryptReader.Read()) { switch (axCryptReader.CurrentItemType) { case AxCryptItemType.Data: case AxCryptItemType.HeaderBlock: break; default: throw new InternalErrorException("The reader returned an item type it should not be possible for it to return."); } headerBlocks.Add(axCryptReader.CurrentHeaderBlock); if (axCryptReader.CurrentHeaderBlock.HeaderBlockType == last) { return; } } throw new FileFormatException("Premature end of stream.", ErrorStatus.EndOfStream); }
/// <summary> /// Creates the specified data stream for reading from, ensuring proper disposal. /// </summary> /// <param name="reader">The reader.</param> /// <param name="chainedStream">The chained stream.</param> /// <returns></returns> public static V2AxCryptDataStream Create(AxCryptReaderBase reader, Stream chainedStream) { return(V2AxCryptDataStream <Stream> .Create((chained) => new V2AxCryptDataStream(reader, chainedStream), chainedStream)); }
private V2AxCryptDataStream(AxCryptReaderBase reader, Stream chainedStream) : base(reader, chainedStream) { }
public void Load(AxCryptReaderBase reader) { HeaderBlocks = LoadFromReader(reader); }