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.

125 lines
2.2 KiB

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