kms.h 9.2 KB

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