Skip to content

Commit

Permalink
AesXtsFile: Don't take the input file until the end of the constructor
Browse files Browse the repository at this point in the history
An exception in the constructor would make it so the file wasn't disposed correctly
  • Loading branch information
Thealexbarney committed Dec 14, 2021
1 parent 2540f07 commit 9451553
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/LibHac/FsSystem/AesXtsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Mode = mode;
_baseFile = new UniqueRef<IFile>(ref baseFile);
Path = path;
KekSeed = kekSeed.ToArray();
VerificationKey = verificationKey.ToArray();
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))
{
Expand All @@ -43,11 +42,12 @@ public AesXtsFile(OpenMode mode, ref UniqueRef<IFile> baseFile, U8String path, R
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);
encStorage.SetResizable(true);

BaseStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Header.DecryptedKey1, Header.DecryptedKey2, BlockSize, true), 4, true);
_baseFile = new UniqueRef<IFile>(ref baseFile);
}

public byte[] GetKey()
Expand Down

0 comments on commit 9451553

Please sign in to comment.