summaryrefslogtreecommitdiff
path: root/LibTSforge/Activators/AVMA4K.cs
blob: 21276520f0a129712659239901bd5600472dec59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace LibTSforge.Activators
{
    using System;
    using LibTSforge.PhysicalStore;
    using LibTSforge.SPP;

    public static class AVMA4k
    {
        public static void Activate(PSVersion version, bool production, Guid actId)
        {
            if (version != PSVersion.WinModern && version != PSVersion.WinBlue)
            {
                throw new NotSupportedException("AVMA licenses are not available for this product.");
            }

            Guid appId;
            if (actId == Guid.Empty)
            {
                appId = SLApi.WINDOWS_APP_ID;
                actId = SLApi.GetDefaultActivationID(appId, false);

                if (actId == Guid.Empty)
                {
                    throw new NotSupportedException("No applicable activation IDs found.");
                }
            }
            else
            {
                appId = SLApi.GetAppId(actId);
            }

            if (SLApi.GetPKeyChannel(SLApi.GetInstalledPkeyID(actId)) != "VT:IA")
            {
                throw new NotSupportedException("Non-VT:IA product key installed.");
            }

            SPPUtils.KillSPP(version);

            Logger.WriteLine("Writing TrustedStore data...");

            using (IPhysicalStore store = SPPUtils.GetStore(version, production))
            {
                string key = string.Format("SPPSVC\\{0}\\{1}", appId, actId);

                ulong unknown = 0;
                ulong time1;
                ulong crcBindTime = (ulong)DateTime.UtcNow.ToFileTime();
                ulong timerTime;

                ulong expiry = Constants.TimerMax;

                long creationTime = BitConverter.ToInt64(store.GetBlock("__##USERSEP##\\$$_RESERVED_$$\\NAMESPACE__", "__##USERSEP-RESERVED##__$$GLOBAL-CREATION-TIME$$").Data, 0);
                long tickCount = BitConverter.ToInt64(store.GetBlock("__##USERSEP##\\$$_RESERVED_$$\\NAMESPACE__", "__##USERSEP-RESERVED##__$$GLOBAL-TICKCOUNT-UPTIME$$").Data, 0);
                long deltaTime = BitConverter.ToInt64(store.GetBlock(key, "__##USERSEP-RESERVED##__$$UP-TIME-DELTA$$").Data, 0);

                time1 = (ulong)(creationTime + tickCount + deltaTime);
                timerTime = crcBindTime / 10000;
                expiry /= 10000;

                VariableBag avmaBinding = new VariableBag(version);

                avmaBinding.Blocks.AddRange(new CRCBlockModern[]
                {
                    new CRCBlockModern
                    {
                        DataType = CRCBlockType.BINARY,
                        Key = new byte[] { },
                        Value = BitConverter.GetBytes(crcBindTime),
                    },
                    new CRCBlockModern
                    {
                        DataType = CRCBlockType.STRING,
                        Key = new byte[] { },
                        ValueAsStr = "AVMA4K",
                    },
                    new CRCBlockModern
                    {
                        DataType = CRCBlockType.STRING,
                        Key = new byte[] { },
                        ValueAsStr = "00491-50000-00001-AA666",
                    }
                });

                byte[] avmaBindingData = avmaBinding.Serialize();

                Timer avmaTimer = new Timer
                {
                    Unknown = unknown,
                    Time1 = time1,
                    Time2 = timerTime,
                    Expiry = expiry
                };

                string storeVal = string.Format("msft:spp/ia/bind/1.0/store/{0}/{1}", appId, actId);
                string timerVal = string.Format("msft:spp/ia/bind/1.0/timer/{0}/{1}", appId, actId);

                store.DeleteBlock(key, storeVal);
                store.DeleteBlock(key, timerVal);

                store.AddBlocks(new PSBlock[]
                {
                    new PSBlock
                    {
                        Type = BlockType.NAMED,
                        Flags = 0x400,
                        KeyAsStr = key,
                        ValueAsStr = storeVal,
                        Data = avmaBindingData,
                    },
                    new PSBlock
                    {
                        Type = BlockType.TIMER,
                        Flags = 0x4,
                        KeyAsStr = key,
                        ValueAsStr = timerVal,
                        Data = avmaTimer.CastToArray()
                    }
                });
            }

            SLApi.RefreshLicenseStatus();
            SLApi.FireStateChangedEvent(appId);
            Logger.WriteLine("Activated using AVMA4k successfully.");
        }
    }
}