AesXtsFile: Don't take the input file until the end of the constructor

An exception in the constructor would make it so the file wasn't disposed correctly
This commit is contained in:
Alex Barney 2021-12-13 17:02:02 -07:00
parent 2540f071ea
commit 9451553fde

View file

@ -23,15 +23,14 @@ public class AesXtsFile : IFile
public AesXtsFile(OpenMode mode, ref UniqueRef<IFile> baseFile, U8String path, ReadOnlySpan<byte> kekSeed, ReadOnlySpan<byte> verificationKey, int blockSize) public AesXtsFile(OpenMode mode, ref UniqueRef<IFile> baseFile, U8String path, ReadOnlySpan<byte> kekSeed, ReadOnlySpan<byte> verificationKey, int blockSize)
{ {
Mode = mode; Mode = mode;
_baseFile = new UniqueRef<IFile>(ref baseFile);
Path = path; Path = path;
KekSeed = kekSeed.ToArray(); KekSeed = kekSeed.ToArray();
VerificationKey = verificationKey.ToArray(); VerificationKey = verificationKey.ToArray();
BlockSize = blockSize; BlockSize = blockSize;
Header = new AesXtsFileHeader(_baseFile.Get); Header = new AesXtsFileHeader(baseFile.Get);
_baseFile.Get.GetSize(out long fileSize).ThrowIfFailure(); baseFile.Get.GetSize(out long fileSize).ThrowIfFailure();
if (!Header.TryDecryptHeader(Path.ToString(), KekSeed, VerificationKey)) if (!Header.TryDecryptHeader(Path.ToString(), KekSeed, VerificationKey))
{ {
@ -43,11 +42,12 @@ public class AesXtsFile : IFile
ThrowHelper.ThrowResult(ResultFs.AesXtsFileSystemFileSizeCorruptedOnFileOpen.Value, "NAX0 key derivation failed."); ThrowHelper.ThrowResult(ResultFs.AesXtsFileSystemFileSizeCorruptedOnFileOpen.Value, "NAX0 key derivation failed.");
} }
var fileStorage = new FileStorage(_baseFile.Get); var fileStorage = new FileStorage(baseFile.Get);
var encStorage = new SubStorage(fileStorage, HeaderLength, fileSize - HeaderLength); var encStorage = new SubStorage(fileStorage, HeaderLength, fileSize - HeaderLength);
encStorage.SetResizable(true); encStorage.SetResizable(true);
BaseStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Header.DecryptedKey1, Header.DecryptedKey2, BlockSize, true), 4, true); BaseStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Header.DecryptedKey1, Header.DecryptedKey2, BlockSize, true), 4, true);
_baseFile = new UniqueRef<IFile>(ref baseFile);
} }
public byte[] GetKey() public byte[] GetKey()