kms.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. typedef struct {
  51. VERSION_INFO;
  52. DWORD VMInfo; // 0 = client is bare metal / 1 = client is VM
  53. DWORD LicenseStatus; // 0 = Unlicensed, 1 = Licensed (Activated), 2 = OOB grace, 3 = OOT grace, 4 = NonGenuineGrace, 5 = Notification, 6 = extended grace
  54. DWORD BindingExpiration; // Expiration of the current status in minutes (e.g. when KMS activation or OOB grace expires).
  55. GUID AppID; // Can currently be Windows, Office2010 or Office2013 (see kms.c, table AppList).
  56. GUID ActID; // Most detailed product list. One product key per ActID (see kms.c, table ExtendedProductList). Is ignored by KMS server.
  57. GUID KMSID; // This is actually what the KMS server uses to grant or refuse activation (see kms.c, table BasicProductList).
  58. GUID CMID; // Client machine id. Used by the KMS server for counting minimum clients.
  59. DWORD N_Policy; // Minimum clients required for activation.
  60. FILETIME ClientTime; // Current client time.
  61. GUID CMID_prev; // previous client machine id. All zeros, if it never changed.
  62. WCHAR WorkstationName[64]; // Workstation name. FQDN if available, NetBIOS otherwise.
  63. } /*__packed*/ REQUEST;
  64. typedef struct {
  65. VERSION_INFO;
  66. DWORD PIDSize; // Size of PIDData in bytes.
  67. WCHAR KmsPID[PID_BUFFER_SIZE]; // ePID (must include terminating zero)
  68. GUID CMID; // Client machine id. Must be the same as in request.
  69. FILETIME ClientTime; // Current client time. Must be the same as in request.
  70. DWORD Count; // Current activated machines. KMS server counts up to N_Policy << 1 then stops
  71. DWORD VLActivationInterval; // Time in minutes when clients should retry activation if it was unsuccessful (default 2 hours)
  72. DWORD VLRenewalInterval; // Time in minutes when clients should renew KMS activation (default 7 days)
  73. } /*__packed*/ RESPONSE;
  74. #ifdef _DEBUG
  75. typedef struct {
  76. VERSION_INFO;
  77. DWORD PIDSize;
  78. WCHAR KmsPID[49]; // Set this to the ePID length you want to debug
  79. GUID CMID;
  80. FILETIME ClientTime;
  81. DWORD Count;
  82. DWORD VLActivationInterval;
  83. DWORD VLRenewalInterval;
  84. } __packed RESPONSE_DEBUG;
  85. #endif
  86. typedef struct {
  87. REQUEST RequestBase; // Base request
  88. BYTE MAC[16]; // Aes 160 bit CMAC
  89. } /*__packed*/ REQUEST_V4;
  90. typedef struct {
  91. RESPONSE ResponseBase; // Base response
  92. BYTE MAC[16]; // Aes 160 bit CMAC
  93. } /*__packed*/ RESPONSE_V4;
  94. typedef struct {
  95. VERSION_INFO; // unencrypted version info
  96. BYTE IV[16]; // IV
  97. REQUEST RequestBase; // Base Request
  98. BYTE Pad[4]; // since this struct is fixed, we use fixed PKCS pad bytes
  99. } /*__packed*/ REQUEST_V5;
  100. typedef REQUEST_V5 REQUEST_V6; // v5 and v6 requests are identical
  101. typedef struct {
  102. VERSION_INFO;
  103. BYTE IV[16];
  104. RESPONSE ResponseBase;
  105. 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
  106. BYTE Hash[32]; // SHA256 of Random used in RandomXoredIVs
  107. BYTE HwId[8]; // HwId from the KMS server
  108. BYTE XoredIVs[16]; // If RequestIV was used for decryption: decrypted Request IV ^ ResponseIV. If NULL IV was used for decryption: decrypted Request IV.
  109. BYTE HMAC[16]; // V6 Hmac (low 16 bytes only), see kms.c CreateV6Hmac
  110. //BYTE Pad[10]; // Pad is variable sized. So do not include in struct
  111. } /*__packed*/ RESPONSE_V6;
  112. typedef struct { // not used except for sizeof(). Fields are the same as RESPONSE_V6
  113. VERSION_INFO;
  114. BYTE IV[16];
  115. RESPONSE ResponseBase;
  116. BYTE RandomXoredIVs[16];
  117. BYTE Hash[32];
  118. } /*__packed*/ RESPONSE_V5;
  119. #ifdef _DEBUG
  120. typedef struct { // Debug structure for direct casting of RPC data in debugger
  121. VERSION_INFO;
  122. BYTE IV[16];
  123. RESPONSE_DEBUG ResponseBase;
  124. BYTE RandomXoredIVs[16];
  125. BYTE MAC[32];
  126. BYTE Unknown[8];
  127. BYTE XorSalts[16];
  128. BYTE HMAC[16];
  129. BYTE Pad[16];
  130. } __packed RESPONSE_V6_DEBUG;
  131. #endif
  132. #define V4_PRE_EPID_SIZE ( \
  133. sizeof(((RESPONSE*)0)->Version) + \
  134. sizeof(((RESPONSE*)0)->PIDSize) \
  135. )
  136. #define V4_POST_EPID_SIZE ( \
  137. sizeof(((RESPONSE*)0)->CMID) + \
  138. sizeof(((RESPONSE*)0)->ClientTime) + \
  139. sizeof(((RESPONSE*)0)->Count) + \
  140. sizeof(((RESPONSE*)0)->VLActivationInterval) + \
  141. sizeof(((RESPONSE*)0)->VLRenewalInterval) \
  142. )
  143. #define V6_DECRYPT_SIZE ( \
  144. sizeof(((REQUEST_V6*)0)->IV) + \
  145. sizeof(((REQUEST_V6*)0)->RequestBase) + \
  146. sizeof(((REQUEST_V6*)0)->Pad) \
  147. )
  148. #define V6_UNENCRYPTED_SIZE ( \
  149. sizeof(((RESPONSE_V6*)0)->Version) + \
  150. sizeof(((RESPONSE_V6*)0)->IV) \
  151. )
  152. #define V6_PRE_EPID_SIZE ( \
  153. V6_UNENCRYPTED_SIZE + \
  154. sizeof(((RESPONSE*)0)->Version) + \
  155. sizeof(((RESPONSE*)0)->PIDSize) \
  156. )
  157. #define V5_POST_EPID_SIZE ( \
  158. V4_POST_EPID_SIZE + \
  159. sizeof(((RESPONSE_V6*)0)->RandomXoredIVs) + \
  160. sizeof(((RESPONSE_V6*)0)->Hash) \
  161. )
  162. #define V6_POST_EPID_SIZE ( \
  163. V5_POST_EPID_SIZE + \
  164. sizeof(((RESPONSE_V6*)0)->HwId) + \
  165. sizeof(((RESPONSE_V6*)0)->XoredIVs) + \
  166. sizeof(((RESPONSE_V6*)0)->HMAC) \
  167. )
  168. #define RESPONSE_RESULT_OK ((1 << 10) - 1) //(9 bits)
  169. typedef union
  170. {
  171. DWORD mask;
  172. struct
  173. {
  174. BOOL HashOK : 1;
  175. BOOL TimeStampOK : 1;
  176. BOOL ClientMachineIDOK : 1;
  177. BOOL VersionOK : 1;
  178. BOOL IVsOK : 1;
  179. BOOL DecryptSuccess : 1;
  180. BOOL HmacSha256OK : 1;
  181. BOOL PidLengthOK : 1;
  182. BOOL RpcOK : 1;
  183. BOOL IVnotSuspicious : 1;
  184. BOOL reserved3 : 1;
  185. BOOL reserved4 : 1;
  186. BOOL reserved5 : 1;
  187. BOOL reserved6 : 1;
  188. uint32_t effectiveResponseSize : 9;
  189. uint32_t correctResponseSize : 9;
  190. };
  191. } RESPONSE_RESULT;
  192. typedef BYTE hwid_t[8];
  193. typedef struct
  194. {
  195. GUID guid;
  196. const char* name;
  197. const char* pid;
  198. uint8_t AppIndex;
  199. uint8_t KmsIndex;
  200. } KmsIdList;
  201. #define KMS_PARAM_MAJOR AppIndex
  202. #define KMS_PARAM_REQUIREDCOUNT KmsIndex
  203. #define APP_ID_WINDOWS 0
  204. #define APP_ID_OFFICE2010 1
  205. #define APP_ID_OFFICE2013 2
  206. #define EPID_INDEX_WINDOWS APP_ID_WINDOWS
  207. #define EPID_INDEX_OFFICE2010 APP_ID_OFFICE2010
  208. #define EPID_INDEX_OFFICE2013 APP_ID_OFFICE2013
  209. #define EPID_INDEX_OFFICE2016 3
  210. // Update these numbers in License Manager
  211. #define KMS_ID_OFFICE2010 0
  212. #define KMS_ID_OFFICE2013 1
  213. #define KMS_ID_OFFICE2013_BETA 2
  214. #define KMS_ID_OFFICE2016 3
  215. #define KMS_ID_VISTA 4
  216. #define KMS_ID_WIN7 5
  217. #define KMS_ID_WIN8_VL 6
  218. #define KMS_ID_WIN_BETA 7
  219. #define KMS_ID_WIN8_RETAIL 8
  220. #define KMS_ID_WIN81_VL 9
  221. #define KMS_ID_WIN81_RETAIL 10
  222. #define KMS_ID_WIN2008A 11
  223. #define KMS_ID_WIN2008B 12
  224. #define KMS_ID_WIN2008C 13
  225. #define KMS_ID_WIN2008R2A 14
  226. #define KMS_ID_WIN2008R2B 15
  227. #define KMS_ID_WIN2008R2C 16
  228. #define KMS_ID_WIN2012 17
  229. #define KMS_ID_WIN2012R2 18
  230. #define KMS_ID_WIN_SRV_BETA 19
  231. #define KMS_ID_WIN10_VL 20
  232. #define KMS_ID_WIN10_RETAIL 21
  233. #define KMS_ID_WIN2016 22
  234. #define KMS_ID_WIN10_LTSB2016 23
  235. #define PWINGUID &AppList[APP_ID_WINDOWS].guid
  236. #define POFFICE2010GUID &AppList[APP_ID_OFFICE2010].guid
  237. #define POFFICE2013GUID &AppList[APP_ID_OFFICE2013].guid
  238. typedef BOOL(__stdcall *RequestCallback_t)(const REQUEST *const baseRequest, RESPONSE *const baseResponse, BYTE *const hwId, const char* const ipstr);
  239. size_t CreateResponseV4(REQUEST_V4 *const Request, BYTE *const response_data, const char* const ipstr);
  240. size_t CreateResponseV6(REQUEST_V6 *restrict Request, BYTE *const response_data, const char* const ipstr);
  241. BYTE *CreateRequestV4(size_t *size, const REQUEST* requestBase);
  242. BYTE *CreateRequestV6(size_t *size, const REQUEST* requestBase);
  243. void randomPidInit();
  244. void get16RandomBytes(void* ptr);
  245. RESPONSE_RESULT DecryptResponseV6(RESPONSE_V6* Response_v6, int responseSize, BYTE* const response, const BYTE* const request, BYTE* hwid);
  246. RESPONSE_RESULT DecryptResponseV4(RESPONSE_V4* Response_v4, const int responseSize, BYTE* const response, const BYTE* const request);
  247. void getUnixTimeAsFileTime(FILETIME *const ts);
  248. __pure int64_t fileTimeToUnixTime(const FILETIME *const ts);
  249. const char* getProductNameHE(const GUID *const guid, const KmsIdList *const List, ProdListIndex_t *const i);
  250. const char* getProductNameLE(const GUID *const guid, const KmsIdList *const List, ProdListIndex_t *const i);
  251. __pure ProdListIndex_t getExtendedProductListSize();
  252. __pure ProdListIndex_t getAppListSize(void);
  253. extern const KmsIdList ProductList[];
  254. extern const KmsIdList AppList[];
  255. extern const KmsIdList ExtendedProductList[];
  256. extern RequestCallback_t CreateResponseBase;
  257. #ifdef _PEDANTIC
  258. uint16_t IsValidLcid(const uint16_t Lcid);
  259. #endif // _PEDANTIC
  260. #endif // __kms_h