kms.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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
  202. {
  203. GUID guid;
  204. const char* name;
  205. const char* pid;
  206. uint8_t AppIndex;
  207. uint8_t KmsIndex;
  208. } KmsIdList;
  209. #define KMS_PARAM_MAJOR AppIndex
  210. #define KMS_PARAM_REQUIREDCOUNT KmsIndex
  211. #define APP_ID_WINDOWS 0
  212. #define APP_ID_OFFICE2010 1
  213. #define APP_ID_OFFICE2013 2
  214. #define EPID_INDEX_WINDOWS APP_ID_WINDOWS
  215. #define EPID_INDEX_OFFICE2010 APP_ID_OFFICE2010
  216. #define EPID_INDEX_OFFICE2013 APP_ID_OFFICE2013
  217. #define EPID_INDEX_OFFICE2016 3
  218. // Update these numbers in License Manager
  219. #define KMS_ID_OFFICE2010 0
  220. #define KMS_ID_OFFICE2013 1
  221. #define KMS_ID_OFFICE2013_BETA 2
  222. #define KMS_ID_OFFICE2016 3
  223. #define KMS_ID_VISTA 4
  224. #define KMS_ID_WIN7 5
  225. #define KMS_ID_WIN8_VL 6
  226. #define KMS_ID_WIN_BETA 7
  227. #define KMS_ID_WIN8_RETAIL 8
  228. #define KMS_ID_WIN81_VL 9
  229. #define KMS_ID_WIN81_RETAIL 10
  230. #define KMS_ID_WIN2008A 11
  231. #define KMS_ID_WIN2008B 12
  232. #define KMS_ID_WIN2008C 13
  233. #define KMS_ID_WIN2008R2A 14
  234. #define KMS_ID_WIN2008R2B 15
  235. #define KMS_ID_WIN2008R2C 16
  236. #define KMS_ID_WIN2012 17
  237. #define KMS_ID_WIN2012R2 18
  238. #define KMS_ID_WIN_SRV_BETA 19
  239. #define KMS_ID_WIN10_VL 20
  240. #define KMS_ID_WIN10_RETAIL 21
  241. #define KMS_ID_WIN2016 22
  242. #define KMS_ID_WIN10_LTSB2016 23
  243. #define PWINGUID &AppList[APP_ID_WINDOWS].guid
  244. #define POFFICE2010GUID &AppList[APP_ID_OFFICE2010].guid
  245. #define POFFICE2013GUID &AppList[APP_ID_OFFICE2013].guid
  246. typedef HRESULT(__stdcall *RequestCallback_t)(const REQUEST *const baseRequest, RESPONSE *const baseResponse, BYTE *const hwId, const char* const ipstr);
  247. size_t CreateResponseV4(REQUEST_V4 *const Request, BYTE *const response_data, const char* const ipstr);
  248. size_t CreateResponseV6(REQUEST_V6 *restrict Request, BYTE *const response_data, const char* const ipstr);
  249. BYTE *CreateRequestV4(size_t *size, const REQUEST* requestBase);
  250. BYTE *CreateRequestV6(size_t *size, const REQUEST* requestBase);
  251. void randomPidInit();
  252. void get16RandomBytes(void* ptr);
  253. RESPONSE_RESULT DecryptResponseV6(RESPONSE_V6* Response_v6, int responseSize, BYTE* const response, const BYTE* const request, BYTE* hwid);
  254. RESPONSE_RESULT DecryptResponseV4(RESPONSE_V4* Response_v4, const int responseSize, BYTE* const response, const BYTE* const request);
  255. void getUnixTimeAsFileTime(FILETIME *const ts);
  256. __pure int64_t fileTimeToUnixTime(const FILETIME *const ts);
  257. const char* getProductNameHE(const GUID *const guid, const KmsIdList *const List, ProdListIndex_t maxList, ProdListIndex_t *const i);
  258. const char* getProductNameLE(const GUID *const guid, const KmsIdList *const List, ProdListIndex_t maxList, ProdListIndex_t *const i);
  259. __pure ProdListIndex_t getExtendedProductListSize();
  260. __pure ProdListIndex_t getAppListSize(void);
  261. __pure ProdListIndex_t getProductListSize(void);
  262. extern const KmsIdList ProductList[];
  263. extern const KmsIdList AppList[];
  264. extern const KmsIdList ExtendedProductList[];
  265. #ifndef NO_STRICT_MODES
  266. void InitializeClientLists();
  267. void CleanUpClientLists();
  268. #endif // !NO_STRICT_MODES
  269. extern RequestCallback_t CreateResponseBase;
  270. #ifdef _PEDANTIC
  271. uint16_t IsValidLcid(const uint16_t Lcid);
  272. #endif // _PEDANTIC
  273. #endif // __kms_h