kms.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #ifndef __kms_h
  2. #define __kms_h
  3. #ifndef CONFIG
  4. #define CONFIG "config.h"
  5. #endif // CONFIG
  6. #include CONFIG
  7. #if _MSC_VER
  8. //#include <time.h>
  9. #else
  10. #include <sys/time.h>
  11. #endif // _MSC_VER
  12. //#include <stdlib.h>
  13. #include "types.h"
  14. //
  15. // REQUEST... types are actually fixed size
  16. // RESPONSE... size may vary, defined here is max possible size
  17. //
  18. #define MAX_RESPONSE_SIZE 384
  19. #define PID_BUFFER_SIZE 64
  20. #define MAX_REQUEST_SIZE sizeof(REQUEST_V6)
  21. #define WORKSTATION_NAME_BUFFER 64
  22. // Constants for V6 time stamp interval
  23. #define TIME_C1 0x00000022816889BDULL
  24. #define TIME_C2 0x000000208CBAB5EDULL
  25. #define TIME_C3 0x3156CD5AC628477AULL
  26. #define VERSION_INFO union \
  27. { \
  28. DWORD Version;\
  29. struct { \
  30. WORD MinorVer; \
  31. WORD MajorVer; \
  32. } /*__packed*/; \
  33. } /*__packed*/
  34. // Aliases for various KMS struct members
  35. #define IsClientVM VMInfo
  36. #define GraceTime BindingExpiration
  37. #define MinutesRemaingInCurrentStatus BindingExpiration
  38. #define ID ActID
  39. #define ApplicationID AppID
  40. #define SkuId ActID
  41. #define KmsId KMSID
  42. #define ClientMachineId CMID
  43. #define MinimumClients N_Policy
  44. #define TimeStamp ClientTime
  45. #define PreviousCLientMachineId CMID_prev
  46. #define Salt IV
  47. #define XorSalt XoredIVs
  48. #define ActivationInterval VLActivationInterval
  49. #define RenewalInterval VLRenewalInterval
  50. #define MAX_CLIENTS 671
  51. typedef struct
  52. {
  53. GUID Guid[MAX_CLIENTS];
  54. int_fast16_t CurrentCount;
  55. int_fast16_t MaxCount;
  56. int_fast16_t CurrentPosition;
  57. } ClientList_t, *PClientList_t;
  58. typedef struct {
  59. VERSION_INFO;
  60. DWORD VMInfo; // 0 = client is bare metal / 1 = client is VM
  61. DWORD LicenseStatus; // 0 = Unlicensed, 1 = Licensed (Activated), 2 = OOB grace, 3 = OOT grace, 4 = NonGenuineGrace, 5 = Notification, 6 = extended grace
  62. DWORD BindingExpiration; // Expiration of the current status in minutes (e.g. when KMS activation or OOB grace expires).
  63. GUID AppID; // Can currently be Windows, Office2010 or Office2013 (see kms.c, table AppList).
  64. GUID ActID; // Most detailed product list. One product key per ActID (see kms.c, table ExtendedProductList). Is ignored by KMS server.
  65. GUID KMSID; // This is actually what the KMS server uses to grant or refuse activation (see kms.c, table BasicProductList).
  66. GUID CMID; // Client machine id. Used by the KMS server for counting minimum clients.
  67. DWORD N_Policy; // Minimum clients required for activation.
  68. FILETIME ClientTime; // Current client time.
  69. GUID CMID_prev; // previous client machine id. All zeros, if it never changed.
  70. WCHAR WorkstationName[64]; // Workstation name. FQDN if available, NetBIOS otherwise.
  71. } /*__packed*/ REQUEST;
  72. typedef struct {
  73. VERSION_INFO;
  74. DWORD PIDSize; // Size of PIDData in bytes.
  75. WCHAR KmsPID[PID_BUFFER_SIZE]; // ePID (must include terminating zero)
  76. GUID CMID; // Client machine id. Must be the same as in request.
  77. FILETIME ClientTime; // Current client time. Must be the same as in request.
  78. DWORD Count; // Current activated machines. KMS server counts up to N_Policy << 1 then stops
  79. DWORD VLActivationInterval; // Time in minutes when clients should retry activation if it was unsuccessful (default 2 hours)
  80. DWORD VLRenewalInterval; // Time in minutes when clients should renew KMS activation (default 7 days)
  81. } /*__packed*/ RESPONSE;
  82. #ifdef _DEBUG
  83. typedef struct {
  84. VERSION_INFO;
  85. DWORD PIDSize;
  86. WCHAR KmsPID[49]; // Set this to the ePID length you want to debug
  87. GUID CMID;
  88. FILETIME ClientTime;
  89. DWORD Count;
  90. DWORD VLActivationInterval;
  91. DWORD VLRenewalInterval;
  92. } __packed RESPONSE_DEBUG;
  93. #endif
  94. typedef struct {
  95. REQUEST RequestBase; // Base request
  96. BYTE MAC[16]; // Aes 160 bit CMAC
  97. } /*__packed*/ REQUEST_V4;
  98. typedef struct {
  99. RESPONSE ResponseBase; // Base response
  100. BYTE MAC[16]; // Aes 160 bit CMAC
  101. } /*__packed*/ RESPONSE_V4;
  102. typedef struct {
  103. VERSION_INFO; // unencrypted version info
  104. BYTE IV[16]; // IV
  105. REQUEST RequestBase; // Base Request
  106. BYTE Pad[4]; // since this struct is fixed, we use fixed PKCS pad bytes
  107. } /*__packed*/ REQUEST_V5;
  108. typedef REQUEST_V5 REQUEST_V6; // v5 and v6 requests are identical
  109. typedef struct {
  110. VERSION_INFO;
  111. BYTE IV[16];
  112. RESPONSE ResponseBase;
  113. BYTE RandomXoredIVs[16]; // If RequestIV was used for decryption: Random ^ decrypted Request IV ^ ResponseIV. If NULL IV was used for decryption: Random ^ decrypted Request IV
  114. BYTE Hash[32]; // SHA256 of Random used in RandomXoredIVs
  115. BYTE HwId[8]; // HwId from the KMS server
  116. BYTE XoredIVs[16]; // If RequestIV was used for decryption: decrypted Request IV ^ ResponseIV. If NULL IV was used for decryption: decrypted Request IV.
  117. BYTE HMAC[16]; // V6 Hmac (low 16 bytes only), see kms.c CreateV6Hmac
  118. //BYTE Pad[10]; // Pad is variable sized. So do not include in struct
  119. } /*__packed*/ RESPONSE_V6;
  120. typedef struct { // not used except for sizeof(). Fields are the same as RESPONSE_V6
  121. VERSION_INFO;
  122. BYTE IV[16];
  123. RESPONSE ResponseBase;
  124. BYTE RandomXoredIVs[16];
  125. BYTE Hash[32];
  126. } /*__packed*/ RESPONSE_V5;
  127. #ifdef _DEBUG
  128. typedef struct { // Debug structure for direct casting of RPC data in debugger
  129. VERSION_INFO;
  130. BYTE IV[16];
  131. RESPONSE_DEBUG ResponseBase;
  132. BYTE RandomXoredIVs[16];
  133. BYTE MAC[32];
  134. BYTE Unknown[8];
  135. BYTE XorSalts[16];
  136. BYTE HMAC[16];
  137. BYTE Pad[16];
  138. } __packed RESPONSE_V6_DEBUG;
  139. #endif
  140. #define V4_PRE_EPID_SIZE ( \
  141. sizeof(((RESPONSE*)0)->Version) + \
  142. sizeof(((RESPONSE*)0)->PIDSize) \
  143. )
  144. #define V4_POST_EPID_SIZE ( \
  145. sizeof(((RESPONSE*)0)->CMID) + \
  146. sizeof(((RESPONSE*)0)->ClientTime) + \
  147. sizeof(((RESPONSE*)0)->Count) + \
  148. sizeof(((RESPONSE*)0)->VLActivationInterval) + \
  149. sizeof(((RESPONSE*)0)->VLRenewalInterval) \
  150. )
  151. #define V6_DECRYPT_SIZE ( \
  152. sizeof(((REQUEST_V6*)0)->IV) + \
  153. sizeof(((REQUEST_V6*)0)->RequestBase) + \
  154. sizeof(((REQUEST_V6*)0)->Pad) \
  155. )
  156. #define V6_UNENCRYPTED_SIZE ( \
  157. sizeof(((RESPONSE_V6*)0)->Version) + \
  158. sizeof(((RESPONSE_V6*)0)->IV) \
  159. )
  160. #define V6_PRE_EPID_SIZE ( \
  161. V6_UNENCRYPTED_SIZE + \
  162. sizeof(((RESPONSE*)0)->Version) + \
  163. sizeof(((RESPONSE*)0)->PIDSize) \
  164. )
  165. #define V5_POST_EPID_SIZE ( \
  166. V4_POST_EPID_SIZE + \
  167. sizeof(((RESPONSE_V6*)0)->RandomXoredIVs) + \
  168. sizeof(((RESPONSE_V6*)0)->Hash) \
  169. )
  170. #define V6_POST_EPID_SIZE ( \
  171. V5_POST_EPID_SIZE + \
  172. sizeof(((RESPONSE_V6*)0)->HwId) + \
  173. sizeof(((RESPONSE_V6*)0)->XoredIVs) + \
  174. sizeof(((RESPONSE_V6*)0)->HMAC) \
  175. )
  176. #define RESPONSE_RESULT_OK ((1 << 10) - 1) //(9 bits)
  177. typedef union
  178. {
  179. DWORD mask;
  180. struct
  181. {
  182. BOOL HashOK : 1;
  183. BOOL TimeStampOK : 1;
  184. BOOL ClientMachineIDOK : 1;
  185. BOOL VersionOK : 1;
  186. BOOL IVsOK : 1;
  187. BOOL DecryptSuccess : 1;
  188. BOOL HmacSha256OK : 1;
  189. BOOL PidLengthOK : 1;
  190. BOOL RpcOK : 1;
  191. BOOL IVnotSuspicious : 1;
  192. BOOL reserved3 : 1;
  193. BOOL reserved4 : 1;
  194. BOOL reserved5 : 1;
  195. BOOL reserved6 : 1;
  196. uint32_t effectiveResponseSize : 9;
  197. uint32_t correctResponseSize : 9;
  198. };
  199. } RESPONSE_RESULT;
  200. typedef BYTE hwid_t[8];
  201. typedef struct CsvlkData
  202. {
  203. union
  204. {
  205. uint64_t EPidOffset;
  206. char* EPid;
  207. };
  208. uint32_t GroupId;
  209. uint32_t MinKeyId;
  210. uint32_t MaxKeyId;
  211. uint8_t MinActiveClients;
  212. uint8_t Reserved[3];
  213. } CsvlkData_t, *PCsvlkData_t;
  214. typedef struct VlmcsdData
  215. {
  216. union
  217. {
  218. GUID Guid;
  219. uint8_t GuidBytes[16];
  220. };
  221. union
  222. {
  223. uint64_t NameOffset;
  224. char* Name;
  225. };
  226. uint8_t AppIndex;
  227. uint8_t KmsIndex;
  228. uint8_t ProtocolVersion;
  229. uint8_t NCountPolicy;
  230. uint8_t IsRetail;
  231. uint8_t IsPreview;
  232. uint8_t EPidIndex;
  233. uint8_t reserved;
  234. } VlmcsdData_t, *PVlmcsdData_t;
  235. typedef struct
  236. {
  237. union
  238. {
  239. uint64_t Offset;
  240. void* Pointer;
  241. };
  242. } DataPointer_t;
  243. #define KMS_OPTIONS_USENDR64 1 << 0
  244. typedef struct VlmcsdHeader
  245. {
  246. BYTE Magic[4];
  247. VERSION_INFO;
  248. uint8_t CsvlkCount;
  249. uint8_t Flags;
  250. uint8_t Reserved[2];
  251. union
  252. {
  253. int32_t Counts[3];
  254. struct
  255. {
  256. int32_t AppItemCount;
  257. int32_t KmsItemCount;
  258. int32_t SkuItemCount;
  259. };
  260. };
  261. union
  262. {
  263. DataPointer_t Datapointers[3];
  264. struct
  265. {
  266. union
  267. {
  268. uint64_t AppItemOffset;
  269. PVlmcsdData_t AppItemList;
  270. };
  271. union
  272. {
  273. uint64_t KmsItemOffset;
  274. PVlmcsdData_t KmsItemList;
  275. };
  276. union
  277. {
  278. uint64_t SkuItemOffset;
  279. PVlmcsdData_t SkuItemList;
  280. };
  281. CsvlkData_t CsvlkData[1];
  282. };
  283. };
  284. } VlmcsdHeader_t, *PVlmcsdHeader_t;
  285. #define EPID_INDEX_WINDOWS 0
  286. #define EPID_INDEX_OFFICE2010 1
  287. #define EPID_INDEX_OFFICE2013 2
  288. #define EPID_INDEX_OFFICE2016 3
  289. #define EPID_INDEX_WINCHINAGOV 4
  290. typedef HRESULT(__stdcall *RequestCallback_t)(const REQUEST *const baseRequest, RESPONSE *const baseResponse, BYTE *const hwId, const char* const ipstr);
  291. size_t CreateResponseV4(REQUEST_V4 *const Request, BYTE *const response_data, const char* const ipstr);
  292. size_t CreateResponseV6(REQUEST_V6 *restrict Request, BYTE *const response_data, const char* const ipstr);
  293. BYTE *CreateRequestV4(size_t *size, const REQUEST* requestBase);
  294. BYTE *CreateRequestV6(size_t *size, const REQUEST* requestBase);
  295. void randomPidInit();
  296. void get16RandomBytes(void* ptr);
  297. RESPONSE_RESULT DecryptResponseV6(RESPONSE_V6* Response_v6, int responseSize, BYTE* const response, const BYTE* const request, BYTE* hwid);
  298. RESPONSE_RESULT DecryptResponseV4(RESPONSE_V4* Response_v4, const int responseSize, BYTE* const response, const BYTE* const request);
  299. void getUnixTimeAsFileTime(FILETIME *const ts);
  300. __pure int64_t fileTimeToUnixTime(const FILETIME *const ts);
  301. #ifndef IS_LIBRARY
  302. int32_t getProductIndex(const GUID* guid, const PVlmcsdData_t list, const int32_t count, char** name, char** ePid);
  303. #endif // IS_LIBRARY
  304. #ifndef NO_STRICT_MODES
  305. void InitializeClientLists();
  306. void CleanUpClientLists();
  307. #endif // !NO_STRICT_MODES
  308. extern RequestCallback_t CreateResponseBase;
  309. #ifdef _PEDANTIC
  310. uint16_t IsValidLcid(const uint16_t lcid);
  311. uint16_t IsValidHostBuild(const uint16_t hostBuild);
  312. #endif // _PEDANTIC
  313. #endif // __kms_h