You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
- CryptoKeys plugin can now be told how many bits to use for keys. Defaults to 2048 as a happy medium between security and performance - After creating the OpenSSL RSA key, use the exp/mod values directly rather than recomputing from primes - Added RSA.h with some TBigInt based helpers for RSA keys - Added FPakSignatureFile to wrap the contents of the pak .sig file, including determining that it is of a new format that supports versioning. Wraps the encryption and decryption of he master table hash - Changed pak master signature hash from crc to SHA1 - Modified access to executable embedded public key data so that it is just an array of bytes. Will allow custom key sizes in the future. #rb joe.barrett, stefan.boberg #jira UE-71280, UE-71377 #ROBOMERGE-OWNER: graeme.thornton #ROBOMERGE-AUTHOR: graeme.thornton #ROBOMERGE-SOURCE: CL 5381335 in //UE4/Release-4.22/... #ROBOMERGE-BOT: RELEASE (Release-4.22 -> Main) [CL 5383057 by graeme thornton in Main branch]
107 lines
2.7 KiB
C++
107 lines
2.7 KiB
C++
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "SignedArchiveWriter.h"
|
|
#include "IPlatformFilePak.h"
|
|
#include "Misc/SecureHash.h"
|
|
#include "HAL/FileManager.h"
|
|
|
|
FSignedArchiveWriter::FSignedArchiveWriter(FArchive& InPak, const FString& InPakFilename, const TPakRSAKey& InPublicKey, const TPakRSAKey& InPrivateKey)
|
|
: BufferArchive(Buffer)
|
|
, PakWriter(InPak)
|
|
, PakSignaturesFilename(FPaths::ChangeExtension(InPakFilename, TEXT("sig")))
|
|
, SizeOnDisk(0)
|
|
, PakSize(0)
|
|
, PublicKey(InPublicKey)
|
|
, PrivateKey(InPrivateKey)
|
|
{
|
|
Buffer.Reserve(FPakInfo::MaxChunkDataSize);
|
|
}
|
|
|
|
FSignedArchiveWriter::~FSignedArchiveWriter()
|
|
{
|
|
if (BufferArchive.Tell() > 0)
|
|
{
|
|
SerializeBufferAndSign();
|
|
}
|
|
delete &PakWriter;
|
|
}
|
|
|
|
void FSignedArchiveWriter::SerializeBufferAndSign()
|
|
{
|
|
// Compute a hash for this buffer data
|
|
ChunkHashes.Add(ComputePakChunkHash(&Buffer[0], Buffer.Num()));
|
|
|
|
// Flush the buffer
|
|
PakWriter.Serialize(&Buffer[0], Buffer.Num());
|
|
BufferArchive.Seek(0);
|
|
Buffer.Empty(FPakInfo::MaxChunkDataSize);
|
|
}
|
|
|
|
bool FSignedArchiveWriter::Close()
|
|
{
|
|
if (BufferArchive.Tell() > 0)
|
|
{
|
|
SerializeBufferAndSign();
|
|
}
|
|
|
|
FArchive* SignatureWriter = IFileManager::Get().CreateFileWriter(*PakSignaturesFilename);
|
|
FPakSignatureFile SignatureFile;
|
|
SignatureFile.Initialize(ChunkHashes, PrivateKey);
|
|
SignatureFile.Serialize(*SignatureWriter);
|
|
delete SignatureWriter;
|
|
|
|
// Test reading back in
|
|
{
|
|
FArchive* SignatureReader = IFileManager::Get().CreateFileReader(*PakSignaturesFilename);
|
|
FPakSignatureFile SigFileTest;
|
|
SigFileTest.Serialize(*SignatureReader);
|
|
UE_CLOG(!SigFileTest.DecryptSignatureAndValidate(PublicKey, FString()), LogPakFile, Fatal, TEXT("Failed to validate RSA encrypted pak hash"));
|
|
delete SignatureReader;
|
|
}
|
|
|
|
return FArchive::Close();
|
|
}
|
|
|
|
void FSignedArchiveWriter::Serialize(void* Data, int64 Length)
|
|
{
|
|
// Serialize data to a buffer. When the max buffer size is reached, the buffer is signed and
|
|
// serialized to disk with its signature
|
|
uint8* DataToWrite = (uint8*)Data;
|
|
int64 RemainingSize = Length;
|
|
while (RemainingSize > 0)
|
|
{
|
|
int64 BufferPos = BufferArchive.Tell();
|
|
int64 SizeToWrite = RemainingSize;
|
|
if (BufferPos + SizeToWrite > FPakInfo::MaxChunkDataSize)
|
|
{
|
|
SizeToWrite = FPakInfo::MaxChunkDataSize - BufferPos;
|
|
}
|
|
|
|
BufferArchive.Serialize(DataToWrite, SizeToWrite);
|
|
if (BufferArchive.Tell() == FPakInfo::MaxChunkDataSize)
|
|
{
|
|
SerializeBufferAndSign();
|
|
}
|
|
|
|
SizeOnDisk += SizeToWrite;
|
|
PakSize += SizeToWrite;
|
|
|
|
RemainingSize -= SizeToWrite;
|
|
DataToWrite += SizeToWrite;
|
|
}
|
|
}
|
|
|
|
int64 FSignedArchiveWriter::Tell()
|
|
{
|
|
return PakSize;
|
|
}
|
|
|
|
int64 FSignedArchiveWriter::TotalSize()
|
|
{
|
|
return PakSize;
|
|
}
|
|
|
|
void FSignedArchiveWriter::Seek(int64 InPos)
|
|
{
|
|
UE_LOG(LogPakFile, Fatal, TEXT("Seek is not supported in FSignedArchiveWriter."));
|
|
} |