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
7.3 KiB

  1. // Copyright (c) 1998 - 1999 Microsoft Corporation
  2. #include "stdafx.h"
  3. #include "license.h"
  4. #include "tlsapi.h"
  5. #include "tlsapip.h"
  6. #include "secstore.h"
  7. #include "..\conv.h"
  8. #define LICENSING_TIME_BOMB L"TIMEBOMB_832cc540-3244-11d2-b416-00c04fa30cc4"
  9. #define RTMLICENSING_TIME_BOMB L"RTMTSTB_832cc540-3244-11d2-b416-00c04fa30cc4"
  10. #define LICENSING_TIME_BOMB_5_0 L"TIMEBOMB_832cc540-3244-11d2-b416-00c04fa30cc4"
  11. #define RTMLICENSING_TIME_BOMB_5_0 L"RTMTSTB_832cc540-3244-11d2-b416-00c04fa30cc4"
  12. // L$ means only readable from the local machine
  13. #define BETA_LICENSING_TIME_BOMB_5_1 L"L$BETA3TIMEBOMB_1320153D-8DA3-4e8e-B27B-0D888223A588"
  14. #define RTM_LICENSING_TIME_BOMB_5_1 L"L$RTMTIMEBOMB_1320153D-8DA3-4e8e-B27B-0D888223A588"
  15. #define BETA_LICENSING_TIME_BOMB_LATEST_VERSION BETA_LICENSING_TIME_BOMB_5_1
  16. #define RTM_LICENSING_TIME_BOMB_LATEST_VERSION RTM_LICENSING_TIME_BOMB_5_1
  17. #define X509_CERT_PRIVATE_KEY_NAME \
  18. L"L$HYDRAENCKEY_dd2d98db-2316-11d2-b414-00c04fa30cc4"
  19. ///////////////////////////////////////////////////////////////////
  20. typedef BOOL
  21. (* TLSISBETANTSERVER)();
  22. BOOL FIsBetaSystem ()
  23. {
  24. HMODULE hmodTlsAPI = LoadLibrary( _T("mstlsapi.dll") );
  25. if (hmodTlsAPI)
  26. {
  27. TLSISBETANTSERVER pfnTLSIsBetaNTServer;
  28. BOOL fBeta = FALSE;
  29. pfnTLSIsBetaNTServer = (TLSISBETANTSERVER) GetProcAddress(hmodTlsAPI,"TLSIsBetaNTServer");
  30. if (NULL != pfnTLSIsBetaNTServer)
  31. {
  32. fBeta = pfnTLSIsBetaNTServer();
  33. }
  34. FreeLibrary(hmodTlsAPI);
  35. return fBeta;
  36. }
  37. else
  38. {
  39. return FALSE;
  40. }
  41. }
  42. TCHAR *IsBetaSystem ()
  43. {
  44. return (FIsBetaSystem() ? _T("Its Beta") : _T("Its RTM"));
  45. }
  46. BOOL HasLicenceGracePeriodExpired ()
  47. {
  48. DWORD status;
  49. DWORD cbByte;
  50. PBYTE pbByte = NULL;
  51. LPWSTR szTimeBombKey;
  52. FILETIME timebomb;
  53. FILETIME filetimeCurrent;
  54. DWORD dwVersion;
  55. dwVersion = GetVersion();
  56. if ((dwVersion & 0x80000000)
  57. || (LOBYTE(LOWORD(dwVersion)) <= 4))
  58. {
  59. cout << endl << " Pre-Windows 2000. No grace period" << endl;
  60. return FALSE;
  61. }
  62. else if ((LOBYTE(LOWORD(dwVersion)) == 5)
  63. && (HIBYTE(LOWORD(dwVersion)) == 0))
  64. {
  65. // Windows 2000
  66. if (FIsBetaSystem())
  67. szTimeBombKey = LICENSING_TIME_BOMB_5_0;
  68. else
  69. szTimeBombKey = RTMLICENSING_TIME_BOMB_5_0;
  70. }
  71. else if ((LOBYTE(LOWORD(dwVersion)) == 5)
  72. && (HIBYTE(LOWORD(dwVersion)) == 1))
  73. {
  74. // Whistler
  75. if (FIsBetaSystem())
  76. szTimeBombKey = BETA_LICENSING_TIME_BOMB_5_1;
  77. else
  78. szTimeBombKey = RTM_LICENSING_TIME_BOMB_5_1;
  79. }
  80. else
  81. {
  82. cout << endl << " Unknown OS. Assume no grace period" << endl;
  83. return FALSE;
  84. }
  85. status = RetrieveKey(
  86. szTimeBombKey,
  87. &pbByte,
  88. &cbByte
  89. );
  90. if(status == ERROR_SUCCESS && pbByte)
  91. {
  92. timebomb = *(FILETIME *)pbByte;
  93. GetSystemTimeAsFileTime(&filetimeCurrent);
  94. // yes license has expired if filetimeCurrent >= timebomb
  95. return (CompareFileTime(&timebomb, &filetimeCurrent) < 1);
  96. }
  97. else
  98. {
  99. // It hasn't been set yet, so we're not expired
  100. return FALSE;
  101. }
  102. }
  103. static gdwCount = 0;
  104. BOOL ServerEnumCallBack(
  105. IN TLS_HANDLE hHandle,
  106. IN LPCTSTR pszServerName,
  107. IN HANDLE /* dwUserData */)
  108. {
  109. USES_CONVERSION;
  110. if (hHandle)
  111. {
  112. cout << endl << " Found #" << ++gdwCount << ":"<< T2A(pszServerName);
  113. }
  114. return 0;
  115. }
  116. typedef HANDLE
  117. (* TLSCONNECTTOANYLSSERVER)(
  118. DWORD dwTimeOut
  119. );
  120. typedef DWORD
  121. (* ENUMERATETLSSERVERNEW)(
  122. TLSENUMERATECALLBACK fCallBack,
  123. HANDLE dwUserData,
  124. DWORD dwTimeOut,
  125. BOOL fRegOnly
  126. );
  127. typedef DWORD
  128. (* ENUMERATETLSSERVEROLD)(
  129. LPCTSTR szDomain,
  130. LPCTSTR szScope,
  131. DWORD dwPlatformType,
  132. TLSENUMERATECALLBACK fCallBack,
  133. HANDLE dwUserData,
  134. DWORD dwTimeOut,
  135. BOOL fRegOnly
  136. );
  137. BOOL EnumerateLicenseServers ()
  138. {
  139. HMODULE hmodTlsAPI = LoadLibrary( _T("mstlsapi.dll") );
  140. if (hmodTlsAPI)
  141. {
  142. DWORD dwResult;
  143. // load TLSShutdown to see if we have the new APIs
  144. if (NULL == GetProcAddress(hmodTlsAPI,"TLSShutdown"))
  145. {
  146. ENUMERATETLSSERVEROLD pfnEnumerateTlsServer;
  147. pfnEnumerateTlsServer = (ENUMERATETLSSERVEROLD) GetProcAddress(hmodTlsAPI,"EnumerateTlsServer");
  148. if (NULL != pfnEnumerateTlsServer)
  149. {
  150. dwResult = pfnEnumerateTlsServer(
  151. NULL,
  152. NULL,
  153. LSKEYPACKPLATFORMTYPE_UNKNOWN,
  154. ServerEnumCallBack,
  155. 0,
  156. 0,
  157. FALSE);
  158. }
  159. else
  160. {
  161. cout << endl << " Failed to GetProcAddress,ErrorCode = " << GetLastError() << endl;
  162. return FALSE;
  163. }
  164. }
  165. else
  166. {
  167. if (NULL != GetProcAddress(hmodTlsAPI,"TLSGetSupportFlags"))
  168. {
  169. // Use newer discovery function
  170. TLSCONNECTTOANYLSSERVER pfnTLSConnectToAnyLsServer;
  171. TLS_HANDLE hServer;
  172. pfnTLSConnectToAnyLsServer = (TLSCONNECTTOANYLSSERVER) GetProcAddress(hmodTlsAPI,"TLSConnectToAnyLsServer");
  173. if (NULL != pfnTLSConnectToAnyLsServer)
  174. {
  175. hServer = pfnTLSConnectToAnyLsServer(INFINITE);
  176. if (NULL != hServer)
  177. {
  178. return TRUE;
  179. }
  180. else
  181. {
  182. return FALSE;
  183. }
  184. }
  185. else
  186. {
  187. cout << endl << " Failed to GetProcAddress TLSConnectToAnyLsServer,ErrorCode = " << GetLastError() << endl;
  188. return FALSE;
  189. }
  190. }
  191. else
  192. {
  193. ENUMERATETLSSERVERNEW pfnEnumerateTlsServer;
  194. pfnEnumerateTlsServer = (ENUMERATETLSSERVERNEW) GetProcAddress(hmodTlsAPI,"EnumerateTlsServer");
  195. if (NULL != pfnEnumerateTlsServer)
  196. {
  197. dwResult = pfnEnumerateTlsServer(
  198. ServerEnumCallBack,
  199. 0,
  200. 0,
  201. FALSE);
  202. }
  203. else
  204. {
  205. cout << endl << " Failed to GetProcAddress,ErrorCode = " << GetLastError() << endl;
  206. return FALSE;
  207. }
  208. }
  209. }
  210. FreeLibrary(hmodTlsAPI);
  211. if (dwResult != NO_ERROR)
  212. cout << endl << " Failed to EnumerateTlsServer,ErrorCode = " << dwResult << endl;
  213. if (gdwCount > 0)
  214. cout << endl;
  215. return gdwCount > 0;
  216. }
  217. else
  218. {
  219. cout << endl << " Failed to load mstlsapi.dll,ErrorCode = " << GetLastError() << endl;
  220. return FALSE;
  221. }
  222. }