Source code of Windows XP (NT5)
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.

287 lines
8.5 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. LPCSTR
  22. FileTimeText(
  23. FILETIME *pft )
  24. {
  25. static char 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. strcpy(buf, asctime(&ctm));
  42. buf[strlen(buf)-1] = 0;
  43. }
  44. else
  45. {
  46. sprintf( buf,
  47. "<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)
  74. {
  75. _tprintf(_TEXT("Version - 0x%08x\n"), pLicensedInfo->dwLicenseVersion);
  76. _tprintf(_TEXT("Quantity - %d\n"), pLicensedInfo->dwQuantity);
  77. _tprintf(_TEXT("Issuer - %s\n"), pLicensedInfo->szIssuer);
  78. _tprintf(_TEXT("Scope - %s\n"), pLicensedInfo->szIssuerScope);
  79. if(pLicensedInfo->szLicensedClient)
  80. _tprintf(_TEXT("Issued to machine - %s\n"), pLicensedInfo->szLicensedClient);
  81. if(pLicensedInfo->szLicensedUser)
  82. _tprintf(_TEXT("Issued to user - %s\n"), pLicensedInfo->szLicensedUser);
  83. _tprintf(_TEXT("Licensed Product\n"));
  84. _tprintf(_TEXT("\tHWID - 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x\n"),
  85. pLicensedInfo->Hwid.dwPlatformID,
  86. pLicensedInfo->Hwid.Data1,
  87. pLicensedInfo->Hwid.Data2,
  88. pLicensedInfo->Hwid.Data3,
  89. pLicensedInfo->Hwid.Data4);
  90. _tprintf(_TEXT("\tlanguage ID - 0x%08x\n"), pLicensedInfo->LicensedProduct.dwLanguageID);
  91. _tprintf(_TEXT("\tPlatform ID - 0x%08x\n"), pLicensedInfo->LicensedProduct.dwPlatformID);
  92. _tprintf(_TEXT("\tProduct Version - 0x%08x\n"), pLicensedInfo->LicensedProduct.pProductInfo->dwVersion);
  93. _tprintf(_TEXT("\tCompany Name - %s\n"), pLicensedInfo->LicensedProduct.pProductInfo->pbCompanyName);
  94. _tprintf(_TEXT("\tProduct ID - %s\n"), pLicensedInfo->LicensedProduct.pProductInfo->pbProductID);
  95. for(DWORD i=0; i < pLicensedInfo->dwNumLicensedVersion; i++)
  96. {
  97. _tprintf(_TEXT("Licensed Product Version %04d.%04d, Flag 0x%08x\n"),
  98. pLicensedInfo->pLicensedVersion[i].wMajorVersion,
  99. pLicensedInfo->pLicensedVersion[i].wMinorVersion,
  100. pLicensedInfo->pLicensedVersion[i].dwFlags);
  101. if (pLicensedInfo->pLicensedVersion[i].dwFlags & LICENSED_VERSION_TEMPORARY)
  102. {
  103. _tprintf(_TEXT("Temporary\t"));
  104. }
  105. else
  106. {
  107. _tprintf(_TEXT("Permanent\t"));
  108. }
  109. if (pLicensedInfo->pLicensedVersion[i].dwFlags & LICENSED_VERSION_RTM)
  110. {
  111. _tprintf(_TEXT("RTM\t"));
  112. }
  113. else
  114. {
  115. _tprintf(_TEXT("Beta\t"));
  116. }
  117. _tprintf(_TEXT("\n"));
  118. }
  119. printf("Not Before - %x %x %s\n",
  120. pLicensedInfo->NotBefore.dwHighDateTime,
  121. pLicensedInfo->NotBefore.dwLowDateTime,
  122. FileTimeText(&pLicensedInfo->NotBefore));
  123. printf("Not After - %x %x %s\n",
  124. pLicensedInfo->NotAfter.dwHighDateTime,
  125. pLicensedInfo->NotAfter.dwLowDateTime,
  126. FileTimeText(&pLicensedInfo->NotAfter));
  127. }
  128. int __cdecl main(int argc, char *argv[])
  129. {
  130. HKEY hKey, hKeyStore;
  131. LPBYTE lpKeyValue=NULL;
  132. DWORD cbKeyValue;
  133. DWORD dwStatus;
  134. DWORD dwKeyType;
  135. TCHAR lpName[128];
  136. DWORD cName;
  137. FILETIME ftWrite;
  138. LICENSEDPRODUCT LicensedInfo[10];
  139. DWORD dwNum;
  140. ULARGE_INTEGER SerialNumber;
  141. BYTE pbKey[1024];
  142. DWORD cbKey=1024;
  143. LICENSE_STATUS lstatus;
  144. dwStatus=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  145. COMMON_STORE,
  146. 0,
  147. KEY_READ,
  148. &hKey);
  149. if(dwStatus != ERROR_SUCCESS)
  150. {
  151. _tprintf(_TEXT("Can't open key - %ld\n"), dwStatus);
  152. return 0;
  153. }
  154. LSInitCertutilLib( 0 );
  155. LicenseGetSecretKey( &cbKey, pbKey );
  156. for (DWORD dwKey = 0; dwStatus == ERROR_SUCCESS; dwKey++)
  157. {
  158. cName = sizeof(lpName) / sizeof(TCHAR);
  159. dwStatus=RegEnumKeyEx(hKey,
  160. dwKey,
  161. lpName,
  162. &cName,
  163. NULL,
  164. NULL,
  165. NULL,
  166. &ftWrite);
  167. if (ERROR_SUCCESS == dwStatus)
  168. {
  169. dwStatus = RegOpenKeyEx(hKey,
  170. lpName,
  171. 0,
  172. KEY_READ,
  173. &hKeyStore);
  174. if(dwStatus != ERROR_SUCCESS)
  175. {
  176. _tprintf(_TEXT("can't open store - %d\n"), dwStatus);
  177. goto cleanup;
  178. }
  179. cbKeyValue=0;
  180. dwStatus = RegQueryValueEx(hKeyStore,
  181. LICENSE_VAL,
  182. NULL,
  183. &dwKeyType,
  184. NULL,
  185. &cbKeyValue);
  186. if(dwStatus != ERROR_SUCCESS)
  187. {
  188. _tprintf(_TEXT("can't get value size - %d\n"), dwStatus);
  189. RegCloseKey(hKeyStore);
  190. goto cleanup;
  191. }
  192. lpKeyValue = (LPBYTE)malloc(cbKeyValue);
  193. if(!lpKeyValue)
  194. {
  195. _tprintf(_TEXT("Can't allocate %d bytes\n"), cbKeyValue);
  196. RegCloseKey(hKeyStore);
  197. goto cleanup;
  198. }
  199. dwStatus = RegQueryValueEx(hKeyStore,
  200. LICENSE_VAL,
  201. NULL,
  202. &dwKeyType,
  203. lpKeyValue,
  204. &cbKeyValue);
  205. if(dwStatus != ERROR_SUCCESS)
  206. {
  207. _tprintf(_TEXT("can't get value - %d\n"), dwStatus);
  208. free(lpKeyValue);
  209. RegCloseKey(hKeyStore);
  210. goto cleanup;
  211. }
  212. memset(LicensedInfo, 0, sizeof(LicensedInfo));
  213. dwNum = 10;
  214. lstatus = LSVerifyDecodeClientLicense(lpKeyValue, cbKeyValue, pbKey, cbKey, &dwNum, LicensedInfo);
  215. if (lstatus == LICENSE_STATUS_OK)
  216. {
  217. for (DWORD i = 0; i < dwNum; i++)
  218. {
  219. _tprintf(_TEXT("\n*** License # %d ***\n"), i+1);
  220. DumpLicensedProduct(LicensedInfo+i);
  221. }
  222. LSFreeLicensedProduct(LicensedInfo);
  223. }
  224. else
  225. {
  226. _tprintf(_TEXT("can't decode license - %d, %d\n"), lstatus, GetLastError());
  227. free(lpKeyValue);
  228. RegCloseKey(hKeyStore);
  229. goto cleanup;
  230. }
  231. free(lpKeyValue);
  232. RegCloseKey(hKeyStore);
  233. _tprintf(_TEXT("\n\n"));
  234. }
  235. }
  236. cleanup:
  237. RegCloseKey(hKey);
  238. LSShutdownCertutilLib();
  239. return 0;
  240. }