Team Fortress 2 Source Code as on 22/4/2020
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.

111 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Platform level security functions.
  4. //
  5. //=============================================================================//
  6. // Uncomment the following line to require the prescence of a hardware key.
  7. //#define REQUIRE_HARDWARE_KEY
  8. #include "pch_tier0.h"
  9. #if defined( _WIN32 ) && !defined( _X360 )
  10. #define WIN_32_LEAN_AND_MEAN
  11. #include <windows.h>
  12. #endif
  13. #include "tier0/platform.h"
  14. #include "tier0/vcrmode.h"
  15. #include "tier0/memalloc.h"
  16. #ifdef REQUIRE_HARDWARE_KEY
  17. // This is the previous key, which was compromised.
  18. //#define VALVE_DESKEY_ID "uW" // Identity Password, Uniquely identifies HL2 keys
  19. #define VALVE_DESKEY_ID "u$" // Identity Password, Uniquely identifies HL2 keys
  20. // Include the key's API:
  21. #include "DESKey/algo.h"
  22. #include "DESKey/dk2win32.h"
  23. #pragma comment(lib, "DESKey/algo32.lib" )
  24. #pragma comment(lib, "DESKey/dk2win32.lib" )
  25. #endif
  26. bool Plat_VerifyHardwareKey()
  27. {
  28. #ifdef REQUIRE_HARDWARE_KEY
  29. // Ensure that a key with our ID exists:
  30. if ( FindDK2( VALVE_DESKEY_ID, NULL ) )
  31. return true;
  32. return false;
  33. #else
  34. return true;
  35. #endif
  36. }
  37. bool Plat_VerifyHardwareKeyDriver()
  38. {
  39. #ifdef REQUIRE_HARDWARE_KEY
  40. // Ensure that the driver is at least installed:
  41. return DK2DriverInstalled() != 0;
  42. #else
  43. return true;
  44. #endif
  45. }
  46. bool Plat_VerifyHardwareKeyPrompt()
  47. {
  48. #ifdef REQUIRE_HARDWARE_KEY
  49. if ( !DK2DriverInstalled() )
  50. {
  51. if( IDCANCEL == MessageBox( NULL, "No drivers detected for the hardware key, please install them and re-run the application.\n", "No Driver Detected", MB_OKCANCEL ) )
  52. {
  53. return false;
  54. }
  55. }
  56. while ( !Plat_VerifyHardwareKey() )
  57. {
  58. if ( IDCANCEL == MessageBox( NULL, "Please insert the hardware key and hit 'ok'.\n", "Insert Hardware Key", MB_OKCANCEL ) )
  59. {
  60. return false;
  61. }
  62. for ( int i=0; i < 2; i++ )
  63. {
  64. // Is the key in now?
  65. if( Plat_VerifyHardwareKey() )
  66. {
  67. return true;
  68. }
  69. // Sleep 2 / 3 of a second before trying again, in case the os recognizes the key slightly after it's being inserted:
  70. Sleep(666);
  71. }
  72. }
  73. return true;
  74. #else
  75. return true;
  76. #endif
  77. }
  78. bool Plat_FastVerifyHardwareKey()
  79. {
  80. #ifdef REQUIRE_HARDWARE_KEY
  81. static int nIterations = 0;
  82. nIterations++;
  83. if( nIterations > 100 )
  84. {
  85. nIterations = 0;
  86. return Plat_VerifyHardwareKey();
  87. }
  88. return true;
  89. #else
  90. return true;
  91. #endif
  92. }