ClientCDKey.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef _CLIENTCDKEY_H
  2. #define _CLIENTCDKEY_H
  3. // ClientCDKey
  4. // Class that implements WON CD-Key functionality for use by clients. Allows
  5. // conversion to/from human readable string. Implements persistance to the registry
  6. // in a secure manner. Implements a lightweight validation check of the key.
  7. #include "../common/won.h"
  8. #include <ostream>
  9. // Forwards
  10. namespace WONCrypt {
  11. class BFSymmetricKey;
  12. };
  13. // In WONCDKey namespace
  14. namespace WONCDKey
  15. {
  16. class ClientCDKey
  17. {
  18. public:
  19. // Standard constructor
  20. // ProductName is string representing product using the CDKey, (i.e., "Homeworld")
  21. // *IMPORTANT* product must be at least 3 chars for good security!!!
  22. explicit ClientCDKey(const std::string& theProductR);
  23. // Copy Constructor
  24. ClientCDKey(const ClientCDKey& theKeyR);
  25. // Destructor
  26. virtual ~ClientCDKey();
  27. // Operators
  28. ClientCDKey& operator=(const ClientCDKey& theKeyR);
  29. bool operator==(const ClientCDKey& theKeyR) const;
  30. bool operator!=(const ClientCDKey& theKeyR) const;
  31. // Product access. Careful setting product. Product is used to encrypt/decypt
  32. // the key to/from its binary form and for the Lightweight check.
  33. const std::string& GetProduct() const;
  34. void SetProduct(const std::string& theProductR);
  35. // Check two keys for equality
  36. virtual bool IsEqual(const ClientCDKey& theKeyR) const;
  37. // Check validity of a key, performs lightweight check if needed.
  38. virtual bool IsValid() const;
  39. // Is this a beta key?
  40. bool IsBeta() const;
  41. // Init from raw, unencrypted key. A return of true DOES NOT imply key is valid!
  42. // (Call IsValid() to verify validity).
  43. virtual bool Init(const __int64& theKeyR);
  44. // Initialize from Human readable string (dashes are optional):
  45. // CVCN-CVCN-CVCN-CVCN-NNNN
  46. // Returns true if init is successful, false if not. A return of true DOES NOT
  47. // imply key is valid! (Call IsValid() to verify validity).
  48. virtual bool Init(const std::string& theStrR);
  49. // Init from encrypted binary image (what is returned by AsBinary). A return of
  50. // true DOES NOT imply key is valid! (Call IsValid() to verify validity).
  51. virtual bool Init(const WONCommon::RawBuffer& theKeyR);
  52. // Fetch as raw, unencrypted (64bit) key
  53. __int64 AsRaw() const;
  54. // Fetch as human readable string. Validates key if needed if validate is true.
  55. // If Keys fails to validate, returns "#######INVALID KEY######"
  56. const std::string& AsString(bool validate=true) const;
  57. // Fetch as encrypted binary image. Valdates key if needed. Returns empty
  58. // buffer if key is not valid.
  59. const WONCommon::RawBuffer& AsBinary() const;
  60. // Load/Save key in registry securely (key is encrypted in the registry)
  61. virtual bool Load();
  62. virtual bool Save() const;
  63. // Clean key from registry
  64. virtual bool CleanReg();
  65. protected:
  66. // Types
  67. enum ValidityState { Unknown, Invalid, Valid };
  68. // Members
  69. std::string mProduct; // Product string
  70. unsigned char mLightCheck; // Lightweight checksum
  71. WONCommon::RawBuffer mKey; // Key value
  72. mutable ValidityState mValidity; // Current key validity
  73. // Buffers for string and binary versions of key
  74. mutable std::string mStrKey; // CD-Key as string
  75. mutable WONCommon::RawBuffer mBinKey; // CD-Key as encrypted binary
  76. // Perform the light validity check. Returns light checksum
  77. unsigned char LightValidityCheck() const;
  78. // Build human readable string rep of CDKey. Fills in mStrKey
  79. void BuildStringKey() const;
  80. // Extract LightCheck, and Key from a buffer
  81. void FieldsFromBuffer(const __int64& theBuf);
  82. // Build buffer from LightCheck and Key
  83. __int64 BufferFromFields() const;
  84. // Decypt mBin Key into buffer
  85. bool DecryptKey(__int64& theBufR);
  86. // Encrypt buffer into mBinKey
  87. bool EncryptKey(const __int64& theBufR) const;
  88. private:
  89. // Methods
  90. void BuildCChar(const __int64& theBuf, unsigned int& theOffset) const;
  91. void BuildVChar(const __int64& theBuf, unsigned int& theOffset) const;
  92. void BuildNChar(const __int64& theBuf, unsigned int& theOffset) const;
  93. void CreateSymmetricKey(WONCrypt::BFSymmetricKey& theSymKeyR) const;
  94. // Class methods
  95. static void RemoveSkipChars(string& theStrR);
  96. static char ValFromBits(const __int64& aBuf, unsigned int theOffset, unsigned int theBits);
  97. static bool ProcessCChar(__int64& theBuf, unsigned int& theOffset, char theChar);
  98. static bool ProcessVChar(__int64& theBuf, unsigned int& theOffset, char theChar);
  99. static bool ProcessNChar(__int64& theBuf, unsigned int& theOffset, char theChar);
  100. };
  101. // Inlines
  102. inline bool
  103. ClientCDKey::operator==(const ClientCDKey& theKeyR) const
  104. { return IsEqual(theKeyR); }
  105. inline bool
  106. ClientCDKey::operator!=(const ClientCDKey& theKeyR) const
  107. { return (! IsEqual(theKeyR)); }
  108. inline const std::string&
  109. ClientCDKey::GetProduct() const
  110. { return mProduct; }
  111. inline void
  112. ClientCDKey::SetProduct(const std::string& theProductR)
  113. { mProduct = theProductR; }
  114. inline __int64
  115. ClientCDKey::AsRaw() const
  116. { return BufferFromFields(); }
  117. }; //namespace WONCDKey
  118. // Output operators
  119. inline std::ostream&
  120. operator<<(std::ostream& os, const WONCDKey::ClientCDKey& theKeyR)
  121. { os << theKeyR.AsString(); return os; }
  122. #endif