1
0

kms.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. #ifndef CONFIG
  2. #define CONFIG "config.h"
  3. #endif // CONFIG
  4. #include CONFIG
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE
  7. #endif
  8. #ifndef _CRT_SECURE_NO_WARNINGS
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #endif
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include <ctype.h>
  15. #include <time.h>
  16. #if !defined(_WIN32)
  17. #if !__ANDROID__
  18. #include <sys/shm.h>
  19. #endif // !__ANDROID__
  20. #include <sys/socket.h>
  21. #include <sys/ipc.h>
  22. #endif // !defined(_WIN32)
  23. #include "output.h"
  24. #include "crypto.h"
  25. #include "endian.h"
  26. #include "kms.h"
  27. #include "shared_globals.h"
  28. #include "helpers.h"
  29. #define FRIENDLY_NAME_WINDOWS "Windows"
  30. #define FRIENDLY_NAME_OFFICE2010 "Office 2010"
  31. #define FRIENDLY_NAME_OFFICE2013 "Office 2013+"
  32. #ifndef IS_LIBRARY
  33. #ifdef NO_LOG
  34. #define LOGTEXT(x) ""
  35. #else //!NO_LOG
  36. #define LOGTEXT(x) x
  37. #endif // !NO_LOG
  38. int32_t getProductIndex(const GUID* guid, const PVlmcsdData_t list, const int32_t count, char** name, char** ePid)
  39. {
  40. int i;
  41. for (i = count - 1; i >= 0; i--)
  42. {
  43. if (IsEqualGUID(guid, &list[i].Guid))
  44. {
  45. if (name) *name = list[i].Name;
  46. if (ePid) *ePid = KmsData->CsvlkData[list[i].EPidIndex].EPid;
  47. return i;
  48. }
  49. }
  50. if (name) *name = "Unknown";
  51. if (ePid) *ePid = KmsData->CsvlkData[EPID_INDEX_WINDOWS].EPid;
  52. return i;
  53. }
  54. #endif // IS_LIBRARY
  55. #ifndef NO_RANDOM_EPID
  56. static const uint16_t HostBuilds[] = { 6002, 7601, 9200, 9600, 14393, 17763 };
  57. // Valid language identifiers to be used in the ePID
  58. static const uint16_t LcidList[] = {
  59. 1078, 1052, 1025, 2049, 3073, 4097, 5121, 6145, 7169, 8193, 9217, 10241, 11265, 12289, 13313, 14337, 15361, 16385,
  60. 1067, 1068, 2092, 1069, 1059, 1093, 5146, 1026, 1027, 1028, 2052, 3076, 4100, 5124, 1050, 4122, 1029, 1030, 1125, 1043, 2067,
  61. 1033, 2057, 3081, 4105, 5129, 6153, 7177, 8201, 9225, 10249, 11273, 12297, 13321, 1061, 1080, 1065, 1035, 1036, 2060,
  62. 3084, 4108, 5132, 6156, 1079, 1110, 1031, 2055, 3079, 4103, 5127, 1032, 1095, 1037, 1081, 1038, 1039, 1057, 1040, 2064, 1041, 1099,
  63. 1087, 1111, 1042, 1088, 1062, 1063, 1071, 1086, 2110, 1100, 1082, 1153, 1102, 1104, 1044, 2068, 1045, 1046, 2070,
  64. 1094, 1131, 2155, 3179, 1048, 1049, 9275, 4155, 5179, 3131, 1083, 2107, 8251, 6203, 7227, 1103, 2074, 6170, 3098,
  65. 7194, 1051, 1060, 1034, 2058, 3082, 4106, 5130, 6154, 7178, 8202, 9226, 10250, 11274, 12298, 13322, 14346, 15370, 16394,
  66. 17418, 18442, 19466, 20490, 1089, 1053, 2077, 1114, 1097, 1092, 1098, 1054, 1074, 1058, 1056, 1091, 2115, 1066, 1106, 1076, 1077
  67. };
  68. uint16_t getPlatformId(uint16_t hostBuild)
  69. {
  70. if (hostBuild < 9000) return 55041;
  71. if (hostBuild <= 9500) return 5426;
  72. if (hostBuild <= 10000) return 6401;
  73. return 3612;
  74. }
  75. #ifdef _PEDANTIC
  76. uint16_t IsValidLcid(const uint16_t lcid)
  77. {
  78. uint16_t i;
  79. for (i = 0; i < vlmcsd_countof(LcidList); i++)
  80. {
  81. if (lcid == LcidList[i]) return lcid;
  82. }
  83. return 0;
  84. }
  85. uint16_t IsValidHostBuild(const uint16_t hostBuild)
  86. {
  87. const uint16_t *hostOS;
  88. for (hostOS = HostBuilds; hostOS < HostBuilds + vlmcsd_countof(HostBuilds); hostOS++)
  89. {
  90. if (hostBuild == *hostOS) return hostBuild;
  91. }
  92. return 0;
  93. }
  94. #endif // _PEDANTIC
  95. #endif // NO_RANDOM_EPID
  96. // Unix time is seconds from 1970-01-01. Should be 64 bits to avoid year 2038 overflow bug.
  97. // FILETIME is 100 nanoseconds from 1601-01-01. Must be 64 bits.
  98. void getUnixTimeAsFileTime(FILETIME *const ts)
  99. {
  100. const int64_t unixtime = (int64_t)time(NULL);
  101. int64_t *filetime = (int64_t*)ts;
  102. PUT_UA64LE(filetime, (unixtime + 11644473600LL) * 10000000LL);
  103. }
  104. __pure int64_t fileTimeToUnixTime(const FILETIME *const ts)
  105. {
  106. return GET_UA64LE(ts) / 10000000LL - 11644473600LL;
  107. }
  108. #ifndef NO_STRICT_MODES
  109. #ifndef NO_CLIENT_LIST
  110. static PClientList_t ClientLists;
  111. static BYTE ZeroGuid[16] = { 0 };
  112. #if !defined(_WIN32) && !defined(__CYGWIN__)
  113. pthread_mutex_t* mutex;
  114. #define mutex_size (((sizeof(pthread_mutex_t)+7)>>3)<<3)
  115. #else
  116. CRITICAL_SECTION* mutex;
  117. #define mutex_size (((sizeof(CRITICAL_SECTION)+7)>>3)<<3)
  118. #endif // _WIN32
  119. #ifndef USE_THREADS
  120. static int shmid_clients = -1;
  121. #endif // USE_THREADS
  122. #if !defined(_WIN32) && !defined(__CYGWIN__)
  123. #define lock_client_lists() pthread_mutex_lock(mutex)
  124. #define unlock_client_lists() pthread_mutex_unlock(mutex)
  125. #define mutex_t pthread_mutex_t
  126. #else
  127. #define lock_client_lists() EnterCriticalSection(mutex)
  128. #define unlock_client_lists() LeaveCriticalSection(mutex)
  129. #define mutex_t CRITICAL_SECTION
  130. #endif
  131. void CleanUpClientLists()
  132. {
  133. # ifndef USE_THREADS
  134. shmctl(shmid_clients, IPC_RMID, NULL);
  135. # endif // !USE_THREADS
  136. }
  137. void InitializeClientLists()
  138. {
  139. int_fast8_t i;
  140. int_fast16_t j;
  141. # ifndef USE_THREADS
  142. if (
  143. (shmid_clients = shmget(IPC_PRIVATE, sizeof(ClientList_t) * KmsData->AppItemCount + mutex_size, IPC_CREAT | 0600)) < 0 ||
  144. (mutex = (mutex_t*)shmat(shmid_clients, NULL, 0)) == (mutex_t*)-1
  145. )
  146. {
  147. int errno_save = errno;
  148. printerrorf("Warning: CMID lists disabled. Could not create shared memory: %s\n", vlmcsd_strerror(errno_save));
  149. if (shmid_clients >= 0) shmctl(shmid_clients, IPC_RMID, NULL);
  150. MaintainClients = FALSE;
  151. return;
  152. }
  153. ClientLists = (PClientList_t)((BYTE*)mutex + mutex_size);
  154. # if __CYGWIN__
  155. InitializeCriticalSection(mutex);
  156. # else // !__CYGWIN__
  157. pthread_mutexattr_t mutex_attr;
  158. pthread_mutexattr_init(&mutex_attr);
  159. pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
  160. pthread_mutex_init(mutex, &mutex_attr);
  161. # endif // !__CYGWIN__
  162. # else // USE_THREADS
  163. ClientLists = (PClientList_t)vlmcsd_malloc(sizeof(ClientList_t) * KmsData->AppItemCount);
  164. mutex = (mutex_t*)vlmcsd_malloc(sizeof(mutex_t));
  165. # if !_WIN32 && !__CYGWIN__
  166. pthread_mutex_init(mutex, NULL);
  167. # else //_WIN32 || __CYGWIN__
  168. InitializeCriticalSection(mutex);
  169. # endif //_WIN32 || __CYGWIN__
  170. # endif // USE_THREADS
  171. memset(ClientLists, 0, sizeof(ClientList_t) * KmsData->AppItemCount);
  172. if (!StartEmpty)
  173. {
  174. for (i = 0; i < KmsData->AppItemCount; i++)
  175. {
  176. const uint8_t maxCount = KmsData->AppItemList[i].NCountPolicy;
  177. ClientLists[i].CurrentCount = (maxCount >> 1) - 1;
  178. ClientLists[i].MaxCount = maxCount;
  179. for (j = 0; j < (maxCount >> 1) - 1; j++)
  180. {
  181. get16RandomBytes(&ClientLists[i].Guid[j]);
  182. }
  183. }
  184. }
  185. }
  186. #endif // NO_CLIENT_LIST
  187. #endif // !NO_STRICT_MODES
  188. #ifndef NO_RANDOM_EPID
  189. // formats an int with a fixed number of digits with leading zeros (helper for ePID generation)
  190. static char* itoc(char *const c, const int i, uint_fast8_t digits)
  191. {
  192. char formatString[8];
  193. if (digits > 9) digits = 0;
  194. strcpy(formatString, "%");
  195. if (digits)
  196. {
  197. formatString[1] = '0';
  198. formatString[2] = digits | 0x30;
  199. formatString[3] = 0;
  200. }
  201. strcat(formatString, "u");
  202. sprintf(c, formatString, i);
  203. return c;
  204. }
  205. static uint8_t getRandomServerType()
  206. {
  207. # if defined(USE_MSRPC) || defined(SIMPLE_RPC)
  208. return rand() % vlmcsd_countof(HostBuilds);
  209. # else // !defined(USE_MSRPC) && !defined(SIMPLE_RPC)
  210. if (!UseServerRpcBTFN)
  211. {
  212. // This isn't possible at all, e.g. KMS host on XP
  213. return rand() % (int)vlmcsd_countof(HostBuilds);
  214. }
  215. // return 9200/9600/14393/17763 if NDR64 is in use, otherwise 6002/7601
  216. if (UseServerRpcNDR64) return rand() % ((int)vlmcsd_countof(HostBuilds) - 2) + 2;
  217. return rand() % 2;
  218. # endif // !defined(USE_MSRPC) && !defined(SIMPLE_RPC)
  219. }
  220. /*
  221. * Generates a random ePID
  222. */
  223. static void generateRandomPid(const int index, char *const szPid, int16_t lang, uint16_t hostBuild)
  224. {
  225. char numberBuffer[12];
  226. if (!hostBuild)
  227. {
  228. hostBuild = HostBuilds[getRandomServerType()];
  229. }
  230. strcpy(szPid, itoc(numberBuffer, getPlatformId(hostBuild), 5));
  231. strcat(szPid, "-");
  232. //if (index > 3) index = 0;
  233. const PCsvlkData_t csvlkData = &KmsData->CsvlkData[index];
  234. strcat(szPid, itoc(numberBuffer, csvlkData->GroupId, 5));
  235. strcat(szPid, "-");
  236. const int keyId = (rand32() % (csvlkData->MaxKeyId - csvlkData->MinKeyId)) + csvlkData->MinKeyId;
  237. strcat(szPid, itoc(numberBuffer, keyId / 1000000, 3));
  238. strcat(szPid, "-");
  239. strcat(szPid, itoc(numberBuffer, keyId % 1000000, 6));
  240. strcat(szPid, "-03-");
  241. if (lang < 1) lang = LcidList[rand() % vlmcsd_countof(LcidList)];
  242. strcat(szPid, itoc(numberBuffer, lang, 0));
  243. strcat(szPid, "-");
  244. strcat(szPid, itoc(numberBuffer, hostBuild, 0));
  245. strcat(szPid, ".0000-");
  246. # define minTime ((time_t)1538490811) /* Release Date Win 2019 */
  247. time_t maxTime;
  248. time(&maxTime);
  249. # ifndef BUILD_TIME
  250. # define BUILD_TIME 1538922811
  251. # endif
  252. if (maxTime < (time_t)BUILD_TIME) // Just in case the system time is < 10/17/2013 1:00 pm
  253. maxTime = (time_t)BUILD_TIME;
  254. time_t kmsTime = (rand32() % (maxTime - minTime)) + minTime;
  255. struct tm *pidTime = gmtime(&kmsTime);
  256. strcat(szPid, itoc(numberBuffer, pidTime->tm_yday + 1, 3));
  257. strcat(szPid, itoc(numberBuffer, pidTime->tm_year + 1900, 4));
  258. }
  259. /*
  260. * Generates random ePIDs and stores them if not already read from ini file.
  261. * For use with randomization level 1
  262. */
  263. void randomPidInit()
  264. {
  265. uint32_t i;
  266. const int16_t lang = Lcid ? Lcid : LcidList[rand() % vlmcsd_countof(LcidList)];
  267. for (i = 0; i < KmsData->CsvlkCount; i++)
  268. {
  269. if (KmsResponseParameters[i].Epid) continue;
  270. char Epid[PID_BUFFER_SIZE];
  271. generateRandomPid(i, Epid, lang, HostBuild);
  272. KmsResponseParameters[i].Epid = (const char*)vlmcsd_strdup(Epid);
  273. #ifndef NO_LOG
  274. KmsResponseParameters[i].EpidSource = "randomized at program start";
  275. #endif // NO_LOG
  276. }
  277. }
  278. #endif // NO_RANDOM_EPID
  279. #ifndef NO_LOG
  280. static int32_t getProductIndexFromAllLists(const GUID* guid, char** productName)
  281. {
  282. return getProductIndex(guid, KmsData->AppItemList, KmsData->AppItemCount + KmsData->KmsItemCount + KmsData->SkuItemCount, productName, NULL);
  283. }
  284. /*
  285. * Logs a Request
  286. */
  287. static void logRequest(const REQUEST *const baseRequest)
  288. {
  289. #ifndef NO_VERBOSE_LOG
  290. if (logverbose)
  291. {
  292. logger("<<< Incoming KMS request\n");
  293. logRequestVerbose(baseRequest, &logger);
  294. return;
  295. }
  296. #endif // NO_VERBOSE_LOG
  297. char *productName;
  298. char clientname[64];
  299. int32_t index = getProductIndexFromAllLists(&baseRequest->ActID, &productName);
  300. if (index < 0) index = getProductIndexFromAllLists(&baseRequest->KMSID, &productName);
  301. if (index < 0) index = getProductIndexFromAllLists(&baseRequest->AppID, &productName);
  302. if (index < 0 || !strcasecmp(productName, "Unknown"))
  303. {
  304. productName = (char*)alloca(GUID_STRING_LENGTH + 1);
  305. uuid2StringLE(&baseRequest->ActID, productName);
  306. }
  307. ucs2_to_utf8(baseRequest->WorkstationName, clientname, 64, 64);
  308. logger("KMS v%i.%i request from %s for %s\n", LE16(baseRequest->MajorVer), LE16(baseRequest->MinorVer), clientname, productName);
  309. }
  310. #endif // NO_LOG
  311. /*
  312. * Converts a utf-8 ePID string to UCS-2 and writes it to a RESPONSE struct
  313. */
  314. #ifndef IS_LIBRARY
  315. static void getEpidFromString(RESPONSE *const Response, const char *const pid)
  316. {
  317. const size_t length = utf8_to_ucs2(Response->KmsPID, pid, PID_BUFFER_SIZE, PID_BUFFER_SIZE * 3);
  318. Response->PIDSize = LE32(((unsigned int)length + 1) << 1);
  319. }
  320. /*
  321. * get ePID from appropriate source
  322. */
  323. static void getEpid(RESPONSE *const baseResponse, const char** EpidSource, const int32_t index, BYTE *const HwId, const char* defaultEPid)
  324. {
  325. #if !defined(NO_RANDOM_EPID) || !defined(NO_CL_PIDS) || !defined(NO_INI_FILE)
  326. const char* pid;
  327. if (KmsResponseParameters[index].Epid == NULL)
  328. {
  329. #ifndef NO_RANDOM_EPID
  330. if (RandomizationLevel == 2)
  331. {
  332. char ePid[PID_BUFFER_SIZE];
  333. generateRandomPid(index, ePid, Lcid, HostBuild);
  334. pid = ePid;
  335. #ifndef NO_LOG
  336. *EpidSource = "randomized on every request";
  337. #endif // NO_LOG
  338. }
  339. else
  340. #endif // NO_RANDOM_EPID
  341. {
  342. pid = defaultEPid;
  343. #ifndef NO_LOG
  344. *EpidSource = "vlmcsd default";
  345. #endif // NO_LOG
  346. }
  347. }
  348. else
  349. {
  350. pid = KmsResponseParameters[index].Epid;
  351. if (HwId && KmsResponseParameters[index].HwId != NULL)
  352. memcpy(HwId, KmsResponseParameters[index].HwId, sizeof(((RESPONSE_V6 *)0)->HwId));
  353. #ifndef NO_LOG
  354. *EpidSource = KmsResponseParameters[index].EpidSource;
  355. #endif // NO_LOG
  356. }
  357. getEpidFromString(baseResponse, pid);
  358. #else // defined(NO_RANDOM_EPID) && defined(NO_CL_PIDS) && !defined(NO_INI_FILE)
  359. getEpidFromString(baseResponse, defaultEPid);
  360. # ifndef NO_LOG
  361. *EpidSource = "vlmcsd default";
  362. # endif // NO_LOG
  363. #endif // defined(NO_RANDOM_EPID) && defined(NO_CL_PIDS) && !defined(NO_INI_FILE)
  364. }
  365. #endif // IS_LIBRARY
  366. #if !defined(NO_LOG) && defined(_PEDANTIC)
  367. static BOOL CheckVersion4Uuid(const GUID *const guid, const char *const szGuidName)
  368. {
  369. if (LE16(guid->Data3) >> 12 != 4 || guid->Data4[0] >> 6 != 2)
  370. {
  371. logger("Warning: %s does not conform to version 4 UUID according to RFC 4122\n", szGuidName);
  372. return FALSE;
  373. }
  374. return TRUE;
  375. }
  376. static void CheckRequest(const REQUEST *const Request)
  377. {
  378. CheckVersion4Uuid(&Request->CMID, "Client machine ID");
  379. CheckVersion4Uuid(&Request->AppID, "Application ID");
  380. CheckVersion4Uuid(&Request->KMSID, "Server SKU ID");
  381. CheckVersion4Uuid(&Request->ActID, "Client SKU ID");
  382. if (LE32(Request->IsClientVM) > 1)
  383. logger("Warning: Virtual Machine field in request must be 0 or 1 but is %u\n", LE32(Request->IsClientVM));
  384. if (LE32(Request->LicenseStatus) > 6)
  385. logger("Warning: License status must be between 0 and 6 but is %u\n", LE32(Request->LicenseStatus));
  386. }
  387. #endif // !defined(NO_LOG) && defined(_PEDANTIC)
  388. #ifndef NO_LOG
  389. /*
  390. * Logs the Response
  391. */
  392. static void logResponse(const RESPONSE *const baseResponse, const BYTE *const hwId, const char *const EpidSource)
  393. {
  394. char utf8pid[PID_BUFFER_SIZE * 3];
  395. ucs2_to_utf8(baseResponse->KmsPID, utf8pid, PID_BUFFER_SIZE, PID_BUFFER_SIZE * 3);
  396. #ifndef NO_VERBOSE_LOG
  397. if (!logverbose)
  398. {
  399. #endif // NO_VERBOSE_LOG
  400. logger("Sending ePID (%s): %s\n", EpidSource, utf8pid);
  401. #ifndef NO_VERBOSE_LOG
  402. }
  403. else
  404. {
  405. logger(">>> Sending response, ePID source = %s\n", EpidSource);
  406. logResponseVerbose(utf8pid, hwId, baseResponse, &logger);
  407. }
  408. #endif // NO_VERBOSE_LOG
  409. }
  410. #endif
  411. #if __UCLIBC__ && !defined(NO_STRICT_MODES)
  412. long long int llabs(long long int j);
  413. #endif
  414. /*
  415. * Creates the unencrypted base response
  416. */
  417. #ifndef IS_LIBRARY
  418. static HRESULT __stdcall CreateResponseBaseCallback(const REQUEST *const baseRequest, RESPONSE *const baseResponse, BYTE *const hwId, const char* const ipstr_unused)
  419. {
  420. const char* EpidSource;
  421. #ifndef NO_LOG
  422. logRequest(baseRequest);
  423. #ifdef _PEDANTIC
  424. CheckRequest(baseRequest);
  425. #endif // _PEDANTIC
  426. #endif // NO_LOG
  427. char* ePid;
  428. const DWORD minClients = LE32(baseRequest->N_Policy);
  429. const DWORD required_clients = minClients < 1 ? 1 : minClients << 1;
  430. int32_t index = getProductIndex(&baseRequest->KMSID, KmsData->KmsItemList, KmsData->KmsItemCount, NULL, &ePid);
  431. # ifndef NO_STRICT_MODES
  432. if (required_clients > 2000)
  433. {
  434. # ifndef NO_LOG
  435. logger("Rejecting request with more than 1000 minimum clients (0x8007000D)\n");
  436. # endif
  437. return 0x8007000D;
  438. }
  439. if (CheckClientTime)
  440. {
  441. const time_t requestTime = (time_t)fileTimeToUnixTime(&baseRequest->ClientTime);
  442. if (llabs(requestTime - time(NULL)) > 60 * 60 * 4)
  443. {
  444. # ifndef NO_LOG
  445. logger("Client time differs more than 4 hours from system time (0xC004F06C)\n");
  446. # endif // !NO_LOG
  447. return 0xC004F06C;
  448. }
  449. }
  450. if (WhitelistingLevel & 2)
  451. {
  452. if (index >= 0 && (KmsData->KmsItemList[index].IsPreview || KmsData->KmsItemList[index].IsRetail))
  453. {
  454. # ifndef NO_LOG
  455. logger("Refusing retail or beta product (0xC004F042)\n");
  456. # endif // !NO_LOG
  457. return 0xC004F042;
  458. }
  459. }
  460. if ((WhitelistingLevel & 1) && index < 0)
  461. {
  462. # ifndef NO_LOG
  463. logger("Refusing unknown product (0xC004F042)\n");
  464. # endif // !NO_LOG
  465. return 0xC004F042;
  466. }
  467. # ifndef NO_CLIENT_LIST
  468. const int32_t appIndex = index < 0 ? 0 : KmsData->KmsItemList[index].AppIndex;
  469. # endif // NO_CLIENT_LIST
  470. # endif // !NO_STRICT_MODES
  471. const int32_t ePidIndex = index < 0 ? 0 : KmsData->KmsItemList[index].EPidIndex;
  472. # if !defined(NO_STRICT_MODES)
  473. if ((WhitelistingLevel & 1) && index >= 0 && !IsEqualGUID(&KmsData->AppItemList[KmsData->KmsItemList[index].AppIndex].Guid, &baseRequest->AppID))
  474. {
  475. # ifndef NO_LOG
  476. logger("Refusing product with incorrect Application ID (0xC004F042)\n");
  477. # endif // NO_LOG
  478. return 0xC004F042;
  479. }
  480. # ifndef NO_CLIENT_LIST
  481. if (MaintainClients)
  482. {
  483. lock_client_lists();
  484. int_fast16_t i;
  485. int_fast8_t isKnownClient = FALSE;
  486. if (required_clients > (DWORD)ClientLists[appIndex].MaxCount) ClientLists[appIndex].MaxCount = required_clients;
  487. for (i = 0; i < ClientLists[appIndex].MaxCount; i++)
  488. {
  489. if (IsEqualGUID(&ClientLists[appIndex].Guid[i], &baseRequest->CMID))
  490. {
  491. isKnownClient = TRUE;
  492. break;
  493. }
  494. }
  495. if (isKnownClient)
  496. {
  497. baseResponse->Count = LE32(ClientLists[appIndex].CurrentCount);
  498. }
  499. else
  500. {
  501. for (i = 0; i < ClientLists[appIndex].MaxCount; i++)
  502. {
  503. if (IsEqualGUID(ZeroGuid, &ClientLists[appIndex].Guid[i]))
  504. {
  505. if (ClientLists[appIndex].CurrentCount >= MAX_CLIENTS)
  506. {
  507. # ifndef NO_LOG
  508. logger("Rejecting more than 671 clients (0xC004D104)\n");
  509. # endif // !NO_LOG
  510. unlock_client_lists();
  511. return 0xC004D104;
  512. }
  513. baseResponse->Count = LE32(++ClientLists[appIndex].CurrentCount);
  514. memcpy(&ClientLists[appIndex].Guid[i], &baseRequest->CMID, sizeof(GUID));
  515. break;
  516. }
  517. }
  518. if (i >= ClientLists[appIndex].MaxCount)
  519. {
  520. memcpy(&ClientLists[appIndex].Guid[ClientLists[appIndex].CurrentPosition], &baseRequest->CMID, sizeof(GUID));
  521. ClientLists[appIndex].CurrentPosition = (ClientLists[appIndex].CurrentPosition + 1) % (ClientLists[appIndex].MaxCount > MAX_CLIENTS ? MAX_CLIENTS : ClientLists[appIndex].MaxCount);
  522. baseResponse->Count = LE32(ClientLists[appIndex].CurrentCount);
  523. }
  524. }
  525. unlock_client_lists();
  526. }
  527. else
  528. # endif // !NO_CLIENT_LIST
  529. # endif // !defined(NO_STRICT_MODES)
  530. {
  531. const uint8_t minimum_answer_clients = (uint8_t)KmsData->CsvlkData[ePidIndex].MinActiveClients;
  532. baseResponse->Count = LE32(required_clients > minimum_answer_clients ? required_clients : minimum_answer_clients);
  533. //if (LE32(baseRequest->N_Policy) > LE32(baseResponse->Count)) baseResponse->Count = LE32(LE32(baseRequest->N_Policy) << 1);
  534. }
  535. getEpid(baseResponse, &EpidSource, ePidIndex, hwId, ePid);
  536. baseResponse->Version = baseRequest->Version;
  537. memcpy(&baseResponse->CMID, &baseRequest->CMID, sizeof(GUID));
  538. memcpy(&baseResponse->ClientTime, &baseRequest->ClientTime, sizeof(FILETIME));
  539. baseResponse->VLActivationInterval = LE32(VLActivationInterval);
  540. baseResponse->VLRenewalInterval = LE32(VLRenewalInterval);
  541. #ifndef NO_LOG
  542. logResponse(baseResponse, hwId, EpidSource);
  543. #endif // NO_LOG
  544. return S_OK;
  545. }
  546. RequestCallback_t CreateResponseBase = &CreateResponseBaseCallback;
  547. #else // IS_LIBRARY
  548. RequestCallback_t CreateResponseBase = NULL;
  549. #endif // IS_LIBRARY
  550. ////TODO: Move to helpers.c
  551. void get16RandomBytes(void* ptr)
  552. {
  553. int i;
  554. for (i = 0; i < 4; i++) ((DWORD*)ptr)[i] = rand32();
  555. }
  556. /*
  557. * Creates v4 response
  558. */
  559. size_t CreateResponseV4(REQUEST_V4 *const request_v4, BYTE *const responseBuffer, const char* const ipstr)
  560. {
  561. RESPONSE_V4* Response = (RESPONSE_V4*)responseBuffer;
  562. HRESULT hResult;
  563. if (FAILED(hResult = CreateResponseBase(&request_v4->RequestBase, &Response->ResponseBase, NULL, ipstr))) return hResult;
  564. const DWORD pidSize = LE32(Response->ResponseBase.PIDSize);
  565. BYTE* postEpidPtr = responseBuffer + V4_PRE_EPID_SIZE + pidSize;
  566. memmove(postEpidPtr, &Response->ResponseBase.CMID, V4_POST_EPID_SIZE);
  567. const size_t encryptSize = V4_PRE_EPID_SIZE + V4_POST_EPID_SIZE + pidSize;
  568. AesCmacV4(responseBuffer, encryptSize, responseBuffer + encryptSize);
  569. return encryptSize + sizeof(Response->MAC);
  570. }
  571. /*
  572. // Workaround for buggy GCC 4.2/4.3
  573. #if defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ < 4)
  574. __attribute__((noinline))
  575. #endif
  576. __pure static uint64_t TimestampInterval(void *ts)
  577. {
  578. return ( GET_UA64LE(ts) / TIME_C1 ) * TIME_C2 + TIME_C3;
  579. }*/
  580. /*
  581. * Creates the HMAC for v6
  582. */
  583. static int_fast8_t CreateV6Hmac(BYTE *const encrypt_start, const size_t encryptSize, int_fast8_t tolerance)
  584. {
  585. BYTE hash[32];
  586. const uint8_t halfHashSize = sizeof(hash) >> 1;
  587. BYTE *responseEnd = encrypt_start + encryptSize;
  588. // This is the time from the response
  589. FILETIME* ft = (FILETIME*)(responseEnd - V6_POST_EPID_SIZE + sizeof(((RESPONSE*)0)->CMID));
  590. // Generate a time slot that changes every 4.11 hours.
  591. // Request and repsonse time must match +/- 1 slot.
  592. // When generating a response tolerance must be 0.
  593. // If verifying the hash, try tolerance -1, 0 and +1. One of them must match.
  594. uint64_t timeSlot = LE64((GET_UA64LE(ft) / TIME_C1 * TIME_C2 + TIME_C3) + (tolerance * TIME_C1));
  595. // The time slot is hashed with SHA256 so it is not so obvious that it is time
  596. Sha256((BYTE*)&timeSlot, sizeof(timeSlot), hash);
  597. // The last 16 bytes of the hashed time slot are the actual HMAC key
  598. if (!Sha256Hmac
  599. (
  600. hash + halfHashSize, // Use last 16 bytes of SHA256 as HMAC key
  601. encrypt_start, // hash only the encrypted part of the v6 response
  602. (DWORD)(encryptSize - sizeof(((RESPONSE_V6*)0)->HMAC)), // encryptSize minus the HMAC itself
  603. hash // use same buffer for resulting hash where the key came from
  604. ))
  605. {
  606. return FALSE;
  607. }
  608. memcpy(responseEnd - sizeof(((RESPONSE_V6*)0)->HMAC), hash + halfHashSize, halfHashSize);
  609. return TRUE;
  610. }
  611. /*
  612. * Creates v5 or v6 response
  613. */
  614. size_t CreateResponseV6(REQUEST_V6 *restrict request_v6, BYTE *const responseBuffer, const char* const ipstr)
  615. {
  616. // The response will be created in a fixed sized struct to
  617. // avoid unaligned access macros and packed structs on RISC systems
  618. // which largely increase code size.
  619. //
  620. // The fixed sized struct with 64 WCHARs for the ePID will be converted
  621. // to a variable sized struct later and requires unaligned access macros.
  622. RESPONSE_V6* Response = (RESPONSE_V6*)responseBuffer;
  623. RESPONSE* baseResponse = &Response->ResponseBase;
  624. #ifdef _DEBUG
  625. // ReSharper disable once CppEntityNeverUsed
  626. RESPONSE_V6_DEBUG* xxx_unused = (RESPONSE_V6_DEBUG*)responseBuffer;
  627. #endif
  628. static const BYTE DefaultHwid[8] = { HWID };
  629. const int_fast8_t v6 = LE16(request_v6->MajorVer) > 5;
  630. AesCtx aesCtx;
  631. AesInitKey(&aesCtx, v6 ? AesKeyV6 : AesKeyV5, v6, AES_KEY_BYTES);
  632. AesDecryptCbc(&aesCtx, NULL, request_v6->IV, V6_DECRYPT_SIZE);
  633. // get random salt and SHA256 it
  634. get16RandomBytes(Response->RandomXoredIVs);
  635. Sha256(Response->RandomXoredIVs, sizeof(Response->RandomXoredIVs), Response->Hash);
  636. if (v6) // V6 specific stuff
  637. {
  638. // In v6 a random IV is generated
  639. Response->Version = request_v6->Version;
  640. get16RandomBytes(Response->IV);
  641. // pre-fill with default HwId (not required for v5)
  642. memcpy(Response->HwId, DefaultHwid, sizeof(Response->HwId));
  643. // Just copy decrypted request IV (using Null IV) here. Note this is identical
  644. // to XORing non-decrypted request and reponse IVs
  645. memcpy(Response->XoredIVs, request_v6->IV, sizeof(Response->XoredIVs));
  646. }
  647. else // V5 specific stuff
  648. {
  649. // In v5 IVs of request and response must be identical (MS client checks this)
  650. // The following memcpy copies Version and IVs at once
  651. memcpy(Response, request_v6, V6_UNENCRYPTED_SIZE);
  652. }
  653. // Xor Random bytes with decrypted request IV
  654. XorBlock(request_v6->IV, Response->RandomXoredIVs);
  655. // Get the base response
  656. HRESULT hResult;
  657. if (FAILED(hResult = CreateResponseBase(&request_v6->RequestBase, baseResponse, Response->HwId, ipstr))) return hResult;
  658. // Convert the fixed sized struct into variable sized
  659. const DWORD pidSize = LE32(baseResponse->PIDSize);
  660. BYTE* postEpidPtr = responseBuffer + V6_PRE_EPID_SIZE + pidSize;
  661. const size_t post_epid_size = v6 ? V6_POST_EPID_SIZE : V5_POST_EPID_SIZE;
  662. memmove(postEpidPtr, &baseResponse->CMID, post_epid_size);
  663. // number of bytes to encrypt
  664. size_t encryptSize =
  665. V6_PRE_EPID_SIZE
  666. - sizeof(Response->Version)
  667. + pidSize
  668. + post_epid_size;
  669. //AesDecryptBlock(&aesCtx, Response->IV);
  670. if (v6 && !CreateV6Hmac(Response->IV, encryptSize, 0)) return 0;
  671. // Padding auto handled by encryption func
  672. AesEncryptCbc(&aesCtx, NULL, Response->IV, &encryptSize);
  673. return encryptSize + sizeof(Response->Version);
  674. }
  675. // Create Hashed KMS Client Request Data for KMS Protocol Version 4
  676. BYTE *CreateRequestV4(size_t *size, const REQUEST* requestBase)
  677. {
  678. *size = sizeof(REQUEST_V4);
  679. // Build a proper KMS client request data
  680. BYTE *request = (BYTE *)vlmcsd_malloc(sizeof(REQUEST_V4));
  681. // Temporary Pointer for access to REQUEST_V4 structure
  682. REQUEST_V4 *request_v4 = (REQUEST_V4 *)request;
  683. // Set KMS Client Request Base
  684. memcpy(&request_v4->RequestBase, requestBase, sizeof(REQUEST));
  685. // Generate Hash Signature
  686. AesCmacV4(request, sizeof(REQUEST), request_v4->MAC);
  687. // Return Request Data
  688. return request;
  689. }
  690. // Create Encrypted KMS Client Request Data for KMS Protocol Version 6
  691. BYTE* CreateRequestV6(size_t *size, const REQUEST* requestBase)
  692. {
  693. *size = sizeof(REQUEST_V6);
  694. // Temporary Pointer for access to REQUEST_V5 structure
  695. REQUEST_V6 *request = (REQUEST_V6 *)vlmcsd_malloc(sizeof(REQUEST_V6));
  696. // KMS Protocol Version
  697. request->Version = requestBase->Version;
  698. // Initialize the IV
  699. get16RandomBytes(request->IV);
  700. // Set KMS Client Request Base
  701. memcpy(&request->RequestBase, requestBase, sizeof(REQUEST));
  702. // Encrypt KMS Client Request
  703. size_t encryptSize = sizeof(request->RequestBase);
  704. AesCtx Ctx;
  705. int_fast8_t v6 = LE16(request->MajorVer) > 5;
  706. AesInitKey(&Ctx, v6 ? AesKeyV6 : AesKeyV5, v6, 16);
  707. AesEncryptCbc(&Ctx, request->IV, (BYTE*)(&request->RequestBase), &encryptSize);
  708. // Return Proper Request Data
  709. return (BYTE*)request;
  710. }
  711. /*
  712. * Checks whether Length of ePID is valid
  713. */
  714. static uint8_t checkPidLength(const RESPONSE *const responseBase)
  715. {
  716. unsigned int i;
  717. if (LE32(responseBase->PIDSize) > (PID_BUFFER_SIZE << 1)) return FALSE;
  718. if (responseBase->KmsPID[(LE32(responseBase->PIDSize) >> 1) - 1]) return FALSE;
  719. for (i = 0; i < (LE32(responseBase->PIDSize) >> 1) - 2; i++)
  720. {
  721. if (!responseBase->KmsPID[i]) return FALSE;
  722. }
  723. return TRUE;
  724. }
  725. /*
  726. * "Decrypts" a KMS v4 response. Actually just copies to a fixed size buffer
  727. */
  728. RESPONSE_RESULT DecryptResponseV4(RESPONSE_V4* response_v4, const int responseSize, BYTE* const rawResponse, const BYTE* const rawRequest)
  729. {
  730. const int copySize =
  731. V4_PRE_EPID_SIZE +
  732. (LE32(((RESPONSE_V4*)rawResponse)->ResponseBase.PIDSize) <= PID_BUFFER_SIZE << 1 ?
  733. LE32(((RESPONSE_V4*)rawResponse)->ResponseBase.PIDSize) :
  734. PID_BUFFER_SIZE << 1);
  735. const int messageSize = copySize + V4_POST_EPID_SIZE;
  736. memcpy(response_v4, rawResponse, copySize);
  737. memcpy(&response_v4->ResponseBase.CMID, rawResponse + copySize, responseSize - copySize);
  738. // ensure PID is null terminated
  739. response_v4->ResponseBase.KmsPID[PID_BUFFER_SIZE - 1] = 0;
  740. uint8_t* mac = rawResponse + messageSize;
  741. AesCmacV4(rawResponse, messageSize, mac);
  742. REQUEST_V4* request_v4 = (REQUEST_V4*)rawRequest;
  743. RESPONSE_RESULT result;
  744. result.mask = (DWORD)~0;
  745. result.PidLengthOK = checkPidLength((RESPONSE*)rawResponse);
  746. result.VersionOK = response_v4->ResponseBase.Version == request_v4->RequestBase.Version;
  747. result.HashOK = !memcmp(&response_v4->MAC, mac, sizeof(response_v4->MAC));
  748. result.TimeStampOK = !memcmp(&response_v4->ResponseBase.ClientTime, &request_v4->RequestBase.ClientTime, sizeof(FILETIME));
  749. result.ClientMachineIDOK = !memcmp(&response_v4->ResponseBase.CMID, &request_v4->RequestBase.CMID, sizeof(GUID));
  750. result.effectiveResponseSize = responseSize;
  751. result.correctResponseSize = sizeof(RESPONSE_V4) - sizeof(response_v4->ResponseBase.KmsPID) + LE32(response_v4->ResponseBase.PIDSize);
  752. return result;
  753. }
  754. static RESPONSE_RESULT VerifyResponseV6(RESPONSE_RESULT result, RESPONSE_V6* response_v6, REQUEST_V6* request_v6, BYTE* const rawResponse)
  755. {
  756. // Check IVs
  757. result.IVsOK = !memcmp // In V6 the XoredIV is actually the request IV
  758. (
  759. response_v6->XoredIVs,
  760. request_v6->IV,
  761. sizeof(response_v6->XoredIVs)
  762. );
  763. result.IVnotSuspicious = !!memcmp // If IVs are identical, it is obviously an emulator
  764. (
  765. request_v6->IV,
  766. response_v6->IV,
  767. sizeof(request_v6->IV)
  768. );
  769. // Check Hmac
  770. int_fast8_t tolerance;
  771. BYTE OldHmac[sizeof(response_v6->HMAC)];
  772. result.HmacSha256OK = FALSE;
  773. memcpy // Save received HMAC to compare with calculated HMAC later
  774. (
  775. OldHmac,
  776. response_v6->HMAC,
  777. sizeof(response_v6->HMAC)
  778. );
  779. //AesEncryptBlock(Ctx, Response_v6->IV); // CreateV6Hmac needs original IV as received over the network
  780. for (tolerance = -1; tolerance < 2; tolerance++)
  781. {
  782. CreateV6Hmac
  783. (
  784. rawResponse + sizeof(response_v6->Version), // Pointer to start of the encrypted part of the response
  785. (size_t)result.correctResponseSize - sizeof(response_v6->Version), // size of the encrypted part
  786. tolerance // tolerance -1, 0, or +1
  787. );
  788. result.HmacSha256OK = !memcmp // Compare both HMACs
  789. (
  790. OldHmac,
  791. rawResponse + (size_t)result.correctResponseSize - sizeof(response_v6->HMAC),
  792. sizeof(OldHmac)
  793. );
  794. if (result.HmacSha256OK) break;
  795. }
  796. return result;
  797. }
  798. static RESPONSE_RESULT VerifyResponseV5(RESPONSE_RESULT result, REQUEST_V5* request_v5, RESPONSE_V5* response_v5)
  799. {
  800. // Check IVs: in V5 (and only v5) request and response IVs must match
  801. result.IVsOK = !memcmp(request_v5->IV, response_v5->IV, sizeof(request_v5->IV));
  802. // V5 has no Hmac, always set to TRUE
  803. result.HmacSha256OK = TRUE;
  804. return result;
  805. }
  806. /*
  807. * Decrypts a KMS v5 or v6 response received from a server.
  808. * hwid must supply a valid 16 byte buffer for v6. hwid is ignored in v5
  809. */
  810. RESPONSE_RESULT DecryptResponseV6(RESPONSE_V6* response_v6, int responseSize, BYTE* const response, const BYTE* const rawRequest, BYTE* hwid)
  811. {
  812. RESPONSE_RESULT result;
  813. result.mask = (DWORD)~0; // Set all bits in the results mask to 1. Assume success first.
  814. result.effectiveResponseSize = responseSize;
  815. int copySize1 =
  816. sizeof(response_v6->Version);
  817. // Decrypt KMS Server Response (encrypted part starts after RequestIV)
  818. responseSize -= copySize1;
  819. AesCtx Ctx;
  820. const int_fast8_t v6 = LE16(((RESPONSE_V6*)response)->MajorVer) > 5;
  821. AesInitKey(&Ctx, v6 ? AesKeyV6 : AesKeyV5, v6, AES_KEY_BYTES);
  822. AesDecryptCbc(&Ctx, NULL, response + copySize1, responseSize);
  823. // Check padding
  824. BYTE* lastPadByte = response + (size_t)result.effectiveResponseSize - 1;
  825. // Must be from 1 to 16
  826. if (!*lastPadByte || *lastPadByte > AES_BLOCK_BYTES)
  827. {
  828. result.DecryptSuccess = FALSE;
  829. return result;
  830. }
  831. // Check if pad bytes are all the same
  832. BYTE* padByte;
  833. for (padByte = lastPadByte - *lastPadByte + 1; padByte < lastPadByte; padByte++)
  834. {
  835. if (*padByte != *lastPadByte)
  836. {
  837. result.DecryptSuccess = FALSE;
  838. return result;
  839. }
  840. }
  841. // Add size of Version, KmsPIDLen and variable size PID
  842. DWORD pidSize = LE32(((RESPONSE_V6*)response)->ResponseBase.PIDSize);
  843. copySize1 +=
  844. V6_UNENCRYPTED_SIZE +
  845. sizeof(response_v6->ResponseBase.PIDSize) +
  846. (pidSize <= PID_BUFFER_SIZE << 1 ? pidSize : PID_BUFFER_SIZE << 1);
  847. // Copy part 1 of response up to variable sized PID
  848. memcpy(response_v6, response, copySize1);
  849. // ensure PID is null terminated
  850. response_v6->ResponseBase.KmsPID[PID_BUFFER_SIZE - 1] = 0;
  851. // Copy part 2
  852. const size_t copySize2 = v6 ? V6_POST_EPID_SIZE : V5_POST_EPID_SIZE;
  853. memcpy(&response_v6->ResponseBase.CMID, response + copySize1, copySize2);
  854. // Decrypting the response is finished here. Now we check the results for validity
  855. // A basic client doesn't need the stuff below this comment but we want to use vlmcs
  856. // as a debug tool for KMS emulators.
  857. REQUEST_V6* request_v6 = (REQUEST_V6*)rawRequest;
  858. const DWORD decryptSize = sizeof(request_v6->IV) + sizeof(request_v6->RequestBase) + sizeof(request_v6->Pad);
  859. AesDecryptCbc(&Ctx, NULL, request_v6->IV, decryptSize);
  860. // Check that all version informations are the same
  861. result.VersionOK =
  862. request_v6->Version == response_v6->ResponseBase.Version &&
  863. request_v6->Version == response_v6->Version &&
  864. request_v6->Version == request_v6->RequestBase.Version;
  865. // Check Base Request
  866. result.PidLengthOK = checkPidLength(&((RESPONSE_V6*)response)->ResponseBase);
  867. result.TimeStampOK = !memcmp(&response_v6->ResponseBase.ClientTime, &request_v6->RequestBase.ClientTime, sizeof(FILETIME));
  868. result.ClientMachineIDOK = IsEqualGUID(&response_v6->ResponseBase.CMID, &request_v6->RequestBase.CMID);
  869. // Rebuild Random Key and Sha256 Hash
  870. BYTE HashVerify[sizeof(response_v6->Hash)];
  871. BYTE RandomKey[sizeof(response_v6->RandomXoredIVs)];
  872. memcpy(RandomKey, request_v6->IV, sizeof(RandomKey));
  873. XorBlock(response_v6->RandomXoredIVs, RandomKey);
  874. Sha256(RandomKey, sizeof(RandomKey), HashVerify);
  875. result.HashOK = !memcmp(response_v6->Hash, HashVerify, sizeof(HashVerify));
  876. // size before encryption (padding not included)
  877. result.correctResponseSize =
  878. (v6 ? sizeof(RESPONSE_V6) : sizeof(RESPONSE_V5))
  879. - sizeof(response_v6->ResponseBase.KmsPID)
  880. + LE32(response_v6->ResponseBase.PIDSize);
  881. // Version specific stuff
  882. if (v6)
  883. {
  884. // Copy the HwId
  885. memcpy(hwid, response_v6->HwId, sizeof(response_v6->HwId));
  886. // Verify the V6 specific part of the response
  887. result = VerifyResponseV6(result, response_v6, request_v6, response);
  888. }
  889. else // V5
  890. {
  891. // Verify the V5 specific part of the response
  892. result = VerifyResponseV5(result, request_v6, (RESPONSE_V5*)response_v6);
  893. }
  894. // padded size after encryption
  895. result.correctResponseSize += (~(result.correctResponseSize - sizeof(response_v6->ResponseBase.Version)) & 0xf) + 1;
  896. return result;
  897. }