Skip to content

Commit 5ae6b4d

Browse files
committed
Refactor and cleanup
1 parent f90695d commit 5ae6b4d

File tree

12 files changed

+215
-333
lines changed

12 files changed

+215
-333
lines changed

MetroUnlocker.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ VisualStudioVersion = 12.0.40629.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MetroUnlocker", "MetroUnlocker\MetroUnlocker.csproj", "{D27EB145-0B58-43AD-BB94-BE000D236D38}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C9C14067-F9A1-4D85-A1D2-162E91B71CC3}"
9+
ProjectSection(SolutionItems) = preProject
10+
DependencyGraph.dgml = DependencyGraph.dgml
11+
EndProjectSection
12+
EndProject
813
Global
914
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1015
Debug|Any CPU = Debug|Any CPU

MetroUnlocker/App.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,38 @@ namespace MetroUnlocker
1717
{
1818
public partial class App : Form
1919
{
20-
public const string AppxKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Windows\\Appx";
20+
public const string AppxRegistryKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Windows\\Appx";
2121

22-
string trustedAppsPolicyName = "AllowAllTrustedApps";
23-
string developmentPolicyName = "AllowDevelopmentWithoutDevLicense";
24-
string specialProfilesPolicyName = "AllowDeploymentInSpecialProfiles";
22+
string TrustedAppsPolicyName = "AllowAllTrustedApps";
23+
string DevelopmentPolicyName = "AllowDevelopmentWithoutDevLicense";
24+
string SpecialProfilesPolicyName = "AllowDeploymentInSpecialProfiles";
2525

2626
public bool LOBEnabled
2727
{
28-
get { return GetGroupPolicy(trustedAppsPolicyName); }
29-
set { SetGroupPolicy(trustedAppsPolicyName, value); }
28+
get { return GetGroupPolicy(TrustedAppsPolicyName); }
29+
set { SetGroupPolicy(TrustedAppsPolicyName, value); }
3030
}
3131

3232
public bool DevelopmentEnabled
3333
{
34-
get { return GetGroupPolicy(developmentPolicyName); }
35-
set { SetGroupPolicy(developmentPolicyName, value); }
34+
get { return GetGroupPolicy(DevelopmentPolicyName); }
35+
set { SetGroupPolicy(DevelopmentPolicyName, value); }
3636
}
3737

3838
public bool SpecialProfilesEnabled
3939
{
40-
get { return GetGroupPolicy(specialProfilesPolicyName); }
41-
set { SetGroupPolicy(specialProfilesPolicyName, value); }
40+
get { return GetGroupPolicy(SpecialProfilesPolicyName); }
41+
set { SetGroupPolicy(SpecialProfilesPolicyName, value); }
4242
}
4343

4444
public void SetGroupPolicy(string policyName, bool enabled)
4545
{
46-
Registry.SetValue(AppxKey, policyName, enabled ? 1 : 0, RegistryValueKind.DWord);
46+
Registry.SetValue(AppxRegistryKey, policyName, enabled ? 1 : 0, RegistryValueKind.DWord);
4747
}
4848

4949
public bool GetGroupPolicy(string policyName)
5050
{
51-
object value = Registry.GetValue(AppxKey, policyName, 0);
51+
object value = Registry.GetValue(AppxRegistryKey, policyName, 0);
5252
return value is int ? (int)value == 1 : false;
5353
}
5454

MetroUnlocker/Common.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public static class Constants
4545

4646
public static class Utils
4747
{
48-
[DllImport("kernel32.dll")]
49-
public static extern uint GetSystemDefaultLCID();
50-
5148
public static string GetArchitecture()
5249
{
5350
string arch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine).ToUpperInvariant();

MetroUnlocker/LibTSForge/Crypto/CryptoUtils.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace MetroUnlocker.LibTSForge.Crypto
66
{
77
public static class CryptoUtils
88
{
9-
public static byte[] GenerateRandomKey(int len)
9+
public static byte[] GenerateRandomKey(int length)
1010
{
11-
byte[] rand = new byte[len];
12-
Random r = new Random();
13-
r.NextBytes(rand);
11+
byte[] randomKey = new byte[length];
12+
Random random = new Random();
13+
random.NextBytes(randomKey);
1414

15-
return rand;
15+
return randomKey;
1616
}
1717

1818
public static byte[] AESEncrypt(byte[] data, byte[] key)

MetroUnlocker/LibTSForge/Crypto/PhysicalStoreCrypto.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ public static byte[] EncryptPhysicalStore(byte[] data, bool production, Physical
4747
byte[] aesKey = Encoding.UTF8.GetBytes("Boop Foxyz nose!");
4848
byte[] hmacKey = CryptoUtils.GenerateRandomKey(0x10);
4949

50-
byte[] encAesKey = CryptoUtils.RSAEncrypt(rsaKey, aesKey);
51-
byte[] aesKeySig = CryptoUtils.RSASign(rsaKey, encAesKey);
52-
byte[] hmacSig = CryptoUtils.HMACSign(hmacKey, data);
50+
byte[] encryptedAesKey = CryptoUtils.RSAEncrypt(rsaKey, aesKey);
51+
byte[] aesKeySignature = CryptoUtils.RSASign(rsaKey, encryptedAesKey);
52+
byte[] hmacSignature = CryptoUtils.HMACSign(hmacKey, data);
5353

5454
byte[] decData = new byte[] { };
55-
decData = decData.Concat(hmacKey).Concat(hmacSig).Concat(BitConverter.GetBytes(0)).Concat(data).ToArray();
55+
decData = decData.Concat(hmacKey).Concat(hmacSignature).Concat(BitConverter.GetBytes(0)).Concat(data).ToArray();
5656
byte[] encData = CryptoUtils.AESEncrypt(decData, aesKey);
5757

5858
BinaryWriter bw = new BinaryWriter(new MemoryStream());
5959
bw.Write(versionTable[version]);
6060
bw.Write(Encoding.UTF8.GetBytes("UNTRUSTSTORE"));
61-
bw.Write(aesKeySig);
62-
bw.Write(encAesKey);
61+
bw.Write(aesKeySignature);
62+
bw.Write(encryptedAesKey);
6363
bw.Write(encData);
6464

6565
return bw.GetBytes();

MetroUnlocker/LibTSForge/Modifiers/ProductKeyInstaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void InstallGeneratedProductKey(PhysicalStoreVersion version, bool
2222
Guid instPkeyId = SLApi.GetInstalledProductKeyId(actId);
2323
if (instPkeyId != Guid.Empty) SLApi.UninstallProductKey(instPkeyId);
2424

25-
if (pkey.Algorithm != PKeyAlgorithm.PKEY2009)
25+
if (pkey.Algorithm != ProductKeyAlgorithm.ProductKey2009)
2626
throw new Exception("The key algorithm isn't 2009");
2727

2828
uint status = SLApi.InstallProductKey(pkey);

MetroUnlocker/LibTSForge/PhysicalStore/PhysicalStore.cs

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -90,92 +90,33 @@ public void AddBlock(ModernBlock block)
9090

9191
public void AddBlocks(IEnumerable<ModernBlock> blocks)
9292
{
93-
foreach (ModernBlock block in blocks)
94-
{
95-
AddBlock(block);
96-
}
93+
foreach (ModernBlock block in blocks) AddBlock(block);
9794
}
9895

9996
public ModernBlock GetBlock(string key, string value)
10097
{
101-
List<ModernBlock> blocks = Data[key];
102-
103-
foreach (ModernBlock block in blocks)
104-
{
105-
if (block.ValueAsString == value)
106-
{
107-
return new ModernBlock
108-
{
109-
Type = block.Type,
110-
Flags = block.Flags,
111-
Key = Utils.EncodeString(key),
112-
Value = block.Value,
113-
Data = block.Data
114-
};
115-
}
116-
}
117-
118-
return null;
98+
if (!Data.ContainsKey(key)) return null;
99+
return Data[key].Find(block => block.ValueAsString == value);
119100
}
120101

121102
public ModernBlock GetBlock(string key, uint value)
122103
{
123-
List<ModernBlock> blocks = Data[key];
124-
125-
foreach (ModernBlock block in blocks)
126-
{
127-
if (block.ValueAsInteger == value)
128-
{
129-
return new ModernBlock
130-
{
131-
Type = block.Type,
132-
Flags = block.Flags,
133-
Key = Utils.EncodeString(key),
134-
Value = block.Value,
135-
Data = block.Data
136-
};
137-
}
138-
}
139-
140-
return null;
104+
if (!Data.ContainsKey(key)) return null;
105+
return Data[key].Find(block => block.ValueAsInteger == value);
141106
}
142107

143108
public void SetBlock(string key, string value, byte[] data)
144109
{
145-
List<ModernBlock> blocks = Data[key];
146-
147-
for (int i = 0; i < blocks.Count; i++)
148-
{
149-
ModernBlock block = blocks[i];
150-
151-
if (block.ValueAsString == value)
152-
{
153-
block.Data = data;
154-
blocks[i] = block;
155-
break;
156-
}
157-
}
158-
159-
Data[key] = blocks;
110+
if (!Data.ContainsKey(key)) return;
111+
int index = Data[key].FindIndex(block => block.ValueAsString == value);
112+
Data[key][index].Data = data;
160113
}
161114

162115
public void SetBlock(string key, uint value, byte[] data)
163116
{
164-
List<ModernBlock> blocks = Data[key];
165-
166-
for (int i = 0; i < blocks.Count; i++)
167-
{
168-
ModernBlock block = blocks[i];
169-
170-
if (block.ValueAsInteger == value)
171-
{
172-
block.Data = data;
173-
blocks[i] = block;
174-
break;
175-
}
176-
}
177-
178-
Data[key] = blocks;
117+
if (!Data.ContainsKey(key)) return;
118+
int index = Data[key].FindIndex(block => block.ValueAsInteger == value);
119+
Data[key][index].Data = data;
179120
}
180121

181122
public void SetBlock(string key, string value, string data)
@@ -200,40 +141,12 @@ public void SetBlock(string key, uint value, uint data)
200141

201142
public void DeleteBlock(string key, string value)
202143
{
203-
if (Data.ContainsKey(key))
204-
{
205-
List<ModernBlock> blocks = Data[key];
206-
207-
foreach (ModernBlock block in blocks)
208-
{
209-
if (block.ValueAsString == value)
210-
{
211-
blocks.Remove(block);
212-
break;
213-
}
214-
}
215-
216-
Data[key] = blocks;
217-
}
144+
Data[key].Remove(GetBlock(key, value));
218145
}
219146

220147
public void DeleteBlock(string key, uint value)
221148
{
222-
if (Data.ContainsKey(key))
223-
{
224-
List<ModernBlock> blocks = Data[key];
225-
226-
foreach (ModernBlock block in blocks)
227-
{
228-
if (block.ValueAsInteger == value)
229-
{
230-
blocks.Remove(block);
231-
break;
232-
}
233-
}
234-
235-
Data[key] = blocks;
236-
}
149+
Data[key].Remove(GetBlock(key, value));
237150
}
238151

239152
public static string GetPath()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
using System.Runtime.InteropServices;
7+
8+
namespace MetroUnlocker.LibTSForge.SPP
9+
{
10+
public enum SLIDType
11+
{
12+
Application,
13+
ProductSku,
14+
LicenseFile,
15+
License,
16+
ProductKey,
17+
AllLicenses,
18+
AllLicenseFiles,
19+
StoreToken,
20+
Last
21+
}
22+
23+
public enum SLDataType
24+
{
25+
None,
26+
String,
27+
DWord,
28+
Binary,
29+
MultiString,
30+
Sum
31+
}
32+
33+
[StructLayout(LayoutKind.Sequential)]
34+
public struct SLLicensingStatus
35+
{
36+
public Guid SkuId;
37+
public uint Status;
38+
public uint GraceTimeDWord;
39+
public uint TotalGraceDaysDWord;
40+
public uint ReasonHResult;
41+
public ulong ValidityExpiration;
42+
}
43+
44+
public class NativeMethods
45+
{
46+
[DllImport("kernel32.dll")]
47+
internal static extern uint GetSystemDefaultLCID();
48+
49+
[DllImport("sppc.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
50+
internal static extern void SLOpen(out IntPtr hSLC);
51+
52+
[DllImport("sppc.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
53+
internal static extern void SLClose(IntPtr hSLC);
54+
55+
[DllImport("sppc.dll", CharSet = CharSet.Unicode)]
56+
internal static extern uint SLInstallProofOfPurchase(IntPtr hSLC, string pwszPKeyAlgorithm, string pwszPKeyString, uint cbPKeySpecificData, byte[] pbPKeySpecificData, ref Guid PKeyId);
57+
58+
[DllImport("sppc.dll", CharSet = CharSet.Unicode)]
59+
internal static extern uint SLUninstallProofOfPurchase(IntPtr hSLC, ref Guid PKeyId);
60+
61+
[DllImport("sppc.dll", CharSet = CharSet.Unicode)]
62+
internal static extern uint SLGenerateOfflineInstallationId(IntPtr hSLC, ref Guid pProductSkuId, ref string ppwszInstallationId);
63+
64+
[DllImport("sppc.dll", CharSet = CharSet.Unicode)]
65+
internal static extern uint SLDepositOfflineConfirmationId(IntPtr hSLC, ref Guid pProductSkuId, string pwszInstallationId, string pwszConfirmationId);
66+
67+
[DllImport("sppc.dll", CharSet = CharSet.Unicode)]
68+
internal static extern uint SLGetSLIDList(IntPtr hSLC, SLIDType eQueryIdType, ref Guid pQueryId, SLIDType eReturnIdType, out uint pnReturnIds, out IntPtr ppReturnIds);
69+
70+
[DllImport("sppc.dll", CharSet = CharSet.Unicode)]
71+
internal static extern uint SLGetInstalledProductKeyIds(IntPtr hSLC, ref Guid pProductSkuId, out uint pnProductKeyIds, out IntPtr ppProductKeyIds);
72+
73+
[DllImport("slc.dll", CharSet = CharSet.Unicode)]
74+
internal static extern uint SLConsumeWindowsRight(uint unknown);
75+
76+
[DllImport("slc.dll", CharSet = CharSet.Unicode)]
77+
internal static extern uint SLGetProductSkuInformation(IntPtr hSLC, ref Guid pProductSkuId, string pwszValueName, out SLDataType peDataType, out uint pcbValue, out IntPtr ppbValue);
78+
79+
[DllImport("slc.dll", CharSet = CharSet.Unicode)]
80+
internal static extern uint SLGetProductSkuInformation(IntPtr hSLC, ref Guid pProductSkuId, string pwszValueName, IntPtr peDataType, out uint pcbValue, out IntPtr ppbValue);
81+
82+
[DllImport("slc.dll", CharSet = CharSet.Unicode)]
83+
internal static extern uint SLGetLicense(IntPtr hSLC, ref Guid pLicenseFileId, out uint pcbLicenseFile, out IntPtr ppbLicenseFile);
84+
85+
[DllImport("slc.dll", CharSet = CharSet.Unicode)]
86+
internal static extern uint SLSetCurrentProductKey(IntPtr hSLC, ref Guid pProductSkuId, ref Guid pProductKeyId);
87+
88+
[DllImport("slc.dll", CharSet = CharSet.Unicode)]
89+
internal static extern uint SLFireEvent(IntPtr hSLC, string pwszEventId, ref Guid pApplicationId);
90+
}
91+
}

0 commit comments

Comments
 (0)