Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
9.6 KiB

  1. #include <windows.h>
  2. #include <shellapi.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <stdlib.h>
  6. #include <rpc.h>
  7. #include <time.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <crt/io.h>
  12. #include <wincrypt.h>
  13. #include "license.h"
  14. #include "certutil.h"
  15. BYTE g_pSecretKey[1024];
  16. DWORD g_cbSecretKey=sizeof(g_pSecretKey);
  17. #define SAFESTRCPY(dest, source) \
  18. _tcsncpy(dest, source, min(_tcslen(source), sizeof(dest) - sizeof(TCHAR)))
  19. #define COMMON_STORE TEXT("Software\\Microsoft\\MSLicensing\\Store")
  20. #define LICENSE_VAL TEXT("ClientLicense")
  21. LPCTSTR
  22. FileTimeText(
  23. FILETIME *pft )
  24. {
  25. static TCHAR buf[80];
  26. FILETIME ftLocal;
  27. struct tm ctm;
  28. SYSTEMTIME st;
  29. FileTimeToLocalFileTime( pft, &ftLocal );
  30. if( FileTimeToSystemTime( &ftLocal, &st ) )
  31. {
  32. ctm.tm_sec = st.wSecond;
  33. ctm.tm_min = st.wMinute;
  34. ctm.tm_hour = st.wHour;
  35. ctm.tm_mday = st.wDay;
  36. ctm.tm_mon = st.wMonth-1;
  37. ctm.tm_year = st.wYear-1900;
  38. ctm.tm_wday = st.wDayOfWeek;
  39. ctm.tm_yday = 0;
  40. ctm.tm_isdst = 0;
  41. _tcscpy(buf, _wasctime(&ctm));
  42. buf[_tcslen(buf)-1] = 0;
  43. }
  44. else
  45. {
  46. swprintf( buf,
  47. L"<FILETIME %08lX:%08lX>",
  48. pft->dwHighDateTime,
  49. pft->dwLowDateTime );
  50. }
  51. return buf;
  52. }
  53. void MyReportError(DWORD errCode)
  54. {
  55. DWORD dwRet;
  56. LPTSTR lpszTemp = NULL;
  57. dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  58. NULL,
  59. errCode,
  60. LANG_NEUTRAL,
  61. (LPTSTR)&lpszTemp,
  62. 0,
  63. NULL);
  64. if(dwRet != 0)
  65. {
  66. _tprintf(_TEXT("Error : %s (%d)\n"), lpszTemp, errCode);
  67. if(lpszTemp)
  68. LocalFree((HLOCAL)lpszTemp);
  69. }
  70. return;
  71. }
  72. //------------------------------------------------------------------------------------------
  73. void DumpLicensedProduct(PLICENSEDPRODUCT pLicensedInfo, BOOL fAdvanced)
  74. {
  75. if(fAdvanced == TRUE)
  76. {
  77. _tprintf(_TEXT("TS Certificate Version - 0x%08x\n"), pLicensedInfo->dwLicenseVersion);
  78. _tprintf(_TEXT("Licensed Product\n"));
  79. _tprintf(_TEXT("\tHWID - 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x\n"),
  80. pLicensedInfo->Hwid.dwPlatformID,
  81. pLicensedInfo->Hwid.Data1,
  82. pLicensedInfo->Hwid.Data2,
  83. pLicensedInfo->Hwid.Data3,
  84. pLicensedInfo->Hwid.Data4);
  85. _tprintf(_TEXT("\tClient Platform ID - 0x%08x\n"), pLicensedInfo->LicensedProduct.dwPlatformID );
  86. _tprintf(_TEXT("\tCompany Name - %s\n"), pLicensedInfo->LicensedProduct.pProductInfo->pbCompanyName);
  87. }
  88. _tprintf(_TEXT("Issuer - %s\n"), pLicensedInfo->szIssuer);
  89. _tprintf(_TEXT("Scope - %s\n"), pLicensedInfo->szIssuerScope);
  90. if(pLicensedInfo->szLicensedClient)
  91. _tprintf(_TEXT("Issued to machine - %s\n"), pLicensedInfo->szLicensedClient);
  92. if(pLicensedInfo->szLicensedUser)
  93. _tprintf(_TEXT("Issued to user - %s\n"), pLicensedInfo->szLicensedUser);
  94. if(fAdvanced == TRUE)
  95. {
  96. _tprintf(_TEXT("\tTS Locale ID - 0x%08x\n"), pLicensedInfo->LicensedProduct.dwLanguageID);
  97. }
  98. _tprintf(_TEXT("\tLicense ID - %s\n"), pLicensedInfo->LicensedProduct.pProductInfo->pbProductID);
  99. for(DWORD i=0; i < pLicensedInfo->dwNumLicensedVersion; i++)
  100. {
  101. if(fAdvanced == TRUE)
  102. {
  103. _tprintf(_TEXT("Licensed Product Version %04d.%04d, Flag 0x%08x\n"),
  104. pLicensedInfo->pLicensedVersion[i].wMajorVersion,
  105. pLicensedInfo->pLicensedVersion[i].wMinorVersion,
  106. pLicensedInfo->pLicensedVersion[i].dwFlags);
  107. }
  108. if (pLicensedInfo->pLicensedVersion[i].dwFlags & LICENSED_VERSION_TEMPORARY)
  109. {
  110. _tprintf(_TEXT("Temporary\t"));
  111. }
  112. else
  113. {
  114. _tprintf(_TEXT("Permanent\t"));
  115. }
  116. if (pLicensedInfo->pLicensedVersion[i].dwFlags & LICENSED_VERSION_RTM)
  117. {
  118. _tprintf(_TEXT("RTM\t"));
  119. }
  120. else
  121. {
  122. _tprintf(_TEXT("Beta\t"));
  123. }
  124. _tprintf(_TEXT("\n"));
  125. }
  126. _tprintf(L"Valid from - %x %x %s\n",
  127. pLicensedInfo->NotBefore.dwHighDateTime,
  128. pLicensedInfo->NotBefore.dwLowDateTime,
  129. FileTimeText(&pLicensedInfo->NotBefore));
  130. _tprintf(L"Expires on - %x %x %s\n",
  131. pLicensedInfo->NotAfter.dwHighDateTime,
  132. pLicensedInfo->NotAfter.dwLowDateTime,
  133. FileTimeText(&pLicensedInfo->NotAfter));
  134. }
  135. int __cdecl wmain(int argc, LPCWSTR argv[])
  136. {
  137. HKEY hKey, hKeyStore;
  138. LPBYTE lpKeyValue=NULL;
  139. DWORD cbKeyValue;
  140. DWORD dwStatus;
  141. DWORD dwKeyType;
  142. TCHAR lpName[128];
  143. DWORD cName;
  144. FILETIME ftWrite;
  145. LICENSEDPRODUCT LicensedInfo[10];
  146. DWORD dwNum;
  147. ULARGE_INTEGER SerialNumber;
  148. BYTE pbKey[1024];
  149. DWORD cbKey=1024;
  150. LICENSE_STATUS lstatus;
  151. BOOL fAdvanced = FALSE;
  152. if(argc > 2 || ((argc == 2) && (_wcsicmp(argv[1], L"/a") != 0)) )
  153. {
  154. _tprintf(_T("Usage: tsctst /A\n"));
  155. _tprintf(_T("/A \t Displays advanced license information in addition to default basics.\n"));
  156. return 0;
  157. }
  158. if(argc == 2)
  159. {
  160. fAdvanced = TRUE;
  161. }
  162. dwStatus=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  163. COMMON_STORE,
  164. 0,
  165. KEY_READ,
  166. &hKey);
  167. if(dwStatus != ERROR_SUCCESS)
  168. {
  169. _tprintf(_TEXT("Can't open key - %ld\n"), dwStatus);
  170. return 0;
  171. }
  172. LSInitCertutilLib( 0 );
  173. LicenseGetSecretKey( &cbKey, pbKey );
  174. for (DWORD dwKey = 0; dwStatus == ERROR_SUCCESS; dwKey++)
  175. {
  176. cName = sizeof(lpName) / sizeof(TCHAR);
  177. dwStatus=RegEnumKeyEx(hKey,
  178. dwKey,
  179. lpName,
  180. &cName,
  181. NULL,
  182. NULL,
  183. NULL,
  184. &ftWrite);
  185. if (ERROR_SUCCESS == dwStatus)
  186. {
  187. if(!cName)
  188. {
  189. _tprintf(_TEXT("Can't open key - %ld\n"), dwStatus);
  190. goto cleanup;
  191. }
  192. dwStatus = RegOpenKeyEx(hKey,
  193. lpName,
  194. 0,
  195. KEY_READ,
  196. &hKeyStore);
  197. if(dwStatus != ERROR_SUCCESS)
  198. {
  199. _tprintf(_TEXT("can't open store %s - %d\n"), lpName, dwStatus);
  200. continue;
  201. }
  202. cbKeyValue=0;
  203. dwStatus = RegQueryValueEx(hKeyStore,
  204. LICENSE_VAL,
  205. NULL,
  206. &dwKeyType,
  207. NULL,
  208. &cbKeyValue);
  209. if(dwStatus != ERROR_SUCCESS)
  210. {
  211. _tprintf(_TEXT("can't get value size - %d\n"), dwStatus);
  212. RegCloseKey(hKeyStore);
  213. continue;
  214. }
  215. lpKeyValue = (LPBYTE)malloc(cbKeyValue);
  216. if(!lpKeyValue)
  217. {
  218. _tprintf(_TEXT("Can't allocate %d bytes\n"), cbKeyValue);
  219. RegCloseKey(hKeyStore);
  220. goto cleanup;
  221. }
  222. dwStatus = RegQueryValueEx(hKeyStore,
  223. LICENSE_VAL,
  224. NULL,
  225. &dwKeyType,
  226. lpKeyValue,
  227. &cbKeyValue);
  228. if(dwStatus != ERROR_SUCCESS)
  229. {
  230. _tprintf(_TEXT("can't get value - %d\n"), dwStatus);
  231. free(lpKeyValue);
  232. RegCloseKey(hKeyStore);
  233. continue;
  234. }
  235. memset(LicensedInfo, 0, sizeof(LicensedInfo));
  236. dwNum = 10;
  237. lstatus = LSVerifyDecodeClientLicense(lpKeyValue, cbKeyValue, pbKey, cbKey, &dwNum, LicensedInfo);
  238. if (lstatus == LICENSE_STATUS_OK)
  239. {
  240. for (DWORD i = 0; i < dwNum; i++)
  241. {
  242. _tprintf(_TEXT("\n*** License # %d ***\n"), i+1);
  243. DumpLicensedProduct(LicensedInfo+i, fAdvanced);
  244. }
  245. LSFreeLicensedProduct(LicensedInfo);
  246. }
  247. else
  248. {
  249. _tprintf(_TEXT("can't decode license - %d, %d\n"), lstatus, GetLastError());
  250. free(lpKeyValue);
  251. RegCloseKey(hKeyStore);
  252. continue;
  253. }
  254. free(lpKeyValue);
  255. RegCloseKey(hKeyStore);
  256. _tprintf(_TEXT("\n\n"));
  257. }//endif
  258. else
  259. {
  260. if(dwKey == 0)
  261. {
  262. _tprintf(_TEXT("\nThe client does not have a license\n"));
  263. }
  264. }
  265. }//endfor
  266. cleanup:
  267. RegCloseKey(hKey);
  268. LSShutdownCertutilLib();
  269. return 0;
  270. }