Leaked source code of windows server 2003
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.

235 lines
5.4 KiB

  1. // Copyright (c) 2002 Microsoft Corporation
  2. //
  3. // Encrypted string class
  4. //
  5. // 18 March 2002
  6. #ifndef ENCRYPTEDSTRING_HPP_INCLUDED
  7. #define ENCRYPTEDSTRING_HPP_INCLUDED
  8. // A class that has a similar public interface as class Burnslib::String, but
  9. // is represented as an encrypted blob, produced by CryptProtectMemory.
  10. //
  11. // This class can be used to represent sensitive data like password strings
  12. // in-memory, instead of holding them as cleartext, which is a security hole
  13. // if the memory pages are swapped to disk, the machine is hibernated, etc.
  14. //
  15. // You should convert any cleartext data into an instance of this class, then
  16. // scribble out your cleartext source copy with SecureZeroMemory.
  17. class EncryptedString
  18. {
  19. public:
  20. // no string may be longer than this many characters, not including the
  21. // terminating null, or it will be truncated.
  22. static const size_t MAX_CHARACTER_COUNT = 1024 * 2 - 1;
  23. // constructs an empty string.
  24. explicit
  25. EncryptedString();
  26. // constructs a copy of an existing, already encoded string
  27. EncryptedString(const EncryptedString& rhs);
  28. // scribbles out the text, and deletes it.
  29. ~EncryptedString()
  30. {
  31. // when the object dies, it had better not have any outstanding
  32. // cleartext copies.
  33. ASSERT(cleartextCopyCount == 0);
  34. Reset();
  35. }
  36. // disposes of the encrypted text, and sets the string to be empty.
  37. void
  38. Clear()
  39. {
  40. // when the object dies, it had better not have any outstanding
  41. // cleartext copies.
  42. ASSERT(cleartextCopyCount == 0);
  43. Reset();
  44. }
  45. // Scribbles out and de-allocates a clear text copy of the string. The
  46. // caller is required to balance each call to GetClearTextCopy with a call
  47. // to DestroyClearTextCopy lest he leak memory and leave cleartext hanging
  48. // around.
  49. //
  50. // cleartext - the same pointer returned by a previous call to
  51. // GetClearTextCopy on the same instance.
  52. void
  53. DestroyClearTextCopy(WCHAR* cleartext) const;
  54. // thrown by GetClearTextCopy if decryption fails.
  55. class DecryptionFailureException
  56. {
  57. public:
  58. DecryptionFailureException()
  59. {
  60. }
  61. };
  62. // Extracts the decoded cleartext representation of the text, including
  63. // null terminator. The caller must invoke DestroyClearTextCopy on the
  64. // result, even if the result is null.
  65. //
  66. // Throws a DecryptionFailureException on failure.
  67. //
  68. // Example:
  69. // WCHAR* cleartext = encoded.GetDecodedCopy();
  70. // if (cleartext)
  71. // {
  72. // // ... use the cleartext
  73. // }
  74. // encoded.DestroyClearTextCopy();
  75. WCHAR*
  76. GetClearTextCopy() const;
  77. // Returns true if the string is zero-length, false if not.
  78. bool
  79. IsEmpty() const
  80. {
  81. return (GetLength() == 0);
  82. }
  83. // Sets the contents of self to the encoded representation of the
  84. // cleartext, replacing the previous value of self. The encoded
  85. // representation will be the same length, in characters, as the
  86. // cleartext.
  87. //
  88. // clearText - in, un-encoded text to be encoded. May be empty string,
  89. // but not a null pointer.
  90. void
  91. Encrypt(const WCHAR* cleartext);
  92. // Returns the length, in unicode characters, of the cleartext, not
  93. // including the null terminator.
  94. size_t
  95. GetLength() const;
  96. // Replaces the contents of self with a copy of the contents of rhs.
  97. // Returns *this.
  98. const EncryptedString&
  99. operator= (const EncryptedString& rhs);
  100. // Compares the cleartext representations of self and rhs, and returns
  101. // true if they are lexicographically the same: the lengths are the same
  102. // and all the characters are the same.
  103. bool
  104. operator== (const EncryptedString& rhs) const;
  105. bool
  106. operator!= (const EncryptedString& rhs) const
  107. {
  108. return !(operator==(rhs));
  109. }
  110. private:
  111. void
  112. Clone(const EncryptedString& rhs);
  113. void
  114. Init(const WCHAR* clearText);
  115. WCHAR*
  116. InternalDecrypt() const;
  117. void
  118. InternalDestroy(WCHAR* cleartext) const;
  119. void
  120. Reset();
  121. // We deliberately do not implement conversion to or from WCHAR* or
  122. // String. This is to force the user of the class to be very deliberate
  123. // about decoding the string. class String is a copy-on-write shared
  124. // reference implementation, and we don't want to make it easy to create
  125. // "hidden" copies of cleartext, or move from one representation to
  126. // another, or accidentally get a string filled with encrypted text.
  127. // deliberately commented out
  128. // explicit
  129. // EncryptedString(const String& cleartext);
  130. // deliberately commented out
  131. // operator WCHAR* ();
  132. // operator String ();
  133. size_t clearTextLength;
  134. // In the course of en[de]crypting, and assigning to the instance, we
  135. // may reallocate the memory pointed to here, but logically the string
  136. // is still "const"
  137. mutable WCHAR* cypherText;
  138. // a logically const instance may have cleartext copies made
  139. mutable int cleartextCopyCount;
  140. // indicates that the encryption worked.
  141. bool isEncrypted;
  142. };
  143. #endif // ENCRYPTEDSTRING_HPP_INCLUDED