Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Use machine key for encryption instead of ProtectSection() #22

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions Server/BackendClasses/BackendClasses.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
28 changes: 28 additions & 0 deletions Server/BackendClasses/Encryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,37 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;

namespace LauncherServerClasses
{
public static class MachineKeyEncryption

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is added to provide a different encryption method for hiding the secret key, locally, on the client machine. The "Protect" and "UnProtect" methods are used for encrypting and decrypting the secret key using the machine key, instead of using RSA.

{
public static string Protect(string text, string purpose)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}

byte[] stream = Encoding.UTF8.GetBytes(text);
byte[] encodedValues = MachineKey.Protect(stream, purpose);
return HttpServerUtility.UrlTokenEncode(encodedValues);
}

public static string UnProtect(string text, string purpose)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}

byte[] stream = HttpServerUtility.UrlTokenDecode(text);
byte[] decodedValues = MachineKey.Unprotect(stream, purpose);
return Encoding.UTF8.GetString(decodedValues);
}
}
public class Encryption
{
// Do not write your own encryption. Use libraries like JWT
Expand Down
13 changes: 12 additions & 1 deletion Server/LauncherClient/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using LauncherServerClasses;

namespace LauncherClient
{
Expand All @@ -32,6 +33,8 @@ public Launcher()
host = new ApiHost();
host.StartHost();

//encryption = new Encryption("the machine key");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to leave this comment in the code?

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
baseURL = configuration.AppSettings.Settings["BaseURL"].Value;
computerKey = configuration.AppSettings.Settings["ComputerKey"].Value;
Expand Down Expand Up @@ -122,8 +125,16 @@ private void game_start_timer_Tick(object sender, EventArgs e)
public void SetConfigValue(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

if(key == "Secret")
{
//string encryptedValue = encryption.Encrypt(value);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to leave this comment in the code?

string encryptedValue = MachineKeyEncryption.Protect(value, "Secret");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Secret" part here should be something more unique. Possibly something like $"Secret for computer {computerKey}"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I'll adjust that

Copy link
Author

@ColsterJ ColsterJ Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be fixed in commit 92bb6e5

value = encryptedValue;
}

configuration.AppSettings.Settings[key].Value = value;
configuration.AppSettings.SectionInformation.ProtectSection(null);
//configuration.AppSettings.SectionInformation.ProtectSection(null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I promise I'm gonna stop leaving this comment after this, but...

did you mean to leave this comment in the code?

please keep thinking about it as you look at this PR in total, even though I've stopped commenting.

configuration.Save();

ConfigurationManager.RefreshSection("appSettings");
Expand Down
2 changes: 1 addition & 1 deletion Server/LauncherClient/LauncherClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
<SignManifests>false</SignManifests>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this change do?

</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Owin, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
8 changes: 7 additions & 1 deletion Server/LauncherClient/Owin/Controllers/GamesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public StatusMessage StartGame(int id, bool install = false)
string baseURL = ConfigurationManager.AppSettings["BaseURL"];
string computerKey = ConfigurationManager.AppSettings["ComputerKey"];
string secretKey = ConfigurationManager.AppSettings["Secret"];
encryption = new Encryption(secretKey);

// We must decrypt the secret key using the machine key
//machineKeyEncryption = new Encryption("the machine key");
secretKey = MachineKeyEncryption.UnProtect(secretKey, "Secret");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Secret" here should match the one you changed in "SetConfigValue" - It shouldn't work if they are different.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I missed that. I edited the function call to match in a145cf0 and it seems to be functioning now.


// Then, we can Encrypt/Decrypt using that decrypted secret key
encryption = new Encryption(secretKey);

string URL = $"{baseURL}/game/checkout/{id}";
SteamGame game = gc.GetSteamLogin(id, URL, computerKey, "");
Expand Down