8#include "HAL/FileManager.h"
10#include "Misc/IEngineCrypto.h"
11#include "Misc/CoreDelegates.h"
12#include "Misc/Base64.h"
13#include "Serialization/Archive.h"
14#include "Serialization/JsonSerializer.h"
38 SetEncryptionKeys(Other.GetEncryptionKeys());
42 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
49 SetEncryptionKeys(MoveTemp(Other.GetEncryptionKeys()));
53 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
58 Other.SetEncryptionKeys(TMap<FGuid, FNamedAESKey>());
64 SetEncryptionKeys(Other.GetEncryptionKeys());
68 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
81 SetEncryptionKeys(MoveTemp(Other.GetEncryptionKeys()));
85 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
94 Other.SetEncryptionKeys(TMap<FGuid, FNamedAESKey>());
118 UE_DEPRECATED(5.1,
"Use Get/SetPrincipalEncryptionKey instead")
127 TSharedPtr<FJsonObject> PublicKey = InObj->GetObjectField(
TEXT(
"PublicKey"));
128 TSharedPtr<FJsonObject> PrivateKey = InObj->GetObjectField(
TEXT(
"PrivateKey"));
130 FString PublicExponentBase64, PrivateExponentBase64, PublicModulusBase64, PrivateModulusBase64;
132 if (PublicKey->TryGetStringField(
"Exponent", PublicExponentBase64)
133 && PublicKey->TryGetStringField(
"Modulus", PublicModulusBase64)
134 && PrivateKey->TryGetStringField(
"Exponent", PrivateExponentBase64)
135 && PrivateKey->TryGetStringField(
"Modulus", PrivateModulusBase64))
137 check(PublicModulusBase64 == PrivateModulusBase64);
139 TArray<uint8> PublicExponent, PrivateExponent, Modulus;
140 FBase64::Decode(PublicExponentBase64, PublicExponent);
141 FBase64::Decode(PrivateExponentBase64, PrivateExponent);
142 FBase64::Decode(PublicModulusBase64, Modulus);
144 return FRSA::CreateKey(PublicExponent, PrivateExponent, Modulus);
155 checkf(File !=
nullptr, TEXT(
"Specified crypto keys cache '%s' does not exist!"), *InFilename);
156 TSharedPtr<FJsonObject> RootObject;
157 TSharedRef<TJsonReader<UTF8CHAR>> Reader = TJsonReaderFactory<UTF8CHAR>::Create(File);
158 if (FJsonSerializer::Deserialize(Reader, RootObject))
160 const TSharedPtr<FJsonObject>* EncryptionKeyObject;
161 if (RootObject->TryGetObjectField(
TEXT(
"EncryptionKey"), EncryptionKeyObject))
164 if ((*EncryptionKeyObject)->TryGetStringField(
TEXT(
"Key"), EncryptionKeyBase64))
166 if (EncryptionKeyBase64
.Len() > 0)
169 FBase64::Decode(EncryptionKeyBase64, Key);
170 check(Key.Num() ==
sizeof(FAES::FAESKey::Key));
172 NewKey.Name =
TEXT(
"Default");
174 FMemory::Memcpy(NewKey.Key.Key, &Key[0],
sizeof(FAES::FAESKey::Key));
175 OutCryptoSettings.GetEncryptionKeys().Add(NewKey
.Guid, NewKey);
180 const TSharedPtr<FJsonObject>* SigningKey =
nullptr;
181 if (RootObject->TryGetObjectField(
TEXT(
"SigningKey"), SigningKey))
183 OutCryptoSettings.SetSigningKey(ParseRSAKeyFromJson(*SigningKey));
186 const TArray<TSharedPtr<FJsonValue>>* SecondaryEncryptionKeyArray =
nullptr;
187 if (RootObject->TryGetArrayField(
TEXT(
"SecondaryEncryptionKeys"), SecondaryEncryptionKeyArray))
189 for (TSharedPtr<FJsonValue> EncryptionKeyValue : *SecondaryEncryptionKeyArray)
192 TSharedPtr<FJsonObject> SecondaryEncryptionKeyObject = EncryptionKeyValue->AsObject();
193 FGuid::Parse(SecondaryEncryptionKeyObject->GetStringField(
TEXT(
"Guid")), NewKey.Guid);
194 NewKey.Name = SecondaryEncryptionKeyObject->GetStringField(
TEXT(
"Name"));
195 FString KeyBase64 = SecondaryEncryptionKeyObject->GetStringField(
TEXT(
"Key"));
198 FBase64::Decode(KeyBase64, Key);
199 check(Key.Num() ==
sizeof(FAES::FAESKey::Key));
200 FMemory::Memcpy(NewKey.Key.Key, &Key[0],
sizeof(FAES::FAESKey::Key));
202 check(!OutCryptoSettings.GetEncryptionKeys().Contains(NewKey.Guid) || OutCryptoSettings.GetEncryptionKeys()[NewKey.Guid].Key == NewKey.Key);
203 OutCryptoSettings.GetEncryptionKeys().Add(NewKey.Guid, NewKey);
208 FGuid EncryptionKeyOverrideGuid;
214 if (KeyChain.GetEncryptionKeys().Contains(
FGuid()))
217 FCoreDelegates::GetPakEncryptionKeyDelegate().BindLambda([DefaultKey](uint8 OutKey[32]) { FMemory::Memcpy(OutKey, DefaultKey.Key,
sizeof(DefaultKey.Key)); });
220 for (
const TMap<FGuid, FNamedAESKey>::ElementType& Key : KeyChain.GetEncryptionKeys())
222 if (Key.Key.IsValid())
224 FCoreDelegates::GetRegisterEncryptionKeyMulticastDelegate().Broadcast(Key.Key, Key.Value.Key);
#define checkf(expr, format,...)
#define UE_DEPRECATED(Version, Message)
static constexpr FRSAKeyHandle InvalidRSAKeyHandle
UE_NODISCARD FORCEINLINE int32 Len() const
UE_NODISCARD FORCEINLINE const TCHAR * operator*() const UE_LIFETIMEBOUND
virtual FArchive * CreateFileReader(const TCHAR *Filename, uint32 ReadFlags=0)=0
static IFileManager & Get()
static FRSAKeyHandle ParseRSAKeyFromJson(TSharedPtr< FJsonObject > InObj)
static void ApplyEncryptionKeys(const FKeyChain &KeyChain)
static void LoadKeyChainFromFile(const FString &InFilename, FKeyChain &OutCryptoSettings)
TMap< FGuid, FNamedAESKey > & GetEncryptionKeys()
const TMap< FGuid, FNamedAESKey > & GetEncryptionKeys() const
FKeyChain & operator=(const FKeyChain &Other)
FKeyChain(const FKeyChain &Other)
const FNamedAESKey * GetPrincipalEncryptionKey() const
PRAGMA_DISABLE_DEPRECATION_WARNINGS FRSAKeyHandle GetSigningKey() const
const FNamedAESKey * MasterEncryptionKey
void SetEncryptionKeys(const TMap< FGuid, FNamedAESKey > &keys)
TMap< FGuid, FNamedAESKey > EncryptionKeys
FKeyChain(FKeyChain &&Other)
PRAGMA_ENABLE_DEPRECATION_WARNINGS FRSAKeyHandle SigningKey
void SetSigningKey(FRSAKeyHandle key)
void SetPrincipalEncryptionKey(const FNamedAESKey *key)
FKeyChain & operator=(FKeyChain &&Other)