UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
KeyChainUtilities.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4// HEADER_UNIT_UNSUPPORTED - Includes JsonSerializer.h which is not in Core module
5
6#include "Base64.h"
7#include "CoreDelegates.h"
8#include "CoreMinimal.h"
9#include "HAL/FileManager.h"
10#include "IEngineCrypto.h"
11#include "NamedAESKey.h"
12#include "RSA.h"
15
17{
18public:
19
20 FKeyChain() = default;
21
23 {
24 SetSigningKey(Other.GetSigningKey());
25 SetEncryptionKeys(Other.GetEncryptionKeys());
26
27 if (Other.GetPrincipalEncryptionKey())
28 {
29 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
30 }
31 }
32
34 {
35 SetSigningKey(Other.GetSigningKey());
36 SetEncryptionKeys(MoveTemp(Other.GetEncryptionKeys()));
37
38 if (Other.GetPrincipalEncryptionKey())
39 {
40 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
41 }
42
43 Other.SetSigningKey(InvalidRSAKeyHandle);
44 Other.SetPrincipalEncryptionKey(nullptr);
45 Other.SetEncryptionKeys(TMap<FGuid, FNamedAESKey>());
46 }
47
49 {
50 SetSigningKey(Other.GetSigningKey());
51 SetEncryptionKeys(Other.GetEncryptionKeys());
52
53 if (Other.GetPrincipalEncryptionKey())
54 {
55 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
56 }
57 else
58 {
60 }
61
62 return *this;
63 }
64
66 {
67 SetSigningKey(Other.GetSigningKey());
68 SetEncryptionKeys(MoveTemp(Other.GetEncryptionKeys()));
69
70 if (Other.GetPrincipalEncryptionKey())
71 {
72 SetPrincipalEncryptionKey(GetEncryptionKeys().Find(Other.GetPrincipalEncryptionKey()->Guid));
73 }
74 else
75 {
77 }
78
79 Other.SetSigningKey(InvalidRSAKeyHandle);
80 Other.SetPrincipalEncryptionKey(nullptr);
81 Other.SetEncryptionKeys(TMap<FGuid, FNamedAESKey>());
82
83 return *this;
84 }
85
89
92
95
98
99 UE_DEPRECATED(5.1, "Use Get/SetSigningKey instead")
101
104
107};
108
109
111{
112 static FRSAKeyHandle ParseRSAKeyFromJson(TSharedPtr<FJsonObject> InObj)
113 {
114 TSharedPtr<FJsonObject> PublicKey = InObj->GetObjectField(TEXT("PublicKey"));
115 TSharedPtr<FJsonObject> PrivateKey = InObj->GetObjectField(TEXT("PrivateKey"));
116
118
119 if (PublicKey->TryGetStringField(TEXT("Exponent"), PublicExponentBase64)
120 && PublicKey->TryGetStringField(TEXT("Modulus"), PublicModulusBase64)
121 && PrivateKey->TryGetStringField(TEXT("Exponent"), PrivateExponentBase64)
122 && PrivateKey->TryGetStringField(TEXT("Modulus"), PrivateModulusBase64))
123 {
125
130
132 }
133 else
134 {
135 return nullptr;
136 }
137 }
138
139 static void LoadKeyChainFromFile(const FString& InFilename, FKeyChain& OutCryptoSettings)
140 {
142 checkf(File != nullptr, TEXT("Specified crypto keys cache '%s' does not exist!"), *InFilename);
143 TSharedPtr<FJsonObject> RootObject;
145 if (FJsonSerializer::Deserialize(Reader, RootObject))
146 {
148 if (RootObject->TryGetObjectField(TEXT("EncryptionKey"), EncryptionKeyObject))
149 {
150 FString EncryptionKeyBase64;
151 if ((*EncryptionKeyObject)->TryGetStringField(TEXT("Key"), EncryptionKeyBase64))
152 {
153 if (EncryptionKeyBase64.Len() > 0)
154 {
155 TArray<uint8> Key;
157 check(Key.Num() == sizeof(FAES::FAESKey::Key));
158 FNamedAESKey NewKey;
159 NewKey.Name = TEXT("Default");
160 NewKey.Guid = FGuid();
161 FMemory::Memcpy(NewKey.Key.Key, &Key[0], sizeof(FAES::FAESKey::Key));
162 OutCryptoSettings.GetEncryptionKeys().Add(NewKey.Guid, NewKey);
163 }
164 }
165 }
166
167 const TSharedPtr<FJsonObject>* SigningKey = nullptr;
168 if (RootObject->TryGetObjectField(TEXT("SigningKey"), SigningKey))
169 {
170 OutCryptoSettings.SetSigningKey(ParseRSAKeyFromJson(*SigningKey));
171 }
172
174 if (RootObject->TryGetArrayField(TEXT("SecondaryEncryptionKeys"), SecondaryEncryptionKeyArray))
175 {
177 {
178 FNamedAESKey NewKey;
180 FGuid::Parse(SecondaryEncryptionKeyObject->GetStringField(TEXT("Guid")), NewKey.Guid);
181 NewKey.Name = SecondaryEncryptionKeyObject->GetStringField(TEXT("Name"));
182 FString KeyBase64 = SecondaryEncryptionKeyObject->GetStringField(TEXT("Key"));
183
184 TArray<uint8> Key;
186 check(Key.Num() == sizeof(FAES::FAESKey::Key));
187 FMemory::Memcpy(NewKey.Key.Key, &Key[0], sizeof(FAES::FAESKey::Key));
188
189 check(!OutCryptoSettings.GetEncryptionKeys().Contains(NewKey.Guid) || OutCryptoSettings.GetEncryptionKeys()[NewKey.Guid].Key == NewKey.Key);
190 OutCryptoSettings.GetEncryptionKeys().Add(NewKey.Guid, NewKey);
191 }
192 }
193 }
194 delete File;
196 OutCryptoSettings.SetPrincipalEncryptionKey(OutCryptoSettings.GetEncryptionKeys().Find(EncryptionKeyOverrideGuid));
197 }
198
199 static void ApplyEncryptionKeys(const FKeyChain& KeyChain)
200 {
201 if (KeyChain.GetEncryptionKeys().Contains(FGuid()))
202 {
203 FAES::FAESKey DefaultKey = KeyChain.GetEncryptionKeys()[FGuid()].Key;
204 FCoreDelegates::GetPakEncryptionKeyDelegate().BindLambda([DefaultKey](uint8 OutKey[32]) { FMemory::Memcpy(OutKey, DefaultKey.Key, sizeof(DefaultKey.Key)); });
205 }
206
207 for (const TMap<FGuid, FNamedAESKey>::ElementType& Key : KeyChain.GetEncryptionKeys())
208 {
209 if (Key.Key.IsValid())
210 {
211 FCoreDelegates::GetRegisterEncryptionKeyMulticastDelegate().Broadcast(Key.Key, Key.Value.Key);
212 }
213 }
214 }
215}
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
constexpr FRSAKeyHandle InvalidRSAKeyHandle
Definition IEngineCrypto.h:10
void * FRSAKeyHandle
Definition IEngineCrypto.h:9
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Archive.h:1208
static CORE_API TDelegate< void(uint8[32])> & GetPakEncryptionKeyDelegate()
Definition CoreDelegates.cpp:100
static CORE_API TMulticastDelegate< void(const FGuid &, const FAES::FAESKey &)> & GetRegisterEncryptionKeyMulticastDelegate()
Definition CoreDelegates.cpp:94
Definition UnrealType.h:3087
static CORE_API IFileManager & Get()
Definition FileManagerGeneric.cpp:1072
virtual FArchive * CreateFileReader(const TCHAR *Filename, uint32 ReadFlags=0)=0
Definition Array.h:670
static TSharedRef< TJsonReader< TElementType_T< StringType > > > Create(StringType &&JsonString)
Definition JsonReader.h:1070
static bool Deserialize(const TSharedRef< TJsonReader< CharType > > &Reader, typename Policy::FValue &OutValue, EFlags InOptions=EFlags::None)
Definition JsonSerializer.h:283
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition SharedPointer.h:153
Definition KeyChainUtilities.h:111
Definition AES.h:27
uint8 Key[KeySize]
Definition AES.h:30
static CORE_API bool Decode(const FString &Source, FString &OutDest, EBase64Mode Mode=EBase64Mode::Standard)
Definition Base64.cpp:166
Definition Guid.h:109
static CORE_API bool Parse(const TCHAR *GuidString, FGuid &OutGuid)
Definition Guid.cpp:307
Definition KeyChainUtilities.h:17
FKeyChain()=default
TMap< FGuid, FNamedAESKey > & GetEncryptionKeys()
Definition KeyChainUtilities.h:94
const TMap< FGuid, FNamedAESKey > & GetEncryptionKeys() const
Definition KeyChainUtilities.h:93
FKeyChain & operator=(const FKeyChain &Other)
Definition KeyChainUtilities.h:48
FKeyChain(const FKeyChain &Other)
Definition KeyChainUtilities.h:22
const FNamedAESKey * GetPrincipalEncryptionKey() const
Definition KeyChainUtilities.h:90
PRAGMA_DISABLE_DEPRECATION_WARNINGS FRSAKeyHandle GetSigningKey() const
Definition KeyChainUtilities.h:87
const FNamedAESKey * MasterEncryptionKey
Definition KeyChainUtilities.h:106
void SetEncryptionKeys(const TMap< FGuid, FNamedAESKey > &keys)
Definition KeyChainUtilities.h:96
TMap< FGuid, FNamedAESKey > EncryptionKeys
Definition KeyChainUtilities.h:103
FKeyChain(FKeyChain &&Other)
Definition KeyChainUtilities.h:33
PRAGMA_ENABLE_DEPRECATION_WARNINGS FRSAKeyHandle SigningKey
Definition KeyChainUtilities.h:100
void SetSigningKey(FRSAKeyHandle key)
Definition KeyChainUtilities.h:88
void SetPrincipalEncryptionKey(const FNamedAESKey *key)
Definition KeyChainUtilities.h:91
FKeyChain & operator=(FKeyChain &&Other)
Definition KeyChainUtilities.h:65
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
Definition NamedAESKey.h:9
static RSA_API FRSAKeyHandle CreateKey(const TArray< uint8 > &InPublicExponent, const TArray< uint8 > &InPrivateExponent, const TArray< uint8 > &InModulus)
Definition RSA.cpp:16